// conflict.mjs — the inline choice (spec §6.5, N3). When a teammate // saved the same blocks this tab changed, autosave's reply carries each // conflicting hunk: its blocks in the text sent (at, count) and the // Markdown of base, mine and theirs. Those blocks are marked in place, // read-only until the person chooses; the rest of the page stays // editable. Each hunk offers Yours, Theirs and Both (yours first, base // build note 24), with theirs drawn as the page would draw it. When every // hunk has a choice the saver posts them to Resolve; nothing nobody // chose is ever committed. import { Plugin, PluginKey } from 'prosemirror-state' import { Decoration, DecorationSet } from 'prosemirror-view' import { DOMSerializer } from 'prosemirror-model' import { serialiseDetailed } from './serialize.mjs' import { fromMarkdown } from './fromMarkdown.mjs' export const conflictKey = new PluginKey('mapa-conflict') // rangesFor is each hunk's blocks in doc, which says the text the hunks // were computed over: [{from, to, index, hunk}], one per hunk. export function rangesFor (doc, fidelity, hunks) { const { emitted } = serialiseDetailed(doc, fidelity) return hunks.map((hunk, index) => { const blocks = emitted.slice(hunk.at, hunk.at + hunk.count) if (blocks.length === 0) { const next = emitted[hunk.at] const at = next ? next.pos : doc.content.size return { from: at, to: at, index, hunk } } const last = blocks[blocks.length - 1] return { from: blocks[0].pos, to: last.pos + last.node.nodeSize, index, hunk } }) } // touches is whether a step's replaced range [start, end) reaches into // a locked range's content. function touches (ranges, start, end) { return ranges.some(range => range.to > range.from && (start === end ? start > range.from && start < range.to : start < range.to && end > range.from)) } function mapRanges (ranges, mapping) { return ranges.map(range => ({ ...range, from: mapping.map(range.from, 1), to: mapping.map(range.to, -1) })) } function element (tag, attrs = {}, ...children) { const node = document.createElement(tag) for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) node.append(...children) return node } const choices = [ ['mine', 'Keep yours'], ['theirs', 'Keep theirs'], ['both', 'Keep both, yours first'] ] // choiceBox is one hunk's choice, drawn beside its blocks. function choiceBox (schema, range, chosen, choose) { const theirs = element('div', { 'mapa-hunk-theirs': '' }) const drawn = fromMarkdown(schema, range.hunk.theirs, { trailing: false }) theirs.append(DOMSerializer.fromSchema(schema).serializeFragment(drawn.content)) const label = `mapa-hunk-${range.index}` const box = element('div', { 'mapa-hunk-choice': '', role: 'group', 'aria-labelledby': label, contenteditable: 'false' }, element('p', { id: label }, element('strong', {}, 'Both of you changed this.'), range.hunk.theirs === '' ? ' They removed it.' : ' Theirs:')) if (range.hunk.theirs !== '') box.append(theirs) const row = element('div', { 'mapa-hunk-buttons': '' }) for (const [value, text] of choices) { const button = element('button', { type: 'button', 'rst-btn': chosen === value ? 'primary' : '', 'aria-pressed': chosen === value ? 'true' : 'false', 'data-choice': value }, text) button.addEventListener('click', event => { event.preventDefault() choose(range.index, value) }) row.append(button) } box.append(row) return box } // conflictPlugin holds the hunks on show. isRemote says a transaction is // the editor's own (a merge applied, Resolve's answer), never locked. // isBusy says Resolve's POST is out: the whole page then takes no typing, // so its answer never lands over text it did not include (review M1). export function conflictPlugin ({ fidelity, isRemote, isBusy = () => false, resolve }) { return new Plugin({ key: conflictKey, state: { init: () => ({ ranges: [], conflict: null, picked: [] }), apply (tr, value, oldState, newState) { const meta = tr.getMeta(conflictKey) if (meta?.clear) return { ranges: [], conflict: null, picked: [] } if (meta?.conflict) { return { ranges: rangesFor(newState.doc, fidelity, meta.conflict.hunks), conflict: meta.conflict, picked: [] } } if (meta?.picked) return { ...value, picked: meta.picked } if (!tr.docChanged || value.ranges.length === 0) return value return { ...value, ranges: mapRanges(value.ranges, tr.mapping) } } }, filterTransaction (tr, state) { if (!tr.docChanged || isRemote() || tr.getMeta(conflictKey)) return true if (isBusy()) return false let ranges = conflictKey.getState(state).ranges if (ranges.length === 0) return true for (const step of tr.steps) { let blocked = false step.getMap().forEach((start, end) => { blocked = blocked || touches(ranges, start, end) }) if (blocked) return false ranges = mapRanges(ranges, { map: (pos, assoc) => step.getMap().map(pos, assoc) }) } return true }, view (view) { return { update () { const locked = conflictKey.getState(view.state).ranges.length > 0 view.dom.toggleAttribute('data-mapa-conflict', locked) } } }, props: { handleDOMEvents: { beforeinput: (view, event) => { if (!isBusy()) return false event.preventDefault() return true } }, decorations (state) { const value = conflictKey.getState(state) if (value.ranges.length === 0) return null const decorations = [] const choose = (view, index, choice) => { const next = [...conflictKey.getState(view.state).picked] next[index] = choice view.dispatch(view.state.tr.setMeta(conflictKey, { picked: next })) if (value.ranges.every(range => next[range.index])) resolve(next) } for (const range of value.ranges) { if (range.to > range.from) { state.doc.nodesBetween(range.from, range.to, (node, pos) => { if (pos >= range.from && node.isTextblock) { decorations.push(Decoration.node(pos, pos + node.nodeSize, { 'mapa-hunk': '', contenteditable: 'false' })) } }) } decorations.push(Decoration.widget(range.from, view => choiceBox(state.schema, range, value.picked[range.index], (index, choice) => choose(view, index, choice)), { side: -1, stopEvent: () => true, ignoreSelection: true, key: `hunk-${range.index}-${value.picked[range.index] ?? ''}` })) } return DecorationSet.create(state.doc, decorations) } } }) } // showConflict puts a reply's hunks on screen, or takes them away. export function showConflict (view, conflict) { view.dispatch(view.state.tr.setMeta(conflictKey, conflict ? { conflict } : { clear: true })) }