@dou-ui/editor 0.1.0-alpha.17 → 0.1.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collaboration/binding.js +39 -49
- package/dist/collaboration/binding.js.map +1 -1
- package/dist/collaboration/inline-diff.js +138 -0
- package/dist/collaboration/inline-diff.js.map +1 -0
- package/dist/collaboration/merged-props.js +10 -10
- package/dist/collaboration/merged-props.js.map +1 -1
- package/dist/collaboration/order.js +41 -1
- package/dist/collaboration/order.js.map +1 -1
- package/dist/collaboration/table.js +64 -25
- package/dist/collaboration/table.js.map +1 -1
- package/dist/collaboration/text.js +213 -1
- package/dist/collaboration/text.js.map +1 -1
- package/dist/core/comments.js +85 -3
- package/dist/core/comments.js.map +1 -1
- package/dist/define-block.js +6 -3
- package/dist/define-block.js.map +1 -1
- package/dist/editor.js.map +1 -1
- package/dist/flow/board-block.js.map +1 -1
- package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js +70 -9
- package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/flow/board.js +124 -5
- package/dist/flow/board.js.map +1 -1
- package/dist/flow/index.js +5 -2
- package/dist/flow/index.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/collaboration/inline-diff.d.ts +9 -0
- package/dist/types/collaboration/order.d.ts +5 -0
- package/dist/types/collaboration/table.d.ts +5 -1
- package/dist/types/collaboration/text.d.ts +20 -0
- package/dist/types/core/comments.d.ts +39 -3
- package/dist/types/define-block.d.ts +16 -4
- package/dist/types/editor.d.ts +9 -1
- package/dist/types/flow/board.d.ts +15 -2
- package/dist/types/flow/index.d.ts +8 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/use-comments.js +2 -2
- package/dist/use-comments.js.map +1 -1
- package/package.json +7 -7
|
@@ -3,13 +3,13 @@ import { blockAround, findBlockById } from "../core/blocks.js";
|
|
|
3
3
|
import { originKey } from "../core/editor.js";
|
|
4
4
|
import { joinStepKey, ownStepKey } from "../core/transaction.js";
|
|
5
5
|
import { keptIndexes, keysBetween, orderKeys } from "./order.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { createDiffBudget } from "./inline-diff.js";
|
|
7
|
+
import { inlineFrom, insertInline, keepInlineComments, prepareTextFromBase, updateText } from "./text.js";
|
|
8
|
+
import { cellAt, cellStart, clearTable, prepareTableText, readTable, writeTable } from "./table.js";
|
|
8
9
|
import { placeOf } from "./cursors.js";
|
|
9
10
|
import { mergedPropsOf, newMerged, readMerged, writeMerged } from "./merged-props.js";
|
|
10
11
|
import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
|
|
11
12
|
import { Fragment } from "prosemirror-model";
|
|
12
|
-
import { Transform } from "prosemirror-transform";
|
|
13
13
|
import * as Y from "yjs";
|
|
14
14
|
// src/collaboration/binding.ts
|
|
15
15
|
var ROOT = "\0root";
|
|
@@ -77,16 +77,16 @@ function writeContent(yblock, content) {
|
|
|
77
77
|
* Bring a stored block's type, props and text from `before` to `after`.
|
|
78
78
|
* Returns whether the stored block holds more than `after` shows (see `writeTable`).
|
|
79
79
|
*/
|
|
80
|
-
function updateContent(yblock, before, after) {
|
|
80
|
+
function updateContent(yblock, before, after, current = before, plans) {
|
|
81
81
|
if (before.type !== after.type) yblock.set("type", after.type.name);
|
|
82
|
-
if (after.type.spec.rows) return writeTable(yblock, before.type === after.type ? before : null, after);
|
|
82
|
+
if (after.type.spec.rows) return writeTable(yblock, before.type === after.type ? before : null, after, plans);
|
|
83
83
|
clearTable(yblock);
|
|
84
84
|
const props = yblock.get("props");
|
|
85
85
|
const merged = mergedPropsOf(after.type);
|
|
86
86
|
for (const [name, value] of Object.entries(after.attrs)) {
|
|
87
87
|
const spec = merged?.[name];
|
|
88
88
|
if (spec) writeMerged(props, name, spec, value, before.type === after.type ? before.attrs[name] : void 0);
|
|
89
|
-
else if (JSON.stringify(
|
|
89
|
+
else if (before.type !== after.type || JSON.stringify(before.attrs[name]) !== JSON.stringify(value)) props.set(name, value);
|
|
90
90
|
}
|
|
91
91
|
for (const name of Array.from(props.keys())) if (!(name in after.attrs)) props.delete(name);
|
|
92
92
|
const text = yblock.get("text");
|
|
@@ -94,7 +94,9 @@ function updateContent(yblock, before, after) {
|
|
|
94
94
|
if (text.length) text.delete(0, text.length);
|
|
95
95
|
return false;
|
|
96
96
|
}
|
|
97
|
-
|
|
97
|
+
const plan = plans?.get(text);
|
|
98
|
+
if (plan) plan(text);
|
|
99
|
+
else updateText(text, current, after);
|
|
98
100
|
return false;
|
|
99
101
|
}
|
|
100
102
|
/**
|
|
@@ -396,43 +398,6 @@ function matchBlocks(before, after) {
|
|
|
396
398
|
visit(ROOT, ROOT);
|
|
397
399
|
return match;
|
|
398
400
|
}
|
|
399
|
-
/**
|
|
400
|
-
* `after` with the comment marks of `before` on the text both start and
|
|
401
|
-
* end with, when `after` has none: a version read from Markdown, which
|
|
402
|
-
* doesn't carry them, leaves the threads where the text stays the same.
|
|
403
|
-
*/
|
|
404
|
-
function keepComments(before, after) {
|
|
405
|
-
const type = after.type.schema.marks.comment;
|
|
406
|
-
if (!type || !after.inlineContent || !before.inlineContent || before.type !== after.type) return after;
|
|
407
|
-
let theirs = false;
|
|
408
|
-
after.descendants((node) => {
|
|
409
|
-
if (type.isInSet(node.marks)) theirs = true;
|
|
410
|
-
});
|
|
411
|
-
if (theirs) return after;
|
|
412
|
-
const ranges = [];
|
|
413
|
-
before.forEach((child, pos) => {
|
|
414
|
-
for (const mark of child.marks) if (mark.type === type) ranges.push({
|
|
415
|
-
from: pos,
|
|
416
|
-
to: pos + child.nodeSize,
|
|
417
|
-
mark
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
if (!ranges.length) return after;
|
|
421
|
-
const a = before.textBetween(0, before.content.size, "", "");
|
|
422
|
-
const b = after.textBetween(0, after.content.size, "", "");
|
|
423
|
-
let head = 0;
|
|
424
|
-
while (head < a.length && head < b.length && a[head] === b[head]) head++;
|
|
425
|
-
let tail = 0;
|
|
426
|
-
while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail++;
|
|
427
|
-
const tr = new Transform(after);
|
|
428
|
-
const shift = b.length - a.length;
|
|
429
|
-
for (const { from, to, mark } of ranges) {
|
|
430
|
-
if (from < head) tr.addMark(from, Math.min(to, head), mark);
|
|
431
|
-
const end = a.length - tail;
|
|
432
|
-
if (to > end) tr.addMark(Math.max(from, end) + shift, to + shift, mark);
|
|
433
|
-
}
|
|
434
|
-
return tr.doc;
|
|
435
|
-
}
|
|
436
401
|
/** Every block id in `index` under `id`, at any depth. */
|
|
437
402
|
function descendants(index, id, out = []) {
|
|
438
403
|
for (const child of index.children.get(id) ?? []) {
|
|
@@ -479,6 +444,34 @@ function updateDoc(map, doc, schema, base) {
|
|
|
479
444
|
for (const id of to.blocks.keys()) if (!stored.has(id) && !gone.has(id)) stored.set(id, map.has(id) ? createId() : id);
|
|
480
445
|
const kept = new Set(stored.values());
|
|
481
446
|
const inDoc = new Set(toBase.values());
|
|
447
|
+
const budget = createDiffBudget();
|
|
448
|
+
const contentUpdates = /* @__PURE__ */ new Map();
|
|
449
|
+
const textPlans = /* @__PURE__ */ new Map();
|
|
450
|
+
for (const [id, placed] of to.blocks) {
|
|
451
|
+
if (gone.has(id)) continue;
|
|
452
|
+
const target = stored.get(id);
|
|
453
|
+
const there = now.blocks.get(target)?.content;
|
|
454
|
+
if (!there) continue;
|
|
455
|
+
const old = toBase.get(id);
|
|
456
|
+
const was = old !== void 0 ? from.blocks.get(old).content : null;
|
|
457
|
+
const compared = was ? keepInlineComments(was, placed.content, budget) : placed.content;
|
|
458
|
+
if (was && compared.eq(was)) continue;
|
|
459
|
+
const before = was && was.type === there.type && was.type === placed.content.type ? was : there;
|
|
460
|
+
const after = before === was ? compared : keepInlineComments(before, placed.content, budget);
|
|
461
|
+
const yblock = map.get(target);
|
|
462
|
+
contentUpdates.set(id, {
|
|
463
|
+
before,
|
|
464
|
+
after,
|
|
465
|
+
current: there
|
|
466
|
+
});
|
|
467
|
+
if (after.type.spec.rows) prepareTableText(yblock, before.type === after.type ? before : null, after, textPlans, budget);
|
|
468
|
+
else if (after.inlineContent) {
|
|
469
|
+
const text = yblock.get("text");
|
|
470
|
+
const currentInline = there.inlineContent ? there : after.type.create(after.attrs, inlineFrom(text, schema, !!after.type.spec.code));
|
|
471
|
+
const baseInline = before.inlineContent ? before : currentInline;
|
|
472
|
+
textPlans.set(text, prepareTextFromBase(baseInline, after, currentInline, budget));
|
|
473
|
+
}
|
|
474
|
+
}
|
|
482
475
|
for (const old of from.blocks.keys()) {
|
|
483
476
|
const there = inDoc.has(old) ? null : nowOf(old);
|
|
484
477
|
if (!there) continue;
|
|
@@ -494,11 +487,8 @@ function updateDoc(map, doc, schema, base) {
|
|
|
494
487
|
writeContent(created, placed.content);
|
|
495
488
|
continue;
|
|
496
489
|
}
|
|
497
|
-
const
|
|
498
|
-
|
|
499
|
-
if (was && keepComments(was, placed.content).eq(was)) continue;
|
|
500
|
-
const there = now.blocks.get(target).content;
|
|
501
|
-
updateContent(yblock, there, keepComments(there, placed.content));
|
|
490
|
+
const update = contentUpdates.get(id);
|
|
491
|
+
if (update) updateContent(yblock, update.before, update.after, update.current, textPlans);
|
|
502
492
|
}
|
|
503
493
|
for (const [key, all] of to.children) {
|
|
504
494
|
if (gone.has(key)) continue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binding.js","names":[],"sources":["../../src/collaboration/binding.ts"],"sourcesContent":["import {\n Fragment,\n type Mark,\n type Node as PMNode,\n type ResolvedPos,\n type Schema,\n} from 'prosemirror-model'\nimport { Transform } from 'prosemirror-transform'\nimport {\n Plugin,\n PluginKey,\n TextSelection,\n type EditorState,\n type Transaction,\n} from 'prosemirror-state'\nimport type { EditorView } from 'prosemirror-view'\nimport * as Y from 'yjs'\nimport { blockAround, findBlockById } from '../core/blocks'\nimport { createId } from '../core/model'\nimport { originKey } from '../core/editor'\nimport { joinStepKey, ownStepKey } from '../core/transaction'\nimport { placeOf } from './cursors'\nimport { keptIndexes, keysBetween, orderKeys } from './order'\nimport { mergedPropsOf, newMerged, readMerged, writeMerged } from './merged-props'\nimport { cellAt, cellStart, clearTable, readTable, writeTable } from './table'\nimport { inlineFrom, insertInline, updateText, type Attributes } from './text'\n\n/*\n * How a document is stored in Yjs. Every block is one entry of a `Y.Map`,\n * by id, holding its type, its props, its text and its place: the id of its\n * parent and an order key among its siblings. Moving a block only changes\n * its place, so text typed into it meanwhile by someone else stays in it.\n *\n * map: Y.Map<id, Y.Map {\n * type: string\n * props: Y.Map<name, value>\n * text: Y.Text (marks as formatting attributes, inline nodes as embeds)\n * parent: string | null\n * order: string\n * }>\n *\n * Tables keep their rows, columns and cells in more entries; see `./table`.\n * Props a block type merges item by item are maps too; see `./merged-props`.\n */\n\nexport type YBlock = Y.Map<unknown>\n\nconst ROOT = '\\u0000root'\n\nconst parentKey = (parent: string | null) => parent ?? ROOT\n\n/** The place of every block as the editor shows it, read from the document. */\ninterface Placed {\n /** The content node (paragraph, heading, …). */\n content: PMNode\n parent: string | null\n}\n\ninterface Index {\n blocks: Map<string, Placed>\n /** Child ids in order, by parent (`ROOT` for the top level). */\n children: Map<string, string[]>\n}\n\nfunction indexDoc(doc: PMNode): Index {\n const blocks = new Map<string, Placed>()\n const children = new Map<string, string[]>()\n const visit = (container: PMNode, parent: string | null) => {\n const ids: string[] = []\n container.forEach((block) => {\n const id = block.attrs.id as string\n ids.push(id)\n blocks.set(id, { content: block.firstChild!, parent })\n if (block.childCount > 1) visit(block.child(1), id)\n })\n children.set(parentKey(parent), ids)\n }\n visit(doc, null)\n return { blocks, children }\n}\n\n/** The content node a stored block stands for. Unknown types become paragraphs. */\nfunction contentFrom(yblock: YBlock, schema: Schema): PMNode {\n const name = yblock.get('type') as string\n const known = schema.nodes[name]\n const type = known?.isInGroup('blockContent') ? known : schema.nodes.paragraph!\n const stored = yblock.get('props') as Y.Map<unknown> | undefined\n const props = stored?.toJSON() ?? {}\n const merged = mergedPropsOf(type)\n const attrs: Attributes = {}\n for (const attr of Object.keys(type.spec.attrs ?? {})) {\n if (!(attr in props)) continue\n const spec = merged?.[attr]\n attrs[attr] = spec ? readMerged(stored!.get(attr), spec) : props[attr]\n }\n if (type.spec.rows) return readTable(yblock, type, attrs)\n if (!type.inlineContent) return type.create(attrs)\n const text = yblock.get('text') as Y.Text | undefined\n const inline = text ? inlineFrom(text, schema, !!type.spec.code) : []\n return type.createChecked(attrs, inline.length ? Fragment.from(inline) : null)\n}\n\nfunction writeContent(yblock: YBlock, content: PMNode) {\n yblock.set('type', content.type.name)\n const text = new Y.Text()\n yblock.set('text', text)\n if (content.type.spec.rows) {\n writeTable(yblock, null, content)\n return\n }\n const props = new Y.Map<unknown>()\n const merged = mergedPropsOf(content.type)\n yblock.set('props', props)\n for (const [name, value] of Object.entries(content.attrs)) {\n const spec = merged?.[name]\n props.set(name, spec ? newMerged(value, spec) : value)\n }\n if (content.inlineContent) insertInline(text, 0, content, 0, content.content.size)\n}\n\n/**\n * Bring a stored block's type, props and text from `before` to `after`.\n * Returns whether the stored block holds more than `after` shows (see `writeTable`).\n */\nfunction updateContent(yblock: YBlock, before: PMNode, after: PMNode): boolean {\n if (before.type !== after.type) yblock.set('type', after.type.name)\n if (after.type.spec.rows)\n return writeTable(yblock, before.type === after.type ? before : null, after)\n clearTable(yblock)\n const props = yblock.get('props') as Y.Map<unknown>\n const merged = mergedPropsOf(after.type)\n for (const [name, value] of Object.entries(after.attrs)) {\n const spec = merged?.[name]\n if (spec)\n writeMerged(\n props,\n name,\n spec,\n value,\n before.type === after.type ? before.attrs[name] : undefined,\n )\n else if (JSON.stringify(props.get(name)) !== JSON.stringify(value)) props.set(name, value)\n }\n for (const name of Array.from(props.keys())) if (!(name in after.attrs)) props.delete(name)\n\n const text = yblock.get('text') as Y.Text\n if (!after.inlineContent) {\n if (text.length) text.delete(0, text.length)\n return false\n }\n updateText(text, before, after)\n return false\n}\n\n/* the stored tree → document */\n\n/**\n * Where every stored block goes. A parent that no longer exists (deleted\n * while someone moved a block into it) leaves the block at the top level;\n * a cycle (two blocks moved into each other at the same time) is broken at\n * its smallest id. Every client reads the same data the same way, so all\n * end up with the same tree.\n */\nfunction placeAll(map: Y.Map<YBlock>): Map<string, string[]> {\n const parentOf = new Map<string, string | null>()\n map.forEach((yblock, id) => {\n const parent = yblock.get('parent') as string | null | undefined\n parentOf.set(id, parent && parent !== id && map.has(parent) ? parent : null)\n })\n const settled = new Set<string>()\n for (const id of parentOf.keys()) {\n const path: string[] = []\n const onPath = new Set<string>()\n let current: string | null = id\n while (current !== null && !settled.has(current)) {\n if (onPath.has(current)) {\n const cycle = path.slice(path.indexOf(current))\n // oxlint-disable-next-line unicorn/no-array-sort -- sorts a fresh copy\n const breakAt = [...cycle].sort()[0]!\n parentOf.set(breakAt, null)\n break\n }\n path.push(current)\n onPath.add(current)\n current = parentOf.get(current) ?? null\n }\n for (const visited of path) settled.add(visited)\n }\n const children = new Map<string, string[]>()\n for (const [id, parent] of parentOf) {\n const key = parentKey(parent)\n const list = children.get(key)\n if (list) list.push(id)\n else children.set(key, [id])\n }\n const orderOf = (id: string) => (map.get(id)!.get('order') as string | undefined) ?? ''\n for (const list of children.values()) {\n // oxlint-disable-next-line unicorn/no-array-sort -- sorts our own list\n list.sort((a, b) => {\n const x = orderOf(a)\n const y = orderOf(b)\n return x < y ? -1 : x > y ? 1 : a < b ? -1 : a > b ? 1 : 0\n })\n }\n return children\n}\n\n/** Whether `$pos` is inside table structure below `depth`. */\nfunction inTable($pos: ResolvedPos, depth: number) {\n for (let d = $pos.depth; d > depth; d--) if ($pos.node(d).type.spec.tableRole) return true\n return false\n}\n\n/** A position as its table cell and offset in it, if it is in one. */\nfunction cellPlace(doc: PMNode, pos: number) {\n const info = blockAround(doc.resolve(pos))\n if (!info?.content.type.spec.rows) return null\n const cell = cellAt(info.content, pos - info.contentPos - 1)\n return cell && { block: info.node.attrs.id as string, ...cell }\n}\n\n/** Put a cursor that was in a replaced table back into its cell. */\nfunction keepCellSelection(state: EditorState, tr: Transaction) {\n const { selection } = state\n if (!(selection instanceof TextSelection)) return\n const back = (pos: number) => {\n const place = cellPlace(state.doc, pos)\n if (!place) return null\n const info = findBlockById(tr.doc, place.block)\n const cell = info && cellStart(info.content, place.key)\n return cell ? info.contentPos + 1 + cell.offset + Math.min(place.index, cell.size) : null\n }\n const anchor = back(selection.anchor)\n const head = back(selection.head)\n if (anchor !== null && head !== null) tr.setSelection(TextSelection.create(tr.doc, anchor, head))\n}\n\n/* the binding */\n\nexport const collaborationKey = new PluginKey<{\n remote: boolean\n tracked: boolean\n /** An undo step of its own (`transact`). */\n own: boolean\n /** Joins the undo step before (the frames of a drag on a whiteboard). */\n join: boolean\n}>('dou-editor-collaboration')\n\nexport interface Binding {\n map: Y.Map<YBlock>\n /** The origin of this editor's own changes; the undo manager tracks it. */\n origin: object\n /** The origin of changes that are not undone (`setBlocks`, fixes). */\n untracked: object\n view: EditorView | null\n /** Ends the current undo step, so a structural change is undone on its own. */\n stopCapturing?: () => void\n /** Makes the next change join the current undo step. */\n joinCapturing?: () => void\n /** Where the cursor was before the change being written, for undo to return to. */\n selectionBefore?: unknown\n /** Set while the undo manager takes a step back or forward. */\n undoing?: 'undo' | 'redo'\n}\n\nexport function createBinding(map: Y.Map<YBlock>): Binding {\n return { map, origin: { binding: 'tracked' }, untracked: { binding: 'untracked' }, view: null }\n}\n\n/** Write the blocks of `doc` into an empty map. */\nexport function writeDoc(map: Y.Map<YBlock>, doc: PMNode) {\n const index = indexDoc(doc)\n map.doc!.transact(() => {\n for (const [key, ids] of index.children) {\n const keys = keysBetween(null, null, ids.length)\n ids.forEach((id, i) => {\n const yblock = new Y.Map<unknown>()\n map.set(id, yblock)\n yblock.set('parent', key === ROOT ? null : key)\n yblock.set('order', keys[i])\n writeContent(yblock, index.blocks.get(id)!.content)\n })\n }\n })\n}\n\n/**\n * Write the changes from `before` to `after` into the map: the content of\n * blocks that changed, blocks added and removed, and the places of the\n * blocks that moved. Returns the blocks that hold more than `after` shows,\n * to be read back (see `writeTable`). Call it inside a transaction.\n */\nfunction writeChanges(map: Y.Map<YBlock>, before: Index, after: Index, schema: Schema): string[] {\n const stale: string[] = []\n for (const [id, placed] of after.blocks) {\n const yblock = map.get(id)\n const old = before.blocks.get(id)\n if (!yblock) {\n const created = new Y.Map<unknown>()\n map.set(id, created)\n writeContent(created, placed.content)\n } else if (old && old.content !== placed.content) {\n if (updateContent(yblock, old.content, placed.content)) stale.push(id)\n } else if (!old) {\n // stored, but not shown before (e.g. the empty placeholder came back)\n if (updateContent(yblock, contentFrom(yblock, schema), placed.content)) stale.push(id)\n }\n }\n for (const id of before.blocks.keys()) if (!after.blocks.has(id)) map.delete(id)\n // places: only lists that changed, and only the blocks that moved in them\n for (const [key, ids] of after.children) {\n const old = before.children.get(key)\n if (old && old.length === ids.length && old.every((id, i) => ids[i] === id)) {\n if (ids.every((id) => map.get(id)!.get('order') !== undefined)) continue\n }\n const parent = key === ROOT ? null : key\n const keys = ids.map((id) => {\n const yblock = map.get(id)!\n const order = yblock.get('order') as string | undefined\n return yblock.get('parent') === parent && order !== undefined ? order : null\n })\n const next = orderKeys(keys)\n ids.forEach((id, index) => {\n if (keys[index] === next[index]) return\n const yblock = map.get(id)!\n if (yblock.get('parent') !== parent) yblock.set('parent', parent)\n yblock.set('order', next[index])\n })\n }\n return stale\n}\n\n/* writing a new version from elsewhere, without an editor (`updateMap`) */\n\n/** The content node of block `id`. */\nconst contentOf = (index: Index, id: string) => index.blocks.get(id)!.content\n\n/** A content node as a string, to tell equal blocks apart quickly. */\nconst keyOf = (content: PMNode) => JSON.stringify(content.toJSON())\n\n/**\n * Pairs of `a` and `b` with equal keys, in the order of both: the equal\n * start and end, then in the middle each of `a` with the first free equal\n * one of `b`, keeping the longest run in order. Indexes into `a` and `b`;\n * about linear, so a long document stays fast.\n */\nfunction equalPairs(a: string[], b: string[]): [number, number][] {\n const pairs: [number, number][] = []\n let start = 0\n while (start < a.length && start < b.length && a[start] === b[start]) {\n pairs.push([start, start])\n start++\n }\n let endA = a.length\n let endB = b.length\n const tail: [number, number][] = []\n while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {\n endA--\n endB--\n tail.push([endA, endB])\n }\n // the middle: each of `a` takes the first free equal one of `b`, then the\n // longest run increasing in `b` stays\n const free = new Map<string, number[]>()\n for (let j = endB - 1; j >= start; j--) {\n const list = free.get(b[j]!)\n if (list) list.push(j)\n else free.set(b[j]!, [j])\n }\n const middle: [number, number][] = []\n for (let i = start; i < endA; i++) {\n const j = free.get(a[i]!)?.pop()\n if (j !== undefined) middle.push([i, j])\n }\n const kept = keptIndexes(middle.map(([, j]) => String(j).padStart(10, '0')))\n middle.forEach((pair, index) => kept.has(index) && pairs.push(pair))\n for (let k = tail.length - 1; k >= 0; k--) pairs.push(tail[k]!)\n return pairs\n}\n\n/** How alike two texts are, 0 to 1, by the pairs of letters they share. */\nfunction likeness(a: string, b: string): number {\n if (a === b) return 1\n if (a.length < 2 || b.length < 2) return 0\n const pairs = new Map<string, number>()\n for (let i = 0; i < a.length - 1; i++) {\n const pair = a.slice(i, i + 2)\n pairs.set(pair, (pairs.get(pair) ?? 0) + 1)\n }\n let shared = 0\n for (let i = 0; i < b.length - 1; i++) {\n const pair = b.slice(i, i + 2)\n const left = pairs.get(pair)\n if (left) {\n shared++\n pairs.set(pair, left - 1)\n }\n }\n return (2 * shared) / (a.length + b.length - 2)\n}\n\n/** How alike two blocks must be to count as one edited. */\nconst ALIKE = 0.4\n/** How many stored blocks ahead a block is looked for between two equal ones. */\nconst LOOK_AHEAD = 32\n/** How alike a block moved out of its order must be to count as the same one edited. */\nconst MOVED_ALIKE = 0.8\n/** The most comparisons spent on blocks moved out of their order. */\nconst MOVED_PAIRS = 40_000\n\n/**\n * Which block of `before` each block of `after` stands for: by id when it\n * keeps one, otherwise by place among the blocks of the same parent: first\n * the blocks with equal content, kept in order, then between those, each\n * block with the most alike one of its type further on (its text is\n * changed in place). A block with nothing alike enough is new. Returns\n * new id → old id.\n */\nfunction matchBlocks(before: Index, after: Index): Map<string, string> {\n const match = new Map<string, string>()\n const used = new Set<string>()\n for (const id of after.blocks.keys())\n if (before.blocks.has(id)) {\n match.set(id, id)\n used.add(id)\n }\n const pair = (a: string, b: string) => {\n match.set(a, b)\n used.add(b)\n }\n const visit = (key: string, oldKey: string | null) => {\n const ids = after.children.get(key) ?? []\n const fresh = ids.filter((id) => !match.has(id))\n const olds = (oldKey === null ? [] : (before.children.get(oldKey) ?? [])).filter(\n (id) => !used.has(id),\n )\n const anchors = equalPairs(\n fresh.map((id) => keyOf(contentOf(after, id))),\n olds.map((id) => keyOf(contentOf(before, id))),\n )\n let i = 0\n let j = 0\n for (const [ai, aj] of [...anchors, [fresh.length, olds.length] as [number, number]]) {\n // between two equal blocks: each takes the most alike block of its type ahead\n for (; i < ai; i++) {\n const node = contentOf(after, fresh[i]!)\n const text = node.textContent\n let best = -1\n let score = ALIKE\n for (let k = j; k < Math.min(aj, j + LOOK_AHEAD); k++) {\n const old = contentOf(before, olds[k]!)\n if (old.type !== node.type) continue\n const like = likeness(text, old.textContent)\n if (like > score || (best < 0 && like >= score)) {\n best = k\n score = like\n }\n }\n if (best >= 0) {\n pair(fresh[i]!, olds[best]!)\n j = best + 1\n }\n }\n if (ai < fresh.length) pair(fresh[ai]!, olds[aj]!)\n i = ai + 1\n j = aj + 1\n }\n // what is left: blocks moved past others, e.g. paragraphs put in another order;\n // the same content first, then very alike text, wherever it is in the list\n const leftNew = fresh.filter((id) => !match.has(id))\n const leftOld = olds.filter((id) => !used.has(id))\n if (leftNew.length && leftOld.length) {\n const byKey = new Map<string, string[]>()\n // last first, so `pop` takes them in order\n for (let k = leftOld.length - 1; k >= 0; k--) {\n const id = leftOld[k]!\n const text = keyOf(contentOf(before, id))\n const list = byKey.get(text)\n if (list) list.push(id)\n else byKey.set(text, [id])\n }\n const rest: string[] = []\n for (const id of leftNew) {\n const old = byKey.get(keyOf(contentOf(after, id)))?.pop()\n if (old) pair(id, old)\n else rest.push(id)\n }\n if (rest.length * leftOld.length <= MOVED_PAIRS)\n for (const id of rest) {\n const node = contentOf(after, id)\n let best: string | null = null\n let score = MOVED_ALIKE\n for (const old of leftOld) {\n if (used.has(old) || contentOf(before, old).type !== node.type) continue\n const like = likeness(node.textContent, contentOf(before, old).textContent)\n if (like >= score) {\n best = old\n score = like\n }\n }\n if (best) pair(id, best)\n }\n }\n for (const id of ids) if (after.children.has(id)) visit(id, match.get(id) ?? null)\n }\n visit(ROOT, ROOT)\n return match\n}\n\n/**\n * `after` with the comment marks of `before` on the text both start and\n * end with, when `after` has none: a version read from Markdown, which\n * doesn't carry them, leaves the threads where the text stays the same.\n */\nfunction keepComments(before: PMNode, after: PMNode): PMNode {\n const type = after.type.schema.marks.comment\n if (!type || !after.inlineContent || !before.inlineContent || before.type !== after.type)\n return after\n let theirs = false\n after.descendants((node) => {\n if (type.isInSet(node.marks)) theirs = true\n })\n if (theirs) return after\n const ranges: { from: number; to: number; mark: Mark }[] = []\n before.forEach((child, pos) => {\n for (const mark of child.marks)\n if (mark.type === type) ranges.push({ from: pos, to: pos + child.nodeSize, mark })\n })\n if (!ranges.length) return after\n const a = before.textBetween(0, before.content.size, '', '')\n const b = after.textBetween(0, after.content.size, '', '')\n let head = 0\n while (head < a.length && head < b.length && a[head] === b[head]) head++\n let tail = 0\n while (\n tail < a.length - head &&\n tail < b.length - head &&\n a[a.length - 1 - tail] === b[b.length - 1 - tail]\n )\n tail++\n const tr = new Transform(after)\n const shift = b.length - a.length\n for (const { from, to, mark } of ranges) {\n // the start both share, and the end, moved by what changed between\n if (from < head) tr.addMark(from, Math.min(to, head), mark)\n const end = a.length - tail\n if (to > end) tr.addMark(Math.max(from, end) + shift, to + shift, mark)\n }\n return tr.doc\n}\n\n/** Every block id in `index` under `id`, at any depth. */\nfunction descendants(index: Index, id: string, out: string[] = []): string[] {\n for (const child of index.children.get(id) ?? []) {\n out.push(child)\n descendants(index, child, out)\n }\n return out\n}\n\n/**\n * Change the map from `base` to `doc`, leaving what changed in the map since\n * `base` was read: blocks of `doc` are matched to those of `base` (see\n * `matchBlocks`), and those of `base` to the stored ones; only blocks that\n * differ between `base` and `doc` are written, removed or moved. Without\n * `base`, the map as it is now. Call it inside a transaction.\n */\nexport function updateDoc(map: Y.Map<YBlock>, doc: PMNode, schema: Schema, base?: PMNode | null) {\n const current = readDoc(map, schema)\n if (!current) return void writeDoc(map, doc)\n const now = indexDoc(current)\n const from = base ? indexDoc(base) : now\n const to = indexDoc(doc)\n const toBase = matchBlocks(from, to)\n const baseToNow = base ? matchBlocks(now, from) : null\n /** The stored block a block of `base` is now, if it still is. */\n const nowOf = (old: string) => {\n const there = baseToNow ? baseToNow.get(old) : old\n return there !== undefined && now.blocks.has(there) ? there : null\n }\n /** The stored block each block of `doc` is written to. */\n const stored = new Map<string, string>()\n for (const [id, old] of toBase) {\n const there = nowOf(old)\n if (there) stored.set(id, there)\n }\n // removed by someone else since `base`: it stays removed, even if `doc`\n // changed it, and so do the blocks `doc` adds under it\n const gone = new Set<string>()\n const walk = (key: string, inGone: boolean) => {\n for (const id of to.children.get(key) ?? []) {\n const old = toBase.get(id)\n const lost = (old !== undefined && !stored.has(id)) || (inGone && !stored.has(id))\n if (lost) gone.add(id)\n walk(id, lost)\n }\n }\n walk(ROOT, false)\n // ids of `doc` for its new blocks: kept unless the map has them already\n for (const id of to.blocks.keys())\n if (!stored.has(id) && !gone.has(id)) stored.set(id, map.has(id) ? createId() : id)\n const kept = new Set(stored.values())\n const inDoc = new Set(toBase.values())\n\n // removed: what `base` had and `doc` hasn't, with what is stored under it\n for (const old of from.blocks.keys()) {\n const there = inDoc.has(old) ? null : nowOf(old)\n if (!there) continue\n for (const id of [there, ...descendants(now, there)])\n if (!kept.has(id) && map.has(id)) map.delete(id)\n }\n\n // content: new blocks written, those changed since `base` brought to `doc`\n for (const [id, placed] of to.blocks) {\n if (gone.has(id)) continue\n const target = stored.get(id)!\n const yblock = map.get(target)\n if (!now.blocks.has(target)) {\n const created = new Y.Map<unknown>()\n map.set(target, created)\n writeContent(created, placed.content)\n continue\n }\n const old = toBase.get(id)\n const was = old !== undefined ? from.blocks.get(old)!.content : null\n if (was && keepComments(was, placed.content).eq(was)) continue\n const there = now.blocks.get(target)!.content\n updateContent(yblock!, there, keepComments(there, placed.content))\n }\n\n // places: in each list of `doc`, the blocks new there or out of their order in `base`\n for (const [key, all] of to.children) {\n if (gone.has(key)) continue\n const ids = all.filter((id) => !gone.has(id))\n const parent = key === ROOT ? null : stored.get(key)!\n const oldParent = key === ROOT ? ROOT : toBase.get(key)\n const siblings = oldParent === undefined ? [] : (from.children.get(oldParent) ?? [])\n const at = new Map(siblings.map((id, index) => [id, index]))\n const inBase = ids.map((id) => {\n const old = toBase.get(id)\n return old !== undefined && at.has(old) ? String(at.get(old)!).padStart(10, '0') : null\n })\n const stays = keptIndexes(inBase)\n const moved = new Set(ids.filter((_, index) => !stays.has(index)).map((id) => stored.get(id)!))\n if (!moved.size) continue\n // the stored list, without what goes elsewhere, with the moved ones put after their neighbour in `doc`\n const list = (now.children.get(parentKey(parent)) ?? []).filter(\n (id) => map.has(id) && !moved.has(id),\n )\n let previous: string | null = null\n for (const id of ids) {\n const target = stored.get(id)!\n if (moved.has(target))\n list.splice(previous === null ? 0 : list.indexOf(previous) + 1, 0, target)\n if (list.includes(target)) previous = target\n }\n const keys = list.map((id) => {\n const yblock = map.get(id)!\n const order = yblock.get('order') as string | undefined\n return !moved.has(id) && yblock.get('parent') === parent && order !== undefined ? order : null\n })\n const next = orderKeys(keys)\n list.forEach((id, index) => {\n if (keys[index] === next[index]) return\n const yblock = map.get(id)!\n if (yblock.get('parent') !== parent) yblock.set('parent', parent)\n yblock.set('order', next[index])\n })\n }\n}\n\n/** The document a map holds, read without an editor; `null` while the map is empty. */\nexport function readDoc(map: Y.Map<YBlock>, schema: Schema): PMNode | null {\n const children = placeAll(map)\n const build = (id: string): PMNode => {\n const content = contentFrom(map.get(id)!, schema)\n const kids = children.get(id)\n const parts = kids?.length\n ? [content, schema.nodes.blockGroup!.create(null, kids.map(build))]\n : [content]\n return schema.nodes.block!.create({ id }, parts)\n }\n const top = children.get(ROOT) ?? []\n return top.length ? schema.nodes.doc!.create(null, top.map(build)) : null\n}\n\nexport function syncPlugin(binding: Binding): Plugin {\n const { map } = binding\n const ydoc = map.doc!\n let schema: Schema\n // the document as last written to or read from the map\n let synced: Index | null = null\n // content nodes read from the map, by id, until the block changes there\n const cache = new Map<string, PMNode>()\n let placeholderId = createId()\n\n function buildDoc(): PMNode {\n const children = placeAll(map)\n const build = (id: string): PMNode => {\n let content = cache.get(id)\n if (!content) {\n content = contentFrom(map.get(id)!, schema)\n cache.set(id, content)\n }\n const kids = children.get(id)\n const parts = kids?.length\n ? [content, schema.nodes.blockGroup!.create(null, kids.map(build))]\n : [content]\n return schema.nodes.block!.create({ id }, parts)\n }\n const top = children.get(ROOT) ?? []\n // an empty map still shows one empty paragraph; it is stored once typed in\n const blocks = top.length\n ? top.map(build)\n : [schema.nodes.block!.create({ id: placeholderId }, schema.nodes.paragraph!.create())]\n return schema.nodes.doc!.create(null, blocks)\n }\n\n /** Show the map's current state in the editor. */\n function readMap(view: EditorView) {\n const doc = view.state.doc\n const next = buildDoc()\n const start = doc.content.findDiffStart(next.content)\n if (start !== null) {\n const end = doc.content.findDiffEnd(next.content)!\n const overlap = start - Math.min(end.a, end.b)\n if (overlap > 0) {\n end.a += overlap\n end.b += overlap\n }\n let from = start\n let to = end.a\n const $from = doc.resolve(from)\n const $to = doc.resolve(to)\n const shared = $from.sharedDepth(to)\n // a change across table cells or rows: swap them whole, a partial cut breaks the grid\n if (!$from.node(shared).inlineContent && (inTable($from, shared) || inTable($to, shared))) {\n if ($from.depth > shared) from = $from.before(shared + 1)\n if ($to.depth > shared) to = $to.after(shared + 1)\n }\n const tail = doc.content.size - to\n const tr = view.state.tr.replace(from, to, next.slice(from, next.content.size - tail))\n if (to !== end.a || from !== start) keepCellSelection(view.state, tr)\n tr.setMeta(collaborationKey, { remote: true })\n tr.setMeta('remote', true)\n // this user's undo comes back through the shared document too\n if (binding.undoing) tr.setMeta(originKey, binding.undoing)\n tr.setMeta('addToHistory', false)\n view.dispatch(tr)\n }\n synced = indexDoc(view.state.doc)\n }\n\n /** Write the editor's changes since the last sync into the map. */\n function writeMap(view: EditorView, tracked: boolean) {\n const doc = view.state.doc\n const before = synced ?? indexDoc(doc)\n const after = indexDoc(doc)\n // moving, indenting or adding blocks is an undo step of its own\n let structural = false\n for (const [key, ids] of after.children) {\n const old = before.children.get(key)\n if (!old || old.length !== ids.length || old.some((id, i) => ids[i] !== id)) structural = true\n }\n if (structural) binding.stopCapturing?.()\n const stale = ydoc.transact(\n () => writeChanges(map, before, after, schema),\n tracked ? binding.origin : binding.untracked,\n )\n for (const id of after.blocks.keys()) cache.set(id, after.blocks.get(id)!.content)\n for (const id of before.blocks.keys()) if (!after.blocks.has(id)) cache.delete(id)\n if (structural) binding.stopCapturing?.()\n synced = after\n if (stale.length) {\n for (const id of stale) cache.delete(id)\n // not from inside this update: show the rest once it is done\n queueMicrotask(() => {\n if (binding.view === view) readMap(view)\n })\n }\n }\n\n return new Plugin({\n key: collaborationKey,\n state: {\n init: () => ({ remote: false, tracked: false, own: false, join: false }),\n apply(tr, value) {\n const meta = tr.getMeta(collaborationKey) as { remote?: boolean } | undefined\n if (!tr.docChanged) return value\n if (meta?.remote) return { remote: true, tracked: false, own: false, join: false }\n // a transaction can come with appended ones (e.g. id fixes), written\n // together: undone together if any of them is\n const tracked = tr.getMeta('addToHistory') !== false\n const own = tr.getMeta(ownStepKey) === true\n const join = tr.getMeta(joinStepKey) === true\n // a step joined on purpose stands on its own here (its\n // `appendedTransaction` is for the built-in history)\n const appended = !join && !!tr.getMeta('appendedTransaction') && !value.remote\n return appended\n ? {\n remote: false,\n tracked: value.tracked || tracked,\n own: value.own || own,\n join: value.join || join,\n }\n : { remote: false, tracked, own, join }\n },\n },\n view(view) {\n schema = view.state.schema\n binding.view = view\n const observer = (\n events: Y.YEvent<Y.AbstractType<unknown>>[],\n transaction: Y.Transaction,\n ) => {\n if (transaction.origin === binding.origin || transaction.origin === binding.untracked)\n return\n // forget the cached content of every block that changed\n for (const event of events) {\n if (event.target === map) {\n for (const id of (event as Y.YMapEvent<unknown>).keysChanged) cache.delete(id)\n continue\n }\n // the path from the map starts with the block's id\n const id = event.path[0]\n if (typeof id === 'string') cache.delete(id)\n }\n readMap(view)\n }\n map.observeDeep(observer)\n if (map.size) readMap(view)\n else synced = indexDoc(view.state.doc)\n placeholderId = (view.state.doc.firstChild?.attrs.id as string | undefined) ?? placeholderId\n return {\n update(current, previous: EditorState) {\n if (current.state.doc === previous.doc) return\n const state = collaborationKey.getState(current.state)!\n if (state.remote) {\n synced = indexDoc(current.state.doc)\n return\n }\n // read before writing: the map still holds the previous text\n binding.selectionBefore = placeOf(binding, previous.doc, previous.selection.head)\n if (state.own) binding.stopCapturing?.()\n else if (state.join) binding.joinCapturing?.()\n writeMap(current, state.tracked)\n if (state.own) binding.stopCapturing?.()\n },\n destroy() {\n map.unobserveDeep(observer)\n binding.view = null\n },\n }\n },\n })\n}\n\nexport type { Transaction }\n"],"mappings":";;;;;;;;;;;;;;AA+CA,IAAM,OAAO;AAEb,IAAM,aAAa,WAA0B,UAAU;AAevD,SAAS,SAAS,KAAoB;CACpC,MAAM,yBAAS,IAAI,IAAoB;CACvC,MAAM,2BAAW,IAAI,IAAsB;CAC3C,MAAM,SAAS,WAAmB,WAA0B;EAC1D,MAAM,MAAgB,CAAC;EACvB,UAAU,SAAS,UAAU;GAC3B,MAAM,KAAK,MAAM,MAAM;GACvB,IAAI,KAAK,EAAE;GACX,OAAO,IAAI,IAAI;IAAE,SAAS,MAAM;IAAa;GAAO,CAAC;GACrD,IAAI,MAAM,aAAa,GAAG,MAAM,MAAM,MAAM,CAAC,GAAG,EAAE;EACpD,CAAC;EACD,SAAS,IAAI,UAAU,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Q;KAAO;KAAS;KAAK;IAAK;GAC1C;EACF;EACA,KAAK,MAAM;GACT,SAAS,KAAK,MAAM;GACpB,QAAQ,OAAO;GACf,MAAM,YACJ,QACA,gBACG;IACH,IAAI,YAAY,WAAW,QAAQ,UAAU,YAAY,WAAW,QAAQ,WAC1E;IAEF,KAAK,MAAM,SAAS,QAAQ;KAC1B,IAAI,MAAM,WAAW,KAAK;MACxB,KAAK,MAAM,MAAO,MAA+B,aAAa,MAAM,OAAO,EAAE;MAC7E;KACF;KAEA,MAAM,KAAK,MAAM,KAAK;KACtB,IAAI,OAAO,OAAO,UAAU,MAAM,OAAO,EAAE;IAC7C;IACA,QAAQ,IAAI;GACd;GACA,IAAI,YAAY,QAAQ;GACxB,IAAI,IAAI,MAAM,QAAQ,IAAI;QACrB,SAAS,SAAS,KAAK,MAAM,GAAG;GACrC,gBAAiB,KAAK,MAAM,IAAI,YAAY,MAAM,MAA6B;GAC/E,OAAO;IACL,OAAO,SAAS,UAAuB;KACrC,IAAI,QAAQ,MAAM,QAAQ,SAAS,KAAK;KACxC,MAAM,QAAQ,iBAAiB,SAAS,QAAQ,KAAK;KACrD,IAAI,MAAM,QAAQ;MAChB,SAAS,SAAS,QAAQ,MAAM,GAAG;MACnC;KACF;KAEA,QAAQ,kBAAkB,QAAQ,SAAS,SAAS,KAAK,SAAS,UAAU,IAAI;KAChF,IAAI,MAAM,KAAK,QAAQ,gBAAgB;UAClC,IAAI,MAAM,MAAM,QAAQ,gBAAgB;KAC7C,SAAS,SAAS,MAAM,OAAO;KAC/B,IAAI,MAAM,KAAK,QAAQ,gBAAgB;IACzC;IACA,UAAU;KACR,IAAI,cAAc,QAAQ;KAC1B,QAAQ,OAAO;IACjB;GACF;EACF;CACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"binding.js","names":["keepComments"],"sources":["../../src/collaboration/binding.ts"],"sourcesContent":["import { Fragment, type Node as PMNode, type ResolvedPos, type Schema } from 'prosemirror-model'\nimport {\n Plugin,\n PluginKey,\n TextSelection,\n type EditorState,\n type Transaction,\n} from 'prosemirror-state'\nimport type { EditorView } from 'prosemirror-view'\nimport * as Y from 'yjs'\nimport { blockAround, findBlockById } from '../core/blocks'\nimport { createId } from '../core/model'\nimport { originKey } from '../core/editor'\nimport { joinStepKey, ownStepKey } from '../core/transaction'\nimport { placeOf } from './cursors'\nimport { keptIndexes, keysBetween, orderKeys } from './order'\nimport { mergedPropsOf, newMerged, readMerged, writeMerged } from './merged-props'\nimport { cellAt, cellStart, clearTable, prepareTableText, readTable, writeTable } from './table'\nimport {\n inlineFrom,\n insertInline,\n keepInlineComments as keepComments,\n prepareTextFromBase,\n updateText,\n type Attributes,\n type TextUpdatePlan,\n} from './text'\nimport { createDiffBudget } from './inline-diff'\n\n/*\n * How a document is stored in Yjs. Every block is one entry of a `Y.Map`,\n * by id, holding its type, its props, its text and its place: the id of its\n * parent and an order key among its siblings. Moving a block only changes\n * its place, so text typed into it meanwhile by someone else stays in it.\n *\n * map: Y.Map<id, Y.Map {\n * type: string\n * props: Y.Map<name, value>\n * text: Y.Text (marks as formatting attributes, inline nodes as embeds)\n * parent: string | null\n * order: string\n * }>\n *\n * Tables keep their rows, columns and cells in more entries; see `./table`.\n * Props a block type merges item by item are maps too; see `./merged-props`.\n */\n\nexport type YBlock = Y.Map<unknown>\n\nconst ROOT = '\\u0000root'\n\nconst parentKey = (parent: string | null) => parent ?? ROOT\n\n/** The place of every block as the editor shows it, read from the document. */\ninterface Placed {\n /** The content node (paragraph, heading, …). */\n content: PMNode\n parent: string | null\n}\n\ninterface Index {\n blocks: Map<string, Placed>\n /** Child ids in order, by parent (`ROOT` for the top level). */\n children: Map<string, string[]>\n}\n\nfunction indexDoc(doc: PMNode): Index {\n const blocks = new Map<string, Placed>()\n const children = new Map<string, string[]>()\n const visit = (container: PMNode, parent: string | null) => {\n const ids: string[] = []\n container.forEach((block) => {\n const id = block.attrs.id as string\n ids.push(id)\n blocks.set(id, { content: block.firstChild!, parent })\n if (block.childCount > 1) visit(block.child(1), id)\n })\n children.set(parentKey(parent), ids)\n }\n visit(doc, null)\n return { blocks, children }\n}\n\n/** The content node a stored block stands for. Unknown types become paragraphs. */\nfunction contentFrom(yblock: YBlock, schema: Schema): PMNode {\n const name = yblock.get('type') as string\n const known = schema.nodes[name]\n const type = known?.isInGroup('blockContent') ? known : schema.nodes.paragraph!\n const stored = yblock.get('props') as Y.Map<unknown> | undefined\n const props = stored?.toJSON() ?? {}\n const merged = mergedPropsOf(type)\n const attrs: Attributes = {}\n for (const attr of Object.keys(type.spec.attrs ?? {})) {\n if (!(attr in props)) continue\n const spec = merged?.[attr]\n attrs[attr] = spec ? readMerged(stored!.get(attr), spec) : props[attr]\n }\n if (type.spec.rows) return readTable(yblock, type, attrs)\n if (!type.inlineContent) return type.create(attrs)\n const text = yblock.get('text') as Y.Text | undefined\n const inline = text ? inlineFrom(text, schema, !!type.spec.code) : []\n return type.createChecked(attrs, inline.length ? Fragment.from(inline) : null)\n}\n\nfunction writeContent(yblock: YBlock, content: PMNode) {\n yblock.set('type', content.type.name)\n const text = new Y.Text()\n yblock.set('text', text)\n if (content.type.spec.rows) {\n writeTable(yblock, null, content)\n return\n }\n const props = new Y.Map<unknown>()\n const merged = mergedPropsOf(content.type)\n yblock.set('props', props)\n for (const [name, value] of Object.entries(content.attrs)) {\n const spec = merged?.[name]\n props.set(name, spec ? newMerged(value, spec) : value)\n }\n if (content.inlineContent) insertInline(text, 0, content, 0, content.content.size)\n}\n\n/**\n * Bring a stored block's type, props and text from `before` to `after`.\n * Returns whether the stored block holds more than `after` shows (see `writeTable`).\n */\nfunction updateContent(\n yblock: YBlock,\n before: PMNode,\n after: PMNode,\n current = before,\n plans?: Map<Y.Text, TextUpdatePlan>,\n): boolean {\n if (before.type !== after.type) yblock.set('type', after.type.name)\n if (after.type.spec.rows)\n return writeTable(yblock, before.type === after.type ? before : null, after, plans)\n clearTable(yblock)\n const props = yblock.get('props') as Y.Map<unknown>\n const merged = mergedPropsOf(after.type)\n for (const [name, value] of Object.entries(after.attrs)) {\n const spec = merged?.[name]\n if (spec)\n writeMerged(\n props,\n name,\n spec,\n value,\n before.type === after.type ? before.attrs[name] : undefined,\n )\n else if (\n before.type !== after.type ||\n JSON.stringify(before.attrs[name]) !== JSON.stringify(value)\n )\n props.set(name, value)\n }\n for (const name of Array.from(props.keys())) if (!(name in after.attrs)) props.delete(name)\n\n const text = yblock.get('text') as Y.Text\n if (!after.inlineContent) {\n if (text.length) text.delete(0, text.length)\n return false\n }\n const plan = plans?.get(text)\n if (plan) plan(text)\n else updateText(text, current, after)\n return false\n}\n\n/* the stored tree → document */\n\n/**\n * Where every stored block goes. A parent that no longer exists (deleted\n * while someone moved a block into it) leaves the block at the top level;\n * a cycle (two blocks moved into each other at the same time) is broken at\n * its smallest id. Every client reads the same data the same way, so all\n * end up with the same tree.\n */\nfunction placeAll(map: Y.Map<YBlock>): Map<string, string[]> {\n const parentOf = new Map<string, string | null>()\n map.forEach((yblock, id) => {\n const parent = yblock.get('parent') as string | null | undefined\n parentOf.set(id, parent && parent !== id && map.has(parent) ? parent : null)\n })\n const settled = new Set<string>()\n for (const id of parentOf.keys()) {\n const path: string[] = []\n const onPath = new Set<string>()\n let current: string | null = id\n while (current !== null && !settled.has(current)) {\n if (onPath.has(current)) {\n const cycle = path.slice(path.indexOf(current))\n // oxlint-disable-next-line unicorn/no-array-sort -- sorts a fresh copy\n const breakAt = [...cycle].sort()[0]!\n parentOf.set(breakAt, null)\n break\n }\n path.push(current)\n onPath.add(current)\n current = parentOf.get(current) ?? null\n }\n for (const visited of path) settled.add(visited)\n }\n const children = new Map<string, string[]>()\n for (const [id, parent] of parentOf) {\n const key = parentKey(parent)\n const list = children.get(key)\n if (list) list.push(id)\n else children.set(key, [id])\n }\n const orderOf = (id: string) => (map.get(id)!.get('order') as string | undefined) ?? ''\n for (const list of children.values()) {\n // oxlint-disable-next-line unicorn/no-array-sort -- sorts our own list\n list.sort((a, b) => {\n const x = orderOf(a)\n const y = orderOf(b)\n return x < y ? -1 : x > y ? 1 : a < b ? -1 : a > b ? 1 : 0\n })\n }\n return children\n}\n\n/** Whether `$pos` is inside table structure below `depth`. */\nfunction inTable($pos: ResolvedPos, depth: number) {\n for (let d = $pos.depth; d > depth; d--) if ($pos.node(d).type.spec.tableRole) return true\n return false\n}\n\n/** A position as its table cell and offset in it, if it is in one. */\nfunction cellPlace(doc: PMNode, pos: number) {\n const info = blockAround(doc.resolve(pos))\n if (!info?.content.type.spec.rows) return null\n const cell = cellAt(info.content, pos - info.contentPos - 1)\n return cell && { block: info.node.attrs.id as string, ...cell }\n}\n\n/** Put a cursor that was in a replaced table back into its cell. */\nfunction keepCellSelection(state: EditorState, tr: Transaction) {\n const { selection } = state\n if (!(selection instanceof TextSelection)) return\n const back = (pos: number) => {\n const place = cellPlace(state.doc, pos)\n if (!place) return null\n const info = findBlockById(tr.doc, place.block)\n const cell = info && cellStart(info.content, place.key)\n return cell ? info.contentPos + 1 + cell.offset + Math.min(place.index, cell.size) : null\n }\n const anchor = back(selection.anchor)\n const head = back(selection.head)\n if (anchor !== null && head !== null) tr.setSelection(TextSelection.create(tr.doc, anchor, head))\n}\n\n/* the binding */\n\nexport const collaborationKey = new PluginKey<{\n remote: boolean\n tracked: boolean\n /** An undo step of its own (`transact`). */\n own: boolean\n /** Joins the undo step before (the frames of a drag on a whiteboard). */\n join: boolean\n}>('dou-editor-collaboration')\n\nexport interface Binding {\n map: Y.Map<YBlock>\n /** The origin of this editor's own changes; the undo manager tracks it. */\n origin: object\n /** The origin of changes that are not undone (`setBlocks`, fixes). */\n untracked: object\n view: EditorView | null\n /** Ends the current undo step, so a structural change is undone on its own. */\n stopCapturing?: () => void\n /** Makes the next change join the current undo step. */\n joinCapturing?: () => void\n /** Where the cursor was before the change being written, for undo to return to. */\n selectionBefore?: unknown\n /** Set while the undo manager takes a step back or forward. */\n undoing?: 'undo' | 'redo'\n}\n\nexport function createBinding(map: Y.Map<YBlock>): Binding {\n return { map, origin: { binding: 'tracked' }, untracked: { binding: 'untracked' }, view: null }\n}\n\n/** Write the blocks of `doc` into an empty map. */\nexport function writeDoc(map: Y.Map<YBlock>, doc: PMNode) {\n const index = indexDoc(doc)\n map.doc!.transact(() => {\n for (const [key, ids] of index.children) {\n const keys = keysBetween(null, null, ids.length)\n ids.forEach((id, i) => {\n const yblock = new Y.Map<unknown>()\n map.set(id, yblock)\n yblock.set('parent', key === ROOT ? null : key)\n yblock.set('order', keys[i])\n writeContent(yblock, index.blocks.get(id)!.content)\n })\n }\n })\n}\n\n/**\n * Write the changes from `before` to `after` into the map: the content of\n * blocks that changed, blocks added and removed, and the places of the\n * blocks that moved. Returns the blocks that hold more than `after` shows,\n * to be read back (see `writeTable`). Call it inside a transaction.\n */\nfunction writeChanges(map: Y.Map<YBlock>, before: Index, after: Index, schema: Schema): string[] {\n const stale: string[] = []\n for (const [id, placed] of after.blocks) {\n const yblock = map.get(id)\n const old = before.blocks.get(id)\n if (!yblock) {\n const created = new Y.Map<unknown>()\n map.set(id, created)\n writeContent(created, placed.content)\n } else if (old && old.content !== placed.content) {\n if (updateContent(yblock, old.content, placed.content)) stale.push(id)\n } else if (!old) {\n // stored, but not shown before (e.g. the empty placeholder came back)\n if (updateContent(yblock, contentFrom(yblock, schema), placed.content)) stale.push(id)\n }\n }\n for (const id of before.blocks.keys()) if (!after.blocks.has(id)) map.delete(id)\n // places: only lists that changed, and only the blocks that moved in them\n for (const [key, ids] of after.children) {\n const old = before.children.get(key)\n if (old && old.length === ids.length && old.every((id, i) => ids[i] === id)) {\n if (ids.every((id) => map.get(id)!.get('order') !== undefined)) continue\n }\n const parent = key === ROOT ? null : key\n const keys = ids.map((id) => {\n const yblock = map.get(id)!\n const order = yblock.get('order') as string | undefined\n return yblock.get('parent') === parent && order !== undefined ? order : null\n })\n const next = orderKeys(keys)\n ids.forEach((id, index) => {\n if (keys[index] === next[index]) return\n const yblock = map.get(id)!\n if (yblock.get('parent') !== parent) yblock.set('parent', parent)\n yblock.set('order', next[index])\n })\n }\n return stale\n}\n\n/* writing a new version from elsewhere, without an editor (`updateMap`) */\n\n/** The content node of block `id`. */\nconst contentOf = (index: Index, id: string) => index.blocks.get(id)!.content\n\n/** A content node as a string, to tell equal blocks apart quickly. */\nconst keyOf = (content: PMNode) => JSON.stringify(content.toJSON())\n\n/**\n * Pairs of `a` and `b` with equal keys, in the order of both: the equal\n * start and end, then in the middle each of `a` with the first free equal\n * one of `b`, keeping the longest run in order. Indexes into `a` and `b`;\n * about linear, so a long document stays fast.\n */\nfunction equalPairs(a: string[], b: string[]): [number, number][] {\n const pairs: [number, number][] = []\n let start = 0\n while (start < a.length && start < b.length && a[start] === b[start]) {\n pairs.push([start, start])\n start++\n }\n let endA = a.length\n let endB = b.length\n const tail: [number, number][] = []\n while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {\n endA--\n endB--\n tail.push([endA, endB])\n }\n // the middle: each of `a` takes the first free equal one of `b`, then the\n // longest run increasing in `b` stays\n const free = new Map<string, number[]>()\n for (let j = endB - 1; j >= start; j--) {\n const list = free.get(b[j]!)\n if (list) list.push(j)\n else free.set(b[j]!, [j])\n }\n const middle: [number, number][] = []\n for (let i = start; i < endA; i++) {\n const j = free.get(a[i]!)?.pop()\n if (j !== undefined) middle.push([i, j])\n }\n const kept = keptIndexes(middle.map(([, j]) => String(j).padStart(10, '0')))\n middle.forEach((pair, index) => kept.has(index) && pairs.push(pair))\n for (let k = tail.length - 1; k >= 0; k--) pairs.push(tail[k]!)\n return pairs\n}\n\n/** How alike two texts are, 0 to 1, by the pairs of letters they share. */\nfunction likeness(a: string, b: string): number {\n if (a === b) return 1\n if (a.length < 2 || b.length < 2) return 0\n const pairs = new Map<string, number>()\n for (let i = 0; i < a.length - 1; i++) {\n const pair = a.slice(i, i + 2)\n pairs.set(pair, (pairs.get(pair) ?? 0) + 1)\n }\n let shared = 0\n for (let i = 0; i < b.length - 1; i++) {\n const pair = b.slice(i, i + 2)\n const left = pairs.get(pair)\n if (left) {\n shared++\n pairs.set(pair, left - 1)\n }\n }\n return (2 * shared) / (a.length + b.length - 2)\n}\n\n/** How alike two blocks must be to count as one edited. */\nconst ALIKE = 0.4\n/** How many stored blocks ahead a block is looked for between two equal ones. */\nconst LOOK_AHEAD = 32\n/** How alike a block moved out of its order must be to count as the same one edited. */\nconst MOVED_ALIKE = 0.8\n/** The most comparisons spent on blocks moved out of their order. */\nconst MOVED_PAIRS = 40_000\n\n/**\n * Which block of `before` each block of `after` stands for: by id when it\n * keeps one, otherwise by place among the blocks of the same parent: first\n * the blocks with equal content, kept in order, then between those, each\n * block with the most alike one of its type further on (its text is\n * changed in place). A block with nothing alike enough is new. Returns\n * new id → old id.\n */\nfunction matchBlocks(before: Index, after: Index): Map<string, string> {\n const match = new Map<string, string>()\n const used = new Set<string>()\n for (const id of after.blocks.keys())\n if (before.blocks.has(id)) {\n match.set(id, id)\n used.add(id)\n }\n const pair = (a: string, b: string) => {\n match.set(a, b)\n used.add(b)\n }\n const visit = (key: string, oldKey: string | null) => {\n const ids = after.children.get(key) ?? []\n const fresh = ids.filter((id) => !match.has(id))\n const olds = (oldKey === null ? [] : (before.children.get(oldKey) ?? [])).filter(\n (id) => !used.has(id),\n )\n const anchors = equalPairs(\n fresh.map((id) => keyOf(contentOf(after, id))),\n olds.map((id) => keyOf(contentOf(before, id))),\n )\n let i = 0\n let j = 0\n for (const [ai, aj] of [...anchors, [fresh.length, olds.length] as [number, number]]) {\n // between two equal blocks: each takes the most alike block of its type ahead\n for (; i < ai; i++) {\n const node = contentOf(after, fresh[i]!)\n const text = node.textContent\n let best = -1\n let score = ALIKE\n for (let k = j; k < Math.min(aj, j + LOOK_AHEAD); k++) {\n const old = contentOf(before, olds[k]!)\n if (old.type !== node.type) continue\n const like = likeness(text, old.textContent)\n if (like > score || (best < 0 && like >= score)) {\n best = k\n score = like\n }\n }\n if (best >= 0) {\n pair(fresh[i]!, olds[best]!)\n j = best + 1\n }\n }\n if (ai < fresh.length) pair(fresh[ai]!, olds[aj]!)\n i = ai + 1\n j = aj + 1\n }\n // what is left: blocks moved past others, e.g. paragraphs put in another order;\n // the same content first, then very alike text, wherever it is in the list\n const leftNew = fresh.filter((id) => !match.has(id))\n const leftOld = olds.filter((id) => !used.has(id))\n if (leftNew.length && leftOld.length) {\n const byKey = new Map<string, string[]>()\n // last first, so `pop` takes them in order\n for (let k = leftOld.length - 1; k >= 0; k--) {\n const id = leftOld[k]!\n const text = keyOf(contentOf(before, id))\n const list = byKey.get(text)\n if (list) list.push(id)\n else byKey.set(text, [id])\n }\n const rest: string[] = []\n for (const id of leftNew) {\n const old = byKey.get(keyOf(contentOf(after, id)))?.pop()\n if (old) pair(id, old)\n else rest.push(id)\n }\n if (rest.length * leftOld.length <= MOVED_PAIRS)\n for (const id of rest) {\n const node = contentOf(after, id)\n let best: string | null = null\n let score = MOVED_ALIKE\n for (const old of leftOld) {\n if (used.has(old) || contentOf(before, old).type !== node.type) continue\n const like = likeness(node.textContent, contentOf(before, old).textContent)\n if (like >= score) {\n best = old\n score = like\n }\n }\n if (best) pair(id, best)\n }\n }\n for (const id of ids) if (after.children.has(id)) visit(id, match.get(id) ?? null)\n }\n visit(ROOT, ROOT)\n return match\n}\n\n/** Every block id in `index` under `id`, at any depth. */\nfunction descendants(index: Index, id: string, out: string[] = []): string[] {\n for (const child of index.children.get(id) ?? []) {\n out.push(child)\n descendants(index, child, out)\n }\n return out\n}\n\n/**\n * Change the map from `base` to `doc`, leaving what changed in the map since\n * `base` was read: blocks of `doc` are matched to those of `base` (see\n * `matchBlocks`), and those of `base` to the stored ones; only blocks that\n * differ between `base` and `doc` are written, removed or moved. Without\n * `base`, the map as it is now. Call it inside a transaction.\n */\nexport function updateDoc(map: Y.Map<YBlock>, doc: PMNode, schema: Schema, base?: PMNode | null) {\n const current = readDoc(map, schema)\n if (!current) return void writeDoc(map, doc)\n const now = indexDoc(current)\n const from = base ? indexDoc(base) : now\n const to = indexDoc(doc)\n const toBase = matchBlocks(from, to)\n const baseToNow = base ? matchBlocks(now, from) : null\n /** The stored block a block of `base` is now, if it still is. */\n const nowOf = (old: string) => {\n const there = baseToNow ? baseToNow.get(old) : old\n return there !== undefined && now.blocks.has(there) ? there : null\n }\n /** The stored block each block of `doc` is written to. */\n const stored = new Map<string, string>()\n for (const [id, old] of toBase) {\n const there = nowOf(old)\n if (there) stored.set(id, there)\n }\n // removed by someone else since `base`: it stays removed, even if `doc`\n // changed it, and so do the blocks `doc` adds under it\n const gone = new Set<string>()\n const walk = (key: string, inGone: boolean) => {\n for (const id of to.children.get(key) ?? []) {\n const old = toBase.get(id)\n const lost = (old !== undefined && !stored.has(id)) || (inGone && !stored.has(id))\n if (lost) gone.add(id)\n walk(id, lost)\n }\n }\n walk(ROOT, false)\n // ids of `doc` for its new blocks: kept unless the map has them already\n for (const id of to.blocks.keys())\n if (!stored.has(id) && !gone.has(id)) stored.set(id, map.has(id) ? createId() : id)\n const kept = new Set(stored.values())\n const inDoc = new Set(toBase.values())\n\n // Plan every content change before touching the shared map. Exact\n // diff has a work limit; exceeding it must reject the whole write.\n const budget = createDiffBudget()\n const contentUpdates = new Map<string, { before: PMNode; after: PMNode; current: PMNode }>()\n const textPlans = new Map<Y.Text, TextUpdatePlan>()\n for (const [id, placed] of to.blocks) {\n if (gone.has(id)) continue\n const target = stored.get(id)!\n const there = now.blocks.get(target)?.content\n if (!there) continue\n const old = toBase.get(id)\n const was = old !== undefined ? from.blocks.get(old)!.content : null\n const compared = was ? keepComments(was, placed.content, budget) : placed.content\n if (was && compared.eq(was)) continue\n const before = was && was.type === there.type && was.type === placed.content.type ? was : there\n const after = before === was ? compared : keepComments(before, placed.content, budget)\n const yblock = map.get(target)!\n contentUpdates.set(id, { before, after, current: there })\n if (after.type.spec.rows) {\n prepareTableText(yblock, before.type === after.type ? before : null, after, textPlans, budget)\n } else if (after.inlineContent) {\n const text = yblock.get('text') as Y.Text\n const currentInline = there.inlineContent\n ? there\n : after.type.create(after.attrs, inlineFrom(text, schema, !!after.type.spec.code))\n const baseInline = before.inlineContent ? before : currentInline\n textPlans.set(text, prepareTextFromBase(baseInline, after, currentInline, budget))\n }\n }\n\n // removed: what `base` had and `doc` hasn't, with what is stored under it\n for (const old of from.blocks.keys()) {\n const there = inDoc.has(old) ? null : nowOf(old)\n if (!there) continue\n for (const id of [there, ...descendants(now, there)])\n if (!kept.has(id) && map.has(id)) map.delete(id)\n }\n\n // content: new blocks written, those changed since `base` brought to `doc`\n for (const [id, placed] of to.blocks) {\n if (gone.has(id)) continue\n const target = stored.get(id)!\n const yblock = map.get(target)\n if (!now.blocks.has(target)) {\n const created = new Y.Map<unknown>()\n map.set(target, created)\n writeContent(created, placed.content)\n continue\n }\n const update = contentUpdates.get(id)\n if (update) updateContent(yblock!, update.before, update.after, update.current, textPlans)\n }\n\n // places: in each list of `doc`, the blocks new there or out of their order in `base`\n for (const [key, all] of to.children) {\n if (gone.has(key)) continue\n const ids = all.filter((id) => !gone.has(id))\n const parent = key === ROOT ? null : stored.get(key)!\n const oldParent = key === ROOT ? ROOT : toBase.get(key)\n const siblings = oldParent === undefined ? [] : (from.children.get(oldParent) ?? [])\n const at = new Map(siblings.map((id, index) => [id, index]))\n const inBase = ids.map((id) => {\n const old = toBase.get(id)\n return old !== undefined && at.has(old) ? String(at.get(old)!).padStart(10, '0') : null\n })\n const stays = keptIndexes(inBase)\n const moved = new Set(ids.filter((_, index) => !stays.has(index)).map((id) => stored.get(id)!))\n if (!moved.size) continue\n // the stored list, without what goes elsewhere, with the moved ones put after their neighbour in `doc`\n const list = (now.children.get(parentKey(parent)) ?? []).filter(\n (id) => map.has(id) && !moved.has(id),\n )\n let previous: string | null = null\n for (const id of ids) {\n const target = stored.get(id)!\n if (moved.has(target))\n list.splice(previous === null ? 0 : list.indexOf(previous) + 1, 0, target)\n if (list.includes(target)) previous = target\n }\n const keys = list.map((id) => {\n const yblock = map.get(id)!\n const order = yblock.get('order') as string | undefined\n return !moved.has(id) && yblock.get('parent') === parent && order !== undefined ? order : null\n })\n const next = orderKeys(keys)\n list.forEach((id, index) => {\n if (keys[index] === next[index]) return\n const yblock = map.get(id)!\n if (yblock.get('parent') !== parent) yblock.set('parent', parent)\n yblock.set('order', next[index])\n })\n }\n}\n\n/** The document a map holds, read without an editor; `null` while the map is empty. */\nexport function readDoc(map: Y.Map<YBlock>, schema: Schema): PMNode | null {\n const children = placeAll(map)\n const build = (id: string): PMNode => {\n const content = contentFrom(map.get(id)!, schema)\n const kids = children.get(id)\n const parts = kids?.length\n ? [content, schema.nodes.blockGroup!.create(null, kids.map(build))]\n : [content]\n return schema.nodes.block!.create({ id }, parts)\n }\n const top = children.get(ROOT) ?? []\n return top.length ? schema.nodes.doc!.create(null, top.map(build)) : null\n}\n\nexport function syncPlugin(binding: Binding): Plugin {\n const { map } = binding\n const ydoc = map.doc!\n let schema: Schema\n // the document as last written to or read from the map\n let synced: Index | null = null\n // content nodes read from the map, by id, until the block changes there\n const cache = new Map<string, PMNode>()\n let placeholderId = createId()\n\n function buildDoc(): PMNode {\n const children = placeAll(map)\n const build = (id: string): PMNode => {\n let content = cache.get(id)\n if (!content) {\n content = contentFrom(map.get(id)!, schema)\n cache.set(id, content)\n }\n const kids = children.get(id)\n const parts = kids?.length\n ? [content, schema.nodes.blockGroup!.create(null, kids.map(build))]\n : [content]\n return schema.nodes.block!.create({ id }, parts)\n }\n const top = children.get(ROOT) ?? []\n // an empty map still shows one empty paragraph; it is stored once typed in\n const blocks = top.length\n ? top.map(build)\n : [schema.nodes.block!.create({ id: placeholderId }, schema.nodes.paragraph!.create())]\n return schema.nodes.doc!.create(null, blocks)\n }\n\n /** Show the map's current state in the editor. */\n function readMap(view: EditorView) {\n const doc = view.state.doc\n const next = buildDoc()\n const start = doc.content.findDiffStart(next.content)\n if (start !== null) {\n const end = doc.content.findDiffEnd(next.content)!\n const overlap = start - Math.min(end.a, end.b)\n if (overlap > 0) {\n end.a += overlap\n end.b += overlap\n }\n let from = start\n let to = end.a\n const $from = doc.resolve(from)\n const $to = doc.resolve(to)\n const shared = $from.sharedDepth(to)\n // a change across table cells or rows: swap them whole, a partial cut breaks the grid\n if (!$from.node(shared).inlineContent && (inTable($from, shared) || inTable($to, shared))) {\n if ($from.depth > shared) from = $from.before(shared + 1)\n if ($to.depth > shared) to = $to.after(shared + 1)\n }\n const tail = doc.content.size - to\n const tr = view.state.tr.replace(from, to, next.slice(from, next.content.size - tail))\n if (to !== end.a || from !== start) keepCellSelection(view.state, tr)\n tr.setMeta(collaborationKey, { remote: true })\n tr.setMeta('remote', true)\n // this user's undo comes back through the shared document too\n if (binding.undoing) tr.setMeta(originKey, binding.undoing)\n tr.setMeta('addToHistory', false)\n view.dispatch(tr)\n }\n synced = indexDoc(view.state.doc)\n }\n\n /** Write the editor's changes since the last sync into the map. */\n function writeMap(view: EditorView, tracked: boolean) {\n const doc = view.state.doc\n const before = synced ?? indexDoc(doc)\n const after = indexDoc(doc)\n // moving, indenting or adding blocks is an undo step of its own\n let structural = false\n for (const [key, ids] of after.children) {\n const old = before.children.get(key)\n if (!old || old.length !== ids.length || old.some((id, i) => ids[i] !== id)) structural = true\n }\n if (structural) binding.stopCapturing?.()\n const stale = ydoc.transact(\n () => writeChanges(map, before, after, schema),\n tracked ? binding.origin : binding.untracked,\n )\n for (const id of after.blocks.keys()) cache.set(id, after.blocks.get(id)!.content)\n for (const id of before.blocks.keys()) if (!after.blocks.has(id)) cache.delete(id)\n if (structural) binding.stopCapturing?.()\n synced = after\n if (stale.length) {\n for (const id of stale) cache.delete(id)\n // not from inside this update: show the rest once it is done\n queueMicrotask(() => {\n if (binding.view === view) readMap(view)\n })\n }\n }\n\n return new Plugin({\n key: collaborationKey,\n state: {\n init: () => ({ remote: false, tracked: false, own: false, join: false }),\n apply(tr, value) {\n const meta = tr.getMeta(collaborationKey) as { remote?: boolean } | undefined\n if (!tr.docChanged) return value\n if (meta?.remote) return { remote: true, tracked: false, own: false, join: false }\n // a transaction can come with appended ones (e.g. id fixes), written\n // together: undone together if any of them is\n const tracked = tr.getMeta('addToHistory') !== false\n const own = tr.getMeta(ownStepKey) === true\n const join = tr.getMeta(joinStepKey) === true\n // a step joined on purpose stands on its own here (its\n // `appendedTransaction` is for the built-in history)\n const appended = !join && !!tr.getMeta('appendedTransaction') && !value.remote\n return appended\n ? {\n remote: false,\n tracked: value.tracked || tracked,\n own: value.own || own,\n join: value.join || join,\n }\n : { remote: false, tracked, own, join }\n },\n },\n view(view) {\n schema = view.state.schema\n binding.view = view\n const observer = (\n events: Y.YEvent<Y.AbstractType<unknown>>[],\n transaction: Y.Transaction,\n ) => {\n if (transaction.origin === binding.origin || transaction.origin === binding.untracked)\n return\n // forget the cached content of every block that changed\n for (const event of events) {\n if (event.target === map) {\n for (const id of (event as Y.YMapEvent<unknown>).keysChanged) cache.delete(id)\n continue\n }\n // the path from the map starts with the block's id\n const id = event.path[0]\n if (typeof id === 'string') cache.delete(id)\n }\n readMap(view)\n }\n map.observeDeep(observer)\n if (map.size) readMap(view)\n else synced = indexDoc(view.state.doc)\n placeholderId = (view.state.doc.firstChild?.attrs.id as string | undefined) ?? placeholderId\n return {\n update(current, previous: EditorState) {\n if (current.state.doc === previous.doc) return\n const state = collaborationKey.getState(current.state)!\n if (state.remote) {\n synced = indexDoc(current.state.doc)\n return\n }\n // read before writing: the map still holds the previous text\n binding.selectionBefore = placeOf(binding, previous.doc, previous.selection.head)\n if (state.own) binding.stopCapturing?.()\n else if (state.join) binding.joinCapturing?.()\n writeMap(current, state.tracked)\n if (state.own) binding.stopCapturing?.()\n },\n destroy() {\n map.unobserveDeep(observer)\n binding.view = null\n },\n }\n },\n })\n}\n\nexport type { Transaction }\n"],"mappings":";;;;;;;;;;;;;;AAiDA,IAAM,OAAO;AAEb,IAAM,aAAa,WAA0B,UAAU;AAevD,SAAS,SAAS,KAAoB;CACpC,MAAM,yBAAS,IAAI,IAAoB;CACvC,MAAM,2BAAW,IAAI,IAAsB;CAC3C,MAAM,SAAS,WAAmB,WAA0B;EAC1D,MAAM,MAAgB,CAA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
2
|
+
var createDiffBudget = () => ({ left: 8e6 });
|
|
3
|
+
var failed = () => /* @__PURE__ */ new Error("[@dou-ui/editor] updateMap cannot safely merge this text within its comparison limit. Write a smaller change, or read the current document and reapply the change before retrying; the shared document has not been changed.");
|
|
4
|
+
function spend(budget) {
|
|
5
|
+
if (--budget.left < 0) throw failed();
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The middle of the shortest edit path from `a[aLo..aHi)` to `b[bLo..bHi)`,
|
|
9
|
+
* found from both ends at once (Myers, linear space), or `null` when the
|
|
10
|
+
* two share nothing. All regions use the same operation budget.
|
|
11
|
+
*/
|
|
12
|
+
function bisect(a, aLo, aHi, b, bLo, bHi, budget) {
|
|
13
|
+
const n = aHi - aLo;
|
|
14
|
+
const m = bHi - bLo;
|
|
15
|
+
const maxD = Math.ceil((n + m) / 2);
|
|
16
|
+
const offset = maxD + 1;
|
|
17
|
+
const size = 2 * offset + 1;
|
|
18
|
+
const v1 = new Int32Array(size).fill(-1);
|
|
19
|
+
const v2 = new Int32Array(size).fill(-1);
|
|
20
|
+
v1[offset + 1] = 0;
|
|
21
|
+
v2[offset + 1] = 0;
|
|
22
|
+
const delta = n - m;
|
|
23
|
+
const front = (delta & 1) !== 0;
|
|
24
|
+
let k1start = 0;
|
|
25
|
+
let k1end = 0;
|
|
26
|
+
let k2start = 0;
|
|
27
|
+
let k2end = 0;
|
|
28
|
+
for (let d = 0; d < maxD; d++) {
|
|
29
|
+
for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
|
30
|
+
spend(budget);
|
|
31
|
+
const i = offset + k1;
|
|
32
|
+
let x1 = k1 === -d || k1 !== d && v1[i - 1] < v1[i + 1] ? v1[i + 1] : v1[i - 1] + 1;
|
|
33
|
+
let y1 = x1 - k1;
|
|
34
|
+
while (x1 < n && y1 < m && a[aLo + x1] === b[bLo + y1]) {
|
|
35
|
+
spend(budget);
|
|
36
|
+
x1++;
|
|
37
|
+
y1++;
|
|
38
|
+
}
|
|
39
|
+
v1[i] = x1;
|
|
40
|
+
if (x1 > n) k1end += 2;
|
|
41
|
+
else if (y1 > m) k1start += 2;
|
|
42
|
+
else if (front) {
|
|
43
|
+
const j = offset + delta - k1;
|
|
44
|
+
if (j >= 0 && j < size && v2[j] !== -1 && x1 >= n - v2[j]) return [x1, y1];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (let k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
|
|
48
|
+
spend(budget);
|
|
49
|
+
const i = offset + k2;
|
|
50
|
+
let x2 = k2 === -d || k2 !== d && v2[i - 1] < v2[i + 1] ? v2[i + 1] : v2[i - 1] + 1;
|
|
51
|
+
let y2 = x2 - k2;
|
|
52
|
+
while (x2 < n && y2 < m && a[aHi - x2 - 1] === b[bHi - y2 - 1]) {
|
|
53
|
+
spend(budget);
|
|
54
|
+
x2++;
|
|
55
|
+
y2++;
|
|
56
|
+
}
|
|
57
|
+
v2[i] = x2;
|
|
58
|
+
if (x2 > n) k2end += 2;
|
|
59
|
+
else if (y2 > m) k2start += 2;
|
|
60
|
+
else if (!front) {
|
|
61
|
+
const j = offset + delta - k2;
|
|
62
|
+
if (j >= 0 && j < size && v1[j] !== -1) {
|
|
63
|
+
const x1 = v1[j];
|
|
64
|
+
const y1 = x1 - (j - offset);
|
|
65
|
+
if (x1 >= n - x2) return [x1, y1];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
/** The changes from `a[aLo..aHi)` to `b[bLo..bHi)`, in order, pushed to `out`. */
|
|
73
|
+
function diffRange(a, aLo, aHi, b, bLo, bHi, budget, out, depth = 0) {
|
|
74
|
+
if (depth > 256) throw failed();
|
|
75
|
+
while (aLo < aHi && bLo < bHi && a[aLo] === b[bLo]) {
|
|
76
|
+
spend(budget);
|
|
77
|
+
aLo++;
|
|
78
|
+
bLo++;
|
|
79
|
+
}
|
|
80
|
+
while (aLo < aHi && bLo < bHi && a[aHi - 1] === b[bHi - 1]) {
|
|
81
|
+
spend(budget);
|
|
82
|
+
aHi--;
|
|
83
|
+
bHi--;
|
|
84
|
+
}
|
|
85
|
+
if (aLo === aHi && bLo === bHi) return;
|
|
86
|
+
if (aLo === aHi || bLo === bHi) {
|
|
87
|
+
out.push([
|
|
88
|
+
aLo,
|
|
89
|
+
aHi,
|
|
90
|
+
bLo,
|
|
91
|
+
bHi
|
|
92
|
+
]);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const seen = /* @__PURE__ */ new Set();
|
|
96
|
+
for (let i = aLo; i < aHi; i++) {
|
|
97
|
+
spend(budget);
|
|
98
|
+
seen.add(a[i]);
|
|
99
|
+
}
|
|
100
|
+
let common = false;
|
|
101
|
+
for (let i = bLo; i < bHi; i++) {
|
|
102
|
+
spend(budget);
|
|
103
|
+
if (seen.has(b[i])) {
|
|
104
|
+
common = true;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!common) {
|
|
109
|
+
out.push([
|
|
110
|
+
aLo,
|
|
111
|
+
aHi,
|
|
112
|
+
bLo,
|
|
113
|
+
bHi
|
|
114
|
+
]);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const split = bisect(a, aLo, aHi, b, bLo, bHi, budget);
|
|
118
|
+
if (!split || !split[0] && !split[1] || split[0] === aHi - aLo && split[1] === bHi - bLo) throw failed();
|
|
119
|
+
diffRange(a, aLo, aLo + split[0], b, bLo, bLo + split[1], budget, out, depth + 1);
|
|
120
|
+
diffRange(a, aLo + split[0], aHi, b, bLo + split[1], bHi, budget, out, depth + 1);
|
|
121
|
+
}
|
|
122
|
+
/** The changes between two lists, items compared with `===`, with touching ones joined. */
|
|
123
|
+
function diffLists(a, b, budget = createDiffBudget()) {
|
|
124
|
+
const raw = [];
|
|
125
|
+
diffRange(a, 0, a.length, b, 0, b.length, budget, raw);
|
|
126
|
+
const out = [];
|
|
127
|
+
for (const change of raw) {
|
|
128
|
+
const last = out[out.length - 1];
|
|
129
|
+
if (last && last[1] === change[0] && last[3] === change[2]) {
|
|
130
|
+
last[1] = change[1];
|
|
131
|
+
last[3] = change[3];
|
|
132
|
+
} else out.push(change);
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
export { createDiffBudget, diffLists };
|
|
137
|
+
|
|
138
|
+
//# sourceMappingURL=inline-diff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inline-diff.js","names":[],"sources":["../../src/collaboration/inline-diff.ts"],"sourcesContent":["// Exact Myers diff with a shared operation budget. A rendering diff may\n// approximate a difficult edit; a collaborative write must never do so.\n/** A changed range by index: `a[aFrom..aTo)` became `b[bFrom..bTo)`. */\ntype Change = [aFrom: number, aTo: number, bFrom: number, bTo: number]\nexport interface DiffBudget {\n left: number\n}\nexport const createDiffBudget = (): DiffBudget => ({ left: 8_000_000 })\nconst failed = () =>\n new Error(\n '[@dou-ui/editor] updateMap cannot safely merge this text within its comparison limit. Write a smaller change, or read the current document and reapply the change before retrying; the shared document has not been changed.',\n )\nfunction spend(budget: DiffBudget) {\n if (--budget.left < 0) throw failed()\n}\n\n/**\n * The middle of the shortest edit path from `a[aLo..aHi)` to `b[bLo..bHi)`,\n * found from both ends at once (Myers, linear space), or `null` when the\n * two share nothing. All regions use the same operation budget.\n */\nfunction bisect(\n a: ArrayLike<unknown>,\n aLo: number,\n aHi: number,\n b: ArrayLike<unknown>,\n bLo: number,\n bHi: number,\n budget: DiffBudget,\n): [number, number] | null {\n const n = aHi - aLo\n const m = bHi - bLo\n const maxD = Math.ceil((n + m) / 2)\n const offset = maxD + 1\n const size = 2 * offset + 1\n const v1 = new Int32Array(size).fill(-1)\n const v2 = new Int32Array(size).fill(-1)\n v1[offset + 1] = 0\n v2[offset + 1] = 0\n const delta = n - m\n // With an odd difference the paths meet on a forward step, else on a reverse one.\n const front = (delta & 1) !== 0\n let k1start = 0\n let k1end = 0\n let k2start = 0\n let k2end = 0\n for (let d = 0; d < maxD; d++) {\n for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {\n spend(budget)\n const i = offset + k1\n let x1 = k1 === -d || (k1 !== d && v1[i - 1]! < v1[i + 1]!) ? v1[i + 1]! : v1[i - 1]! + 1\n let y1 = x1 - k1\n while (x1 < n && y1 < m && a[aLo + x1] === b[bLo + y1]) {\n spend(budget)\n x1++\n y1++\n }\n v1[i] = x1\n if (x1 > n) k1end += 2\n else if (y1 > m) k1start += 2\n else if (front) {\n const j = offset + delta - k1\n if (j >= 0 && j < size && v2[j] !== -1 && x1 >= n - v2[j]!) return [x1, y1]\n }\n }\n for (let k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {\n spend(budget)\n const i = offset + k2\n let x2 = k2 === -d || (k2 !== d && v2[i - 1]! < v2[i + 1]!) ? v2[i + 1]! : v2[i - 1]! + 1\n let y2 = x2 - k2\n while (x2 < n && y2 < m && a[aHi - x2 - 1] === b[bHi - y2 - 1]) {\n spend(budget)\n x2++\n y2++\n }\n v2[i] = x2\n if (x2 > n) k2end += 2\n else if (y2 > m) k2start += 2\n else if (!front) {\n const j = offset + delta - k2\n if (j >= 0 && j < size && v1[j] !== -1) {\n const x1 = v1[j]!\n const y1 = x1 - (j - offset)\n if (x1 >= n - x2) return [x1, y1]\n }\n }\n }\n }\n return null\n}\n\n/** The changes from `a[aLo..aHi)` to `b[bLo..bHi)`, in order, pushed to `out`. */\nfunction diffRange(\n a: ArrayLike<unknown>,\n aLo: number,\n aHi: number,\n b: ArrayLike<unknown>,\n bLo: number,\n bHi: number,\n budget: DiffBudget,\n out: Change[],\n depth = 0,\n) {\n if (depth > 256) throw failed()\n while (aLo < aHi && bLo < bHi && a[aLo] === b[bLo]) {\n spend(budget)\n aLo++\n bLo++\n }\n while (aLo < aHi && bLo < bHi && a[aHi - 1] === b[bHi - 1]) {\n spend(budget)\n aHi--\n bHi--\n }\n if (aLo === aHi && bLo === bHi) return\n if (aLo === aHi || bLo === bHi) {\n out.push([aLo, aHi, bLo, bHi])\n return\n }\n // Entirely different alphabets need no edit-path search. This is an\n // exact replacement, including for very long generated text.\n const seen = new Set<unknown>()\n for (let i = aLo; i < aHi; i++) {\n spend(budget)\n seen.add(a[i])\n }\n let common = false\n for (let i = bLo; i < bHi; i++) {\n spend(budget)\n if (seen.has(b[i])) {\n common = true\n break\n }\n }\n if (!common) {\n out.push([aLo, aHi, bLo, bHi])\n return\n }\n const split = bisect(a, aLo, aHi, b, bLo, bHi, budget)\n if (!split || (!split[0] && !split[1]) || (split[0] === aHi - aLo && split[1] === bHi - bLo))\n throw failed()\n diffRange(a, aLo, aLo + split[0], b, bLo, bLo + split[1], budget, out, depth + 1)\n diffRange(a, aLo + split[0], aHi, b, bLo + split[1], bHi, budget, out, depth + 1)\n}\n\n/** The changes between two lists, items compared with `===`, with touching ones joined. */\nexport function diffLists(\n a: ArrayLike<unknown>,\n b: ArrayLike<unknown>,\n budget = createDiffBudget(),\n): Change[] {\n const raw: Change[] = []\n diffRange(a, 0, a.length, b, 0, b.length, budget, raw)\n const out: Change[] = []\n for (const change of raw) {\n const last = out[out.length - 1]\n if (last && last[1] === change[0] && last[3] === change[2]) {\n last[1] = change[1]\n last[3] = change[3]\n } else out.push(change)\n }\n return out\n}\n"],"mappings":";AAOA,IAAa,0BAAsC,EAAE,MAAM,IAAU;AACrE,IAAM,+BACJ,IAAI,MACF,8NACF;AACF,SAAS,MAAM,QAAoB;CACjC,IAAI,EAAE,OAAO,OAAO,GAAG,MAAM,OAAO;AACtC;;;;;;AAOA,SAAS,OACP,GACA,KACA,KACA,GACA,KACA,KACA,QACyB;CACzB,MAAM,IAAI,MAAM;CAChB,MAAM,IAAI,MAAM;CAChB,MAAM,OAAO,KAAK,MAAM,IAAI,KAAK,CAAC;CAClC,MAAM,SAAS,OAAO;CACtB,MAAM,OAAO,IAAI,SAAS;CAC1B,MAAM,KAAK,IAAI,WAAW,IAAI,CA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AO,GAAG,KAAK,KAAK,GAAG,KAAK,KAAK,MAAM;CACrD,IAAI,CAAC,SAAU,CAAC,MAAM,MAAM,CAAC,MAAM,MAAQ,MAAM,OAAO,MAAM,OAAO,MAAM,OAAO,MAAM,KACtF,MAAM,OAAO;CACf,UAAU,GAAG,KAAK,MAAM,MAAM,IAAI,GAAG,KAAK,MAAM,MAAM,IAAI,QAAQ,KAAK,QAAQ,CAAC;CAChF,UAAU,GAAG,MAAM,MAAM,IAAI,KAAK,GAAG,MAAM,MAAM,IAAI,KAAK,QAAQ,KAAK,QAAQ,CAAC;AAClF;;AAGA,SAAgB,UACd,GACA,GACA,SAAS,iBAAiB,GAChB;CACV,MAAM,MAAgB,CAAC;CACvB,UAAU,GAAG,GAAG,EAAE,QAAQ,GAAG,GAAG,EAAE,QAAQ,QAAQ,GAAG;CACrD,MAAM,MAAgB,CAAC;CACvB,KAAK,MAAM,UAAU,KAAK;EACxB,MAAM,OAAO,IAAI,IAAI,SAAS;EAC9B,IAAI,QAAQ,KAAK,OAAO,OAAO,MAAM,KAAK,OAAO,OAAO,IAAI;GAC1D,KAAK,KAAK,OAAO;GACjB,KAAK,KAAK,OAAO;EACnB,OAAO,IAAI,KAAK,MAAM;CACxB;CACA,OAAO;AACT"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { changedOrderKeys, keysBetween } from "./order.js";
|
|
2
2
|
import * as Y from "yjs";
|
|
3
3
|
// src/collaboration/merged-props.ts
|
|
4
4
|
/** The field holding an item's place in its list; no item field is named so. */
|
|
@@ -85,15 +85,14 @@ function writeList(map, after, objects, before) {
|
|
|
85
85
|
const gone = before ? Array.from(old.keys()) : Array.from(map.keys());
|
|
86
86
|
for (const id of gone) if (!ids.has(id)) map.delete(id);
|
|
87
87
|
for (const item of items) writeItem(map, item, objects, before ? old.get(item.id) : void 0);
|
|
88
|
-
const
|
|
89
|
-
const
|
|
88
|
+
const current = /* @__PURE__ */ new Map();
|
|
89
|
+
for (const [id, fields] of map) {
|
|
90
|
+
if (!(fields instanceof Y.Map)) continue;
|
|
90
91
|
const order = fields.get(ORDER);
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
if (keys[i] !== next[i]) fields.set(ORDER, next[i]);
|
|
96
|
-
});
|
|
92
|
+
current.set(id, typeof order === "string" ? order : null);
|
|
93
|
+
}
|
|
94
|
+
const keys = changedOrderKeys([...old.keys()], items.map((item) => item.id), current, (a, b) => a.localeCompare(b));
|
|
95
|
+
for (const [id, key] of keys) map.get(id).set(ORDER, key);
|
|
97
96
|
}
|
|
98
97
|
/** A new item, for a map not yet in the document (which can't be read). */
|
|
99
98
|
function newItem(item, objects, order) {
|
|
@@ -124,6 +123,7 @@ function newMerged(value, { lists, objects }) {
|
|
|
124
123
|
* object is stored as it is.
|
|
125
124
|
*/
|
|
126
125
|
function writeMerged(props, name, spec, after, before) {
|
|
126
|
+
if (same(before, after)) return;
|
|
127
127
|
if (!isObject(after)) {
|
|
128
128
|
if (!same(props.get(name), after)) props.set(name, after);
|
|
129
129
|
return;
|
|
@@ -145,7 +145,7 @@ function writeMerged(props, name, spec, after, before) {
|
|
|
145
145
|
else writeList(list, value, objects, Array.isArray(old) ? old : void 0);
|
|
146
146
|
} else if (base ? !same(base[key], value) : !same(map.get(key), value)) map.set(key, value);
|
|
147
147
|
}
|
|
148
|
-
for (const key of Array.from(map.keys())) if (after[key] === void 0) map.delete(key);
|
|
148
|
+
for (const key of Array.from(map.keys())) if (after[key] === void 0 && (!base || base[key] !== void 0)) map.delete(key);
|
|
149
149
|
}
|
|
150
150
|
export { mergedPropsOf, newMerged, readMerged, writeMerged };
|
|
151
151
|
|