@quillmark/svelte 0.29.3 → 0.30.0
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/README.md +1 -1
- package/dist/core/address.d.ts +3 -3
- package/dist/core/address.js +1 -1
- package/dist/core/codec/decode.d.ts +1 -3
- package/dist/core/codec/decode.js +145 -206
- package/dist/core/codec/encode.d.ts +13 -35
- package/dist/core/codec/encode.js +127 -408
- package/dist/core/codec/field.d.ts +9 -9
- package/dist/core/codec/field.js +85 -76
- package/dist/core/codec/index.d.ts +2 -2
- package/dist/core/codec/index.js +4 -4
- package/dist/core/codec/islands.d.ts +13 -9
- package/dist/core/codec/islands.js +54 -49
- package/dist/core/codec/marks.d.ts +12 -18
- package/dist/core/codec/marks.js +16 -26
- package/dist/core/codec/positions.d.ts +14 -7
- package/dist/core/codec/positions.js +43 -50
- package/dist/core/codec/schema.d.ts +1 -1
- package/dist/core/codec/schema.js +26 -8
- package/dist/core/codec/slash.js +2 -2
- package/dist/core/codec/table-copy.d.ts +3 -3
- package/dist/core/codec/table-copy.js +6 -4
- package/dist/core/codec/table-paste.js +13 -9
- package/dist/core/codec/table-view.js +19 -12
- package/dist/core/codec/table.d.ts +77 -43
- package/dist/core/codec/table.js +157 -48
- package/dist/visual/ArrayField.svelte +1 -1
- package/dist/visual/FormatPopover.svelte +1 -1
- package/dist/visual/ProseValue.svelte +8 -8
- package/dist/visual/VisualEditorInner.svelte +17 -11
- package/dist/visual/leaves.d.ts +1 -1
- package/dist/visual/leaves.js +1 -1
- package/dist/visual/structure.js +1 -10
- package/package.json +2 -2
|
@@ -1,12 +1,35 @@
|
|
|
1
1
|
import { core } from '../lifecycle.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { HARD_BREAK, ISLAND_SLOT, islandEntryFromNode } from './islands.js';
|
|
3
|
+
import { contentDescriptorFromPM, markKey } from './marks.js';
|
|
4
|
+
function idsOf(doc) {
|
|
5
|
+
const next = new Map();
|
|
6
|
+
doc.descendants((node) => {
|
|
7
|
+
const id = (node.attrs.block ?? node.attrs.id);
|
|
8
|
+
if (typeof id !== 'string')
|
|
9
|
+
return true;
|
|
10
|
+
const m = /^([a-z]+)(\d+)$/.exec(id);
|
|
11
|
+
if (m)
|
|
12
|
+
next.set(m[1], Math.max(next.get(m[1]) ?? 0, Number(m[2]) + 1));
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
return { blocks: new Set(), containers: new Set(), next };
|
|
16
|
+
}
|
|
17
|
+
function claim(ids, held, want, prefix) {
|
|
18
|
+
if (typeof want === 'string' && want && !held.has(want)) {
|
|
19
|
+
held.add(want);
|
|
20
|
+
return want;
|
|
21
|
+
}
|
|
22
|
+
let n = ids.next.get(prefix) ?? 0;
|
|
23
|
+
let id = `${prefix}${n}`;
|
|
24
|
+
while (held.has(id))
|
|
25
|
+
id = `${prefix}${++n}`;
|
|
26
|
+
ids.next.set(prefix, n + 1);
|
|
27
|
+
held.add(id);
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
/** Append `s` to the accumulated text. */
|
|
7
31
|
function appendText(acc, s) {
|
|
8
32
|
acc.text += s;
|
|
9
|
-
acc.usvEnd += usvLength(s);
|
|
10
33
|
}
|
|
11
34
|
/**
|
|
12
35
|
* Walk a PM document once, producing the content projection and the position-map runs; sharing the
|
|
@@ -15,503 +38,199 @@ function appendText(acc, s) {
|
|
|
15
38
|
export function scanDoc(doc) {
|
|
16
39
|
const acc = {
|
|
17
40
|
text: '',
|
|
18
|
-
|
|
41
|
+
blocks: [],
|
|
42
|
+
containers: [],
|
|
19
43
|
marks: [],
|
|
20
44
|
islands: [],
|
|
21
45
|
runs: [],
|
|
22
|
-
usvEnd: 0,
|
|
23
46
|
rawMarks: [],
|
|
24
|
-
lastContentEndPm: 0
|
|
47
|
+
lastContentEndPm: 0,
|
|
48
|
+
ids: idsOf(doc)
|
|
25
49
|
};
|
|
26
50
|
scanBlocks(acc, doc, 0, []);
|
|
51
|
+
if (!acc.blocks.length)
|
|
52
|
+
acc.blocks.push({ id: claim(acc.ids, acc.ids.blocks, null, 'b'), kind: 'para' });
|
|
27
53
|
acc.marks = mergeMarks(acc.rawMarks);
|
|
28
54
|
return acc;
|
|
29
55
|
}
|
|
30
56
|
/**
|
|
31
|
-
* The content projection a scan carries, for a caller holding a `Scan` that would
|
|
32
|
-
* again.
|
|
57
|
+
* The content projection a scan carries, with `anchors`, for a caller holding a `Scan` that would
|
|
58
|
+
* otherwise walk again. An empty part is left out, as a read leaves it.
|
|
33
59
|
*/
|
|
34
|
-
export function scanContent(s) {
|
|
35
|
-
const { text
|
|
36
|
-
|
|
60
|
+
export function scanContent(s, anchors = []) {
|
|
61
|
+
const out = { text: s.text, blocks: s.blocks };
|
|
62
|
+
if (s.containers.length)
|
|
63
|
+
out.containers = s.containers;
|
|
64
|
+
if (s.marks.length)
|
|
65
|
+
out.marks = s.marks;
|
|
66
|
+
if (anchors.length)
|
|
67
|
+
out.anchors = [...anchors].sort((a, b) => a.start - b.start || a.end - b.end || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
68
|
+
if (s.islands.length)
|
|
69
|
+
out.islands = s.islands;
|
|
70
|
+
return out;
|
|
37
71
|
}
|
|
38
72
|
/** `pmToContent`: the pure inverse of decode (drops the position runs). */
|
|
39
|
-
export function pmToContent(doc) {
|
|
40
|
-
return scanContent(scanDoc(doc));
|
|
73
|
+
export function pmToContent(doc, anchors = []) {
|
|
74
|
+
return scanContent(scanDoc(doc), anchors);
|
|
41
75
|
}
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
* siblings of one run shape read as a single container (contiguity plus an equal path is the
|
|
45
|
-
* store's "same one"), so each alternates its `instance` against the sibling before it; a container
|
|
46
|
-
* one level in already differs in path above it.
|
|
47
|
-
*/
|
|
48
|
-
function scanBlocks(acc, parent, contentStart, containers) {
|
|
76
|
+
/** Walk a block-content fragment; `contentStart` is the PM pos before its first child. */
|
|
77
|
+
function scanBlocks(acc, parent, contentStart, path) {
|
|
49
78
|
let pm = contentStart;
|
|
50
|
-
let prevShape = null;
|
|
51
|
-
let instance = 0;
|
|
52
79
|
parent.forEach((child) => {
|
|
53
|
-
|
|
54
|
-
instance = shape !== null && shape === prevShape ? instance ^ 1 : 0;
|
|
55
|
-
prevShape = shape;
|
|
56
|
-
scanBlock(acc, child, pm, containers, instance);
|
|
80
|
+
scanBlock(acc, child, pm, path);
|
|
57
81
|
pm += child.nodeSize;
|
|
58
82
|
});
|
|
59
83
|
}
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
switch (node.type.name) {
|
|
66
|
-
case 'blockquote':
|
|
67
|
-
return 'quote';
|
|
68
|
-
case 'bullet_list':
|
|
69
|
-
return 'list\u0000bullet';
|
|
70
|
-
case 'ordered_list':
|
|
71
|
-
return 'list\u0000ordered';
|
|
72
|
-
default:
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
84
|
+
/** A container the walk opens: its record, named once, and the path under it. */
|
|
85
|
+
function open(acc, node, path, record, prefix) {
|
|
86
|
+
const id = claim(acc.ids, acc.ids.containers, node.attrs.id, prefix);
|
|
87
|
+
acc.containers.push({ id, ...record });
|
|
88
|
+
return [...path, id];
|
|
75
89
|
}
|
|
76
|
-
function scanBlock(acc, node, nodePos,
|
|
90
|
+
function scanBlock(acc, node, nodePos, path) {
|
|
77
91
|
const name = node.type.name;
|
|
78
92
|
const contentStart = nodePos + 1;
|
|
79
93
|
switch (name) {
|
|
80
94
|
case 'paragraph':
|
|
81
|
-
case 'heading':
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const kind = textblockKind(node);
|
|
85
|
-
beginLine(acc, contentStart, containers, kind);
|
|
86
|
-
scanInline(acc, node, contentStart, containers, kind);
|
|
95
|
+
case 'heading':
|
|
96
|
+
beginBlock(acc, node, contentStart, path, textblockKind(node));
|
|
97
|
+
scanInline(acc, node, contentStart);
|
|
87
98
|
acc.lastContentEndPm = contentStart + node.content.size;
|
|
88
99
|
break;
|
|
89
|
-
}
|
|
90
100
|
case 'code_block': {
|
|
91
|
-
const lang = node.attrs.lang
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const codeText = node.textContent;
|
|
101
|
+
const lang = node.attrs.lang;
|
|
102
|
+
beginBlock(acc, node, contentStart, path, lang ? { kind: 'code', attrs: { lang } } : { kind: 'code' });
|
|
103
|
+
const codeText = node.textContent.replaceAll('\n', HARD_BREAK);
|
|
95
104
|
if (codeText.length)
|
|
96
105
|
emitText(acc, codeText, contentStart, []);
|
|
97
|
-
// Internal `\n`s already sit in the text (and its run); add the extra
|
|
98
|
-
// content lines they imply as `continues` code lines.
|
|
99
|
-
const extra = codeText.split('\n').length - 1;
|
|
100
|
-
for (let i = 0; i < extra; i++)
|
|
101
|
-
acc.lines.push({ containers, continues: true, ...kind });
|
|
102
106
|
acc.lastContentEndPm = contentStart + node.content.size;
|
|
103
107
|
break;
|
|
104
108
|
}
|
|
105
109
|
case 'horizontal_rule':
|
|
106
|
-
|
|
110
|
+
beginBlock(acc, node, nodePos, path, { kind: 'rule' });
|
|
107
111
|
acc.lastContentEndPm = nodePos + 1;
|
|
108
112
|
break;
|
|
109
113
|
case 'island_block':
|
|
110
|
-
|
|
111
|
-
acc.runs.push({ kind: 'atom', pmStart: nodePos,
|
|
114
|
+
beginBlock(acc, node, nodePos, path, { kind: 'para' }, node.attrs.block);
|
|
115
|
+
acc.runs.push({ kind: 'atom', pmStart: nodePos, at: acc.text.length });
|
|
112
116
|
appendText(acc, ISLAND_SLOT);
|
|
113
117
|
acc.islands.push(islandEntryFromNode(node.attrs));
|
|
114
118
|
acc.lastContentEndPm = nodePos + 1;
|
|
115
119
|
break;
|
|
116
120
|
case 'blockquote':
|
|
117
|
-
scanBlocks(acc, node, contentStart,
|
|
118
|
-
...containers,
|
|
119
|
-
withInstance({ container: 'quote' }, instance)
|
|
120
|
-
]);
|
|
121
|
+
scanBlocks(acc, node, contentStart, open(acc, node, path, { kind: 'quote' }, 'q'));
|
|
121
122
|
break;
|
|
122
123
|
case 'bullet_list':
|
|
123
124
|
case 'ordered_list': {
|
|
124
125
|
const ordered = name === 'ordered_list';
|
|
125
126
|
const start = ordered ? node.attrs.start : 1;
|
|
127
|
+
const list = open(acc, node, path, { kind: 'list', attrs: { ordered, start } }, 'l');
|
|
126
128
|
let itemPos = contentStart;
|
|
127
|
-
node.forEach((item
|
|
129
|
+
node.forEach((item) => {
|
|
128
130
|
const checked = item.attrs.checked;
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
container: 'list_item',
|
|
132
|
-
attrs: checked === null ? attrs : { ...attrs, checked }
|
|
133
|
-
}, instance);
|
|
134
|
-
scanBlocks(acc, item, itemPos + 1, [...containers, container]);
|
|
131
|
+
const record = checked === null ? { kind: 'item' } : { kind: 'item', attrs: { checked } };
|
|
132
|
+
scanBlocks(acc, item, itemPos + 1, open(acc, item, list, record, 'i'));
|
|
135
133
|
itemPos += item.nodeSize;
|
|
136
134
|
});
|
|
137
135
|
break;
|
|
138
136
|
}
|
|
139
137
|
default:
|
|
140
|
-
// Unknown block type:
|
|
141
|
-
|
|
138
|
+
// Unknown block type: an empty paragraph (defensive).
|
|
139
|
+
beginBlock(acc, node, contentStart, path, { kind: 'para' });
|
|
142
140
|
acc.lastContentEndPm = contentStart;
|
|
143
141
|
}
|
|
144
142
|
}
|
|
145
|
-
/** A textblock's
|
|
143
|
+
/** A textblock's kind: a heading's `level`, a paragraph's `x-` kind where it holds one, or `para`. */
|
|
146
144
|
function textblockKind(node) {
|
|
147
145
|
if (node.type.name === 'heading')
|
|
148
146
|
return { kind: 'heading', attrs: { level: node.attrs.level } };
|
|
147
|
+
const ext = node.attrs.ext;
|
|
148
|
+
if (ext)
|
|
149
|
+
return Object.keys(ext.attrs).length
|
|
150
|
+
? { kind: ext.kind, attrs: ext.attrs }
|
|
151
|
+
: { kind: ext.kind };
|
|
149
152
|
return { kind: 'para' };
|
|
150
153
|
}
|
|
151
|
-
/**
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
*/
|
|
155
|
-
function withInstance(container, instance) {
|
|
156
|
-
return instance === 0 ? container : { ...container, instance };
|
|
157
|
-
}
|
|
158
|
-
/** Open a new content line: emit the boundary `\n` (except the first line) + record. */
|
|
159
|
-
function beginLine(acc, thisContentStart, containers, kind) {
|
|
160
|
-
if (acc.lines.length > 0) {
|
|
154
|
+
/** Open a new block: emit the boundary `\n` (except before the first) and its record. */
|
|
155
|
+
function beginBlock(acc, node, thisContentStart, path, kind, want = node.attrs.id) {
|
|
156
|
+
if (acc.blocks.length > 0) {
|
|
161
157
|
acc.runs.push({
|
|
162
158
|
kind: 'nl',
|
|
163
159
|
pmStart: acc.lastContentEndPm,
|
|
164
160
|
pmEnd: thisContentStart,
|
|
165
|
-
|
|
161
|
+
at: acc.text.length
|
|
166
162
|
});
|
|
167
163
|
appendText(acc, '\n');
|
|
168
164
|
}
|
|
169
|
-
acc.
|
|
165
|
+
const id = claim(acc.ids, acc.ids.blocks, want, 'b');
|
|
166
|
+
acc.blocks.push({ id, ...(path.length ? { path } : {}), ...kind });
|
|
170
167
|
}
|
|
171
168
|
/** Walk a textblock's inline content; emit text/atom/hard-break runs + marks. */
|
|
172
|
-
function scanInline(acc, block, contentStart
|
|
169
|
+
function scanInline(acc, block, contentStart) {
|
|
173
170
|
let pm = contentStart;
|
|
174
171
|
block.forEach((child) => {
|
|
175
172
|
if (child.isText) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// hard break is: `joinTextblocksAround` (a bare `replaceStep`) leaves one. It rides the
|
|
180
|
-
// text run, where PM and USV advance together.
|
|
181
|
-
const breaks = text.split('\n').length - 1;
|
|
182
|
-
for (let i = 0; i < breaks; i++)
|
|
183
|
-
acc.lines.push({ containers, continues: true, ...kind });
|
|
173
|
+
// A `\n` in a textblock that is not `whitespace: 'pre'` is the break a hard break is:
|
|
174
|
+
// `joinTextblocksAround` (a bare `replaceStep`) leaves one.
|
|
175
|
+
emitText(acc, (child.text ?? '').replaceAll('\n', HARD_BREAK), pm, child.marks);
|
|
184
176
|
}
|
|
185
177
|
else if (child.type.name === 'hard_break') {
|
|
186
|
-
|
|
187
|
-
acc
|
|
188
|
-
appendText(acc, '\n');
|
|
189
|
-
acc.lines.push({ containers, continues: true, ...kind });
|
|
178
|
+
acc.runs.push({ kind: 'nl', pmStart: pm, pmEnd: pm + 1, at: acc.text.length });
|
|
179
|
+
appendText(acc, HARD_BREAK);
|
|
190
180
|
}
|
|
191
181
|
else if (child.type.name === 'island_inline') {
|
|
192
|
-
acc.runs.push({ kind: 'atom', pmStart: pm,
|
|
182
|
+
acc.runs.push({ kind: 'atom', pmStart: pm, at: acc.text.length });
|
|
193
183
|
appendText(acc, ISLAND_SLOT);
|
|
194
184
|
acc.islands.push(islandEntryFromNode(child.attrs));
|
|
195
185
|
}
|
|
196
186
|
pm += child.nodeSize;
|
|
197
187
|
});
|
|
198
188
|
}
|
|
199
|
-
/** Append a text node's chars: one position run + a content mark per formatting
|
|
189
|
+
/** Append a text node's chars: one position run + a content mark per formatting mark. */
|
|
200
190
|
function emitText(acc, s, pmStart, marks) {
|
|
201
191
|
if (!s.length)
|
|
202
192
|
return;
|
|
203
|
-
const
|
|
204
|
-
acc.runs.push({ kind: 'text', pmStart,
|
|
193
|
+
const at = acc.text.length;
|
|
194
|
+
acc.runs.push({ kind: 'text', pmStart, at, s });
|
|
205
195
|
appendText(acc, s);
|
|
206
|
-
for (const mark of marks)
|
|
207
|
-
acc.rawMarks.push({ start:
|
|
208
|
-
}
|
|
196
|
+
for (const mark of marks)
|
|
197
|
+
acc.rawMarks.push({ start: at, end: acc.text.length, ...contentDescriptorFromPM(mark) });
|
|
209
198
|
}
|
|
210
|
-
/**
|
|
211
|
-
* Merge adjacent/overlapping same-descriptor marks into maximal ranges, by the same primitives the
|
|
212
|
-
* mark diff runs on (`groupFormatting` + `union`).
|
|
213
|
-
*/
|
|
199
|
+
/** Merge adjacent/overlapping same-descriptor marks into maximal ranges. */
|
|
214
200
|
function mergeMarks(raw) {
|
|
215
|
-
const
|
|
216
|
-
for (const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
/** The edit `oldRt` → `newRt`: diffs the text once. */
|
|
224
|
-
export function contentEdit(oldRt, newRt) {
|
|
225
|
-
const delta = diffText(oldRt.text, newRt.text);
|
|
226
|
-
return delta ? { oldRt, newRt, delta } : { oldRt, newRt };
|
|
227
|
-
}
|
|
228
|
-
/** Lower an edit to a `ChangeBundle`: the old→new content diff, as ops. */
|
|
229
|
-
export function lower(edit, opts = {}) {
|
|
230
|
-
const { oldRt, newRt } = edit;
|
|
231
|
-
const { delta, islandOps } = splitIslands(edit);
|
|
232
|
-
const lineOps = diffLines(oldRt, newRt, edit.delta);
|
|
233
|
-
// The text-moving channels first: the mark diff reads them back off the bundle,
|
|
234
|
-
// so what it rebases against is what `applyChange` will be handed.
|
|
235
|
-
const bundle = {};
|
|
236
|
-
if (delta)
|
|
237
|
-
bundle.delta = delta;
|
|
238
|
-
if (islandOps.length)
|
|
239
|
-
bundle.islandOps = islandOps;
|
|
240
|
-
if (lineOps.length)
|
|
241
|
-
bundle.lineOps = lineOps;
|
|
242
|
-
const markOps = diffMarks(oldRt, newRt, bundle, opts);
|
|
243
|
-
if (markOps.length)
|
|
244
|
-
bundle.markOps = markOps;
|
|
245
|
-
return bundle;
|
|
246
|
-
}
|
|
247
|
-
/** A minimal single-splice text delta over USV code points, or `undefined` if equal. */
|
|
248
|
-
function diffText(oldText, newText) {
|
|
249
|
-
if (oldText === newText)
|
|
250
|
-
return undefined;
|
|
251
|
-
const a = codePoints(oldText);
|
|
252
|
-
const b = codePoints(newText);
|
|
253
|
-
let p = 0;
|
|
254
|
-
const min = Math.min(a.length, b.length);
|
|
255
|
-
while (p < min && a[p] === b[p])
|
|
256
|
-
p++;
|
|
257
|
-
let s = 0;
|
|
258
|
-
while (s < min - p && a[a.length - 1 - s] === b[b.length - 1 - s])
|
|
259
|
-
s++;
|
|
260
|
-
const ops = [];
|
|
261
|
-
if (p > 0)
|
|
262
|
-
ops.push({ retain: p });
|
|
263
|
-
const del = a.length - p - s;
|
|
264
|
-
if (del > 0)
|
|
265
|
-
ops.push({ delete: del });
|
|
266
|
-
const ins = b.slice(p, b.length - s).join('');
|
|
267
|
-
if (ins.length > 0)
|
|
268
|
-
ops.push({ insert: ins });
|
|
269
|
-
if (s > 0)
|
|
270
|
-
ops.push({ retain: s });
|
|
271
|
-
return { ops };
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Whether the splice moves a line boundary (a `\n` deleted or inserted). Where it does not, equal
|
|
275
|
-
* metadata on both sides is the whole story. Where it does, the store's split/join inheritance
|
|
276
|
-
* decides what each line carries: one splice that joins a boundary and opens another leaves the
|
|
277
|
-
* line count and metadata array unchanged while the kinds slide.
|
|
278
|
-
*/
|
|
279
|
-
function movesBoundary(oldText, delta) {
|
|
280
|
-
if (!delta)
|
|
281
|
-
return false;
|
|
282
|
-
let at = 0;
|
|
283
|
-
let cps;
|
|
284
|
-
for (const op of delta.ops) {
|
|
285
|
-
if ('retain' in op)
|
|
286
|
-
at += op.retain;
|
|
287
|
-
else if ('insert' in op) {
|
|
288
|
-
if (op.insert.includes('\n'))
|
|
289
|
-
return true;
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
cps ??= codePoints(oldText);
|
|
293
|
-
if (cps.slice(at, at + op.delete).includes('\n'))
|
|
294
|
-
return true;
|
|
295
|
-
at += op.delete;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return false;
|
|
299
|
-
}
|
|
300
|
-
/** Per-line `setContainers` / `setKind` / `setContinues` when line metadata changed or
|
|
301
|
-
* the splice moved a boundary; else none. */
|
|
302
|
-
function diffLines(oldRt, newRt, delta) {
|
|
303
|
-
if (!movesBoundary(oldRt.text, delta) && lineMetaEqual(oldRt.lines, newRt.lines))
|
|
304
|
-
return [];
|
|
305
|
-
const ops = [];
|
|
306
|
-
// Force every new line's metadata; redundant ops are safe no-ops. `continues` is a property of
|
|
307
|
-
// the `\n` preceding a line, so line 0 never carries it and it is set only for lines ≥ 1.
|
|
308
|
-
for (let i = 0; i < newRt.lines.length; i++) {
|
|
309
|
-
const l = newRt.lines[i];
|
|
310
|
-
ops.push({ op: 'setContainers', line: i, containers: l.containers });
|
|
311
|
-
ops.push(kindOp(i, l));
|
|
312
|
-
if (i > 0)
|
|
313
|
-
ops.push({ op: 'setContinues', line: i, continues: !!l.continues });
|
|
314
|
-
}
|
|
315
|
-
return ops;
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* A line minus its `containers` / `continues` envelope: exactly `setKind`'s payload
|
|
319
|
-
* (`ContentLineKind`). Lifted whole, so an arm added upstream reaches the op by construction.
|
|
320
|
-
*/
|
|
321
|
-
function kindPart(l) {
|
|
322
|
-
const { containers: _containers, continues: _continues, ...kind } = l;
|
|
323
|
-
return kind;
|
|
324
|
-
}
|
|
325
|
-
function kindOp(line, l) {
|
|
326
|
-
return { op: 'setKind', line, ...kindPart(l) };
|
|
327
|
-
}
|
|
328
|
-
function lineMetaEqual(a, b) {
|
|
329
|
-
if (a.length !== b.length)
|
|
330
|
-
return false;
|
|
331
|
-
for (let i = 0; i < a.length; i++) {
|
|
332
|
-
if (!!a[i].continues !== !!b[i].continues)
|
|
333
|
-
return false;
|
|
334
|
-
if (kindKey(a[i]) !== kindKey(b[i]))
|
|
335
|
-
return false;
|
|
336
|
-
if (!valueEqual(a[i].containers, b[i].containers))
|
|
337
|
-
return false;
|
|
201
|
+
const groups = new Map();
|
|
202
|
+
for (const m of raw) {
|
|
203
|
+
const { start, end, ...descriptor } = m;
|
|
204
|
+
const key = markKey(descriptor);
|
|
205
|
+
let g = groups.get(key);
|
|
206
|
+
if (!g)
|
|
207
|
+
groups.set(key, (g = { descriptor: descriptor, intervals: [] }));
|
|
208
|
+
g.intervals.push({ start, end });
|
|
338
209
|
}
|
|
339
|
-
return true;
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* A line kind's comparison key, key-order-insensitive at every depth: the sides come from different
|
|
343
|
-
* producers (a WASM read and this scan) and a kind carries a nested `attrs` bag.
|
|
344
|
-
*/
|
|
345
|
-
function kindKey(l) {
|
|
346
|
-
return canonicalJson(kindPart(l));
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Split the edit's islands off the text splice. `applyChange` throws `IslandSlotInInsert` on a
|
|
350
|
-
* `delta` that types a slot, so every slot inside the splice's inserted region is stripped from it
|
|
351
|
-
* and placed by `{ op: 'insert', at }`, which carries the backing entry. Every other island kept
|
|
352
|
-
* its slot, so a changed payload is `{ op: 'set' }` by the id both sides carry. A deleted island
|
|
353
|
-
* needs no op: the delta that removes its slot drops the entry.
|
|
354
|
-
*
|
|
355
|
-
* `at` is the slot's position in the final text: inserts apply left to right, so by the k-th, the k
|
|
356
|
-
* slots before it are in place. An id the store does not carry emits nothing (it would throw
|
|
357
|
-
* `UnknownIslandId`); nothing mints or renames an island id, so that is unreachable.
|
|
358
|
-
*/
|
|
359
|
-
function splitIslands(edit) {
|
|
360
|
-
const { oldRt, newRt, delta } = edit;
|
|
361
|
-
const slots = slotPositions(newRt.text);
|
|
362
|
-
const { start, end } = insertedRegion(delta);
|
|
363
|
-
const oldById = new Map(oldRt.islands.map((isl) => [isl.id, isl]));
|
|
364
|
-
const islandOps = [];
|
|
365
|
-
let inserts = 0;
|
|
366
|
-
newRt.islands.forEach((isl, i) => {
|
|
367
|
-
const at = slots[i];
|
|
368
|
-
if (at !== undefined && at >= start && at < end) {
|
|
369
|
-
islandOps.push({ op: 'insert', at, ...isl });
|
|
370
|
-
inserts++;
|
|
371
|
-
}
|
|
372
|
-
else {
|
|
373
|
-
const before = oldById.get(isl.id);
|
|
374
|
-
if (before && !valueEqual(before, isl))
|
|
375
|
-
islandOps.push({ op: 'set', ...isl });
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
return { delta: inserts && delta ? withoutSlots(delta) : delta, islandOps };
|
|
379
|
-
}
|
|
380
|
-
/** The USV positions of `text`'s island slots, in document order: the i-th is the i-th island's. */
|
|
381
|
-
function slotPositions(text) {
|
|
382
210
|
const out = [];
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
* `[retain?][delete?][insert?][retain?]`, so the leading retain is where the insert lands.
|
|
394
|
-
*/
|
|
395
|
-
function insertedRegion(delta) {
|
|
396
|
-
if (!delta)
|
|
397
|
-
return { start: 0, end: 0 };
|
|
398
|
-
const lead = delta.ops[0];
|
|
399
|
-
const start = lead && 'retain' in lead ? lead.retain : 0;
|
|
400
|
-
const insert = delta.ops.find((op) => 'insert' in op);
|
|
401
|
-
return { start, end: start + (insert ? usvLength(insert.insert) : 0) };
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* The delta with every island slot removed from its insert, or `undefined` when only retains
|
|
405
|
-
* remain.
|
|
406
|
-
*/
|
|
407
|
-
function withoutSlots(delta) {
|
|
408
|
-
const ops = [];
|
|
409
|
-
let splices = false;
|
|
410
|
-
for (const op of delta.ops) {
|
|
411
|
-
if ('insert' in op) {
|
|
412
|
-
const kept = op.insert.split(ISLAND_SLOT).join('');
|
|
413
|
-
if (kept.length) {
|
|
414
|
-
ops.push({ insert: kept });
|
|
415
|
-
splices = true;
|
|
211
|
+
for (const g of groups.values()) {
|
|
212
|
+
const sorted = [...g.intervals].sort((a, b) => a.start - b.start);
|
|
213
|
+
let cur = null;
|
|
214
|
+
for (const iv of sorted) {
|
|
215
|
+
if (cur && iv.start <= cur.end)
|
|
216
|
+
cur.end = Math.max(cur.end, iv.end);
|
|
217
|
+
else {
|
|
218
|
+
if (cur)
|
|
219
|
+
out.push({ ...cur, ...g.descriptor });
|
|
220
|
+
cur = { ...iv };
|
|
416
221
|
}
|
|
417
222
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
splices = true;
|
|
421
|
-
ops.push(op);
|
|
422
|
-
}
|
|
223
|
+
if (cur)
|
|
224
|
+
out.push({ ...cur, ...g.descriptor });
|
|
423
225
|
}
|
|
424
|
-
return
|
|
226
|
+
return out.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
425
227
|
}
|
|
228
|
+
// ── Lowering ────────────────────────────────────────────────────────────────
|
|
426
229
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
230
|
+
* The change taking `oldRt` to `newRt`, or `null` where the two are one value: the store's own
|
|
231
|
+
* `diff`, so what lands is the projection, ids and anchors and all.
|
|
429
232
|
*/
|
|
430
|
-
function
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
// `markOps` are the difference from it.
|
|
434
|
-
const rebased = core().mapMarks(oldRt, moved);
|
|
435
|
-
// Group both sides by descriptor key (excluding anchors).
|
|
436
|
-
const oldGroups = groupFormatting(rebased);
|
|
437
|
-
const newGroups = groupFormatting(newRt.marks);
|
|
438
|
-
const keys = new Set([...oldGroups.keys(), ...newGroups.keys()]);
|
|
439
|
-
for (const key of keys) {
|
|
440
|
-
const oldG = oldGroups.get(key);
|
|
441
|
-
const newG = newGroups.get(key);
|
|
442
|
-
const descriptor = (newG ?? oldG).descriptor;
|
|
443
|
-
const oldCov = union((oldG?.intervals ?? []).filter((iv) => iv.end > iv.start));
|
|
444
|
-
const newCov = union((newG?.intervals ?? []).filter((iv) => iv.end > iv.start));
|
|
445
|
-
for (const iv of subtract(oldCov, newCov))
|
|
446
|
-
ops.push({ op: 'remove', start: iv.start, end: iv.end, ...descriptor });
|
|
447
|
-
for (const iv of subtract(newCov, oldCov))
|
|
448
|
-
ops.push({ op: 'add', start: iv.start, end: iv.end, ...descriptor });
|
|
449
|
-
}
|
|
450
|
-
// Anchors: diff the decoration sets by id (positions already in final coords).
|
|
451
|
-
if (opts.newAnchors) {
|
|
452
|
-
const oldA = new Map(anchorsFromContent({ marks: rebased }).map((a) => [a.id, a.pos]));
|
|
453
|
-
const newA = new Map(opts.newAnchors.map((a) => [a.id, a.pos]));
|
|
454
|
-
for (const [id, pos] of newA) {
|
|
455
|
-
if (!oldA.has(id) || oldA.get(id) !== pos) {
|
|
456
|
-
if (oldA.has(id))
|
|
457
|
-
ops.push({ op: 'removeAnchor', id });
|
|
458
|
-
ops.push({ op: 'add', start: pos, end: pos, type: 'anchor', attrs: { id } });
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
for (const id of oldA.keys())
|
|
462
|
-
if (!newA.has(id))
|
|
463
|
-
ops.push({ op: 'removeAnchor', id });
|
|
464
|
-
}
|
|
465
|
-
return ops;
|
|
466
|
-
}
|
|
467
|
-
/** Group non-anchor marks by descriptor key. */
|
|
468
|
-
function groupFormatting(marks) {
|
|
469
|
-
const groups = new Map();
|
|
470
|
-
for (const m of marks) {
|
|
471
|
-
if (m.type === 'anchor')
|
|
472
|
-
continue;
|
|
473
|
-
const descriptor = descriptorOf(m);
|
|
474
|
-
const key = markKey(descriptor);
|
|
475
|
-
let g = groups.get(key);
|
|
476
|
-
if (!g) {
|
|
477
|
-
g = { descriptor, intervals: [] };
|
|
478
|
-
groups.set(key, g);
|
|
479
|
-
}
|
|
480
|
-
g.intervals.push({ start: m.start, end: m.end });
|
|
481
|
-
}
|
|
482
|
-
return groups;
|
|
483
|
-
}
|
|
484
|
-
/** Union a set of intervals into sorted, disjoint, maximal intervals. */
|
|
485
|
-
function union(intervals) {
|
|
486
|
-
if (!intervals.length)
|
|
487
|
-
return [];
|
|
488
|
-
const sorted = [...intervals].sort((a, b) => a.start - b.start || a.end - b.end);
|
|
489
|
-
const out = [{ ...sorted[0] }];
|
|
490
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
491
|
-
const last = out[out.length - 1];
|
|
492
|
-
if (sorted[i].start <= last.end)
|
|
493
|
-
last.end = Math.max(last.end, sorted[i].end);
|
|
494
|
-
else
|
|
495
|
-
out.push({ ...sorted[i] });
|
|
496
|
-
}
|
|
497
|
-
return out;
|
|
498
|
-
}
|
|
499
|
-
/** Interval-set subtraction `a \ b` (both assumed unioned/sorted). */
|
|
500
|
-
function subtract(a, b) {
|
|
501
|
-
const out = [];
|
|
502
|
-
for (const iv of a) {
|
|
503
|
-
let cursor = iv.start;
|
|
504
|
-
for (const cut of b) {
|
|
505
|
-
if (cut.end <= cursor || cut.start >= iv.end)
|
|
506
|
-
continue;
|
|
507
|
-
if (cut.start > cursor)
|
|
508
|
-
out.push({ start: cursor, end: Math.min(cut.start, iv.end) });
|
|
509
|
-
cursor = Math.max(cursor, cut.end);
|
|
510
|
-
if (cursor >= iv.end)
|
|
511
|
-
break;
|
|
512
|
-
}
|
|
513
|
-
if (cursor < iv.end)
|
|
514
|
-
out.push({ start: cursor, end: iv.end });
|
|
515
|
-
}
|
|
516
|
-
return out.filter((iv) => iv.end > iv.start);
|
|
233
|
+
export function lower(oldRt, newRt) {
|
|
234
|
+
const change = core().diff(oldRt, newRt);
|
|
235
|
+
return change.steps.length ? change : null;
|
|
517
236
|
}
|