@stesura/wordgard-view 0.0.0-stage → 0.4.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/CHANGELOG.md +7 -0
- package/LICENSE.txt +21 -0
- package/README.md +19 -2
- package/THIRD_PARTY_LICENSES.txt +89 -0
- package/dist/index.d.ts +411 -0
- package/dist/index.js +2891 -0
- package/package.json +47 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,2891 @@
|
|
|
1
|
+
// src/contexts/editorEvents.ts
|
|
2
|
+
import { Wordgard } from "wordgard/editor";
|
|
3
|
+
import { GardState } from "wordgard/state";
|
|
4
|
+
var editorEvents = [
|
|
5
|
+
"keydown",
|
|
6
|
+
"keyup",
|
|
7
|
+
"keypress",
|
|
8
|
+
"beforeinput",
|
|
9
|
+
"input",
|
|
10
|
+
"mousedown",
|
|
11
|
+
"mouseup",
|
|
12
|
+
"mouseover",
|
|
13
|
+
"mouseout",
|
|
14
|
+
"click",
|
|
15
|
+
"dblclick",
|
|
16
|
+
"contextmenu",
|
|
17
|
+
"focus",
|
|
18
|
+
"blur",
|
|
19
|
+
"copy",
|
|
20
|
+
"cut",
|
|
21
|
+
"paste",
|
|
22
|
+
"dragstart",
|
|
23
|
+
"dragend",
|
|
24
|
+
"dragenter",
|
|
25
|
+
"dragover",
|
|
26
|
+
"dragleave",
|
|
27
|
+
"drop",
|
|
28
|
+
"compositionstart",
|
|
29
|
+
"compositionupdate",
|
|
30
|
+
"compositionend"
|
|
31
|
+
];
|
|
32
|
+
var createListeners = () => ({
|
|
33
|
+
keydown: [],
|
|
34
|
+
keyup: [],
|
|
35
|
+
keypress: [],
|
|
36
|
+
beforeinput: [],
|
|
37
|
+
input: [],
|
|
38
|
+
mousedown: [],
|
|
39
|
+
mouseup: [],
|
|
40
|
+
mouseover: [],
|
|
41
|
+
mouseout: [],
|
|
42
|
+
click: [],
|
|
43
|
+
dblclick: [],
|
|
44
|
+
contextmenu: [],
|
|
45
|
+
focus: [],
|
|
46
|
+
blur: [],
|
|
47
|
+
copy: [],
|
|
48
|
+
cut: [],
|
|
49
|
+
paste: [],
|
|
50
|
+
dragstart: [],
|
|
51
|
+
dragend: [],
|
|
52
|
+
dragenter: [],
|
|
53
|
+
dragover: [],
|
|
54
|
+
dragleave: [],
|
|
55
|
+
drop: [],
|
|
56
|
+
compositionstart: [],
|
|
57
|
+
compositionupdate: [],
|
|
58
|
+
compositionend: []
|
|
59
|
+
});
|
|
60
|
+
var registries = /* @__PURE__ */ new WeakMap();
|
|
61
|
+
var listenersOf = (wg, type) => {
|
|
62
|
+
let registry = registries.get(wg);
|
|
63
|
+
if (!registry) {
|
|
64
|
+
registry = createListeners();
|
|
65
|
+
registries.set(wg, registry);
|
|
66
|
+
}
|
|
67
|
+
return registry[type];
|
|
68
|
+
};
|
|
69
|
+
var handler = (type) => Wordgard.domEventHandler(type, (event, wg) => {
|
|
70
|
+
for (const listener of [...listenersOf(wg, type)])
|
|
71
|
+
if (listener(event) || event.defaultPrevented) return true;
|
|
72
|
+
return false;
|
|
73
|
+
});
|
|
74
|
+
var installed = GardState.Facet.define({
|
|
75
|
+
combine: (values) => values.length > 0
|
|
76
|
+
});
|
|
77
|
+
var hasEditorEventHandlers = (state) => state.facet(installed.reader);
|
|
78
|
+
var editorEventHandlers = GardState.prec.highest([
|
|
79
|
+
editorEvents.map(handler),
|
|
80
|
+
installed.of(true)
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
// src/decorations/decorationHandlers.ts
|
|
84
|
+
var createDecorationHandlers = (getPos) => ({
|
|
85
|
+
getPos,
|
|
86
|
+
stopEvent: /* @__PURE__ */ new Set(),
|
|
87
|
+
ignoreMutation: /* @__PURE__ */ new Set()
|
|
88
|
+
});
|
|
89
|
+
var roots = /* @__PURE__ */ new WeakMap();
|
|
90
|
+
var decorationRoot = (handlers) => (dom) => {
|
|
91
|
+
if (dom) roots.set(dom, handlers);
|
|
92
|
+
};
|
|
93
|
+
var someAround = (node, says) => {
|
|
94
|
+
for (let cur = node; cur && !cur.wgTile?.isDoc; cur = cur.parentNode) {
|
|
95
|
+
const handlers = cur instanceof Element ? roots.get(cur) : void 0;
|
|
96
|
+
if (handlers && says(handlers)) return true;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
};
|
|
100
|
+
var stopsEvent = (event) => someAround(
|
|
101
|
+
event.target instanceof Node ? event.target : null,
|
|
102
|
+
({ stopEvent }) => [...stopEvent].some((stop) => stop(event))
|
|
103
|
+
);
|
|
104
|
+
var ignoresMutation = (record) => someAround(
|
|
105
|
+
record.target,
|
|
106
|
+
({ ignoreMutation }) => [...ignoreMutation].some((ignore) => ignore(record))
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// src/decorations/iterDeco.ts
|
|
110
|
+
import { Attributes as Attributes4, Elt as Elt6, Leaf as Leaf2, Mark } from "wordgard/doc";
|
|
111
|
+
import { Widget as Widget4 } from "wordgard/editor";
|
|
112
|
+
|
|
113
|
+
// src/changes.ts
|
|
114
|
+
var indexes = /* @__PURE__ */ new WeakMap();
|
|
115
|
+
var indexOf = (changes) => {
|
|
116
|
+
const found = indexes.get(changes);
|
|
117
|
+
if (found) return found;
|
|
118
|
+
const index = {
|
|
119
|
+
startA: [],
|
|
120
|
+
endA: [],
|
|
121
|
+
startB: [],
|
|
122
|
+
endB: [],
|
|
123
|
+
ins: [],
|
|
124
|
+
first: Infinity,
|
|
125
|
+
last: -Infinity
|
|
126
|
+
};
|
|
127
|
+
for (let i = 0, posA = 0, posB = 0; i < changes.length; i += 2) {
|
|
128
|
+
const len = changes[i] ?? 0;
|
|
129
|
+
const ins = changes[i + 1] ?? 0;
|
|
130
|
+
const endB = posB + (ins < 0 ? len : ins);
|
|
131
|
+
index.startA.push(posA);
|
|
132
|
+
index.endA.push(posA + len);
|
|
133
|
+
index.startB.push(posB);
|
|
134
|
+
index.endB.push(endB);
|
|
135
|
+
index.ins.push(ins);
|
|
136
|
+
if (ins != -1) {
|
|
137
|
+
index.first = Math.min(index.first, posA);
|
|
138
|
+
index.last = posA + len;
|
|
139
|
+
}
|
|
140
|
+
posA += len;
|
|
141
|
+
posB = endB;
|
|
142
|
+
}
|
|
143
|
+
indexes.set(changes, index);
|
|
144
|
+
return index;
|
|
145
|
+
};
|
|
146
|
+
var sectionAt = (ends, pos, inclusive = false) => {
|
|
147
|
+
let low = 0;
|
|
148
|
+
let high = ends.length;
|
|
149
|
+
while (low < high) {
|
|
150
|
+
const mid = low + high >> 1;
|
|
151
|
+
const end = ends[mid] ?? 0;
|
|
152
|
+
if (inclusive ? end >= pos : end > pos) high = mid;
|
|
153
|
+
else low = mid + 1;
|
|
154
|
+
}
|
|
155
|
+
return low;
|
|
156
|
+
};
|
|
157
|
+
var mapNodePos = (changes, pos, leaf = false) => {
|
|
158
|
+
const { startA, endA, startB, endB, ins } = indexOf(changes);
|
|
159
|
+
const k = sectionAt(endA, pos);
|
|
160
|
+
const fromA = startA[k] ?? 0;
|
|
161
|
+
const fromB = startB[k] ?? 0;
|
|
162
|
+
if (k == endA.length) {
|
|
163
|
+
const totalA = endA.at(-1) ?? 0;
|
|
164
|
+
return { pos: (endB.at(-1) ?? 0) + pos - totalA, retagged: false };
|
|
165
|
+
}
|
|
166
|
+
if ((ins[k] ?? 0) < 0) return { pos: fromB + pos - fromA, retagged: false };
|
|
167
|
+
const lenA = (endA[k] ?? 0) - fromA;
|
|
168
|
+
if (leaf && lenA == ins[k]) return { pos: fromB + pos - fromA, retagged: true };
|
|
169
|
+
return fromA == pos && lenA == 1 && ins[k] == 1 ? { pos: fromB, retagged: true } : null;
|
|
170
|
+
};
|
|
171
|
+
var samePlace = (changes, oldStart, start) => oldStart == 0 ? start == 0 : mapNodePos(changes, oldStart - 1)?.pos == start - 1;
|
|
172
|
+
var redrawn = (changes, pos) => {
|
|
173
|
+
if (!changes) return false;
|
|
174
|
+
const { endB, ins } = indexOf(changes);
|
|
175
|
+
const k = sectionAt(endB, pos);
|
|
176
|
+
return k < endB.length && ins[k] != -1;
|
|
177
|
+
};
|
|
178
|
+
var isKept = (changes, from, to) => {
|
|
179
|
+
if (!changes) return false;
|
|
180
|
+
const { startB, endB, ins } = indexOf(changes);
|
|
181
|
+
for (let k = sectionAt(endB, from, true); k < endB.length; k++) {
|
|
182
|
+
const start = startB[k] ?? 0;
|
|
183
|
+
const end = endB[k] ?? 0;
|
|
184
|
+
if (start > to) break;
|
|
185
|
+
if (ins[k] != -1 && (end == start ? start >= from && start <= to : start < to && end > from))
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
};
|
|
190
|
+
var pointMapper = (changes) => {
|
|
191
|
+
const { startA, endA, startB, ins } = indexOf(changes);
|
|
192
|
+
return (pos) => {
|
|
193
|
+
const found = [];
|
|
194
|
+
for (let k = sectionAt(endA, pos, true); k < endA.length; k++) {
|
|
195
|
+
const a = startA[k] ?? 0;
|
|
196
|
+
if (a > pos) break;
|
|
197
|
+
const b = startB[k] ?? 0;
|
|
198
|
+
const inserted = ins[k] ?? 0;
|
|
199
|
+
if (inserted < 0) found.push(b + pos - a);
|
|
200
|
+
else {
|
|
201
|
+
if (pos == a) found.push(b);
|
|
202
|
+
if (pos == (endA[k] ?? 0)) found.push(b + inserted);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return found;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
var unchangedEnds = (changes, oldPlot, oldStart, plot) => {
|
|
209
|
+
const { first, last } = indexOf(changes);
|
|
210
|
+
const before = oldPlot.content;
|
|
211
|
+
const after = plot.content;
|
|
212
|
+
const both = Math.min(before.length, after.length);
|
|
213
|
+
let head = 0;
|
|
214
|
+
for (let pos = oldStart; head < both; head++) {
|
|
215
|
+
const child = before[head];
|
|
216
|
+
if (!child || child !== after[head] || pos + child.length >= first) break;
|
|
217
|
+
pos += child.length;
|
|
218
|
+
}
|
|
219
|
+
let tail = 0;
|
|
220
|
+
for (let pos = oldStart + oldPlot.contentLength; tail < both - head; tail++) {
|
|
221
|
+
const child = before[before.length - 1 - tail];
|
|
222
|
+
if (!child || child !== after[after.length - 1 - tail] || pos - child.length <= last) break;
|
|
223
|
+
pos -= child.length;
|
|
224
|
+
}
|
|
225
|
+
return { head, tail };
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// src/renderStore.ts
|
|
229
|
+
var cells = /* @__PURE__ */ new WeakMap();
|
|
230
|
+
var cellOf = (view) => {
|
|
231
|
+
let cell = cells.get(view);
|
|
232
|
+
if (!cell) cells.set(view, cell = { view, listener: null });
|
|
233
|
+
return cell;
|
|
234
|
+
};
|
|
235
|
+
var moveCell = (store, old, view) => {
|
|
236
|
+
const cell = cellOf(old);
|
|
237
|
+
cells.set(view, cell);
|
|
238
|
+
if (cell.view == view) store.pending.delete(cell);
|
|
239
|
+
else store.pending.set(cell, view);
|
|
240
|
+
};
|
|
241
|
+
var publishCells = (store) => {
|
|
242
|
+
const before = [];
|
|
243
|
+
for (const [cell, view] of store.pending) {
|
|
244
|
+
before.push([cell, cell.view]);
|
|
245
|
+
cell.view = view;
|
|
246
|
+
cell.listener?.();
|
|
247
|
+
}
|
|
248
|
+
return () => {
|
|
249
|
+
for (const [cell, view] of before) cell.view = view;
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// src/reactKeys.ts
|
|
254
|
+
var createNodeKey = () => Math.floor(Math.random() * 281474976710655).toString(16);
|
|
255
|
+
var addFreshKeys = (keys, plot) => {
|
|
256
|
+
if (keys.has(plot)) return;
|
|
257
|
+
keys.set(
|
|
258
|
+
plot,
|
|
259
|
+
plot.content.map((child) => child.isText ? "" : createNodeKey())
|
|
260
|
+
);
|
|
261
|
+
for (const child of plot.content) if (child.isPlot) addFreshKeys(keys, child);
|
|
262
|
+
};
|
|
263
|
+
var mapKeys = (keys, oldDoc, newDoc, changes) => {
|
|
264
|
+
const replaced = /* @__PURE__ */ new WeakMap();
|
|
265
|
+
const mapChildren = (oldPlot, oldStart, newPlot, newStart) => {
|
|
266
|
+
if (keys.has(newPlot)) return;
|
|
267
|
+
replaced.set(newPlot, oldPlot);
|
|
268
|
+
const oldKeys = keys.get(oldPlot) ?? [];
|
|
269
|
+
const { head, tail } = unchangedEnds(changes, oldPlot, oldStart, newPlot);
|
|
270
|
+
const before = oldPlot.content;
|
|
271
|
+
const after = newPlot.content;
|
|
272
|
+
const oldTail = before.length - tail;
|
|
273
|
+
const newTail = after.length - tail;
|
|
274
|
+
let headSize = 0;
|
|
275
|
+
for (let i = 0; i < head; i++) headSize += before[i]?.length ?? 0;
|
|
276
|
+
const found = /* @__PURE__ */ new Map();
|
|
277
|
+
const byNode = /* @__PURE__ */ new Map();
|
|
278
|
+
const retagged = /* @__PURE__ */ new Map();
|
|
279
|
+
for (let i = head, pos = oldStart + headSize; i < oldTail; i++) {
|
|
280
|
+
const node = before[i];
|
|
281
|
+
if (!node) continue;
|
|
282
|
+
const key = oldKeys[i];
|
|
283
|
+
if (key && !node.isText) {
|
|
284
|
+
const old = { key, node, start: pos };
|
|
285
|
+
const mapped = mapNodePos(changes, pos, node.isLeaf);
|
|
286
|
+
if (mapped && !mapped.retagged) found.set(mapped.pos, old);
|
|
287
|
+
else {
|
|
288
|
+
byNode.set(node, [...byNode.get(node) ?? [], old]);
|
|
289
|
+
if (mapped) retagged.set(mapped.pos, old);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
pos += node.length;
|
|
293
|
+
}
|
|
294
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
295
|
+
const moved = [];
|
|
296
|
+
for (let i = head; i < newTail; i++) {
|
|
297
|
+
const child = after[i];
|
|
298
|
+
const old = child && !child.isText ? byNode.get(child)?.find((old2) => !claimed.has(old2)) : void 0;
|
|
299
|
+
if (old) claimed.add(old);
|
|
300
|
+
moved.push(old);
|
|
301
|
+
}
|
|
302
|
+
const same = (child, key) => {
|
|
303
|
+
if (key !== void 0) return key;
|
|
304
|
+
if (child.isPlot) addFreshKeys(keys, child);
|
|
305
|
+
return child.isText ? "" : createNodeKey();
|
|
306
|
+
};
|
|
307
|
+
const newKeys = [];
|
|
308
|
+
for (let i = 0; i < head; i++) {
|
|
309
|
+
const child = after[i];
|
|
310
|
+
if (child) newKeys.push(same(child, oldKeys[i]));
|
|
311
|
+
}
|
|
312
|
+
for (let i = head, pos = newStart + headSize; i < newTail; i++) {
|
|
313
|
+
const child = after[i];
|
|
314
|
+
if (!child) continue;
|
|
315
|
+
const start = pos;
|
|
316
|
+
pos += child.length;
|
|
317
|
+
if (child.isText) {
|
|
318
|
+
newKeys.push("");
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
let kept = found.get(start) ?? moved[i - head];
|
|
322
|
+
const retag = kept ? void 0 : retagged.get(start);
|
|
323
|
+
if (retag && !claimed.has(retag) && retag.node.type == child.type) {
|
|
324
|
+
claimed.add(retag);
|
|
325
|
+
kept = retag;
|
|
326
|
+
}
|
|
327
|
+
if (child.isPlot && kept?.node.isPlot && kept.node != child)
|
|
328
|
+
mapChildren(kept.node, kept.start + 1, child, start + 1);
|
|
329
|
+
else if (child.isPlot) addFreshKeys(keys, child);
|
|
330
|
+
newKeys.push(kept ? kept.key : createNodeKey());
|
|
331
|
+
}
|
|
332
|
+
for (let i = newTail; i < after.length; i++) {
|
|
333
|
+
const child = after[i];
|
|
334
|
+
if (child) newKeys.push(same(child, oldKeys[i - newTail + oldTail]));
|
|
335
|
+
}
|
|
336
|
+
keys.set(newPlot, newKeys);
|
|
337
|
+
};
|
|
338
|
+
mapChildren(oldDoc, 0, newDoc, 0);
|
|
339
|
+
return replaced;
|
|
340
|
+
};
|
|
341
|
+
var typeIds = /* @__PURE__ */ new WeakMap();
|
|
342
|
+
var lastTypeId = 0;
|
|
343
|
+
var typeId = (type) => {
|
|
344
|
+
let id = typeIds.get(type);
|
|
345
|
+
if (id === void 0) typeIds.set(type, id = ++lastTypeId);
|
|
346
|
+
return id;
|
|
347
|
+
};
|
|
348
|
+
var childViewKeys = (views) => {
|
|
349
|
+
const fixed = (view) => view.type == "composition" || view.type == "wrapper" ? view.key : void 0;
|
|
350
|
+
const taken = /* @__PURE__ */ new Set();
|
|
351
|
+
for (const view of views) {
|
|
352
|
+
const key = fixed(view);
|
|
353
|
+
if (key !== void 0) taken.add(key);
|
|
354
|
+
}
|
|
355
|
+
let previous = "";
|
|
356
|
+
let widgets = false;
|
|
357
|
+
const keyed = views.map((view) => {
|
|
358
|
+
if (view.type == "widget") {
|
|
359
|
+
if (view.key !== void 0) return { view, key: view.key };
|
|
360
|
+
widgets = true;
|
|
361
|
+
return { view, key: previous };
|
|
362
|
+
}
|
|
363
|
+
let key = fixed(view);
|
|
364
|
+
if (key === void 0) {
|
|
365
|
+
key = view.type == "node" ? view.key : `${previous}>${view.type == "buffer" ? `buffer${view.side}` : view.type}`;
|
|
366
|
+
while (taken.has(key)) key += "+";
|
|
367
|
+
}
|
|
368
|
+
previous = key;
|
|
369
|
+
return { view, key };
|
|
370
|
+
});
|
|
371
|
+
if (!widgets) return keyed;
|
|
372
|
+
let next = "";
|
|
373
|
+
const used = /* @__PURE__ */ new Set();
|
|
374
|
+
for (let i = keyed.length - 1; i >= 0; i--) {
|
|
375
|
+
const entry = keyed[i];
|
|
376
|
+
if (!entry) continue;
|
|
377
|
+
const { view } = entry;
|
|
378
|
+
if (view.type != "widget") {
|
|
379
|
+
next = entry.key;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (view.key !== void 0) continue;
|
|
383
|
+
const id = typeId(view.widget.type);
|
|
384
|
+
let key = view.side > 0 ? `${next}<w${id}` : `${entry.key}>w${id}`;
|
|
385
|
+
while (used.has(key)) key += "+";
|
|
386
|
+
used.add(key);
|
|
387
|
+
entry.key = key;
|
|
388
|
+
}
|
|
389
|
+
return keyed;
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// src/wordgardInternals.ts
|
|
393
|
+
import { Elt } from "wordgard/doc";
|
|
394
|
+
import { Decoration, Widget, Wordgard as Wordgard2 } from "wordgard/editor";
|
|
395
|
+
import { basicSchema } from "wordgard/schema";
|
|
396
|
+
import { GardState as GardState2 } from "wordgard/state";
|
|
397
|
+
import { Emphasis, Image, Paragraph } from "wordgard/types";
|
|
398
|
+
var TileFlag = {
|
|
399
|
+
NodeInner: 1,
|
|
400
|
+
PlotContent: 2,
|
|
401
|
+
Spanning: 4,
|
|
402
|
+
Wrapper: 8,
|
|
403
|
+
Point: 16,
|
|
404
|
+
PointBefore: 32,
|
|
405
|
+
PointAfter: 64,
|
|
406
|
+
Composition: 128,
|
|
407
|
+
Synced: 256,
|
|
408
|
+
Atom: 512,
|
|
409
|
+
Uneditable: 1024,
|
|
410
|
+
HasContent: 2048,
|
|
411
|
+
AfterContent: 4096,
|
|
412
|
+
ContentNotLast: 8192,
|
|
413
|
+
Dirty: 16384
|
|
414
|
+
};
|
|
415
|
+
var Reused = { Full: 1, DOM: 2 };
|
|
416
|
+
var DocTile = Wordgard2.DocTile;
|
|
417
|
+
var tileClasses;
|
|
418
|
+
var getTileClasses = () => {
|
|
419
|
+
if (tileClasses) return tileClasses;
|
|
420
|
+
const wg = Wordgard2.create({ doc: "<p>x</p><p></p>", config: [basicSchema()] });
|
|
421
|
+
const [text, empty] = wg.contentDOM.children;
|
|
422
|
+
const classOf = (dom) => {
|
|
423
|
+
if (!dom?.wgTile) throw new Error("Wordgard's tile tree has an unexpected shape");
|
|
424
|
+
return Object.getPrototypeOf(dom.wgTile).constructor;
|
|
425
|
+
};
|
|
426
|
+
tileClasses = {
|
|
427
|
+
EltTile: classOf(text),
|
|
428
|
+
TextTile: classOf(text?.firstChild),
|
|
429
|
+
WidgetTile: classOf(empty?.firstChild)
|
|
430
|
+
};
|
|
431
|
+
return tileClasses;
|
|
432
|
+
};
|
|
433
|
+
var decorationInternals;
|
|
434
|
+
var facetOf = (extension) => {
|
|
435
|
+
if (typeof extension == "object" && extension && "facet" in extension && extension.facet instanceof GardState2.Facet)
|
|
436
|
+
return extension.facet;
|
|
437
|
+
throw new Error("Wordgard's decoration facets have an unexpected shape");
|
|
438
|
+
};
|
|
439
|
+
var getDecorationInternals = () => {
|
|
440
|
+
if (decorationInternals) return decorationInternals;
|
|
441
|
+
const classOf = (value) => Object.getPrototypeOf(value).constructor;
|
|
442
|
+
decorationInternals = {
|
|
443
|
+
WidgetDecoration: classOf(Decoration.Point.widget(Widget.br)),
|
|
444
|
+
ShapeDecoration: classOf(Decoration.Point.shape(Elt.mk("span"))),
|
|
445
|
+
AttributeDecoration: classOf(Decoration.Point.attributes({})),
|
|
446
|
+
WrapperDecoration: classOf(Decoration.Point.wrapper(Elt.mk("span", [0]))),
|
|
447
|
+
WrapperRangeDecoration: classOf(Decoration.Range.wrapper("span", {})),
|
|
448
|
+
AttributeRangeDecoration: classOf(Decoration.Range.attribute("data-x", "")),
|
|
449
|
+
tagShape: facetOf(Decoration.Tag.shape(Image, Elt.mk("img"))),
|
|
450
|
+
tagWidget: facetOf(Decoration.Tag.widget(Paragraph, "before", Widget.br)),
|
|
451
|
+
tagWrapper: facetOf(Decoration.Tag.wrapper(Paragraph, Elt.mk("div", [0]))),
|
|
452
|
+
tagAttribute: facetOf(Decoration.Tag.attribute(Paragraph, "data-x", "")),
|
|
453
|
+
markShape: facetOf(Decoration.Mark.shape(Emphasis, { element: "i" }))
|
|
454
|
+
};
|
|
455
|
+
return decorationInternals;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// src/decorations/DecoIterator.ts
|
|
459
|
+
import { Attributes as Attributes3, Elt as Elt4, Leaf } from "wordgard/doc";
|
|
460
|
+
import { Decoration as Decoration2, Widget as Widget3 } from "wordgard/editor";
|
|
461
|
+
import { PointSet, RangeSet } from "wordgard/set";
|
|
462
|
+
|
|
463
|
+
// src/decorations/widgetType.ts
|
|
464
|
+
import { Attributes as Attributes2, Elt as Elt3 } from "wordgard/doc";
|
|
465
|
+
import { Widget as Widget2 } from "wordgard/editor";
|
|
466
|
+
|
|
467
|
+
// src/decorations/shapeType.ts
|
|
468
|
+
import { Attributes, Elt as Elt2 } from "wordgard/doc";
|
|
469
|
+
|
|
470
|
+
// src/viewRoots.ts
|
|
471
|
+
var viewRoots = /* @__PURE__ */ new WeakSet();
|
|
472
|
+
var viewRoot = (dom) => {
|
|
473
|
+
if (dom) viewRoots.add(dom);
|
|
474
|
+
};
|
|
475
|
+
var drawnRoots = /* @__PURE__ */ new WeakSet();
|
|
476
|
+
var drawnRoot = (dom) => {
|
|
477
|
+
if (dom) drawnRoots.add(dom);
|
|
478
|
+
};
|
|
479
|
+
var isDrawnRoot = (dom) => dom instanceof Element && drawnRoots.has(dom);
|
|
480
|
+
|
|
481
|
+
// src/decorations/replacedRoots.ts
|
|
482
|
+
var tiledRoots = /* @__PURE__ */ new WeakMap();
|
|
483
|
+
var replacements = /* @__PURE__ */ new WeakMap();
|
|
484
|
+
var rootReplaced = (old, root) => {
|
|
485
|
+
if (old == root) return;
|
|
486
|
+
const tiled = old.wgTile ? old : tiledRoots.get(old);
|
|
487
|
+
if (!tiled) return;
|
|
488
|
+
tiledRoots.set(root, tiled);
|
|
489
|
+
replacements.set(tiled, root);
|
|
490
|
+
};
|
|
491
|
+
var currentRoot = (dom) => replacements.get(dom) ?? dom;
|
|
492
|
+
var tiledRoot = (root) => root.wgTile ? root : tiledRoots.get(root) ?? root;
|
|
493
|
+
var isReplaced = (node) => {
|
|
494
|
+
if (!(node instanceof Element)) return false;
|
|
495
|
+
const latest = replacements.get(tiledRoot(node));
|
|
496
|
+
return !!latest && latest != node;
|
|
497
|
+
};
|
|
498
|
+
var swapsRoots = (record) => record.type == "childList" && record.addedNodes.length + record.removedNodes.length > 0 && // Not one rebuilt since, moved later.
|
|
499
|
+
[...record.addedNodes].every(
|
|
500
|
+
(node) => node instanceof Element && tiledRoots.has(node) && !node.wgTile
|
|
501
|
+
) && [...record.removedNodes].every(isReplaced);
|
|
502
|
+
|
|
503
|
+
// src/decorations/shapeType.ts
|
|
504
|
+
var reserved = "data-wg-component";
|
|
505
|
+
var shapeIds = /* @__PURE__ */ new Set();
|
|
506
|
+
var defineShape = () => {
|
|
507
|
+
const id = String(shapeIds.size + 1);
|
|
508
|
+
shapeIds.add(id);
|
|
509
|
+
const shapeElt = (element, attrs, atom) => Elt2.create(element, Attributes.merge(attrs, [reserved, id]), atom ? [] : [0]);
|
|
510
|
+
const forTag = (tag) => {
|
|
511
|
+
const { shape: shape2 } = tag.type;
|
|
512
|
+
const outer = shape2.create(tag.param);
|
|
513
|
+
return shapeElt(outer.tagName, outer.attrs, shape2.atom);
|
|
514
|
+
};
|
|
515
|
+
const shape = Object.assign(forTag, {
|
|
516
|
+
of: (element, { atom = false } = {}) => shapeElt(element, [], atom)
|
|
517
|
+
});
|
|
518
|
+
return { id, shape };
|
|
519
|
+
};
|
|
520
|
+
var idOf = (elt) => Attributes.get(elt.attrs, reserved);
|
|
521
|
+
var isComponentShape = (elt) => {
|
|
522
|
+
const id = idOf(elt);
|
|
523
|
+
return id != null && shapeIds.has(id);
|
|
524
|
+
};
|
|
525
|
+
var shapeAttributes = (elt) => {
|
|
526
|
+
const attrs = [];
|
|
527
|
+
for (let i = 0; i + 1 < elt.attrs.length; i += 2) {
|
|
528
|
+
const name = elt.attrs[i];
|
|
529
|
+
const value = elt.attrs[i + 1];
|
|
530
|
+
if (name !== void 0 && value !== void 0 && name != reserved) attrs.push(name, value);
|
|
531
|
+
}
|
|
532
|
+
return attrs;
|
|
533
|
+
};
|
|
534
|
+
var roots2 = /* @__PURE__ */ new WeakMap();
|
|
535
|
+
var contents = /* @__PURE__ */ new WeakMap();
|
|
536
|
+
var wrapped = /* @__PURE__ */ new WeakSet();
|
|
537
|
+
var everContent = /* @__PURE__ */ new WeakSet();
|
|
538
|
+
var formerContents = /* @__PURE__ */ new WeakSet();
|
|
539
|
+
var lastContents = /* @__PURE__ */ new WeakMap();
|
|
540
|
+
var shapeOfRoot = (dom) => roots2.get(dom);
|
|
541
|
+
var contentOf = (handle) => handle.content ?? (handle.wrapper || handle.atom ? null : handle.root);
|
|
542
|
+
var lastRoots = /* @__PURE__ */ new WeakMap();
|
|
543
|
+
var rootRef = (handle) => (dom) => {
|
|
544
|
+
if (handle.root) roots2.delete(handle.root);
|
|
545
|
+
handle.root = dom;
|
|
546
|
+
if (!dom) return;
|
|
547
|
+
roots2.set(dom, handle);
|
|
548
|
+
const last = lastRoots.get(handle);
|
|
549
|
+
if (last) rootReplaced(last, dom);
|
|
550
|
+
lastRoots.set(handle, dom);
|
|
551
|
+
};
|
|
552
|
+
var atomRef = (handle, atom) => () => {
|
|
553
|
+
handle.atom = atom;
|
|
554
|
+
};
|
|
555
|
+
var createShapeHandle = () => {
|
|
556
|
+
const handle = { root: null, content: null, wrapper: false, atom: false };
|
|
557
|
+
const contentDOMRef = (dom) => {
|
|
558
|
+
if (handle.content) contents.delete(handle.content);
|
|
559
|
+
handle.content = dom;
|
|
560
|
+
if (!dom) return;
|
|
561
|
+
contents.set(dom, handle);
|
|
562
|
+
everContent.add(dom);
|
|
563
|
+
const last = lastContents.get(handle);
|
|
564
|
+
if (last && last != dom) formerContents.add(last);
|
|
565
|
+
formerContents.delete(dom);
|
|
566
|
+
lastContents.set(handle, dom);
|
|
567
|
+
};
|
|
568
|
+
return { handle, ref: rootRef(handle), contentDOMRef };
|
|
569
|
+
};
|
|
570
|
+
var createWrapperHandle = () => {
|
|
571
|
+
const handle = { root: null, content: null, wrapper: true, atom: false };
|
|
572
|
+
const wrappedRef = (dom) => {
|
|
573
|
+
if (handle.content) wrapped.delete(handle.content);
|
|
574
|
+
handle.content = dom;
|
|
575
|
+
if (dom) wrapped.add(dom);
|
|
576
|
+
};
|
|
577
|
+
return { handle, ref: rootRef(handle), wrappedRef };
|
|
578
|
+
};
|
|
579
|
+
var nodePos = (handle, name) => () => {
|
|
580
|
+
let tile = handle.root?.isConnected ? handle.root.wgTile : void 0;
|
|
581
|
+
while (tile && !tile.tag) tile = tile.parent ?? void 0;
|
|
582
|
+
if (!tile) throw new Error(`This ${name} shape or wrapper isn't drawn`);
|
|
583
|
+
return tile.posBefore;
|
|
584
|
+
};
|
|
585
|
+
var shapeAround = (node) => {
|
|
586
|
+
for (let cur = node; cur; cur = cur.parentNode) {
|
|
587
|
+
if (!(cur instanceof Element)) continue;
|
|
588
|
+
if (contents.has(cur)) return null;
|
|
589
|
+
const root = roots2.get(cur);
|
|
590
|
+
if (root) return root.content || root.atom ? root : null;
|
|
591
|
+
if (wrapped.has(cur)) return null;
|
|
592
|
+
}
|
|
593
|
+
return null;
|
|
594
|
+
};
|
|
595
|
+
var contentShape = (dom) => {
|
|
596
|
+
const handle = contents.get(dom) ?? roots2.get(dom);
|
|
597
|
+
return handle && contentOf(handle) == dom ? handle : null;
|
|
598
|
+
};
|
|
599
|
+
var shapeOf = (node) => {
|
|
600
|
+
for (let cur = node; cur; cur = cur.parentNode) {
|
|
601
|
+
const handle = cur instanceof Element ? roots2.get(cur) : void 0;
|
|
602
|
+
if (handle) return handle;
|
|
603
|
+
}
|
|
604
|
+
return null;
|
|
605
|
+
};
|
|
606
|
+
var movesContent = (record) => record.target instanceof Element && formerContents.has(record.target) || [...record.addedNodes, ...record.removedNodes].some(
|
|
607
|
+
(node) => node instanceof Element && everContent.has(node)
|
|
608
|
+
);
|
|
609
|
+
var sortRecords = (records, changed, inline) => {
|
|
610
|
+
const moved = /* @__PURE__ */ new Set();
|
|
611
|
+
for (const record of records)
|
|
612
|
+
if (record.type == "childList" && movesContent(record)) {
|
|
613
|
+
const handle = shapeOf(record.target);
|
|
614
|
+
if (handle) moved.add(handle);
|
|
615
|
+
}
|
|
616
|
+
const keeps = (record) => {
|
|
617
|
+
const { target } = record;
|
|
618
|
+
if (swapsRoots(record)) {
|
|
619
|
+
for (const node of record.addedNodes) if (node instanceof Element) changed.add(node);
|
|
620
|
+
return false;
|
|
621
|
+
}
|
|
622
|
+
if (record.type == "attributes") {
|
|
623
|
+
if (!(target instanceof Element)) return true;
|
|
624
|
+
return !contents.has(target) && !roots2.has(target) && !isDrawnRoot(target) && !shapeAround(target);
|
|
625
|
+
}
|
|
626
|
+
const own = record.type == "childList" && target instanceof Element && contentShape(target);
|
|
627
|
+
if (own) {
|
|
628
|
+
if (own.root && inline(own.root) && !moved.has(own)) return true;
|
|
629
|
+
if (own.root) changed.add(own.root);
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
const handle = shapeAround(target);
|
|
633
|
+
if (!handle) return true;
|
|
634
|
+
const content = contentOf(handle) ?? (handle.atom ? handle.root : null);
|
|
635
|
+
if (record.type == "childList" && handle.root && content && target instanceof Element && target.contains(content))
|
|
636
|
+
changed.add(handle.root);
|
|
637
|
+
return false;
|
|
638
|
+
};
|
|
639
|
+
return records.filter((record) => keeps(record) && !ignoresMutation(record));
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// src/decorations/widgetType.ts
|
|
643
|
+
var drawn = /* @__PURE__ */ new WeakSet();
|
|
644
|
+
var defineWidgetType = ({
|
|
645
|
+
propagateEvent,
|
|
646
|
+
...spec
|
|
647
|
+
}) => {
|
|
648
|
+
const type = Widget2.define({
|
|
649
|
+
...spec,
|
|
650
|
+
propagateEvent: (event) => !stopsEvent(event) && (typeof propagateEvent == "function" ? propagateEvent(event) : propagateEvent !== false)
|
|
651
|
+
});
|
|
652
|
+
drawn.add(type);
|
|
653
|
+
return type;
|
|
654
|
+
};
|
|
655
|
+
var isComponentWidget = (widget) => drawn.has(widget.type);
|
|
656
|
+
var attributed = "widget";
|
|
657
|
+
var attributeWidget = (widget, attrs, tagName) => Elt3.create(
|
|
658
|
+
tagName,
|
|
659
|
+
isComponentWidget(widget) ? Attributes2.merge(attrs, [reserved, attributed]) : attrs,
|
|
660
|
+
[widget]
|
|
661
|
+
);
|
|
662
|
+
var attributedWidget = (elt) => {
|
|
663
|
+
const [widget] = elt.children;
|
|
664
|
+
return Attributes2.get(elt.attrs, reserved) == attributed && widget instanceof Widget2 ? widget : null;
|
|
665
|
+
};
|
|
666
|
+
var widgetRoots = /* @__PURE__ */ new WeakSet();
|
|
667
|
+
var widgetRoot = (dom) => {
|
|
668
|
+
if (dom) widgetRoots.add(dom);
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
// src/decorations/DecoIterator.ts
|
|
672
|
+
var getDecoSet = (state) => {
|
|
673
|
+
const set = { points: /* @__PURE__ */ new Map(), ranges: /* @__PURE__ */ new Map() };
|
|
674
|
+
for (const src of state.facet(Decoration2.Point.source.reader)) set.points.set(src, src(state));
|
|
675
|
+
for (const src of state.facet(Decoration2.Range.source.reader)) set.ranges.set(src, src(state));
|
|
676
|
+
return set;
|
|
677
|
+
};
|
|
678
|
+
var memo = (f) => {
|
|
679
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
680
|
+
return (arg) => {
|
|
681
|
+
let found = map.get(arg);
|
|
682
|
+
if (found === void 0) map.set(arg, found = f(arg));
|
|
683
|
+
return found;
|
|
684
|
+
};
|
|
685
|
+
};
|
|
686
|
+
var addMarkAttributes = (overrides, shape, tag) => {
|
|
687
|
+
let attrs;
|
|
688
|
+
for (const mark of tag.marks) {
|
|
689
|
+
const type = overrides.get(mark.type) || mark.type;
|
|
690
|
+
if (type.attribute && (mark.type.spanning || !tag.isText)) {
|
|
691
|
+
const { get, target } = type.attribute;
|
|
692
|
+
const markAttrs = get(mark.value);
|
|
693
|
+
if (markAttrs.length) {
|
|
694
|
+
if (target && shape instanceof Elt4) shape = shape.addAttrs(markAttrs, target);
|
|
695
|
+
else attrs = attrs ? Attributes3.merge(attrs, markAttrs) : markAttrs;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return attrs ? addAttrs(shape, attrs, tag.type.isInline) : shape;
|
|
700
|
+
};
|
|
701
|
+
var addAttrs = (shape, attrs, inline) => shape instanceof Elt4 ? shape.addAttrs(attrs) : attributeWidget(shape, attrs, inline ? "span" : "div");
|
|
702
|
+
var applyDeco = (shape, deco, tag) => {
|
|
703
|
+
const { AttributeDecoration, WrapperDecoration } = getDecorationInternals();
|
|
704
|
+
if (deco instanceof AttributeDecoration) {
|
|
705
|
+
return deco.selector && shape instanceof Elt4 ? shape.addAttrs(deco.attrs, deco.selector) : addAttrs(shape, deco.attrs, tag.type.isInline);
|
|
706
|
+
} else if (deco instanceof WrapperDecoration) {
|
|
707
|
+
return deco.selector && shape instanceof Elt4 ? shape.wrap(deco.elt, deco.selector) : deco.elt.fill([shape]);
|
|
708
|
+
}
|
|
709
|
+
return shape;
|
|
710
|
+
};
|
|
711
|
+
var baseTagShape = memo(
|
|
712
|
+
(tag) => tag.is(Leaf.Text) ? Widget3.editableText.of(tag.param) : tag.type.shape.create(tag.param)
|
|
713
|
+
);
|
|
714
|
+
var SpanIterator = class {
|
|
715
|
+
constructor(ranges, points, start, end) {
|
|
716
|
+
this.ranges = ranges;
|
|
717
|
+
this.points = points;
|
|
718
|
+
this.end = end;
|
|
719
|
+
this.from = this.to = start;
|
|
720
|
+
}
|
|
721
|
+
ranges;
|
|
722
|
+
points;
|
|
723
|
+
end;
|
|
724
|
+
active = [];
|
|
725
|
+
activeEnd = [];
|
|
726
|
+
from;
|
|
727
|
+
to;
|
|
728
|
+
point = null;
|
|
729
|
+
pointSource = null;
|
|
730
|
+
done = false;
|
|
731
|
+
next() {
|
|
732
|
+
if (this.done) return this;
|
|
733
|
+
if (this.point) this.point = null;
|
|
734
|
+
const { ranges, points, active, activeEnd } = this;
|
|
735
|
+
for (; ; ) {
|
|
736
|
+
const [startPos, startSide] = ranges.value ? [ranges.from, ranges.value.inclusiveStart ? -1 : 1] : [1e9, 0];
|
|
737
|
+
let endPos = 1e9;
|
|
738
|
+
let endSide = 0;
|
|
739
|
+
let nextActive = -1;
|
|
740
|
+
for (let i = 0; i < active.length; i++) {
|
|
741
|
+
const pos = activeEnd[i] ?? 0;
|
|
742
|
+
const side = active[i]?.inclusiveEnd ? 1 : -1;
|
|
743
|
+
if ((pos - endPos || side - endSide) < 0) {
|
|
744
|
+
endPos = pos;
|
|
745
|
+
endSide = side;
|
|
746
|
+
nextActive = i;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
const [pointPos, pointSide2] = points.value ? [points.from, points.value.side] : [1e9, 1];
|
|
750
|
+
const nextPos = Math.min(startPos, endPos, pointPos);
|
|
751
|
+
if (this.to == this.end && nextPos > this.to) {
|
|
752
|
+
this.done = true;
|
|
753
|
+
break;
|
|
754
|
+
} else if (nextPos > this.to) {
|
|
755
|
+
this.from = this.to;
|
|
756
|
+
this.to = Math.min(this.end, nextPos);
|
|
757
|
+
break;
|
|
758
|
+
} else if (pointPos == nextPos && (startPos > pointPos || pointSide2 < 0) && (endPos > pointPos || pointSide2 < 0)) {
|
|
759
|
+
this.point = points.value;
|
|
760
|
+
this.pointSource = points.set;
|
|
761
|
+
this.from = this.to = points.from;
|
|
762
|
+
points.next();
|
|
763
|
+
break;
|
|
764
|
+
} else if ((startPos - endPos || startSide - endSide) < 0) {
|
|
765
|
+
if (ranges.value) active.push(ranges.value);
|
|
766
|
+
activeEnd.push(ranges.to);
|
|
767
|
+
ranges.next();
|
|
768
|
+
} else {
|
|
769
|
+
active.splice(nextActive, 1);
|
|
770
|
+
activeEnd.splice(nextActive, 1);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
var tagScope = (tag, atom) => 4 /* All */ | (atom ? 1 /* Atom */ | (tag.type.isInline ? 2 /* InlineAtom */ : 0) : 0);
|
|
777
|
+
var renderMarkWrapper = memo((mark) => {
|
|
778
|
+
const shape = mark.type.element;
|
|
779
|
+
if (!shape) throw new Error(`${mark.name} has no element shape`);
|
|
780
|
+
return Elt4.create(shape.name, shape.attrs(mark.value), [0]);
|
|
781
|
+
});
|
|
782
|
+
var documentMarks = /* @__PURE__ */ new WeakMap();
|
|
783
|
+
var documentMark = (mark) => documentMarks.get(mark) ?? mark;
|
|
784
|
+
var renderWrapper = (src) => src instanceof getDecorationInternals().WrapperRangeDecoration ? src.elt : renderMarkWrapper(src);
|
|
785
|
+
var compareSetPrec = (setA, setB, array) => {
|
|
786
|
+
if (setA != setB)
|
|
787
|
+
for (const set of array) {
|
|
788
|
+
if (set == setA) return -1;
|
|
789
|
+
if (set == setB) return 1;
|
|
790
|
+
}
|
|
791
|
+
return 0;
|
|
792
|
+
};
|
|
793
|
+
var pastPlotAt = (pos, target) => {
|
|
794
|
+
const { content } = pos.parent.node;
|
|
795
|
+
let start = pos.pos - pos.inText;
|
|
796
|
+
for (let i = pos.index; i < content.length; i++) {
|
|
797
|
+
const child = content[i];
|
|
798
|
+
if (!child) break;
|
|
799
|
+
const end = start + child.length;
|
|
800
|
+
if (target < end) return child.isPlot && target > start ? end : target;
|
|
801
|
+
start = end;
|
|
802
|
+
}
|
|
803
|
+
return target;
|
|
804
|
+
};
|
|
805
|
+
var none = [];
|
|
806
|
+
var DecoIterator = class {
|
|
807
|
+
constructor(state, decoSet) {
|
|
808
|
+
this.state = state;
|
|
809
|
+
this.decoSet = decoSet;
|
|
810
|
+
const { tagShape, tagWidget, tagWrapper, tagAttribute, markShape } = getDecorationInternals();
|
|
811
|
+
this.tagShapes = state.facet(tagShape.reader);
|
|
812
|
+
this.globalWidgets = state.facet(tagWidget.reader);
|
|
813
|
+
this.globalWrappers = state.facet(tagWrapper.reader);
|
|
814
|
+
this.globalAttrs = state.facet(tagAttribute.reader);
|
|
815
|
+
this.markShapes = state.facet(markShape.reader);
|
|
816
|
+
this.pos = state.doc.resolve(0);
|
|
817
|
+
this.rangeCursor = RangeSet.cursor([...decoSet.ranges.values()]);
|
|
818
|
+
this.pointSets = [...decoSet.points.values()];
|
|
819
|
+
this.pointCursor = PointSet.cursor(this.pointSets);
|
|
820
|
+
}
|
|
821
|
+
state;
|
|
822
|
+
decoSet;
|
|
823
|
+
tagShapes;
|
|
824
|
+
globalWidgets;
|
|
825
|
+
globalWrappers;
|
|
826
|
+
globalAttrs;
|
|
827
|
+
markShapes;
|
|
828
|
+
pos;
|
|
829
|
+
rangeCursor;
|
|
830
|
+
pointSets;
|
|
831
|
+
pointCursor;
|
|
832
|
+
widgets(tag, place, walker) {
|
|
833
|
+
if (place == 2 /* Start */ && tag.type.isInline) walker.widget(Widget3.img, -1, false);
|
|
834
|
+
for (const src of this.globalWidgets) {
|
|
835
|
+
if (src.place == place && tag.type == src.type) {
|
|
836
|
+
const widget = typeof src.widget == "function" ? src.widget(tag) : src.widget;
|
|
837
|
+
if (widget)
|
|
838
|
+
walker.widget(
|
|
839
|
+
widget,
|
|
840
|
+
place == 0 /* Before */ || place == 3 /* End */ ? 1 : -1,
|
|
841
|
+
false
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (place == 3 /* End */ && tag.type.isInline) walker.widget(Widget3.img, 1, false);
|
|
846
|
+
}
|
|
847
|
+
// Enumerate all wrapper elements for a given node. Spanning wrappers
|
|
848
|
+
// are always moved to the front of the result. Within the
|
|
849
|
+
// spanning/non-spanning wrappers, the ordering is determined by rank.
|
|
850
|
+
nodeWrappers(tag, active, atom) {
|
|
851
|
+
const { WrapperRangeDecoration } = getDecorationInternals();
|
|
852
|
+
const wrappers = [];
|
|
853
|
+
for (const mark of tag.marks) {
|
|
854
|
+
const shape = this.markShapes.get(mark.type) || mark.type;
|
|
855
|
+
if (shape.element) {
|
|
856
|
+
const drawn2 = shape == mark.type ? mark : shape.of(mark.value);
|
|
857
|
+
if (drawn2 != mark) documentMarks.set(drawn2, mark);
|
|
858
|
+
wrappers.push(drawn2);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
for (const val of active) {
|
|
862
|
+
if (val instanceof WrapperRangeDecoration && tagScope(tag, atom) & val.scope && (!val.query || this.state.schema.matchNode(tag.type, val.query)))
|
|
863
|
+
wrappers.push(val);
|
|
864
|
+
}
|
|
865
|
+
if (!wrappers.length) return none;
|
|
866
|
+
if (wrappers.length > 1)
|
|
867
|
+
wrappers.sort(
|
|
868
|
+
(a, b) => (spanningOf(a) == spanningOf(b) ? 0 : spanningOf(a) ? -1 : 1) || rankOf(a) - rankOf(b)
|
|
869
|
+
);
|
|
870
|
+
return wrappers;
|
|
871
|
+
}
|
|
872
|
+
walk(from, inclusiveStart, to, walker) {
|
|
873
|
+
const { WidgetDecoration, ShapeDecoration } = getDecorationInternals();
|
|
874
|
+
this.rangeCursor.goto(from);
|
|
875
|
+
this.pointCursor.goto(from, inclusiveStart ? -1e9 : 1e9 /* After */);
|
|
876
|
+
const iter = new SpanIterator(this.rangeCursor, this.pointCursor, from, to);
|
|
877
|
+
let pos = from >= this.pos.pos ? this.pos.advance(from - this.pos.pos) : this.state.doc.resolve(from);
|
|
878
|
+
let started = inclusiveStart;
|
|
879
|
+
let atomParent;
|
|
880
|
+
for (let p = pos.parent; p; p = p.parent)
|
|
881
|
+
if (!p.node.isDoc && (this.state.isAtom(p.node.type) || this.hasAtomShape(p.before)))
|
|
882
|
+
atomParent = p;
|
|
883
|
+
const pendingDeco = [];
|
|
884
|
+
let pendingPos = -1;
|
|
885
|
+
let pendingShape = null;
|
|
886
|
+
let pendingShapeSet = null;
|
|
887
|
+
let skipped = null;
|
|
888
|
+
const flushSkipped = () => {
|
|
889
|
+
if (!skipped) return;
|
|
890
|
+
const tag = skipped;
|
|
891
|
+
skipped = null;
|
|
892
|
+
this.widgets(tag, 1 /* After */, walker);
|
|
893
|
+
};
|
|
894
|
+
const wrap = {
|
|
895
|
+
skip: (node, pos2) => {
|
|
896
|
+
flushSkipped();
|
|
897
|
+
if (started) this.widgets(node.tag, 0 /* Before */, walker);
|
|
898
|
+
else started = true;
|
|
899
|
+
const hasPending = pendingPos == pos2 && !node.isText;
|
|
900
|
+
let shape = this.tagShape(node.tag, iter.active, hasPending ? pendingShape : null);
|
|
901
|
+
if (hasPending) for (const deco of pendingDeco) shape = applyDeco(shape, deco, node.tag);
|
|
902
|
+
if (shape.hasContent) throw new Error("Leaf nodes shapes shouldn't have a content hole");
|
|
903
|
+
walker.node(node, shape, this.nodeWrappers(node.tag, iter.active, true), void 0, pos2);
|
|
904
|
+
this.widgets(node.tag, 1 /* After */, walker);
|
|
905
|
+
},
|
|
906
|
+
enterPlot: (node, pos2) => {
|
|
907
|
+
flushSkipped();
|
|
908
|
+
if (started) this.widgets(node.tag, 0 /* Before */, walker);
|
|
909
|
+
else started = true;
|
|
910
|
+
let shape = this.tagShape(node.tag, iter.active, pendingPos == pos2 ? pendingShape : null);
|
|
911
|
+
if (pendingPos == pos2)
|
|
912
|
+
for (const deco of pendingDeco) shape = applyDeco(shape, deco, node.tag);
|
|
913
|
+
const wrappers = this.nodeWrappers(node.tag, iter.active, !shape.hasContent);
|
|
914
|
+
const atom = !shape.hasContent;
|
|
915
|
+
if (atom) {
|
|
916
|
+
walker.node(node, shape, wrappers, pos2 + node.length > to ? to - pos2 : void 0, pos2);
|
|
917
|
+
} else {
|
|
918
|
+
if (shape instanceof Elt4 && !walker.enter(node, shape, wrappers, pos2)) {
|
|
919
|
+
skipped = node.tag;
|
|
920
|
+
return false;
|
|
921
|
+
}
|
|
922
|
+
this.widgets(node.tag, 2 /* Start */, walker);
|
|
923
|
+
}
|
|
924
|
+
return !atom;
|
|
925
|
+
},
|
|
926
|
+
leavePlot: (tag) => {
|
|
927
|
+
flushSkipped();
|
|
928
|
+
if (started && !atomParent) this.widgets(tag, 3 /* End */, walker);
|
|
929
|
+
else started = true;
|
|
930
|
+
walker.leave();
|
|
931
|
+
this.widgets(tag, 1 /* After */, walker);
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
if (inclusiveStart && !atomParent) {
|
|
935
|
+
const before = pos.nodeBefore;
|
|
936
|
+
if (before) this.widgets(before.tag, 1 /* After */, walker);
|
|
937
|
+
else this.widgets(pos.parent.node.tag, 2 /* Start */, walker);
|
|
938
|
+
}
|
|
939
|
+
for (; !iter.next().done; ) {
|
|
940
|
+
if (atomParent) {
|
|
941
|
+
const end = Math.min(to, atomParent.after);
|
|
942
|
+
const done = atomParent.after <= to;
|
|
943
|
+
walker.nodePart(atomParent.node, end - pos.pos, done);
|
|
944
|
+
pos = pos.advance(end - pos.pos);
|
|
945
|
+
if (done) {
|
|
946
|
+
this.widgets(atomParent.node.tag, 1 /* After */, walker);
|
|
947
|
+
atomParent = void 0;
|
|
948
|
+
}
|
|
949
|
+
while (iter.point && iter.from < end) iter.next();
|
|
950
|
+
} else if (iter.point) {
|
|
951
|
+
if (iter.from < pos.pos) continue;
|
|
952
|
+
flushSkipped();
|
|
953
|
+
const value = iter.point;
|
|
954
|
+
if (value instanceof WidgetDecoration) {
|
|
955
|
+
walker.widget(value.widget, value.side, true);
|
|
956
|
+
} else {
|
|
957
|
+
if (pendingPos < pos.pos) {
|
|
958
|
+
pendingDeco.length = 0;
|
|
959
|
+
pendingShape = null;
|
|
960
|
+
pendingPos = pos.pos;
|
|
961
|
+
}
|
|
962
|
+
if (value instanceof ShapeDecoration && (!pendingShape || pendingShapeSet && iter.pointSource && compareSetPrec(pendingShapeSet, iter.pointSource, this.pointSets))) {
|
|
963
|
+
pendingShape = value.shape;
|
|
964
|
+
pendingShapeSet = iter.pointSource;
|
|
965
|
+
} else {
|
|
966
|
+
pendingDeco.push(value);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
} else if (iter.to > pos.pos) {
|
|
970
|
+
pos = pos.walk(pastPlotAt(pos, iter.to) - pos.pos, wrap);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
if (pos.pos < to) pos = pos.walk(pastPlotAt(pos, to) - pos.pos, wrap);
|
|
974
|
+
flushSkipped();
|
|
975
|
+
if (atomParent) {
|
|
976
|
+
walker.nodePart(atomParent.node, 0, atomParent.after == to);
|
|
977
|
+
} else {
|
|
978
|
+
const after = pos.nodeAfter;
|
|
979
|
+
if (after) this.widgets(after.tag, 0 /* Before */, walker);
|
|
980
|
+
else this.widgets(pos.parent.node.tag, 3 /* End */, walker);
|
|
981
|
+
}
|
|
982
|
+
this.pos = pos;
|
|
983
|
+
}
|
|
984
|
+
tagShape(tag, active, fromPoint) {
|
|
985
|
+
const { AttributeRangeDecoration } = getDecorationInternals();
|
|
986
|
+
let shape = fromPoint ?? this.tagShapes.get(tag.type)?.(tag) ?? baseTagShape(tag);
|
|
987
|
+
if (tag.marks.length) shape = addMarkAttributes(this.markShapes, shape, tag);
|
|
988
|
+
let add;
|
|
989
|
+
for (const src of this.globalAttrs)
|
|
990
|
+
if (tag.type == src.type) {
|
|
991
|
+
if (src.target && shape instanceof Elt4)
|
|
992
|
+
shape = shape.addAttrs([src.attr, src.value(tag)], src.target);
|
|
993
|
+
else Attributes3.push(add ||= [], src.attr, src.value(tag));
|
|
994
|
+
}
|
|
995
|
+
const scope = tagScope(tag, !shape.hasContent);
|
|
996
|
+
for (const { type, elt, target } of this.globalWrappers)
|
|
997
|
+
if (tag.type == type) {
|
|
998
|
+
shape = target && shape instanceof Elt4 ? shape.wrap(elt, target) : elt.fill([shape]);
|
|
999
|
+
}
|
|
1000
|
+
for (const deco of active) {
|
|
1001
|
+
if (deco instanceof AttributeRangeDecoration && scope & deco.scope && (!deco.query || this.state.schema.matchNode(tag.type, deco.query)))
|
|
1002
|
+
Attributes3.push(add ||= [], deco.attribute, deco.value);
|
|
1003
|
+
}
|
|
1004
|
+
if (add) {
|
|
1005
|
+
if (shape instanceof Elt4)
|
|
1006
|
+
shape = Elt4.create(shape.tagName, Attributes3.merge(shape.attrs, add), shape.children);
|
|
1007
|
+
else shape = attributeWidget(shape, add, tag.type.isBlock ? "div" : "span");
|
|
1008
|
+
}
|
|
1009
|
+
return shape;
|
|
1010
|
+
}
|
|
1011
|
+
atomCache = /* @__PURE__ */ new Map();
|
|
1012
|
+
hasAtomShape(pos) {
|
|
1013
|
+
const { ShapeDecoration } = getDecorationInternals();
|
|
1014
|
+
let has = this.atomCache.get(pos);
|
|
1015
|
+
if (has === void 0) {
|
|
1016
|
+
has = this.pointSets.some(
|
|
1017
|
+
(set) => set.at(pos, (deco) => deco instanceof ShapeDecoration && !deco.shape.hasContent)
|
|
1018
|
+
);
|
|
1019
|
+
this.atomCache.set(pos, has);
|
|
1020
|
+
}
|
|
1021
|
+
return has;
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
var rankOf = (src) => src instanceof getDecorationInternals().WrapperRangeDecoration ? src.rank : src.type.rank;
|
|
1025
|
+
var spanningOf = (src) => src instanceof getDecorationInternals().WrapperRangeDecoration ? src.spanning : src.type.spanning;
|
|
1026
|
+
|
|
1027
|
+
// src/decorations/markType.ts
|
|
1028
|
+
var markIds = /* @__PURE__ */ new Set();
|
|
1029
|
+
var defineMarkShape = () => {
|
|
1030
|
+
const id = `m${markIds.size + 1}`;
|
|
1031
|
+
markIds.add(id);
|
|
1032
|
+
const shapes2 = /* @__PURE__ */ new Map();
|
|
1033
|
+
const shape = (element) => {
|
|
1034
|
+
let found = shapes2.get(element);
|
|
1035
|
+
if (!found) {
|
|
1036
|
+
found = { element, attributes: { [reserved]: id } };
|
|
1037
|
+
shapes2.set(element, found);
|
|
1038
|
+
}
|
|
1039
|
+
return found;
|
|
1040
|
+
};
|
|
1041
|
+
return { id, shape };
|
|
1042
|
+
};
|
|
1043
|
+
var isComponentMark = (elt) => {
|
|
1044
|
+
const id = idOf(elt);
|
|
1045
|
+
return id != null && markIds.has(id);
|
|
1046
|
+
};
|
|
1047
|
+
var markTiles = /* @__PURE__ */ new WeakSet();
|
|
1048
|
+
var markOfTile = /* @__PURE__ */ new WeakMap();
|
|
1049
|
+
|
|
1050
|
+
// src/decorations/wrapperType.ts
|
|
1051
|
+
import { Elt as Elt5 } from "wordgard/doc";
|
|
1052
|
+
var wrapperIds = /* @__PURE__ */ new Set();
|
|
1053
|
+
var defineWrapper = () => {
|
|
1054
|
+
const id = `w${wrapperIds.size + 1}`;
|
|
1055
|
+
wrapperIds.add(id);
|
|
1056
|
+
const elts = /* @__PURE__ */ new Map();
|
|
1057
|
+
const elt = (element) => {
|
|
1058
|
+
let found = elts.get(element);
|
|
1059
|
+
if (!found) {
|
|
1060
|
+
found = Elt5.mk(element, { [reserved]: id }, [0]);
|
|
1061
|
+
elts.set(element, found);
|
|
1062
|
+
}
|
|
1063
|
+
return found;
|
|
1064
|
+
};
|
|
1065
|
+
return { id, elt };
|
|
1066
|
+
};
|
|
1067
|
+
var isComponentWrapper = (elt) => {
|
|
1068
|
+
const id = idOf(elt);
|
|
1069
|
+
return id != null && wrapperIds.has(id);
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
// src/decorations/iterDeco.ts
|
|
1073
|
+
var cursorWrappers = (marks) => marks.filter((mark) => mark.type.element).map((mark) => wrapperView(renderMarkWrapper(mark), mark.type.spanning, mark));
|
|
1074
|
+
var wrapperView = (elt, spanning, mark) => mark && isComponentMark(elt) ? { elt, spanning, mark: documentMark(mark) } : { elt, spanning };
|
|
1075
|
+
var sameMark = (a, b) => a == b || !!a && !!b && a.eq(b);
|
|
1076
|
+
var VOID = /^(br|hr|img|input|wbr)$/i;
|
|
1077
|
+
var uneditable = (shape, inEditable) => {
|
|
1078
|
+
if (!inEditable || shape.hasContent) return { shape, inEditable, flag: 0 };
|
|
1079
|
+
const attr = Attributes4.get(shape.attrs, "contenteditable");
|
|
1080
|
+
let flag = attr == "false" ? TileFlag.Uneditable : 0;
|
|
1081
|
+
if (attr == null && !VOID.test(shape.tagName)) {
|
|
1082
|
+
shape = Elt6.create(
|
|
1083
|
+
shape.tagName,
|
|
1084
|
+
Attributes4.merge(shape.attrs, ["contenteditable", "false"]),
|
|
1085
|
+
shape.children
|
|
1086
|
+
);
|
|
1087
|
+
flag = TileFlag.Uneditable;
|
|
1088
|
+
}
|
|
1089
|
+
return { shape, inEditable: false, flag };
|
|
1090
|
+
};
|
|
1091
|
+
var sameNodeView = (a, b) => a == b || a.node == b.node && a.children == b.children && a.shape.eq(b.shape);
|
|
1092
|
+
var sameView = (view, other) => {
|
|
1093
|
+
if (view == other) return true;
|
|
1094
|
+
switch (view.type) {
|
|
1095
|
+
case "node":
|
|
1096
|
+
return other.type == "node" && view.key == other.key && sameNodeView(view, other);
|
|
1097
|
+
case "text":
|
|
1098
|
+
return other.type == "text" && view.text == other.text;
|
|
1099
|
+
case "wrapper":
|
|
1100
|
+
return other.type == "wrapper" && view.key == other.key && view.spanning == other.spanning && view.elt.eq(other.elt) && sameMark(view.mark, other.mark) && view.children.length == other.children.length && view.children.every((child, i) => {
|
|
1101
|
+
const match = other.children[i];
|
|
1102
|
+
return !!match && sameView(child, match);
|
|
1103
|
+
});
|
|
1104
|
+
case "buffer":
|
|
1105
|
+
return other.type == "buffer" && view.side == other.side;
|
|
1106
|
+
case "widget":
|
|
1107
|
+
return other.type == "widget" && view.side == other.side && view.key == other.key && view.offset == other.offset && view.widget.eq(other.widget);
|
|
1108
|
+
case "composition":
|
|
1109
|
+
return other.type == "composition" && view.key == other.key && view.rendered == other.rendered;
|
|
1110
|
+
case "trailingHack":
|
|
1111
|
+
return other.type == "trailingHack";
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
var shapes = /* @__PURE__ */ new WeakMap();
|
|
1115
|
+
var shapeOf2 = (views) => shapes.get(views) ?? views;
|
|
1116
|
+
var keepSame = (store, before, after, head = 0, tail = 0) => {
|
|
1117
|
+
const whole = !head && !tail;
|
|
1118
|
+
const old = new Map(
|
|
1119
|
+
childViewKeys(whole ? before : before.slice(head, before.length - tail)).map(
|
|
1120
|
+
({ view, key }) => [key, view]
|
|
1121
|
+
)
|
|
1122
|
+
);
|
|
1123
|
+
let changed = before.length != after.length;
|
|
1124
|
+
let sameShape = !changed;
|
|
1125
|
+
childViewKeys(whole ? after : after.slice(head, after.length - tail)).forEach(
|
|
1126
|
+
({ view, key }, at) => {
|
|
1127
|
+
const i = head + at;
|
|
1128
|
+
const match = old.get(key);
|
|
1129
|
+
let found = view;
|
|
1130
|
+
if (match && sameView(match, view)) found = match;
|
|
1131
|
+
else if (match?.type == "wrapper" && view.type == "wrapper" && match.elt.eq(view.elt) && sameMark(match.mark, view.mark))
|
|
1132
|
+
keepSame(store, match.children, view.children);
|
|
1133
|
+
if (match?.type == "node" && found.type == "node") {
|
|
1134
|
+
moveCell(store, match, found);
|
|
1135
|
+
if (isNative(match) != isNative(found)) sameShape = false;
|
|
1136
|
+
}
|
|
1137
|
+
if (found != before[i]) {
|
|
1138
|
+
changed = true;
|
|
1139
|
+
if (found.type != "node" || match != before[i]) sameShape = false;
|
|
1140
|
+
}
|
|
1141
|
+
after[i] = found;
|
|
1142
|
+
}
|
|
1143
|
+
);
|
|
1144
|
+
if (!changed) {
|
|
1145
|
+
shapes.set(after, shapeOf2(before));
|
|
1146
|
+
return before;
|
|
1147
|
+
}
|
|
1148
|
+
if (sameShape) shapes.set(after, shapeOf2(before));
|
|
1149
|
+
return after;
|
|
1150
|
+
};
|
|
1151
|
+
var isNative = ({ shape }) => !(shape instanceof Elt6) && !isComponentWidget(shape);
|
|
1152
|
+
var isPoint = (view) => view?.type == "buffer" || view?.type == "widget";
|
|
1153
|
+
var openWrapper = (views, { elt, spanning, mark }) => {
|
|
1154
|
+
let i = views.length - 1;
|
|
1155
|
+
while (isPoint(views[i])) i--;
|
|
1156
|
+
const prev = views[i];
|
|
1157
|
+
if (spanning && prev?.type == "wrapper" && prev.spanning && prev.elt.eq(elt) && sameMark(prev.mark, mark)) {
|
|
1158
|
+
prev.children.push(...views.splice(i + 1));
|
|
1159
|
+
return prev;
|
|
1160
|
+
}
|
|
1161
|
+
const wrapper = { type: "wrapper", elt, spanning, children: [] };
|
|
1162
|
+
if (mark) wrapper.mark = mark;
|
|
1163
|
+
views.push(wrapper);
|
|
1164
|
+
return wrapper;
|
|
1165
|
+
};
|
|
1166
|
+
var addText = (views, text) => {
|
|
1167
|
+
const last = views.at(-1);
|
|
1168
|
+
if (last?.type == "text") last.text += text;
|
|
1169
|
+
else views.push({ type: "text", text });
|
|
1170
|
+
};
|
|
1171
|
+
var needsTrailingHack = (views) => {
|
|
1172
|
+
for (let i = views.length - 1; i >= 0; i--) {
|
|
1173
|
+
const view = views[i];
|
|
1174
|
+
if (!view) continue;
|
|
1175
|
+
if (view.type == "wrapper") {
|
|
1176
|
+
const found = needsTrailingHack(view.children);
|
|
1177
|
+
if (found !== void 0) return found;
|
|
1178
|
+
} else if (view.type == "widget") {
|
|
1179
|
+
if (view.widget.type.inFlow) return false;
|
|
1180
|
+
} else if (view.type == "node") {
|
|
1181
|
+
const { shape, children } = view;
|
|
1182
|
+
if (!(shape instanceof Elt6)) {
|
|
1183
|
+
if (shape.type.inFlow) return false;
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
if (!children) return shape.tagName.toLowerCase() == "br";
|
|
1187
|
+
if (shape.children.includes(0)) {
|
|
1188
|
+
const found = needsTrailingHack(children);
|
|
1189
|
+
if (found !== void 0) return found;
|
|
1190
|
+
}
|
|
1191
|
+
} else {
|
|
1192
|
+
return false;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
return void 0;
|
|
1196
|
+
};
|
|
1197
|
+
var isFlat = (plot, views, head, tail) => {
|
|
1198
|
+
if (plot.isTextblock || plot.isInline || views.length != plot.content.length) return false;
|
|
1199
|
+
for (let i = head; i < views.length - tail; i++) if (views[i]?.type != "node") return false;
|
|
1200
|
+
return true;
|
|
1201
|
+
};
|
|
1202
|
+
var pointKeys = (context, old, oldStart) => {
|
|
1203
|
+
const byPos = /* @__PURE__ */ new Map();
|
|
1204
|
+
if (old && oldStart !== null && context.changes) {
|
|
1205
|
+
const map = pointMapper(context.changes);
|
|
1206
|
+
const collect = (views) => {
|
|
1207
|
+
for (const view of views) {
|
|
1208
|
+
if (view.type == "wrapper") collect(view.children);
|
|
1209
|
+
else if (view.type == "widget" && view.offset !== void 0)
|
|
1210
|
+
for (const pos of map(oldStart + view.offset)) {
|
|
1211
|
+
const found = byPos.get(pos);
|
|
1212
|
+
if (found) found.push(view);
|
|
1213
|
+
else byPos.set(pos, [view]);
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
collect(old.views);
|
|
1218
|
+
}
|
|
1219
|
+
const used = /* @__PURE__ */ new Set();
|
|
1220
|
+
return (pos, widget, side) => {
|
|
1221
|
+
const candidates = (byPos.get(pos) ?? []).filter(
|
|
1222
|
+
(view) => Math.sign(view.side) == Math.sign(side) && view.key && !used.has(view.key)
|
|
1223
|
+
);
|
|
1224
|
+
const found = candidates.find((view) => view.widget.eq(widget)) ?? candidates.find((view) => view.widget.type == widget.type);
|
|
1225
|
+
const key = found?.key ?? createNodeKey();
|
|
1226
|
+
used.add(key);
|
|
1227
|
+
return key;
|
|
1228
|
+
};
|
|
1229
|
+
};
|
|
1230
|
+
var sources = (wrappers) => wrappers.map(
|
|
1231
|
+
(src) => wrapperView(renderWrapper(src), spanningOf(src), src instanceof Mark ? src : void 0)
|
|
1232
|
+
);
|
|
1233
|
+
var iterDeco = (context, plot, start, parent = { start: 0, oldStart: 0 }, fresh = false) => {
|
|
1234
|
+
const end = start + plot.contentLength;
|
|
1235
|
+
const oldStartOf = (layout2) => layout2 && parent.oldStart !== null ? parent.oldStart + layout2.offset : null;
|
|
1236
|
+
const fromLast = (layout2) => fresh || layout2?.pass == context.pass ? void 0 : layout2;
|
|
1237
|
+
const previous = fromLast(context.layouts.get(plot));
|
|
1238
|
+
const previousStart = oldStartOf(previous);
|
|
1239
|
+
if (previous && context.changes && previousStart !== null && samePlace(context.changes, previousStart, start) && !context.composing.has(plot) && isKept(context.changes, start, end)) {
|
|
1240
|
+
previous.offset = start - parent.start;
|
|
1241
|
+
previous.pass = context.pass;
|
|
1242
|
+
return previous.views;
|
|
1243
|
+
}
|
|
1244
|
+
const oldPlot = context.replaced.get(plot) ?? plot;
|
|
1245
|
+
const old = fromLast(context.layouts.get(oldPlot));
|
|
1246
|
+
const oldStart = oldStartOf(old);
|
|
1247
|
+
const nodeKeys = context.store.keys.get(plot);
|
|
1248
|
+
if (!nodeKeys) throw new Error(`No keys for ${plot.name}`);
|
|
1249
|
+
let head = 0;
|
|
1250
|
+
let tail = 0;
|
|
1251
|
+
if (old?.flat && oldStart !== null && context.changes && samePlace(context.changes, oldStart, start) && !context.composing.has(plot))
|
|
1252
|
+
({ head, tail } = unchangedEnds(context.changes, oldPlot, oldStart, plot));
|
|
1253
|
+
const content = plot.content;
|
|
1254
|
+
const middle = content.length - tail;
|
|
1255
|
+
let from = start;
|
|
1256
|
+
for (let i = 0; i < head; i++) from += content[i]?.length ?? 0;
|
|
1257
|
+
let to = end;
|
|
1258
|
+
for (let i = middle; i < content.length; i++) to -= content[i]?.length ?? 0;
|
|
1259
|
+
const views = old && head ? old.views.slice(0, head) : [];
|
|
1260
|
+
const plots = [];
|
|
1261
|
+
let at = start;
|
|
1262
|
+
let keyAt = null;
|
|
1263
|
+
const indexAt = /* @__PURE__ */ new Map();
|
|
1264
|
+
for (let index = head, offset = from; index < middle; index++) {
|
|
1265
|
+
indexAt.set(offset, index);
|
|
1266
|
+
offset += content[index]?.length ?? 0;
|
|
1267
|
+
}
|
|
1268
|
+
const wrapped2 = (wrappers) => {
|
|
1269
|
+
let target = views;
|
|
1270
|
+
for (const view of wrappers) {
|
|
1271
|
+
const wrapper = openWrapper(target, view);
|
|
1272
|
+
if (view.key) wrapper.key = view.key;
|
|
1273
|
+
target = wrapper.children;
|
|
1274
|
+
}
|
|
1275
|
+
return target;
|
|
1276
|
+
};
|
|
1277
|
+
const nodeView = (node, shape, pos) => {
|
|
1278
|
+
const key = nodeKeys[indexAt.get(pos) ?? -1];
|
|
1279
|
+
if (key === void 0) throw new Error(`No key for ${node.name}`);
|
|
1280
|
+
return { type: "node", node, key, shape, children: null };
|
|
1281
|
+
};
|
|
1282
|
+
const walker = {
|
|
1283
|
+
enter: (node, shape, wrappers, pos) => {
|
|
1284
|
+
const view = nodeView(node, shape, pos);
|
|
1285
|
+
wrapped2(sources(wrappers)).push(view);
|
|
1286
|
+
plots.push({ view, plot: node, pos, fresh: fresh || context.atoms.delete(node) });
|
|
1287
|
+
at = pos + node.length;
|
|
1288
|
+
return false;
|
|
1289
|
+
},
|
|
1290
|
+
leave: () => {
|
|
1291
|
+
},
|
|
1292
|
+
node: (node, shape, wrappers, partial, pos) => {
|
|
1293
|
+
if (partial !== void 0)
|
|
1294
|
+
throw new Error(`${context.words.library} can't render part of ${node.name}`);
|
|
1295
|
+
at = pos + node.length;
|
|
1296
|
+
if (node.isPlot) context.atoms.add(node);
|
|
1297
|
+
if (node.is(Leaf2.Text) && !node.param) return;
|
|
1298
|
+
let target = wrapped2(sources(wrappers));
|
|
1299
|
+
if (!node.is(Leaf2.Text)) {
|
|
1300
|
+
target.push(nodeView(node, shape, pos));
|
|
1301
|
+
return;
|
|
1302
|
+
}
|
|
1303
|
+
while (shape instanceof Elt6) {
|
|
1304
|
+
if (isComponentWrapper(shape))
|
|
1305
|
+
throw new Error(`A ${context.words.name} wrapper can't wrap text`);
|
|
1306
|
+
const elt = Elt6.create(shape.tagName, shape.attrs, [0]);
|
|
1307
|
+
target = openWrapper(target, { elt, spanning: true }).children;
|
|
1308
|
+
const [inner] = shape.children;
|
|
1309
|
+
if (inner === void 0 || inner === 0 || typeof inner == "string")
|
|
1310
|
+
throw new Error("Unexpected text shape");
|
|
1311
|
+
shape = inner;
|
|
1312
|
+
}
|
|
1313
|
+
addText(target, node.param);
|
|
1314
|
+
},
|
|
1315
|
+
nodePart: (node) => {
|
|
1316
|
+
throw new Error(`${context.words.library} can't render part of ${node.name}`);
|
|
1317
|
+
},
|
|
1318
|
+
widget: (widget, side, point) => {
|
|
1319
|
+
if (widget == Widget4.img) views.push({ type: "buffer", side: side < 0 ? -1 : 1 });
|
|
1320
|
+
else if (point && isComponentWidget(widget)) {
|
|
1321
|
+
keyAt ??= pointKeys(context, old, oldStart);
|
|
1322
|
+
views.push({
|
|
1323
|
+
type: "widget",
|
|
1324
|
+
widget,
|
|
1325
|
+
side,
|
|
1326
|
+
key: keyAt(at, widget, side),
|
|
1327
|
+
offset: at - start
|
|
1328
|
+
});
|
|
1329
|
+
} else views.push({ type: "widget", widget, side });
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
const { composition } = context;
|
|
1333
|
+
if (composition?.plot == plot) {
|
|
1334
|
+
const from2 = start + composition.layout.from;
|
|
1335
|
+
context.deco.walk(start, true, from2, walker);
|
|
1336
|
+
const target = wrapped2(composition.layout.wrappers);
|
|
1337
|
+
if (composition.layout.cursor) target.push({ type: "buffer", side: -1 });
|
|
1338
|
+
else
|
|
1339
|
+
target.push({
|
|
1340
|
+
type: "composition",
|
|
1341
|
+
key: composition.rendered?.key ?? "composition",
|
|
1342
|
+
rendered: composition.rendered?.text ?? null
|
|
1343
|
+
});
|
|
1344
|
+
at = start + composition.layout.to;
|
|
1345
|
+
context.deco.walk(start + composition.layout.to, false, end, walker);
|
|
1346
|
+
} else {
|
|
1347
|
+
context.deco.walk(from, true, to, walker);
|
|
1348
|
+
}
|
|
1349
|
+
for (const { view, plot: plot2, pos, fresh: fresh2 } of plots)
|
|
1350
|
+
view.children = iterDeco(context, plot2, pos + 1, { start, oldStart }, fresh2);
|
|
1351
|
+
if (old && tail) {
|
|
1352
|
+
views.push(...old.views.slice(old.views.length - tail));
|
|
1353
|
+
for (let i = middle, pos = to; i < content.length; i++) {
|
|
1354
|
+
const child = content[i];
|
|
1355
|
+
if (!child) continue;
|
|
1356
|
+
const layout2 = child.isPlot ? context.layouts.get(child) : void 0;
|
|
1357
|
+
if (layout2) {
|
|
1358
|
+
layout2.offset = pos + 1 - start;
|
|
1359
|
+
layout2.pass = context.pass;
|
|
1360
|
+
}
|
|
1361
|
+
pos += child.length;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
if ((plot.isTextblock || plot.isInline) && needsTrailingHack(views) !== false)
|
|
1365
|
+
views.push({ type: "trailingHack" });
|
|
1366
|
+
const fromHere = old && oldStart !== null && context.changes && samePlace(context.changes, oldStart, start);
|
|
1367
|
+
const laid = old && fromHere ? keepSame(context.store, old.views, views, head, tail) : views;
|
|
1368
|
+
context.layouts.set(plot, {
|
|
1369
|
+
offset: start - parent.start,
|
|
1370
|
+
pass: context.pass,
|
|
1371
|
+
views: laid,
|
|
1372
|
+
flat: isFlat(plot, laid, head, tail)
|
|
1373
|
+
});
|
|
1374
|
+
return laid;
|
|
1375
|
+
};
|
|
1376
|
+
|
|
1377
|
+
// src/decorations/nodeWrapperType.ts
|
|
1378
|
+
import { Leaf as Leaf3, Node as Node2 } from "wordgard/doc";
|
|
1379
|
+
import { GardState as GardState3 } from "wordgard/state";
|
|
1380
|
+
var createNodeWrappers = (entries) => {
|
|
1381
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
1382
|
+
return {
|
|
1383
|
+
entries,
|
|
1384
|
+
of: (type) => {
|
|
1385
|
+
if (resolved.has(type)) return resolved.get(type);
|
|
1386
|
+
const components = entries.filter(
|
|
1387
|
+
(entry) => entry.type ? entry.type == type : type != Leaf3.Text && !!entry.select?.(type)
|
|
1388
|
+
).map((entry) => entry.component);
|
|
1389
|
+
const found = components.length ? components : void 0;
|
|
1390
|
+
resolved.set(type, found);
|
|
1391
|
+
return found;
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
};
|
|
1395
|
+
var sameWrappers = (a, b) => a.entries.length == b.entries.length && a.entries.every((entry, i) => {
|
|
1396
|
+
const other = b.entries[i];
|
|
1397
|
+
return !!other && entry.type == other.type && entry.select == other.select && entry.component == other.component;
|
|
1398
|
+
});
|
|
1399
|
+
var defineNodeWrappers = () => {
|
|
1400
|
+
const facet = GardState3.Facet.define({
|
|
1401
|
+
combine: createNodeWrappers,
|
|
1402
|
+
// A change redraws the whole document (the renderer's `redrawsAll`): an
|
|
1403
|
+
// extension built again with the same wrappers keeps the old value.
|
|
1404
|
+
compare: sameWrappers
|
|
1405
|
+
});
|
|
1406
|
+
const nodeWrapper = (type, component) => {
|
|
1407
|
+
if (typeof type == "function") return facet.of({ type: null, select: type, component });
|
|
1408
|
+
const resolved = Node2.Type.get(type);
|
|
1409
|
+
if (resolved == Leaf3.Text) throw new Error("A node wrapper can't wrap text");
|
|
1410
|
+
return facet.of({ type: resolved, select: null, component });
|
|
1411
|
+
};
|
|
1412
|
+
return { facet, nodeWrapper };
|
|
1413
|
+
};
|
|
1414
|
+
|
|
1415
|
+
// src/editorStore.ts
|
|
1416
|
+
var NONE = /* @__PURE__ */ Symbol("none");
|
|
1417
|
+
var read = (state, field) => {
|
|
1418
|
+
try {
|
|
1419
|
+
return state.field(field);
|
|
1420
|
+
} catch {
|
|
1421
|
+
return NONE;
|
|
1422
|
+
}
|
|
1423
|
+
};
|
|
1424
|
+
var entryOf = (value, key) => value instanceof Map ? value.get(key) : value;
|
|
1425
|
+
var notifyFields = (fields, before, after) => {
|
|
1426
|
+
const pending = [];
|
|
1427
|
+
for (const [field, { all, keys }] of fields) {
|
|
1428
|
+
const a = read(before, field);
|
|
1429
|
+
const b = read(after, field);
|
|
1430
|
+
if (a === b) continue;
|
|
1431
|
+
pending.push(...all);
|
|
1432
|
+
for (const [key, listeners] of keys) {
|
|
1433
|
+
if (entryOf(a, key) !== entryOf(b, key)) pending.push(...listeners);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
for (const listener of pending) listener();
|
|
1437
|
+
};
|
|
1438
|
+
var editors = /* @__PURE__ */ new WeakMap();
|
|
1439
|
+
var editorOf = (wg) => {
|
|
1440
|
+
const found = editors.get(wg);
|
|
1441
|
+
if (found) return found;
|
|
1442
|
+
let state = wg.state;
|
|
1443
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1444
|
+
const fields = /* @__PURE__ */ new Map();
|
|
1445
|
+
const effects = /* @__PURE__ */ new Set();
|
|
1446
|
+
const taken = [];
|
|
1447
|
+
const measures = /* @__PURE__ */ new Set();
|
|
1448
|
+
let requested = false;
|
|
1449
|
+
const runEffects = () => {
|
|
1450
|
+
if (!wg.connected) return;
|
|
1451
|
+
const pending = new Set(effects);
|
|
1452
|
+
effects.clear();
|
|
1453
|
+
taken.push(pending);
|
|
1454
|
+
try {
|
|
1455
|
+
for (const effect of pending) {
|
|
1456
|
+
pending.delete(effect);
|
|
1457
|
+
effect();
|
|
1458
|
+
}
|
|
1459
|
+
} finally {
|
|
1460
|
+
taken.splice(taken.indexOf(pending), 1);
|
|
1461
|
+
}
|
|
1462
|
+
};
|
|
1463
|
+
const requestEffects = () => {
|
|
1464
|
+
if (requested) return;
|
|
1465
|
+
requested = true;
|
|
1466
|
+
queueMicrotask(() => {
|
|
1467
|
+
requested = false;
|
|
1468
|
+
if (!editor.flushing && wg.state == state) runEffects();
|
|
1469
|
+
});
|
|
1470
|
+
};
|
|
1471
|
+
const readMeasures = (wg2) => {
|
|
1472
|
+
const writes = [...measures].flatMap((measure) => measure() ?? []);
|
|
1473
|
+
if (!writes.length) return;
|
|
1474
|
+
wg2.scheduleDOMWrite(
|
|
1475
|
+
() => editor.batch(() => {
|
|
1476
|
+
for (const write of writes) write();
|
|
1477
|
+
})
|
|
1478
|
+
);
|
|
1479
|
+
};
|
|
1480
|
+
const editor = {
|
|
1481
|
+
wg,
|
|
1482
|
+
store: {
|
|
1483
|
+
getState: () => state,
|
|
1484
|
+
subscribe: (listener) => {
|
|
1485
|
+
listeners.add(listener);
|
|
1486
|
+
return () => listeners.delete(listener);
|
|
1487
|
+
},
|
|
1488
|
+
subscribeField: (field, listener, key) => {
|
|
1489
|
+
let found2 = fields.get(field);
|
|
1490
|
+
if (!found2) fields.set(field, found2 = { all: /* @__PURE__ */ new Set(), keys: /* @__PURE__ */ new Map() });
|
|
1491
|
+
const { all, keys } = found2;
|
|
1492
|
+
const set = key === void 0 ? all : keys.get(key) ?? /* @__PURE__ */ new Set();
|
|
1493
|
+
if (key !== void 0) keys.set(key, set);
|
|
1494
|
+
set.add(listener);
|
|
1495
|
+
return () => {
|
|
1496
|
+
set.delete(listener);
|
|
1497
|
+
if (key !== void 0 && !set.size) keys.delete(key);
|
|
1498
|
+
if (!all.size && !keys.size) fields.delete(field);
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1501
|
+
},
|
|
1502
|
+
afterFlush: (effect) => {
|
|
1503
|
+
effects.add(effect);
|
|
1504
|
+
if (!editor.flushing) requestEffects();
|
|
1505
|
+
},
|
|
1506
|
+
cancel: (effect) => {
|
|
1507
|
+
effects.delete(effect);
|
|
1508
|
+
for (const pending of taken) pending.delete(effect);
|
|
1509
|
+
},
|
|
1510
|
+
measures,
|
|
1511
|
+
requestMeasure: () => wg.scheduleDOMRead(readMeasures),
|
|
1512
|
+
publish: (next) => {
|
|
1513
|
+
if (next == state) return;
|
|
1514
|
+
const before = state;
|
|
1515
|
+
state = next;
|
|
1516
|
+
for (const listener of listeners) listener();
|
|
1517
|
+
if (fields.size) notifyFields(fields, before, next);
|
|
1518
|
+
},
|
|
1519
|
+
readMeasures,
|
|
1520
|
+
runEffects,
|
|
1521
|
+
requestEffects,
|
|
1522
|
+
flushing: false,
|
|
1523
|
+
batch: (change) => change()
|
|
1524
|
+
};
|
|
1525
|
+
editors.set(wg, editor);
|
|
1526
|
+
return editor;
|
|
1527
|
+
};
|
|
1528
|
+
var findEditor = (wg) => editors.get(wg);
|
|
1529
|
+
|
|
1530
|
+
// src/helpers/elt.ts
|
|
1531
|
+
var attrsToRecord = (attrs) => {
|
|
1532
|
+
const record = {};
|
|
1533
|
+
for (let i = 0; i + 1 < attrs.length; i += 2) {
|
|
1534
|
+
const name = attrs[i];
|
|
1535
|
+
const value = attrs[i + 1];
|
|
1536
|
+
if (name !== void 0 && value !== void 0) record[name] = value;
|
|
1537
|
+
}
|
|
1538
|
+
return record;
|
|
1539
|
+
};
|
|
1540
|
+
var namespaced = /^(svg|math):/;
|
|
1541
|
+
var localTagName = (tagName) => tagName.replace(namespaced, "");
|
|
1542
|
+
|
|
1543
|
+
// src/helpers/trailingBreak.ts
|
|
1544
|
+
import { Widget as Widget5 } from "wordgard/editor";
|
|
1545
|
+
var isTrailingBreak = (dom) => {
|
|
1546
|
+
const tile = dom.nodeName == "BR" ? dom.wgTile : void 0;
|
|
1547
|
+
return !!tile && "widget" in tile && tile.widget == Widget5.br;
|
|
1548
|
+
};
|
|
1549
|
+
|
|
1550
|
+
// src/helpers/staticCopy.ts
|
|
1551
|
+
var EDITOR_CLASSES = ["wg-selected-node", "wg-selected-cell"];
|
|
1552
|
+
var TRAILING_BREAK = "wg-trailing-break";
|
|
1553
|
+
var staticCopy = (dom) => {
|
|
1554
|
+
const copy = dom.cloneNode(true);
|
|
1555
|
+
if (!(copy instanceof Element)) throw new Error("Not an element");
|
|
1556
|
+
const sources2 = dom.querySelectorAll("br");
|
|
1557
|
+
copy.querySelectorAll("br").forEach((br, i) => {
|
|
1558
|
+
const source = sources2[i];
|
|
1559
|
+
if (source && isTrailingBreak(source)) br.classList.add(TRAILING_BREAK);
|
|
1560
|
+
});
|
|
1561
|
+
copy.querySelectorAll("img.wg-buffer, img.wg-blockBuffer").forEach((buffer) => buffer.remove());
|
|
1562
|
+
for (const element of [copy, ...copy.querySelectorAll("*")]) {
|
|
1563
|
+
element.removeAttribute("id");
|
|
1564
|
+
element.removeAttribute("contenteditable");
|
|
1565
|
+
if (EDITOR_CLASSES.some((name) => element.classList.contains(name))) {
|
|
1566
|
+
element.classList.remove(...EDITOR_CLASSES);
|
|
1567
|
+
if (!element.classList.length) element.removeAttribute("class");
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return copy;
|
|
1571
|
+
};
|
|
1572
|
+
|
|
1573
|
+
// src/RenderedWordgard.ts
|
|
1574
|
+
import { Wordgard as Wordgard3 } from "wordgard/editor";
|
|
1575
|
+
import { GardState as GardState4 } from "wordgard/state";
|
|
1576
|
+
|
|
1577
|
+
// src/tile.ts
|
|
1578
|
+
import {
|
|
1579
|
+
Elt as Elt8,
|
|
1580
|
+
Leaf as Leaf4
|
|
1581
|
+
} from "wordgard/doc";
|
|
1582
|
+
import { Widget as Widget6 } from "wordgard/editor";
|
|
1583
|
+
|
|
1584
|
+
// src/composition.ts
|
|
1585
|
+
import { Attributes as Attributes5, Elt as Elt7 } from "wordgard/doc";
|
|
1586
|
+
var plotAround = (doc, from, to) => {
|
|
1587
|
+
const path = /* @__PURE__ */ new Set();
|
|
1588
|
+
let plot = doc;
|
|
1589
|
+
let start = 0;
|
|
1590
|
+
for (; ; ) {
|
|
1591
|
+
let pos = start;
|
|
1592
|
+
let next = null;
|
|
1593
|
+
for (const child of plot.content) {
|
|
1594
|
+
if (child.isPlot && pos + 1 <= from && to <= pos + child.length - 1) {
|
|
1595
|
+
next = { plot: child, start: pos + 1 };
|
|
1596
|
+
break;
|
|
1597
|
+
}
|
|
1598
|
+
pos += child.length;
|
|
1599
|
+
}
|
|
1600
|
+
if (!next) return { plot, start, path };
|
|
1601
|
+
path.add(next.plot);
|
|
1602
|
+
({ plot, start } = next);
|
|
1603
|
+
}
|
|
1604
|
+
};
|
|
1605
|
+
var isRendered = (dom, rendered) => dom instanceof Element && rendered.wrappers.has(dom) || !!dom.wgTile?.isDoc || !!(dom.wgTile && (dom.wgTile.flags & TileFlag.PlotContent || markTiles.has(dom.wgTile)));
|
|
1606
|
+
var browserMade = (target, rendered) => {
|
|
1607
|
+
let node = target;
|
|
1608
|
+
while (node.parentNode instanceof Element && !isRendered(node.parentNode, rendered))
|
|
1609
|
+
node = node.parentNode;
|
|
1610
|
+
return node;
|
|
1611
|
+
};
|
|
1612
|
+
var domWrappers = (node, rendered) => {
|
|
1613
|
+
const wrappers = [];
|
|
1614
|
+
for (let dom = node.parentNode; dom instanceof Element; dom = dom.parentNode) {
|
|
1615
|
+
const tile = dom.wgTile;
|
|
1616
|
+
if (!(tile instanceof getTileClasses().EltTile) || !(tile.flags & TileFlag.Wrapper)) break;
|
|
1617
|
+
const mark = markOfTile.get(tile);
|
|
1618
|
+
if (markTiles.has(tile) && !mark) continue;
|
|
1619
|
+
const key = rendered.wrappers.get(dom);
|
|
1620
|
+
wrappers.unshift({ elt: tile.elt, spanning: tile.isSpanning, key, ...mark && { mark } });
|
|
1621
|
+
}
|
|
1622
|
+
return wrappers;
|
|
1623
|
+
};
|
|
1624
|
+
var separates = (changes, fromB, toB) => {
|
|
1625
|
+
for (let i = 0, posB = 0; i < changes.length; i += 2) {
|
|
1626
|
+
const len = changes[i] ?? 0;
|
|
1627
|
+
const ins = changes[i + 1] ?? 0;
|
|
1628
|
+
const endB = posB + (ins < 0 ? len : ins);
|
|
1629
|
+
if (ins >= 0 && fromB < endB && toB > posB && (posB < fromB || endB > toB)) return false;
|
|
1630
|
+
posB = endB;
|
|
1631
|
+
}
|
|
1632
|
+
return true;
|
|
1633
|
+
};
|
|
1634
|
+
var compositionView = (doc, info, changes, rendered) => {
|
|
1635
|
+
if (!separates(changes, info.fromB, info.toB)) return null;
|
|
1636
|
+
const { plot, start, path } = plotAround(doc, info.fromB, info.toB);
|
|
1637
|
+
const node = info.target && browserMade(info.target, rendered);
|
|
1638
|
+
return {
|
|
1639
|
+
plot,
|
|
1640
|
+
path,
|
|
1641
|
+
layout: {
|
|
1642
|
+
from: info.fromB - start,
|
|
1643
|
+
to: info.toB - start,
|
|
1644
|
+
wrappers: node ? domWrappers(node, rendered) : cursorWrappers(info.wrapCursor ?? []),
|
|
1645
|
+
cursor: !info.target
|
|
1646
|
+
},
|
|
1647
|
+
target: info.target,
|
|
1648
|
+
node,
|
|
1649
|
+
text: info.text,
|
|
1650
|
+
wrapCursor: info.wrapCursor ?? null,
|
|
1651
|
+
rendered: info.target && node == info.target && rendered.texts.get(info.target) || null
|
|
1652
|
+
};
|
|
1653
|
+
};
|
|
1654
|
+
var browserElt = (dom) => Elt7.create(
|
|
1655
|
+
dom.nodeName.toLowerCase(),
|
|
1656
|
+
Attributes5.read(
|
|
1657
|
+
Object.fromEntries(dom.getAttributeNames().map((name) => [name, dom.getAttribute(name)]))
|
|
1658
|
+
),
|
|
1659
|
+
[0]
|
|
1660
|
+
);
|
|
1661
|
+
|
|
1662
|
+
// src/tile.ts
|
|
1663
|
+
var roots3 = /* @__PURE__ */ new WeakMap();
|
|
1664
|
+
var rootOf = (dom) => {
|
|
1665
|
+
const found = roots3.get(dom);
|
|
1666
|
+
if (!found) throw new Error("No renderer for this element");
|
|
1667
|
+
return found;
|
|
1668
|
+
};
|
|
1669
|
+
var tileViews = /* @__PURE__ */ new WeakMap();
|
|
1670
|
+
var viewTiles = /* @__PURE__ */ new WeakMap();
|
|
1671
|
+
var nodeViewOf = (tile) => tileViews.get(tile);
|
|
1672
|
+
var renderable = /* @__PURE__ */ new WeakSet();
|
|
1673
|
+
var checkRenderable = (plot, words) => {
|
|
1674
|
+
if (renderable.has(plot)) return;
|
|
1675
|
+
for (const node of plot.content) {
|
|
1676
|
+
if (node.is(Leaf4.Text) || node.isPlot && renderable.has(node)) continue;
|
|
1677
|
+
const shape = node.isPlot ? node.type.shape.create(node.tag.param) : node.type.shape.create(node.param);
|
|
1678
|
+
checkShape(node.name, shape, words);
|
|
1679
|
+
if (node.isPlot && shape.hasContent) checkRenderable(node, words);
|
|
1680
|
+
}
|
|
1681
|
+
renderable.add(plot);
|
|
1682
|
+
};
|
|
1683
|
+
var checkShape = (name, shape, words, foreign = false) => {
|
|
1684
|
+
const ns = /^(svg|math):(.*)$/.exec(shape.tagName);
|
|
1685
|
+
const unsupported = `${words.library} can't render the shape of ${name} yet`;
|
|
1686
|
+
if (ns && !foreign && ns[1] != ns[2]) throw new Error(unsupported);
|
|
1687
|
+
for (const child of shape.children) {
|
|
1688
|
+
if (child === "") throw new Error(unsupported);
|
|
1689
|
+
if (child instanceof Elt8) checkShape(name, child, words, foreign || !!ns);
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
var widgetDOM = /* @__PURE__ */ new WeakSet();
|
|
1693
|
+
var placeWidgets = (dom, order) => {
|
|
1694
|
+
const wanted = new Set(order);
|
|
1695
|
+
let at = dom.firstChild;
|
|
1696
|
+
for (const node of order) {
|
|
1697
|
+
while (at && !wanted.has(at)) at = at.nextSibling;
|
|
1698
|
+
if (at == node) at = node.nextSibling;
|
|
1699
|
+
else if (widgetDOM.has(node)) dom.insertBefore(node, at);
|
|
1700
|
+
else at = node.nextSibling;
|
|
1701
|
+
}
|
|
1702
|
+
return [...dom.childNodes].filter((child) => !wanted.has(child));
|
|
1703
|
+
};
|
|
1704
|
+
var holdsWidgets = /* @__PURE__ */ new WeakSet();
|
|
1705
|
+
var noChrome = /* @__PURE__ */ new Set();
|
|
1706
|
+
var childCursor = (dom, words, chrome2 = noChrome) => {
|
|
1707
|
+
let last = null;
|
|
1708
|
+
let widgets = false;
|
|
1709
|
+
const order = [];
|
|
1710
|
+
const trailing = [];
|
|
1711
|
+
const after = () => last ? last.nextSibling : dom.firstChild;
|
|
1712
|
+
return {
|
|
1713
|
+
// Whether `skip` may be used: with widget DOM among the children, which
|
|
1714
|
+
// React doesn't draw, the cursor has to walk them to find what goes.
|
|
1715
|
+
canSkip: !holdsWidgets.has(dom) && !chrome2.size,
|
|
1716
|
+
// Takes a node React left where it was, without reading the DOM.
|
|
1717
|
+
skip: (node) => {
|
|
1718
|
+
last = node;
|
|
1719
|
+
order.push(node);
|
|
1720
|
+
},
|
|
1721
|
+
next: () => {
|
|
1722
|
+
let child = after();
|
|
1723
|
+
for (; child && (widgetDOM.has(child) || chrome2.has(child)); child = child.nextSibling) {
|
|
1724
|
+
if (widgetDOM.has(child)) {
|
|
1725
|
+
widgets = true;
|
|
1726
|
+
} else {
|
|
1727
|
+
last = child;
|
|
1728
|
+
order.push(child);
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
if (!child) throw new Error(`${words.name}'s DOM is missing a node`);
|
|
1732
|
+
last = child;
|
|
1733
|
+
order.push(child);
|
|
1734
|
+
return child;
|
|
1735
|
+
},
|
|
1736
|
+
place: (node) => {
|
|
1737
|
+
widgets = true;
|
|
1738
|
+
order.push(node);
|
|
1739
|
+
},
|
|
1740
|
+
// Places widget DOM before `ref`, a node already handed out or placed, or
|
|
1741
|
+
// last, after any chrome.
|
|
1742
|
+
insert: (node, ref) => {
|
|
1743
|
+
widgets = true;
|
|
1744
|
+
if (!ref) {
|
|
1745
|
+
trailing.push(node);
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
const at = order.indexOf(ref);
|
|
1749
|
+
if (at < 0) throw new Error("Widget DOM placed before a node not handed out");
|
|
1750
|
+
order.splice(at, 0, node);
|
|
1751
|
+
},
|
|
1752
|
+
// Checks React drew no more, puts the widget DOM in place and returns what
|
|
1753
|
+
// the element no longer holds.
|
|
1754
|
+
done: () => {
|
|
1755
|
+
for (let child = after(); child; child = child.nextSibling) {
|
|
1756
|
+
if (chrome2.has(child)) {
|
|
1757
|
+
order.push(child);
|
|
1758
|
+
continue;
|
|
1759
|
+
}
|
|
1760
|
+
if (!widgetDOM.has(child)) throw new Error(`${words.name}'s DOM has extra nodes`);
|
|
1761
|
+
widgets = true;
|
|
1762
|
+
}
|
|
1763
|
+
order.push(...trailing);
|
|
1764
|
+
if (!widgets) {
|
|
1765
|
+
holdsWidgets.delete(dom);
|
|
1766
|
+
return [];
|
|
1767
|
+
}
|
|
1768
|
+
holdsWidgets.add(dom);
|
|
1769
|
+
return placeWidgets(dom, order);
|
|
1770
|
+
}
|
|
1771
|
+
};
|
|
1772
|
+
};
|
|
1773
|
+
var elementOf = (dom, what) => {
|
|
1774
|
+
if (!(dom instanceof Element)) throw new Error(`Expected an element for ${what}`);
|
|
1775
|
+
return dom;
|
|
1776
|
+
};
|
|
1777
|
+
var widgetPool = (old) => ({ old, byPos: null });
|
|
1778
|
+
var widgetsAt = (pool, context, pos) => {
|
|
1779
|
+
if (!pool.byPos) {
|
|
1780
|
+
const byPos = /* @__PURE__ */ new Map();
|
|
1781
|
+
const { old } = pool;
|
|
1782
|
+
if (old && context.changes) {
|
|
1783
|
+
const map = pointMapper(context.changes);
|
|
1784
|
+
const collect = (tile, start) => {
|
|
1785
|
+
let pos2 = start;
|
|
1786
|
+
for (const child of tile.children) {
|
|
1787
|
+
const { dom } = child;
|
|
1788
|
+
if (widgetDOM.has(dom) || dom instanceof Element && widgetRoots.has(dom) && !child.tag) {
|
|
1789
|
+
for (const at of map(pos2)) {
|
|
1790
|
+
const found = byPos.get(at);
|
|
1791
|
+
if (found) found.push(child);
|
|
1792
|
+
else byPos.set(at, [child]);
|
|
1793
|
+
}
|
|
1794
|
+
} else if (!child.tag && !child.isNodeInner) {
|
|
1795
|
+
collect(child, pos2);
|
|
1796
|
+
}
|
|
1797
|
+
pos2 += child.length;
|
|
1798
|
+
}
|
|
1799
|
+
};
|
|
1800
|
+
collect(old, old.posAtStart);
|
|
1801
|
+
}
|
|
1802
|
+
pool.byPos = byPos;
|
|
1803
|
+
}
|
|
1804
|
+
return pool.byPos.get(pos) ?? [];
|
|
1805
|
+
};
|
|
1806
|
+
var findWidget = (tiles, context, match) => {
|
|
1807
|
+
const { WidgetTile } = getTileClasses();
|
|
1808
|
+
for (const tile of tiles)
|
|
1809
|
+
if (tile instanceof WidgetTile && !context.taken.has(tile.dom) && match(tile)) return tile;
|
|
1810
|
+
return null;
|
|
1811
|
+
};
|
|
1812
|
+
var placeWidget = (tile, cursor, context, fresh) => {
|
|
1813
|
+
widgetDOM.add(tile.dom);
|
|
1814
|
+
context.taken.add(tile.dom);
|
|
1815
|
+
cursor.place(tile.dom);
|
|
1816
|
+
if (fresh) context.toConnect.push(tile);
|
|
1817
|
+
return synced(tile);
|
|
1818
|
+
};
|
|
1819
|
+
var setUneditable = (dom) => {
|
|
1820
|
+
if (!(dom instanceof HTMLElement)) return false;
|
|
1821
|
+
const cur = dom.contentEditable;
|
|
1822
|
+
if (cur == "false") return true;
|
|
1823
|
+
if (cur != "inherit" || !VOID.test(dom.nodeName)) return false;
|
|
1824
|
+
dom.contentEditable = "false";
|
|
1825
|
+
return true;
|
|
1826
|
+
};
|
|
1827
|
+
var uneditableFlag = (widget, dom) => !widget.type.editable && setUneditable(dom) ? TileFlag.Uneditable : 0;
|
|
1828
|
+
var madeUneditable = /* @__PURE__ */ new WeakSet();
|
|
1829
|
+
var makeUneditable = (dom) => {
|
|
1830
|
+
if (!(dom instanceof HTMLElement) || dom.contentEditable != "inherit" || VOID.test(dom.nodeName))
|
|
1831
|
+
return;
|
|
1832
|
+
dom.contentEditable = "false";
|
|
1833
|
+
madeUneditable.add(dom);
|
|
1834
|
+
};
|
|
1835
|
+
var makeEditable = (dom) => {
|
|
1836
|
+
if (madeUneditable.delete(dom)) dom.removeAttribute("contenteditable");
|
|
1837
|
+
};
|
|
1838
|
+
var addBlockSeparators = (tile, cursor, context) => {
|
|
1839
|
+
const { WidgetTile } = getTileClasses();
|
|
1840
|
+
const reusable = (node) => node instanceof Element && widgetDOM.has(node) && !context.taken.has(node) && node.wgTile instanceof WidgetTile && node.wgTile.widget == Widget6.blockImg ? node : null;
|
|
1841
|
+
const { children } = tile;
|
|
1842
|
+
for (let i = 0, prev = null; ; i++) {
|
|
1843
|
+
const next = i < children.length ? children[i] : null;
|
|
1844
|
+
if (next?.isNodeInner) continue;
|
|
1845
|
+
if (!next && !prev) break;
|
|
1846
|
+
if ((!next || next.flags & TileFlag.Uneditable && !(next.flags & TileFlag.PointBefore)) && (!prev || prev.flags & TileFlag.Uneditable && !(prev.flags & TileFlag.PointAfter))) {
|
|
1847
|
+
const ref = next ? next.dom : null;
|
|
1848
|
+
const dom = reusable(ref ? ref.previousSibling : tile.dom.lastChild) ?? Widget6.blockImg.render(context.wg);
|
|
1849
|
+
const flags = TileFlag.Point | TileFlag.PointAfter;
|
|
1850
|
+
tile.putChild(synced(new WidgetTile(Widget6.blockImg, null, flags, dom)), i++);
|
|
1851
|
+
widgetDOM.add(dom);
|
|
1852
|
+
context.taken.add(dom);
|
|
1853
|
+
cursor.insert(dom, ref);
|
|
1854
|
+
}
|
|
1855
|
+
if (!next) break;
|
|
1856
|
+
prev = next;
|
|
1857
|
+
}
|
|
1858
|
+
};
|
|
1859
|
+
var pointSide = (side) => side < 0 ? TileFlag.PointBefore : side > 0 ? TileFlag.PointAfter : 0;
|
|
1860
|
+
var reactWidgetTile = (dom, context, keep, create) => {
|
|
1861
|
+
if (!(dom instanceof Element) || !widgetRoots.has(dom)) {
|
|
1862
|
+
const { name, widgetRoot: widgetRoot2 } = context.words;
|
|
1863
|
+
throw new Error(`A ${name} widget's component must ${widgetRoot2}`);
|
|
1864
|
+
}
|
|
1865
|
+
const { WidgetTile } = getTileClasses();
|
|
1866
|
+
const old = dom.wgTile;
|
|
1867
|
+
context.taken.add(dom);
|
|
1868
|
+
if (old instanceof WidgetTile && keep(old)) {
|
|
1869
|
+
context.reused.set(old, Reused.Full);
|
|
1870
|
+
return old;
|
|
1871
|
+
}
|
|
1872
|
+
if (old) context.reused.set(old, Reused.DOM);
|
|
1873
|
+
const tile = create(dom);
|
|
1874
|
+
context.toConnect.push(tile);
|
|
1875
|
+
return synced(tile);
|
|
1876
|
+
};
|
|
1877
|
+
var addChildTiles = (parent, plot, views, cursor, context, pool, start, kept) => {
|
|
1878
|
+
const { EltTile, TextTile, WidgetTile } = getTileClasses();
|
|
1879
|
+
let pos = start;
|
|
1880
|
+
const add = (tile) => {
|
|
1881
|
+
parent.addChild(tile);
|
|
1882
|
+
pos += tile.length;
|
|
1883
|
+
};
|
|
1884
|
+
let keyed = null;
|
|
1885
|
+
const keyAt = (i) => (keyed ??= childViewKeys(views))[i]?.key ?? "";
|
|
1886
|
+
for (let i = 0; i < views.length; i++) {
|
|
1887
|
+
const view = views[i];
|
|
1888
|
+
if (!view) continue;
|
|
1889
|
+
if (view.type == "composition" && !context.composition?.node) continue;
|
|
1890
|
+
if (view.type == "widget") {
|
|
1891
|
+
const { widget } = view;
|
|
1892
|
+
const side = pointSide(view.side);
|
|
1893
|
+
const matching = (tile2) => !tile2.tag && !tile2.isNodeInner && (tile2.flags & (TileFlag.PointBefore | TileFlag.PointAfter)) == side && !context.dirty.has(tile2.dom) && tile2.widget.eq(widget);
|
|
1894
|
+
if (isComponentWidget(widget)) {
|
|
1895
|
+
const keep = (tile3) => matching(tile3) && widgetsAt(pool, context, pos).includes(tile3);
|
|
1896
|
+
const tile2 = reactWidgetTile(
|
|
1897
|
+
cursor.next(),
|
|
1898
|
+
context,
|
|
1899
|
+
keep,
|
|
1900
|
+
(dom3) => new WidgetTile(widget, null, TileFlag.Point | side | uneditableFlag(widget, dom3), dom3)
|
|
1901
|
+
);
|
|
1902
|
+
add(tile2);
|
|
1903
|
+
continue;
|
|
1904
|
+
}
|
|
1905
|
+
const old = findWidget(widgetsAt(pool, context, pos), context, matching);
|
|
1906
|
+
if (old) {
|
|
1907
|
+
context.reused.set(old, Reused.Full);
|
|
1908
|
+
add(placeWidget(old, cursor, context, false));
|
|
1909
|
+
continue;
|
|
1910
|
+
}
|
|
1911
|
+
const dom2 = widget.render(context.wg);
|
|
1912
|
+
const flags = TileFlag.Point | side | uneditableFlag(widget, dom2);
|
|
1913
|
+
const tile = new WidgetTile(widget, null, flags, dom2);
|
|
1914
|
+
add(placeWidget(tile, cursor, context, true));
|
|
1915
|
+
continue;
|
|
1916
|
+
}
|
|
1917
|
+
if (view.type == "node" && !(view.shape instanceof Elt8)) {
|
|
1918
|
+
const { node, shape } = view;
|
|
1919
|
+
const tag = node.isPlot ? node.tag : node;
|
|
1920
|
+
if (isComponentWidget(shape)) {
|
|
1921
|
+
const keep = (old2) => {
|
|
1922
|
+
const oldView2 = tileViews.get(old2);
|
|
1923
|
+
return !!oldView2 && sameNodeView(oldView2, view) && !redrawn(context.changes, pos);
|
|
1924
|
+
};
|
|
1925
|
+
const tile2 = reactWidgetTile(
|
|
1926
|
+
cursor.next(),
|
|
1927
|
+
context,
|
|
1928
|
+
keep,
|
|
1929
|
+
(dom3) => new WidgetTile(shape, tag, TileFlag.Atom | uneditableFlag(shape, dom3), dom3, node.length)
|
|
1930
|
+
);
|
|
1931
|
+
tileViews.set(tile2, view);
|
|
1932
|
+
add(tile2);
|
|
1933
|
+
continue;
|
|
1934
|
+
}
|
|
1935
|
+
const old = findWidget(
|
|
1936
|
+
widgetsAt(pool, context, pos),
|
|
1937
|
+
context,
|
|
1938
|
+
(tile2) => !!tile2.tag && tile2.widget.eq(shape)
|
|
1939
|
+
);
|
|
1940
|
+
const oldView = old && tileViews.get(old);
|
|
1941
|
+
if (old && oldView && sameNodeView(oldView, view) && !redrawn(context.changes, pos)) {
|
|
1942
|
+
context.reused.set(old, Reused.Full);
|
|
1943
|
+
add(placeWidget(old, cursor, context, false));
|
|
1944
|
+
continue;
|
|
1945
|
+
}
|
|
1946
|
+
if (old) context.reused.set(old, Reused.DOM);
|
|
1947
|
+
const dom2 = old ? old.dom : shape.render(context.wg);
|
|
1948
|
+
const flags = TileFlag.Atom | uneditableFlag(shape, dom2);
|
|
1949
|
+
const tile = new WidgetTile(shape, tag, flags, dom2, node.length);
|
|
1950
|
+
tileViews.set(tile, view);
|
|
1951
|
+
add(placeWidget(tile, cursor, context, true));
|
|
1952
|
+
continue;
|
|
1953
|
+
}
|
|
1954
|
+
const known = kept && cursor.canSkip && view.type == "node" ? viewTiles.get(view) : void 0;
|
|
1955
|
+
const same = known?.dom.parentNode == parent.dom ? known : void 0;
|
|
1956
|
+
const dom = same ? same.dom : cursor.next();
|
|
1957
|
+
if (same) cursor.skip(same.dom);
|
|
1958
|
+
if (view.type == "text") {
|
|
1959
|
+
if (!(dom instanceof Text)) throw new Error("Expected a text node");
|
|
1960
|
+
context.rendered.texts.set(dom, { key: keyAt(i), text: view.text });
|
|
1961
|
+
add(synced(new TextTile(view.text, dom)));
|
|
1962
|
+
} else if (view.type == "composition") {
|
|
1963
|
+
if (!context.composition || dom !== context.composition.node)
|
|
1964
|
+
throw new Error("Expected the composition's node");
|
|
1965
|
+
add(compositionTile(dom, context.composition));
|
|
1966
|
+
} else if (view.type == "node") {
|
|
1967
|
+
const { node, shape, children } = view;
|
|
1968
|
+
const old = same ?? dom.wgTile;
|
|
1969
|
+
const oldView = old && tileViews.get(old);
|
|
1970
|
+
if (old && oldView && sameNodeView(oldView, view) && !holdsChanged(old, context)) {
|
|
1971
|
+
if (context.oldWidgets) {
|
|
1972
|
+
if (!children && redrawn(context.changes, pos)) context.toConnect.push(old);
|
|
1973
|
+
else context.reused.set(old, Reused.Full);
|
|
1974
|
+
}
|
|
1975
|
+
viewTiles.set(view, old);
|
|
1976
|
+
add(old);
|
|
1977
|
+
continue;
|
|
1978
|
+
}
|
|
1979
|
+
if (!(shape instanceof Elt8)) throw new Error("Expected an element shape");
|
|
1980
|
+
const tag = node.isPlot ? node.tag : node;
|
|
1981
|
+
const oldTag = oldView && (oldView.node.isPlot ? oldView.node.tag : oldView.node);
|
|
1982
|
+
const keepShape = !!oldTag?.eq(tag) && !!oldView?.shape.eq(shape) && !redrawn(context.changes, pos);
|
|
1983
|
+
const contentStart = pos + 1;
|
|
1984
|
+
const fill = (tile2, content, pool2) => {
|
|
1985
|
+
const kept2 = !!context.changes && !!pool2.old;
|
|
1986
|
+
if (node.isPlot && children)
|
|
1987
|
+
addChildTiles(tile2, node, children, content, context, pool2, contentStart, kept2);
|
|
1988
|
+
};
|
|
1989
|
+
const tile = shapeTile(shape, tag, node.length, elementOf(dom, node.name), {
|
|
1990
|
+
inEditable: true,
|
|
1991
|
+
container: isContainer(node),
|
|
1992
|
+
blocks: holdsBlocks(node),
|
|
1993
|
+
afterContent: 0,
|
|
1994
|
+
keepShape,
|
|
1995
|
+
fill,
|
|
1996
|
+
context
|
|
1997
|
+
});
|
|
1998
|
+
tileViews.set(tile, view);
|
|
1999
|
+
viewTiles.set(view, tile);
|
|
2000
|
+
add(tile);
|
|
2001
|
+
} else if (view.type == "wrapper") {
|
|
2002
|
+
const flags = TileFlag.Wrapper | (view.spanning ? TileFlag.Spanning : 0);
|
|
2003
|
+
const element = elementOf(dom, "a wrapper");
|
|
2004
|
+
context.rendered.wrappers.set(element, keyAt(i));
|
|
2005
|
+
const wrapperKept = kept && !!element.wgTile;
|
|
2006
|
+
if (view.mark) {
|
|
2007
|
+
const fill = (tile3, content2, contentKept) => {
|
|
2008
|
+
addChildTiles(tile3, plot, view.children, content2, context, pool, pos, contentKept);
|
|
2009
|
+
if (holdsBlocks(plot)) addBlockSeparators(tile3, content2, context);
|
|
2010
|
+
};
|
|
2011
|
+
const tile2 = reactMarkTile(view, view.mark, flags, element, context, fill, wrapperKept);
|
|
2012
|
+
markViews.set(tile2, { view, plot });
|
|
2013
|
+
add(tile2);
|
|
2014
|
+
continue;
|
|
2015
|
+
}
|
|
2016
|
+
const tile = EltTile.of(view.elt, null, flags, 0, element);
|
|
2017
|
+
const content = childCursor(element, context.words);
|
|
2018
|
+
addChildTiles(tile, plot, view.children, content, context, pool, pos, wrapperKept);
|
|
2019
|
+
if (holdsBlocks(plot)) addBlockSeparators(tile, content, context);
|
|
2020
|
+
finish(content, context);
|
|
2021
|
+
tile.length = tile.children.reduce((length, ch) => length + ch.length, 0);
|
|
2022
|
+
add(synced(tile));
|
|
2023
|
+
} else if (view.type == "buffer") {
|
|
2024
|
+
const side = view.side < 0 ? TileFlag.PointBefore : TileFlag.PointAfter;
|
|
2025
|
+
const tile = new WidgetTile(
|
|
2026
|
+
Widget6.img,
|
|
2027
|
+
null,
|
|
2028
|
+
TileFlag.Point | side,
|
|
2029
|
+
elementOf(dom, "a buffer")
|
|
2030
|
+
);
|
|
2031
|
+
add(synced(tile));
|
|
2032
|
+
} else {
|
|
2033
|
+
const tile = new WidgetTile(
|
|
2034
|
+
Widget6.br,
|
|
2035
|
+
null,
|
|
2036
|
+
TileFlag.Point | TileFlag.PointAfter,
|
|
2037
|
+
elementOf(dom, "the trailing <br>")
|
|
2038
|
+
);
|
|
2039
|
+
add(synced(tile));
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
};
|
|
2043
|
+
var finish = (cursor, context) => {
|
|
2044
|
+
context.strays.push(...cursor.done());
|
|
2045
|
+
};
|
|
2046
|
+
var browserTile = (dom) => getTileClasses().EltTile.of(browserElt(dom), null, 0, 0, dom);
|
|
2047
|
+
var compositionTile = (dom, composition) => {
|
|
2048
|
+
const { TextTile } = getTileClasses();
|
|
2049
|
+
if (dom instanceof Text) return synced(new TextTile(composition.text, dom, TileFlag.Composition));
|
|
2050
|
+
const element = elementOf(dom, "the composition");
|
|
2051
|
+
const inner = [...element.childNodes].find((child) => child.contains(composition.target));
|
|
2052
|
+
if (!inner) throw new Error("Expected the composition's node");
|
|
2053
|
+
const tile = browserTile(element);
|
|
2054
|
+
tile.addChild(compositionTile(inner, composition));
|
|
2055
|
+
tile.length = composition.text.length;
|
|
2056
|
+
return synced(tile);
|
|
2057
|
+
};
|
|
2058
|
+
var shapeTile = (shape, tag, length, dom, shapeContext) => {
|
|
2059
|
+
if (isComponentShape(shape) || isComponentWrapper(shape))
|
|
2060
|
+
return reactShapeTile(shape, tag, length, dom, shapeContext);
|
|
2061
|
+
const attributed2 = attributedWidget(shape);
|
|
2062
|
+
if (attributed2) return attributedWidgetTile(attributed2, shape, tag, length, dom, shapeContext);
|
|
2063
|
+
const { inEditable, afterContent, keepShape, fill, context } = shapeContext;
|
|
2064
|
+
const { EltTile, WidgetTile } = getTileClasses();
|
|
2065
|
+
const previous = dom.wgTile ?? null;
|
|
2066
|
+
const { shape: elt, inEditable: inner, flag } = uneditable(shape, inEditable);
|
|
2067
|
+
const flags = tag ? elt.hasContent ? 0 : TileFlag.Atom : TileFlag.NodeInner | (elt.hasContent ? 0 : TileFlag.Point);
|
|
2068
|
+
const tile = EltTile.of(elt, tag, flags | flag | afterContent, tag ? length : 0, dom);
|
|
2069
|
+
const cursor = childCursor(dom, context.words);
|
|
2070
|
+
let after = 0;
|
|
2071
|
+
for (const child of elt.children) {
|
|
2072
|
+
if (child === 0) {
|
|
2073
|
+
tile.flags |= TileFlag.PlotContent;
|
|
2074
|
+
fill(tile, cursor, widgetPool(previous));
|
|
2075
|
+
after = TileFlag.AfterContent;
|
|
2076
|
+
} else if (typeof child == "string") {
|
|
2077
|
+
const text = cursor.next();
|
|
2078
|
+
if (!(text instanceof Text)) throw new Error("Expected a text node in a shape");
|
|
2079
|
+
const flags2 = TileFlag.Point | TileFlag.NodeInner | after;
|
|
2080
|
+
tile.addChild(synced(new WidgetTile(Widget6.text.of(child), null, flags2, text)));
|
|
2081
|
+
} else if (child instanceof Elt8) {
|
|
2082
|
+
const inside = { ...shapeContext, inEditable: inner, afterContent: after };
|
|
2083
|
+
tile.addChild(shapeTile(child, null, 0, elementOf(cursor.next(), child.tagName), inside));
|
|
2084
|
+
} else if (isComponentWidget(child)) {
|
|
2085
|
+
const flags2 = TileFlag.Point | TileFlag.NodeInner | after;
|
|
2086
|
+
const keep = (old) => keepShape && old.isNodeInner && old.widget.eq(child);
|
|
2087
|
+
const widget = reactWidgetTile(
|
|
2088
|
+
cursor.next(),
|
|
2089
|
+
context,
|
|
2090
|
+
keep,
|
|
2091
|
+
(dom2) => new WidgetTile(child, null, flags2 | (inner ? uneditableFlag(child, dom2) : 0), dom2)
|
|
2092
|
+
);
|
|
2093
|
+
tile.addChild(widget);
|
|
2094
|
+
} else {
|
|
2095
|
+
const old = findWidget(
|
|
2096
|
+
previous?.children ?? [],
|
|
2097
|
+
context,
|
|
2098
|
+
(tile2) => tile2.isNodeInner && tile2.widget.eq(child)
|
|
2099
|
+
);
|
|
2100
|
+
if (old && keepShape) {
|
|
2101
|
+
context.reused.set(old, Reused.Full);
|
|
2102
|
+
tile.addChild(placeWidget(old, cursor, context, false));
|
|
2103
|
+
continue;
|
|
2104
|
+
}
|
|
2105
|
+
if (old) context.reused.set(old, Reused.DOM);
|
|
2106
|
+
const dom2 = old ? old.dom : child.render(context.wg);
|
|
2107
|
+
const flags2 = TileFlag.Point | TileFlag.NodeInner | after | (inner ? uneditableFlag(child, dom2) : 0);
|
|
2108
|
+
tile.addChild(placeWidget(new WidgetTile(child, null, flags2, dom2), cursor, context, true));
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
if (tile.flags & TileFlag.PlotContent && shapeContext.blocks)
|
|
2112
|
+
addBlockSeparators(tile, cursor, context);
|
|
2113
|
+
finish(cursor, context);
|
|
2114
|
+
if (!(tile.flags & TileFlag.Atom))
|
|
2115
|
+
tile.length = tile.children.reduce((sum, ch) => sum + ch.length, tile.boundary * 2);
|
|
2116
|
+
return synced(tile);
|
|
2117
|
+
};
|
|
2118
|
+
var attributedWidgetTile = (widget, wrapper, tag, length, dom, { inEditable, afterContent, keepShape, context }) => {
|
|
2119
|
+
const { WidgetTile } = getTileClasses();
|
|
2120
|
+
const { flag } = uneditable(wrapper, inEditable);
|
|
2121
|
+
const flags = (tag ? TileFlag.Atom : TileFlag.Point | TileFlag.NodeInner) | afterContent | flag;
|
|
2122
|
+
const keep = (old) => keepShape && old.widget.eq(widget);
|
|
2123
|
+
return reactWidgetTile(
|
|
2124
|
+
dom,
|
|
2125
|
+
context,
|
|
2126
|
+
keep,
|
|
2127
|
+
(dom2) => new WidgetTile(widget, tag, flags, dom2, tag ? length : 0)
|
|
2128
|
+
);
|
|
2129
|
+
};
|
|
2130
|
+
var chrome = Widget6.define({
|
|
2131
|
+
render: () => {
|
|
2132
|
+
throw new Error("The renderer draws a shape's chrome");
|
|
2133
|
+
},
|
|
2134
|
+
propagateEvent: (event) => !stopsEvent(event)
|
|
2135
|
+
}).of(null);
|
|
2136
|
+
var shapeTiles = /* @__PURE__ */ new WeakSet();
|
|
2137
|
+
var ownTileClass = null;
|
|
2138
|
+
var ownTile = (elt, tag, flags, length, dom) => {
|
|
2139
|
+
ownTileClass ??= class extends getTileClasses().EltTile {
|
|
2140
|
+
ignoreEvent(event) {
|
|
2141
|
+
return event.target == this.dom && stopsEvent(event);
|
|
2142
|
+
}
|
|
2143
|
+
};
|
|
2144
|
+
if (elt.hasContent) {
|
|
2145
|
+
flags |= TileFlag.HasContent;
|
|
2146
|
+
const hole = elt.children.indexOf(0);
|
|
2147
|
+
if (hole > -1 && hole < elt.children.length - 1) flags |= TileFlag.ContentNotLast;
|
|
2148
|
+
}
|
|
2149
|
+
return new ownTileClass(elt, tag, flags, length, dom);
|
|
2150
|
+
};
|
|
2151
|
+
var addChromeTile = (tile, child, flags) => {
|
|
2152
|
+
if (!(child instanceof Element || child instanceof Text || child instanceof Comment)) return;
|
|
2153
|
+
if (child instanceof Element) makeUneditable(child);
|
|
2154
|
+
const { WidgetTile } = getTileClasses();
|
|
2155
|
+
tile.addChild(synced(new WidgetTile(chrome, null, flags, child)));
|
|
2156
|
+
};
|
|
2157
|
+
var isAnchor = (node) => node instanceof Comment || node instanceof Text && !node.length;
|
|
2158
|
+
var checkAnchors = (nodes, what, words) => {
|
|
2159
|
+
for (const node of nodes)
|
|
2160
|
+
if (isAnchor(node))
|
|
2161
|
+
throw new Error(
|
|
2162
|
+
`A ${words.name} ${what} holds an empty text node or a comment beside its content: ${words.anchor}`
|
|
2163
|
+
);
|
|
2164
|
+
};
|
|
2165
|
+
var markViews = /* @__PURE__ */ new WeakMap();
|
|
2166
|
+
var markContents = /* @__PURE__ */ new WeakMap();
|
|
2167
|
+
var reactMarkTile = (view, mark, flags, dom, context, fill, kept) => {
|
|
2168
|
+
const { EltTile } = getTileClasses();
|
|
2169
|
+
const handle = shapeOfRoot(dom);
|
|
2170
|
+
const { words } = context;
|
|
2171
|
+
if (!handle || handle.wrapper)
|
|
2172
|
+
throw new Error(`A ${words.name} mark's component must ${words.root}`);
|
|
2173
|
+
if (dom.localName != localTagName(view.elt.tagName))
|
|
2174
|
+
warnElement(words, "mark", view.elt.tagName, dom.localName);
|
|
2175
|
+
const content = contentOf(handle) ?? dom;
|
|
2176
|
+
if (!dom.contains(content))
|
|
2177
|
+
throw new Error(`A ${words.name} mark's content element is outside it`);
|
|
2178
|
+
const owner = tiledRoot(dom).wgTile;
|
|
2179
|
+
const old = owner && markContents.get(owner);
|
|
2180
|
+
const moved = !!old && old.dom != content;
|
|
2181
|
+
if (old && moved) removeWidgetDOM(old, content);
|
|
2182
|
+
let contentTile2 = null;
|
|
2183
|
+
const build2 = (element, elt, flags2) => {
|
|
2184
|
+
const tile2 = element == content ? EltTile.of(elt, null, flags2, 0, element) : ownTile(elt, null, flags2, 0, element);
|
|
2185
|
+
shapeTiles.add(tile2);
|
|
2186
|
+
markTiles.add(tile2);
|
|
2187
|
+
if (element == content) {
|
|
2188
|
+
const cursor = childCursor(element, words);
|
|
2189
|
+
fill(tile2, cursor, kept && !moved);
|
|
2190
|
+
finish(cursor, context);
|
|
2191
|
+
contentTile2 = tile2;
|
|
2192
|
+
} else {
|
|
2193
|
+
checkAnchors(element.childNodes, "mark", words);
|
|
2194
|
+
for (const child of [...element.childNodes]) {
|
|
2195
|
+
if (child instanceof Element && child.contains(content)) {
|
|
2196
|
+
makeEditable(child);
|
|
2197
|
+
tile2.addChild(build2(child, Elt8.mk(child.localName, [0]), TileFlag.Wrapper));
|
|
2198
|
+
} else {
|
|
2199
|
+
addChromeTile(tile2, child, TileFlag.Point);
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
tile2.length = tile2.children.reduce((sum, ch) => sum + ch.length, 0);
|
|
2204
|
+
return synced(tile2);
|
|
2205
|
+
};
|
|
2206
|
+
const tile = build2(dom, view.elt, flags);
|
|
2207
|
+
markOfTile.set(tile, mark);
|
|
2208
|
+
if (contentTile2) markContents.set(tile, contentTile2);
|
|
2209
|
+
return tile;
|
|
2210
|
+
};
|
|
2211
|
+
var reactShapeTile = (elt, tag, length, dom, shapeContext) => {
|
|
2212
|
+
const { afterContent, container, blocks, fill, context } = shapeContext;
|
|
2213
|
+
const { EltTile, WidgetTile } = getTileClasses();
|
|
2214
|
+
const { words } = context;
|
|
2215
|
+
const handle = shapeOfRoot(dom);
|
|
2216
|
+
const what = isComponentWrapper(elt) ? "wrapper" : "shape";
|
|
2217
|
+
if (!handle || handle.wrapper != (what == "wrapper"))
|
|
2218
|
+
throw new Error(`A ${words.name} ${what}'s component must ${words.root}`);
|
|
2219
|
+
if (dom.localName != localTagName(elt.tagName))
|
|
2220
|
+
warnElement(words, what, elt.tagName, dom.localName);
|
|
2221
|
+
if (handle.wrapper && !handle.content)
|
|
2222
|
+
throw new Error(`A ${words.name} wrapper's component must draw ${words.wrapped}`);
|
|
2223
|
+
const atom = !handle.wrapper && !elt.hasContent;
|
|
2224
|
+
if (atom && handle.content)
|
|
2225
|
+
throw new Error(
|
|
2226
|
+
`A ${words.name} shape drawing an atom has no content: drop its ${words.content}`
|
|
2227
|
+
);
|
|
2228
|
+
const content = atom ? null : contentOf(handle) ?? dom;
|
|
2229
|
+
if (content && !dom.contains(content) || handle.wrapper && content == dom)
|
|
2230
|
+
throw new Error(
|
|
2231
|
+
`A ${words.name} ${what}'s ${handle.wrapper ? "children are" : "content element is"} outside it`
|
|
2232
|
+
);
|
|
2233
|
+
const { inEditable, flag } = uneditable(elt, shapeContext.inEditable);
|
|
2234
|
+
if (flag) makeUneditable(dom);
|
|
2235
|
+
const owner = tiledRoot(dom).wgTile;
|
|
2236
|
+
const old = !handle.wrapper && owner ? contentTile(owner) : null;
|
|
2237
|
+
const moved = old && old.dom != content ? old : null;
|
|
2238
|
+
if (moved) {
|
|
2239
|
+
for (const child of moved.children) if (widgetDOM.has(child.dom)) child.dom.remove();
|
|
2240
|
+
}
|
|
2241
|
+
if (handle.wrapper && content && owner) removeWidgetDOM(owner, content);
|
|
2242
|
+
const wrappedTile = (element, after) => {
|
|
2243
|
+
const [child] = elt.children;
|
|
2244
|
+
const inside = { ...shapeContext, inEditable, afterContent: after };
|
|
2245
|
+
if (child instanceof Elt8) return shapeTile(child, null, 0, element, inside);
|
|
2246
|
+
if (child === void 0 || child === 0 || typeof child == "string" || !isComponentWidget(child))
|
|
2247
|
+
throw new Error(`A ${words.name} wrapper wraps an element or a ${words.name} widget`);
|
|
2248
|
+
const flags2 = TileFlag.Point | TileFlag.NodeInner | after;
|
|
2249
|
+
const keep = (old2) => shapeContext.keepShape && old2.isNodeInner && old2.widget.eq(child);
|
|
2250
|
+
return reactWidgetTile(
|
|
2251
|
+
element,
|
|
2252
|
+
context,
|
|
2253
|
+
keep,
|
|
2254
|
+
(dom2) => new WidgetTile(child, null, flags2 | (inEditable ? uneditableFlag(child, dom2) : 0), dom2)
|
|
2255
|
+
);
|
|
2256
|
+
};
|
|
2257
|
+
const addChrome = (tile, child, after) => addChromeTile(tile, child, TileFlag.Point | TileFlag.NodeInner | after);
|
|
2258
|
+
const build2 = (element, elt2, tag2, flags2) => {
|
|
2259
|
+
const previous = element.wgTile ?? null;
|
|
2260
|
+
const tile = element == content && !handle.wrapper ? EltTile.of(elt2, tag2, flags2, tag2 ? length : 0, element) : ownTile(elt2, tag2, flags2, tag2 ? length : 0, element);
|
|
2261
|
+
shapeTiles.add(tile);
|
|
2262
|
+
if (element == content && !handle.wrapper) {
|
|
2263
|
+
tile.flags |= TileFlag.PlotContent;
|
|
2264
|
+
const children = [...element.childNodes];
|
|
2265
|
+
checkAnchors(children, "shape's content element", words);
|
|
2266
|
+
const isChrome = (child) => container && !isContentDOM(child);
|
|
2267
|
+
const first = children.findIndex((child) => !isChrome(child));
|
|
2268
|
+
const last = children.findLastIndex((child) => !isChrome(child));
|
|
2269
|
+
if (children.slice(first, last + 1).some(isChrome))
|
|
2270
|
+
throw new Error(`A ${words.name} shape's chrome is between its content's nodes`);
|
|
2271
|
+
const before = first < 0 ? children : children.slice(0, first);
|
|
2272
|
+
const after = first < 0 ? [] : children.slice(last + 1);
|
|
2273
|
+
for (const child of before) addChrome(tile, child, 0);
|
|
2274
|
+
const cursor = childCursor(element, words, /* @__PURE__ */ new Set([...before, ...after]));
|
|
2275
|
+
fill(tile, cursor, widgetPool(moved ?? previous));
|
|
2276
|
+
for (const child of after) addChrome(tile, child, TileFlag.AfterContent);
|
|
2277
|
+
if (blocks) addBlockSeparators(tile, cursor, context);
|
|
2278
|
+
finish(cursor, context);
|
|
2279
|
+
} else {
|
|
2280
|
+
let after = 0;
|
|
2281
|
+
for (const child of [...element.childNodes]) {
|
|
2282
|
+
if (handle.wrapper && child == content) {
|
|
2283
|
+
tile.addChild(wrappedTile(content, after));
|
|
2284
|
+
after = TileFlag.AfterContent;
|
|
2285
|
+
} else if (child instanceof Element && child.contains(content)) {
|
|
2286
|
+
const structure = Elt8.mk(child.localName, hole ? [0] : []);
|
|
2287
|
+
makeEditable(child);
|
|
2288
|
+
tile.addChild(build2(child, structure, null, innerFlags | after));
|
|
2289
|
+
after = TileFlag.AfterContent;
|
|
2290
|
+
} else {
|
|
2291
|
+
addChrome(tile, child, after);
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
if (!(tile.flags & TileFlag.Atom))
|
|
2296
|
+
tile.length = tile.children.reduce((sum, ch) => sum + ch.length, tile.boundary * 2);
|
|
2297
|
+
return synced(tile);
|
|
2298
|
+
};
|
|
2299
|
+
const hole = elt.hasContent;
|
|
2300
|
+
const innerFlags = TileFlag.NodeInner | (hole ? 0 : TileFlag.Point);
|
|
2301
|
+
const flags = tag ? hole ? 0 : TileFlag.Atom : innerFlags;
|
|
2302
|
+
return build2(dom, elt, tag, flags | flag | afterContent);
|
|
2303
|
+
};
|
|
2304
|
+
var removeWidgetDOM = (tile, keep) => {
|
|
2305
|
+
for (const child of tile.children) {
|
|
2306
|
+
if (!widgetDOM.has(child.dom)) removeWidgetDOM(child, keep);
|
|
2307
|
+
else if (!keep.contains(child.dom)) child.dom.remove();
|
|
2308
|
+
}
|
|
2309
|
+
};
|
|
2310
|
+
var isContainer = (node) => node.isPlot && !node.isTextblock && !node.isInline;
|
|
2311
|
+
var holdsBlocks = (node) => node.isPlot && !node.inlineContent;
|
|
2312
|
+
var nodeTileOf = (root) => {
|
|
2313
|
+
let tile = root.wgTile;
|
|
2314
|
+
while (tile && !tile.tag) tile = tile.parent ?? void 0;
|
|
2315
|
+
return tile;
|
|
2316
|
+
};
|
|
2317
|
+
var holdsText = (root) => {
|
|
2318
|
+
const tile = nodeTileOf(tiledRoot(root));
|
|
2319
|
+
const view = tile && tileViews.get(tile);
|
|
2320
|
+
return !view || !isContainer(view.node);
|
|
2321
|
+
};
|
|
2322
|
+
var isContentDOM = (node) => widgetDOM.has(node) || node instanceof Element && (viewRoots.has(node) || !!shapeOfRoot(node) || widgetRoots.has(node));
|
|
2323
|
+
var isChromeTile = (tile) => tile instanceof getTileClasses().WidgetTile && tile.widget == chrome;
|
|
2324
|
+
var warned = /* @__PURE__ */ new Set();
|
|
2325
|
+
var warnElement = (words, what, expected, found) => {
|
|
2326
|
+
const key = `${words.name}:${what}:${expected}>${found}`;
|
|
2327
|
+
if (warned.has(key)) return;
|
|
2328
|
+
warned.add(key);
|
|
2329
|
+
const fix = {
|
|
2330
|
+
shape: `draw the element its shape says (the node type's, or the one given to \`of\`), or pass <${found}> to \`of\``,
|
|
2331
|
+
wrapper: "give createWrapper the element its component draws",
|
|
2332
|
+
mark: "give createMarkShape the element its component draws"
|
|
2333
|
+
}[what];
|
|
2334
|
+
console.error(
|
|
2335
|
+
`A ${words.name} ${what}'s component drew <${found}> where its ${what} says <${expected}>: ${fix}`
|
|
2336
|
+
);
|
|
2337
|
+
};
|
|
2338
|
+
var holdsChanged = (tile, context) => {
|
|
2339
|
+
for (const root of context.changedShapes) if (tile.dom.contains(root)) return true;
|
|
2340
|
+
return false;
|
|
2341
|
+
};
|
|
2342
|
+
var synced = (tile) => {
|
|
2343
|
+
tile.flags |= TileFlag.Synced;
|
|
2344
|
+
return tile;
|
|
2345
|
+
};
|
|
2346
|
+
var setAttributes = (dom, attrs) => {
|
|
2347
|
+
const wanted = attrsToRecord(attrs);
|
|
2348
|
+
for (const name of dom.getAttributeNames())
|
|
2349
|
+
if (!Object.hasOwn(wanted, name)) dom.removeAttribute(name);
|
|
2350
|
+
for (const [name, value] of Object.entries(wanted))
|
|
2351
|
+
if (dom.getAttribute(name) != value) dom.setAttribute(name, value);
|
|
2352
|
+
};
|
|
2353
|
+
var restore = (tile, composing) => {
|
|
2354
|
+
if (composing) keepComposing(composing);
|
|
2355
|
+
const { EltTile, WidgetTile } = getTileClasses();
|
|
2356
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
2357
|
+
const resetAttributes = (tile2) => {
|
|
2358
|
+
if (!(tile2.flags & TileFlag.Dirty)) return;
|
|
2359
|
+
if (tile2 instanceof EltTile && !shapeTiles.has(tile2)) setAttributes(tile2.dom, tile2.elt.attrs);
|
|
2360
|
+
else if (tile2 instanceof WidgetTile) dirty.add(tile2.dom);
|
|
2361
|
+
tile2.children.forEach(resetAttributes);
|
|
2362
|
+
};
|
|
2363
|
+
resetAttributes(tile);
|
|
2364
|
+
tile.sync();
|
|
2365
|
+
return dirty;
|
|
2366
|
+
};
|
|
2367
|
+
var keepComposing = (node) => {
|
|
2368
|
+
const parent = node.parentNode?.wgTile;
|
|
2369
|
+
const own = node.wgTile;
|
|
2370
|
+
const clean = (tile2) => {
|
|
2371
|
+
tile2.flags &= ~TileFlag.Dirty;
|
|
2372
|
+
tile2.children.forEach(clean);
|
|
2373
|
+
};
|
|
2374
|
+
if (own && own.parent == parent) {
|
|
2375
|
+
clean(own);
|
|
2376
|
+
return;
|
|
2377
|
+
}
|
|
2378
|
+
if (!(parent instanceof DocTile || parent instanceof getTileClasses().EltTile)) return;
|
|
2379
|
+
let index = parent.children.length;
|
|
2380
|
+
for (let next = node.nextSibling; next; next = next.nextSibling) {
|
|
2381
|
+
const found = parent.children.findIndex((child) => child.dom == next);
|
|
2382
|
+
if (found > -1) {
|
|
2383
|
+
index = found;
|
|
2384
|
+
break;
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
const { TextTile } = getTileClasses();
|
|
2388
|
+
const tile = node instanceof Text ? new TextTile(node.nodeValue ?? "", node, TileFlag.Composition) : browserTile(elementOf(node, "the composition"));
|
|
2389
|
+
parent.children.splice(index, 0, synced(tile));
|
|
2390
|
+
tile.parent = parent;
|
|
2391
|
+
};
|
|
2392
|
+
var keepsAll = (changes) => {
|
|
2393
|
+
for (let i = 1; i < changes.length; i += 2) if (changes[i] != -1) return false;
|
|
2394
|
+
return true;
|
|
2395
|
+
};
|
|
2396
|
+
var layout = (state, decoSet, changes, entry, composition, replaced) => {
|
|
2397
|
+
const previous = entry.composition;
|
|
2398
|
+
const composing = /* @__PURE__ */ new Set();
|
|
2399
|
+
for (const view of [previous, composition])
|
|
2400
|
+
if (view) for (const plot of [state.doc, view.plot, ...view.path]) composing.add(plot);
|
|
2401
|
+
return iterDeco(
|
|
2402
|
+
{
|
|
2403
|
+
deco: new DecoIterator(state, decoSet),
|
|
2404
|
+
pass: {},
|
|
2405
|
+
store: entry.store,
|
|
2406
|
+
changes,
|
|
2407
|
+
layouts: entry.layouts,
|
|
2408
|
+
atoms: entry.atoms,
|
|
2409
|
+
composition,
|
|
2410
|
+
composing,
|
|
2411
|
+
replaced,
|
|
2412
|
+
words: entry.renderer.words
|
|
2413
|
+
},
|
|
2414
|
+
state.doc,
|
|
2415
|
+
0
|
|
2416
|
+
);
|
|
2417
|
+
};
|
|
2418
|
+
var newEntry = (dom, wg, Tile, create) => {
|
|
2419
|
+
const store = { keys: /* @__PURE__ */ new WeakMap(), pending: /* @__PURE__ */ new Map() };
|
|
2420
|
+
const failures = { error: null, count: 0, built: 0 };
|
|
2421
|
+
const renderer = create(dom, {
|
|
2422
|
+
wg,
|
|
2423
|
+
store,
|
|
2424
|
+
failed: (thrown) => {
|
|
2425
|
+
failures.error = { thrown };
|
|
2426
|
+
failures.count++;
|
|
2427
|
+
}
|
|
2428
|
+
});
|
|
2429
|
+
editorOf(wg).batch = renderer.batch;
|
|
2430
|
+
const entry = {
|
|
2431
|
+
renderer,
|
|
2432
|
+
Tile,
|
|
2433
|
+
last: null,
|
|
2434
|
+
store,
|
|
2435
|
+
rendered: { texts: /* @__PURE__ */ new WeakMap(), wrappers: /* @__PURE__ */ new WeakMap() },
|
|
2436
|
+
layouts: /* @__PURE__ */ new WeakMap(),
|
|
2437
|
+
atoms: /* @__PURE__ */ new WeakSet(),
|
|
2438
|
+
composition: null,
|
|
2439
|
+
failures
|
|
2440
|
+
};
|
|
2441
|
+
roots3.set(dom, entry);
|
|
2442
|
+
return { entry, renderer };
|
|
2443
|
+
};
|
|
2444
|
+
var throwRenderError = (entry) => {
|
|
2445
|
+
const { error } = entry.failures;
|
|
2446
|
+
entry.failures.error = null;
|
|
2447
|
+
if (error) throw error.thrown;
|
|
2448
|
+
};
|
|
2449
|
+
var firstLayout = (state, entry) => {
|
|
2450
|
+
checkRenderable(state.doc, entry.renderer.words);
|
|
2451
|
+
addFreshKeys(entry.store.keys, state.doc);
|
|
2452
|
+
const decoSet = getDecoSet(state);
|
|
2453
|
+
return { state, decoSet, views: layout(state, decoSet, null, entry, null, /* @__PURE__ */ new WeakMap()) };
|
|
2454
|
+
};
|
|
2455
|
+
var RefusedUpdate = class extends Error {
|
|
2456
|
+
};
|
|
2457
|
+
var createDocTile = (Tile, create, state, dom, wg) => {
|
|
2458
|
+
if (!roots3.has(dom)) {
|
|
2459
|
+
dom.replaceChildren();
|
|
2460
|
+
newEntry(dom, wg, Tile, create);
|
|
2461
|
+
}
|
|
2462
|
+
const { decoSet, views } = firstLayout(state, rootOf(dom));
|
|
2463
|
+
return render(state, dom, decoSet, views, {
|
|
2464
|
+
wg,
|
|
2465
|
+
old: null,
|
|
2466
|
+
changes: null,
|
|
2467
|
+
dirty: /* @__PURE__ */ new Set()
|
|
2468
|
+
});
|
|
2469
|
+
};
|
|
2470
|
+
var hostDocTile = (Tile, create, state, dom, wg) => {
|
|
2471
|
+
dom.replaceChildren();
|
|
2472
|
+
const { entry, renderer } = newEntry(dom, wg, Tile, create);
|
|
2473
|
+
const laid = firstLayout(state, entry);
|
|
2474
|
+
entry.last = laid;
|
|
2475
|
+
renderer.prepare(state, laid.views);
|
|
2476
|
+
return new Tile(state, dom, null, laid.decoSet);
|
|
2477
|
+
};
|
|
2478
|
+
var mountDocTile = (dom, wg, old) => {
|
|
2479
|
+
const entry = rootOf(dom);
|
|
2480
|
+
throwRenderError(entry);
|
|
2481
|
+
const { last } = entry;
|
|
2482
|
+
if (!last) throw new Error("Nothing rendered to mount");
|
|
2483
|
+
return build(last.state, dom, last.decoSet, last.views, {
|
|
2484
|
+
wg,
|
|
2485
|
+
old: old instanceof RenderedDocTile ? old : null,
|
|
2486
|
+
changes: null,
|
|
2487
|
+
dirty: /* @__PURE__ */ new Set()
|
|
2488
|
+
});
|
|
2489
|
+
};
|
|
2490
|
+
var render = (state, dom, decoSet, views, options) => {
|
|
2491
|
+
const entry = rootOf(dom);
|
|
2492
|
+
const editor = editorOf(options.wg);
|
|
2493
|
+
const before = { state: editor.store.getState(), last: entry.last };
|
|
2494
|
+
entry.last = { state, decoSet, views };
|
|
2495
|
+
let takeBack = () => {
|
|
2496
|
+
};
|
|
2497
|
+
try {
|
|
2498
|
+
entry.renderer.commit(state, views, () => {
|
|
2499
|
+
takeBack = publishCells(entry.store);
|
|
2500
|
+
editor.publish(state);
|
|
2501
|
+
});
|
|
2502
|
+
} catch (error) {
|
|
2503
|
+
if (error instanceof RefusedUpdate) {
|
|
2504
|
+
takeBack();
|
|
2505
|
+
entry.last = before.last;
|
|
2506
|
+
editor.publish(before.state);
|
|
2507
|
+
}
|
|
2508
|
+
throw error;
|
|
2509
|
+
}
|
|
2510
|
+
throwRenderError(entry);
|
|
2511
|
+
entry.store.pending.clear();
|
|
2512
|
+
return build(state, dom, decoSet, views, options);
|
|
2513
|
+
};
|
|
2514
|
+
var build = (state, dom, decoSet, views, { wg, old, changes, dirty, composition = null }) => {
|
|
2515
|
+
const entry = rootOf(dom);
|
|
2516
|
+
entry.composition = composition;
|
|
2517
|
+
const tile = new entry.Tile(state, dom, composition?.wrapCursor ?? null, decoSet);
|
|
2518
|
+
const { words } = entry.renderer;
|
|
2519
|
+
const cursor = childCursor(dom, words);
|
|
2520
|
+
const context = {
|
|
2521
|
+
words,
|
|
2522
|
+
keys: entry.store.keys,
|
|
2523
|
+
rendered: entry.rendered,
|
|
2524
|
+
composition,
|
|
2525
|
+
wg,
|
|
2526
|
+
changes,
|
|
2527
|
+
reused: /* @__PURE__ */ new Map(),
|
|
2528
|
+
oldWidgets: !!old?.widgets,
|
|
2529
|
+
toConnect: [],
|
|
2530
|
+
taken: /* @__PURE__ */ new Set(),
|
|
2531
|
+
strays: [],
|
|
2532
|
+
dirty,
|
|
2533
|
+
changedShapes: takeShapeChanges(wg)
|
|
2534
|
+
};
|
|
2535
|
+
addChildTiles(tile, state.doc, views, cursor, context, widgetPool(old), 0, !!changes && !!old);
|
|
2536
|
+
if (holdsBlocks(state.doc)) addBlockSeparators(tile, cursor, context);
|
|
2537
|
+
finish(cursor, context);
|
|
2538
|
+
for (const stray of context.strays) if (!context.taken.has(stray)) stray.remove();
|
|
2539
|
+
takeShapeChanges(wg);
|
|
2540
|
+
tile.length = state.doc.length;
|
|
2541
|
+
tile.widgets = context.oldWidgets || context.taken.size > 0;
|
|
2542
|
+
if (wg.connected) {
|
|
2543
|
+
if (old?.widgets) for (const child of old.children) child.disconnect(context.reused);
|
|
2544
|
+
for (const widget of context.toConnect) widget.connect();
|
|
2545
|
+
}
|
|
2546
|
+
entry.failures.built = entry.failures.count;
|
|
2547
|
+
return synced(tile);
|
|
2548
|
+
};
|
|
2549
|
+
var RenderedDocTile = class extends DocTile {
|
|
2550
|
+
// Whether this tree, or one before it, held widget DOM: those get
|
|
2551
|
+
// disconnected when dropped.
|
|
2552
|
+
widgets = false;
|
|
2553
|
+
// Wordgard's own `update` diffs the decorations and calls this with the
|
|
2554
|
+
// ranges to redraw, decoration changes included.
|
|
2555
|
+
updateRanges(state, decoSet, ranges, wg, info) {
|
|
2556
|
+
const entry = rootOf(this.dom);
|
|
2557
|
+
const changes = entry.renderer.redrawsAll(this.state, state) ? [this.state.doc.length, state.doc.length] : ranges;
|
|
2558
|
+
const editor = editorOf(wg);
|
|
2559
|
+
if (editor.measures.size) wg.scheduleDOMRead(editor.readMeasures);
|
|
2560
|
+
if (entry.failures.built != entry.failures.count) {
|
|
2561
|
+
entry.failures.error = null;
|
|
2562
|
+
checkRenderable(state.doc, entry.renderer.words);
|
|
2563
|
+
addFreshKeys(entry.store.keys, state.doc);
|
|
2564
|
+
entry.store.pending.clear();
|
|
2565
|
+
const views2 = layout(state, decoSet, null, entry, null, /* @__PURE__ */ new WeakMap());
|
|
2566
|
+
return render(state, this.dom, decoSet, views2, {
|
|
2567
|
+
wg,
|
|
2568
|
+
old: this,
|
|
2569
|
+
changes: null,
|
|
2570
|
+
dirty: /* @__PURE__ */ new Set()
|
|
2571
|
+
});
|
|
2572
|
+
}
|
|
2573
|
+
const previous = entry.composition;
|
|
2574
|
+
if (keepsAll(changes) && !info && !previous) {
|
|
2575
|
+
entry.renderer.batch(() => editor.publish(state));
|
|
2576
|
+
throwRenderError(entry);
|
|
2577
|
+
rebuildRoots(takeShapeChanges(wg), wg);
|
|
2578
|
+
return this;
|
|
2579
|
+
}
|
|
2580
|
+
checkRenderable(state.doc, entry.renderer.words);
|
|
2581
|
+
const composition = info ? compositionView(state.doc, info, changes, entry.rendered) : null;
|
|
2582
|
+
const replaced = mapKeys(entry.store.keys, this.state.doc, state.doc, changes);
|
|
2583
|
+
const views = layout(state, decoSet, changes, entry, composition, replaced);
|
|
2584
|
+
const node = composition?.node ?? null;
|
|
2585
|
+
const dirty = restore(this, node);
|
|
2586
|
+
if (previous?.node && previous.node != node) {
|
|
2587
|
+
if (previous.rendered) previous.node.nodeValue = previous.rendered.text;
|
|
2588
|
+
else previous.node.remove();
|
|
2589
|
+
}
|
|
2590
|
+
return render(state, this.dom, decoSet, views, {
|
|
2591
|
+
wg,
|
|
2592
|
+
old: this,
|
|
2593
|
+
changes,
|
|
2594
|
+
dirty,
|
|
2595
|
+
composition
|
|
2596
|
+
});
|
|
2597
|
+
}
|
|
2598
|
+
};
|
|
2599
|
+
var contentTile = (tile) => {
|
|
2600
|
+
if (tile.flags & TileFlag.PlotContent) return tile;
|
|
2601
|
+
for (const child of tile.children) {
|
|
2602
|
+
const found = child.isNodeInner ? contentTile(child) : null;
|
|
2603
|
+
if (found) return found;
|
|
2604
|
+
}
|
|
2605
|
+
return null;
|
|
2606
|
+
};
|
|
2607
|
+
var rebuildContext = (wg) => {
|
|
2608
|
+
const entry = rootOf(wg.contentDOM);
|
|
2609
|
+
const length = entry.last?.state.doc.length;
|
|
2610
|
+
return {
|
|
2611
|
+
words: entry.renderer.words,
|
|
2612
|
+
keys: entry.store.keys,
|
|
2613
|
+
rendered: entry.rendered,
|
|
2614
|
+
composition: entry.composition,
|
|
2615
|
+
wg,
|
|
2616
|
+
// Nothing changed in the document: widgets map to where they were.
|
|
2617
|
+
changes: length === void 0 ? null : [length, -1],
|
|
2618
|
+
reused: /* @__PURE__ */ new Map(),
|
|
2619
|
+
oldWidgets: false,
|
|
2620
|
+
toConnect: [],
|
|
2621
|
+
taken: /* @__PURE__ */ new Set(),
|
|
2622
|
+
strays: [],
|
|
2623
|
+
dirty: /* @__PURE__ */ new Set(),
|
|
2624
|
+
changedShapes: /* @__PURE__ */ new Set()
|
|
2625
|
+
};
|
|
2626
|
+
};
|
|
2627
|
+
var keepContent = (old, tile, cursor, context) => {
|
|
2628
|
+
for (const child of old.children) {
|
|
2629
|
+
if (isChromeTile(child)) continue;
|
|
2630
|
+
if (widgetDOM.has(child.dom)) cursor.place(child.dom);
|
|
2631
|
+
else if (cursor.next() != child.dom)
|
|
2632
|
+
throw new Error(`A ${context.words.name} decoration's content moved`);
|
|
2633
|
+
context.reused.set(child, Reused.Full);
|
|
2634
|
+
tile.addChild(child);
|
|
2635
|
+
}
|
|
2636
|
+
};
|
|
2637
|
+
var replaceTile = (old, tile, context) => {
|
|
2638
|
+
const { parent } = old;
|
|
2639
|
+
if (!parent) return;
|
|
2640
|
+
parent.children[parent.children.indexOf(old)] = tile;
|
|
2641
|
+
tile.parent = parent;
|
|
2642
|
+
old.parent = null;
|
|
2643
|
+
for (const stray of context.strays) if (!context.taken.has(stray)) stray.remove();
|
|
2644
|
+
if (context.wg.connected) {
|
|
2645
|
+
old.disconnect(context.reused);
|
|
2646
|
+
for (const widget of context.toConnect) widget.connect();
|
|
2647
|
+
}
|
|
2648
|
+
};
|
|
2649
|
+
var rebuildShape = (old, wg) => {
|
|
2650
|
+
const view = tileViews.get(old);
|
|
2651
|
+
const dom = old.dom instanceof Element ? currentRoot(old.dom) : old.dom;
|
|
2652
|
+
if (!dom.isConnected) return;
|
|
2653
|
+
if (!view || !old.parent || !(view.shape instanceof Elt8)) return;
|
|
2654
|
+
const context = rebuildContext(wg);
|
|
2655
|
+
const { node, shape, children } = view;
|
|
2656
|
+
const oldContent = contentTile(old);
|
|
2657
|
+
const contentStart = old.posBefore + 1;
|
|
2658
|
+
const fill = (tile2, cursor, pool) => {
|
|
2659
|
+
if (oldContent && oldContent.dom == tile2.dom) keepContent(oldContent, tile2, cursor, context);
|
|
2660
|
+
else if (node.isPlot && children)
|
|
2661
|
+
addChildTiles(tile2, node, children, cursor, context, pool, contentStart, false);
|
|
2662
|
+
};
|
|
2663
|
+
const tag = node.isPlot ? node.tag : node;
|
|
2664
|
+
const tile = shapeTile(shape, tag, node.length, elementOf(dom, node.name), {
|
|
2665
|
+
inEditable: true,
|
|
2666
|
+
container: isContainer(node),
|
|
2667
|
+
blocks: holdsBlocks(node),
|
|
2668
|
+
afterContent: 0,
|
|
2669
|
+
keepShape: true,
|
|
2670
|
+
fill,
|
|
2671
|
+
context
|
|
2672
|
+
});
|
|
2673
|
+
tileViews.set(tile, view);
|
|
2674
|
+
viewTiles.set(view, tile);
|
|
2675
|
+
replaceTile(old, tile, context);
|
|
2676
|
+
};
|
|
2677
|
+
var rebuildMark = (old, wg) => {
|
|
2678
|
+
const found = markViews.get(old);
|
|
2679
|
+
const mark = markOfTile.get(old);
|
|
2680
|
+
const dom = old.dom instanceof Element ? currentRoot(old.dom) : old.dom;
|
|
2681
|
+
if (!dom.isConnected || !found || !mark || !old.parent) return;
|
|
2682
|
+
const { view, plot } = found;
|
|
2683
|
+
const context = rebuildContext(wg);
|
|
2684
|
+
const oldContent = markContents.get(old);
|
|
2685
|
+
const pos = old.posBefore;
|
|
2686
|
+
const fill = (tile2, cursor) => {
|
|
2687
|
+
if (oldContent && oldContent.dom == tile2.dom) {
|
|
2688
|
+
keepContent(oldContent, tile2, cursor, context);
|
|
2689
|
+
return;
|
|
2690
|
+
}
|
|
2691
|
+
addChildTiles(
|
|
2692
|
+
tile2,
|
|
2693
|
+
plot,
|
|
2694
|
+
view.children,
|
|
2695
|
+
cursor,
|
|
2696
|
+
context,
|
|
2697
|
+
widgetPool(oldContent ?? null),
|
|
2698
|
+
pos,
|
|
2699
|
+
false
|
|
2700
|
+
);
|
|
2701
|
+
if (holdsBlocks(plot)) addBlockSeparators(tile2, cursor, context);
|
|
2702
|
+
};
|
|
2703
|
+
const flags = old.flags & (TileFlag.Wrapper | TileFlag.Spanning);
|
|
2704
|
+
const tile = reactMarkTile(view, mark, flags, elementOf(dom, "a mark"), context, fill, false);
|
|
2705
|
+
markViews.set(tile, found);
|
|
2706
|
+
replaceTile(old, tile, context);
|
|
2707
|
+
};
|
|
2708
|
+
var rebuildWidget = (old, wg) => {
|
|
2709
|
+
const dom = old.dom instanceof Element ? currentRoot(old.dom) : old.dom;
|
|
2710
|
+
if (dom == old.dom || !(dom instanceof Element) || !dom.isConnected || !old.parent) return;
|
|
2711
|
+
const { WidgetTile } = getTileClasses();
|
|
2712
|
+
const context = rebuildContext(wg);
|
|
2713
|
+
const flags = old.flags & ~TileFlag.Uneditable | uneditableFlag(old.widget, dom);
|
|
2714
|
+
const tile = synced(new WidgetTile(old.widget, old.tag, flags, dom, old.length));
|
|
2715
|
+
const view = tileViews.get(old);
|
|
2716
|
+
if (view) {
|
|
2717
|
+
tileViews.set(tile, view);
|
|
2718
|
+
viewTiles.set(view, tile);
|
|
2719
|
+
}
|
|
2720
|
+
context.toConnect.push(tile);
|
|
2721
|
+
replaceTile(old, tile, context);
|
|
2722
|
+
};
|
|
2723
|
+
var shapeObservers = /* @__PURE__ */ new WeakMap();
|
|
2724
|
+
var settle = (wg) => roots3.get(wg.contentDOM)?.renderer.settle?.() ?? true;
|
|
2725
|
+
var observeShapes = (wg, queue) => {
|
|
2726
|
+
const changed = /* @__PURE__ */ new Set();
|
|
2727
|
+
class ShapeObserver extends MutationObserver {
|
|
2728
|
+
takeRecords() {
|
|
2729
|
+
const settled = settle(wg);
|
|
2730
|
+
const kept = sortRecords(super.takeRecords(), changed, holdsText);
|
|
2731
|
+
if (settled) rebuildShapes(wg);
|
|
2732
|
+
return kept;
|
|
2733
|
+
}
|
|
2734
|
+
// Records not delivered yet, unsorted.
|
|
2735
|
+
pending() {
|
|
2736
|
+
return super.takeRecords();
|
|
2737
|
+
}
|
|
2738
|
+
// What the rebuild itself moved (widget DOM, strays), not an edit.
|
|
2739
|
+
discard() {
|
|
2740
|
+
super.takeRecords();
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
const observer = new ShapeObserver((records) => {
|
|
2744
|
+
const settled = settle(wg);
|
|
2745
|
+
const kept = sortRecords([...records, ...observer.pending()], changed, holdsText);
|
|
2746
|
+
if (kept.length) queue(kept);
|
|
2747
|
+
if (settled) rebuildShapes(wg);
|
|
2748
|
+
});
|
|
2749
|
+
shapeObservers.set(wg, { observer, changed, discard: () => observer.discard() });
|
|
2750
|
+
return observer;
|
|
2751
|
+
};
|
|
2752
|
+
var takeShapeChanges = (wg) => {
|
|
2753
|
+
const found = shapeObservers.get(wg);
|
|
2754
|
+
if (!found) return /* @__PURE__ */ new Set();
|
|
2755
|
+
found.observer.takeRecords();
|
|
2756
|
+
const changed = new Set(found.changed);
|
|
2757
|
+
found.changed.clear();
|
|
2758
|
+
return changed;
|
|
2759
|
+
};
|
|
2760
|
+
var rebuildShapes = (wg) => {
|
|
2761
|
+
const found = shapeObservers.get(wg);
|
|
2762
|
+
if (!found || editorOf(wg).flushing || !roots3.has(wg.contentDOM)) return;
|
|
2763
|
+
if (!found.changed.size) return;
|
|
2764
|
+
rebuildRoots(found.changed, wg);
|
|
2765
|
+
found.changed.clear();
|
|
2766
|
+
found.discard();
|
|
2767
|
+
};
|
|
2768
|
+
var rebuildRoots = (changed, wg) => {
|
|
2769
|
+
const { WidgetTile } = getTileClasses();
|
|
2770
|
+
const tiles = /* @__PURE__ */ new Set();
|
|
2771
|
+
for (const root of changed) {
|
|
2772
|
+
if (!root.isConnected) continue;
|
|
2773
|
+
const tiled = tiledRoot(root);
|
|
2774
|
+
const own = tiled.wgTile;
|
|
2775
|
+
const alone = own && (markOfTile.has(own) || own instanceof WidgetTile && tiled != root) ? own : null;
|
|
2776
|
+
const tile = alone ?? nodeTileOf(tiled);
|
|
2777
|
+
if (tile) tiles.add(tile);
|
|
2778
|
+
}
|
|
2779
|
+
const depth = (tile) => {
|
|
2780
|
+
let found = 0;
|
|
2781
|
+
for (let at = tile.parent; at; at = at.parent) found++;
|
|
2782
|
+
return found;
|
|
2783
|
+
};
|
|
2784
|
+
for (const tile of [...tiles].sort((a, b) => depth(b) - depth(a)))
|
|
2785
|
+
if (markOfTile.has(tile)) rebuildMark(tile, wg);
|
|
2786
|
+
else if (tile instanceof WidgetTile) rebuildWidget(tile, wg);
|
|
2787
|
+
else rebuildShape(tile, wg);
|
|
2788
|
+
};
|
|
2789
|
+
|
|
2790
|
+
// src/RenderedWordgard.ts
|
|
2791
|
+
var retry = () => {
|
|
2792
|
+
};
|
|
2793
|
+
var RenderedWordgard = class extends Wordgard3 {
|
|
2794
|
+
constructor(spec, tiles, hosted) {
|
|
2795
|
+
const create = DocTile.create;
|
|
2796
|
+
const Self = new.target;
|
|
2797
|
+
const self = { editor: null };
|
|
2798
|
+
DocTile.create = (state, dom, wg) => {
|
|
2799
|
+
if (!(wg instanceof Self)) return create(state, dom, wg);
|
|
2800
|
+
DocTile.create = create;
|
|
2801
|
+
if (!hosted) return tiles.create(state, dom, wg);
|
|
2802
|
+
return tiles.host(state, dom, wg, () => {
|
|
2803
|
+
const { editor } = self;
|
|
2804
|
+
if (editor)
|
|
2805
|
+
editor.docTile = editor.observer.ignore(
|
|
2806
|
+
() => mountDocTile(editor.contentDOM, editor, editor.docTile)
|
|
2807
|
+
);
|
|
2808
|
+
});
|
|
2809
|
+
};
|
|
2810
|
+
const events = spec.state ? {
|
|
2811
|
+
state: spec.state.update({ effects: GardState4.appendConfig.of(editorEventHandlers) }).state
|
|
2812
|
+
} : { config: [editorEventHandlers, spec.config ?? []] };
|
|
2813
|
+
try {
|
|
2814
|
+
super({ ...spec, ...events, parent: void 0 });
|
|
2815
|
+
} finally {
|
|
2816
|
+
DocTile.create = create;
|
|
2817
|
+
}
|
|
2818
|
+
self.editor = this;
|
|
2819
|
+
this.observer.observer = observeShapes(this, (records) => {
|
|
2820
|
+
this.observer.queue.push(...records);
|
|
2821
|
+
this.scheduleFlush();
|
|
2822
|
+
});
|
|
2823
|
+
spec.parent?.appendChild(this.dom);
|
|
2824
|
+
}
|
|
2825
|
+
// Wordgard's flush runs from an animation frame and from the methods that
|
|
2826
|
+
// need an up-to-date DOM; useEditorEffect's effects run after it. A refused
|
|
2827
|
+
// update leaves Wordgard's transactions pending with no frame requested for
|
|
2828
|
+
// them, so one is.
|
|
2829
|
+
flush() {
|
|
2830
|
+
const editor = editorOf(this);
|
|
2831
|
+
this.observer.queue.push(...this.observer.observer.takeRecords());
|
|
2832
|
+
editor.flushing = true;
|
|
2833
|
+
try {
|
|
2834
|
+
super.flush();
|
|
2835
|
+
} catch (error) {
|
|
2836
|
+
if (error instanceof RefusedUpdate) this.scheduleDOMRead(retry);
|
|
2837
|
+
throw error;
|
|
2838
|
+
} finally {
|
|
2839
|
+
editor.flushing = false;
|
|
2840
|
+
}
|
|
2841
|
+
editor.runEffects();
|
|
2842
|
+
if (!hasEditorEventHandlers(this.state))
|
|
2843
|
+
this.dispatch({ effects: GardState4.appendConfig.of(editorEventHandlers) });
|
|
2844
|
+
}
|
|
2845
|
+
};
|
|
2846
|
+
export {
|
|
2847
|
+
DocTile,
|
|
2848
|
+
RefusedUpdate,
|
|
2849
|
+
RenderedDocTile,
|
|
2850
|
+
RenderedWordgard,
|
|
2851
|
+
TileFlag,
|
|
2852
|
+
VOID,
|
|
2853
|
+
atomRef,
|
|
2854
|
+
attributedWidget,
|
|
2855
|
+
attrsToRecord,
|
|
2856
|
+
cellOf,
|
|
2857
|
+
childViewKeys,
|
|
2858
|
+
createDecorationHandlers,
|
|
2859
|
+
createDocTile,
|
|
2860
|
+
createNodeWrappers,
|
|
2861
|
+
createShapeHandle,
|
|
2862
|
+
createWrapperHandle,
|
|
2863
|
+
decorationRoot,
|
|
2864
|
+
defineMarkShape,
|
|
2865
|
+
defineNodeWrappers,
|
|
2866
|
+
defineShape,
|
|
2867
|
+
defineWidgetType,
|
|
2868
|
+
defineWrapper,
|
|
2869
|
+
drawnRoot,
|
|
2870
|
+
editorOf,
|
|
2871
|
+
findEditor,
|
|
2872
|
+
hostDocTile,
|
|
2873
|
+
idOf,
|
|
2874
|
+
isComponentMark,
|
|
2875
|
+
isComponentShape,
|
|
2876
|
+
isComponentWidget,
|
|
2877
|
+
isComponentWrapper,
|
|
2878
|
+
isTrailingBreak,
|
|
2879
|
+
listenersOf,
|
|
2880
|
+
localTagName,
|
|
2881
|
+
namespaced,
|
|
2882
|
+
nodePos,
|
|
2883
|
+
nodeViewOf,
|
|
2884
|
+
rootReplaced,
|
|
2885
|
+
shapeAttributes,
|
|
2886
|
+
shapeOf2 as shapeOf,
|
|
2887
|
+
staticCopy,
|
|
2888
|
+
uneditable,
|
|
2889
|
+
viewRoot,
|
|
2890
|
+
widgetRoot
|
|
2891
|
+
};
|