@dou-ui/editor 0.1.0-alpha.3 → 0.1.0-alpha.30
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 +371 -57
- package/dist/collaboration/binding.js.map +1 -1
- package/dist/collaboration/cursors.js +42 -10
- package/dist/collaboration/cursors.js.map +1 -1
- package/dist/collaboration/index.js +61 -11
- package/dist/collaboration/index.js.map +1 -1
- package/dist/collaboration/inline-diff.js +138 -0
- package/dist/collaboration/inline-diff.js.map +1 -0
- package/dist/collaboration/merged-props.js +152 -0
- package/dist/collaboration/merged-props.js.map +1 -0
- package/dist/collaboration/order.js +41 -1
- package/dist/collaboration/order.js.map +1 -1
- package/dist/collaboration/table.js +64 -25
- package/dist/collaboration/table.js.map +1 -1
- package/dist/collaboration/text.js +213 -1
- package/dist/collaboration/text.js.map +1 -1
- package/dist/components/block-handle.js.map +1 -1
- package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js +127 -17
- package/dist/components/block-handle.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/slash-choices.js +6 -0
- package/dist/components/slash-choices.js.map +1 -0
- package/dist/components/slash-choices.vue_vue_type_script_setup_true_lang.js +56 -0
- package/dist/components/slash-choices.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/slash-menu.js.map +1 -1
- package/dist/components/slash-menu.vue_vue_type_script_setup_true_lang.js +185 -20
- package/dist/components/slash-menu.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/core/block-selection.js +32 -4
- package/dist/core/block-selection.js.map +1 -1
- package/dist/core/blocks.js +21 -3
- package/dist/core/blocks.js.map +1 -1
- package/dist/core/cjk.js +54 -0
- package/dist/core/cjk.js.map +1 -0
- package/dist/core/commands.js +125 -13
- package/dist/core/commands.js.map +1 -1
- package/dist/core/comments.js +85 -3
- package/dist/core/comments.js.map +1 -1
- package/dist/core/convert.js +3 -3
- package/dist/core/convert.js.map +1 -1
- package/dist/core/extension.js +6 -2
- package/dist/core/extension.js.map +1 -1
- package/dist/core/model.js +10 -3
- package/dist/core/model.js.map +1 -1
- package/dist/core/output.js +26 -10
- package/dist/core/output.js.map +1 -1
- package/dist/core/prepare.js +12 -5
- package/dist/core/prepare.js.map +1 -1
- package/dist/core/schema.js.map +1 -1
- package/dist/core/transaction.js +19 -4
- package/dist/core/transaction.js.map +1 -1
- package/dist/define-block.js +8 -4
- package/dist/define-block.js.map +1 -1
- package/dist/document.js +59 -4
- package/dist/document.js.map +1 -1
- package/dist/dom.js +109 -13
- package/dist/dom.js.map +1 -1
- package/dist/editor.js.map +1 -1
- package/dist/editor.vue_vue_type_script_setup_true_lang.js +6 -5
- package/dist/editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/editor2.js.map +1 -1
- package/dist/extensions/callout/html.js +5 -4
- package/dist/extensions/callout/html.js.map +1 -1
- package/dist/extensions/columns/column-picker.js +6 -0
- package/dist/extensions/columns/column-picker.js.map +1 -0
- package/dist/extensions/columns/column-picker.vue_vue_type_script_setup_true_lang.js +57 -0
- package/dist/extensions/columns/column-picker.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/extensions/columns/columns-controls.js +6 -0
- package/dist/extensions/columns/columns-controls.js.map +1 -0
- package/dist/extensions/columns/columns-controls.vue_vue_type_script_setup_true_lang.js +341 -0
- package/dist/extensions/columns/columns-controls.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/extensions/columns/html.js +34 -0
- package/dist/extensions/columns/html.js.map +1 -0
- package/dist/extensions/columns/index.js +57 -0
- package/dist/extensions/columns/index.js.map +1 -0
- package/dist/extensions/columns/keys.js +53 -0
- package/dist/extensions/columns/keys.js.map +1 -0
- package/dist/extensions/columns/plugin.js +213 -0
- package/dist/extensions/columns/plugin.js.map +1 -0
- package/dist/extensions/columns/schema.js +58 -0
- package/dist/extensions/columns/schema.js.map +1 -0
- package/dist/extensions/columns/select-columns.js +6 -0
- package/dist/extensions/columns/select-columns.js.map +1 -0
- package/dist/extensions/columns/select-columns.vue_vue_type_script_setup_true_lang.js +71 -0
- package/dist/extensions/columns/select-columns.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/extensions/editor-kit.js +3 -1
- package/dist/extensions/editor-kit.js.map +1 -1
- package/dist/extensions/list-html.js +1 -1
- package/dist/extensions/list-html.js.map +1 -1
- package/dist/extensions/table/plugin.js +33 -1
- package/dist/extensions/table/plugin.js.map +1 -1
- package/dist/extensions/table/schema.js +5 -1
- package/dist/extensions/table/schema.js.map +1 -1
- package/dist/extensions/table/table-controls.js.map +1 -1
- package/dist/extensions/table/table-controls.vue_vue_type_script_setup_true_lang.js +80 -9
- package/dist/extensions/table/table-controls.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/extensions/table/view.js +181 -3
- package/dist/extensions/table/view.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 +659 -0
- package/dist/flow/board-block.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/flow/board.js +221 -0
- package/dist/flow/board.js.map +1 -0
- package/dist/flow/index.js +173 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/locale.js +78 -6
- package/dist/locale.js.map +1 -1
- package/dist/markdown/cjk-friendly.js +62 -0
- package/dist/markdown/cjk-friendly.js.map +1 -0
- package/dist/markdown/index.js +4 -3
- package/dist/markdown/index.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/style.js +2 -0
- package/dist/types/collaboration/binding.d.ts +12 -0
- package/dist/types/collaboration/cursors.d.ts +29 -1
- package/dist/types/collaboration/index.d.ts +53 -6
- package/dist/types/collaboration/inline-diff.d.ts +9 -0
- package/dist/types/collaboration/merged-props.d.ts +20 -0
- package/dist/types/collaboration/order.d.ts +5 -0
- package/dist/types/collaboration/table.d.ts +5 -1
- package/dist/types/collaboration/text.d.ts +20 -0
- package/dist/types/components/slash-choices.vue.d.ts +18 -0
- package/dist/types/core/blocks.d.ts +10 -0
- package/dist/types/core/cjk.d.ts +23 -0
- package/dist/types/core/commands.d.ts +17 -2
- package/dist/types/core/comments.d.ts +39 -3
- package/dist/types/core/convert.d.ts +10 -2
- package/dist/types/core/extension.d.ts +58 -4
- package/dist/types/core/model.d.ts +6 -1
- package/dist/types/core/prepare.d.ts +16 -2
- package/dist/types/core/schema.d.ts +1 -1
- package/dist/types/core/transaction.d.ts +6 -0
- package/dist/types/define-block.d.ts +16 -4
- package/dist/types/document.d.ts +5 -2
- package/dist/types/dom.d.ts +41 -4
- package/dist/types/editor.d.ts +15 -1
- package/dist/types/editor.vue.d.ts +24 -0
- package/dist/types/extensions/columns/column-picker.vue.d.ts +10 -0
- package/dist/types/extensions/columns/columns-controls.vue.d.ts +3 -0
- package/dist/types/extensions/columns/html.d.ts +10 -0
- package/dist/types/extensions/columns/index.d.ts +9 -0
- package/dist/types/extensions/columns/keys.d.ts +6 -0
- package/dist/types/extensions/columns/plugin.d.ts +28 -0
- package/dist/types/extensions/columns/schema.d.ts +17 -0
- package/dist/types/extensions/columns/select-columns.vue.d.ts +13 -0
- package/dist/types/extensions/editor-kit.d.ts +4 -1
- package/dist/types/extensions/table/view.d.ts +36 -2
- package/dist/types/flow/board-block.vue.d.ts +16 -0
- package/dist/types/flow/board.d.ts +53 -0
- package/dist/types/flow/index.d.ts +65 -0
- package/dist/types/index.d.ts +29 -4
- package/dist/types/locale.d.ts +34 -0
- package/dist/types/markdown/cjk-friendly.d.ts +6 -0
- package/dist/types/markdown/index.d.ts +11 -4
- package/dist/types/use-comments.d.ts +1 -0
- package/dist/use-comments.js +63 -9
- package/dist/use-comments.js.map +1 -1
- package/package.json +14 -5
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/collaboration/inline-diff.ts
|
|
2
|
+
var createDiffBudget = () => ({ left: 8e6 });
|
|
3
|
+
var failed = () => /* @__PURE__ */ new Error("[@dou-ui/editor] updateMap cannot safely merge this text within its comparison limit. Write a smaller change, or read the current document and reapply the change before retrying; the shared document has not been changed.");
|
|
4
|
+
function spend(budget) {
|
|
5
|
+
if (--budget.left < 0) throw failed();
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The middle of the shortest edit path from `a[aLo..aHi)` to `b[bLo..bHi)`,
|
|
9
|
+
* found from both ends at once (Myers, linear space), or `null` when the
|
|
10
|
+
* two share nothing. All regions use the same operation budget.
|
|
11
|
+
*/
|
|
12
|
+
function bisect(a, aLo, aHi, b, bLo, bHi, budget) {
|
|
13
|
+
const n = aHi - aLo;
|
|
14
|
+
const m = bHi - bLo;
|
|
15
|
+
const maxD = Math.ceil((n + m) / 2);
|
|
16
|
+
const offset = maxD + 1;
|
|
17
|
+
const size = 2 * offset + 1;
|
|
18
|
+
const v1 = new Int32Array(size).fill(-1);
|
|
19
|
+
const v2 = new Int32Array(size).fill(-1);
|
|
20
|
+
v1[offset + 1] = 0;
|
|
21
|
+
v2[offset + 1] = 0;
|
|
22
|
+
const delta = n - m;
|
|
23
|
+
const front = (delta & 1) !== 0;
|
|
24
|
+
let k1start = 0;
|
|
25
|
+
let k1end = 0;
|
|
26
|
+
let k2start = 0;
|
|
27
|
+
let k2end = 0;
|
|
28
|
+
for (let d = 0; d < maxD; d++) {
|
|
29
|
+
for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
|
30
|
+
spend(budget);
|
|
31
|
+
const i = offset + k1;
|
|
32
|
+
let x1 = k1 === -d || k1 !== d && v1[i - 1] < v1[i + 1] ? v1[i + 1] : v1[i - 1] + 1;
|
|
33
|
+
let y1 = x1 - k1;
|
|
34
|
+
while (x1 < n && y1 < m && a[aLo + x1] === b[bLo + y1]) {
|
|
35
|
+
spend(budget);
|
|
36
|
+
x1++;
|
|
37
|
+
y1++;
|
|
38
|
+
}
|
|
39
|
+
v1[i] = x1;
|
|
40
|
+
if (x1 > n) k1end += 2;
|
|
41
|
+
else if (y1 > m) k1start += 2;
|
|
42
|
+
else if (front) {
|
|
43
|
+
const j = offset + delta - k1;
|
|
44
|
+
if (j >= 0 && j < size && v2[j] !== -1 && x1 >= n - v2[j]) return [x1, y1];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (let k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
|
|
48
|
+
spend(budget);
|
|
49
|
+
const i = offset + k2;
|
|
50
|
+
let x2 = k2 === -d || k2 !== d && v2[i - 1] < v2[i + 1] ? v2[i + 1] : v2[i - 1] + 1;
|
|
51
|
+
let y2 = x2 - k2;
|
|
52
|
+
while (x2 < n && y2 < m && a[aHi - x2 - 1] === b[bHi - y2 - 1]) {
|
|
53
|
+
spend(budget);
|
|
54
|
+
x2++;
|
|
55
|
+
y2++;
|
|
56
|
+
}
|
|
57
|
+
v2[i] = x2;
|
|
58
|
+
if (x2 > n) k2end += 2;
|
|
59
|
+
else if (y2 > m) k2start += 2;
|
|
60
|
+
else if (!front) {
|
|
61
|
+
const j = offset + delta - k2;
|
|
62
|
+
if (j >= 0 && j < size && v1[j] !== -1) {
|
|
63
|
+
const x1 = v1[j];
|
|
64
|
+
const y1 = x1 - (j - offset);
|
|
65
|
+
if (x1 >= n - x2) return [x1, y1];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
/** The changes from `a[aLo..aHi)` to `b[bLo..bHi)`, in order, pushed to `out`. */
|
|
73
|
+
function diffRange(a, aLo, aHi, b, bLo, bHi, budget, out, depth = 0) {
|
|
74
|
+
if (depth > 256) throw failed();
|
|
75
|
+
while (aLo < aHi && bLo < bHi && a[aLo] === b[bLo]) {
|
|
76
|
+
spend(budget);
|
|
77
|
+
aLo++;
|
|
78
|
+
bLo++;
|
|
79
|
+
}
|
|
80
|
+
while (aLo < aHi && bLo < bHi && a[aHi - 1] === b[bHi - 1]) {
|
|
81
|
+
spend(budget);
|
|
82
|
+
aHi--;
|
|
83
|
+
bHi--;
|
|
84
|
+
}
|
|
85
|
+
if (aLo === aHi && bLo === bHi) return;
|
|
86
|
+
if (aLo === aHi || bLo === bHi) {
|
|
87
|
+
out.push([
|
|
88
|
+
aLo,
|
|
89
|
+
aHi,
|
|
90
|
+
bLo,
|
|
91
|
+
bHi
|
|
92
|
+
]);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const seen = /* @__PURE__ */ new Set();
|
|
96
|
+
for (let i = aLo; i < aHi; i++) {
|
|
97
|
+
spend(budget);
|
|
98
|
+
seen.add(a[i]);
|
|
99
|
+
}
|
|
100
|
+
let common = false;
|
|
101
|
+
for (let i = bLo; i < bHi; i++) {
|
|
102
|
+
spend(budget);
|
|
103
|
+
if (seen.has(b[i])) {
|
|
104
|
+
common = true;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!common) {
|
|
109
|
+
out.push([
|
|
110
|
+
aLo,
|
|
111
|
+
aHi,
|
|
112
|
+
bLo,
|
|
113
|
+
bHi
|
|
114
|
+
]);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const split = bisect(a, aLo, aHi, b, bLo, bHi, budget);
|
|
118
|
+
if (!split || !split[0] && !split[1] || split[0] === aHi - aLo && split[1] === bHi - bLo) throw failed();
|
|
119
|
+
diffRange(a, aLo, aLo + split[0], b, bLo, bLo + split[1], budget, out, depth + 1);
|
|
120
|
+
diffRange(a, aLo + split[0], aHi, b, bLo + split[1], bHi, budget, out, depth + 1);
|
|
121
|
+
}
|
|
122
|
+
/** The changes between two lists, items compared with `===`, with touching ones joined. */
|
|
123
|
+
function diffLists(a, b, budget = createDiffBudget()) {
|
|
124
|
+
const raw = [];
|
|
125
|
+
diffRange(a, 0, a.length, b, 0, b.length, budget, raw);
|
|
126
|
+
const out = [];
|
|
127
|
+
for (const change of raw) {
|
|
128
|
+
const last = out[out.length - 1];
|
|
129
|
+
if (last && last[1] === change[0] && last[3] === change[2]) {
|
|
130
|
+
last[1] = change[1];
|
|
131
|
+
last[3] = change[3];
|
|
132
|
+
} else out.push(change);
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
export { createDiffBudget, diffLists };
|
|
137
|
+
|
|
138
|
+
//# sourceMappingURL=inline-diff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inline-diff.js","names":[],"sources":["../../src/collaboration/inline-diff.ts"],"sourcesContent":["// Exact Myers diff with a shared operation budget. A rendering diff may\n// approximate a difficult edit; a collaborative write must never do so.\n/** A changed range by index: `a[aFrom..aTo)` became `b[bFrom..bTo)`. */\ntype Change = [aFrom: number, aTo: number, bFrom: number, bTo: number]\nexport interface DiffBudget {\n left: number\n}\nexport const createDiffBudget = (): DiffBudget => ({ left: 8_000_000 })\nconst failed = () =>\n new Error(\n '[@dou-ui/editor] updateMap cannot safely merge this text within its comparison limit. Write a smaller change, or read the current document and reapply the change before retrying; the shared document has not been changed.',\n )\nfunction spend(budget: DiffBudget) {\n if (--budget.left < 0) throw failed()\n}\n\n/**\n * The middle of the shortest edit path from `a[aLo..aHi)` to `b[bLo..bHi)`,\n * found from both ends at once (Myers, linear space), or `null` when the\n * two share nothing. All regions use the same operation budget.\n */\nfunction bisect(\n a: ArrayLike<unknown>,\n aLo: number,\n aHi: number,\n b: ArrayLike<unknown>,\n bLo: number,\n bHi: number,\n budget: DiffBudget,\n): [number, number] | null {\n const n = aHi - aLo\n const m = bHi - bLo\n const maxD = Math.ceil((n + m) / 2)\n const offset = maxD + 1\n const size = 2 * offset + 1\n const v1 = new Int32Array(size).fill(-1)\n const v2 = new Int32Array(size).fill(-1)\n v1[offset + 1] = 0\n v2[offset + 1] = 0\n const delta = n - m\n // With an odd difference the paths meet on a forward step, else on a reverse one.\n const front = (delta & 1) !== 0\n let k1start = 0\n let k1end = 0\n let k2start = 0\n let k2end = 0\n for (let d = 0; d < maxD; d++) {\n for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {\n spend(budget)\n const i = offset + k1\n let x1 = k1 === -d || (k1 !== d && v1[i - 1]! < v1[i + 1]!) ? v1[i + 1]! : v1[i - 1]! + 1\n let y1 = x1 - k1\n while (x1 < n && y1 < m && a[aLo + x1] === b[bLo + y1]) {\n spend(budget)\n x1++\n y1++\n }\n v1[i] = x1\n if (x1 > n) k1end += 2\n else if (y1 > m) k1start += 2\n else if (front) {\n const j = offset + delta - k1\n if (j >= 0 && j < size && v2[j] !== -1 && x1 >= n - v2[j]!) return [x1, y1]\n }\n }\n for (let k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {\n spend(budget)\n const i = offset + k2\n let x2 = k2 === -d || (k2 !== d && v2[i - 1]! < v2[i + 1]!) ? v2[i + 1]! : v2[i - 1]! + 1\n let y2 = x2 - k2\n while (x2 < n && y2 < m && a[aHi - x2 - 1] === b[bHi - y2 - 1]) {\n spend(budget)\n x2++\n y2++\n }\n v2[i] = x2\n if (x2 > n) k2end += 2\n else if (y2 > m) k2start += 2\n else if (!front) {\n const j = offset + delta - k2\n if (j >= 0 && j < size && v1[j] !== -1) {\n const x1 = v1[j]!\n const y1 = x1 - (j - offset)\n if (x1 >= n - x2) return [x1, y1]\n }\n }\n }\n }\n return null\n}\n\n/** The changes from `a[aLo..aHi)` to `b[bLo..bHi)`, in order, pushed to `out`. */\nfunction diffRange(\n a: ArrayLike<unknown>,\n aLo: number,\n aHi: number,\n b: ArrayLike<unknown>,\n bLo: number,\n bHi: number,\n budget: DiffBudget,\n out: Change[],\n depth = 0,\n) {\n if (depth > 256) throw failed()\n while (aLo < aHi && bLo < bHi && a[aLo] === b[bLo]) {\n spend(budget)\n aLo++\n bLo++\n }\n while (aLo < aHi && bLo < bHi && a[aHi - 1] === b[bHi - 1]) {\n spend(budget)\n aHi--\n bHi--\n }\n if (aLo === aHi && bLo === bHi) return\n if (aLo === aHi || bLo === bHi) {\n out.push([aLo, aHi, bLo, bHi])\n return\n }\n // Entirely different alphabets need no edit-path search. This is an\n // exact replacement, including for very long generated text.\n const seen = new Set<unknown>()\n for (let i = aLo; i < aHi; i++) {\n spend(budget)\n seen.add(a[i])\n }\n let common = false\n for (let i = bLo; i < bHi; i++) {\n spend(budget)\n if (seen.has(b[i])) {\n common = true\n break\n }\n }\n if (!common) {\n out.push([aLo, aHi, bLo, bHi])\n return\n }\n const split = bisect(a, aLo, aHi, b, bLo, bHi, budget)\n if (!split || (!split[0] && !split[1]) || (split[0] === aHi - aLo && split[1] === bHi - bLo))\n throw failed()\n diffRange(a, aLo, aLo + split[0], b, bLo, bLo + split[1], budget, out, depth + 1)\n diffRange(a, aLo + split[0], aHi, b, bLo + split[1], bHi, budget, out, depth + 1)\n}\n\n/** The changes between two lists, items compared with `===`, with touching ones joined. */\nexport function diffLists(\n a: ArrayLike<unknown>,\n b: ArrayLike<unknown>,\n budget = createDiffBudget(),\n): Change[] {\n const raw: Change[] = []\n diffRange(a, 0, a.length, b, 0, b.length, budget, raw)\n const out: Change[] = []\n for (const change of raw) {\n const last = out[out.length - 1]\n if (last && last[1] === change[0] && last[3] === change[2]) {\n last[1] = change[1]\n last[3] = change[3]\n } else out.push(change)\n }\n return out\n}\n"],"mappings":";AAOA,IAAa,0BAAsC,EAAE,MAAM,IAAU;AACrE,IAAM,+BACJ,IAAI,MACF,8NACF;AACF,SAAS,MAAM,QAAoB;CACjC,IAAI,EAAE,OAAO,OAAO,GAAG,MAAM,OAAO;AACtC;;;;;;AAOA,SAAS,OACP,GACA,KACA,KACA,GACA,KACA,KACA,QACyB;CACzB,MAAM,IAAI,MAAM;CAChB,MAAM,IAAI,MAAM;CAChB,MAAM,OAAO,KAAK,MAAM,IAAI,KAAK,CAAC;CAClC,MAAM,SAAS,OAAO;CACtB,MAAM,OAAO,IAAI,SAAS;CAC1B,MAAM,KAAK,IAAI,WAAW,IAAI,CAAC,CAAC,KAAK,EAAE;CACvC,MAAM,KAAK,IAAI,WAAW,IAAI,CAAC,C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AAU,CAAC,MAAM,MAAM,CAAC,MAAM,MAAQ,MAAM,OAAO,MAAM,OAAO,MAAM,OAAO,MAAM,KACtF,MAAM,OAAO;CACf,UAAU,GAAG,KAAK,MAAM,MAAM,IAAI,GAAG,KAAK,MAAM,MAAM,IAAI,QAAQ,KAAK,QAAQ,CAAC;CAChF,UAAU,GAAG,MAAM,MAAM,IAAI,KAAK,GAAG,MAAM,MAAM,IAAI,KAAK,QAAQ,KAAK,QAAQ,CAAC;AAClF;;AAGA,SAAgB,UACd,GACA,GACA,SAAS,iBAAiB,GAChB;CACV,MAAM,MAAgB,CAAC;CACvB,UAAU,GAAG,GAAG,EAAE,QAAQ,GAAG,GAAG,EAAE,QAAQ,QAAQ,GAAG;CACrD,MAAM,MAAgB,CAAC;CACvB,KAAK,MAAM,UAAU,KAAK;EACxB,MAAM,OAAO,IAAI,IAAI,SAAS;EAC9B,IAAI,QAAQ,KAAK,OAAO,OAAO,MAAM,KAAK,OAAO,OAAO,IAAI;GAC1D,KAAK,KAAK,OAAO;GACjB,KAAK,KAAK,OAAO;EACnB,OAAO,IAAI,KAAK,MAAM;CACxB;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { changedOrderKeys, keysBetween } 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 current = /* @__PURE__ */ new Map();
|
|
89
|
+
for (const [id, fields] of map) {
|
|
90
|
+
if (!(fields instanceof Y.Map)) continue;
|
|
91
|
+
const order = fields.get(ORDER);
|
|
92
|
+
current.set(id, typeof order === "string" ? order : null);
|
|
93
|
+
}
|
|
94
|
+
const keys = changedOrderKeys([...old.keys()], items.map((item) => item.id), current, (a, b) => a.localeCompare(b));
|
|
95
|
+
for (const [id, key] of keys) map.get(id).set(ORDER, key);
|
|
96
|
+
}
|
|
97
|
+
/** A new item, for a map not yet in the document (which can't be read). */
|
|
98
|
+
function newItem(item, objects, order) {
|
|
99
|
+
const fields = new Y.Map();
|
|
100
|
+
if (order !== void 0) fields.set(ORDER, order);
|
|
101
|
+
for (const [key, value] of Object.entries(item)) if (key !== "id" && value !== void 0) fields.set(key, newField(key, value, objects));
|
|
102
|
+
return fields;
|
|
103
|
+
}
|
|
104
|
+
function newList(items, objects) {
|
|
105
|
+
const list = new Y.Map();
|
|
106
|
+
const valid = items.filter(isItem);
|
|
107
|
+
const keys = keysBetween(null, null, valid.length);
|
|
108
|
+
valid.forEach((item, i) => list.set(item.id, newItem(item, objects, keys[i])));
|
|
109
|
+
return list;
|
|
110
|
+
}
|
|
111
|
+
/** A merged prop as it is first stored: built without reading, as nothing is in the document yet. */
|
|
112
|
+
function newMerged(value, { lists, objects }) {
|
|
113
|
+
if (!isObject(value)) return value;
|
|
114
|
+
const map = new Y.Map();
|
|
115
|
+
for (const [key, field] of Object.entries(value)) {
|
|
116
|
+
if (field === void 0) continue;
|
|
117
|
+
map.set(key, lists.includes(key) && Array.isArray(field) ? newList(field, objects) : field);
|
|
118
|
+
}
|
|
119
|
+
return map;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Brings the stored prop from `before` to `after`. A value that isn't an
|
|
123
|
+
* object is stored as it is.
|
|
124
|
+
*/
|
|
125
|
+
function writeMerged(props, name, spec, after, before) {
|
|
126
|
+
if (same(before, after)) return;
|
|
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 && (!base || base[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 { changedOrderKeys, keysBetween } 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 const current = new Map<string, string | null>()\n for (const [id, fields] of map) {\n if (!(fields instanceof Y.Map)) continue\n const order = fields.get(ORDER)\n current.set(id, typeof order === 'string' ? order : null)\n }\n const keys = changedOrderKeys(\n [...old.keys()],\n items.map((item) => item.id),\n current,\n (a, b) => a.localeCompare(b),\n )\n for (const [id, key] of keys) (map.get(id) as Shared).set(ORDER, key)\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 (same(before, after)) return\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()))\n if (after[key] === undefined && (!base || base[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,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AG,IAAI,IAAI,KAAK,KAAK;CAC5F;CACA,KAAK,MAAM,OAAO,MAAM,KAAK,IAAI,KAAK,CAAC,GACrC,IAAI,MAAM,SAAS,KAAA,MAAc,CAAC,QAAQ,KAAK,SAAS,KAAA,IAAY,IAAI,OAAO,GAAG;AACtF"}
|
|
@@ -83,6 +83,46 @@ function orderKeys(keys) {
|
|
|
83
83
|
}
|
|
84
84
|
return result;
|
|
85
85
|
}
|
|
86
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Keys only for items inserted or moved between base and written. Other
|
|
88
|
+
* surviving items retain the room's order, including its new items.
|
|
89
|
+
*/
|
|
90
|
+
function changedOrderKeys(before, after, current, compareIds) {
|
|
91
|
+
const old = new Map(before.map((id, index) => [id, String(index).padStart(12, "0")]));
|
|
92
|
+
const stays = keptIndexes(after.map((id) => old.get(id) ?? null));
|
|
93
|
+
const moved = new Set(after.filter((id, index) => current.has(id) && !stays.has(index)));
|
|
94
|
+
if (!moved.size) return /* @__PURE__ */ new Map();
|
|
95
|
+
const ordered = [...current.keys()].filter((id) => !moved.has(id));
|
|
96
|
+
ordered.sort((a, b) => {
|
|
97
|
+
const left = current.get(a) ?? "";
|
|
98
|
+
const right = current.get(b) ?? "";
|
|
99
|
+
return left === right ? compareIds ? compareIds(a, b) : a < b ? -1 : a > b ? 1 : 0 : left < right ? -1 : 1;
|
|
100
|
+
});
|
|
101
|
+
const links = /* @__PURE__ */ new Map();
|
|
102
|
+
let head = ordered[0] ?? null;
|
|
103
|
+
ordered.forEach((id, index) => links.set(id, {
|
|
104
|
+
previous: ordered[index - 1] ?? null,
|
|
105
|
+
next: ordered[index + 1] ?? null
|
|
106
|
+
}));
|
|
107
|
+
let previous = null;
|
|
108
|
+
for (const id of after) {
|
|
109
|
+
if (moved.has(id)) {
|
|
110
|
+
const next = previous === null ? head : links.get(previous).next;
|
|
111
|
+
links.set(id, {
|
|
112
|
+
previous,
|
|
113
|
+
next
|
|
114
|
+
});
|
|
115
|
+
if (previous === null) head = id;
|
|
116
|
+
else links.get(previous).next = id;
|
|
117
|
+
if (next !== null) links.get(next).previous = id;
|
|
118
|
+
}
|
|
119
|
+
if (links.has(id)) previous = id;
|
|
120
|
+
}
|
|
121
|
+
const result = [];
|
|
122
|
+
for (let id = head; id !== null; id = links.get(id).next) result.push(id);
|
|
123
|
+
const next = orderKeys(result.map((id) => moved.has(id) ? null : current.get(id) ?? null));
|
|
124
|
+
return new Map(result.flatMap((id, index) => current.get(id) !== next[index] ? [[id, next[index]]] : []));
|
|
125
|
+
}
|
|
126
|
+
export { changedOrderKeys, keptIndexes, keyBetween, keysBetween, orderKeys };
|
|
87
127
|
|
|
88
128
|
//# sourceMappingURL=order.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.js","names":[],"sources":["../../src/collaboration/order.ts"],"sourcesContent":["/**\n * Order keys: strings that sort like the blocks they belong to. A key can\n * always be made between two others, so moving or inserting a block only\n * writes that block's key, never its neighbours'.\n */\n\n// ascending in code-unit order, so plain `<` compares keys\nconst DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'\n\n/** A key between `a` and `b` (`b === null` means no upper bound). Keys never end in `0`. */\nfunction midpoint(a: string, b: string | null): string {\n if (b !== null) {\n let n = 0\n while ((a[n] ?? '0') === b[n]) n++\n if (n > 0) return b.slice(0, n) + midpoint(a.slice(n), b.slice(n))\n }\n const low = a ? DIGITS.indexOf(a[0]!) : 0\n const high = b !== null ? DIGITS.indexOf(b[0]!) : DIGITS.length\n if (high - low > 1) return DIGITS[Math.round((low + high) / 2)]!\n if (b !== null && b.length > 1) return b.slice(0, 1)\n return DIGITS[low]! + midpoint(a.slice(1), null)\n}\n\n/** A key after `a` and before `b`; either may be missing. Needs `a < b`. */\nexport function keyBetween(a: string | null, b: string | null): string {\n if (a !== null && b !== null && a >= b)\n throw new Error(`[@dou-ui/editor] bad order keys ${a} ${b}`)\n return midpoint(a ?? '', b)\n}\n\n/** `n` keys in order between `a` and `b`, kept short by splitting the range in halves. */\nexport function keysBetween(a: string | null, b: string | null, n: number): string[] {\n if (n <= 0) return []\n const middle = keyBetween(a, b)\n const left = Math.floor((n - 1) / 2)\n return [...keysBetween(a, middle, left), middle, ...keysBetween(middle, b, n - 1 - left)]\n}\n\n/**\n * Which of `keys` (in their wanted order) can stay: the longest strictly\n * increasing run. Missing keys (`null`) never stay. Returns the indexes.\n */\nexport function keptIndexes(keys: (string | null)[]): Set<number> {\n // patience sorting with back links\n const tails: number[] = []\n const previous: number[] = []\n keys.forEach((key, index) => {\n if (key === null) return\n let low = 0\n let high = tails.length\n while (low < high) {\n const mid = (low + high) >> 1\n if (keys[tails[mid]!]! < key) low = mid + 1\n else high = mid\n }\n previous[index] = low > 0 ? tails[low - 1]! : -1\n tails[low] = index\n })\n const kept = new Set<number>()\n let index = tails.length ? tails[tails.length - 1]! : -1\n while (index >= 0) {\n kept.add(index)\n index = previous[index]!\n }\n return kept\n}\n\n/**\n * Order keys for items in their wanted order, given each one's current key\n * (`null` when it has none). The longest run already in order keeps its\n * keys; the others get fresh keys between their kept neighbours.\n */\nexport function orderKeys(keys: (string | null)[]): string[] {\n const kept = keptIndexes(keys)\n const result = keys.slice()\n let i = 0\n while (i < result.length) {\n if (kept.has(i)) {\n i++\n continue\n }\n let j = i\n while (j < result.length && !kept.has(j)) j++\n const fresh = keysBetween(\n i > 0 ? result[i - 1]! : null,\n j < result.length ? result[j]! : null,\n j - i,\n )\n for (let k = i; k < j; k++) result[k] = fresh[k - i]!\n i = j\n }\n return result as string[]\n}\n"],"mappings":";;;;;;AAOA,IAAM,SAAS;;AAGf,SAAS,SAAS,GAAW,GAA0B;CACrD,IAAI,MAAM,MAAM;EACd,IAAI,IAAI;EACR,QAAQ,EAAE,MAAM,SAAS,EAAE,IAAI;EAC/B,IAAI,IAAI,GAAG,OAAO,EAAE,MAAM,GAAG,CAAC,IAAI,SAAS,EAAE,MAAM,CAAC,GAAG,EAAE,MA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QAAQ,MAAM,SAAS,MAAM,MAAM,SAAS,KAAM;CACtD,OAAO,SAAS,GAAG;EACjB,KAAK,IAAI,KAAK;EACd,QAAQ,SAAS;CACnB;CACA,OAAO;AACT;;;;;;AAOA,SAAgB,UAAU,MAAmC;CAC3D,MAAM,OAAO,YAAY,IAAI;CAC7B,MAAM,SAAS,KAAK,MAAM;CAC1B,IAAI,IAAI;CACR,OAAO,IAAI,OAAO,QAAQ;EACxB,IAAI,KAAK,IAAI,CAAC,GAAG;GACf;GACA;EACF;EACA,IAAI,IAAI;EACR,OAAO,IAAI,OAAO,UAAU,CAAC,KAAK,IAAI,CAAC,GAAG;EAC1C,MAAM,QAAQ,YACZ,IAAI,IAAI,OAAO,IAAI,KAAM,MACzB,IAAI,OAAO,SAAS,OAAO,KAAM,MACjC,IAAI,CACN;EACA,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,OAAO,KAAK,MAAM,IAAI;EAClD,IAAI;CACN;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"order.js","names":[],"sources":["../../src/collaboration/order.ts"],"sourcesContent":["/**\n * Order keys: strings that sort like the blocks they belong to. A key can\n * always be made between two others, so moving or inserting a block only\n * writes that block's key, never its neighbours'.\n */\n\n// ascending in code-unit order, so plain `<` compares keys\nconst DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'\n\n/** A key between `a` and `b` (`b === null` means no upper bound). Keys never end in `0`. */\nfunction midpoint(a: string, b: string | null): string {\n if (b !== null) {\n let n = 0\n while ((a[n] ?? '0') === b[n]) n++\n if (n > 0) return b.slice(0, n) + midpoint(a.slice(n), b.slice(n))\n }\n const low = a ? DIGITS.indexOf(a[0]!) : 0\n const high = b !== null ? DIGITS.indexOf(b[0]!) : DIGITS.length\n if (high - low > 1) return DIGITS[Math.round((low + high) / 2)]!\n if (b !== null && b.length > 1) return b.slice(0, 1)\n return DIGITS[low]! + midpoint(a.slice(1), null)\n}\n\n/** A key after `a` and before `b`; either may be missing. Needs `a < b`. */\nexport function keyBetween(a: string | null, b: string | null): string {\n if (a !== null && b !== null && a >= b)\n throw new Error(`[@dou-ui/editor] bad order keys ${a} ${b}`)\n return midpoint(a ?? '', b)\n}\n\n/** `n` keys in order between `a` and `b`, kept short by splitting the range in halves. */\nexport function keysBetween(a: string | null, b: string | null, n: number): string[] {\n if (n <= 0) return []\n const middle = keyBetween(a, b)\n const left = Math.floor((n - 1) / 2)\n return [...keysBetween(a, middle, left), middle, ...keysBetween(middle, b, n - 1 - left)]\n}\n\n/**\n * Which of `keys` (in their wanted order) can stay: the longest strictly\n * increasing run. Missing keys (`null`) never stay. Returns the indexes.\n */\nexport function keptIndexes(keys: (string | null)[]): Set<number> {\n // patience sorting with back links\n const tails: number[] = []\n const previous: number[] = []\n keys.forEach((key, index) => {\n if (key === null) return\n let low = 0\n let high = tails.length\n while (low < high) {\n const mid = (low + high) >> 1\n if (keys[tails[mid]!]! < key) low = mid + 1\n else high = mid\n }\n previous[index] = low > 0 ? tails[low - 1]! : -1\n tails[low] = index\n })\n const kept = new Set<number>()\n let index = tails.length ? tails[tails.length - 1]! : -1\n while (index >= 0) {\n kept.add(index)\n index = previous[index]!\n }\n return kept\n}\n\n/**\n * Order keys for items in their wanted order, given each one's current key\n * (`null` when it has none). The longest run already in order keeps its\n * keys; the others get fresh keys between their kept neighbours.\n */\nexport function orderKeys(keys: (string | null)[]): string[] {\n const kept = keptIndexes(keys)\n const result = keys.slice()\n let i = 0\n while (i < result.length) {\n if (kept.has(i)) {\n i++\n continue\n }\n let j = i\n while (j < result.length && !kept.has(j)) j++\n const fresh = keysBetween(\n i > 0 ? result[i - 1]! : null,\n j < result.length ? result[j]! : null,\n j - i,\n )\n for (let k = i; k < j; k++) result[k] = fresh[k - i]!\n i = j\n }\n return result as string[]\n}\n\n/**\n * Keys only for items inserted or moved between base and written. Other\n * surviving items retain the room's order, including its new items.\n */\nexport function changedOrderKeys(\n before: string[],\n after: string[],\n current: Map<string, string | null>,\n compareIds?: (a: string, b: string) => number,\n): Map<string, string> {\n const old = new Map(before.map((id, index) => [id, String(index).padStart(12, '0')]))\n const stays = keptIndexes(after.map((id) => old.get(id) ?? null))\n const moved = new Set(after.filter((id, index) => current.has(id) && !stays.has(index)))\n if (!moved.size) return new Map()\n const ordered = [...current.keys()].filter((id) => !moved.has(id))\n ordered.sort((a, b) => {\n const left = current.get(a) ?? ''\n const right = current.get(b) ?? ''\n return left === right\n ? compareIds\n ? compareIds(a, b)\n : a < b\n ? -1\n : a > b\n ? 1\n : 0\n : left < right\n ? -1\n : 1\n })\n // Linked neighbours avoid repeated scans/splices when many items move.\n const links = new Map<string, { previous: string | null; next: string | null }>()\n let head = ordered[0] ?? null\n ordered.forEach((id, index) =>\n links.set(id, { previous: ordered[index - 1] ?? null, next: ordered[index + 1] ?? null }),\n )\n let previous: string | null = null\n for (const id of after) {\n if (moved.has(id)) {\n const next = previous === null ? head : links.get(previous)!.next\n links.set(id, { previous, next })\n if (previous === null) head = id\n else links.get(previous)!.next = id\n if (next !== null) links.get(next)!.previous = id\n }\n if (links.has(id)) previous = id\n }\n const result: string[] = []\n for (let id = head; id !== null; id = links.get(id)!.next) result.push(id)\n const keys = result.map((id) => (moved.has(id) ? null : (current.get(id) ?? null)))\n const next = orderKeys(keys)\n return new Map(\n result.flatMap((id, index) =>\n current.get(id) !== next[index] ? [[id, next[index]!] as const] : [],\n ),\n )\n}\n"],"mappings":";;;;;;AAOA,IAAM,SAAS;;AAGf,SAAS,SAAS,GAAW,GAA0B;CACrD,IAAI,MAAM,MAAM;EACd,IAAI,IAAI;EACR,QAAQ,EAAE,MAAM,SAAS,EAAE,IAAI;EAC/B,IAAI,IAAI,GAAG,OAAO,EAAE,MAAM,GAAG,CAAC,IAAI,SAAS,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;CACnE;CACA,MAAM,MAAM,IAAI,OAAO,QAAQ,EAAE,EAAG,IAAI;CACxC,MAA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F,KACA,IAAI,IACF,IACA,IACN,OAAO,QACL,KACA;CACR,CAAC;CAED,MAAM,wBAAQ,IAAI,IAA8D;CAChF,IAAI,OAAO,QAAQ,MAAM;CACzB,QAAQ,SAAS,IAAI,UACnB,MAAM,IAAI,IAAI;EAAE,UAAU,QAAQ,QAAQ,MAAM;EAAM,MAAM,QAAQ,QAAQ,MAAM;CAAK,CAAC,CAC1F;CACA,IAAI,WAA0B;CAC9B,KAAK,MAAM,MAAM,OAAO;EACtB,IAAI,MAAM,IAAI,EAAE,GAAG;GACjB,MAAM,OAAO,aAAa,OAAO,OAAO,MAAM,IAAI,QAAQ,CAAC,CAAE;GAC7D,MAAM,IAAI,IAAI;IAAE;IAAU;GAAK,CAAC;GAChC,IAAI,aAAa,MAAM,OAAO;QACzB,MAAM,IAAI,QAAQ,CAAC,CAAE,OAAO;GACjC,IAAI,SAAS,MAAM,MAAM,IAAI,IAAI,CAAC,CAAE,WAAW;EACjD;EACA,IAAI,MAAM,IAAI,EAAE,GAAG,WAAW;CAChC;CACA,MAAM,SAAmB,CAAC;CAC1B,KAAK,IAAI,KAAK,MAAM,OAAO,MAAM,KAAK,MAAM,IAAI,EAAE,CAAC,CAAE,MAAM,OAAO,KAAK,EAAE;CAEzE,MAAM,OAAO,UADA,OAAO,KAAK,OAAQ,MAAM,IAAI,EAAE,IAAI,OAAQ,QAAQ,IAAI,EAAE,KAAK,IACrD,CAAI;CAC3B,OAAO,IAAI,IACT,OAAO,SAAS,IAAI,UAClB,QAAQ,IAAI,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,IAAI,KAAK,MAAO,CAAU,IAAI,CAAC,CACrE,CACF;AACF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createId, inlineOf } from "../core/model.js";
|
|
2
|
-
import {
|
|
3
|
-
import { inlineFrom, insertInline, updateText } from "./text.js";
|
|
2
|
+
import { changedOrderKeys } from "./order.js";
|
|
3
|
+
import { inlineFrom, insertInline, prepareTextFromBase, updateText } from "./text.js";
|
|
4
4
|
import * as Y from "yjs";
|
|
5
5
|
// src/collaboration/table.ts
|
|
6
6
|
var PARTS = [
|
|
@@ -10,6 +10,7 @@ var PARTS = [
|
|
|
10
10
|
"merges"
|
|
11
11
|
];
|
|
12
12
|
var cellKey = (row, column) => `${row}:${column}`;
|
|
13
|
+
var mergeKey = (merge, rows, columns) => JSON.stringify([merge.rows.filter((id) => !rows || rows.has(id)).sort(), merge.columns.filter((id) => !columns || columns.has(id)).sort()]);
|
|
13
14
|
var rectKey = (rect) => `${rect.top},${rect.left},${rect.bottom},${rect.right}`;
|
|
14
15
|
function part(yblock, name) {
|
|
15
16
|
let map = yblock.get(name);
|
|
@@ -103,23 +104,36 @@ function readTable(yblock, type, props) {
|
|
|
103
104
|
function writeOrder(map, before, ids) {
|
|
104
105
|
const kept = new Set(ids);
|
|
105
106
|
for (const id of before) if (!kept.has(id)) map.delete(id);
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
107
|
+
const old = new Set(before);
|
|
108
|
+
const live = ids.filter((id) => map.has(id) || !old.has(id));
|
|
109
|
+
for (const id of live) if (!map.has(id)) map.set(id, new Y.Map());
|
|
110
|
+
const current = new Map([...map].map(([id, fields]) => [id, fields.get("order") ?? null]));
|
|
111
|
+
const keys = changedOrderKeys(before, live, current);
|
|
112
|
+
for (const [id, key] of keys) map.get(id).set("order", key);
|
|
113
|
+
}
|
|
114
|
+
/** Build all cell writes without changing the map, including any diff failures. */
|
|
115
|
+
function prepareTableText(yblock, before, after, plans, budget) {
|
|
116
|
+
if (!before) return;
|
|
117
|
+
const spec = after.type.spec.rows;
|
|
118
|
+
const old = spec.grid(before);
|
|
119
|
+
const grid = spec.grid(after);
|
|
120
|
+
const previous = new Map(old.cells.map((cell) => [cellKey(old.rows[cell.row], old.columns[cell.col].id), cell.node]));
|
|
121
|
+
const cells = yblock.get("cells");
|
|
122
|
+
for (const cell of grid.cells) {
|
|
123
|
+
const key = cellKey(grid.rows[cell.row], grid.columns[cell.col].id);
|
|
124
|
+
const base = previous.get(key);
|
|
125
|
+
const text = cells?.get(key);
|
|
126
|
+
if (!base || !text || base.content.eq(cell.node.content)) continue;
|
|
127
|
+
const current = cell.node.type.create(null, inlineFrom(text, cell.node.type.schema, false));
|
|
128
|
+
plans.set(text, prepareTextFromBase(base, cell.node, current, budget));
|
|
129
|
+
}
|
|
116
130
|
}
|
|
117
131
|
/**
|
|
118
132
|
* Write the table `after` into a stored table that holds `before` (`null`
|
|
119
133
|
* for a new one). Returns whether cells that became visible hold text the
|
|
120
134
|
* editor doesn't show yet, typed by someone else while they were merged away.
|
|
121
135
|
*/
|
|
122
|
-
function writeTable(yblock, before, after) {
|
|
136
|
+
function writeTable(yblock, before, after, plans) {
|
|
123
137
|
const spec = after.type.spec.rows;
|
|
124
138
|
const grid = spec.grid(after);
|
|
125
139
|
const old = before ? spec.grid(before) : null;
|
|
@@ -128,8 +142,8 @@ function writeTable(yblock, before, after) {
|
|
|
128
142
|
props = new Y.Map();
|
|
129
143
|
yblock.set("props", props);
|
|
130
144
|
}
|
|
131
|
-
for (const [name, value] of Object.entries(grid.props)) if (JSON.stringify(props
|
|
132
|
-
for (const name of Array.from(props.keys())) if (!(name in grid.props)) props.delete(name);
|
|
145
|
+
for (const [name, value] of Object.entries(grid.props)) if (!old || JSON.stringify(old.props[name]) !== JSON.stringify(value)) props.set(name, value);
|
|
146
|
+
for (const name of Array.from(props.keys())) if (!(name in grid.props) && (!old || name in old.props)) props.delete(name);
|
|
133
147
|
const rowsMap = part(yblock, "rows");
|
|
134
148
|
const colsMap = part(yblock, "columns");
|
|
135
149
|
const cells = part(yblock, "cells");
|
|
@@ -139,13 +153,16 @@ function writeTable(yblock, before, after) {
|
|
|
139
153
|
const oldRows = old?.rows ?? [];
|
|
140
154
|
writeOrder(rowsMap, oldRows, grid.rows);
|
|
141
155
|
writeOrder(colsMap, oldCols, colIds);
|
|
156
|
+
const oldColumns = new Map(old?.columns.map((column) => [column.id, column]));
|
|
142
157
|
for (const column of grid.columns) {
|
|
143
158
|
const entry = colsMap.get(column.id);
|
|
159
|
+
if (!entry) continue;
|
|
160
|
+
const previous = oldColumns.get(column.id);
|
|
144
161
|
for (const [name, value] of Object.entries(column)) {
|
|
145
162
|
if (name === "id") continue;
|
|
146
|
-
if (JSON.stringify(
|
|
163
|
+
if (!previous || JSON.stringify(previous[name]) !== JSON.stringify(value)) entry.set(name, value);
|
|
147
164
|
}
|
|
148
|
-
for (const name of Array.from(entry.keys())) if (name !== "order" && !(name in column)) entry.delete(name);
|
|
165
|
+
for (const name of Array.from(entry.keys())) if (name !== "order" && !(name in column) && (!previous || name in previous)) entry.delete(name);
|
|
149
166
|
}
|
|
150
167
|
const allRows = /* @__PURE__ */ new Set([...oldRows, ...rowsMap.keys()]);
|
|
151
168
|
const allCols = /* @__PURE__ */ new Set([...oldCols, ...colsMap.keys()]);
|
|
@@ -153,6 +170,15 @@ function writeTable(yblock, before, after) {
|
|
|
153
170
|
for (const col of oldCols) if (!colsMap.has(col)) for (const row of allRows) cells.delete(cellKey(row, col));
|
|
154
171
|
const stored = mergeRects(merges, grid.rows, colIds);
|
|
155
172
|
const shown = new Set(stored.map(rectKey));
|
|
173
|
+
const baseMerges = /* @__PURE__ */ new Set();
|
|
174
|
+
for (const cell of old?.cells ?? []) {
|
|
175
|
+
if (cell.rowspan * cell.colspan < 2) continue;
|
|
176
|
+
baseMerges.add(mergeKey({
|
|
177
|
+
rows: old.rows.slice(cell.row, cell.row + cell.rowspan),
|
|
178
|
+
columns: oldCols.slice(cell.col, cell.col + cell.colspan)
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
181
|
+
const wantedMerges = /* @__PURE__ */ new Set();
|
|
156
182
|
const wanted = /* @__PURE__ */ new Set();
|
|
157
183
|
const created = [];
|
|
158
184
|
for (const cell of grid.cells) {
|
|
@@ -164,14 +190,17 @@ function writeTable(yblock, before, after) {
|
|
|
164
190
|
right: cell.col + cell.colspan
|
|
165
191
|
};
|
|
166
192
|
const key = rectKey(rect);
|
|
193
|
+
const value = {
|
|
194
|
+
rows: grid.rows.slice(rect.top, rect.bottom),
|
|
195
|
+
columns: colIds.slice(rect.left, rect.right)
|
|
196
|
+
};
|
|
197
|
+
const stable = mergeKey(value);
|
|
198
|
+
wantedMerges.add(stable);
|
|
167
199
|
wanted.add(key);
|
|
168
|
-
if (shown.has(key)) continue;
|
|
200
|
+
if (baseMerges.has(stable) || shown.has(key)) continue;
|
|
169
201
|
const id = createId();
|
|
170
202
|
created.push(id);
|
|
171
|
-
merges.set(id,
|
|
172
|
-
rows: grid.rows.slice(rect.top, rect.bottom),
|
|
173
|
-
columns: colIds.slice(rect.left, rect.right)
|
|
174
|
-
});
|
|
203
|
+
merges.set(id, value);
|
|
175
204
|
for (let r = rect.top; r < rect.bottom; r++) for (let c = rect.left; c < rect.right; c++) {
|
|
176
205
|
if (r === rect.top && c === rect.left) continue;
|
|
177
206
|
const text = cells.get(cellKey(grid.rows[r], colIds[c]));
|
|
@@ -180,11 +209,17 @@ function writeTable(yblock, before, after) {
|
|
|
180
209
|
}
|
|
181
210
|
const kept = new Set(created);
|
|
182
211
|
for (const rect of stored) if (wanted.has(rectKey(rect))) kept.add(rect.id);
|
|
183
|
-
|
|
212
|
+
const baseRows = new Set(old?.rows ?? []);
|
|
213
|
+
const baseColumns = new Set(oldCols);
|
|
214
|
+
for (const [id, merge] of merges) {
|
|
215
|
+
const stable = mergeKey(merge, baseRows, baseColumns);
|
|
216
|
+
if (!kept.has(id) && baseMerges.has(stable) && !wantedMerges.has(stable)) merges.delete(id);
|
|
217
|
+
}
|
|
184
218
|
const oldCells = /* @__PURE__ */ new Map();
|
|
185
219
|
for (const cell of old?.cells ?? []) oldCells.set(cellKey(old.rows[cell.row], oldCols[cell.col]), cell.node);
|
|
186
220
|
let revealed = false;
|
|
187
221
|
for (const cell of grid.cells) {
|
|
222
|
+
if (!rowsMap.has(grid.rows[cell.row]) || !colsMap.has(colIds[cell.col])) continue;
|
|
188
223
|
const key = cellKey(grid.rows[cell.row], colIds[cell.col]);
|
|
189
224
|
const node = cell.node;
|
|
190
225
|
let text = cells.get(key);
|
|
@@ -196,7 +231,11 @@ function writeTable(yblock, before, after) {
|
|
|
196
231
|
}
|
|
197
232
|
const previous = oldCells.get(key);
|
|
198
233
|
if (previous) {
|
|
199
|
-
if (previous !== node && !previous.content.eq(node.content))
|
|
234
|
+
if (previous !== node && !previous.content.eq(node.content)) {
|
|
235
|
+
const plan = plans?.get(text);
|
|
236
|
+
if (plan) plan(text);
|
|
237
|
+
else updateText(text, previous, node);
|
|
238
|
+
}
|
|
200
239
|
} else if (node.content.size) {
|
|
201
240
|
text.delete(0, text.length);
|
|
202
241
|
insertInline(text, 0, node, 0, node.content.size);
|
|
@@ -229,6 +268,6 @@ function cellStart(table, key) {
|
|
|
229
268
|
};
|
|
230
269
|
return null;
|
|
231
270
|
}
|
|
232
|
-
export { cellAt, cellStart, clearTable, readTable, writeTable };
|
|
271
|
+
export { cellAt, cellStart, clearTable, prepareTableText, readTable, writeTable };
|
|
233
272
|
|
|
234
273
|
//# sourceMappingURL=table.js.map
|