@dou-ui/editor 0.1.0-alpha.13 → 0.1.0-alpha.15
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 +32 -10
- package/dist/collaboration/binding.js.map +1 -1
- package/dist/collaboration/index.js +3 -0
- package/dist/collaboration/index.js.map +1 -1
- package/dist/collaboration/merged-props.js +152 -0
- package/dist/collaboration/merged-props.js.map +1 -0
- package/dist/components/block-handle.js.map +1 -1
- package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js +55 -5
- package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/core/extension.js.map +1 -1
- package/dist/core/model.js +2 -1
- package/dist/core/model.js.map +1 -1
- package/dist/core/transaction.js +7 -1
- package/dist/core/transaction.js.map +1 -1
- package/dist/define-block.js +2 -1
- package/dist/define-block.js.map +1 -1
- package/dist/document.js +20 -1
- package/dist/document.js.map +1 -1
- package/dist/dom.js +12 -1
- package/dist/dom.js.map +1 -1
- package/dist/editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/editor2.js.map +1 -1
- package/dist/flow/board-block.js +6 -0
- package/dist/flow/board-block.js.map +1 -0
- package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js +437 -0
- package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/flow/board.js +102 -0
- package/dist/flow/board.js.map +1 -0
- package/dist/flow/index.js +166 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/locale.js +33 -6
- package/dist/locale.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/style.js +1 -0
- package/dist/types/collaboration/binding.d.ts +4 -0
- package/dist/types/collaboration/merged-props.d.ts +20 -0
- package/dist/types/core/extension.d.ts +5 -2
- package/dist/types/core/transaction.d.ts +6 -0
- package/dist/types/document.d.ts +3 -2
- package/dist/types/dom.d.ts +7 -0
- package/dist/types/editor.vue.d.ts +9 -0
- package/dist/types/flow/board-block.vue.d.ts +16 -0
- package/dist/types/flow/board.d.ts +38 -0
- package/dist/types/flow/index.d.ts +59 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/locale.d.ts +10 -0
- package/package.json +14 -5
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { createId } from "../core/model.js";
|
|
2
2
|
import { blockAround, findBlockById } from "../core/blocks.js";
|
|
3
3
|
import { originKey } from "../core/editor.js";
|
|
4
|
-
import { ownStepKey } from "../core/transaction.js";
|
|
4
|
+
import { joinStepKey, ownStepKey } from "../core/transaction.js";
|
|
5
5
|
import { keptIndexes, keysBetween, orderKeys } from "./order.js";
|
|
6
6
|
import { inlineFrom, insertInline, updateText } from "./text.js";
|
|
7
7
|
import { cellAt, cellStart, clearTable, readTable, writeTable } from "./table.js";
|
|
8
8
|
import { placeOf } from "./cursors.js";
|
|
9
|
+
import { mergedPropsOf, newMerged, readMerged, writeMerged } from "./merged-props.js";
|
|
9
10
|
import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
|
|
10
11
|
import { Fragment } from "prosemirror-model";
|
|
11
12
|
import { Transform } from "prosemirror-transform";
|
|
@@ -40,9 +41,15 @@ function contentFrom(yblock, schema) {
|
|
|
40
41
|
const name = yblock.get("type");
|
|
41
42
|
const known = schema.nodes[name];
|
|
42
43
|
const type = known?.isInGroup("blockContent") ? known : schema.nodes.paragraph;
|
|
43
|
-
const
|
|
44
|
+
const stored = yblock.get("props");
|
|
45
|
+
const props = stored?.toJSON() ?? {};
|
|
46
|
+
const merged = mergedPropsOf(type);
|
|
44
47
|
const attrs = {};
|
|
45
|
-
for (const attr of Object.keys(type.spec.attrs ?? {}))
|
|
48
|
+
for (const attr of Object.keys(type.spec.attrs ?? {})) {
|
|
49
|
+
if (!(attr in props)) continue;
|
|
50
|
+
const spec = merged?.[attr];
|
|
51
|
+
attrs[attr] = spec ? readMerged(stored.get(attr), spec) : props[attr];
|
|
52
|
+
}
|
|
46
53
|
if (type.spec.rows) return readTable(yblock, type, attrs);
|
|
47
54
|
if (!type.inlineContent) return type.create(attrs);
|
|
48
55
|
const text = yblock.get("text");
|
|
@@ -58,8 +65,12 @@ function writeContent(yblock, content) {
|
|
|
58
65
|
return;
|
|
59
66
|
}
|
|
60
67
|
const props = new Y.Map();
|
|
61
|
-
|
|
68
|
+
const merged = mergedPropsOf(content.type);
|
|
62
69
|
yblock.set("props", props);
|
|
70
|
+
for (const [name, value] of Object.entries(content.attrs)) {
|
|
71
|
+
const spec = merged?.[name];
|
|
72
|
+
props.set(name, spec ? newMerged(value, spec) : value);
|
|
73
|
+
}
|
|
63
74
|
if (content.inlineContent) insertInline(text, 0, content, 0, content.content.size);
|
|
64
75
|
}
|
|
65
76
|
/**
|
|
@@ -71,7 +82,12 @@ function updateContent(yblock, before, after) {
|
|
|
71
82
|
if (after.type.spec.rows) return writeTable(yblock, before.type === after.type ? before : null, after);
|
|
72
83
|
clearTable(yblock);
|
|
73
84
|
const props = yblock.get("props");
|
|
74
|
-
|
|
85
|
+
const merged = mergedPropsOf(after.type);
|
|
86
|
+
for (const [name, value] of Object.entries(after.attrs)) {
|
|
87
|
+
const spec = merged?.[name];
|
|
88
|
+
if (spec) writeMerged(props, name, spec, value, before.type === after.type ? before.attrs[name] : void 0);
|
|
89
|
+
else if (JSON.stringify(props.get(name)) !== JSON.stringify(value)) props.set(name, value);
|
|
90
|
+
}
|
|
75
91
|
for (const name of Array.from(props.keys())) if (!(name in after.attrs)) props.delete(name);
|
|
76
92
|
const text = yblock.get("text");
|
|
77
93
|
if (!after.inlineContent) {
|
|
@@ -615,7 +631,8 @@ function syncPlugin(binding) {
|
|
|
615
631
|
init: () => ({
|
|
616
632
|
remote: false,
|
|
617
633
|
tracked: false,
|
|
618
|
-
own: false
|
|
634
|
+
own: false,
|
|
635
|
+
join: false
|
|
619
636
|
}),
|
|
620
637
|
apply(tr, value) {
|
|
621
638
|
const meta = tr.getMeta(collaborationKey);
|
|
@@ -623,18 +640,22 @@ function syncPlugin(binding) {
|
|
|
623
640
|
if (meta?.remote) return {
|
|
624
641
|
remote: true,
|
|
625
642
|
tracked: false,
|
|
626
|
-
own: false
|
|
643
|
+
own: false,
|
|
644
|
+
join: false
|
|
627
645
|
};
|
|
628
646
|
const tracked = tr.getMeta("addToHistory") !== false;
|
|
629
647
|
const own = tr.getMeta(ownStepKey) === true;
|
|
630
|
-
|
|
648
|
+
const join = tr.getMeta(joinStepKey) === true;
|
|
649
|
+
return !join && !!tr.getMeta("appendedTransaction") && !value.remote ? {
|
|
631
650
|
remote: false,
|
|
632
651
|
tracked: value.tracked || tracked,
|
|
633
|
-
own: value.own || own
|
|
652
|
+
own: value.own || own,
|
|
653
|
+
join: value.join || join
|
|
634
654
|
} : {
|
|
635
655
|
remote: false,
|
|
636
656
|
tracked,
|
|
637
|
-
own
|
|
657
|
+
own,
|
|
658
|
+
join
|
|
638
659
|
};
|
|
639
660
|
}
|
|
640
661
|
},
|
|
@@ -667,6 +688,7 @@ function syncPlugin(binding) {
|
|
|
667
688
|
}
|
|
668
689
|
binding.selectionBefore = placeOf(binding, previous.doc, previous.selection.head);
|
|
669
690
|
if (state.own) binding.stopCapturing?.();
|
|
691
|
+
else if (state.join) binding.joinCapturing?.();
|
|
670
692
|
writeMap(current, state.tracked);
|
|
671
693
|
if (state.own) binding.stopCapturing?.();
|
|
672
694
|
},
|
|
@@ -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 { ownStepKey } from '../core/transaction'\nimport { placeOf } from './cursors'\nimport { keptIndexes, keysBetween, orderKeys } from './order'\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 */\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 props = (yblock.get('props') as Y.Map<unknown> | undefined)?.toJSON() ?? {}\n const attrs: Attributes = {}\n for (const attr of Object.keys(type.spec.attrs ?? {}))\n if (attr in props) attrs[attr] = props[attr]\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 for (const [name, value] of Object.entries(content.attrs)) props.set(name, value)\n yblock.set('props', props)\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 for (const [name, value] of Object.entries(after.attrs)) {\n 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}>('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 /** 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 }),\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 }\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 appended = !!tr.getMeta('appendedTransaction') && !value.remote\n return appended\n ? { remote: false, tracked: value.tracked || tracked, own: value.own || own }\n : { remote: false, tracked, own }\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 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":";;;;;;;;;;;;;AA6CA,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,MAAM,GAAG,GAAG;CACrC;CACA,MAAM,KAAK,IAAI;CACf,OAAO;EAAE;EAAQ;CAAS;AAC5B;;AAGA,SAAS,YAAY,QAAgB,QAAwB;CAC3D,MAAM,OAAO,OAAO,IAAI,MAAM;CAC9B,MAAM,QAAQ,OAAO,MAAM;CAC3B,MAAM,OAAO,OAAO,UAAU,cAAc,IAAI,QAAQ,OAAO,MAAM;CACrE,MAAM,QAAS,OAAO,IAAI,OAAO,CAAC,EAAiC,OAAO,KAAK,CAAC;CAChF,MAAM,QAAoB,CAAC;CAC3B,KAAK,MAAM,QAAQ,OAAO,KAAK,KAAK,KAAK,SAAS,CAAC,CAAC,GAClD,IAAI,QAAQ,OAAO,MAAM,QAAQ,MAAM;CACzC,IAAI,KAAK,KAAK,MAAM,OAAO,UAAU,QAAQ,MAAM,KAAK;CACxD,IAAI,CAAC,KAAK,eAAe,OAAO,KAAK,OAAO,KAAK;CACjD,MAAM,OAA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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;CAC3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AAS,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"}
|
|
@@ -29,6 +29,9 @@ function collaboration(options) {
|
|
|
29
29
|
captureTimeout: 500
|
|
30
30
|
});
|
|
31
31
|
binding.stopCapturing = () => undoManager.stopCapturing();
|
|
32
|
+
binding.joinCapturing = () => {
|
|
33
|
+
if (undoManager.undoStack.length) undoManager.lastChange = Date.now();
|
|
34
|
+
};
|
|
32
35
|
undoManager.on("stack-item-added", (event) => {
|
|
33
36
|
if (!event.stackItem.meta.has("selection")) event.stackItem.meta.set("selection", binding.selectionBefore ?? null);
|
|
34
37
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/collaboration/index.ts"],"sourcesContent":["import {\n applySharedCommentAction,\n collaborationColors,\n readSharedComments,\n type CommentThread,\n type SharedCommentMap,\n} from 'dou-ui'\nimport type { Schema } from 'prosemirror-model'\nimport { shallowRef } from 'vue'\nimport { Plugin, TextSelection, type Command } from 'prosemirror-state'\nimport * as Y from 'yjs'\nimport {\n defineEditorExtension,\n resolveEditorExtensions,\n type EditorExtension,\n} from '../core/extension'\nimport { blocksToDoc, docToBlocks, type EditorDocument, type PartialBlock } from '../core/model'\nimport { editorKit } from '../extensions/editor-kit'\nimport { createBinding, readDoc, syncPlugin, updateDoc, writeDoc, type YBlock } from './binding'\nimport {\n cursorIn,\n cursorsPlugin,\n positionOf,\n type AwarenessLike,\n type EditorCursorTarget,\n type EditorPresence,\n type EditorPresencePlace,\n} from './cursors'\n\n/** How the people editing together see each other. */\nexport interface CollaborationUser {\n name: string\n /** The cursor color, any CSS color. Picked from the client id when left out. */\n color?: string\n}\n\nexport interface CollaborationOptions {\n /** Where the document lives, e.g. `ydoc.getMap('document')`. */\n map: Y.Map<unknown>\n /** Shares cursors and selections (`y-protocols/awareness`). Without it, others' cursors are not shown. */\n awareness?: AwarenessLike\n /** This user, shown at their cursor to the others. Needs `awareness`. */\n user?: CollaborationUser\n /**\n * Where the comment threads live, e.g. `ydoc.getMap('comments')`, so\n * they reach everyone with the document. `v-model:comments` then only\n * mirrors them. Without it, the text's anchors still travel with the\n * document and the threads are the app's to share.\n */\n comments?: Y.Map<unknown>\n}\n\nexport { collaborationColors }\n\n/**\n * Edit together through a Yjs document. The document lives in `map`: the\n * editor's `v-model` only mirrors it and changes to `v-model` are not\n * written back. Undo only takes back this user's own changes.\n */\nexport function collaboration(options: CollaborationOptions): EditorExtension {\n const map = options.map as Y.Map<YBlock>\n const { awareness, user } = options\n if (awareness && user) {\n const color = user.color ?? collaborationColors[map.doc!.clientID % collaborationColors.length]\n awareness.setLocalStateField('user', { name: user.name, color })\n }\n const binding = createBinding(map)\n const undoManager = new Y.UndoManager(map, {\n trackedOrigins: new Set([binding.origin]),\n captureTimeout: 500,\n })\n binding.stopCapturing = () => undoManager.stopCapturing()\n // put the cursor back where it was when the change was made\n undoManager.on('stack-item-added', (event: { stackItem: { meta: Map<string, unknown> } }) => {\n if (!event.stackItem.meta.has('selection'))\n event.stackItem.meta.set('selection', binding.selectionBefore ?? null)\n })\n undoManager.on('stack-item-popped', (event: { stackItem: { meta: Map<string, unknown> } }) => {\n const view = binding.view\n const place = event.stackItem.meta.get('selection') as Parameters<typeof positionOf>[2] | null\n if (!view || !place) return\n const pos = positionOf(binding, view.state.doc, place)\n if (pos === null) return\n view.dispatch(view.state.tr.setSelection(TextSelection.near(view.state.doc.resolve(pos))))\n })\n const run =\n (action: 'undo' | 'redo'): Command =>\n (_state, dispatch) => {\n const stack = action === 'undo' ? undoManager.undoStack : undoManager.redoStack\n if (!stack.length) return false\n if (dispatch) {\n binding.undoing = action\n try {\n undoManager[action]()\n } finally {\n binding.undoing = undefined\n }\n }\n return true\n }\n\n const shared = options.comments\n const threads = shallowRef<readonly CommentThread[]>([])\n const readThreads = () => (threads.value = readSharedComments(shared as SharedCommentMap))\n if (shared) readThreads()\n // follows the shared threads while the editor is there\n const commentsPlugin = new Plugin({\n view: () => {\n shared!.observe(readThreads)\n readThreads()\n return { destroy: () => shared!.unobserve(readThreads) }\n },\n })\n\n return defineEditorExtension({\n name: 'collaboration',\n externalDocument: true,\n comments: shared\n ? () => ({\n threads,\n apply: (action) =>\n applySharedCommentAction(shared as SharedCommentMap, action, binding.untracked),\n })\n : undefined,\n plugins: () => [\n syncPlugin(binding),\n ...(awareness ? [cursorsPlugin(binding, awareness)] : []),\n ...(shared ? [commentsPlugin] : []),\n ],\n history: () => ({ plugins: [], undo: run('undo'), redo: run('redo') }),\n })\n}\n\ntype ExtensionList = (EditorExtension | EditorExtension[])[]\n\n/** The extensions the editors of the shared document use; `[editorKit()]` when left out. */\nconst schemas = new WeakMap<ExtensionList, Schema>()\nlet defaultSchema: Schema | undefined\n\n/** The schema of a list of extensions, resolved once per list; without one, of `[editorKit()]`. */\nfunction schemaOf(extensions?: ExtensionList): Schema {\n if (!extensions) return (defaultSchema ??= resolveEditorExtensions([editorKit()]).schema)\n let schema = schemas.get(extensions)\n if (!schema) {\n schema = resolveEditorExtensions(extensions).schema\n schemas.set(extensions, schema)\n }\n return schema\n}\n\nexport interface CursorAtOptions {\n /** Where a selection starts; left out, only a cursor at `head`. */\n anchor?: EditorCursorTarget\n /** The extensions the editors use, for custom blocks. Defaults to `[editorKit()]`. */\n extensions?: ExtensionList\n}\n\n/**\n * The value for the awareness field `editor` that puts a cursor at `head`,\n * selecting from `options.anchor` when given, without an editor: e.g. on\n * the server, for a program writing into the document with `updateMap`, so\n * the people in the editor see where it writes. `null` when the block isn't\n * in the map or has no text, e.g. an image, a divider, or a table without\n * `cell`.\n */\nexport function cursorAt(\n map: Y.Map<unknown>,\n head: EditorCursorTarget,\n options: CursorAtOptions = {},\n): EditorPresence | null {\n const schema = schemaOf(options.extensions)\n const holdsText = (name: string) => {\n const known = schema.nodes[name]\n // unknown types are read as paragraphs\n const type = known?.isInGroup('blockContent') ? known : schema.nodes.paragraph!\n return type.inlineContent\n }\n return cursorIn(map, head, options.anchor ?? head, holdsText)\n}\n\n/** Runs `write` as one transaction of the map's document, from `origin`. */\nfunction transact(map: Y.Map<unknown>, write: () => void, origin: unknown) {\n if (map.doc) map.doc.transact(write, origin ?? null)\n else write()\n}\n\nexport interface FillMapOptions {\n /** The extensions the editors use, for custom blocks. Defaults to `[editorKit()]`. */\n extensions?: ExtensionList\n /** The origin of the Yjs transaction, so the others can tell who wrote it. */\n origin?: unknown\n}\n\n/**\n * Write a document into an empty map, e.g. when a document that so far was\n * saved as JSON starts being edited together. Do it once, where the\n * document is created (often on the server): two clients filling the same\n * empty map at the same time would both add their copy.\n */\nexport function fillMap(\n map: Y.Map<unknown>,\n doc: EditorDocument<PartialBlock>,\n options: FillMapOptions = {},\n) {\n const schema = schemaOf(options.extensions)\n transact(\n map,\n () => writeDoc(map as Y.Map<YBlock>, blocksToDoc(schema, doc.blocks)),\n options.origin,\n )\n}\n\nexport interface UpdateMapOptions extends FillMapOptions {\n /**\n * The version `doc` was made from, e.g. the one read with `readMap` and\n * given to a program that took a while to write `doc`. Only what differs\n * between `base` and `doc` is written, so what others changed in the map\n * meanwhile stays. Get it the same way as `doc`: when `doc` is read from\n * Markdown, read `base` from the Markdown the program was given. Left\n * out, the map as it is now.\n */\n base?: EditorDocument<PartialBlock>\n}\n\n/**\n * Change a shared map from `options.base` to `doc`, without an editor, e.g.\n * on the server when a program wrote a new version while people edit the\n * document. Only what differs is written: blocks keep their id and their\n * text is changed character by character, so the cursors, undo and comment\n * threads of the others stay. Blocks with new ids (e.g. read from Markdown)\n * are matched to the stored ones by content and place; one that matches\n * none is written as a new block, and one left over is removed. An empty\n * map is filled.\n */\nexport function updateMap(\n map: Y.Map<unknown>,\n doc: EditorDocument<PartialBlock>,\n options: UpdateMapOptions = {},\n) {\n const schema = schemaOf(options.extensions)\n const { base } = options\n transact(\n map,\n () =>\n updateDoc(\n map as Y.Map<YBlock>,\n blocksToDoc(schema, doc.blocks),\n schema,\n base && blocksToDoc(schema, base.blocks),\n ),\n options.origin,\n )\n}\n\nexport interface ReadMapOptions {\n /** The extensions the editors use, for custom blocks. Defaults to `[editorKit()]`. */\n extensions?: ExtensionList\n}\n\n/**\n * The document a shared map holds, read without an editor, e.g. to save a\n * version on the server. An empty map reads as no blocks.\n */\nexport function readMap(map: Y.Map<unknown>, options: ReadMapOptions = {}): EditorDocument {\n const doc = readDoc(map as Y.Map<YBlock>, schemaOf(options.extensions))\n return { version: 1, blocks: doc ? docToBlocks(doc) : [] }\n}\n\nexport type { AwarenessLike, EditorCursorTarget, EditorPresence, EditorPresencePlace }\n"],"mappings":";;;;;;;;;;;;;;;AA2DA,SAAgB,cAAc,SAAgD;CAC5E,MAAM,MAAM,QAAQ;CACpB,MAAM,EAAE,WAAW,SAAS;CAC5B,IAAI,aAAa,MAAM;EACrB,MAAM,QAAQ,KAAK,SAAS,oBAAoB,IAAI,IAAK,WAAW,oBAAoB;EACxF,UAAU,mBAAmB,QAAQ;GAAE,MAAM,KAAK;GAAM;EAAM,CAAC;CACjE;CACA,MAAM,UAAU,cAAc,GAAG;CACjC,MAAM,cAAc,IAAI,EAAE,YAAY,KAAK;EACzC,gCAAgB,IAAI,IAAI,CAAC,QAAQ,MAAM,CAAC;EACxC,gBAAgB;CAClB,CAAC;CACD,QAAQ,sBAAsB,YAAY,cAAc;CAExD,YAAY,GAAG,qBAAqB,UAAyD;EAC3F,IAAI,CAAC,MAAM,UAAU,KAAK,IAAI,WAAW,GACvC,MAAM,UAAU,KAAK,IAAI,aAAa,QAAQ,mBAAmB,IAAI;CACzE,CAAC;CACD,YAAY,GAAG,sBAAsB,UAAyD;EAC5F,MAAM,OAAO,QAAQ;EACrB,MAAM,QAAQ,MAAM,UAAU,KAAK,IAAI,WAAW;EAClD,IAAI,CAAC,QAAQ,CAAC,OAAO;EACrB,MAAM,MAAM,WAAW,SAAS,KAAK,MAAM,KAAK,K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haredCommentAction,\n collaborationColors,\n readSharedComments,\n type CommentThread,\n type SharedCommentMap,\n} from 'dou-ui'\nimport type { Schema } from 'prosemirror-model'\nimport { shallowRef } from 'vue'\nimport { Plugin, TextSelection, type Command } from 'prosemirror-state'\nimport * as Y from 'yjs'\nimport {\n defineEditorExtension,\n resolveEditorExtensions,\n type EditorExtension,\n} from '../core/extension'\nimport { blocksToDoc, docToBlocks, type EditorDocument, type PartialBlock } from '../core/model'\nimport { editorKit } from '../extensions/editor-kit'\nimport { createBinding, readDoc, syncPlugin, updateDoc, writeDoc, type YBlock } from './binding'\nimport {\n cursorIn,\n cursorsPlugin,\n positionOf,\n type AwarenessLike,\n type EditorCursorTarget,\n type EditorPresence,\n type EditorPresencePlace,\n} from './cursors'\n\n/** How the people editing together see each other. */\nexport interface CollaborationUser {\n name: string\n /** The cursor color, any CSS color. Picked from the client id when left out. */\n color?: string\n}\n\nexport interface CollaborationOptions {\n /** Where the document lives, e.g. `ydoc.getMap('document')`. */\n map: Y.Map<unknown>\n /** Shares cursors and selections (`y-protocols/awareness`). Without it, others' cursors are not shown. */\n awareness?: AwarenessLike\n /** This user, shown at their cursor to the others. Needs `awareness`. */\n user?: CollaborationUser\n /**\n * Where the comment threads live, e.g. `ydoc.getMap('comments')`, so\n * they reach everyone with the document. `v-model:comments` then only\n * mirrors them. Without it, the text's anchors still travel with the\n * document and the threads are the app's to share.\n */\n comments?: Y.Map<unknown>\n}\n\nexport { collaborationColors }\n\n/**\n * Edit together through a Yjs document. The document lives in `map`: the\n * editor's `v-model` only mirrors it and changes to `v-model` are not\n * written back. Undo only takes back this user's own changes.\n */\nexport function collaboration(options: CollaborationOptions): EditorExtension {\n const map = options.map as Y.Map<YBlock>\n const { awareness, user } = options\n if (awareness && user) {\n const color = user.color ?? collaborationColors[map.doc!.clientID % collaborationColors.length]\n awareness.setLocalStateField('user', { name: user.name, color })\n }\n const binding = createBinding(map)\n const undoManager = new Y.UndoManager(map, {\n trackedOrigins: new Set([binding.origin]),\n captureTimeout: 500,\n })\n binding.stopCapturing = () => undoManager.stopCapturing()\n // as if the last change were just now: the next one is captured with it\n binding.joinCapturing = () => {\n if (undoManager.undoStack.length) undoManager.lastChange = Date.now()\n }\n // put the cursor back where it was when the change was made\n undoManager.on('stack-item-added', (event: { stackItem: { meta: Map<string, unknown> } }) => {\n if (!event.stackItem.meta.has('selection'))\n event.stackItem.meta.set('selection', binding.selectionBefore ?? null)\n })\n undoManager.on('stack-item-popped', (event: { stackItem: { meta: Map<string, unknown> } }) => {\n const view = binding.view\n const place = event.stackItem.meta.get('selection') as Parameters<typeof positionOf>[2] | null\n if (!view || !place) return\n const pos = positionOf(binding, view.state.doc, place)\n if (pos === null) return\n view.dispatch(view.state.tr.setSelection(TextSelection.near(view.state.doc.resolve(pos))))\n })\n const run =\n (action: 'undo' | 'redo'): Command =>\n (_state, dispatch) => {\n const stack = action === 'undo' ? undoManager.undoStack : undoManager.redoStack\n if (!stack.length) return false\n if (dispatch) {\n binding.undoing = action\n try {\n undoManager[action]()\n } finally {\n binding.undoing = undefined\n }\n }\n return true\n }\n\n const shared = options.comments\n const threads = shallowRef<readonly CommentThread[]>([])\n const readThreads = () => (threads.value = readSharedComments(shared as SharedCommentMap))\n if (shared) readThreads()\n // follows the shared threads while the editor is there\n const commentsPlugin = new Plugin({\n view: () => {\n shared!.observe(readThreads)\n readThreads()\n return { destroy: () => shared!.unobserve(readThreads) }\n },\n })\n\n return defineEditorExtension({\n name: 'collaboration',\n externalDocument: true,\n comments: shared\n ? () => ({\n threads,\n apply: (action) =>\n applySharedCommentAction(shared as SharedCommentMap, action, binding.untracked),\n })\n : undefined,\n plugins: () => [\n syncPlugin(binding),\n ...(awareness ? [cursorsPlugin(binding, awareness)] : []),\n ...(shared ? [commentsPlugin] : []),\n ],\n history: () => ({ plugins: [], undo: run('undo'), redo: run('redo') }),\n })\n}\n\ntype ExtensionList = (EditorExtension | EditorExtension[])[]\n\n/** The extensions the editors of the shared document use; `[editorKit()]` when left out. */\nconst schemas = new WeakMap<ExtensionList, Schema>()\nlet defaultSchema: Schema | undefined\n\n/** The schema of a list of extensions, resolved once per list; without one, of `[editorKit()]`. */\nfunction schemaOf(extensions?: ExtensionList): Schema {\n if (!extensions) return (defaultSchema ??= resolveEditorExtensions([editorKit()]).schema)\n let schema = schemas.get(extensions)\n if (!schema) {\n schema = resolveEditorExtensions(extensions).schema\n schemas.set(extensions, schema)\n }\n return schema\n}\n\nexport interface CursorAtOptions {\n /** Where a selection starts; left out, only a cursor at `head`. */\n anchor?: EditorCursorTarget\n /** The extensions the editors use, for custom blocks. Defaults to `[editorKit()]`. */\n extensions?: ExtensionList\n}\n\n/**\n * The value for the awareness field `editor` that puts a cursor at `head`,\n * selecting from `options.anchor` when given, without an editor: e.g. on\n * the server, for a program writing into the document with `updateMap`, so\n * the people in the editor see where it writes. `null` when the block isn't\n * in the map or has no text, e.g. an image, a divider, or a table without\n * `cell`.\n */\nexport function cursorAt(\n map: Y.Map<unknown>,\n head: EditorCursorTarget,\n options: CursorAtOptions = {},\n): EditorPresence | null {\n const schema = schemaOf(options.extensions)\n const holdsText = (name: string) => {\n const known = schema.nodes[name]\n // unknown types are read as paragraphs\n const type = known?.isInGroup('blockContent') ? known : schema.nodes.paragraph!\n return type.inlineContent\n }\n return cursorIn(map, head, options.anchor ?? head, holdsText)\n}\n\n/** Runs `write` as one transaction of the map's document, from `origin`. */\nfunction transact(map: Y.Map<unknown>, write: () => void, origin: unknown) {\n if (map.doc) map.doc.transact(write, origin ?? null)\n else write()\n}\n\nexport interface FillMapOptions {\n /** The extensions the editors use, for custom blocks. Defaults to `[editorKit()]`. */\n extensions?: ExtensionList\n /** The origin of the Yjs transaction, so the others can tell who wrote it. */\n origin?: unknown\n}\n\n/**\n * Write a document into an empty map, e.g. when a document that so far was\n * saved as JSON starts being edited together. Do it once, where the\n * document is created (often on the server): two clients filling the same\n * empty map at the same time would both add their copy.\n */\nexport function fillMap(\n map: Y.Map<unknown>,\n doc: EditorDocument<PartialBlock>,\n options: FillMapOptions = {},\n) {\n const schema = schemaOf(options.extensions)\n transact(\n map,\n () => writeDoc(map as Y.Map<YBlock>, blocksToDoc(schema, doc.blocks)),\n options.origin,\n )\n}\n\nexport interface UpdateMapOptions extends FillMapOptions {\n /**\n * The version `doc` was made from, e.g. the one read with `readMap` and\n * given to a program that took a while to write `doc`. Only what differs\n * between `base` and `doc` is written, so what others changed in the map\n * meanwhile stays. Get it the same way as `doc`: when `doc` is read from\n * Markdown, read `base` from the Markdown the program was given. Left\n * out, the map as it is now.\n */\n base?: EditorDocument<PartialBlock>\n}\n\n/**\n * Change a shared map from `options.base` to `doc`, without an editor, e.g.\n * on the server when a program wrote a new version while people edit the\n * document. Only what differs is written: blocks keep their id and their\n * text is changed character by character, so the cursors, undo and comment\n * threads of the others stay. Blocks with new ids (e.g. read from Markdown)\n * are matched to the stored ones by content and place; one that matches\n * none is written as a new block, and one left over is removed. An empty\n * map is filled.\n */\nexport function updateMap(\n map: Y.Map<unknown>,\n doc: EditorDocument<PartialBlock>,\n options: UpdateMapOptions = {},\n) {\n const schema = schemaOf(options.extensions)\n const { base } = options\n transact(\n map,\n () =>\n updateDoc(\n map as Y.Map<YBlock>,\n blocksToDoc(schema, doc.blocks),\n schema,\n base && blocksToDoc(schema, base.blocks),\n ),\n options.origin,\n )\n}\n\nexport interface ReadMapOptions {\n /** The extensions the editors use, for custom blocks. Defaults to `[editorKit()]`. */\n extensions?: ExtensionList\n}\n\n/**\n * The document a shared map holds, read without an editor, e.g. to save a\n * version on the server. An empty map reads as no blocks.\n */\nexport function readMap(map: Y.Map<unknown>, options: ReadMapOptions = {}): EditorDocument {\n const doc = readDoc(map as Y.Map<YBlock>, schemaOf(options.extensions))\n return { version: 1, blocks: doc ? docToBlocks(doc) : [] }\n}\n\nexport type { AwarenessLike, EditorCursorTarget, EditorPresence, EditorPresencePlace }\n"],"mappings":";;;;;;;;;;;;;;;AA2DA,SAAgB,cAAc,SAAgD;CAC5E,MAAM,M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AC9D;;AAGA,SAAS,SAAS,KAAqB,OAAmB,QAAiB;CACzE,IAAI,IAAI,KAAK,IAAI,IAAI,SAAS,OAAO,UAAU,IAAI;MAC9C,MAAM;AACb;;;;;;;AAeA,SAAgB,QACd,KACA,KACA,UAA0B,CAAC,GAC3B;CACA,MAAM,SAAS,SAAS,QAAQ,UAAU;CAC1C,SACE,WACM,SAAS,KAAsB,YAAY,QAAQ,IAAI,MAAM,CAAC,GACpE,QAAQ,MACV;AACF;;;;;;;;;;;AAwBA,SAAgB,UACd,KACA,KACA,UAA4B,CAAC,GAC7B;CACA,MAAM,SAAS,SAAS,QAAQ,UAAU;CAC1C,MAAM,EAAE,SAAS;CACjB,SACE,WAEE,UACE,KACA,YAAY,QAAQ,IAAI,MAAM,GAC9B,QACA,QAAQ,YAAY,QAAQ,KAAK,MAAM,CACzC,GACF,QAAQ,MACV;AACF;;;;;AAWA,SAAgB,QAAQ,KAAqB,UAA0B,CAAC,GAAmB;CACzF,MAAM,MAAM,QAAQ,KAAsB,SAAS,QAAQ,UAAU,CAAC;CACtE,OAAO;EAAE,SAAS;EAAG,QAAQ,MAAM,YAAY,GAAG,IAAI,CAAC;CAAE;AAC3D"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { keysBetween, orderKeys } from "./order.js";
|
|
2
|
+
import * as Y from "yjs";
|
|
3
|
+
// src/collaboration/merged-props.ts
|
|
4
|
+
/** The field holding an item's place in its list; no item field is named so. */
|
|
5
|
+
var ORDER = "\0order";
|
|
6
|
+
/** The props a block type merges, by name. */
|
|
7
|
+
function mergedPropsOf(type) {
|
|
8
|
+
const spec = type.spec.mergeProps;
|
|
9
|
+
if (!spec) return void 0;
|
|
10
|
+
return Object.fromEntries(Object.entries(spec).map(([name, value]) => [name, Array.isArray(value) ? {
|
|
11
|
+
lists: value,
|
|
12
|
+
objects: []
|
|
13
|
+
} : {
|
|
14
|
+
lists: value.lists,
|
|
15
|
+
objects: value.objects ?? []
|
|
16
|
+
}]));
|
|
17
|
+
}
|
|
18
|
+
var isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
19
|
+
var isItem = (value) => isObject(value) && typeof value.id === "string" && !!value.id;
|
|
20
|
+
var same = (a, b) => a === b || JSON.stringify(a) === JSON.stringify(b);
|
|
21
|
+
/** Reads a merged prop back: lists in a stable order (by id), the same on every client. */
|
|
22
|
+
function readMerged(value, { lists }) {
|
|
23
|
+
if (!(value instanceof Y.Map)) return value instanceof Y.AbstractType ? value.toJSON() : value;
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const [key, field] of value.entries()) if (lists.includes(key) && field instanceof Y.Map) {
|
|
26
|
+
const items = [];
|
|
27
|
+
for (const [id, fields] of field.entries()) {
|
|
28
|
+
if (!(fields instanceof Y.Map)) continue;
|
|
29
|
+
const { [ORDER]: order, ...rest } = fields.toJSON();
|
|
30
|
+
items.push([typeof order === "string" ? order : "", {
|
|
31
|
+
...rest,
|
|
32
|
+
id
|
|
33
|
+
}]);
|
|
34
|
+
}
|
|
35
|
+
items.sort(([a, x], [b, y]) => a === b ? x.id.localeCompare(y.id) : a < b ? -1 : 1);
|
|
36
|
+
result[key] = items.map(([, item]) => item);
|
|
37
|
+
} else result[key] = field instanceof Y.AbstractType ? field.toJSON() : field;
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
/** Writes an object's keys into a map, only those that changed since `before`. */
|
|
41
|
+
function writeKeys(map, after, before) {
|
|
42
|
+
for (const [key, value] of Object.entries(after)) {
|
|
43
|
+
if (value === void 0) continue;
|
|
44
|
+
if (before ? !same(before[key], value) : !same(map.get(key), value)) map.set(key, value);
|
|
45
|
+
}
|
|
46
|
+
for (const key of Array.from(map.keys())) {
|
|
47
|
+
if (after[key] !== void 0) continue;
|
|
48
|
+
if (!before || before[key] !== void 0) map.delete(key);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** A field as it is first stored: an object one of `objects` as a map of its keys. */
|
|
52
|
+
function newField(key, value, objects) {
|
|
53
|
+
if (!objects.includes(key) || !isObject(value)) return value;
|
|
54
|
+
const map = new Y.Map();
|
|
55
|
+
for (const [name, field] of Object.entries(value)) if (field !== void 0) map.set(name, field);
|
|
56
|
+
return map;
|
|
57
|
+
}
|
|
58
|
+
/** Writes one item's fields, only those that changed. */
|
|
59
|
+
function writeItem(map, item, objects, before) {
|
|
60
|
+
const existing = map.get(item.id);
|
|
61
|
+
if (!(existing instanceof Y.Map)) {
|
|
62
|
+
if (!before) map.set(item.id, newItem(item, objects));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
for (const [key, value] of Object.entries(item)) {
|
|
66
|
+
if (key === "id" || value === void 0) continue;
|
|
67
|
+
const stored = existing.get(key);
|
|
68
|
+
const old = before?.[key];
|
|
69
|
+
if (objects.includes(key) && isObject(value) && stored instanceof Y.Map) {
|
|
70
|
+
writeKeys(stored, value, before ? isObject(old) ? old : {} : void 0);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (before ? !same(old, value) : !same(stored instanceof Y.AbstractType ? stored.toJSON() : stored, value)) existing.set(key, newField(key, value, objects));
|
|
74
|
+
}
|
|
75
|
+
for (const key of Array.from(existing.keys())) {
|
|
76
|
+
if (key === ORDER || item[key] !== void 0) continue;
|
|
77
|
+
if (!before || before[key] !== void 0) existing.delete(key);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Brings a list from `before` to `after`; without `before`, from what the map holds. */
|
|
81
|
+
function writeList(map, after, objects, before) {
|
|
82
|
+
const items = after.filter(isItem);
|
|
83
|
+
const ids = new Set(items.map((item) => item.id));
|
|
84
|
+
const old = new Map((before ?? []).filter(isItem).map((item) => [item.id, item]));
|
|
85
|
+
const gone = before ? Array.from(old.keys()) : Array.from(map.keys());
|
|
86
|
+
for (const id of gone) if (!ids.has(id)) map.delete(id);
|
|
87
|
+
for (const item of items) writeItem(map, item, objects, before ? old.get(item.id) : void 0);
|
|
88
|
+
const stored = items.map((item) => map.get(item.id)).filter((fields) => fields instanceof Y.Map);
|
|
89
|
+
const keys = stored.map((fields) => {
|
|
90
|
+
const order = fields.get(ORDER);
|
|
91
|
+
return typeof order === "string" ? order : null;
|
|
92
|
+
});
|
|
93
|
+
const next = orderKeys(keys);
|
|
94
|
+
stored.forEach((fields, i) => {
|
|
95
|
+
if (keys[i] !== next[i]) fields.set(ORDER, next[i]);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/** A new item, for a map not yet in the document (which can't be read). */
|
|
99
|
+
function newItem(item, objects, order) {
|
|
100
|
+
const fields = new Y.Map();
|
|
101
|
+
if (order !== void 0) fields.set(ORDER, order);
|
|
102
|
+
for (const [key, value] of Object.entries(item)) if (key !== "id" && value !== void 0) fields.set(key, newField(key, value, objects));
|
|
103
|
+
return fields;
|
|
104
|
+
}
|
|
105
|
+
function newList(items, objects) {
|
|
106
|
+
const list = new Y.Map();
|
|
107
|
+
const valid = items.filter(isItem);
|
|
108
|
+
const keys = keysBetween(null, null, valid.length);
|
|
109
|
+
valid.forEach((item, i) => list.set(item.id, newItem(item, objects, keys[i])));
|
|
110
|
+
return list;
|
|
111
|
+
}
|
|
112
|
+
/** A merged prop as it is first stored: built without reading, as nothing is in the document yet. */
|
|
113
|
+
function newMerged(value, { lists, objects }) {
|
|
114
|
+
if (!isObject(value)) return value;
|
|
115
|
+
const map = new Y.Map();
|
|
116
|
+
for (const [key, field] of Object.entries(value)) {
|
|
117
|
+
if (field === void 0) continue;
|
|
118
|
+
map.set(key, lists.includes(key) && Array.isArray(field) ? newList(field, objects) : field);
|
|
119
|
+
}
|
|
120
|
+
return map;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Brings the stored prop from `before` to `after`. A value that isn't an
|
|
124
|
+
* object is stored as it is.
|
|
125
|
+
*/
|
|
126
|
+
function writeMerged(props, name, spec, after, before) {
|
|
127
|
+
if (!isObject(after)) {
|
|
128
|
+
if (!same(props.get(name), after)) props.set(name, after);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const stored = props.get(name);
|
|
132
|
+
if (!(stored instanceof Y.Map)) {
|
|
133
|
+
props.set(name, newMerged(after, spec));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const { lists, objects } = spec;
|
|
137
|
+
const map = stored;
|
|
138
|
+
const base = isObject(before) ? before : void 0;
|
|
139
|
+
for (const [key, value] of Object.entries(after)) {
|
|
140
|
+
if (value === void 0) continue;
|
|
141
|
+
if (lists.includes(key) && Array.isArray(value)) {
|
|
142
|
+
const list = map.get(key);
|
|
143
|
+
const old = base?.[key];
|
|
144
|
+
if (!(list instanceof Y.Map)) map.set(key, newList(value, objects));
|
|
145
|
+
else writeList(list, value, objects, Array.isArray(old) ? old : void 0);
|
|
146
|
+
} else if (base ? !same(base[key], value) : !same(map.get(key), value)) map.set(key, value);
|
|
147
|
+
}
|
|
148
|
+
for (const key of Array.from(map.keys())) if (after[key] === void 0) map.delete(key);
|
|
149
|
+
}
|
|
150
|
+
export { mergedPropsOf, newMerged, readMerged, writeMerged };
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=merged-props.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merged-props.js","names":[],"sources":["../../src/collaboration/merged-props.ts"],"sourcesContent":["import type { NodeType } from 'prosemirror-model'\nimport * as Y from 'yjs'\nimport { keysBetween, orderKeys } from './order'\n\n/*\n * Props whose lists of `{ id }` items merge item by item when edited\n * together, e.g. the nodes and edges of a whiteboard in a block. A block\n * type names them in its spec:\n *\n * mergeProps: { board: { lists: ['nodes', 'edges'], objects: ['data'] } }\n *\n * (`{ board: ['nodes', 'edges'] }` for lists alone) and such a prop is\n * stored as\n *\n * props.board: Y.Map {\n * nodes: Y.Map<item id, Y.Map<field, value>>\n * edges: Y.Map<item id, Y.Map<field, value>>\n * (other fields, e.g. version, as plain values)\n * }\n *\n * with an item's place in its list as an order key among its fields, and\n * its `objects` fields (e.g. a node's `data`) as a Y.Map of\n * their keys. Two people moving two different nodes, changing two fields\n * of the same node, or two keys of its data (its text and its color) both\n * keep their change. An item someone removed stays removed when another\n * person changes it meanwhile.\n */\n\ntype Item = Record<string, unknown>\n\n/** The field holding an item's place in its list; no item field is named so. */\nconst ORDER = '\\u0000order'\ntype Shared = Y.Map<unknown>\n\n/** How a prop merges: its fields that are lists of items, and the item fields merged key by key. */\nexport interface MergeSpec {\n lists: readonly string[]\n objects: readonly string[]\n}\n\ntype MergePropsSpec = Record<\n string,\n readonly string[] | { lists: readonly string[]; objects?: readonly string[] }\n>\n\n/** The props a block type merges, by name. */\nexport function mergedPropsOf(type: NodeType): Record<string, MergeSpec> | undefined {\n const spec = type.spec.mergeProps as MergePropsSpec | undefined\n if (!spec) return undefined\n return Object.fromEntries(\n Object.entries(spec).map(([name, value]) => [\n name,\n Array.isArray(value)\n ? { lists: value, objects: [] }\n : { lists: (value as MergeSpec).lists, objects: (value as MergeSpec).objects ?? [] },\n ]),\n )\n}\n\nconst isObject = (value: unknown): value is Item =>\n !!value && typeof value === 'object' && !Array.isArray(value)\n\nconst isItem = (value: unknown): value is Item & { id: string } =>\n isObject(value) && typeof value.id === 'string' && !!value.id\n\nconst same = (a: unknown, b: unknown) => a === b || JSON.stringify(a) === JSON.stringify(b)\n\n/** Reads a merged prop back: lists in a stable order (by id), the same on every client. */\nexport function readMerged(value: unknown, { lists }: MergeSpec): unknown {\n if (!(value instanceof Y.Map)) return value instanceof Y.AbstractType ? value.toJSON() : value\n const result: Item = {}\n for (const [key, field] of value.entries()) {\n if (lists.includes(key) && field instanceof Y.Map) {\n const items: [string, Item][] = []\n for (const [id, fields] of field.entries()) {\n if (!(fields instanceof Y.Map)) continue\n const { [ORDER]: order, ...rest } = fields.toJSON() as Item\n items.push([typeof order === 'string' ? order : '', { ...rest, id }])\n }\n // by place, then by id: the same order on every client\n items.sort(([a, x], [b, y]) =>\n a === b ? (x.id as string).localeCompare(y.id as string) : a < b ? -1 : 1,\n )\n result[key] = items.map(([, item]) => item)\n } else result[key] = field instanceof Y.AbstractType ? field.toJSON() : field\n }\n return result\n}\n\n/** Writes an object's keys into a map, only those that changed since `before`. */\nfunction writeKeys(map: Shared, after: Item, before?: Item) {\n for (const [key, value] of Object.entries(after)) {\n if (value === undefined) continue\n if (before ? !same(before[key], value) : !same(map.get(key), value)) map.set(key, value)\n }\n for (const key of Array.from(map.keys())) {\n if (after[key] !== undefined) continue\n if (!before || before[key] !== undefined) map.delete(key)\n }\n}\n\n/** A field as it is first stored: an object one of `objects` as a map of its keys. */\nfunction newField(key: string, value: unknown, objects: readonly string[]) {\n if (!objects.includes(key) || !isObject(value)) return value\n const map = new Y.Map<unknown>()\n for (const [name, field] of Object.entries(value)) if (field !== undefined) map.set(name, field)\n return map\n}\n\n/** Writes one item's fields, only those that changed. */\nfunction writeItem(\n map: Shared,\n item: Item & { id: string },\n objects: readonly string[],\n before?: Item,\n) {\n const existing = map.get(item.id)\n if (!(existing instanceof Y.Map)) {\n // removed by someone else meanwhile: it stays removed\n if (!before) map.set(item.id, newItem(item, objects))\n return\n }\n for (const [key, value] of Object.entries(item)) {\n if (key === 'id' || value === undefined) continue\n const stored = existing.get(key)\n const old = before?.[key]\n if (objects.includes(key) && isObject(value) && stored instanceof Y.Map) {\n writeKeys(stored as Shared, value, before ? (isObject(old) ? old : {}) : undefined)\n continue\n }\n const changed = before\n ? !same(old, value)\n : !same(stored instanceof Y.AbstractType ? stored.toJSON() : stored, value)\n if (changed) existing.set(key, newField(key, value, objects))\n }\n for (const key of Array.from(existing.keys())) {\n if (key === ORDER || item[key] !== undefined) continue\n if (!before || before[key] !== undefined) existing.delete(key)\n }\n}\n\n/** Brings a list from `before` to `after`; without `before`, from what the map holds. */\nfunction writeList(map: Shared, after: unknown[], objects: readonly string[], before?: unknown[]) {\n const items = after.filter(isItem)\n const ids = new Set(items.map((item) => item.id))\n const old = new Map((before ?? []).filter(isItem).map((item) => [item.id, item]))\n const gone = before ? Array.from(old.keys()) : Array.from(map.keys())\n for (const id of gone) if (!ids.has(id)) map.delete(id)\n for (const item of items) writeItem(map, item, objects, before ? old.get(item.id) : undefined)\n // places: only those of the items that moved, or have none yet\n const stored = items\n .map((item) => map.get(item.id))\n .filter((fields): fields is Shared => fields instanceof Y.Map)\n const keys = stored.map((fields) => {\n const order = fields.get(ORDER)\n return typeof order === 'string' ? order : null\n })\n const next = orderKeys(keys)\n stored.forEach((fields, i) => {\n if (keys[i] !== next[i]) fields.set(ORDER, next[i])\n })\n}\n\n/** A new item, for a map not yet in the document (which can't be read). */\nfunction newItem(item: Item, objects: readonly string[], order?: string): Shared {\n const fields = new Y.Map<unknown>()\n if (order !== undefined) fields.set(ORDER, order)\n for (const [key, value] of Object.entries(item))\n if (key !== 'id' && value !== undefined) fields.set(key, newField(key, value, objects))\n return fields\n}\n\nfunction newList(items: unknown[], objects: readonly string[]): Shared {\n const list = new Y.Map<unknown>()\n const valid = items.filter(isItem)\n const keys = keysBetween(null, null, valid.length)\n valid.forEach((item, i) => list.set(item.id, newItem(item, objects, keys[i])))\n return list\n}\n\n/** A merged prop as it is first stored: built without reading, as nothing is in the document yet. */\nexport function newMerged(value: unknown, { lists, objects }: MergeSpec): unknown {\n if (!isObject(value)) return value\n const map = new Y.Map<unknown>()\n for (const [key, field] of Object.entries(value)) {\n if (field === undefined) continue\n map.set(key, lists.includes(key) && Array.isArray(field) ? newList(field, objects) : field)\n }\n return map\n}\n\n/**\n * Brings the stored prop from `before` to `after`. A value that isn't an\n * object is stored as it is.\n */\nexport function writeMerged(\n props: Shared,\n name: string,\n spec: MergeSpec,\n after: unknown,\n before?: unknown,\n) {\n if (!isObject(after)) {\n if (!same(props.get(name), after)) props.set(name, after)\n return\n }\n const stored = props.get(name)\n if (!(stored instanceof Y.Map)) {\n props.set(name, newMerged(after, spec))\n return\n }\n const { lists, objects } = spec\n const map = stored as Shared\n const base = isObject(before) ? before : undefined\n for (const [key, value] of Object.entries(after)) {\n if (value === undefined) continue\n if (lists.includes(key) && Array.isArray(value)) {\n const list = map.get(key)\n const old = base?.[key]\n if (!(list instanceof Y.Map)) map.set(key, newList(value, objects))\n else writeList(list as Shared, value, objects, Array.isArray(old) ? old : undefined)\n } else if (base ? !same(base[key], value) : !same(map.get(key), value)) map.set(key, value)\n }\n for (const key of Array.from(map.keys())) if (after[key] === undefined) map.delete(key)\n}\n"],"mappings":";;;;AA+BA,IAAM,QAAQ;;AAed,SAAgB,cAAc,MAAuD;CACnF,MAAM,OAAO,KAAK,KAAK;CACvB,IAAI,CAAC,MAAM,OAAO,KAAA;CAClB,OAAO,OAAO,YACZ,OAAO,QAAQ,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM,WAAW,CAC1C,MACA,MAAM,QAAQ,KAAK,IACf;EAAE,OAAO;EAAO,SAAS,CAAC;CAAE,IAC5B;EAAE,OAAQ,MAAoB;EAAO,SAAU,MAAoB,WAAW,CAAC;CAAE,CACvF,CAAC,CACH;AACF;AAEA,IAAM,YAAY,UAChB,CAAC,CAAC,SAAS,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK;AAE9D,IAAM,UAAU,UACd,SAAS,KAAK,KAAK,OAAO,MAAM,OAAO,YAAY,CAAC,CAAC,MAAM;AAE7D,IAAM,QAAQ,GAAY,MAAe,MAAM,KAAK,KAAK,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC;;AAG1F,SAAgB,WAAW,OAAgB,EAAE,SAA6B;CACxE,IAAI,EAAE,iBAAiB,EAAE,MAAM,OAAO,iBAAiB,EAAE,eAAe,MAAM,OAAO,IAAI;CACzF,MAAM,SAAe,CAAC;CACtB,KAAK,MAAM,CAAC,KAAK,UAAU,MAAM,QAAQ,GACvC,IAAI,MAAM,SAAS,GAAG,KAAK,iBAAiB,EAAE,KAAK;EACjD,MAAM,QAA0B,CAAC;EACjC,KAAK,MAAM,CAAC,IAAI,WAAW,MAAM,QAAQ,GAAG;GAC1C,IAAI,EAAE,kBAAkB,EAAE,MAAM;GAChC,MAAM,GAAG,QAAQ,OA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