| 1 | // conflict.mjs — the inline choice (spec §6.5, N3). When a teammate |
| 2 | // saved the same blocks this tab changed, autosave's reply carries each |
| 3 | // conflicting hunk: its blocks in the text sent (at, count) and the |
| 4 | // Markdown of base, mine and theirs. Those blocks are marked in place, |
| 5 | // read-only until the person chooses; the rest of the page stays |
| 6 | // editable. Each hunk offers Yours, Theirs and Both (yours first, base |
| 7 | // build note 24), with theirs drawn as the page would draw it. When every |
| 8 | // hunk has a choice the saver posts them to Resolve; nothing nobody |
| 9 | // chose is ever committed. |
| 10 | |
| 11 | import { Plugin, PluginKey } from 'prosemirror-state' |
| 12 | import { Decoration, DecorationSet } from 'prosemirror-view' |
| 13 | import { DOMSerializer } from 'prosemirror-model' |
| 14 | import { serialiseDetailed } from './serialize.mjs' |
| 15 | import { fromMarkdown } from './fromMarkdown.mjs' |
| 16 | |
| 17 | export const conflictKey = new PluginKey('mapa-conflict') |
| 18 | |
| 19 | // rangesFor is each hunk's blocks in doc, which says the text the hunks |
| 20 | // were computed over: [{from, to, index, hunk}], one per hunk. |
| 21 | export function rangesFor (doc, fidelity, hunks) { |
| 22 | const { emitted } = serialiseDetailed(doc, fidelity) |
| 23 | return hunks.map((hunk, index) => { |
| 24 | const blocks = emitted.slice(hunk.at, hunk.at + hunk.count) |
| 25 | if (blocks.length === 0) { |
| 26 | const next = emitted[hunk.at] |
| 27 | const at = next ? next.pos : doc.content.size |
| 28 | return { from: at, to: at, index, hunk } |
| 29 | } |
| 30 | const last = blocks[blocks.length - 1] |
| 31 | return { from: blocks[0].pos, to: last.pos + last.node.nodeSize, index, hunk } |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | // touches is whether a step's replaced range [start, end) reaches into |
| 36 | // a locked range's content. |
| 37 | function touches (ranges, start, end) { |
| 38 | return ranges.some(range => range.to > range.from && |
| 39 | (start === end ? start > range.from && start < range.to : start < range.to && end > range.from)) |
| 40 | } |
| 41 | |
| 42 | function mapRanges (ranges, mapping) { |
| 43 | return ranges.map(range => ({ |
| 44 | ...range, from: mapping.map(range.from, 1), to: mapping.map(range.to, -1) |
| 45 | })) |
| 46 | } |
| 47 | |
| 48 | function element (tag, attrs = {}, ...children) { |
| 49 | const node = document.createElement(tag) |
| 50 | for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) |
| 51 | node.append(...children) |
| 52 | return node |
| 53 | } |
| 54 | |
| 55 | const choices = [ |
| 56 | ['mine', 'Keep yours'], ['theirs', 'Keep theirs'], ['both', 'Keep both, yours first'] |
| 57 | ] |
| 58 | |
| 59 | // choiceBox is one hunk's choice, drawn beside its blocks. |
| 60 | function choiceBox (schema, range, chosen, choose) { |
| 61 | const theirs = element('div', { 'mapa-hunk-theirs': '' }) |
| 62 | const drawn = fromMarkdown(schema, range.hunk.theirs, { trailing: false }) |
| 63 | theirs.append(DOMSerializer.fromSchema(schema).serializeFragment(drawn.content)) |
| 64 | const label = `mapa-hunk-${range.index}` |
| 65 | const box = element('div', { |
| 66 | 'mapa-hunk-choice': '', role: 'group', 'aria-labelledby': label, contenteditable: 'false' |
| 67 | }, element('p', { id: label }, element('strong', {}, 'Both of you changed this.'), |
| 68 | range.hunk.theirs === '' ? ' They removed it.' : ' Theirs:')) |
| 69 | if (range.hunk.theirs !== '') box.append(theirs) |
| 70 | const row = element('div', { 'mapa-hunk-buttons': '' }) |
| 71 | for (const [value, text] of choices) { |
| 72 | const button = element('button', { |
| 73 | type: 'button', 'rst-btn': chosen === value ? 'primary' : '', |
| 74 | 'aria-pressed': chosen === value ? 'true' : 'false', 'data-choice': value |
| 75 | }, text) |
| 76 | button.addEventListener('click', event => { |
| 77 | event.preventDefault() |
| 78 | choose(range.index, value) |
| 79 | }) |
| 80 | row.append(button) |
| 81 | } |
| 82 | box.append(row) |
| 83 | return box |
| 84 | } |
| 85 | |
| 86 | // conflictPlugin holds the hunks on show. isRemote says a transaction is |
| 87 | // the editor's own (a merge applied, Resolve's answer), never locked. |
| 88 | // isBusy says Resolve's POST is out: the whole page then takes no typing, |
| 89 | // so its answer never lands over text it did not include (review M1). |
| 90 | export function conflictPlugin ({ fidelity, isRemote, isBusy = () => false, resolve }) { |
| 91 | return new Plugin({ |
| 92 | key: conflictKey, |
| 93 | state: { |
| 94 | init: () => ({ ranges: [], conflict: null, picked: [] }), |
| 95 | apply (tr, value, oldState, newState) { |
| 96 | const meta = tr.getMeta(conflictKey) |
| 97 | if (meta?.clear) return { ranges: [], conflict: null, picked: [] } |
| 98 | if (meta?.conflict) { |
| 99 | return { ranges: rangesFor(newState.doc, fidelity, meta.conflict.hunks), conflict: meta.conflict, picked: [] } |
| 100 | } |
| 101 | if (meta?.picked) return { ...value, picked: meta.picked } |
| 102 | if (!tr.docChanged || value.ranges.length === 0) return value |
| 103 | return { ...value, ranges: mapRanges(value.ranges, tr.mapping) } |
| 104 | } |
| 105 | }, |
| 106 | filterTransaction (tr, state) { |
| 107 | if (!tr.docChanged || isRemote() || tr.getMeta(conflictKey)) return true |
| 108 | if (isBusy()) return false |
| 109 | let ranges = conflictKey.getState(state).ranges |
| 110 | if (ranges.length === 0) return true |
| 111 | for (const step of tr.steps) { |
| 112 | let blocked = false |
| 113 | step.getMap().forEach((start, end) => { blocked = blocked || touches(ranges, start, end) }) |
| 114 | if (blocked) return false |
| 115 | ranges = mapRanges(ranges, { map: (pos, assoc) => step.getMap().map(pos, assoc) }) |
| 116 | } |
| 117 | return true |
| 118 | }, |
| 119 | view (view) { |
| 120 | return { |
| 121 | update () { |
| 122 | const locked = conflictKey.getState(view.state).ranges.length > 0 |
| 123 | view.dom.toggleAttribute('data-mapa-conflict', locked) |
| 124 | } |
| 125 | } |
| 126 | }, |
| 127 | props: { |
| 128 | handleDOMEvents: { |
| 129 | beforeinput: (view, event) => { |
| 130 | if (!isBusy()) return false |
| 131 | event.preventDefault() |
| 132 | return true |
| 133 | } |
| 134 | }, |
| 135 | decorations (state) { |
| 136 | const value = conflictKey.getState(state) |
| 137 | if (value.ranges.length === 0) return null |
| 138 | const decorations = [] |
| 139 | const choose = (view, index, choice) => { |
| 140 | const next = [...conflictKey.getState(view.state).picked] |
| 141 | next[index] = choice |
| 142 | view.dispatch(view.state.tr.setMeta(conflictKey, { picked: next })) |
| 143 | if (value.ranges.every(range => next[range.index])) resolve(next) |
| 144 | } |
| 145 | for (const range of value.ranges) { |
| 146 | if (range.to > range.from) { |
| 147 | state.doc.nodesBetween(range.from, range.to, (node, pos) => { |
| 148 | if (pos >= range.from && node.isTextblock) { |
| 149 | decorations.push(Decoration.node(pos, pos + node.nodeSize, |
| 150 | { 'mapa-hunk': '', contenteditable: 'false' })) |
| 151 | } |
| 152 | }) |
| 153 | } |
| 154 | decorations.push(Decoration.widget(range.from, view => |
| 155 | choiceBox(state.schema, range, value.picked[range.index], |
| 156 | (index, choice) => choose(view, index, choice)), |
| 157 | { side: -1, stopEvent: () => true, ignoreSelection: true, key: `hunk-${range.index}-${value.picked[range.index] ?? ''}` })) |
| 158 | } |
| 159 | return DecorationSet.create(state.doc, decorations) |
| 160 | } |
| 161 | } |
| 162 | }) |
| 163 | } |
| 164 | |
| 165 | // showConflict puts a reply's hunks on screen, or takes them away. |
| 166 | export function showConflict (view, conflict) { |
| 167 | view.dispatch(view.state.tr.setMeta(conflictKey, conflict ? { conflict } : { clear: true })) |
| 168 | } |
| 169 | |