@vite-hub/realtime 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -10
- package/dist/application-path-CJblgbWK.js +6 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.js +142 -0
- package/dist/conflict-CmZizTYl.d.ts +17 -0
- package/dist/conflict-rguoaHlE.js +34 -0
- package/dist/document-BFaqcCAL.js +2029 -0
- package/dist/document-CtY6umWY.d.ts +16 -0
- package/dist/durable-key-tnSw58ZK.js +8 -0
- package/dist/error-diagnostics-Cvm6vRXQ.js +73 -0
- package/dist/index.d.ts +17 -4
- package/dist/index.js +3 -1
- package/dist/layout-Br1aOL7o.js +153 -0
- package/dist/layout-WNWejwOY.d.ts +18 -0
- package/dist/protocol-9ZDLEcSS.js +146 -0
- package/dist/server.d.ts +95 -26
- package/dist/server.js +1596 -497
- package/dist/types-Bw6-42Se.d.ts +215 -0
- package/dist/vite.d.ts +80 -6
- package/dist/vite.js +582 -38
- package/dist/vue.d.ts +67 -3808
- package/dist/vue.js +338 -64
- package/package.json +61 -32
- package/dist/editor-extensions-CZEzrJ-t.js +0 -4860
- package/dist/types-BSDtKw5d.d.ts +0 -42
|
@@ -1,4860 +0,0 @@
|
|
|
1
|
-
import { Node } from "@tiptap/core";
|
|
2
|
-
import * as Y from "yjs";
|
|
3
|
-
import { ContentType, Item, Text, UndoManager, XmlElement } from "yjs";
|
|
4
|
-
import "y-protocols/awareness";
|
|
5
|
-
import { createMutex } from "lib0/mutex";
|
|
6
|
-
import * as math from "lib0/math";
|
|
7
|
-
import * as object from "lib0/object";
|
|
8
|
-
import * as set from "lib0/set";
|
|
9
|
-
import { simpleDiff } from "lib0/diff";
|
|
10
|
-
import * as error from "lib0/error";
|
|
11
|
-
import * as random from "lib0/random";
|
|
12
|
-
import * as environment from "lib0/environment";
|
|
13
|
-
import * as dom from "lib0/dom";
|
|
14
|
-
import * as eventloop from "lib0/eventloop";
|
|
15
|
-
import * as map from "lib0/map";
|
|
16
|
-
import * as sha256 from "lib0/hash/sha256";
|
|
17
|
-
import * as buf from "lib0/buffer";
|
|
18
|
-
import * as decoding from "lib0/decoding";
|
|
19
|
-
import * as encoding from "lib0/encoding";
|
|
20
|
-
import Image from "@tiptap/extension-image";
|
|
21
|
-
import Document from "@tiptap/extension-document";
|
|
22
|
-
import { TableKit } from "@tiptap/extension-table";
|
|
23
|
-
import { Markdown } from "@tiptap/markdown";
|
|
24
|
-
import StarterKit from "@tiptap/starter-kit";
|
|
25
|
-
import { Marked } from "marked";
|
|
26
|
-
//#region ../../node_modules/.pnpm/prosemirror-model@1.25.11/node_modules/prosemirror-model/dist/index.js
|
|
27
|
-
function findDiffStart(a, b, pos) {
|
|
28
|
-
for (let i = 0;; i++) {
|
|
29
|
-
if (i == a.childCount || i == b.childCount) return a.childCount == b.childCount ? null : pos;
|
|
30
|
-
let childA = a.child(i), childB = b.child(i);
|
|
31
|
-
if (childA == childB) {
|
|
32
|
-
pos += childA.nodeSize;
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
if (!childA.sameMarkup(childB)) return pos;
|
|
36
|
-
if (childA.isText && childA.text != childB.text) {
|
|
37
|
-
let tA = childA.text, tB = childB.text, j = 0;
|
|
38
|
-
for (; tA[j] == tB[j]; j++) pos++;
|
|
39
|
-
if (j && j < tA.length && j < tB.length && surrogateHigh(tA.charCodeAt(j - 1)) && surrogateLow(tA.charCodeAt(j))) pos--;
|
|
40
|
-
return pos;
|
|
41
|
-
}
|
|
42
|
-
if (childA.content.size || childB.content.size) {
|
|
43
|
-
let inner = findDiffStart(childA.content, childB.content, pos + 1);
|
|
44
|
-
if (inner != null) return inner;
|
|
45
|
-
}
|
|
46
|
-
pos += childA.nodeSize;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function findDiffEnd(a, b, posA, posB) {
|
|
50
|
-
for (let iA = a.childCount, iB = b.childCount;;) {
|
|
51
|
-
if (iA == 0 || iB == 0) return iA == iB ? null : {
|
|
52
|
-
a: posA,
|
|
53
|
-
b: posB
|
|
54
|
-
};
|
|
55
|
-
let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
|
|
56
|
-
if (childA == childB) {
|
|
57
|
-
posA -= size;
|
|
58
|
-
posB -= size;
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
if (!childA.sameMarkup(childB)) return {
|
|
62
|
-
a: posA,
|
|
63
|
-
b: posB
|
|
64
|
-
};
|
|
65
|
-
if (childA.isText && childA.text != childB.text) {
|
|
66
|
-
let tA = childA.text, tB = childB.text, iA = tA.length, iB = tB.length;
|
|
67
|
-
while (iA > 0 && iB > 0 && tA[iA - 1] == tB[iB - 1]) {
|
|
68
|
-
iA--;
|
|
69
|
-
iB--;
|
|
70
|
-
posA--;
|
|
71
|
-
posB--;
|
|
72
|
-
}
|
|
73
|
-
if (iA && iB && iA < tA.length && surrogateHigh(tA.charCodeAt(iA - 1)) && surrogateLow(tA.charCodeAt(iA))) {
|
|
74
|
-
posA++;
|
|
75
|
-
posB++;
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
a: posA,
|
|
79
|
-
b: posB
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
if (childA.content.size || childB.content.size) {
|
|
83
|
-
let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
|
|
84
|
-
if (inner) return inner;
|
|
85
|
-
}
|
|
86
|
-
posA -= size;
|
|
87
|
-
posB -= size;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function surrogateLow(ch) {
|
|
91
|
-
return ch >= 56320 && ch < 57344;
|
|
92
|
-
}
|
|
93
|
-
function surrogateHigh(ch) {
|
|
94
|
-
return ch >= 55296 && ch < 56320;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
A fragment represents a node's collection of child nodes.
|
|
98
|
-
|
|
99
|
-
Like nodes, fragments are persistent data structures, and you
|
|
100
|
-
should not mutate them or their content. Rather, you create new
|
|
101
|
-
instances whenever needed. The API tries to make this easy.
|
|
102
|
-
*/
|
|
103
|
-
var Fragment = class Fragment {
|
|
104
|
-
/**
|
|
105
|
-
@internal
|
|
106
|
-
*/
|
|
107
|
-
constructor(content, size) {
|
|
108
|
-
this.content = content;
|
|
109
|
-
this.size = size || 0;
|
|
110
|
-
if (size == null) for (let i = 0; i < content.length; i++) this.size += content[i].nodeSize;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
Invoke a callback for all descendant nodes between the given two
|
|
114
|
-
positions (relative to start of this fragment). Doesn't descend
|
|
115
|
-
into a node when the callback returns `false`.
|
|
116
|
-
*/
|
|
117
|
-
nodesBetween(from, to, f, nodeStart = 0, parent) {
|
|
118
|
-
for (let i = 0, pos = 0; pos < to; i++) {
|
|
119
|
-
let child = this.content[i], end = pos + child.nodeSize;
|
|
120
|
-
if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
|
|
121
|
-
let start = pos + 1;
|
|
122
|
-
child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
|
|
123
|
-
}
|
|
124
|
-
pos = end;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
Call the given callback for every descendant node. `pos` will be
|
|
129
|
-
relative to the start of the fragment. The callback may return
|
|
130
|
-
`false` to prevent traversal of a given node's children.
|
|
131
|
-
*/
|
|
132
|
-
descendants(f) {
|
|
133
|
-
this.nodesBetween(0, this.size, f);
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
Extract the text between `from` and `to`. See the same method on
|
|
137
|
-
[`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
|
|
138
|
-
*/
|
|
139
|
-
textBetween(from, to, blockSeparator, leafText) {
|
|
140
|
-
let text = "", first = true;
|
|
141
|
-
this.nodesBetween(from, to, (node, pos) => {
|
|
142
|
-
let nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos) : !node.isLeaf ? "" : leafText ? typeof leafText === "function" ? leafText(node) : leafText : node.type.spec.leafText ? node.type.spec.leafText(node) : "";
|
|
143
|
-
if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) if (first) first = false;
|
|
144
|
-
else text += blockSeparator;
|
|
145
|
-
text += nodeText;
|
|
146
|
-
}, 0);
|
|
147
|
-
return text;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
Create a new fragment containing the combined content of this
|
|
151
|
-
fragment and the other.
|
|
152
|
-
*/
|
|
153
|
-
append(other) {
|
|
154
|
-
if (!other.size) return this;
|
|
155
|
-
if (!this.size) return other;
|
|
156
|
-
let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
|
|
157
|
-
if (last.isText && last.sameMarkup(first)) {
|
|
158
|
-
content[content.length - 1] = last.withText(last.text + first.text);
|
|
159
|
-
i = 1;
|
|
160
|
-
}
|
|
161
|
-
for (; i < other.content.length; i++) content.push(other.content[i]);
|
|
162
|
-
return new Fragment(content, this.size + other.size);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
Cut out the sub-fragment between the two given positions.
|
|
166
|
-
*/
|
|
167
|
-
cut(from, to = this.size) {
|
|
168
|
-
if (from == 0 && to == this.size) return this;
|
|
169
|
-
let result = [], size = 0;
|
|
170
|
-
if (to > from) for (let i = 0, pos = 0; pos < to; i++) {
|
|
171
|
-
let child = this.content[i], end = pos + child.nodeSize;
|
|
172
|
-
if (end > from) {
|
|
173
|
-
if (pos < from || end > to) if (child.isText) child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
|
|
174
|
-
else child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
|
|
175
|
-
result.push(child);
|
|
176
|
-
size += child.nodeSize;
|
|
177
|
-
}
|
|
178
|
-
pos = end;
|
|
179
|
-
}
|
|
180
|
-
return new Fragment(result, size);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
@internal
|
|
184
|
-
*/
|
|
185
|
-
cutByIndex(from, to) {
|
|
186
|
-
if (from == to) return Fragment.empty;
|
|
187
|
-
if (from == 0 && to == this.content.length) return this;
|
|
188
|
-
return new Fragment(this.content.slice(from, to));
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
Create a new fragment in which the node at the given index is
|
|
192
|
-
replaced by the given node.
|
|
193
|
-
*/
|
|
194
|
-
replaceChild(index, node) {
|
|
195
|
-
let current = this.content[index];
|
|
196
|
-
if (current == node) return this;
|
|
197
|
-
let copy = this.content.slice();
|
|
198
|
-
let size = this.size + node.nodeSize - current.nodeSize;
|
|
199
|
-
copy[index] = node;
|
|
200
|
-
return new Fragment(copy, size);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
Create a new fragment by prepending the given node to this
|
|
204
|
-
fragment.
|
|
205
|
-
*/
|
|
206
|
-
addToStart(node) {
|
|
207
|
-
return new Fragment([node].concat(this.content), this.size + node.nodeSize);
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
Create a new fragment by appending the given node to this
|
|
211
|
-
fragment.
|
|
212
|
-
*/
|
|
213
|
-
addToEnd(node) {
|
|
214
|
-
return new Fragment(this.content.concat(node), this.size + node.nodeSize);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
Compare this fragment to another one.
|
|
218
|
-
*/
|
|
219
|
-
eq(other) {
|
|
220
|
-
if (this.content.length != other.content.length) return false;
|
|
221
|
-
for (let i = 0; i < this.content.length; i++) if (!this.content[i].eq(other.content[i])) return false;
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
The first child of the fragment, or `null` if it is empty.
|
|
226
|
-
*/
|
|
227
|
-
get firstChild() {
|
|
228
|
-
return this.content.length ? this.content[0] : null;
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
The last child of the fragment, or `null` if it is empty.
|
|
232
|
-
*/
|
|
233
|
-
get lastChild() {
|
|
234
|
-
return this.content.length ? this.content[this.content.length - 1] : null;
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
The number of child nodes in this fragment.
|
|
238
|
-
*/
|
|
239
|
-
get childCount() {
|
|
240
|
-
return this.content.length;
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
Get the child node at the given index. Raise an error when the
|
|
244
|
-
index is out of range.
|
|
245
|
-
*/
|
|
246
|
-
child(index) {
|
|
247
|
-
let found = this.content[index];
|
|
248
|
-
if (!found) throw new RangeError("Index " + index + " out of range for " + this);
|
|
249
|
-
return found;
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
Get the child node at the given index, if it exists.
|
|
253
|
-
*/
|
|
254
|
-
maybeChild(index) {
|
|
255
|
-
return this.content[index] || null;
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
Call `f` for every child node, passing the node, its offset
|
|
259
|
-
into this parent node, and its index.
|
|
260
|
-
*/
|
|
261
|
-
forEach(f) {
|
|
262
|
-
for (let i = 0, p = 0; i < this.content.length; i++) {
|
|
263
|
-
let child = this.content[i];
|
|
264
|
-
f(child, p, i);
|
|
265
|
-
p += child.nodeSize;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
Find the first position at which this fragment and another
|
|
270
|
-
fragment differ, or `null` if they are the same.
|
|
271
|
-
*/
|
|
272
|
-
findDiffStart(other, pos = 0) {
|
|
273
|
-
return findDiffStart(this, other, pos);
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
Find the first position, searching from the end, at which this
|
|
277
|
-
fragment and the given fragment differ, or `null` if they are
|
|
278
|
-
the same. Since this position will not be the same in both
|
|
279
|
-
nodes, an object with two separate positions is returned.
|
|
280
|
-
*/
|
|
281
|
-
findDiffEnd(other, pos = this.size, otherPos = other.size) {
|
|
282
|
-
return findDiffEnd(this, other, pos, otherPos);
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
Find the index and inner offset corresponding to a given relative
|
|
286
|
-
position in this fragment. The result object will be reused
|
|
287
|
-
(overwritten) the next time the function is called. @internal
|
|
288
|
-
*/
|
|
289
|
-
findIndex(pos) {
|
|
290
|
-
if (pos == 0) return retIndex(0, pos);
|
|
291
|
-
if (pos == this.size) return retIndex(this.content.length, pos);
|
|
292
|
-
if (pos > this.size || pos < 0) throw new RangeError(`Position ${pos} outside of fragment (${this})`);
|
|
293
|
-
for (let i = 0, curPos = 0;; i++) {
|
|
294
|
-
let cur = this.child(i), end = curPos + cur.nodeSize;
|
|
295
|
-
if (end >= pos) {
|
|
296
|
-
if (end == pos) return retIndex(i + 1, end);
|
|
297
|
-
return retIndex(i, curPos);
|
|
298
|
-
}
|
|
299
|
-
curPos = end;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
Return a debugging string that describes this fragment.
|
|
304
|
-
*/
|
|
305
|
-
toString() {
|
|
306
|
-
return "<" + this.toStringInner() + ">";
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
@internal
|
|
310
|
-
*/
|
|
311
|
-
toStringInner() {
|
|
312
|
-
return this.content.join(", ");
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
Create a JSON-serializeable representation of this fragment.
|
|
316
|
-
*/
|
|
317
|
-
toJSON() {
|
|
318
|
-
return this.content.length ? this.content.map((n) => n.toJSON()) : null;
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
Deserialize a fragment from its JSON representation.
|
|
322
|
-
*/
|
|
323
|
-
static fromJSON(schema, value) {
|
|
324
|
-
if (!value) return Fragment.empty;
|
|
325
|
-
if (!Array.isArray(value)) throw new RangeError("Invalid input for Fragment.fromJSON");
|
|
326
|
-
return Fragment.fromArray(value.map(schema.nodeFromJSON));
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
Build a fragment from an array of nodes. Ensures that adjacent
|
|
330
|
-
text nodes with the same marks are joined together.
|
|
331
|
-
*/
|
|
332
|
-
static fromArray(array) {
|
|
333
|
-
if (!array.length) return Fragment.empty;
|
|
334
|
-
let joined, size = 0;
|
|
335
|
-
for (let i = 0; i < array.length; i++) {
|
|
336
|
-
let node = array[i];
|
|
337
|
-
size += node.nodeSize;
|
|
338
|
-
if (i && node.isText && array[i - 1].sameMarkup(node)) {
|
|
339
|
-
if (!joined) joined = array.slice(0, i);
|
|
340
|
-
joined[joined.length - 1] = node.withText(joined[joined.length - 1].text + node.text);
|
|
341
|
-
} else if (joined) joined.push(node);
|
|
342
|
-
}
|
|
343
|
-
return new Fragment(joined || array, size);
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
Create a fragment from something that can be interpreted as a
|
|
347
|
-
set of nodes. For `null`, it returns the empty fragment. For a
|
|
348
|
-
fragment, the fragment itself. For a node or array of nodes, a
|
|
349
|
-
fragment containing those nodes.
|
|
350
|
-
*/
|
|
351
|
-
static from(nodes) {
|
|
352
|
-
if (!nodes) return Fragment.empty;
|
|
353
|
-
if (nodes instanceof Fragment) return nodes;
|
|
354
|
-
if (Array.isArray(nodes)) return this.fromArray(nodes);
|
|
355
|
-
if (nodes.attrs) return new Fragment([nodes], nodes.nodeSize);
|
|
356
|
-
throw new RangeError("Can not convert " + nodes + " to a Fragment" + (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
/**
|
|
360
|
-
An empty fragment. Intended to be reused whenever a node doesn't
|
|
361
|
-
contain anything (rather than allocating a new empty fragment for
|
|
362
|
-
each leaf node).
|
|
363
|
-
*/
|
|
364
|
-
Fragment.empty = new Fragment([], 0);
|
|
365
|
-
const found = {
|
|
366
|
-
index: 0,
|
|
367
|
-
offset: 0
|
|
368
|
-
};
|
|
369
|
-
function retIndex(index, offset) {
|
|
370
|
-
found.index = index;
|
|
371
|
-
found.offset = offset;
|
|
372
|
-
return found;
|
|
373
|
-
}
|
|
374
|
-
function compareDeep(a, b) {
|
|
375
|
-
if (a === b) return true;
|
|
376
|
-
if (!(a && typeof a == "object") || !(b && typeof b == "object")) return false;
|
|
377
|
-
let array = Array.isArray(a);
|
|
378
|
-
if (Array.isArray(b) != array) return false;
|
|
379
|
-
if (array) {
|
|
380
|
-
if (a.length != b.length) return false;
|
|
381
|
-
for (let i = 0; i < a.length; i++) if (!compareDeep(a[i], b[i])) return false;
|
|
382
|
-
} else {
|
|
383
|
-
for (let p in a) if (!(p in b) || !compareDeep(a[p], b[p])) return false;
|
|
384
|
-
for (let p in b) if (!(p in a)) return false;
|
|
385
|
-
}
|
|
386
|
-
return true;
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
A mark is a piece of information that can be attached to a node,
|
|
390
|
-
such as it being emphasized, in code font, or a link. It has a
|
|
391
|
-
type and optionally a set of attributes that provide further
|
|
392
|
-
information (such as the target of the link). Marks are created
|
|
393
|
-
through a `Schema`, which controls which types exist and which
|
|
394
|
-
attributes they have.
|
|
395
|
-
*/
|
|
396
|
-
var Mark = class Mark {
|
|
397
|
-
/**
|
|
398
|
-
@internal
|
|
399
|
-
*/
|
|
400
|
-
constructor(type, attrs) {
|
|
401
|
-
this.type = type;
|
|
402
|
-
this.attrs = attrs;
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
Given a set of marks, create a new set which contains this one as
|
|
406
|
-
well, in the right position. If this mark is already in the set,
|
|
407
|
-
the set itself is returned. If any marks that are set to be
|
|
408
|
-
[exclusive](https://prosemirror.net/docs/ref/#model.MarkSpec.excludes) with this mark are present,
|
|
409
|
-
those are replaced by this one.
|
|
410
|
-
*/
|
|
411
|
-
addToSet(set) {
|
|
412
|
-
let copy, placed = false;
|
|
413
|
-
for (let i = 0; i < set.length; i++) {
|
|
414
|
-
let other = set[i];
|
|
415
|
-
if (this.eq(other)) return set;
|
|
416
|
-
if (this.type.excludes(other.type)) {
|
|
417
|
-
if (!copy) copy = set.slice(0, i);
|
|
418
|
-
} else if (other.type.excludes(this.type)) return set;
|
|
419
|
-
else {
|
|
420
|
-
if (!placed && other.type.rank > this.type.rank) {
|
|
421
|
-
if (!copy) copy = set.slice(0, i);
|
|
422
|
-
copy.push(this);
|
|
423
|
-
placed = true;
|
|
424
|
-
}
|
|
425
|
-
if (copy) copy.push(other);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
if (!copy) copy = set.slice();
|
|
429
|
-
if (!placed) copy.push(this);
|
|
430
|
-
return copy;
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
Remove this mark from the given set, returning a new set. If this
|
|
434
|
-
mark is not in the set, the set itself is returned.
|
|
435
|
-
*/
|
|
436
|
-
removeFromSet(set) {
|
|
437
|
-
for (let i = 0; i < set.length; i++) if (this.eq(set[i])) return set.slice(0, i).concat(set.slice(i + 1));
|
|
438
|
-
return set;
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
Test whether this mark is in the given set of marks.
|
|
442
|
-
*/
|
|
443
|
-
isInSet(set) {
|
|
444
|
-
for (let i = 0; i < set.length; i++) if (this.eq(set[i])) return true;
|
|
445
|
-
return false;
|
|
446
|
-
}
|
|
447
|
-
/**
|
|
448
|
-
Test whether this mark has the same type and attributes as
|
|
449
|
-
another mark.
|
|
450
|
-
*/
|
|
451
|
-
eq(other) {
|
|
452
|
-
return this == other || this.type == other.type && compareDeep(this.attrs, other.attrs);
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
Convert this mark to a JSON-serializeable representation.
|
|
456
|
-
*/
|
|
457
|
-
toJSON() {
|
|
458
|
-
let obj = { type: this.type.name };
|
|
459
|
-
for (let _ in this.attrs) {
|
|
460
|
-
obj.attrs = this.attrs;
|
|
461
|
-
break;
|
|
462
|
-
}
|
|
463
|
-
return obj;
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
Deserialize a mark from JSON.
|
|
467
|
-
*/
|
|
468
|
-
static fromJSON(schema, json) {
|
|
469
|
-
if (!json) throw new RangeError("Invalid input for Mark.fromJSON");
|
|
470
|
-
let type = schema.marks[json.type];
|
|
471
|
-
if (!type) throw new RangeError(`There is no mark type ${json.type} in this schema`);
|
|
472
|
-
let mark = type.create(json.attrs);
|
|
473
|
-
type.checkAttrs(mark.attrs);
|
|
474
|
-
return mark;
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
Test whether two sets of marks are identical.
|
|
478
|
-
*/
|
|
479
|
-
static sameSet(a, b) {
|
|
480
|
-
if (a == b) return true;
|
|
481
|
-
if (a.length != b.length) return false;
|
|
482
|
-
for (let i = 0; i < a.length; i++) if (!a[i].eq(b[i])) return false;
|
|
483
|
-
return true;
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
Create a properly sorted mark set from null, a single mark, or an
|
|
487
|
-
unsorted array of marks.
|
|
488
|
-
*/
|
|
489
|
-
static setFrom(marks) {
|
|
490
|
-
if (!marks || Array.isArray(marks) && marks.length == 0) return Mark.none;
|
|
491
|
-
if (marks instanceof Mark) return [marks];
|
|
492
|
-
let copy = marks.slice();
|
|
493
|
-
copy.sort((a, b) => a.type.rank - b.type.rank);
|
|
494
|
-
return copy;
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
/**
|
|
498
|
-
The empty set of marks.
|
|
499
|
-
*/
|
|
500
|
-
Mark.none = [];
|
|
501
|
-
/**
|
|
502
|
-
Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
|
|
503
|
-
given an invalid replacement.
|
|
504
|
-
*/
|
|
505
|
-
var ReplaceError = class extends Error {};
|
|
506
|
-
/**
|
|
507
|
-
A slice represents a piece cut out of a larger document. It
|
|
508
|
-
stores not only a fragment, but also the depth up to which nodes on
|
|
509
|
-
both side are ‘open’ (cut through).
|
|
510
|
-
*/
|
|
511
|
-
var Slice = class Slice {
|
|
512
|
-
/**
|
|
513
|
-
Create a slice. When specifying a non-zero open depth, you must
|
|
514
|
-
make sure that there are nodes of at least that depth at the
|
|
515
|
-
appropriate side of the fragment—i.e. if the fragment is an
|
|
516
|
-
empty paragraph node, `openStart` and `openEnd` can't be greater
|
|
517
|
-
than 1.
|
|
518
|
-
|
|
519
|
-
It is not necessary for the content of open nodes to conform to
|
|
520
|
-
the schema's content constraints, though it should be a valid
|
|
521
|
-
start/end/middle for such a node, depending on which sides are
|
|
522
|
-
open.
|
|
523
|
-
*/
|
|
524
|
-
constructor(content, openStart, openEnd) {
|
|
525
|
-
this.content = content;
|
|
526
|
-
this.openStart = openStart;
|
|
527
|
-
this.openEnd = openEnd;
|
|
528
|
-
}
|
|
529
|
-
/**
|
|
530
|
-
The size this slice would add when inserted into a document.
|
|
531
|
-
*/
|
|
532
|
-
get size() {
|
|
533
|
-
return this.content.size - this.openStart - this.openEnd;
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
@internal
|
|
537
|
-
*/
|
|
538
|
-
insertAt(pos, fragment) {
|
|
539
|
-
let content = insertInto(this.content, pos + this.openStart, fragment, this.openStart + 1, this.openEnd + 1);
|
|
540
|
-
return content && new Slice(content, this.openStart, this.openEnd);
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
@internal
|
|
544
|
-
*/
|
|
545
|
-
removeBetween(from, to) {
|
|
546
|
-
return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
Tests whether this slice is equal to another slice.
|
|
550
|
-
*/
|
|
551
|
-
eq(other) {
|
|
552
|
-
return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
|
|
553
|
-
}
|
|
554
|
-
/**
|
|
555
|
-
@internal
|
|
556
|
-
*/
|
|
557
|
-
toString() {
|
|
558
|
-
return this.content + "(" + this.openStart + "," + this.openEnd + ")";
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
Convert a slice to a JSON-serializable representation.
|
|
562
|
-
*/
|
|
563
|
-
toJSON() {
|
|
564
|
-
if (!this.content.size) return null;
|
|
565
|
-
let json = { content: this.content.toJSON() };
|
|
566
|
-
if (this.openStart > 0) json.openStart = this.openStart;
|
|
567
|
-
if (this.openEnd > 0) json.openEnd = this.openEnd;
|
|
568
|
-
return json;
|
|
569
|
-
}
|
|
570
|
-
/**
|
|
571
|
-
Deserialize a slice from its JSON representation.
|
|
572
|
-
*/
|
|
573
|
-
static fromJSON(schema, json) {
|
|
574
|
-
if (!json) return Slice.empty;
|
|
575
|
-
let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
|
|
576
|
-
if (typeof openStart != "number" || typeof openEnd != "number") throw new RangeError("Invalid input for Slice.fromJSON");
|
|
577
|
-
return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
Create a slice from a fragment by taking the maximum possible
|
|
581
|
-
open value on both side of the fragment.
|
|
582
|
-
*/
|
|
583
|
-
static maxOpen(fragment, openIsolating = true) {
|
|
584
|
-
let openStart = 0, openEnd = 0;
|
|
585
|
-
for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild) openStart++;
|
|
586
|
-
for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild) openEnd++;
|
|
587
|
-
return new Slice(fragment, openStart, openEnd);
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
/**
|
|
591
|
-
The empty slice.
|
|
592
|
-
*/
|
|
593
|
-
Slice.empty = new Slice(Fragment.empty, 0, 0);
|
|
594
|
-
function removeRange(content, from, to) {
|
|
595
|
-
let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
|
|
596
|
-
let { index: indexTo, offset: offsetTo } = content.findIndex(to);
|
|
597
|
-
if (offset == from || child.isText) {
|
|
598
|
-
if (offsetTo != to && !content.child(indexTo).isText) throw new RangeError("Removing non-flat range");
|
|
599
|
-
return content.cut(0, from).append(content.cut(to));
|
|
600
|
-
}
|
|
601
|
-
if (index != indexTo) throw new RangeError("Removing non-flat range");
|
|
602
|
-
return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
|
|
603
|
-
}
|
|
604
|
-
function insertInto(content, dist, insert, openStart, openEnd, parent) {
|
|
605
|
-
let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
|
|
606
|
-
if (offset == dist || child.isText) {
|
|
607
|
-
if (parent && openStart <= 0 && openEnd <= 0 && !parent.canReplace(index, index, insert)) return null;
|
|
608
|
-
return content.cut(0, dist).append(insert).append(content.cut(dist));
|
|
609
|
-
}
|
|
610
|
-
let inner = insertInto(child.content, dist - offset - 1, insert, index == 0 ? openStart - 1 : 0, index == content.childCount - 1 ? openEnd - 1 : 0, child);
|
|
611
|
-
return inner && content.replaceChild(index, child.copy(inner));
|
|
612
|
-
}
|
|
613
|
-
function replace($from, $to, slice) {
|
|
614
|
-
if (slice.openStart > $from.depth) throw new ReplaceError("Inserted content deeper than insertion position");
|
|
615
|
-
if ($from.depth - slice.openStart != $to.depth - slice.openEnd) throw new ReplaceError("Inconsistent open depths");
|
|
616
|
-
return replaceOuter($from, $to, slice, 0);
|
|
617
|
-
}
|
|
618
|
-
function replaceOuter($from, $to, slice, depth) {
|
|
619
|
-
let index = $from.index(depth), node = $from.node(depth);
|
|
620
|
-
if (index == $to.index(depth) && depth < $from.depth - slice.openStart) {
|
|
621
|
-
let inner = replaceOuter($from, $to, slice, depth + 1);
|
|
622
|
-
return node.copy(node.content.replaceChild(index, inner));
|
|
623
|
-
} else if (!slice.content.size) return close(node, replaceTwoWay($from, $to, depth));
|
|
624
|
-
else if (!slice.openStart && !slice.openEnd && $from.depth == depth && $to.depth == depth) {
|
|
625
|
-
let parent = $from.parent, content = parent.content;
|
|
626
|
-
return close(parent, content.cut(0, $from.parentOffset).append(slice.content).append(content.cut($to.parentOffset)));
|
|
627
|
-
} else {
|
|
628
|
-
let { start, end } = prepareSliceForReplace(slice, $from);
|
|
629
|
-
return close(node, replaceThreeWay($from, start, end, $to, depth));
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
function checkJoin(main, sub) {
|
|
633
|
-
if (!sub.type.compatibleContent(main.type)) throw new ReplaceError("Cannot join " + sub.type.name + " onto " + main.type.name);
|
|
634
|
-
}
|
|
635
|
-
function joinable($before, $after, depth) {
|
|
636
|
-
let node = $before.node(depth);
|
|
637
|
-
checkJoin(node, $after.node(depth));
|
|
638
|
-
return node;
|
|
639
|
-
}
|
|
640
|
-
function addNode(child, target) {
|
|
641
|
-
let last = target.length - 1;
|
|
642
|
-
if (last >= 0 && child.isText && child.sameMarkup(target[last])) target[last] = child.withText(target[last].text + child.text);
|
|
643
|
-
else target.push(child);
|
|
644
|
-
}
|
|
645
|
-
function addRange($start, $end, depth, target) {
|
|
646
|
-
let node = ($end || $start).node(depth);
|
|
647
|
-
let startIndex = 0, endIndex = $end ? $end.index(depth) : node.childCount;
|
|
648
|
-
if ($start) {
|
|
649
|
-
startIndex = $start.index(depth);
|
|
650
|
-
if ($start.depth > depth) startIndex++;
|
|
651
|
-
else if ($start.textOffset) {
|
|
652
|
-
addNode($start.nodeAfter, target);
|
|
653
|
-
startIndex++;
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
for (let i = startIndex; i < endIndex; i++) addNode(node.child(i), target);
|
|
657
|
-
if ($end && $end.depth == depth && $end.textOffset) addNode($end.nodeBefore, target);
|
|
658
|
-
}
|
|
659
|
-
function close(node, content) {
|
|
660
|
-
if (!node.type.validContent(content)) throw new ReplaceError("Invalid content for node " + node.type.name);
|
|
661
|
-
return node.copy(content);
|
|
662
|
-
}
|
|
663
|
-
function replaceThreeWay($from, $start, $end, $to, depth) {
|
|
664
|
-
let openStart = $from.depth > depth && joinable($from, $start, depth + 1);
|
|
665
|
-
let openEnd = $to.depth > depth && joinable($end, $to, depth + 1);
|
|
666
|
-
let content = [];
|
|
667
|
-
addRange(null, $from, depth, content);
|
|
668
|
-
if (openStart && openEnd && $start.index(depth) == $end.index(depth)) {
|
|
669
|
-
checkJoin(openStart, openEnd);
|
|
670
|
-
addNode(close(openStart, replaceThreeWay($from, $start, $end, $to, depth + 1)), content);
|
|
671
|
-
} else {
|
|
672
|
-
if (openStart) addNode(close(openStart, replaceTwoWay($from, $start, depth + 1)), content);
|
|
673
|
-
addRange($start, $end, depth, content);
|
|
674
|
-
if (openEnd) addNode(close(openEnd, replaceTwoWay($end, $to, depth + 1)), content);
|
|
675
|
-
}
|
|
676
|
-
addRange($to, null, depth, content);
|
|
677
|
-
return new Fragment(content);
|
|
678
|
-
}
|
|
679
|
-
function replaceTwoWay($from, $to, depth) {
|
|
680
|
-
let content = [];
|
|
681
|
-
addRange(null, $from, depth, content);
|
|
682
|
-
if ($from.depth > depth) addNode(close(joinable($from, $to, depth + 1), replaceTwoWay($from, $to, depth + 1)), content);
|
|
683
|
-
addRange($to, null, depth, content);
|
|
684
|
-
return new Fragment(content);
|
|
685
|
-
}
|
|
686
|
-
function prepareSliceForReplace(slice, $along) {
|
|
687
|
-
let extra = $along.depth - slice.openStart;
|
|
688
|
-
let node = $along.node(extra).copy(slice.content);
|
|
689
|
-
for (let i = extra - 1; i >= 0; i--) node = $along.node(i).copy(Fragment.from(node));
|
|
690
|
-
return {
|
|
691
|
-
start: node.resolveNoCache(slice.openStart + extra),
|
|
692
|
-
end: node.resolveNoCache(node.content.size - slice.openEnd - extra)
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
/**
|
|
696
|
-
You can [_resolve_](https://prosemirror.net/docs/ref/#model.Node.resolve) a position to get more
|
|
697
|
-
information about it. Objects of this class represent such a
|
|
698
|
-
resolved position, providing various pieces of context
|
|
699
|
-
information, and some helper methods.
|
|
700
|
-
|
|
701
|
-
Throughout this interface, methods that take an optional `depth`
|
|
702
|
-
parameter will interpret undefined as `this.depth` and negative
|
|
703
|
-
numbers as `this.depth + value`.
|
|
704
|
-
*/
|
|
705
|
-
var ResolvedPos = class ResolvedPos {
|
|
706
|
-
/**
|
|
707
|
-
@internal
|
|
708
|
-
*/
|
|
709
|
-
constructor(pos, path, parentOffset) {
|
|
710
|
-
this.pos = pos;
|
|
711
|
-
this.path = path;
|
|
712
|
-
this.parentOffset = parentOffset;
|
|
713
|
-
this.depth = path.length / 3 - 1;
|
|
714
|
-
}
|
|
715
|
-
/**
|
|
716
|
-
@internal
|
|
717
|
-
*/
|
|
718
|
-
resolveDepth(val) {
|
|
719
|
-
if (val == null) return this.depth;
|
|
720
|
-
if (val < 0) return this.depth + val;
|
|
721
|
-
return val;
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
The parent node that the position points into. Note that even if
|
|
725
|
-
a position points into a text node, that node is not considered
|
|
726
|
-
the parent—text nodes are ‘flat’ in this model, and have no content.
|
|
727
|
-
*/
|
|
728
|
-
get parent() {
|
|
729
|
-
return this.node(this.depth);
|
|
730
|
-
}
|
|
731
|
-
/**
|
|
732
|
-
The root node in which the position was resolved.
|
|
733
|
-
*/
|
|
734
|
-
get doc() {
|
|
735
|
-
return this.node(0);
|
|
736
|
-
}
|
|
737
|
-
/**
|
|
738
|
-
The ancestor node at the given level. `p.node(p.depth)` is the
|
|
739
|
-
same as `p.parent`.
|
|
740
|
-
*/
|
|
741
|
-
node(depth) {
|
|
742
|
-
return this.path[this.resolveDepth(depth) * 3];
|
|
743
|
-
}
|
|
744
|
-
/**
|
|
745
|
-
The index into the ancestor at the given level. If this points
|
|
746
|
-
at the 3rd node in the 2nd paragraph on the top level, for
|
|
747
|
-
example, `p.index(0)` is 1 and `p.index(1)` is 2.
|
|
748
|
-
*/
|
|
749
|
-
index(depth) {
|
|
750
|
-
return this.path[this.resolveDepth(depth) * 3 + 1];
|
|
751
|
-
}
|
|
752
|
-
/**
|
|
753
|
-
The index pointing after this position into the ancestor at the
|
|
754
|
-
given level.
|
|
755
|
-
*/
|
|
756
|
-
indexAfter(depth) {
|
|
757
|
-
depth = this.resolveDepth(depth);
|
|
758
|
-
return this.index(depth) + (depth == this.depth && !this.textOffset ? 0 : 1);
|
|
759
|
-
}
|
|
760
|
-
/**
|
|
761
|
-
The (absolute) position at the start of the node at the given
|
|
762
|
-
level.
|
|
763
|
-
*/
|
|
764
|
-
start(depth) {
|
|
765
|
-
depth = this.resolveDepth(depth);
|
|
766
|
-
return depth == 0 ? 0 : this.path[depth * 3 - 1] + 1;
|
|
767
|
-
}
|
|
768
|
-
/**
|
|
769
|
-
The (absolute) position at the end of the node at the given
|
|
770
|
-
level.
|
|
771
|
-
*/
|
|
772
|
-
end(depth) {
|
|
773
|
-
depth = this.resolveDepth(depth);
|
|
774
|
-
return this.start(depth) + this.node(depth).content.size;
|
|
775
|
-
}
|
|
776
|
-
/**
|
|
777
|
-
The (absolute) position directly before the wrapping node at the
|
|
778
|
-
given level, or, when `depth` is `this.depth + 1`, the original
|
|
779
|
-
position.
|
|
780
|
-
*/
|
|
781
|
-
before(depth) {
|
|
782
|
-
depth = this.resolveDepth(depth);
|
|
783
|
-
if (!depth) throw new RangeError("There is no position before the top-level node");
|
|
784
|
-
return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1];
|
|
785
|
-
}
|
|
786
|
-
/**
|
|
787
|
-
The (absolute) position directly after the wrapping node at the
|
|
788
|
-
given level, or the original position when `depth` is `this.depth + 1`.
|
|
789
|
-
*/
|
|
790
|
-
after(depth) {
|
|
791
|
-
depth = this.resolveDepth(depth);
|
|
792
|
-
if (!depth) throw new RangeError("There is no position after the top-level node");
|
|
793
|
-
return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1] + this.path[depth * 3].nodeSize;
|
|
794
|
-
}
|
|
795
|
-
/**
|
|
796
|
-
When this position points into a text node, this returns the
|
|
797
|
-
distance between the position and the start of the text node.
|
|
798
|
-
Will be zero for positions that point between nodes.
|
|
799
|
-
*/
|
|
800
|
-
get textOffset() {
|
|
801
|
-
return this.pos - this.path[this.path.length - 1];
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
Get the node directly after the position, if any. If the position
|
|
805
|
-
points into a text node, only the part of that node after the
|
|
806
|
-
position is returned.
|
|
807
|
-
*/
|
|
808
|
-
get nodeAfter() {
|
|
809
|
-
let parent = this.parent, index = this.index(this.depth);
|
|
810
|
-
if (index == parent.childCount) return null;
|
|
811
|
-
let dOff = this.pos - this.path[this.path.length - 1], child = parent.child(index);
|
|
812
|
-
return dOff ? parent.child(index).cut(dOff) : child;
|
|
813
|
-
}
|
|
814
|
-
/**
|
|
815
|
-
Get the node directly before the position, if any. If the
|
|
816
|
-
position points into a text node, only the part of that node
|
|
817
|
-
before the position is returned.
|
|
818
|
-
*/
|
|
819
|
-
get nodeBefore() {
|
|
820
|
-
let index = this.index(this.depth);
|
|
821
|
-
let dOff = this.pos - this.path[this.path.length - 1];
|
|
822
|
-
if (dOff) return this.parent.child(index).cut(0, dOff);
|
|
823
|
-
return index == 0 ? null : this.parent.child(index - 1);
|
|
824
|
-
}
|
|
825
|
-
/**
|
|
826
|
-
Get the position at the given index in the parent node at the
|
|
827
|
-
given depth (which defaults to `this.depth`).
|
|
828
|
-
*/
|
|
829
|
-
posAtIndex(index, depth) {
|
|
830
|
-
depth = this.resolveDepth(depth);
|
|
831
|
-
let node = this.path[depth * 3], pos = depth == 0 ? 0 : this.path[depth * 3 - 1] + 1;
|
|
832
|
-
for (let i = 0; i < index; i++) pos += node.child(i).nodeSize;
|
|
833
|
-
return pos;
|
|
834
|
-
}
|
|
835
|
-
/**
|
|
836
|
-
Get the marks at this position, factoring in the surrounding
|
|
837
|
-
marks' [`inclusive`](https://prosemirror.net/docs/ref/#model.MarkSpec.inclusive) property. If the
|
|
838
|
-
position is at the start of a non-empty node, the marks of the
|
|
839
|
-
node after it (if any) are returned.
|
|
840
|
-
*/
|
|
841
|
-
marks() {
|
|
842
|
-
let parent = this.parent, index = this.index();
|
|
843
|
-
if (parent.content.size == 0) return Mark.none;
|
|
844
|
-
if (this.textOffset) return parent.child(index).marks;
|
|
845
|
-
let main = parent.maybeChild(index - 1), other = parent.maybeChild(index);
|
|
846
|
-
if (!main) {
|
|
847
|
-
let tmp = main;
|
|
848
|
-
main = other;
|
|
849
|
-
other = tmp;
|
|
850
|
-
}
|
|
851
|
-
let marks = main.marks;
|
|
852
|
-
for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusive === false && (!other || !marks[i].isInSet(other.marks))) marks = marks[i--].removeFromSet(marks);
|
|
853
|
-
return marks;
|
|
854
|
-
}
|
|
855
|
-
/**
|
|
856
|
-
Get the marks after the current position, if any, except those
|
|
857
|
-
that are non-inclusive and not present at position `$end`. This
|
|
858
|
-
is mostly useful for getting the set of marks to preserve after a
|
|
859
|
-
deletion. Will return `null` if this position is at the end of
|
|
860
|
-
its parent node or its parent node isn't a textblock (in which
|
|
861
|
-
case no marks should be preserved).
|
|
862
|
-
*/
|
|
863
|
-
marksAcross($end) {
|
|
864
|
-
let after = this.parent.maybeChild(this.index());
|
|
865
|
-
if (!after || !after.isInline) return null;
|
|
866
|
-
let marks = after.marks, next = $end.parent.maybeChild($end.index());
|
|
867
|
-
for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusive === false && (!next || !marks[i].isInSet(next.marks))) marks = marks[i--].removeFromSet(marks);
|
|
868
|
-
return marks;
|
|
869
|
-
}
|
|
870
|
-
/**
|
|
871
|
-
The depth up to which this position and the given (non-resolved)
|
|
872
|
-
position share the same parent nodes.
|
|
873
|
-
*/
|
|
874
|
-
sharedDepth(pos) {
|
|
875
|
-
for (let depth = this.depth; depth > 0; depth--) if (this.start(depth) <= pos && this.end(depth) >= pos) return depth;
|
|
876
|
-
return 0;
|
|
877
|
-
}
|
|
878
|
-
/**
|
|
879
|
-
Returns a range based on the place where this position and the
|
|
880
|
-
given position diverge around block content. If both point into
|
|
881
|
-
the same textblock, for example, a range around that textblock
|
|
882
|
-
will be returned. If they point into different blocks, the range
|
|
883
|
-
around those blocks in their shared ancestor is returned. You can
|
|
884
|
-
pass in an optional predicate that will be called with a parent
|
|
885
|
-
node to see if a range into that parent is acceptable.
|
|
886
|
-
*/
|
|
887
|
-
blockRange(other = this, pred) {
|
|
888
|
-
if (other.pos < this.pos) return other.blockRange(this);
|
|
889
|
-
for (let d = this.depth - (this.parent.inlineContent || this.pos == other.pos ? 1 : 0); d >= 0; d--) if (other.pos <= this.end(d) && (!pred || pred(this.node(d)))) return new NodeRange(this, other, d);
|
|
890
|
-
return null;
|
|
891
|
-
}
|
|
892
|
-
/**
|
|
893
|
-
Query whether the given position shares the same parent node.
|
|
894
|
-
*/
|
|
895
|
-
sameParent(other) {
|
|
896
|
-
return this.pos - this.parentOffset == other.pos - other.parentOffset;
|
|
897
|
-
}
|
|
898
|
-
/**
|
|
899
|
-
Return the greater of this and the given position.
|
|
900
|
-
*/
|
|
901
|
-
max(other) {
|
|
902
|
-
return other.pos > this.pos ? other : this;
|
|
903
|
-
}
|
|
904
|
-
/**
|
|
905
|
-
Return the smaller of this and the given position.
|
|
906
|
-
*/
|
|
907
|
-
min(other) {
|
|
908
|
-
return other.pos < this.pos ? other : this;
|
|
909
|
-
}
|
|
910
|
-
/**
|
|
911
|
-
@internal
|
|
912
|
-
*/
|
|
913
|
-
toString() {
|
|
914
|
-
let str = "";
|
|
915
|
-
for (let i = 1; i <= this.depth; i++) str += (str ? "/" : "") + this.node(i).type.name + "_" + this.index(i - 1);
|
|
916
|
-
return str + ":" + this.parentOffset;
|
|
917
|
-
}
|
|
918
|
-
/**
|
|
919
|
-
@internal
|
|
920
|
-
*/
|
|
921
|
-
static resolve(doc, pos) {
|
|
922
|
-
if (!(pos >= 0 && pos <= doc.content.size)) throw new RangeError("Position " + pos + " out of range");
|
|
923
|
-
let path = [];
|
|
924
|
-
let start = 0, parentOffset = pos;
|
|
925
|
-
for (let node = doc;;) {
|
|
926
|
-
let { index, offset } = node.content.findIndex(parentOffset);
|
|
927
|
-
let rem = parentOffset - offset;
|
|
928
|
-
path.push(node, index, start + offset);
|
|
929
|
-
if (!rem) break;
|
|
930
|
-
node = node.child(index);
|
|
931
|
-
if (node.isText) break;
|
|
932
|
-
parentOffset = rem - 1;
|
|
933
|
-
start += offset + 1;
|
|
934
|
-
}
|
|
935
|
-
return new ResolvedPos(pos, path, parentOffset);
|
|
936
|
-
}
|
|
937
|
-
/**
|
|
938
|
-
@internal
|
|
939
|
-
*/
|
|
940
|
-
static resolveCached(doc, pos) {
|
|
941
|
-
let cache = resolveCache.get(doc);
|
|
942
|
-
if (cache) for (let i = 0; i < cache.elts.length; i++) {
|
|
943
|
-
let elt = cache.elts[i];
|
|
944
|
-
if (elt.pos == pos) return elt;
|
|
945
|
-
}
|
|
946
|
-
else resolveCache.set(doc, cache = new ResolveCache());
|
|
947
|
-
let result = cache.elts[cache.i] = ResolvedPos.resolve(doc, pos);
|
|
948
|
-
cache.i = (cache.i + 1) % resolveCacheSize;
|
|
949
|
-
return result;
|
|
950
|
-
}
|
|
951
|
-
};
|
|
952
|
-
var ResolveCache = class {
|
|
953
|
-
constructor() {
|
|
954
|
-
this.elts = [];
|
|
955
|
-
this.i = 0;
|
|
956
|
-
}
|
|
957
|
-
};
|
|
958
|
-
const resolveCacheSize = 12, resolveCache = /* @__PURE__ */ new WeakMap();
|
|
959
|
-
/**
|
|
960
|
-
Represents a flat range of content, i.e. one that starts and
|
|
961
|
-
ends in the same node.
|
|
962
|
-
*/
|
|
963
|
-
var NodeRange = class {
|
|
964
|
-
/**
|
|
965
|
-
Construct a node range. `$from` and `$to` should point into the
|
|
966
|
-
same node until at least the given `depth`, since a node range
|
|
967
|
-
denotes an adjacent set of nodes in a single parent node.
|
|
968
|
-
*/
|
|
969
|
-
constructor($from, $to, depth) {
|
|
970
|
-
this.$from = $from;
|
|
971
|
-
this.$to = $to;
|
|
972
|
-
this.depth = depth;
|
|
973
|
-
}
|
|
974
|
-
/**
|
|
975
|
-
The position at the start of the range.
|
|
976
|
-
*/
|
|
977
|
-
get start() {
|
|
978
|
-
return this.$from.before(this.depth + 1);
|
|
979
|
-
}
|
|
980
|
-
/**
|
|
981
|
-
The position at the end of the range.
|
|
982
|
-
*/
|
|
983
|
-
get end() {
|
|
984
|
-
return this.$to.after(this.depth + 1);
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
The parent node that the range points into.
|
|
988
|
-
*/
|
|
989
|
-
get parent() {
|
|
990
|
-
return this.$from.node(this.depth);
|
|
991
|
-
}
|
|
992
|
-
/**
|
|
993
|
-
The start index of the range in the parent node.
|
|
994
|
-
*/
|
|
995
|
-
get startIndex() {
|
|
996
|
-
return this.$from.index(this.depth);
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
The end index of the range in the parent node.
|
|
1000
|
-
*/
|
|
1001
|
-
get endIndex() {
|
|
1002
|
-
return this.$to.indexAfter(this.depth);
|
|
1003
|
-
}
|
|
1004
|
-
};
|
|
1005
|
-
const emptyAttrs = Object.create(null);
|
|
1006
|
-
/**
|
|
1007
|
-
This class represents a node in the tree that makes up a
|
|
1008
|
-
ProseMirror document. So a document is an instance of `Node`, with
|
|
1009
|
-
children that are also instances of `Node`.
|
|
1010
|
-
|
|
1011
|
-
Nodes are persistent data structures. Instead of changing them, you
|
|
1012
|
-
create new ones with the content you want. Old ones keep pointing
|
|
1013
|
-
at the old document shape. This is made cheaper by sharing
|
|
1014
|
-
structure between the old and new data as much as possible, which a
|
|
1015
|
-
tree shape like this (without back pointers) makes easy.
|
|
1016
|
-
|
|
1017
|
-
**Do not** directly mutate the properties of a `Node` object. See
|
|
1018
|
-
[the guide](https://prosemirror.net/docs/guide/#doc) for more information.
|
|
1019
|
-
*/
|
|
1020
|
-
var Node$1 = class Node$1 {
|
|
1021
|
-
/**
|
|
1022
|
-
@internal
|
|
1023
|
-
*/
|
|
1024
|
-
constructor(type, attrs, content, marks = Mark.none) {
|
|
1025
|
-
this.type = type;
|
|
1026
|
-
this.attrs = attrs;
|
|
1027
|
-
this.marks = marks;
|
|
1028
|
-
this.content = content || Fragment.empty;
|
|
1029
|
-
}
|
|
1030
|
-
/**
|
|
1031
|
-
The array of this node's child nodes.
|
|
1032
|
-
*/
|
|
1033
|
-
get children() {
|
|
1034
|
-
return this.content.content;
|
|
1035
|
-
}
|
|
1036
|
-
/**
|
|
1037
|
-
The size of this node, as defined by the integer-based [indexing
|
|
1038
|
-
scheme](https://prosemirror.net/docs/guide/#doc.indexing). For text nodes, this is the
|
|
1039
|
-
amount of characters. For other leaf nodes, it is one. For
|
|
1040
|
-
non-leaf nodes, it is the size of the content plus two (the
|
|
1041
|
-
start and end token).
|
|
1042
|
-
*/
|
|
1043
|
-
get nodeSize() {
|
|
1044
|
-
return this.isLeaf ? 1 : 2 + this.content.size;
|
|
1045
|
-
}
|
|
1046
|
-
/**
|
|
1047
|
-
The number of children that the node has.
|
|
1048
|
-
*/
|
|
1049
|
-
get childCount() {
|
|
1050
|
-
return this.content.childCount;
|
|
1051
|
-
}
|
|
1052
|
-
/**
|
|
1053
|
-
Get the child node at the given index. Raises an error when the
|
|
1054
|
-
index is out of range.
|
|
1055
|
-
*/
|
|
1056
|
-
child(index) {
|
|
1057
|
-
return this.content.child(index);
|
|
1058
|
-
}
|
|
1059
|
-
/**
|
|
1060
|
-
Get the child node at the given index, if it exists.
|
|
1061
|
-
*/
|
|
1062
|
-
maybeChild(index) {
|
|
1063
|
-
return this.content.maybeChild(index);
|
|
1064
|
-
}
|
|
1065
|
-
/**
|
|
1066
|
-
Call `f` for every child node, passing the node, its offset
|
|
1067
|
-
into this parent node, and its index.
|
|
1068
|
-
*/
|
|
1069
|
-
forEach(f) {
|
|
1070
|
-
this.content.forEach(f);
|
|
1071
|
-
}
|
|
1072
|
-
/**
|
|
1073
|
-
Invoke a callback for all descendant nodes recursively overlapping
|
|
1074
|
-
the given two positions that are relative to start of this
|
|
1075
|
-
node's content. This includes all ancestors of the nodes
|
|
1076
|
-
containing the two positions. The callback is invoked with the
|
|
1077
|
-
node, its position relative to the original node (method receiver),
|
|
1078
|
-
its parent node, and its child index. When the callback returns
|
|
1079
|
-
false for a given node, that node's children will not be
|
|
1080
|
-
recursed over. The last parameter can be used to specify a
|
|
1081
|
-
starting position to count from.
|
|
1082
|
-
*/
|
|
1083
|
-
nodesBetween(from, to, f, startPos = 0) {
|
|
1084
|
-
this.content.nodesBetween(from, to, f, startPos, this);
|
|
1085
|
-
}
|
|
1086
|
-
/**
|
|
1087
|
-
Call the given callback for every descendant node. Doesn't
|
|
1088
|
-
descend into a node when the callback returns `false`.
|
|
1089
|
-
*/
|
|
1090
|
-
descendants(f) {
|
|
1091
|
-
this.nodesBetween(0, this.content.size, f);
|
|
1092
|
-
}
|
|
1093
|
-
/**
|
|
1094
|
-
Concatenates all the text nodes found in this fragment and its
|
|
1095
|
-
children.
|
|
1096
|
-
*/
|
|
1097
|
-
get textContent() {
|
|
1098
|
-
return this.isLeaf && this.type.spec.leafText ? this.type.spec.leafText(this) : this.textBetween(0, this.content.size, "");
|
|
1099
|
-
}
|
|
1100
|
-
/**
|
|
1101
|
-
Get all text between positions `from` and `to`. When
|
|
1102
|
-
`blockSeparator` is given, it will be inserted to separate text
|
|
1103
|
-
from different block nodes. If `leafText` is given, it'll be
|
|
1104
|
-
inserted for every non-text leaf node encountered, otherwise
|
|
1105
|
-
[`leafText`](https://prosemirror.net/docs/ref/#model.NodeSpec.leafText) will be used.
|
|
1106
|
-
*/
|
|
1107
|
-
textBetween(from, to, blockSeparator, leafText) {
|
|
1108
|
-
return this.content.textBetween(from, to, blockSeparator, leafText);
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
Returns this node's first child, or `null` if there are no
|
|
1112
|
-
children.
|
|
1113
|
-
*/
|
|
1114
|
-
get firstChild() {
|
|
1115
|
-
return this.content.firstChild;
|
|
1116
|
-
}
|
|
1117
|
-
/**
|
|
1118
|
-
Returns this node's last child, or `null` if there are no
|
|
1119
|
-
children.
|
|
1120
|
-
*/
|
|
1121
|
-
get lastChild() {
|
|
1122
|
-
return this.content.lastChild;
|
|
1123
|
-
}
|
|
1124
|
-
/**
|
|
1125
|
-
Test whether two nodes represent the same piece of document.
|
|
1126
|
-
*/
|
|
1127
|
-
eq(other) {
|
|
1128
|
-
return this == other || this.sameMarkup(other) && this.content.eq(other.content);
|
|
1129
|
-
}
|
|
1130
|
-
/**
|
|
1131
|
-
Compare the markup (type, attributes, and marks) of this node to
|
|
1132
|
-
those of another. Returns `true` if both have the same markup.
|
|
1133
|
-
*/
|
|
1134
|
-
sameMarkup(other) {
|
|
1135
|
-
return this.hasMarkup(other.type, other.attrs, other.marks);
|
|
1136
|
-
}
|
|
1137
|
-
/**
|
|
1138
|
-
Check whether this node's markup correspond to the given type,
|
|
1139
|
-
attributes, and marks.
|
|
1140
|
-
*/
|
|
1141
|
-
hasMarkup(type, attrs, marks) {
|
|
1142
|
-
return this.type == type && compareDeep(this.attrs, attrs || type.defaultAttrs || emptyAttrs) && Mark.sameSet(this.marks, marks || Mark.none);
|
|
1143
|
-
}
|
|
1144
|
-
/**
|
|
1145
|
-
Create a new node with the same markup as this node, containing
|
|
1146
|
-
the given content (or empty, if no content is given).
|
|
1147
|
-
*/
|
|
1148
|
-
copy(content = null) {
|
|
1149
|
-
if (content == this.content) return this;
|
|
1150
|
-
return new Node$1(this.type, this.attrs, content, this.marks);
|
|
1151
|
-
}
|
|
1152
|
-
/**
|
|
1153
|
-
Create a copy of this node, with the given set of marks instead
|
|
1154
|
-
of the node's own marks.
|
|
1155
|
-
*/
|
|
1156
|
-
mark(marks) {
|
|
1157
|
-
return marks == this.marks ? this : new Node$1(this.type, this.attrs, this.content, marks);
|
|
1158
|
-
}
|
|
1159
|
-
/**
|
|
1160
|
-
Create a copy of this node with only the content between the
|
|
1161
|
-
given positions. If `to` is not given, it defaults to the end of
|
|
1162
|
-
the node.
|
|
1163
|
-
*/
|
|
1164
|
-
cut(from, to = this.content.size) {
|
|
1165
|
-
if (from == 0 && to == this.content.size) return this;
|
|
1166
|
-
return this.copy(this.content.cut(from, to));
|
|
1167
|
-
}
|
|
1168
|
-
/**
|
|
1169
|
-
Cut out the part of the document between the given positions, and
|
|
1170
|
-
return it as a `Slice` object.
|
|
1171
|
-
*/
|
|
1172
|
-
slice(from, to = this.content.size, includeParents = false) {
|
|
1173
|
-
if (from == to) return Slice.empty;
|
|
1174
|
-
let $from = this.resolve(from), $to = this.resolve(to);
|
|
1175
|
-
let depth = includeParents ? 0 : $from.sharedDepth(to);
|
|
1176
|
-
let start = $from.start(depth);
|
|
1177
|
-
return new Slice($from.node(depth).content.cut($from.pos - start, $to.pos - start), $from.depth - depth, $to.depth - depth);
|
|
1178
|
-
}
|
|
1179
|
-
/**
|
|
1180
|
-
Replace the part of the document between the given positions with
|
|
1181
|
-
the given slice. The slice must 'fit', meaning its open sides
|
|
1182
|
-
must be able to connect to the surrounding content, and its
|
|
1183
|
-
content nodes must be valid children for the node they are placed
|
|
1184
|
-
into. If any of this is violated, an error of type
|
|
1185
|
-
[`ReplaceError`](https://prosemirror.net/docs/ref/#model.ReplaceError) is thrown.
|
|
1186
|
-
*/
|
|
1187
|
-
replace(from, to, slice) {
|
|
1188
|
-
return replace(this.resolve(from), this.resolve(to), slice);
|
|
1189
|
-
}
|
|
1190
|
-
/**
|
|
1191
|
-
Find the node directly after the given position.
|
|
1192
|
-
*/
|
|
1193
|
-
nodeAt(pos) {
|
|
1194
|
-
for (let node = this;;) {
|
|
1195
|
-
let { index, offset } = node.content.findIndex(pos);
|
|
1196
|
-
node = node.maybeChild(index);
|
|
1197
|
-
if (!node) return null;
|
|
1198
|
-
if (offset == pos || node.isText) return node;
|
|
1199
|
-
pos -= offset + 1;
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
/**
|
|
1203
|
-
Find the (direct) child node after the given offset, if any,
|
|
1204
|
-
and return it along with its index and offset relative to this
|
|
1205
|
-
node.
|
|
1206
|
-
*/
|
|
1207
|
-
childAfter(pos) {
|
|
1208
|
-
let { index, offset } = this.content.findIndex(pos);
|
|
1209
|
-
return {
|
|
1210
|
-
node: this.content.maybeChild(index),
|
|
1211
|
-
index,
|
|
1212
|
-
offset
|
|
1213
|
-
};
|
|
1214
|
-
}
|
|
1215
|
-
/**
|
|
1216
|
-
Find the (direct) child node before the given offset, if any,
|
|
1217
|
-
and return it along with its index and offset relative to this
|
|
1218
|
-
node.
|
|
1219
|
-
*/
|
|
1220
|
-
childBefore(pos) {
|
|
1221
|
-
if (pos == 0) return {
|
|
1222
|
-
node: null,
|
|
1223
|
-
index: 0,
|
|
1224
|
-
offset: 0
|
|
1225
|
-
};
|
|
1226
|
-
let { index, offset } = this.content.findIndex(pos);
|
|
1227
|
-
if (offset < pos) return {
|
|
1228
|
-
node: this.content.child(index),
|
|
1229
|
-
index,
|
|
1230
|
-
offset
|
|
1231
|
-
};
|
|
1232
|
-
let node = this.content.child(index - 1);
|
|
1233
|
-
return {
|
|
1234
|
-
node,
|
|
1235
|
-
index: index - 1,
|
|
1236
|
-
offset: offset - node.nodeSize
|
|
1237
|
-
};
|
|
1238
|
-
}
|
|
1239
|
-
/**
|
|
1240
|
-
Resolve the given position in the document, returning an
|
|
1241
|
-
[object](https://prosemirror.net/docs/ref/#model.ResolvedPos) with information about its context.
|
|
1242
|
-
*/
|
|
1243
|
-
resolve(pos) {
|
|
1244
|
-
return ResolvedPos.resolveCached(this, pos);
|
|
1245
|
-
}
|
|
1246
|
-
/**
|
|
1247
|
-
@internal
|
|
1248
|
-
*/
|
|
1249
|
-
resolveNoCache(pos) {
|
|
1250
|
-
return ResolvedPos.resolve(this, pos);
|
|
1251
|
-
}
|
|
1252
|
-
/**
|
|
1253
|
-
Test whether a given mark or mark type occurs in this document
|
|
1254
|
-
between the two given positions.
|
|
1255
|
-
*/
|
|
1256
|
-
rangeHasMark(from, to, type) {
|
|
1257
|
-
let found = false;
|
|
1258
|
-
if (to > from) this.nodesBetween(from, to, (node) => {
|
|
1259
|
-
if (type.isInSet(node.marks)) found = true;
|
|
1260
|
-
return !found;
|
|
1261
|
-
});
|
|
1262
|
-
return found;
|
|
1263
|
-
}
|
|
1264
|
-
/**
|
|
1265
|
-
True when this is a block (non-inline node)
|
|
1266
|
-
*/
|
|
1267
|
-
get isBlock() {
|
|
1268
|
-
return this.type.isBlock;
|
|
1269
|
-
}
|
|
1270
|
-
/**
|
|
1271
|
-
True when this is a textblock node, a block node with inline
|
|
1272
|
-
content.
|
|
1273
|
-
*/
|
|
1274
|
-
get isTextblock() {
|
|
1275
|
-
return this.type.isTextblock;
|
|
1276
|
-
}
|
|
1277
|
-
/**
|
|
1278
|
-
True when this node allows inline content.
|
|
1279
|
-
*/
|
|
1280
|
-
get inlineContent() {
|
|
1281
|
-
return this.type.inlineContent;
|
|
1282
|
-
}
|
|
1283
|
-
/**
|
|
1284
|
-
True when this is an inline node (a text node or a node that can
|
|
1285
|
-
appear among text).
|
|
1286
|
-
*/
|
|
1287
|
-
get isInline() {
|
|
1288
|
-
return this.type.isInline;
|
|
1289
|
-
}
|
|
1290
|
-
/**
|
|
1291
|
-
True when this is a text node.
|
|
1292
|
-
*/
|
|
1293
|
-
get isText() {
|
|
1294
|
-
return this.type.isText;
|
|
1295
|
-
}
|
|
1296
|
-
/**
|
|
1297
|
-
True when this is a leaf node.
|
|
1298
|
-
*/
|
|
1299
|
-
get isLeaf() {
|
|
1300
|
-
return this.type.isLeaf;
|
|
1301
|
-
}
|
|
1302
|
-
/**
|
|
1303
|
-
True when this is an atom, i.e. when it does not have directly
|
|
1304
|
-
editable content. This is usually the same as `isLeaf`, but can
|
|
1305
|
-
be configured with the [`atom` property](https://prosemirror.net/docs/ref/#model.NodeSpec.atom)
|
|
1306
|
-
on a node's spec (typically used when the node is displayed as
|
|
1307
|
-
an uneditable [node view](https://prosemirror.net/docs/ref/#view.NodeView)).
|
|
1308
|
-
*/
|
|
1309
|
-
get isAtom() {
|
|
1310
|
-
return this.type.isAtom;
|
|
1311
|
-
}
|
|
1312
|
-
/**
|
|
1313
|
-
Return a string representation of this node for debugging
|
|
1314
|
-
purposes.
|
|
1315
|
-
*/
|
|
1316
|
-
toString() {
|
|
1317
|
-
if (this.type.spec.toDebugString) return this.type.spec.toDebugString(this);
|
|
1318
|
-
let name = this.type.name;
|
|
1319
|
-
if (this.content.size) name += "(" + this.content.toStringInner() + ")";
|
|
1320
|
-
return wrapMarks(this.marks, name);
|
|
1321
|
-
}
|
|
1322
|
-
/**
|
|
1323
|
-
Get the content match in this node at the given index.
|
|
1324
|
-
*/
|
|
1325
|
-
contentMatchAt(index) {
|
|
1326
|
-
let match = this.type.contentMatch.matchFragment(this.content, 0, index);
|
|
1327
|
-
if (!match) throw new Error("Called contentMatchAt on a node with invalid content");
|
|
1328
|
-
return match;
|
|
1329
|
-
}
|
|
1330
|
-
/**
|
|
1331
|
-
Test whether replacing the range between `from` and `to` (by
|
|
1332
|
-
child index) with the given replacement fragment (which defaults
|
|
1333
|
-
to the empty fragment) would leave the node's content valid. You
|
|
1334
|
-
can optionally pass `start` and `end` indices into the
|
|
1335
|
-
replacement fragment.
|
|
1336
|
-
*/
|
|
1337
|
-
canReplace(from, to, replacement = Fragment.empty, start = 0, end = replacement.childCount) {
|
|
1338
|
-
let one = this.contentMatchAt(from).matchFragment(replacement, start, end);
|
|
1339
|
-
let two = one && one.matchFragment(this.content, to);
|
|
1340
|
-
if (!two || !two.validEnd) return false;
|
|
1341
|
-
for (let i = start; i < end; i++) if (!this.type.allowsMarks(replacement.child(i).marks)) return false;
|
|
1342
|
-
return true;
|
|
1343
|
-
}
|
|
1344
|
-
/**
|
|
1345
|
-
Test whether replacing the range `from` to `to` (by index) with
|
|
1346
|
-
a node of the given type would leave the node's content valid.
|
|
1347
|
-
*/
|
|
1348
|
-
canReplaceWith(from, to, type, marks) {
|
|
1349
|
-
if (marks && !this.type.allowsMarks(marks)) return false;
|
|
1350
|
-
let start = this.contentMatchAt(from).matchType(type);
|
|
1351
|
-
let end = start && start.matchFragment(this.content, to);
|
|
1352
|
-
return end ? end.validEnd : false;
|
|
1353
|
-
}
|
|
1354
|
-
/**
|
|
1355
|
-
Test whether the given node's content could be appended to this
|
|
1356
|
-
node. If that node is empty, this will only return true if there
|
|
1357
|
-
is at least one node type that can appear in both nodes (to avoid
|
|
1358
|
-
merging completely incompatible nodes).
|
|
1359
|
-
*/
|
|
1360
|
-
canAppend(other) {
|
|
1361
|
-
if (other.content.size) return this.canReplace(this.childCount, this.childCount, other.content);
|
|
1362
|
-
else return this.type.compatibleContent(other.type);
|
|
1363
|
-
}
|
|
1364
|
-
/**
|
|
1365
|
-
Check whether this node and its descendants conform to the
|
|
1366
|
-
schema, and raise an exception when they do not.
|
|
1367
|
-
*/
|
|
1368
|
-
check() {
|
|
1369
|
-
this.type.checkContent(this.content);
|
|
1370
|
-
this.type.checkAttrs(this.attrs);
|
|
1371
|
-
let copy = Mark.none;
|
|
1372
|
-
for (let i = 0; i < this.marks.length; i++) {
|
|
1373
|
-
let mark = this.marks[i];
|
|
1374
|
-
mark.type.checkAttrs(mark.attrs);
|
|
1375
|
-
copy = mark.addToSet(copy);
|
|
1376
|
-
}
|
|
1377
|
-
if (!Mark.sameSet(copy, this.marks)) throw new RangeError(`Invalid collection of marks for node ${this.type.name}: ${this.marks.map((m) => m.type.name)}`);
|
|
1378
|
-
this.content.forEach((node) => node.check());
|
|
1379
|
-
}
|
|
1380
|
-
/**
|
|
1381
|
-
Return a JSON-serializeable representation of this node.
|
|
1382
|
-
*/
|
|
1383
|
-
toJSON() {
|
|
1384
|
-
let obj = { type: this.type.name };
|
|
1385
|
-
for (let _ in this.attrs) {
|
|
1386
|
-
obj.attrs = this.attrs;
|
|
1387
|
-
break;
|
|
1388
|
-
}
|
|
1389
|
-
if (this.content.size) obj.content = this.content.toJSON();
|
|
1390
|
-
if (this.marks.length) obj.marks = this.marks.map((n) => n.toJSON());
|
|
1391
|
-
return obj;
|
|
1392
|
-
}
|
|
1393
|
-
/**
|
|
1394
|
-
Deserialize a node from its JSON representation.
|
|
1395
|
-
*/
|
|
1396
|
-
static fromJSON(schema, json) {
|
|
1397
|
-
if (!json) throw new RangeError("Invalid input for Node.fromJSON");
|
|
1398
|
-
let marks = void 0;
|
|
1399
|
-
if (json.marks) {
|
|
1400
|
-
if (!Array.isArray(json.marks)) throw new RangeError("Invalid mark data for Node.fromJSON");
|
|
1401
|
-
marks = json.marks.map(schema.markFromJSON);
|
|
1402
|
-
}
|
|
1403
|
-
if (json.type == "text") {
|
|
1404
|
-
if (typeof json.text != "string") throw new RangeError("Invalid text node in JSON");
|
|
1405
|
-
return schema.text(json.text, marks);
|
|
1406
|
-
}
|
|
1407
|
-
let content = Fragment.fromJSON(schema, json.content);
|
|
1408
|
-
let node = schema.nodeType(json.type).create(json.attrs, content, marks);
|
|
1409
|
-
node.type.checkAttrs(node.attrs);
|
|
1410
|
-
return node;
|
|
1411
|
-
}
|
|
1412
|
-
};
|
|
1413
|
-
Node$1.prototype.text = void 0;
|
|
1414
|
-
function wrapMarks(marks, str) {
|
|
1415
|
-
for (let i = marks.length - 1; i >= 0; i--) str = marks[i].type.name + "(" + str + ")";
|
|
1416
|
-
return str;
|
|
1417
|
-
}
|
|
1418
|
-
/**
|
|
1419
|
-
Instances of this class represent a match state of a node type's
|
|
1420
|
-
[content expression](https://prosemirror.net/docs/ref/#model.NodeSpec.content), and can be used to
|
|
1421
|
-
find out whether further content matches here, and whether a given
|
|
1422
|
-
position is a valid end of the node.
|
|
1423
|
-
*/
|
|
1424
|
-
var ContentMatch = class ContentMatch {
|
|
1425
|
-
/**
|
|
1426
|
-
@internal
|
|
1427
|
-
*/
|
|
1428
|
-
constructor(validEnd) {
|
|
1429
|
-
this.validEnd = validEnd;
|
|
1430
|
-
/**
|
|
1431
|
-
@internal
|
|
1432
|
-
*/
|
|
1433
|
-
this.next = [];
|
|
1434
|
-
/**
|
|
1435
|
-
@internal
|
|
1436
|
-
*/
|
|
1437
|
-
this.wrapCache = [];
|
|
1438
|
-
}
|
|
1439
|
-
/**
|
|
1440
|
-
@internal
|
|
1441
|
-
*/
|
|
1442
|
-
static parse(string, nodeTypes) {
|
|
1443
|
-
let stream = new TokenStream(string, nodeTypes);
|
|
1444
|
-
if (stream.next == null) return ContentMatch.empty;
|
|
1445
|
-
let expr = parseExpr(stream);
|
|
1446
|
-
if (stream.next) stream.err("Unexpected trailing text");
|
|
1447
|
-
let match = dfa(nfa(expr));
|
|
1448
|
-
checkForDeadEnds(match, stream);
|
|
1449
|
-
return match;
|
|
1450
|
-
}
|
|
1451
|
-
/**
|
|
1452
|
-
Match a node type, returning a match after that node if
|
|
1453
|
-
successful.
|
|
1454
|
-
*/
|
|
1455
|
-
matchType(type) {
|
|
1456
|
-
for (let i = 0; i < this.next.length; i++) if (this.next[i].type == type) return this.next[i].next;
|
|
1457
|
-
return null;
|
|
1458
|
-
}
|
|
1459
|
-
/**
|
|
1460
|
-
Try to match a fragment. Returns the resulting match when
|
|
1461
|
-
successful.
|
|
1462
|
-
*/
|
|
1463
|
-
matchFragment(frag, start = 0, end = frag.childCount) {
|
|
1464
|
-
let cur = this;
|
|
1465
|
-
for (let i = start; cur && i < end; i++) cur = cur.matchType(frag.child(i).type);
|
|
1466
|
-
return cur;
|
|
1467
|
-
}
|
|
1468
|
-
/**
|
|
1469
|
-
@internal
|
|
1470
|
-
*/
|
|
1471
|
-
get inlineContent() {
|
|
1472
|
-
return this.next.length != 0 && this.next[0].type.isInline;
|
|
1473
|
-
}
|
|
1474
|
-
/**
|
|
1475
|
-
Get the first matching node type at this match position that can
|
|
1476
|
-
be generated.
|
|
1477
|
-
*/
|
|
1478
|
-
get defaultType() {
|
|
1479
|
-
for (let i = 0; i < this.next.length; i++) {
|
|
1480
|
-
let { type } = this.next[i];
|
|
1481
|
-
if (!(type.isText || type.hasRequiredAttrs())) return type;
|
|
1482
|
-
}
|
|
1483
|
-
return null;
|
|
1484
|
-
}
|
|
1485
|
-
/**
|
|
1486
|
-
@internal
|
|
1487
|
-
*/
|
|
1488
|
-
compatible(other) {
|
|
1489
|
-
for (let i = 0; i < this.next.length; i++) for (let j = 0; j < other.next.length; j++) if (this.next[i].type == other.next[j].type) return true;
|
|
1490
|
-
return false;
|
|
1491
|
-
}
|
|
1492
|
-
/**
|
|
1493
|
-
Try to match the given fragment, and if that fails, see if it can
|
|
1494
|
-
be made to match by inserting nodes in front of it. When
|
|
1495
|
-
successful, return a fragment of inserted nodes (which may be
|
|
1496
|
-
empty if nothing had to be inserted). When `toEnd` is true, only
|
|
1497
|
-
return a fragment if the resulting match goes to the end of the
|
|
1498
|
-
content expression.
|
|
1499
|
-
*/
|
|
1500
|
-
fillBefore(after, toEnd = false, startIndex = 0) {
|
|
1501
|
-
let seen = [this];
|
|
1502
|
-
function search(match, types) {
|
|
1503
|
-
let finished = match.matchFragment(after, startIndex);
|
|
1504
|
-
if (finished && (!toEnd || finished.validEnd)) return Fragment.from(types.map((tp) => tp.createAndFill()));
|
|
1505
|
-
for (let i = 0; i < match.next.length; i++) {
|
|
1506
|
-
let { type, next } = match.next[i];
|
|
1507
|
-
if (!(type.isText || type.hasRequiredAttrs()) && seen.indexOf(next) == -1) {
|
|
1508
|
-
seen.push(next);
|
|
1509
|
-
let found = search(next, types.concat(type));
|
|
1510
|
-
if (found) return found;
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
return null;
|
|
1514
|
-
}
|
|
1515
|
-
return search(this, []);
|
|
1516
|
-
}
|
|
1517
|
-
/**
|
|
1518
|
-
Find a set of wrapping node types that would allow a node of the
|
|
1519
|
-
given type to appear at this position. The result may be empty
|
|
1520
|
-
(when it fits directly) and will be null when no such wrapping
|
|
1521
|
-
exists.
|
|
1522
|
-
*/
|
|
1523
|
-
findWrapping(target) {
|
|
1524
|
-
for (let i = 0; i < this.wrapCache.length; i += 2) if (this.wrapCache[i] == target) return this.wrapCache[i + 1];
|
|
1525
|
-
let computed = this.computeWrapping(target);
|
|
1526
|
-
this.wrapCache.push(target, computed);
|
|
1527
|
-
return computed;
|
|
1528
|
-
}
|
|
1529
|
-
/**
|
|
1530
|
-
@internal
|
|
1531
|
-
*/
|
|
1532
|
-
computeWrapping(target) {
|
|
1533
|
-
let seen = Object.create(null), active = [{
|
|
1534
|
-
match: this,
|
|
1535
|
-
type: null,
|
|
1536
|
-
via: null
|
|
1537
|
-
}];
|
|
1538
|
-
while (active.length) {
|
|
1539
|
-
let current = active.shift(), match = current.match;
|
|
1540
|
-
if (match.matchType(target)) {
|
|
1541
|
-
let result = [];
|
|
1542
|
-
for (let obj = current; obj.type; obj = obj.via) result.push(obj.type);
|
|
1543
|
-
return result.reverse();
|
|
1544
|
-
}
|
|
1545
|
-
for (let i = 0; i < match.next.length; i++) {
|
|
1546
|
-
let { type, next } = match.next[i];
|
|
1547
|
-
if (!type.isLeaf && !type.hasRequiredAttrs() && !(type.name in seen) && (!current.type || next.validEnd)) {
|
|
1548
|
-
active.push({
|
|
1549
|
-
match: type.contentMatch,
|
|
1550
|
-
type,
|
|
1551
|
-
via: current
|
|
1552
|
-
});
|
|
1553
|
-
seen[type.name] = true;
|
|
1554
|
-
}
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
return null;
|
|
1558
|
-
}
|
|
1559
|
-
/**
|
|
1560
|
-
The number of outgoing edges this node has in the finite
|
|
1561
|
-
automaton that describes the content expression.
|
|
1562
|
-
*/
|
|
1563
|
-
get edgeCount() {
|
|
1564
|
-
return this.next.length;
|
|
1565
|
-
}
|
|
1566
|
-
/**
|
|
1567
|
-
Get the _n_th outgoing edge from this node in the finite
|
|
1568
|
-
automaton that describes the content expression.
|
|
1569
|
-
*/
|
|
1570
|
-
edge(n) {
|
|
1571
|
-
if (n >= this.next.length) throw new RangeError(`There's no ${n}th edge in this content match`);
|
|
1572
|
-
return this.next[n];
|
|
1573
|
-
}
|
|
1574
|
-
/**
|
|
1575
|
-
@internal
|
|
1576
|
-
*/
|
|
1577
|
-
toString() {
|
|
1578
|
-
let seen = [];
|
|
1579
|
-
function scan(m) {
|
|
1580
|
-
seen.push(m);
|
|
1581
|
-
for (let i = 0; i < m.next.length; i++) if (seen.indexOf(m.next[i].next) == -1) scan(m.next[i].next);
|
|
1582
|
-
}
|
|
1583
|
-
scan(this);
|
|
1584
|
-
return seen.map((m, i) => {
|
|
1585
|
-
let out = i + (m.validEnd ? "*" : " ") + " ";
|
|
1586
|
-
for (let i = 0; i < m.next.length; i++) out += (i ? ", " : "") + m.next[i].type.name + "->" + seen.indexOf(m.next[i].next);
|
|
1587
|
-
return out;
|
|
1588
|
-
}).join("\n");
|
|
1589
|
-
}
|
|
1590
|
-
};
|
|
1591
|
-
/**
|
|
1592
|
-
@internal
|
|
1593
|
-
*/
|
|
1594
|
-
ContentMatch.empty = new ContentMatch(true);
|
|
1595
|
-
var TokenStream = class {
|
|
1596
|
-
constructor(string, nodeTypes) {
|
|
1597
|
-
this.string = string;
|
|
1598
|
-
this.nodeTypes = nodeTypes;
|
|
1599
|
-
this.inline = null;
|
|
1600
|
-
this.pos = 0;
|
|
1601
|
-
this.tokens = string.split(/\s*(?=\b|\W|$)/);
|
|
1602
|
-
if (this.tokens[this.tokens.length - 1] == "") this.tokens.pop();
|
|
1603
|
-
if (this.tokens[0] == "") this.tokens.shift();
|
|
1604
|
-
}
|
|
1605
|
-
get next() {
|
|
1606
|
-
return this.tokens[this.pos];
|
|
1607
|
-
}
|
|
1608
|
-
eat(tok) {
|
|
1609
|
-
return this.next == tok && (this.pos++ || true);
|
|
1610
|
-
}
|
|
1611
|
-
err(str) {
|
|
1612
|
-
throw new SyntaxError(str + " (in content expression '" + this.string + "')");
|
|
1613
|
-
}
|
|
1614
|
-
};
|
|
1615
|
-
function parseExpr(stream) {
|
|
1616
|
-
let exprs = [];
|
|
1617
|
-
do
|
|
1618
|
-
exprs.push(parseExprSeq(stream));
|
|
1619
|
-
while (stream.eat("|"));
|
|
1620
|
-
return exprs.length == 1 ? exprs[0] : {
|
|
1621
|
-
type: "choice",
|
|
1622
|
-
exprs
|
|
1623
|
-
};
|
|
1624
|
-
}
|
|
1625
|
-
function parseExprSeq(stream) {
|
|
1626
|
-
let exprs = [];
|
|
1627
|
-
do
|
|
1628
|
-
exprs.push(parseExprSubscript(stream));
|
|
1629
|
-
while (stream.next && stream.next != ")" && stream.next != "|");
|
|
1630
|
-
return exprs.length == 1 ? exprs[0] : {
|
|
1631
|
-
type: "seq",
|
|
1632
|
-
exprs
|
|
1633
|
-
};
|
|
1634
|
-
}
|
|
1635
|
-
function parseExprSubscript(stream) {
|
|
1636
|
-
let expr = parseExprAtom(stream);
|
|
1637
|
-
for (;;) if (stream.eat("+")) expr = {
|
|
1638
|
-
type: "plus",
|
|
1639
|
-
expr
|
|
1640
|
-
};
|
|
1641
|
-
else if (stream.eat("*")) expr = {
|
|
1642
|
-
type: "star",
|
|
1643
|
-
expr
|
|
1644
|
-
};
|
|
1645
|
-
else if (stream.eat("?")) expr = {
|
|
1646
|
-
type: "opt",
|
|
1647
|
-
expr
|
|
1648
|
-
};
|
|
1649
|
-
else if (stream.eat("{")) expr = parseExprRange(stream, expr);
|
|
1650
|
-
else break;
|
|
1651
|
-
return expr;
|
|
1652
|
-
}
|
|
1653
|
-
function parseNum(stream) {
|
|
1654
|
-
if (/\D/.test(stream.next)) stream.err("Expected number, got '" + stream.next + "'");
|
|
1655
|
-
let result = Number(stream.next);
|
|
1656
|
-
stream.pos++;
|
|
1657
|
-
return result;
|
|
1658
|
-
}
|
|
1659
|
-
function parseExprRange(stream, expr) {
|
|
1660
|
-
let min = parseNum(stream), max = min;
|
|
1661
|
-
if (stream.eat(",")) if (stream.next != "}") max = parseNum(stream);
|
|
1662
|
-
else max = -1;
|
|
1663
|
-
if (!stream.eat("}")) stream.err("Unclosed braced range");
|
|
1664
|
-
return {
|
|
1665
|
-
type: "range",
|
|
1666
|
-
min,
|
|
1667
|
-
max,
|
|
1668
|
-
expr
|
|
1669
|
-
};
|
|
1670
|
-
}
|
|
1671
|
-
function resolveName(stream, name) {
|
|
1672
|
-
let types = stream.nodeTypes, type = types[name];
|
|
1673
|
-
if (type) return [type];
|
|
1674
|
-
let result = [];
|
|
1675
|
-
for (let typeName in types) {
|
|
1676
|
-
let type = types[typeName];
|
|
1677
|
-
if (type.isInGroup(name)) result.push(type);
|
|
1678
|
-
}
|
|
1679
|
-
if (result.length == 0) stream.err("No node type or group '" + name + "' found");
|
|
1680
|
-
return result;
|
|
1681
|
-
}
|
|
1682
|
-
function parseExprAtom(stream) {
|
|
1683
|
-
if (stream.eat("(")) {
|
|
1684
|
-
let expr = parseExpr(stream);
|
|
1685
|
-
if (!stream.eat(")")) stream.err("Missing closing paren");
|
|
1686
|
-
return expr;
|
|
1687
|
-
} else if (!/\W/.test(stream.next)) {
|
|
1688
|
-
let exprs = resolveName(stream, stream.next).map((type) => {
|
|
1689
|
-
if (stream.inline == null) stream.inline = type.isInline;
|
|
1690
|
-
else if (stream.inline != type.isInline) stream.err("Mixing inline and block content");
|
|
1691
|
-
return {
|
|
1692
|
-
type: "name",
|
|
1693
|
-
value: type
|
|
1694
|
-
};
|
|
1695
|
-
});
|
|
1696
|
-
stream.pos++;
|
|
1697
|
-
return exprs.length == 1 ? exprs[0] : {
|
|
1698
|
-
type: "choice",
|
|
1699
|
-
exprs
|
|
1700
|
-
};
|
|
1701
|
-
} else stream.err("Unexpected token '" + stream.next + "'");
|
|
1702
|
-
}
|
|
1703
|
-
function nfa(expr) {
|
|
1704
|
-
let nfa = [[]];
|
|
1705
|
-
connect(compile(expr, 0), node());
|
|
1706
|
-
return nfa;
|
|
1707
|
-
function node() {
|
|
1708
|
-
return nfa.push([]) - 1;
|
|
1709
|
-
}
|
|
1710
|
-
function edge(from, to, term) {
|
|
1711
|
-
let edge = {
|
|
1712
|
-
term,
|
|
1713
|
-
to
|
|
1714
|
-
};
|
|
1715
|
-
nfa[from].push(edge);
|
|
1716
|
-
return edge;
|
|
1717
|
-
}
|
|
1718
|
-
function connect(edges, to) {
|
|
1719
|
-
edges.forEach((edge) => edge.to = to);
|
|
1720
|
-
}
|
|
1721
|
-
function compile(expr, from) {
|
|
1722
|
-
if (expr.type == "choice") return expr.exprs.reduce((out, expr) => out.concat(compile(expr, from)), []);
|
|
1723
|
-
else if (expr.type == "seq") for (let i = 0;; i++) {
|
|
1724
|
-
let next = compile(expr.exprs[i], from);
|
|
1725
|
-
if (i == expr.exprs.length - 1) return next;
|
|
1726
|
-
connect(next, from = node());
|
|
1727
|
-
}
|
|
1728
|
-
else if (expr.type == "star") {
|
|
1729
|
-
let loop = node();
|
|
1730
|
-
edge(from, loop);
|
|
1731
|
-
connect(compile(expr.expr, loop), loop);
|
|
1732
|
-
return [edge(loop)];
|
|
1733
|
-
} else if (expr.type == "plus") {
|
|
1734
|
-
let loop = node();
|
|
1735
|
-
connect(compile(expr.expr, from), loop);
|
|
1736
|
-
connect(compile(expr.expr, loop), loop);
|
|
1737
|
-
return [edge(loop)];
|
|
1738
|
-
} else if (expr.type == "opt") return [edge(from)].concat(compile(expr.expr, from));
|
|
1739
|
-
else if (expr.type == "range") {
|
|
1740
|
-
let cur = from;
|
|
1741
|
-
for (let i = 0; i < expr.min; i++) {
|
|
1742
|
-
let next = node();
|
|
1743
|
-
connect(compile(expr.expr, cur), next);
|
|
1744
|
-
cur = next;
|
|
1745
|
-
}
|
|
1746
|
-
if (expr.max == -1) connect(compile(expr.expr, cur), cur);
|
|
1747
|
-
else for (let i = expr.min; i < expr.max; i++) {
|
|
1748
|
-
let next = node();
|
|
1749
|
-
edge(cur, next);
|
|
1750
|
-
connect(compile(expr.expr, cur), next);
|
|
1751
|
-
cur = next;
|
|
1752
|
-
}
|
|
1753
|
-
return [edge(cur)];
|
|
1754
|
-
} else if (expr.type == "name") return [edge(from, void 0, expr.value)];
|
|
1755
|
-
else throw new Error("Unknown expr type");
|
|
1756
|
-
}
|
|
1757
|
-
}
|
|
1758
|
-
function cmp(a, b) {
|
|
1759
|
-
return b - a;
|
|
1760
|
-
}
|
|
1761
|
-
function nullFrom(nfa, node) {
|
|
1762
|
-
let result = [];
|
|
1763
|
-
scan(node);
|
|
1764
|
-
return result.sort(cmp);
|
|
1765
|
-
function scan(node) {
|
|
1766
|
-
let edges = nfa[node];
|
|
1767
|
-
if (edges.length == 1 && !edges[0].term) return scan(edges[0].to);
|
|
1768
|
-
result.push(node);
|
|
1769
|
-
for (let i = 0; i < edges.length; i++) {
|
|
1770
|
-
let { term, to } = edges[i];
|
|
1771
|
-
if (!term && result.indexOf(to) == -1) scan(to);
|
|
1772
|
-
}
|
|
1773
|
-
}
|
|
1774
|
-
}
|
|
1775
|
-
function dfa(nfa) {
|
|
1776
|
-
let labeled = Object.create(null);
|
|
1777
|
-
return explore(nullFrom(nfa, 0));
|
|
1778
|
-
function explore(states) {
|
|
1779
|
-
let out = [];
|
|
1780
|
-
states.forEach((node) => {
|
|
1781
|
-
nfa[node].forEach(({ term, to }) => {
|
|
1782
|
-
if (!term) return;
|
|
1783
|
-
let set;
|
|
1784
|
-
for (let i = 0; i < out.length; i++) if (out[i][0] == term) set = out[i][1];
|
|
1785
|
-
nullFrom(nfa, to).forEach((node) => {
|
|
1786
|
-
if (!set) out.push([term, set = []]);
|
|
1787
|
-
if (set.indexOf(node) == -1) set.push(node);
|
|
1788
|
-
});
|
|
1789
|
-
});
|
|
1790
|
-
});
|
|
1791
|
-
let state = labeled[states.join(",")] = new ContentMatch(states.indexOf(nfa.length - 1) > -1);
|
|
1792
|
-
for (let i = 0; i < out.length; i++) {
|
|
1793
|
-
let states = out[i][1].sort(cmp);
|
|
1794
|
-
state.next.push({
|
|
1795
|
-
type: out[i][0],
|
|
1796
|
-
next: labeled[states.join(",")] || explore(states)
|
|
1797
|
-
});
|
|
1798
|
-
}
|
|
1799
|
-
return state;
|
|
1800
|
-
}
|
|
1801
|
-
}
|
|
1802
|
-
function checkForDeadEnds(match, stream) {
|
|
1803
|
-
for (let i = 0, work = [match]; i < work.length; i++) {
|
|
1804
|
-
let state = work[i], dead = !state.validEnd, nodes = [];
|
|
1805
|
-
for (let j = 0; j < state.next.length; j++) {
|
|
1806
|
-
let { type, next } = state.next[j];
|
|
1807
|
-
nodes.push(type.name);
|
|
1808
|
-
if (dead && !(type.isText || type.hasRequiredAttrs())) dead = false;
|
|
1809
|
-
if (work.indexOf(next) == -1) work.push(next);
|
|
1810
|
-
}
|
|
1811
|
-
if (dead) stream.err("Only non-generatable nodes (" + nodes.join(", ") + ") in a required position (see https://prosemirror.net/docs/guide/#generatable)");
|
|
1812
|
-
}
|
|
1813
|
-
}
|
|
1814
|
-
//#endregion
|
|
1815
|
-
//#region ../../node_modules/.pnpm/prosemirror-transform@1.12.0/node_modules/prosemirror-transform/dist/index.js
|
|
1816
|
-
const lower16 = 65535;
|
|
1817
|
-
const factor16 = Math.pow(2, 16);
|
|
1818
|
-
function makeRecover(index, offset) {
|
|
1819
|
-
return index + offset * factor16;
|
|
1820
|
-
}
|
|
1821
|
-
function recoverIndex(value) {
|
|
1822
|
-
return value & lower16;
|
|
1823
|
-
}
|
|
1824
|
-
function recoverOffset(value) {
|
|
1825
|
-
return (value - (value & lower16)) / factor16;
|
|
1826
|
-
}
|
|
1827
|
-
const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
|
|
1828
|
-
/**
|
|
1829
|
-
An object representing a mapped position with extra
|
|
1830
|
-
information.
|
|
1831
|
-
*/
|
|
1832
|
-
var MapResult = class {
|
|
1833
|
-
/**
|
|
1834
|
-
@internal
|
|
1835
|
-
*/
|
|
1836
|
-
constructor(pos, delInfo, recover) {
|
|
1837
|
-
this.pos = pos;
|
|
1838
|
-
this.delInfo = delInfo;
|
|
1839
|
-
this.recover = recover;
|
|
1840
|
-
}
|
|
1841
|
-
/**
|
|
1842
|
-
Tells you whether the position was deleted, that is, whether the
|
|
1843
|
-
step removed the token on the side queried (via the `assoc`)
|
|
1844
|
-
argument from the document.
|
|
1845
|
-
*/
|
|
1846
|
-
get deleted() {
|
|
1847
|
-
return (this.delInfo & DEL_SIDE) > 0;
|
|
1848
|
-
}
|
|
1849
|
-
/**
|
|
1850
|
-
Tells you whether the token before the mapped position was deleted.
|
|
1851
|
-
*/
|
|
1852
|
-
get deletedBefore() {
|
|
1853
|
-
return (this.delInfo & 5) > 0;
|
|
1854
|
-
}
|
|
1855
|
-
/**
|
|
1856
|
-
True when the token after the mapped position was deleted.
|
|
1857
|
-
*/
|
|
1858
|
-
get deletedAfter() {
|
|
1859
|
-
return (this.delInfo & 6) > 0;
|
|
1860
|
-
}
|
|
1861
|
-
/**
|
|
1862
|
-
Tells whether any of the steps mapped through deletes across the
|
|
1863
|
-
position (including both the token before and after the
|
|
1864
|
-
position).
|
|
1865
|
-
*/
|
|
1866
|
-
get deletedAcross() {
|
|
1867
|
-
return (this.delInfo & DEL_ACROSS) > 0;
|
|
1868
|
-
}
|
|
1869
|
-
};
|
|
1870
|
-
/**
|
|
1871
|
-
A map describing the deletions and insertions made by a step, which
|
|
1872
|
-
can be used to find the correspondence between positions in the
|
|
1873
|
-
pre-step version of a document and the same position in the
|
|
1874
|
-
post-step version.
|
|
1875
|
-
*/
|
|
1876
|
-
var StepMap = class StepMap {
|
|
1877
|
-
/**
|
|
1878
|
-
Create a position map. The modifications to the document are
|
|
1879
|
-
represented as an array of numbers, in which each group of three
|
|
1880
|
-
represents a modified chunk as `[start, oldSize, newSize]`.
|
|
1881
|
-
*/
|
|
1882
|
-
constructor(ranges, inverted = false) {
|
|
1883
|
-
this.ranges = ranges;
|
|
1884
|
-
this.inverted = inverted;
|
|
1885
|
-
if (!ranges.length && StepMap.empty) return StepMap.empty;
|
|
1886
|
-
}
|
|
1887
|
-
/**
|
|
1888
|
-
@internal
|
|
1889
|
-
*/
|
|
1890
|
-
recover(value) {
|
|
1891
|
-
let diff = 0, index = recoverIndex(value);
|
|
1892
|
-
if (!this.inverted) for (let i = 0; i < index; i++) diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
|
|
1893
|
-
return this.ranges[index * 3] + diff + recoverOffset(value);
|
|
1894
|
-
}
|
|
1895
|
-
mapResult(pos, assoc = 1) {
|
|
1896
|
-
return this._map(pos, assoc, false);
|
|
1897
|
-
}
|
|
1898
|
-
map(pos, assoc = 1) {
|
|
1899
|
-
return this._map(pos, assoc, true);
|
|
1900
|
-
}
|
|
1901
|
-
/**
|
|
1902
|
-
@internal
|
|
1903
|
-
*/
|
|
1904
|
-
_map(pos, assoc, simple) {
|
|
1905
|
-
let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
1906
|
-
for (let i = 0; i < this.ranges.length; i += 3) {
|
|
1907
|
-
let start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
1908
|
-
if (start > pos) break;
|
|
1909
|
-
let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
|
|
1910
|
-
if (pos <= end) {
|
|
1911
|
-
let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
|
|
1912
|
-
let result = start + diff + (side < 0 ? 0 : newSize);
|
|
1913
|
-
if (simple) return result;
|
|
1914
|
-
let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
|
|
1915
|
-
let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
|
|
1916
|
-
if (assoc < 0 ? pos != start : pos != end) del |= DEL_SIDE;
|
|
1917
|
-
return new MapResult(result, del, recover);
|
|
1918
|
-
}
|
|
1919
|
-
diff += newSize - oldSize;
|
|
1920
|
-
}
|
|
1921
|
-
return simple ? pos + diff : new MapResult(pos + diff, 0, null);
|
|
1922
|
-
}
|
|
1923
|
-
/**
|
|
1924
|
-
@internal
|
|
1925
|
-
*/
|
|
1926
|
-
touches(pos, recover) {
|
|
1927
|
-
let diff = 0, index = recoverIndex(recover);
|
|
1928
|
-
let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
1929
|
-
for (let i = 0; i < this.ranges.length; i += 3) {
|
|
1930
|
-
let start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
1931
|
-
if (start > pos) break;
|
|
1932
|
-
let oldSize = this.ranges[i + oldIndex];
|
|
1933
|
-
if (pos <= start + oldSize && i == index * 3) return true;
|
|
1934
|
-
diff += this.ranges[i + newIndex] - oldSize;
|
|
1935
|
-
}
|
|
1936
|
-
return false;
|
|
1937
|
-
}
|
|
1938
|
-
/**
|
|
1939
|
-
Calls the given function on each of the changed ranges included in
|
|
1940
|
-
this map.
|
|
1941
|
-
*/
|
|
1942
|
-
forEach(f) {
|
|
1943
|
-
let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
1944
|
-
for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
|
|
1945
|
-
let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
|
|
1946
|
-
let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
|
|
1947
|
-
f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
|
|
1948
|
-
diff += newSize - oldSize;
|
|
1949
|
-
}
|
|
1950
|
-
}
|
|
1951
|
-
/**
|
|
1952
|
-
Create an inverted version of this map. The result can be used to
|
|
1953
|
-
map positions in the post-step document to the pre-step document.
|
|
1954
|
-
*/
|
|
1955
|
-
invert() {
|
|
1956
|
-
return new StepMap(this.ranges, !this.inverted);
|
|
1957
|
-
}
|
|
1958
|
-
/**
|
|
1959
|
-
@internal
|
|
1960
|
-
*/
|
|
1961
|
-
toString() {
|
|
1962
|
-
return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
|
|
1963
|
-
}
|
|
1964
|
-
/**
|
|
1965
|
-
Create a map that moves all positions by offset `n` (which may be
|
|
1966
|
-
negative). This can be useful when applying steps meant for a
|
|
1967
|
-
sub-document to a larger document, or vice-versa.
|
|
1968
|
-
*/
|
|
1969
|
-
static offset(n) {
|
|
1970
|
-
return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [
|
|
1971
|
-
0,
|
|
1972
|
-
-n,
|
|
1973
|
-
0
|
|
1974
|
-
] : [
|
|
1975
|
-
0,
|
|
1976
|
-
0,
|
|
1977
|
-
n
|
|
1978
|
-
]);
|
|
1979
|
-
}
|
|
1980
|
-
};
|
|
1981
|
-
/**
|
|
1982
|
-
A StepMap that contains no changed ranges.
|
|
1983
|
-
*/
|
|
1984
|
-
StepMap.empty = new StepMap([]);
|
|
1985
|
-
const stepsByID = Object.create(null);
|
|
1986
|
-
/**
|
|
1987
|
-
A step object represents an atomic change. It generally applies
|
|
1988
|
-
only to the document it was created for, since the positions
|
|
1989
|
-
stored in it will only make sense for that document.
|
|
1990
|
-
|
|
1991
|
-
New steps are defined by creating classes that extend `Step`,
|
|
1992
|
-
overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
|
|
1993
|
-
methods, and registering your class with a unique
|
|
1994
|
-
JSON-serialization identifier using
|
|
1995
|
-
[`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
|
|
1996
|
-
*/
|
|
1997
|
-
var Step = class {
|
|
1998
|
-
/**
|
|
1999
|
-
Get the step map that represents the changes made by this step,
|
|
2000
|
-
and which can be used to transform between positions in the old
|
|
2001
|
-
and the new document.
|
|
2002
|
-
*/
|
|
2003
|
-
getMap() {
|
|
2004
|
-
return StepMap.empty;
|
|
2005
|
-
}
|
|
2006
|
-
/**
|
|
2007
|
-
Try to merge this step with another one, to be applied directly
|
|
2008
|
-
after it. Returns the merged step when possible, null if the
|
|
2009
|
-
steps can't be merged.
|
|
2010
|
-
*/
|
|
2011
|
-
merge(other) {
|
|
2012
|
-
return null;
|
|
2013
|
-
}
|
|
2014
|
-
/**
|
|
2015
|
-
Deserialize a step from its JSON representation. Will call
|
|
2016
|
-
through to the step class' own implementation of this method.
|
|
2017
|
-
*/
|
|
2018
|
-
static fromJSON(schema, json) {
|
|
2019
|
-
if (!json || !json.stepType) throw new RangeError("Invalid input for Step.fromJSON");
|
|
2020
|
-
let type = stepsByID[json.stepType];
|
|
2021
|
-
if (!type) throw new RangeError(`No step type ${json.stepType} defined`);
|
|
2022
|
-
return type.fromJSON(schema, json);
|
|
2023
|
-
}
|
|
2024
|
-
/**
|
|
2025
|
-
To be able to serialize steps to JSON, each step needs a string
|
|
2026
|
-
ID to attach to its JSON representation. Use this method to
|
|
2027
|
-
register an ID for your step classes. Try to pick something
|
|
2028
|
-
that's unlikely to clash with steps from other modules.
|
|
2029
|
-
*/
|
|
2030
|
-
static jsonID(id, stepClass) {
|
|
2031
|
-
if (id in stepsByID) throw new RangeError("Duplicate use of step JSON ID " + id);
|
|
2032
|
-
stepsByID[id] = stepClass;
|
|
2033
|
-
stepClass.prototype.jsonID = id;
|
|
2034
|
-
return stepClass;
|
|
2035
|
-
}
|
|
2036
|
-
};
|
|
2037
|
-
/**
|
|
2038
|
-
The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
|
|
2039
|
-
new document or a failure value.
|
|
2040
|
-
*/
|
|
2041
|
-
var StepResult = class StepResult {
|
|
2042
|
-
/**
|
|
2043
|
-
@internal
|
|
2044
|
-
*/
|
|
2045
|
-
constructor(doc, failed) {
|
|
2046
|
-
this.doc = doc;
|
|
2047
|
-
this.failed = failed;
|
|
2048
|
-
}
|
|
2049
|
-
/**
|
|
2050
|
-
Create a successful step result.
|
|
2051
|
-
*/
|
|
2052
|
-
static ok(doc) {
|
|
2053
|
-
return new StepResult(doc, null);
|
|
2054
|
-
}
|
|
2055
|
-
/**
|
|
2056
|
-
Create a failed step result.
|
|
2057
|
-
*/
|
|
2058
|
-
static fail(message) {
|
|
2059
|
-
return new StepResult(null, message);
|
|
2060
|
-
}
|
|
2061
|
-
/**
|
|
2062
|
-
Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
|
|
2063
|
-
arguments. Create a successful result if it succeeds, and a
|
|
2064
|
-
failed one if it throws a `ReplaceError`.
|
|
2065
|
-
*/
|
|
2066
|
-
static fromReplace(doc, from, to, slice) {
|
|
2067
|
-
try {
|
|
2068
|
-
return StepResult.ok(doc.replace(from, to, slice));
|
|
2069
|
-
} catch (e) {
|
|
2070
|
-
if (e instanceof ReplaceError) return StepResult.fail(e.message);
|
|
2071
|
-
throw e;
|
|
2072
|
-
}
|
|
2073
|
-
}
|
|
2074
|
-
};
|
|
2075
|
-
function mapFragment(fragment, f, parent) {
|
|
2076
|
-
let mapped = [];
|
|
2077
|
-
for (let i = 0; i < fragment.childCount; i++) {
|
|
2078
|
-
let child = fragment.child(i);
|
|
2079
|
-
if (child.content.size) child = child.copy(mapFragment(child.content, f, child));
|
|
2080
|
-
if (child.isInline) child = f(child, parent, i);
|
|
2081
|
-
mapped.push(child);
|
|
2082
|
-
}
|
|
2083
|
-
return Fragment.fromArray(mapped);
|
|
2084
|
-
}
|
|
2085
|
-
/**
|
|
2086
|
-
Add a mark to all inline content between two positions.
|
|
2087
|
-
*/
|
|
2088
|
-
var AddMarkStep = class AddMarkStep extends Step {
|
|
2089
|
-
/**
|
|
2090
|
-
Create a mark step.
|
|
2091
|
-
*/
|
|
2092
|
-
constructor(from, to, mark) {
|
|
2093
|
-
super();
|
|
2094
|
-
this.from = from;
|
|
2095
|
-
this.to = to;
|
|
2096
|
-
this.mark = mark;
|
|
2097
|
-
}
|
|
2098
|
-
apply(doc) {
|
|
2099
|
-
let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
|
|
2100
|
-
let parent = $from.node($from.sharedDepth(this.to));
|
|
2101
|
-
let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
|
|
2102
|
-
if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type)) return node;
|
|
2103
|
-
return node.mark(this.mark.addToSet(node.marks));
|
|
2104
|
-
}, parent), oldSlice.openStart, oldSlice.openEnd);
|
|
2105
|
-
return StepResult.fromReplace(doc, this.from, this.to, slice);
|
|
2106
|
-
}
|
|
2107
|
-
invert() {
|
|
2108
|
-
return new RemoveMarkStep(this.from, this.to, this.mark);
|
|
2109
|
-
}
|
|
2110
|
-
map(mapping) {
|
|
2111
|
-
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
2112
|
-
if (from.deleted && to.deleted || from.pos >= to.pos) return null;
|
|
2113
|
-
return new AddMarkStep(from.pos, to.pos, this.mark);
|
|
2114
|
-
}
|
|
2115
|
-
merge(other) {
|
|
2116
|
-
if (other instanceof AddMarkStep && other.mark.eq(this.mark) && this.from <= other.to && this.to >= other.from) return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
|
|
2117
|
-
return null;
|
|
2118
|
-
}
|
|
2119
|
-
toJSON() {
|
|
2120
|
-
return {
|
|
2121
|
-
stepType: "addMark",
|
|
2122
|
-
mark: this.mark.toJSON(),
|
|
2123
|
-
from: this.from,
|
|
2124
|
-
to: this.to
|
|
2125
|
-
};
|
|
2126
|
-
}
|
|
2127
|
-
/**
|
|
2128
|
-
@internal
|
|
2129
|
-
*/
|
|
2130
|
-
static fromJSON(schema, json) {
|
|
2131
|
-
if (typeof json.from != "number" || typeof json.to != "number") throw new RangeError("Invalid input for AddMarkStep.fromJSON");
|
|
2132
|
-
return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
|
|
2133
|
-
}
|
|
2134
|
-
};
|
|
2135
|
-
Step.jsonID("addMark", AddMarkStep);
|
|
2136
|
-
/**
|
|
2137
|
-
Remove a mark from all inline content between two positions.
|
|
2138
|
-
*/
|
|
2139
|
-
var RemoveMarkStep = class RemoveMarkStep extends Step {
|
|
2140
|
-
/**
|
|
2141
|
-
Create a mark-removing step.
|
|
2142
|
-
*/
|
|
2143
|
-
constructor(from, to, mark) {
|
|
2144
|
-
super();
|
|
2145
|
-
this.from = from;
|
|
2146
|
-
this.to = to;
|
|
2147
|
-
this.mark = mark;
|
|
2148
|
-
}
|
|
2149
|
-
apply(doc) {
|
|
2150
|
-
let oldSlice = doc.slice(this.from, this.to);
|
|
2151
|
-
let slice = new Slice(mapFragment(oldSlice.content, (node) => {
|
|
2152
|
-
return node.mark(this.mark.removeFromSet(node.marks));
|
|
2153
|
-
}, doc), oldSlice.openStart, oldSlice.openEnd);
|
|
2154
|
-
return StepResult.fromReplace(doc, this.from, this.to, slice);
|
|
2155
|
-
}
|
|
2156
|
-
invert() {
|
|
2157
|
-
return new AddMarkStep(this.from, this.to, this.mark);
|
|
2158
|
-
}
|
|
2159
|
-
map(mapping) {
|
|
2160
|
-
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
2161
|
-
if (from.deleted && to.deleted || from.pos >= to.pos) return null;
|
|
2162
|
-
return new RemoveMarkStep(from.pos, to.pos, this.mark);
|
|
2163
|
-
}
|
|
2164
|
-
merge(other) {
|
|
2165
|
-
if (other instanceof RemoveMarkStep && other.mark.eq(this.mark) && this.from <= other.to && this.to >= other.from) return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
|
|
2166
|
-
return null;
|
|
2167
|
-
}
|
|
2168
|
-
toJSON() {
|
|
2169
|
-
return {
|
|
2170
|
-
stepType: "removeMark",
|
|
2171
|
-
mark: this.mark.toJSON(),
|
|
2172
|
-
from: this.from,
|
|
2173
|
-
to: this.to
|
|
2174
|
-
};
|
|
2175
|
-
}
|
|
2176
|
-
/**
|
|
2177
|
-
@internal
|
|
2178
|
-
*/
|
|
2179
|
-
static fromJSON(schema, json) {
|
|
2180
|
-
if (typeof json.from != "number" || typeof json.to != "number") throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
|
|
2181
|
-
return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
|
|
2182
|
-
}
|
|
2183
|
-
};
|
|
2184
|
-
Step.jsonID("removeMark", RemoveMarkStep);
|
|
2185
|
-
/**
|
|
2186
|
-
Add a mark to a specific node.
|
|
2187
|
-
*/
|
|
2188
|
-
var AddNodeMarkStep = class AddNodeMarkStep extends Step {
|
|
2189
|
-
/**
|
|
2190
|
-
Create a node mark step.
|
|
2191
|
-
*/
|
|
2192
|
-
constructor(pos, mark) {
|
|
2193
|
-
super();
|
|
2194
|
-
this.pos = pos;
|
|
2195
|
-
this.mark = mark;
|
|
2196
|
-
}
|
|
2197
|
-
apply(doc) {
|
|
2198
|
-
let node = doc.nodeAt(this.pos);
|
|
2199
|
-
if (!node) return StepResult.fail("No node at mark step's position");
|
|
2200
|
-
let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
|
|
2201
|
-
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
2202
|
-
}
|
|
2203
|
-
invert(doc) {
|
|
2204
|
-
let node = doc.nodeAt(this.pos);
|
|
2205
|
-
if (node) {
|
|
2206
|
-
let newSet = this.mark.addToSet(node.marks);
|
|
2207
|
-
if (newSet.length == node.marks.length) {
|
|
2208
|
-
for (let i = 0; i < node.marks.length; i++) if (!node.marks[i].isInSet(newSet)) return new AddNodeMarkStep(this.pos, node.marks[i]);
|
|
2209
|
-
return new AddNodeMarkStep(this.pos, this.mark);
|
|
2210
|
-
}
|
|
2211
|
-
}
|
|
2212
|
-
return new RemoveNodeMarkStep(this.pos, this.mark);
|
|
2213
|
-
}
|
|
2214
|
-
map(mapping) {
|
|
2215
|
-
let pos = mapping.mapResult(this.pos, 1);
|
|
2216
|
-
return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
|
|
2217
|
-
}
|
|
2218
|
-
toJSON() {
|
|
2219
|
-
return {
|
|
2220
|
-
stepType: "addNodeMark",
|
|
2221
|
-
pos: this.pos,
|
|
2222
|
-
mark: this.mark.toJSON()
|
|
2223
|
-
};
|
|
2224
|
-
}
|
|
2225
|
-
/**
|
|
2226
|
-
@internal
|
|
2227
|
-
*/
|
|
2228
|
-
static fromJSON(schema, json) {
|
|
2229
|
-
if (typeof json.pos != "number") throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
|
|
2230
|
-
return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
|
|
2231
|
-
}
|
|
2232
|
-
};
|
|
2233
|
-
Step.jsonID("addNodeMark", AddNodeMarkStep);
|
|
2234
|
-
/**
|
|
2235
|
-
Remove a mark from a specific node.
|
|
2236
|
-
*/
|
|
2237
|
-
var RemoveNodeMarkStep = class RemoveNodeMarkStep extends Step {
|
|
2238
|
-
/**
|
|
2239
|
-
Create a mark-removing step.
|
|
2240
|
-
*/
|
|
2241
|
-
constructor(pos, mark) {
|
|
2242
|
-
super();
|
|
2243
|
-
this.pos = pos;
|
|
2244
|
-
this.mark = mark;
|
|
2245
|
-
}
|
|
2246
|
-
apply(doc) {
|
|
2247
|
-
let node = doc.nodeAt(this.pos);
|
|
2248
|
-
if (!node) return StepResult.fail("No node at mark step's position");
|
|
2249
|
-
let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
|
|
2250
|
-
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
2251
|
-
}
|
|
2252
|
-
invert(doc) {
|
|
2253
|
-
let node = doc.nodeAt(this.pos);
|
|
2254
|
-
if (!node || !this.mark.isInSet(node.marks)) return this;
|
|
2255
|
-
return new AddNodeMarkStep(this.pos, this.mark);
|
|
2256
|
-
}
|
|
2257
|
-
map(mapping) {
|
|
2258
|
-
let pos = mapping.mapResult(this.pos, 1);
|
|
2259
|
-
return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
|
|
2260
|
-
}
|
|
2261
|
-
toJSON() {
|
|
2262
|
-
return {
|
|
2263
|
-
stepType: "removeNodeMark",
|
|
2264
|
-
pos: this.pos,
|
|
2265
|
-
mark: this.mark.toJSON()
|
|
2266
|
-
};
|
|
2267
|
-
}
|
|
2268
|
-
/**
|
|
2269
|
-
@internal
|
|
2270
|
-
*/
|
|
2271
|
-
static fromJSON(schema, json) {
|
|
2272
|
-
if (typeof json.pos != "number") throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
|
|
2273
|
-
return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
|
|
2274
|
-
}
|
|
2275
|
-
};
|
|
2276
|
-
Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
|
|
2277
|
-
/**
|
|
2278
|
-
Replace a part of the document with a slice of new content.
|
|
2279
|
-
*/
|
|
2280
|
-
var ReplaceStep = class ReplaceStep extends Step {
|
|
2281
|
-
/**
|
|
2282
|
-
The given `slice` should fit the 'gap' between `from` and
|
|
2283
|
-
`to`—the depths must line up, and the surrounding nodes must be
|
|
2284
|
-
able to be joined with the open sides of the slice. When
|
|
2285
|
-
`structure` is true, the step will fail if the content between
|
|
2286
|
-
from and to is not just a sequence of closing and then opening
|
|
2287
|
-
tokens (this is to guard against rebased replace steps
|
|
2288
|
-
overwriting something they weren't supposed to).
|
|
2289
|
-
*/
|
|
2290
|
-
constructor(from, to, slice, structure = false) {
|
|
2291
|
-
super();
|
|
2292
|
-
this.from = from;
|
|
2293
|
-
this.to = to;
|
|
2294
|
-
this.slice = slice;
|
|
2295
|
-
this.structure = structure;
|
|
2296
|
-
}
|
|
2297
|
-
apply(doc) {
|
|
2298
|
-
if (this.structure && contentBetween(doc, this.from, this.to)) return StepResult.fail("Structure replace would overwrite content");
|
|
2299
|
-
return StepResult.fromReplace(doc, this.from, this.to, this.slice);
|
|
2300
|
-
}
|
|
2301
|
-
getMap() {
|
|
2302
|
-
return new StepMap([
|
|
2303
|
-
this.from,
|
|
2304
|
-
this.to - this.from,
|
|
2305
|
-
this.slice.size
|
|
2306
|
-
]);
|
|
2307
|
-
}
|
|
2308
|
-
invert(doc) {
|
|
2309
|
-
return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
|
|
2310
|
-
}
|
|
2311
|
-
map(mapping) {
|
|
2312
|
-
let to = mapping.mapResult(this.to, -1);
|
|
2313
|
-
let from = this.from == this.to && ReplaceStep.MAP_BIAS < 0 ? to : mapping.mapResult(this.from, 1);
|
|
2314
|
-
if (from.deletedAcross && to.deletedAcross) return null;
|
|
2315
|
-
return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
|
|
2316
|
-
}
|
|
2317
|
-
merge(other) {
|
|
2318
|
-
if (!(other instanceof ReplaceStep) || other.structure || this.structure) return null;
|
|
2319
|
-
if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
|
|
2320
|
-
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty : new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
|
|
2321
|
-
return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
|
|
2322
|
-
} else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
|
|
2323
|
-
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty : new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
|
|
2324
|
-
return new ReplaceStep(other.from, this.to, slice, this.structure);
|
|
2325
|
-
} else return null;
|
|
2326
|
-
}
|
|
2327
|
-
toJSON() {
|
|
2328
|
-
let json = {
|
|
2329
|
-
stepType: "replace",
|
|
2330
|
-
from: this.from,
|
|
2331
|
-
to: this.to
|
|
2332
|
-
};
|
|
2333
|
-
if (this.slice.size) json.slice = this.slice.toJSON();
|
|
2334
|
-
if (this.structure) json.structure = true;
|
|
2335
|
-
return json;
|
|
2336
|
-
}
|
|
2337
|
-
/**
|
|
2338
|
-
@internal
|
|
2339
|
-
*/
|
|
2340
|
-
static fromJSON(schema, json) {
|
|
2341
|
-
if (typeof json.from != "number" || typeof json.to != "number") throw new RangeError("Invalid input for ReplaceStep.fromJSON");
|
|
2342
|
-
return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
|
|
2343
|
-
}
|
|
2344
|
-
};
|
|
2345
|
-
/**
|
|
2346
|
-
By default, for backwards compatibility, an inserting step
|
|
2347
|
-
mapped over an insertion at that same position fill move after
|
|
2348
|
-
the inserted content. In a collaborative editing situation, that
|
|
2349
|
-
can make redone insertions appear in unexpected places. You can
|
|
2350
|
-
set this to -1 to make such mapping keep the step before the
|
|
2351
|
-
insertion instead.
|
|
2352
|
-
*/
|
|
2353
|
-
ReplaceStep.MAP_BIAS = 1;
|
|
2354
|
-
Step.jsonID("replace", ReplaceStep);
|
|
2355
|
-
/**
|
|
2356
|
-
Replace a part of the document with a slice of content, but
|
|
2357
|
-
preserve a range of the replaced content by moving it into the
|
|
2358
|
-
slice.
|
|
2359
|
-
*/
|
|
2360
|
-
var ReplaceAroundStep = class ReplaceAroundStep extends Step {
|
|
2361
|
-
/**
|
|
2362
|
-
Create a replace-around step with the given range and gap.
|
|
2363
|
-
`insert` should be the point in the slice into which the content
|
|
2364
|
-
of the gap should be moved. `structure` has the same meaning as
|
|
2365
|
-
it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
|
|
2366
|
-
*/
|
|
2367
|
-
constructor(from, to, gapFrom, gapTo, slice, insert, structure = false) {
|
|
2368
|
-
super();
|
|
2369
|
-
this.from = from;
|
|
2370
|
-
this.to = to;
|
|
2371
|
-
this.gapFrom = gapFrom;
|
|
2372
|
-
this.gapTo = gapTo;
|
|
2373
|
-
this.slice = slice;
|
|
2374
|
-
this.insert = insert;
|
|
2375
|
-
this.structure = structure;
|
|
2376
|
-
}
|
|
2377
|
-
apply(doc) {
|
|
2378
|
-
if (this.structure && (contentBetween(doc, this.from, this.gapFrom) || contentBetween(doc, this.gapTo, this.to))) return StepResult.fail("Structure gap-replace would overwrite content");
|
|
2379
|
-
let gap = doc.slice(this.gapFrom, this.gapTo);
|
|
2380
|
-
if (gap.openStart || gap.openEnd) return StepResult.fail("Gap is not a flat range");
|
|
2381
|
-
let inserted = this.slice.insertAt(this.insert, gap.content);
|
|
2382
|
-
if (!inserted) return StepResult.fail("Content does not fit in gap");
|
|
2383
|
-
return StepResult.fromReplace(doc, this.from, this.to, inserted);
|
|
2384
|
-
}
|
|
2385
|
-
getMap() {
|
|
2386
|
-
return new StepMap([
|
|
2387
|
-
this.from,
|
|
2388
|
-
this.gapFrom - this.from,
|
|
2389
|
-
this.insert,
|
|
2390
|
-
this.gapTo,
|
|
2391
|
-
this.to - this.gapTo,
|
|
2392
|
-
this.slice.size - this.insert
|
|
2393
|
-
]);
|
|
2394
|
-
}
|
|
2395
|
-
invert(doc) {
|
|
2396
|
-
let gap = this.gapTo - this.gapFrom;
|
|
2397
|
-
return new ReplaceAroundStep(this.from, this.from + this.slice.size + gap, this.from + this.insert, this.from + this.insert + gap, doc.slice(this.from, this.to).removeBetween(this.gapFrom - this.from, this.gapTo - this.from), this.gapFrom - this.from, this.structure);
|
|
2398
|
-
}
|
|
2399
|
-
map(mapping) {
|
|
2400
|
-
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
2401
|
-
let gapFrom = this.from == this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1);
|
|
2402
|
-
let gapTo = this.to == this.gapTo ? to.pos : mapping.map(this.gapTo, 1);
|
|
2403
|
-
if (from.deletedAcross && to.deletedAcross || gapFrom < from.pos || gapTo > to.pos) return null;
|
|
2404
|
-
return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
|
|
2405
|
-
}
|
|
2406
|
-
toJSON() {
|
|
2407
|
-
let json = {
|
|
2408
|
-
stepType: "replaceAround",
|
|
2409
|
-
from: this.from,
|
|
2410
|
-
to: this.to,
|
|
2411
|
-
gapFrom: this.gapFrom,
|
|
2412
|
-
gapTo: this.gapTo,
|
|
2413
|
-
insert: this.insert
|
|
2414
|
-
};
|
|
2415
|
-
if (this.slice.size) json.slice = this.slice.toJSON();
|
|
2416
|
-
if (this.structure) json.structure = true;
|
|
2417
|
-
return json;
|
|
2418
|
-
}
|
|
2419
|
-
/**
|
|
2420
|
-
@internal
|
|
2421
|
-
*/
|
|
2422
|
-
static fromJSON(schema, json) {
|
|
2423
|
-
if (typeof json.from != "number" || typeof json.to != "number" || typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number") throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
|
|
2424
|
-
return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
|
|
2425
|
-
}
|
|
2426
|
-
};
|
|
2427
|
-
Step.jsonID("replaceAround", ReplaceAroundStep);
|
|
2428
|
-
function contentBetween(doc, from, to) {
|
|
2429
|
-
let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
|
|
2430
|
-
while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
|
|
2431
|
-
depth--;
|
|
2432
|
-
dist--;
|
|
2433
|
-
}
|
|
2434
|
-
if (dist > 0) {
|
|
2435
|
-
let next = $from.node(depth).maybeChild($from.indexAfter(depth));
|
|
2436
|
-
while (dist > 0) {
|
|
2437
|
-
if (!next || next.isLeaf) return true;
|
|
2438
|
-
next = next.firstChild;
|
|
2439
|
-
dist--;
|
|
2440
|
-
}
|
|
2441
|
-
}
|
|
2442
|
-
return false;
|
|
2443
|
-
}
|
|
2444
|
-
/**
|
|
2445
|
-
Update an attribute in a specific node.
|
|
2446
|
-
*/
|
|
2447
|
-
var AttrStep = class AttrStep extends Step {
|
|
2448
|
-
/**
|
|
2449
|
-
Construct an attribute step.
|
|
2450
|
-
*/
|
|
2451
|
-
constructor(pos, attr, value) {
|
|
2452
|
-
super();
|
|
2453
|
-
this.pos = pos;
|
|
2454
|
-
this.attr = attr;
|
|
2455
|
-
this.value = value;
|
|
2456
|
-
}
|
|
2457
|
-
apply(doc) {
|
|
2458
|
-
let node = doc.nodeAt(this.pos);
|
|
2459
|
-
if (!node) return StepResult.fail("No node at attribute step's position");
|
|
2460
|
-
let attrs = Object.create(null);
|
|
2461
|
-
for (let name in node.attrs) attrs[name] = node.attrs[name];
|
|
2462
|
-
attrs[this.attr] = this.value;
|
|
2463
|
-
let updated = node.type.create(attrs, null, node.marks);
|
|
2464
|
-
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
2465
|
-
}
|
|
2466
|
-
getMap() {
|
|
2467
|
-
return StepMap.empty;
|
|
2468
|
-
}
|
|
2469
|
-
invert(doc) {
|
|
2470
|
-
return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
|
|
2471
|
-
}
|
|
2472
|
-
map(mapping) {
|
|
2473
|
-
let pos = mapping.mapResult(this.pos, 1);
|
|
2474
|
-
return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
|
|
2475
|
-
}
|
|
2476
|
-
toJSON() {
|
|
2477
|
-
return {
|
|
2478
|
-
stepType: "attr",
|
|
2479
|
-
pos: this.pos,
|
|
2480
|
-
attr: this.attr,
|
|
2481
|
-
value: this.value
|
|
2482
|
-
};
|
|
2483
|
-
}
|
|
2484
|
-
static fromJSON(schema, json) {
|
|
2485
|
-
if (typeof json.pos != "number" || typeof json.attr != "string") throw new RangeError("Invalid input for AttrStep.fromJSON");
|
|
2486
|
-
return new AttrStep(json.pos, json.attr, json.value);
|
|
2487
|
-
}
|
|
2488
|
-
};
|
|
2489
|
-
Step.jsonID("attr", AttrStep);
|
|
2490
|
-
/**
|
|
2491
|
-
Update an attribute in the doc node.
|
|
2492
|
-
*/
|
|
2493
|
-
var DocAttrStep = class DocAttrStep extends Step {
|
|
2494
|
-
/**
|
|
2495
|
-
Construct an attribute step.
|
|
2496
|
-
*/
|
|
2497
|
-
constructor(attr, value) {
|
|
2498
|
-
super();
|
|
2499
|
-
this.attr = attr;
|
|
2500
|
-
this.value = value;
|
|
2501
|
-
}
|
|
2502
|
-
apply(doc) {
|
|
2503
|
-
let attrs = Object.create(null);
|
|
2504
|
-
for (let name in doc.attrs) attrs[name] = doc.attrs[name];
|
|
2505
|
-
attrs[this.attr] = this.value;
|
|
2506
|
-
let updated = doc.type.create(attrs, doc.content, doc.marks);
|
|
2507
|
-
return StepResult.ok(updated);
|
|
2508
|
-
}
|
|
2509
|
-
getMap() {
|
|
2510
|
-
return StepMap.empty;
|
|
2511
|
-
}
|
|
2512
|
-
invert(doc) {
|
|
2513
|
-
return new DocAttrStep(this.attr, doc.attrs[this.attr]);
|
|
2514
|
-
}
|
|
2515
|
-
map(mapping) {
|
|
2516
|
-
return this;
|
|
2517
|
-
}
|
|
2518
|
-
toJSON() {
|
|
2519
|
-
return {
|
|
2520
|
-
stepType: "docAttr",
|
|
2521
|
-
attr: this.attr,
|
|
2522
|
-
value: this.value
|
|
2523
|
-
};
|
|
2524
|
-
}
|
|
2525
|
-
static fromJSON(schema, json) {
|
|
2526
|
-
if (typeof json.attr != "string") throw new RangeError("Invalid input for DocAttrStep.fromJSON");
|
|
2527
|
-
return new DocAttrStep(json.attr, json.value);
|
|
2528
|
-
}
|
|
2529
|
-
};
|
|
2530
|
-
Step.jsonID("docAttr", DocAttrStep);
|
|
2531
|
-
/**
|
|
2532
|
-
@internal
|
|
2533
|
-
*/
|
|
2534
|
-
let TransformError = class extends Error {};
|
|
2535
|
-
TransformError = function TransformError(message) {
|
|
2536
|
-
let err = Error.call(this, message);
|
|
2537
|
-
err.__proto__ = TransformError.prototype;
|
|
2538
|
-
return err;
|
|
2539
|
-
};
|
|
2540
|
-
TransformError.prototype = Object.create(Error.prototype);
|
|
2541
|
-
TransformError.prototype.constructor = TransformError;
|
|
2542
|
-
TransformError.prototype.name = "TransformError";
|
|
2543
|
-
//#endregion
|
|
2544
|
-
//#region ../../node_modules/.pnpm/prosemirror-state@1.4.4/node_modules/prosemirror-state/dist/index.js
|
|
2545
|
-
const classesById = Object.create(null);
|
|
2546
|
-
/**
|
|
2547
|
-
Superclass for editor selections. Every selection type should
|
|
2548
|
-
extend this. Should not be instantiated directly.
|
|
2549
|
-
*/
|
|
2550
|
-
var Selection = class {
|
|
2551
|
-
/**
|
|
2552
|
-
Initialize a selection with the head and anchor and ranges. If no
|
|
2553
|
-
ranges are given, constructs a single range across `$anchor` and
|
|
2554
|
-
`$head`.
|
|
2555
|
-
*/
|
|
2556
|
-
constructor($anchor, $head, ranges) {
|
|
2557
|
-
this.$anchor = $anchor;
|
|
2558
|
-
this.$head = $head;
|
|
2559
|
-
this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
|
|
2560
|
-
}
|
|
2561
|
-
/**
|
|
2562
|
-
The selection's anchor, as an unresolved position.
|
|
2563
|
-
*/
|
|
2564
|
-
get anchor() {
|
|
2565
|
-
return this.$anchor.pos;
|
|
2566
|
-
}
|
|
2567
|
-
/**
|
|
2568
|
-
The selection's head.
|
|
2569
|
-
*/
|
|
2570
|
-
get head() {
|
|
2571
|
-
return this.$head.pos;
|
|
2572
|
-
}
|
|
2573
|
-
/**
|
|
2574
|
-
The lower bound of the selection's main range.
|
|
2575
|
-
*/
|
|
2576
|
-
get from() {
|
|
2577
|
-
return this.$from.pos;
|
|
2578
|
-
}
|
|
2579
|
-
/**
|
|
2580
|
-
The upper bound of the selection's main range.
|
|
2581
|
-
*/
|
|
2582
|
-
get to() {
|
|
2583
|
-
return this.$to.pos;
|
|
2584
|
-
}
|
|
2585
|
-
/**
|
|
2586
|
-
The resolved lower bound of the selection's main range.
|
|
2587
|
-
*/
|
|
2588
|
-
get $from() {
|
|
2589
|
-
return this.ranges[0].$from;
|
|
2590
|
-
}
|
|
2591
|
-
/**
|
|
2592
|
-
The resolved upper bound of the selection's main range.
|
|
2593
|
-
*/
|
|
2594
|
-
get $to() {
|
|
2595
|
-
return this.ranges[0].$to;
|
|
2596
|
-
}
|
|
2597
|
-
/**
|
|
2598
|
-
Indicates whether the selection contains any content.
|
|
2599
|
-
*/
|
|
2600
|
-
get empty() {
|
|
2601
|
-
let ranges = this.ranges;
|
|
2602
|
-
for (let i = 0; i < ranges.length; i++) if (ranges[i].$from.pos != ranges[i].$to.pos) return false;
|
|
2603
|
-
return true;
|
|
2604
|
-
}
|
|
2605
|
-
/**
|
|
2606
|
-
Get the content of this selection as a slice.
|
|
2607
|
-
*/
|
|
2608
|
-
content() {
|
|
2609
|
-
return this.$from.doc.slice(this.from, this.to, true);
|
|
2610
|
-
}
|
|
2611
|
-
/**
|
|
2612
|
-
Replace the selection with a slice or, if no slice is given,
|
|
2613
|
-
delete the selection. Will append to the given transaction.
|
|
2614
|
-
*/
|
|
2615
|
-
replace(tr, content = Slice.empty) {
|
|
2616
|
-
let lastNode = content.content.lastChild, lastParent = null;
|
|
2617
|
-
for (let i = 0; i < content.openEnd; i++) {
|
|
2618
|
-
lastParent = lastNode;
|
|
2619
|
-
lastNode = lastNode.lastChild;
|
|
2620
|
-
}
|
|
2621
|
-
let mapFrom = tr.steps.length, ranges = this.ranges;
|
|
2622
|
-
for (let i = 0; i < ranges.length; i++) {
|
|
2623
|
-
let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
2624
|
-
tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
|
|
2625
|
-
if (i == 0) selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
|
|
2626
|
-
}
|
|
2627
|
-
}
|
|
2628
|
-
/**
|
|
2629
|
-
Replace the selection with the given node, appending the changes
|
|
2630
|
-
to the given transaction.
|
|
2631
|
-
*/
|
|
2632
|
-
replaceWith(tr, node) {
|
|
2633
|
-
let mapFrom = tr.steps.length, ranges = this.ranges;
|
|
2634
|
-
for (let i = 0; i < ranges.length; i++) {
|
|
2635
|
-
let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
2636
|
-
let from = mapping.map($from.pos), to = mapping.map($to.pos);
|
|
2637
|
-
if (i) tr.deleteRange(from, to);
|
|
2638
|
-
else {
|
|
2639
|
-
tr.replaceRangeWith(from, to, node);
|
|
2640
|
-
selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
|
|
2641
|
-
}
|
|
2642
|
-
}
|
|
2643
|
-
}
|
|
2644
|
-
/**
|
|
2645
|
-
Find a valid cursor or leaf node selection starting at the given
|
|
2646
|
-
position and searching back if `dir` is negative, and forward if
|
|
2647
|
-
positive. When `textOnly` is true, only consider cursor
|
|
2648
|
-
selections. Will return null when no valid selection position is
|
|
2649
|
-
found.
|
|
2650
|
-
*/
|
|
2651
|
-
static findFrom($pos, dir, textOnly = false) {
|
|
2652
|
-
let inner = $pos.parent.inlineContent ? new TextSelection($pos) : findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
|
|
2653
|
-
if (inner) return inner;
|
|
2654
|
-
for (let depth = $pos.depth - 1; depth >= 0; depth--) {
|
|
2655
|
-
let found = dir < 0 ? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly) : findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
|
|
2656
|
-
if (found) return found;
|
|
2657
|
-
}
|
|
2658
|
-
return null;
|
|
2659
|
-
}
|
|
2660
|
-
/**
|
|
2661
|
-
Find a valid cursor or leaf node selection near the given
|
|
2662
|
-
position. Searches forward first by default, but if `bias` is
|
|
2663
|
-
negative, it will search backwards first.
|
|
2664
|
-
*/
|
|
2665
|
-
static near($pos, bias = 1) {
|
|
2666
|
-
return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
|
|
2667
|
-
}
|
|
2668
|
-
/**
|
|
2669
|
-
Find the cursor or leaf node selection closest to the start of
|
|
2670
|
-
the given document. Will return an
|
|
2671
|
-
[`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
|
|
2672
|
-
exists.
|
|
2673
|
-
*/
|
|
2674
|
-
static atStart(doc) {
|
|
2675
|
-
return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
|
|
2676
|
-
}
|
|
2677
|
-
/**
|
|
2678
|
-
Find the cursor or leaf node selection closest to the end of the
|
|
2679
|
-
given document.
|
|
2680
|
-
*/
|
|
2681
|
-
static atEnd(doc) {
|
|
2682
|
-
return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
|
|
2683
|
-
}
|
|
2684
|
-
/**
|
|
2685
|
-
Deserialize the JSON representation of a selection. Must be
|
|
2686
|
-
implemented for custom classes (as a static class method).
|
|
2687
|
-
*/
|
|
2688
|
-
static fromJSON(doc, json) {
|
|
2689
|
-
if (!json || !json.type) throw new RangeError("Invalid input for Selection.fromJSON");
|
|
2690
|
-
let cls = classesById[json.type];
|
|
2691
|
-
if (!cls) throw new RangeError(`No selection type ${json.type} defined`);
|
|
2692
|
-
return cls.fromJSON(doc, json);
|
|
2693
|
-
}
|
|
2694
|
-
/**
|
|
2695
|
-
To be able to deserialize selections from JSON, custom selection
|
|
2696
|
-
classes must register themselves with an ID string, so that they
|
|
2697
|
-
can be disambiguated. Try to pick something that's unlikely to
|
|
2698
|
-
clash with classes from other modules.
|
|
2699
|
-
*/
|
|
2700
|
-
static jsonID(id, selectionClass) {
|
|
2701
|
-
if (id in classesById) throw new RangeError("Duplicate use of selection JSON ID " + id);
|
|
2702
|
-
classesById[id] = selectionClass;
|
|
2703
|
-
selectionClass.prototype.jsonID = id;
|
|
2704
|
-
return selectionClass;
|
|
2705
|
-
}
|
|
2706
|
-
/**
|
|
2707
|
-
Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
|
|
2708
|
-
which is a value that can be mapped without having access to a
|
|
2709
|
-
current document, and later resolved to a real selection for a
|
|
2710
|
-
given document again. (This is used mostly by the history to
|
|
2711
|
-
track and restore old selections.) The default implementation of
|
|
2712
|
-
this method just converts the selection to a text selection and
|
|
2713
|
-
returns the bookmark for that.
|
|
2714
|
-
*/
|
|
2715
|
-
getBookmark() {
|
|
2716
|
-
return TextSelection.between(this.$anchor, this.$head).getBookmark();
|
|
2717
|
-
}
|
|
2718
|
-
};
|
|
2719
|
-
Selection.prototype.visible = true;
|
|
2720
|
-
/**
|
|
2721
|
-
Represents a selected range in a document.
|
|
2722
|
-
*/
|
|
2723
|
-
var SelectionRange = class {
|
|
2724
|
-
/**
|
|
2725
|
-
Create a range.
|
|
2726
|
-
*/
|
|
2727
|
-
constructor($from, $to) {
|
|
2728
|
-
this.$from = $from;
|
|
2729
|
-
this.$to = $to;
|
|
2730
|
-
}
|
|
2731
|
-
};
|
|
2732
|
-
let warnedAboutTextSelection = false;
|
|
2733
|
-
function checkTextSelection($pos) {
|
|
2734
|
-
if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
|
|
2735
|
-
warnedAboutTextSelection = true;
|
|
2736
|
-
console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
|
|
2737
|
-
}
|
|
2738
|
-
}
|
|
2739
|
-
/**
|
|
2740
|
-
A text selection represents a classical editor selection, with a
|
|
2741
|
-
head (the moving side) and anchor (immobile side), both of which
|
|
2742
|
-
point into textblock nodes. It can be empty (a regular cursor
|
|
2743
|
-
position).
|
|
2744
|
-
*/
|
|
2745
|
-
var TextSelection = class TextSelection extends Selection {
|
|
2746
|
-
/**
|
|
2747
|
-
Construct a text selection between the given points.
|
|
2748
|
-
*/
|
|
2749
|
-
constructor($anchor, $head = $anchor) {
|
|
2750
|
-
checkTextSelection($anchor);
|
|
2751
|
-
checkTextSelection($head);
|
|
2752
|
-
super($anchor, $head);
|
|
2753
|
-
}
|
|
2754
|
-
/**
|
|
2755
|
-
Returns a resolved position if this is a cursor selection (an
|
|
2756
|
-
empty text selection), and null otherwise.
|
|
2757
|
-
*/
|
|
2758
|
-
get $cursor() {
|
|
2759
|
-
return this.$anchor.pos == this.$head.pos ? this.$head : null;
|
|
2760
|
-
}
|
|
2761
|
-
map(doc, mapping) {
|
|
2762
|
-
let $head = doc.resolve(mapping.map(this.head));
|
|
2763
|
-
if (!$head.parent.inlineContent) return Selection.near($head);
|
|
2764
|
-
let $anchor = doc.resolve(mapping.map(this.anchor));
|
|
2765
|
-
return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
|
|
2766
|
-
}
|
|
2767
|
-
replace(tr, content = Slice.empty) {
|
|
2768
|
-
super.replace(tr, content);
|
|
2769
|
-
if (content == Slice.empty) {
|
|
2770
|
-
let marks = this.$from.marksAcross(this.$to);
|
|
2771
|
-
if (marks) tr.ensureMarks(marks);
|
|
2772
|
-
}
|
|
2773
|
-
}
|
|
2774
|
-
eq(other) {
|
|
2775
|
-
return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
|
|
2776
|
-
}
|
|
2777
|
-
getBookmark() {
|
|
2778
|
-
return new TextBookmark(this.anchor, this.head);
|
|
2779
|
-
}
|
|
2780
|
-
toJSON() {
|
|
2781
|
-
return {
|
|
2782
|
-
type: "text",
|
|
2783
|
-
anchor: this.anchor,
|
|
2784
|
-
head: this.head
|
|
2785
|
-
};
|
|
2786
|
-
}
|
|
2787
|
-
/**
|
|
2788
|
-
@internal
|
|
2789
|
-
*/
|
|
2790
|
-
static fromJSON(doc, json) {
|
|
2791
|
-
if (typeof json.anchor != "number" || typeof json.head != "number") throw new RangeError("Invalid input for TextSelection.fromJSON");
|
|
2792
|
-
return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
|
|
2793
|
-
}
|
|
2794
|
-
/**
|
|
2795
|
-
Create a text selection from non-resolved positions.
|
|
2796
|
-
*/
|
|
2797
|
-
static create(doc, anchor, head = anchor) {
|
|
2798
|
-
let $anchor = doc.resolve(anchor);
|
|
2799
|
-
return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
|
|
2800
|
-
}
|
|
2801
|
-
/**
|
|
2802
|
-
Return a text selection that spans the given positions or, if
|
|
2803
|
-
they aren't text positions, find a text selection near them.
|
|
2804
|
-
`bias` determines whether the method searches forward (default)
|
|
2805
|
-
or backwards (negative number) first. Will fall back to calling
|
|
2806
|
-
[`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
|
|
2807
|
-
doesn't contain a valid text position.
|
|
2808
|
-
*/
|
|
2809
|
-
static between($anchor, $head, bias) {
|
|
2810
|
-
let dPos = $anchor.pos - $head.pos;
|
|
2811
|
-
if (!bias || dPos) bias = dPos >= 0 ? 1 : -1;
|
|
2812
|
-
if (!$head.parent.inlineContent) {
|
|
2813
|
-
let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
|
|
2814
|
-
if (found) $head = found.$head;
|
|
2815
|
-
else return Selection.near($head, bias);
|
|
2816
|
-
}
|
|
2817
|
-
if (!$anchor.parent.inlineContent) if (dPos == 0) $anchor = $head;
|
|
2818
|
-
else {
|
|
2819
|
-
$anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
|
|
2820
|
-
if ($anchor.pos < $head.pos != dPos < 0) $anchor = $head;
|
|
2821
|
-
}
|
|
2822
|
-
return new TextSelection($anchor, $head);
|
|
2823
|
-
}
|
|
2824
|
-
};
|
|
2825
|
-
Selection.jsonID("text", TextSelection);
|
|
2826
|
-
var TextBookmark = class TextBookmark {
|
|
2827
|
-
constructor(anchor, head) {
|
|
2828
|
-
this.anchor = anchor;
|
|
2829
|
-
this.head = head;
|
|
2830
|
-
}
|
|
2831
|
-
map(mapping) {
|
|
2832
|
-
return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
|
|
2833
|
-
}
|
|
2834
|
-
resolve(doc) {
|
|
2835
|
-
return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
|
|
2836
|
-
}
|
|
2837
|
-
};
|
|
2838
|
-
/**
|
|
2839
|
-
A node selection is a selection that points at a single node. All
|
|
2840
|
-
nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
|
|
2841
|
-
target of a node selection. In such a selection, `from` and `to`
|
|
2842
|
-
point directly before and after the selected node, `anchor` equals
|
|
2843
|
-
`from`, and `head` equals `to`..
|
|
2844
|
-
*/
|
|
2845
|
-
var NodeSelection = class NodeSelection extends Selection {
|
|
2846
|
-
/**
|
|
2847
|
-
Create a node selection. Does not verify the validity of its
|
|
2848
|
-
argument.
|
|
2849
|
-
*/
|
|
2850
|
-
constructor($pos) {
|
|
2851
|
-
let node = $pos.nodeAfter;
|
|
2852
|
-
let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
|
|
2853
|
-
super($pos, $end);
|
|
2854
|
-
this.node = node;
|
|
2855
|
-
}
|
|
2856
|
-
map(doc, mapping) {
|
|
2857
|
-
let { deleted, pos } = mapping.mapResult(this.anchor);
|
|
2858
|
-
let $pos = doc.resolve(pos);
|
|
2859
|
-
if (deleted) return Selection.near($pos);
|
|
2860
|
-
return new NodeSelection($pos);
|
|
2861
|
-
}
|
|
2862
|
-
content() {
|
|
2863
|
-
return new Slice(Fragment.from(this.node), 0, 0);
|
|
2864
|
-
}
|
|
2865
|
-
eq(other) {
|
|
2866
|
-
return other instanceof NodeSelection && other.anchor == this.anchor;
|
|
2867
|
-
}
|
|
2868
|
-
toJSON() {
|
|
2869
|
-
return {
|
|
2870
|
-
type: "node",
|
|
2871
|
-
anchor: this.anchor
|
|
2872
|
-
};
|
|
2873
|
-
}
|
|
2874
|
-
getBookmark() {
|
|
2875
|
-
return new NodeBookmark(this.anchor);
|
|
2876
|
-
}
|
|
2877
|
-
/**
|
|
2878
|
-
@internal
|
|
2879
|
-
*/
|
|
2880
|
-
static fromJSON(doc, json) {
|
|
2881
|
-
if (typeof json.anchor != "number") throw new RangeError("Invalid input for NodeSelection.fromJSON");
|
|
2882
|
-
return new NodeSelection(doc.resolve(json.anchor));
|
|
2883
|
-
}
|
|
2884
|
-
/**
|
|
2885
|
-
Create a node selection from non-resolved positions.
|
|
2886
|
-
*/
|
|
2887
|
-
static create(doc, from) {
|
|
2888
|
-
return new NodeSelection(doc.resolve(from));
|
|
2889
|
-
}
|
|
2890
|
-
/**
|
|
2891
|
-
Determines whether the given node may be selected as a node
|
|
2892
|
-
selection.
|
|
2893
|
-
*/
|
|
2894
|
-
static isSelectable(node) {
|
|
2895
|
-
return !node.isText && node.type.spec.selectable !== false;
|
|
2896
|
-
}
|
|
2897
|
-
};
|
|
2898
|
-
NodeSelection.prototype.visible = false;
|
|
2899
|
-
Selection.jsonID("node", NodeSelection);
|
|
2900
|
-
var NodeBookmark = class NodeBookmark {
|
|
2901
|
-
constructor(anchor) {
|
|
2902
|
-
this.anchor = anchor;
|
|
2903
|
-
}
|
|
2904
|
-
map(mapping) {
|
|
2905
|
-
let { deleted, pos } = mapping.mapResult(this.anchor);
|
|
2906
|
-
return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
|
|
2907
|
-
}
|
|
2908
|
-
resolve(doc) {
|
|
2909
|
-
let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
|
|
2910
|
-
if (node && NodeSelection.isSelectable(node)) return new NodeSelection($pos);
|
|
2911
|
-
return Selection.near($pos);
|
|
2912
|
-
}
|
|
2913
|
-
};
|
|
2914
|
-
/**
|
|
2915
|
-
A selection type that represents selecting the whole document
|
|
2916
|
-
(which can not necessarily be expressed with a text selection, when
|
|
2917
|
-
there are for example leaf block nodes at the start or end of the
|
|
2918
|
-
document).
|
|
2919
|
-
*/
|
|
2920
|
-
var AllSelection = class AllSelection extends Selection {
|
|
2921
|
-
/**
|
|
2922
|
-
Create an all-selection over the given document.
|
|
2923
|
-
*/
|
|
2924
|
-
constructor(doc) {
|
|
2925
|
-
super(doc.resolve(0), doc.resolve(doc.content.size));
|
|
2926
|
-
}
|
|
2927
|
-
replace(tr, content = Slice.empty) {
|
|
2928
|
-
if (content == Slice.empty) {
|
|
2929
|
-
tr.delete(0, tr.doc.content.size);
|
|
2930
|
-
let sel = Selection.atStart(tr.doc);
|
|
2931
|
-
if (!sel.eq(tr.selection)) tr.setSelection(sel);
|
|
2932
|
-
} else super.replace(tr, content);
|
|
2933
|
-
}
|
|
2934
|
-
toJSON() {
|
|
2935
|
-
return { type: "all" };
|
|
2936
|
-
}
|
|
2937
|
-
/**
|
|
2938
|
-
@internal
|
|
2939
|
-
*/
|
|
2940
|
-
static fromJSON(doc) {
|
|
2941
|
-
return new AllSelection(doc);
|
|
2942
|
-
}
|
|
2943
|
-
map(doc) {
|
|
2944
|
-
return new AllSelection(doc);
|
|
2945
|
-
}
|
|
2946
|
-
eq(other) {
|
|
2947
|
-
return other instanceof AllSelection;
|
|
2948
|
-
}
|
|
2949
|
-
getBookmark() {
|
|
2950
|
-
return AllBookmark;
|
|
2951
|
-
}
|
|
2952
|
-
};
|
|
2953
|
-
Selection.jsonID("all", AllSelection);
|
|
2954
|
-
const AllBookmark = {
|
|
2955
|
-
map() {
|
|
2956
|
-
return this;
|
|
2957
|
-
},
|
|
2958
|
-
resolve(doc) {
|
|
2959
|
-
return new AllSelection(doc);
|
|
2960
|
-
}
|
|
2961
|
-
};
|
|
2962
|
-
function findSelectionIn(doc, node, pos, index, dir, text = false) {
|
|
2963
|
-
if (node.inlineContent) return TextSelection.create(doc, pos);
|
|
2964
|
-
for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
|
|
2965
|
-
let child = node.child(i);
|
|
2966
|
-
if (!child.isAtom) {
|
|
2967
|
-
let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
|
|
2968
|
-
if (inner) return inner;
|
|
2969
|
-
} else if (!text && NodeSelection.isSelectable(child)) return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
|
|
2970
|
-
pos += child.nodeSize * dir;
|
|
2971
|
-
}
|
|
2972
|
-
return null;
|
|
2973
|
-
}
|
|
2974
|
-
function selectionToInsertionEnd(tr, startLen, bias) {
|
|
2975
|
-
let last = tr.steps.length - 1;
|
|
2976
|
-
if (last < startLen) return;
|
|
2977
|
-
let step = tr.steps[last];
|
|
2978
|
-
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) return;
|
|
2979
|
-
let map = tr.mapping.maps[last], end;
|
|
2980
|
-
map.forEach((_from, _to, _newFrom, newTo) => {
|
|
2981
|
-
if (end == null) end = newTo;
|
|
2982
|
-
});
|
|
2983
|
-
tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
|
|
2984
|
-
}
|
|
2985
|
-
function bind(f, self) {
|
|
2986
|
-
return !self || !f ? f : f.bind(self);
|
|
2987
|
-
}
|
|
2988
|
-
var FieldDesc = class {
|
|
2989
|
-
constructor(name, desc, self) {
|
|
2990
|
-
this.name = name;
|
|
2991
|
-
this.init = bind(desc.init, self);
|
|
2992
|
-
this.apply = bind(desc.apply, self);
|
|
2993
|
-
}
|
|
2994
|
-
};
|
|
2995
|
-
new FieldDesc("doc", {
|
|
2996
|
-
init(config) {
|
|
2997
|
-
return config.doc || config.schema.topNodeType.createAndFill();
|
|
2998
|
-
},
|
|
2999
|
-
apply(tr) {
|
|
3000
|
-
return tr.doc;
|
|
3001
|
-
}
|
|
3002
|
-
}), new FieldDesc("selection", {
|
|
3003
|
-
init(config, instance) {
|
|
3004
|
-
return config.selection || Selection.atStart(instance.doc);
|
|
3005
|
-
},
|
|
3006
|
-
apply(tr) {
|
|
3007
|
-
return tr.selection;
|
|
3008
|
-
}
|
|
3009
|
-
}), new FieldDesc("storedMarks", {
|
|
3010
|
-
init(config) {
|
|
3011
|
-
return config.storedMarks || null;
|
|
3012
|
-
},
|
|
3013
|
-
apply(tr, _marks, _old, state) {
|
|
3014
|
-
return state.selection.$cursor ? tr.storedMarks : null;
|
|
3015
|
-
}
|
|
3016
|
-
}), new FieldDesc("scrollToSelection", {
|
|
3017
|
-
init() {
|
|
3018
|
-
return 0;
|
|
3019
|
-
},
|
|
3020
|
-
apply(tr, prev) {
|
|
3021
|
-
return tr.scrolledIntoView ? prev + 1 : prev;
|
|
3022
|
-
}
|
|
3023
|
-
});
|
|
3024
|
-
function bindProps(obj, self, target) {
|
|
3025
|
-
for (let prop in obj) {
|
|
3026
|
-
let val = obj[prop];
|
|
3027
|
-
if (val instanceof Function) val = val.bind(self);
|
|
3028
|
-
else if (prop == "handleDOMEvents") val = bindProps(val, self, {});
|
|
3029
|
-
target[prop] = val;
|
|
3030
|
-
}
|
|
3031
|
-
return target;
|
|
3032
|
-
}
|
|
3033
|
-
/**
|
|
3034
|
-
Plugins bundle functionality that can be added to an editor.
|
|
3035
|
-
They are part of the [editor state](https://prosemirror.net/docs/ref/#state.EditorState) and
|
|
3036
|
-
may influence that state and the view that contains it.
|
|
3037
|
-
*/
|
|
3038
|
-
var Plugin = class {
|
|
3039
|
-
/**
|
|
3040
|
-
Create a plugin.
|
|
3041
|
-
*/
|
|
3042
|
-
constructor(spec) {
|
|
3043
|
-
this.spec = spec;
|
|
3044
|
-
/**
|
|
3045
|
-
The [props](https://prosemirror.net/docs/ref/#view.EditorProps) exported by this plugin.
|
|
3046
|
-
*/
|
|
3047
|
-
this.props = {};
|
|
3048
|
-
if (spec.props) bindProps(spec.props, this, this.props);
|
|
3049
|
-
this.key = spec.key ? spec.key.key : createKey("plugin");
|
|
3050
|
-
}
|
|
3051
|
-
/**
|
|
3052
|
-
Extract the plugin's state field from an editor state.
|
|
3053
|
-
*/
|
|
3054
|
-
getState(state) {
|
|
3055
|
-
return state[this.key];
|
|
3056
|
-
}
|
|
3057
|
-
};
|
|
3058
|
-
const keys = Object.create(null);
|
|
3059
|
-
function createKey(name) {
|
|
3060
|
-
if (name in keys) return name + "$" + ++keys[name];
|
|
3061
|
-
keys[name] = 0;
|
|
3062
|
-
return name + "$";
|
|
3063
|
-
}
|
|
3064
|
-
/**
|
|
3065
|
-
A key is used to [tag](https://prosemirror.net/docs/ref/#state.PluginSpec.key) plugins in a way
|
|
3066
|
-
that makes it possible to find them, given an editor state.
|
|
3067
|
-
Assigning a key does mean only one plugin of that type can be
|
|
3068
|
-
active in a state.
|
|
3069
|
-
*/
|
|
3070
|
-
var PluginKey = class {
|
|
3071
|
-
/**
|
|
3072
|
-
Create a plugin key.
|
|
3073
|
-
*/
|
|
3074
|
-
constructor(name = "key") {
|
|
3075
|
-
this.key = createKey(name);
|
|
3076
|
-
}
|
|
3077
|
-
/**
|
|
3078
|
-
Get the active plugin with this key, if any, from an editor
|
|
3079
|
-
state.
|
|
3080
|
-
*/
|
|
3081
|
-
get(state) {
|
|
3082
|
-
return state.config.pluginsByKey[this.key];
|
|
3083
|
-
}
|
|
3084
|
-
/**
|
|
3085
|
-
Get the plugin's state from an editor state.
|
|
3086
|
-
*/
|
|
3087
|
-
getState(state) {
|
|
3088
|
-
return state[this.key];
|
|
3089
|
-
}
|
|
3090
|
-
};
|
|
3091
|
-
//#endregion
|
|
3092
|
-
//#region ../../node_modules/.pnpm/@tiptap+y-tiptap@3.0.8_prosemirror-model@1.25.11_prosemirror-state@1.4.4_prosemirror-vi_929d0cf90bfff81acbde9235d0a2d0eb/node_modules/@tiptap/y-tiptap/dist/y-tiptap.js
|
|
3093
|
-
/**
|
|
3094
|
-
* The unique prosemirror plugin key for syncPlugin
|
|
3095
|
-
*
|
|
3096
|
-
* @public
|
|
3097
|
-
*/
|
|
3098
|
-
const ySyncPluginKey = new PluginKey("y-sync");
|
|
3099
|
-
/**
|
|
3100
|
-
* The unique prosemirror plugin key for undoPlugin
|
|
3101
|
-
*
|
|
3102
|
-
* @public
|
|
3103
|
-
*/
|
|
3104
|
-
const yUndoPluginKey = new PluginKey("y-undo");
|
|
3105
|
-
new PluginKey("yjs-cursor");
|
|
3106
|
-
/**
|
|
3107
|
-
* Custom function to transform sha256 hash to N byte
|
|
3108
|
-
*
|
|
3109
|
-
* @param {Uint8Array} digest
|
|
3110
|
-
*/
|
|
3111
|
-
const _convolute = (digest) => {
|
|
3112
|
-
const N = 6;
|
|
3113
|
-
for (let i = N; i < digest.length; i++) digest[i % N] = digest[i % N] ^ digest[i];
|
|
3114
|
-
return digest.slice(0, N);
|
|
3115
|
-
};
|
|
3116
|
-
/**
|
|
3117
|
-
* @param {any} json
|
|
3118
|
-
*/
|
|
3119
|
-
const hashOfJSON = (json) => buf.toBase64(_convolute(sha256.digest(buf.encodeAny(json))));
|
|
3120
|
-
/**
|
|
3121
|
-
* @param {Y.Item} item
|
|
3122
|
-
* @param {Y.Snapshot} [snapshot]
|
|
3123
|
-
*/
|
|
3124
|
-
const isVisible = (item, snapshot) => snapshot === void 0 ? !item.deleted : snapshot.sv.has(item.id.client) && snapshot.sv.get(item.id.client) > item.id.clock && !Y.isDeleted(snapshot.ds, item.id);
|
|
3125
|
-
/**
|
|
3126
|
-
* Either a node if type is YXmlElement or an Array of text nodes if YXmlText
|
|
3127
|
-
* @typedef {Map<Y.AbstractType<any>, PModel.Node | Array<PModel.Node>>} ProsemirrorMapping
|
|
3128
|
-
*/
|
|
3129
|
-
/**
|
|
3130
|
-
* @typedef {Object} ColorDef
|
|
3131
|
-
* @property {string} ColorDef.light
|
|
3132
|
-
* @property {string} ColorDef.dark
|
|
3133
|
-
*/
|
|
3134
|
-
/**
|
|
3135
|
-
* @typedef {Object} YSyncOpts
|
|
3136
|
-
* @property {Array<ColorDef>} [YSyncOpts.colors]
|
|
3137
|
-
* @property {Map<string,ColorDef>} [YSyncOpts.colorMapping]
|
|
3138
|
-
* @property {Y.PermanentUserData|null} [YSyncOpts.permanentUserData]
|
|
3139
|
-
* @property {ProsemirrorMapping} [YSyncOpts.mapping]
|
|
3140
|
-
* @property {function} [YSyncOpts.onFirstRender] Fired when the content from Yjs is initially rendered to ProseMirror
|
|
3141
|
-
*/
|
|
3142
|
-
/**
|
|
3143
|
-
* @type {Array<ColorDef>}
|
|
3144
|
-
*/
|
|
3145
|
-
const defaultColors = [{
|
|
3146
|
-
light: "#ecd44433",
|
|
3147
|
-
dark: "#ecd444"
|
|
3148
|
-
}];
|
|
3149
|
-
/**
|
|
3150
|
-
* @param {Map<string,ColorDef>} colorMapping
|
|
3151
|
-
* @param {Array<ColorDef>} colors
|
|
3152
|
-
* @param {string} user
|
|
3153
|
-
* @return {ColorDef}
|
|
3154
|
-
*/
|
|
3155
|
-
const getUserColor = (colorMapping, colors, user) => {
|
|
3156
|
-
if (!colorMapping.has(user)) {
|
|
3157
|
-
if (colorMapping.size < colors.length) {
|
|
3158
|
-
const usedColors = set.create();
|
|
3159
|
-
colorMapping.forEach((color) => usedColors.add(color));
|
|
3160
|
-
colors = colors.filter((color) => !usedColors.has(color));
|
|
3161
|
-
}
|
|
3162
|
-
colorMapping.set(user, random.oneOf(colors));
|
|
3163
|
-
}
|
|
3164
|
-
return colorMapping.get(user);
|
|
3165
|
-
};
|
|
3166
|
-
/**
|
|
3167
|
-
* This plugin listens to changes in prosemirror view and keeps yXmlState and view in sync.
|
|
3168
|
-
*
|
|
3169
|
-
* This plugin also keeps references to the type and the shared document so other plugins can access it.
|
|
3170
|
-
* @param {Y.XmlFragment} yXmlFragment
|
|
3171
|
-
* @param {YSyncOpts} opts
|
|
3172
|
-
* @return {any} Returns a prosemirror plugin that binds to this type
|
|
3173
|
-
*/
|
|
3174
|
-
const ySyncPlugin = (yXmlFragment, { colors = defaultColors, colorMapping = /* @__PURE__ */ new Map(), permanentUserData = null, onFirstRender = () => {}, mapping } = {}) => {
|
|
3175
|
-
let initialContentChanged = false;
|
|
3176
|
-
const binding = new ProsemirrorBinding(yXmlFragment, mapping);
|
|
3177
|
-
const plugin = new Plugin({
|
|
3178
|
-
props: { editable: (state) => {
|
|
3179
|
-
const syncState = ySyncPluginKey.getState(state);
|
|
3180
|
-
return syncState.snapshot == null && syncState.prevSnapshot == null;
|
|
3181
|
-
} },
|
|
3182
|
-
key: ySyncPluginKey,
|
|
3183
|
-
state: {
|
|
3184
|
-
/**
|
|
3185
|
-
* @returns {any}
|
|
3186
|
-
*/
|
|
3187
|
-
init: (_initargs, _state) => {
|
|
3188
|
-
return {
|
|
3189
|
-
type: yXmlFragment,
|
|
3190
|
-
doc: yXmlFragment.doc,
|
|
3191
|
-
binding,
|
|
3192
|
-
snapshot: null,
|
|
3193
|
-
prevSnapshot: null,
|
|
3194
|
-
isChangeOrigin: false,
|
|
3195
|
-
isUndoRedoOperation: false,
|
|
3196
|
-
addToHistory: true,
|
|
3197
|
-
colors,
|
|
3198
|
-
colorMapping,
|
|
3199
|
-
permanentUserData
|
|
3200
|
-
};
|
|
3201
|
-
},
|
|
3202
|
-
apply: (tr, pluginState) => {
|
|
3203
|
-
const change = tr.getMeta(ySyncPluginKey);
|
|
3204
|
-
if (change !== void 0) {
|
|
3205
|
-
pluginState = Object.assign({}, pluginState);
|
|
3206
|
-
for (const key in change) pluginState[key] = change[key];
|
|
3207
|
-
}
|
|
3208
|
-
pluginState.addToHistory = tr.getMeta("addToHistory") !== false;
|
|
3209
|
-
pluginState.isChangeOrigin = change !== void 0 && !!change.isChangeOrigin;
|
|
3210
|
-
pluginState.isUndoRedoOperation = change !== void 0 && !!change.isChangeOrigin && !!change.isUndoRedoOperation;
|
|
3211
|
-
if (binding.prosemirrorView !== null) {
|
|
3212
|
-
if (change !== void 0 && (change.snapshot != null || change.prevSnapshot != null)) eventloop.timeout(0, () => {
|
|
3213
|
-
if (binding.prosemirrorView == null) return;
|
|
3214
|
-
if (change.restore == null) binding._renderSnapshot(change.snapshot, change.prevSnapshot, pluginState);
|
|
3215
|
-
else {
|
|
3216
|
-
binding._renderSnapshot(change.snapshot, change.snapshot, pluginState);
|
|
3217
|
-
delete pluginState.restore;
|
|
3218
|
-
delete pluginState.snapshot;
|
|
3219
|
-
delete pluginState.prevSnapshot;
|
|
3220
|
-
binding.mux(() => {
|
|
3221
|
-
binding._prosemirrorChanged(binding.prosemirrorView.state.doc);
|
|
3222
|
-
});
|
|
3223
|
-
}
|
|
3224
|
-
});
|
|
3225
|
-
}
|
|
3226
|
-
return pluginState;
|
|
3227
|
-
}
|
|
3228
|
-
},
|
|
3229
|
-
view: (view) => {
|
|
3230
|
-
binding.initView(view);
|
|
3231
|
-
if (mapping == null) binding._forceRerender();
|
|
3232
|
-
onFirstRender();
|
|
3233
|
-
return {
|
|
3234
|
-
update: () => {
|
|
3235
|
-
const pluginState = plugin.getState(view.state);
|
|
3236
|
-
if (pluginState.snapshot == null && pluginState.prevSnapshot == null) {
|
|
3237
|
-
if (initialContentChanged || view.state.doc.content.findDiffStart(view.state.doc.type.createAndFill().content) !== null) {
|
|
3238
|
-
initialContentChanged = true;
|
|
3239
|
-
if (pluginState.addToHistory === false && !pluginState.isChangeOrigin) {
|
|
3240
|
-
const yUndoPluginState = yUndoPluginKey.getState(view.state);
|
|
3241
|
-
/**
|
|
3242
|
-
* @type {Y.UndoManager}
|
|
3243
|
-
*/
|
|
3244
|
-
const um = yUndoPluginState && yUndoPluginState.undoManager;
|
|
3245
|
-
if (um) um.stopCapturing();
|
|
3246
|
-
}
|
|
3247
|
-
binding.mux(() => {
|
|
3248
|
-
/** @type {Y.Doc} */ pluginState.doc.transact((tr) => {
|
|
3249
|
-
tr.meta.set("addToHistory", pluginState.addToHistory);
|
|
3250
|
-
binding._prosemirrorChanged(view.state.doc);
|
|
3251
|
-
}, ySyncPluginKey);
|
|
3252
|
-
});
|
|
3253
|
-
}
|
|
3254
|
-
}
|
|
3255
|
-
},
|
|
3256
|
-
destroy: () => {
|
|
3257
|
-
binding.destroy();
|
|
3258
|
-
}
|
|
3259
|
-
};
|
|
3260
|
-
}
|
|
3261
|
-
});
|
|
3262
|
-
return plugin;
|
|
3263
|
-
};
|
|
3264
|
-
/**
|
|
3265
|
-
* Resolves one text-selection endpoint after a remote update. Falls back to
|
|
3266
|
-
* content-based matching when the Yjs resolution is missing or lands in the
|
|
3267
|
-
* wrong block, and keeps the Yjs resolution when the fallback finds nothing.
|
|
3268
|
-
*
|
|
3269
|
-
* @param {import('prosemirror-model').Node} newDoc
|
|
3270
|
-
* @param {import('prosemirror-model').Node} oldDoc
|
|
3271
|
-
* @param {number|null|undefined} oldAbs
|
|
3272
|
-
* @param {number|null} resolved
|
|
3273
|
-
* @return {number|null}
|
|
3274
|
-
*/
|
|
3275
|
-
const recoverSelectionEndpoint = (newDoc, oldDoc, oldAbs, resolved) => {
|
|
3276
|
-
if (oldAbs == null) return resolved;
|
|
3277
|
-
if (!(resolved === null || oldAbs > 1 && resolved <= 1 || isMisresolvedAfterStructuralChange(oldDoc, newDoc, oldAbs, resolved))) return resolved;
|
|
3278
|
-
const recovered = findAbsolutePositionAfterStructuralChange(oldDoc, newDoc, oldAbs);
|
|
3279
|
-
return recovered !== null ? recovered : resolved;
|
|
3280
|
-
};
|
|
3281
|
-
/**
|
|
3282
|
-
* @param {import('prosemirror-state').Transaction} tr
|
|
3283
|
-
* @param {ReturnType<typeof getRelativeSelection>} relSel
|
|
3284
|
-
* @param {ProsemirrorBinding} binding
|
|
3285
|
-
* @param {import('prosemirror-model').Node} [oldDoc]
|
|
3286
|
-
*/
|
|
3287
|
-
const restoreRelativeSelection = (tr, relSel, binding, oldDoc) => {
|
|
3288
|
-
if (relSel !== null && relSel.anchor !== null && relSel.head !== null) if (relSel.type === "all") tr.setSelection(new AllSelection(tr.doc));
|
|
3289
|
-
else if (relSel.type === "node") {
|
|
3290
|
-
const anchor = relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.anchor, binding.mapping);
|
|
3291
|
-
if (anchor !== null) tr.setSelection(createSafeNodeSelection(tr, anchor));
|
|
3292
|
-
} else if (relSel.type === "nodeRange") {
|
|
3293
|
-
const selection = createSafeNodeRangeSelection(tr, relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.anchor, binding.mapping), relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.head, binding.mapping), relSel.depth);
|
|
3294
|
-
if (selection !== null) tr.setSelection(selection);
|
|
3295
|
-
} else {
|
|
3296
|
-
let anchor = relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.anchor, binding.mapping);
|
|
3297
|
-
let head = relativePositionToAbsolutePosition(binding.doc, binding.type, relSel.head, binding.mapping);
|
|
3298
|
-
if (oldDoc != null) {
|
|
3299
|
-
anchor = recoverSelectionEndpoint(tr.doc, oldDoc, relSel.absAnchor, anchor);
|
|
3300
|
-
head = recoverSelectionEndpoint(tr.doc, oldDoc, relSel.absHead, head);
|
|
3301
|
-
}
|
|
3302
|
-
if (anchor === null) anchor = head;
|
|
3303
|
-
if (head === null) head = anchor;
|
|
3304
|
-
if (anchor !== null && head !== null) tr.setSelection(TextSelection.between(tr.doc.resolve(anchor), tr.doc.resolve(head)));
|
|
3305
|
-
}
|
|
3306
|
-
};
|
|
3307
|
-
/**
|
|
3308
|
-
* @param {import('prosemirror-state').Transaction} tr
|
|
3309
|
-
* @param {number} pos
|
|
3310
|
-
* @returns {import('prosemirror-state').Selection}
|
|
3311
|
-
*
|
|
3312
|
-
* Creates a NodeSelection if the position points to a valid node, otherwise
|
|
3313
|
-
* creates a TextSelection near the position.
|
|
3314
|
-
*/
|
|
3315
|
-
const createSafeNodeSelection = (tr, pos) => {
|
|
3316
|
-
const $pos = tr.doc.resolve(pos);
|
|
3317
|
-
if ($pos.nodeAfter) return NodeSelection.create(tr.doc, pos);
|
|
3318
|
-
else return TextSelection.near($pos);
|
|
3319
|
-
};
|
|
3320
|
-
/**
|
|
3321
|
-
* Safely reconstructs a NodeRangeSelection from resolved absolute positions.
|
|
3322
|
-
*
|
|
3323
|
-
* @param {import('prosemirror-state').Transaction} tr - The transaction whose document provides resolved positions.
|
|
3324
|
-
* @param {number|null} anchor - Absolute document position marking the start (boundary) of the node range.
|
|
3325
|
-
* Use `relativePositionToAbsolutePosition` before calling this function.
|
|
3326
|
-
* @param {number|null} head - Absolute document position marking the end (boundary) of the node range.
|
|
3327
|
-
* Use `relativePositionToAbsolutePosition` before calling this function.
|
|
3328
|
-
* @param {number|undefined} depth - The nesting depth at which the range operates (e.g. 0 for
|
|
3329
|
-
* top-level blocks, 1 for blocks nested inside a wrapper). Passed through to
|
|
3330
|
-
* `Selection.fromJSON`; ignored by `@tiptap/extension-node-range` < v2.29 but
|
|
3331
|
-
* properly stored starting from that version.
|
|
3332
|
-
* @returns {import('prosemirror-state').Selection|null} Reconstructed selection, or null if
|
|
3333
|
-
* anchor/head could not be resolved.
|
|
3334
|
-
*/
|
|
3335
|
-
const createSafeNodeRangeSelection = (tr, anchor, head, depth) => {
|
|
3336
|
-
if (anchor === null || head === null) return null;
|
|
3337
|
-
const clampedAnchor = Math.min(Math.max(anchor, 0), tr.doc.content.size);
|
|
3338
|
-
const clampedHead = Math.min(Math.max(head, 0), tr.doc.content.size);
|
|
3339
|
-
try {
|
|
3340
|
-
const selection = Selection.fromJSON(tr.doc, {
|
|
3341
|
-
type: "nodeRange",
|
|
3342
|
-
anchor: clampedAnchor,
|
|
3343
|
-
head: clampedHead,
|
|
3344
|
-
depth
|
|
3345
|
-
});
|
|
3346
|
-
if (!selection.ranges.length) return TextSelection.near(tr.doc.resolve(clampedAnchor));
|
|
3347
|
-
return selection;
|
|
3348
|
-
} catch (e) {
|
|
3349
|
-
return TextSelection.near(tr.doc.resolve(clampedAnchor));
|
|
3350
|
-
}
|
|
3351
|
-
};
|
|
3352
|
-
/**
|
|
3353
|
-
* @param {ProsemirrorBinding} pmbinding
|
|
3354
|
-
* @param {import('prosemirror-state').EditorState} state
|
|
3355
|
-
*/
|
|
3356
|
-
const getRelativeSelection = (pmbinding, state) => {
|
|
3357
|
-
const type = state.selection.jsonID;
|
|
3358
|
-
return {
|
|
3359
|
-
type,
|
|
3360
|
-
depth: type === "nodeRange" ? state.selection.depth : void 0,
|
|
3361
|
-
anchor: absolutePositionToRelativePosition(state.selection.anchor, pmbinding.type, pmbinding.mapping),
|
|
3362
|
-
head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping),
|
|
3363
|
-
absAnchor: state.selection.anchor,
|
|
3364
|
-
absHead: state.selection.head
|
|
3365
|
-
};
|
|
3366
|
-
};
|
|
3367
|
-
/**
|
|
3368
|
-
* Binding for prosemirror.
|
|
3369
|
-
*
|
|
3370
|
-
* @protected
|
|
3371
|
-
*/
|
|
3372
|
-
var ProsemirrorBinding = class {
|
|
3373
|
-
/**
|
|
3374
|
-
* @param {Y.XmlFragment} yXmlFragment The bind source
|
|
3375
|
-
* @param {ProsemirrorMapping} mapping
|
|
3376
|
-
*/
|
|
3377
|
-
constructor(yXmlFragment, mapping = /* @__PURE__ */ new Map()) {
|
|
3378
|
-
this.type = yXmlFragment;
|
|
3379
|
-
/**
|
|
3380
|
-
* this will be set once the view is created
|
|
3381
|
-
* @type {any}
|
|
3382
|
-
*/
|
|
3383
|
-
this.prosemirrorView = null;
|
|
3384
|
-
this.mux = createMutex();
|
|
3385
|
-
this.mapping = mapping;
|
|
3386
|
-
/**
|
|
3387
|
-
* Is overlapping mark - i.e. mark does not exclude itself.
|
|
3388
|
-
*
|
|
3389
|
-
* @type {Map<import('prosemirror-model').MarkType, boolean>}
|
|
3390
|
-
*/
|
|
3391
|
-
this.isOMark = /* @__PURE__ */ new Map();
|
|
3392
|
-
this._observeFunction = this._typeChanged.bind(this);
|
|
3393
|
-
/**
|
|
3394
|
-
* @type {Y.Doc}
|
|
3395
|
-
*/
|
|
3396
|
-
this.doc = yXmlFragment.doc;
|
|
3397
|
-
/**
|
|
3398
|
-
* current selection as relative positions in the Yjs model
|
|
3399
|
-
*/
|
|
3400
|
-
this.beforeTransactionSelection = null;
|
|
3401
|
-
this.beforeAllTransactions = () => {
|
|
3402
|
-
if (this.beforeTransactionSelection === null && this.prosemirrorView != null) this.beforeTransactionSelection = getRelativeSelection(this, this.prosemirrorView.state);
|
|
3403
|
-
};
|
|
3404
|
-
this.afterAllTransactions = () => {
|
|
3405
|
-
this.beforeTransactionSelection = null;
|
|
3406
|
-
};
|
|
3407
|
-
this._domSelectionInView = null;
|
|
3408
|
-
}
|
|
3409
|
-
/**
|
|
3410
|
-
* Create a transaction for changing the prosemirror state.
|
|
3411
|
-
*
|
|
3412
|
-
* @returns
|
|
3413
|
-
*/
|
|
3414
|
-
get _tr() {
|
|
3415
|
-
return this.prosemirrorView.state.tr.setMeta("addToHistory", false);
|
|
3416
|
-
}
|
|
3417
|
-
_isLocalCursorInView() {
|
|
3418
|
-
if (!this.prosemirrorView.hasFocus()) return false;
|
|
3419
|
-
if (environment.isBrowser && this._domSelectionInView === null) {
|
|
3420
|
-
eventloop.timeout(0, () => {
|
|
3421
|
-
this._domSelectionInView = null;
|
|
3422
|
-
});
|
|
3423
|
-
this._domSelectionInView = this._isDomSelectionInView();
|
|
3424
|
-
}
|
|
3425
|
-
return this._domSelectionInView;
|
|
3426
|
-
}
|
|
3427
|
-
_isDomSelectionInView() {
|
|
3428
|
-
const selection = this.prosemirrorView._root.getSelection();
|
|
3429
|
-
if (selection == null || selection.anchorNode == null) return false;
|
|
3430
|
-
const range = this.prosemirrorView._root.createRange();
|
|
3431
|
-
range.setStart(selection.anchorNode, selection.anchorOffset);
|
|
3432
|
-
range.setEnd(selection.focusNode, selection.focusOffset);
|
|
3433
|
-
if (range.getClientRects().length === 0) {
|
|
3434
|
-
if (range.startContainer && range.collapsed) range.selectNodeContents(range.startContainer);
|
|
3435
|
-
}
|
|
3436
|
-
const bounding = range.getBoundingClientRect();
|
|
3437
|
-
const documentElement = dom.doc.documentElement;
|
|
3438
|
-
return bounding.bottom >= 0 && bounding.right >= 0 && bounding.left <= (window.innerWidth || documentElement.clientWidth || 0) && bounding.top <= (window.innerHeight || documentElement.clientHeight || 0);
|
|
3439
|
-
}
|
|
3440
|
-
/**
|
|
3441
|
-
* @param {Y.Snapshot} snapshot
|
|
3442
|
-
* @param {Y.Snapshot} prevSnapshot
|
|
3443
|
-
*/
|
|
3444
|
-
renderSnapshot(snapshot, prevSnapshot) {
|
|
3445
|
-
if (!prevSnapshot) prevSnapshot = Y.createSnapshot(Y.createDeleteSet(), /* @__PURE__ */ new Map());
|
|
3446
|
-
this.prosemirrorView.dispatch(this._tr.setMeta(ySyncPluginKey, {
|
|
3447
|
-
snapshot,
|
|
3448
|
-
prevSnapshot
|
|
3449
|
-
}));
|
|
3450
|
-
}
|
|
3451
|
-
unrenderSnapshot() {
|
|
3452
|
-
this.mapping.clear();
|
|
3453
|
-
this.mux(() => {
|
|
3454
|
-
const fragmentContent = this.type.toArray().map((t) => createNodeFromYElement(t, this.prosemirrorView.state.schema, this)).filter((n) => n !== null);
|
|
3455
|
-
const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new Slice(Fragment.from(fragmentContent), 0, 0));
|
|
3456
|
-
tr.setMeta(ySyncPluginKey, {
|
|
3457
|
-
snapshot: null,
|
|
3458
|
-
prevSnapshot: null
|
|
3459
|
-
});
|
|
3460
|
-
this.prosemirrorView.dispatch(tr);
|
|
3461
|
-
});
|
|
3462
|
-
}
|
|
3463
|
-
_forceRerender() {
|
|
3464
|
-
this.mapping.clear();
|
|
3465
|
-
this.mux(() => {
|
|
3466
|
-
const sel = this.beforeTransactionSelection !== null ? null : this.prosemirrorView.state.selection;
|
|
3467
|
-
const fragmentContent = this.type.toArray().map((t) => createNodeFromYElement(t, this.prosemirrorView.state.schema, this)).filter((n) => n !== null);
|
|
3468
|
-
const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new Slice(Fragment.from(fragmentContent), 0, 0));
|
|
3469
|
-
if (sel) {
|
|
3470
|
-
/**
|
|
3471
|
-
* If the Prosemirror document we just created from this.type is
|
|
3472
|
-
* smaller than the previous document, the selection might be
|
|
3473
|
-
* out of bound, which would make Prosemirror throw an error.
|
|
3474
|
-
*/
|
|
3475
|
-
const clampedAnchor = math.min(math.max(sel.anchor, 0), tr.doc.content.size);
|
|
3476
|
-
const clampedHead = math.min(math.max(sel.head, 0), tr.doc.content.size);
|
|
3477
|
-
tr.setSelection(TextSelection.create(tr.doc, clampedAnchor, clampedHead));
|
|
3478
|
-
}
|
|
3479
|
-
this.prosemirrorView.dispatch(tr.setMeta(ySyncPluginKey, {
|
|
3480
|
-
isChangeOrigin: true,
|
|
3481
|
-
binding: this
|
|
3482
|
-
}));
|
|
3483
|
-
});
|
|
3484
|
-
}
|
|
3485
|
-
/**
|
|
3486
|
-
* @param {Y.Snapshot|Uint8Array} snapshot
|
|
3487
|
-
* @param {Y.Snapshot|Uint8Array} prevSnapshot
|
|
3488
|
-
* @param {Object} pluginState
|
|
3489
|
-
*/
|
|
3490
|
-
_renderSnapshot(snapshot, prevSnapshot, pluginState) {
|
|
3491
|
-
/**
|
|
3492
|
-
* The document that contains the full history of this document.
|
|
3493
|
-
* @type {Y.Doc}
|
|
3494
|
-
*/
|
|
3495
|
-
let historyDoc = this.doc;
|
|
3496
|
-
let historyType = this.type;
|
|
3497
|
-
if (!snapshot) snapshot = Y.snapshot(this.doc);
|
|
3498
|
-
if (snapshot instanceof Uint8Array || prevSnapshot instanceof Uint8Array) {
|
|
3499
|
-
if (!(snapshot instanceof Uint8Array) || !(prevSnapshot instanceof Uint8Array)) error.unexpectedCase();
|
|
3500
|
-
historyDoc = new Y.Doc({ gc: false });
|
|
3501
|
-
Y.applyUpdateV2(historyDoc, prevSnapshot);
|
|
3502
|
-
prevSnapshot = Y.snapshot(historyDoc);
|
|
3503
|
-
Y.applyUpdateV2(historyDoc, snapshot);
|
|
3504
|
-
snapshot = Y.snapshot(historyDoc);
|
|
3505
|
-
if (historyType._item === null) {
|
|
3506
|
-
/**
|
|
3507
|
-
* If is a root type, we need to find the root key in the initial document
|
|
3508
|
-
* and use it to get the history type.
|
|
3509
|
-
*/
|
|
3510
|
-
const rootKey = Array.from(this.doc.share.keys()).find((key) => this.doc.share.get(key) === this.type);
|
|
3511
|
-
historyType = historyDoc.getXmlFragment(rootKey);
|
|
3512
|
-
} else {
|
|
3513
|
-
/**
|
|
3514
|
-
* If it is a sub type, we use the item id to find the history type.
|
|
3515
|
-
*/
|
|
3516
|
-
const historyStructs = historyDoc.store.clients.get(historyType._item.id.client) ?? [];
|
|
3517
|
-
historyType = historyStructs[Y.findIndexSS(historyStructs, historyType._item.id.clock)].content.type;
|
|
3518
|
-
}
|
|
3519
|
-
}
|
|
3520
|
-
this.mapping.clear();
|
|
3521
|
-
this.mux(() => {
|
|
3522
|
-
historyDoc.transact((transaction) => {
|
|
3523
|
-
/**
|
|
3524
|
-
* @type {Y.PermanentUserData}
|
|
3525
|
-
*/
|
|
3526
|
-
const pud = pluginState.permanentUserData;
|
|
3527
|
-
if (pud) pud.dss.forEach((ds) => {
|
|
3528
|
-
Y.iterateDeletedStructs(transaction, ds, (_item) => {});
|
|
3529
|
-
});
|
|
3530
|
-
/**
|
|
3531
|
-
* @param {'removed'|'added'} type
|
|
3532
|
-
* @param {Y.ID} id
|
|
3533
|
-
*/
|
|
3534
|
-
const computeYChange = (type, id) => {
|
|
3535
|
-
const user = type === "added" ? pud.getUserByClientId(id.client) : pud.getUserByDeletedId(id);
|
|
3536
|
-
return {
|
|
3537
|
-
user,
|
|
3538
|
-
type,
|
|
3539
|
-
color: getUserColor(pluginState.colorMapping, pluginState.colors, user)
|
|
3540
|
-
};
|
|
3541
|
-
};
|
|
3542
|
-
const fragmentContent = Y.typeListToArraySnapshot(historyType, new Y.Snapshot(prevSnapshot.ds, snapshot.sv)).map((t) => {
|
|
3543
|
-
if (!t._item.deleted || isVisible(t._item, snapshot) || isVisible(t._item, prevSnapshot)) return createNodeFromYElement(t, this.prosemirrorView.state.schema, {
|
|
3544
|
-
mapping: /* @__PURE__ */ new Map(),
|
|
3545
|
-
isOMark: /* @__PURE__ */ new Map()
|
|
3546
|
-
}, snapshot, prevSnapshot, computeYChange);
|
|
3547
|
-
else return null;
|
|
3548
|
-
}).filter((n) => n !== null);
|
|
3549
|
-
const tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new Slice(Fragment.from(fragmentContent), 0, 0));
|
|
3550
|
-
this.prosemirrorView.dispatch(tr.setMeta(ySyncPluginKey, { isChangeOrigin: true }));
|
|
3551
|
-
}, ySyncPluginKey);
|
|
3552
|
-
});
|
|
3553
|
-
}
|
|
3554
|
-
/**
|
|
3555
|
-
* @param {Array<Y.YEvent<any>>} events
|
|
3556
|
-
* @param {Y.Transaction} transaction
|
|
3557
|
-
*/
|
|
3558
|
-
_typeChanged(events, transaction) {
|
|
3559
|
-
if (this.prosemirrorView == null) return;
|
|
3560
|
-
const syncState = ySyncPluginKey.getState(this.prosemirrorView.state);
|
|
3561
|
-
if (events.length === 0 || syncState.snapshot != null || syncState.prevSnapshot != null) {
|
|
3562
|
-
this.renderSnapshot(syncState.snapshot, syncState.prevSnapshot);
|
|
3563
|
-
return;
|
|
3564
|
-
}
|
|
3565
|
-
this.mux(() => {
|
|
3566
|
-
/**
|
|
3567
|
-
* @param {any} _
|
|
3568
|
-
* @param {Y.AbstractType<any>} type
|
|
3569
|
-
*/
|
|
3570
|
-
const delType = (_, type) => this.mapping.delete(type);
|
|
3571
|
-
Y.iterateDeletedStructs(transaction, transaction.deleteSet, (struct) => {
|
|
3572
|
-
if (struct.constructor === Y.Item) {
|
|
3573
|
-
const type = struct.content.type;
|
|
3574
|
-
type && this.mapping.delete(type);
|
|
3575
|
-
}
|
|
3576
|
-
});
|
|
3577
|
-
transaction.changed.forEach(delType);
|
|
3578
|
-
transaction.changedParentTypes.forEach(delType);
|
|
3579
|
-
this.mapping.clear();
|
|
3580
|
-
const fragmentContent = this.type.toArray().map((t) => createNodeIfNotExists(t, this.prosemirrorView.state.schema, this)).filter((n) => n !== null);
|
|
3581
|
-
const oldDoc = this.prosemirrorView.state.doc;
|
|
3582
|
-
let tr = this._tr.replace(0, this.prosemirrorView.state.doc.content.size, new Slice(Fragment.from(fragmentContent), 0, 0));
|
|
3583
|
-
restoreRelativeSelection(tr, this.beforeTransactionSelection, this, oldDoc);
|
|
3584
|
-
tr = tr.setMeta(ySyncPluginKey, {
|
|
3585
|
-
isChangeOrigin: true,
|
|
3586
|
-
isUndoRedoOperation: transaction.origin instanceof Y.UndoManager
|
|
3587
|
-
});
|
|
3588
|
-
if (this.beforeTransactionSelection !== null && this._isLocalCursorInView()) tr.scrollIntoView();
|
|
3589
|
-
this.prosemirrorView.dispatch(tr);
|
|
3590
|
-
});
|
|
3591
|
-
}
|
|
3592
|
-
/**
|
|
3593
|
-
* @param {import('prosemirror-model').Node} doc
|
|
3594
|
-
*/
|
|
3595
|
-
_prosemirrorChanged(doc) {
|
|
3596
|
-
this.doc.transact(() => {
|
|
3597
|
-
updateYFragment(this.doc, this.type, doc, this);
|
|
3598
|
-
this.beforeTransactionSelection = getRelativeSelection(this, this.prosemirrorView.state);
|
|
3599
|
-
}, ySyncPluginKey);
|
|
3600
|
-
}
|
|
3601
|
-
/**
|
|
3602
|
-
* View is ready to listen to changes. Register observers.
|
|
3603
|
-
* @param {any} prosemirrorView
|
|
3604
|
-
*/
|
|
3605
|
-
initView(prosemirrorView) {
|
|
3606
|
-
if (this.prosemirrorView != null) this.destroy();
|
|
3607
|
-
this.prosemirrorView = prosemirrorView;
|
|
3608
|
-
this.doc.on("beforeAllTransactions", this.beforeAllTransactions);
|
|
3609
|
-
this.doc.on("afterAllTransactions", this.afterAllTransactions);
|
|
3610
|
-
this.type.observeDeep(this._observeFunction);
|
|
3611
|
-
}
|
|
3612
|
-
destroy() {
|
|
3613
|
-
if (this.prosemirrorView == null) return;
|
|
3614
|
-
this.prosemirrorView = null;
|
|
3615
|
-
this.type.unobserveDeep(this._observeFunction);
|
|
3616
|
-
this.doc.off("beforeAllTransactions", this.beforeAllTransactions);
|
|
3617
|
-
this.doc.off("afterAllTransactions", this.afterAllTransactions);
|
|
3618
|
-
}
|
|
3619
|
-
};
|
|
3620
|
-
/**
|
|
3621
|
-
* @private
|
|
3622
|
-
* @param {Y.XmlElement | Y.XmlHook} el
|
|
3623
|
-
* @param {PModel.Schema} schema
|
|
3624
|
-
* @param {BindingMetadata} meta
|
|
3625
|
-
* @param {Y.Snapshot} [snapshot]
|
|
3626
|
-
* @param {Y.Snapshot} [prevSnapshot]
|
|
3627
|
-
* @param {function('removed' | 'added', Y.ID):any} [computeYChange]
|
|
3628
|
-
* @return {PModel.Node | null}
|
|
3629
|
-
*/
|
|
3630
|
-
const createNodeIfNotExists = (el, schema, meta, snapshot, prevSnapshot, computeYChange) => {
|
|
3631
|
-
const node = meta.mapping.get(el);
|
|
3632
|
-
if (node === void 0) if (el instanceof Y.XmlElement) return createNodeFromYElement(el, schema, meta, snapshot, prevSnapshot, computeYChange);
|
|
3633
|
-
else throw error.methodUnimplemented();
|
|
3634
|
-
return node;
|
|
3635
|
-
};
|
|
3636
|
-
/**
|
|
3637
|
-
* @private
|
|
3638
|
-
* @param {Y.XmlElement} el
|
|
3639
|
-
* @param {any} schema
|
|
3640
|
-
* @param {BindingMetadata} meta
|
|
3641
|
-
* @param {Y.Snapshot} [snapshot]
|
|
3642
|
-
* @param {Y.Snapshot} [prevSnapshot]
|
|
3643
|
-
* @param {function('removed' | 'added', Y.ID):any} [computeYChange]
|
|
3644
|
-
* @return {PModel.Node | null} Returns node if node could be created. Otherwise it deletes the yjs type and returns null
|
|
3645
|
-
*/
|
|
3646
|
-
const createNodeFromYElement = (el, schema, meta, snapshot, prevSnapshot, computeYChange) => {
|
|
3647
|
-
const children = [];
|
|
3648
|
-
/**
|
|
3649
|
-
* @param {Y.XmlElement | Y.XmlText} type
|
|
3650
|
-
*/
|
|
3651
|
-
const createChildren = (type) => {
|
|
3652
|
-
if (type instanceof Y.XmlElement) {
|
|
3653
|
-
const n = createNodeIfNotExists(type, schema, meta, snapshot, prevSnapshot, computeYChange);
|
|
3654
|
-
if (n !== null) children.push(n);
|
|
3655
|
-
} else {
|
|
3656
|
-
const nextytext = (type._item.right?.content)?.type;
|
|
3657
|
-
if (nextytext instanceof Y.Text && !nextytext._item.deleted && nextytext._item.id.client === nextytext.doc.clientID) {
|
|
3658
|
-
type.applyDelta([{ retain: type.length }, ...nextytext.toDelta()]);
|
|
3659
|
-
nextytext.doc.transact((tr) => {
|
|
3660
|
-
nextytext._item.delete(tr);
|
|
3661
|
-
});
|
|
3662
|
-
}
|
|
3663
|
-
const ns = createTextNodesFromYText(type, schema, meta, snapshot, prevSnapshot, computeYChange);
|
|
3664
|
-
if (ns !== null) ns.forEach((textchild) => {
|
|
3665
|
-
if (textchild !== null) children.push(textchild);
|
|
3666
|
-
});
|
|
3667
|
-
}
|
|
3668
|
-
};
|
|
3669
|
-
if (snapshot === void 0 || prevSnapshot === void 0) el.toArray().forEach(createChildren);
|
|
3670
|
-
else Y.typeListToArraySnapshot(el, new Y.Snapshot(prevSnapshot.ds, snapshot.sv)).forEach(createChildren);
|
|
3671
|
-
try {
|
|
3672
|
-
const attrs = el.getAttributes(snapshot);
|
|
3673
|
-
if (snapshot !== void 0) {
|
|
3674
|
-
if (!isVisible(el._item, snapshot)) attrs.ychange = computeYChange ? computeYChange(
|
|
3675
|
-
"removed",
|
|
3676
|
-
/** @type {Y.Item} */
|
|
3677
|
-
el._item.id
|
|
3678
|
-
) : { type: "removed" };
|
|
3679
|
-
else if (!isVisible(el._item, prevSnapshot)) attrs.ychange = computeYChange ? computeYChange(
|
|
3680
|
-
"added",
|
|
3681
|
-
/** @type {Y.Item} */
|
|
3682
|
-
el._item.id
|
|
3683
|
-
) : { type: "added" };
|
|
3684
|
-
}
|
|
3685
|
-
const node = schema.node(el.nodeName, attrs, children);
|
|
3686
|
-
meta.mapping.set(el, node);
|
|
3687
|
-
return node;
|
|
3688
|
-
} catch (e) {
|
|
3689
|
-
/** @type {Y.Doc} */ el.doc.transact((transaction) => {
|
|
3690
|
-
/** @type {Y.Item} */ el._item.delete(transaction);
|
|
3691
|
-
}, ySyncPluginKey);
|
|
3692
|
-
meta.mapping.delete(el);
|
|
3693
|
-
return null;
|
|
3694
|
-
}
|
|
3695
|
-
};
|
|
3696
|
-
/**
|
|
3697
|
-
* @private
|
|
3698
|
-
* @param {Y.XmlText} text
|
|
3699
|
-
* @param {import('prosemirror-model').Schema} schema
|
|
3700
|
-
* @param {BindingMetadata} _meta
|
|
3701
|
-
* @param {Y.Snapshot} [snapshot]
|
|
3702
|
-
* @param {Y.Snapshot} [prevSnapshot]
|
|
3703
|
-
* @param {function('removed' | 'added', Y.ID):any} [computeYChange]
|
|
3704
|
-
* @return {Array<PModel.Node>|null}
|
|
3705
|
-
*/
|
|
3706
|
-
const createTextNodesFromYText = (text, schema, _meta, snapshot, prevSnapshot, computeYChange) => {
|
|
3707
|
-
const nodes = [];
|
|
3708
|
-
const deltas = text.toDelta(snapshot, prevSnapshot, computeYChange);
|
|
3709
|
-
try {
|
|
3710
|
-
for (let i = 0; i < deltas.length; i++) {
|
|
3711
|
-
const delta = deltas[i];
|
|
3712
|
-
nodes.push(schema.text(delta.insert, attributesToMarks(delta.attributes, schema)));
|
|
3713
|
-
}
|
|
3714
|
-
} catch (e) {
|
|
3715
|
-
/** @type {Y.Doc} */ text.doc.transact((transaction) => {
|
|
3716
|
-
/** @type {Y.Item} */ text._item.delete(transaction);
|
|
3717
|
-
}, ySyncPluginKey);
|
|
3718
|
-
return null;
|
|
3719
|
-
}
|
|
3720
|
-
return nodes;
|
|
3721
|
-
};
|
|
3722
|
-
/**
|
|
3723
|
-
* @private
|
|
3724
|
-
* @param {Array<any>} nodes prosemirror node
|
|
3725
|
-
* @param {BindingMetadata} meta
|
|
3726
|
-
* @return {Y.XmlText}
|
|
3727
|
-
*/
|
|
3728
|
-
const createTypeFromTextNodes = (nodes, meta) => {
|
|
3729
|
-
const type = new Y.XmlText();
|
|
3730
|
-
const delta = nodes.map((node) => ({
|
|
3731
|
-
insert: node.text,
|
|
3732
|
-
attributes: marksToAttributes(node.marks, meta)
|
|
3733
|
-
}));
|
|
3734
|
-
type.applyDelta(delta);
|
|
3735
|
-
meta.mapping.set(type, nodes);
|
|
3736
|
-
return type;
|
|
3737
|
-
};
|
|
3738
|
-
/**
|
|
3739
|
-
* @private
|
|
3740
|
-
* @param {any} node prosemirror node
|
|
3741
|
-
* @param {BindingMetadata} meta
|
|
3742
|
-
* @return {Y.XmlElement}
|
|
3743
|
-
*/
|
|
3744
|
-
const createTypeFromElementNode = (node, meta) => {
|
|
3745
|
-
const type = new Y.XmlElement(node.type.name);
|
|
3746
|
-
for (const key in node.attrs) {
|
|
3747
|
-
const val = node.attrs[key];
|
|
3748
|
-
if (val !== null && key !== "ychange") type.setAttribute(key, val);
|
|
3749
|
-
}
|
|
3750
|
-
type.insert(0, normalizePNodeContent(node).map((n) => createTypeFromTextOrElementNode(n, meta)));
|
|
3751
|
-
meta.mapping.set(type, node);
|
|
3752
|
-
return type;
|
|
3753
|
-
};
|
|
3754
|
-
/**
|
|
3755
|
-
* @private
|
|
3756
|
-
* @param {PModel.Node|Array<PModel.Node>} node prosemirror text node
|
|
3757
|
-
* @param {BindingMetadata} meta
|
|
3758
|
-
* @return {Y.XmlElement|Y.XmlText}
|
|
3759
|
-
*/
|
|
3760
|
-
const createTypeFromTextOrElementNode = (node, meta) => node instanceof Array ? createTypeFromTextNodes(node, meta) : createTypeFromElementNode(node, meta);
|
|
3761
|
-
/**
|
|
3762
|
-
* @param {any} val
|
|
3763
|
-
*/
|
|
3764
|
-
const isObject = (val) => typeof val === "object" && val !== null;
|
|
3765
|
-
/**
|
|
3766
|
-
* @param {any} pattrs
|
|
3767
|
-
* @param {any} yattrs
|
|
3768
|
-
*/
|
|
3769
|
-
const equalAttrs = (pattrs, yattrs) => {
|
|
3770
|
-
const keys = Object.keys(pattrs).filter((key) => pattrs[key] !== null);
|
|
3771
|
-
let eq = keys.length === Object.keys(yattrs).filter((key) => yattrs[key] !== null).length;
|
|
3772
|
-
for (let i = 0; i < keys.length && eq; i++) {
|
|
3773
|
-
const key = keys[i];
|
|
3774
|
-
const l = pattrs[key];
|
|
3775
|
-
const r = yattrs[key];
|
|
3776
|
-
eq = key === "ychange" || l === r || isObject(l) && isObject(r) && equalAttrs(l, r);
|
|
3777
|
-
}
|
|
3778
|
-
return eq;
|
|
3779
|
-
};
|
|
3780
|
-
/**
|
|
3781
|
-
* @typedef {Array<Array<PModel.Node>|PModel.Node>} NormalizedPNodeContent
|
|
3782
|
-
*/
|
|
3783
|
-
/**
|
|
3784
|
-
* @param {any} pnode
|
|
3785
|
-
* @return {NormalizedPNodeContent}
|
|
3786
|
-
*/
|
|
3787
|
-
const normalizePNodeContent = (pnode) => {
|
|
3788
|
-
const c = pnode.content.content;
|
|
3789
|
-
const res = [];
|
|
3790
|
-
for (let i = 0; i < c.length; i++) {
|
|
3791
|
-
const n = c[i];
|
|
3792
|
-
if (n.isText) {
|
|
3793
|
-
const textNodes = [];
|
|
3794
|
-
for (let tnode = c[i]; i < c.length && tnode.isText; tnode = c[++i]) textNodes.push(tnode);
|
|
3795
|
-
i--;
|
|
3796
|
-
res.push(textNodes);
|
|
3797
|
-
} else res.push(n);
|
|
3798
|
-
}
|
|
3799
|
-
return res;
|
|
3800
|
-
};
|
|
3801
|
-
/**
|
|
3802
|
-
* @param {Y.XmlText} ytext
|
|
3803
|
-
* @param {Array<any>} ptexts
|
|
3804
|
-
*/
|
|
3805
|
-
const equalYTextPText = (ytext, ptexts) => {
|
|
3806
|
-
const delta = ytext.toDelta();
|
|
3807
|
-
return delta.length === ptexts.length && delta.every(
|
|
3808
|
-
/** @type {(d:any,i:number) => boolean} */
|
|
3809
|
-
(d, i) => d.insert === ptexts[i].text && object.keys(d.attributes || {}).length === ptexts[i].marks.length && object.every(d.attributes, (attr, yattrname) => {
|
|
3810
|
-
const markname = yattr2markname(yattrname);
|
|
3811
|
-
const pmarks = ptexts[i].marks;
|
|
3812
|
-
if (!pmarks.find(
|
|
3813
|
-
/** @param {any} mark */
|
|
3814
|
-
(mark) => mark.type.name === markname
|
|
3815
|
-
)) return false;
|
|
3816
|
-
return equalAttrs(attr, pmarks.find(
|
|
3817
|
-
/** @param {any} mark */
|
|
3818
|
-
(mark) => mark.type.name === markname
|
|
3819
|
-
)?.attrs);
|
|
3820
|
-
})
|
|
3821
|
-
);
|
|
3822
|
-
};
|
|
3823
|
-
/**
|
|
3824
|
-
* @param {Y.XmlElement|Y.XmlText|Y.XmlHook} ytype
|
|
3825
|
-
* @param {any|Array<any>} pnode
|
|
3826
|
-
*/
|
|
3827
|
-
const equalYTypePNode = (ytype, pnode) => {
|
|
3828
|
-
if (ytype instanceof Y.XmlElement && !(pnode instanceof Array) && matchNodeName(ytype, pnode)) {
|
|
3829
|
-
const normalizedContent = normalizePNodeContent(pnode);
|
|
3830
|
-
return ytype._length === normalizedContent.length && equalAttrs(ytype.getAttributes(), pnode.attrs) && ytype.toArray().every((ychild, i) => equalYTypePNode(ychild, normalizedContent[i]));
|
|
3831
|
-
}
|
|
3832
|
-
return ytype instanceof Y.XmlText && pnode instanceof Array && equalYTextPText(ytype, pnode);
|
|
3833
|
-
};
|
|
3834
|
-
/**
|
|
3835
|
-
* For a given Y type and ProseMirror node content, check if the ProseMirror node content is the same as the mapped ProseMirror node content.
|
|
3836
|
-
*
|
|
3837
|
-
* @param {PModel.Node | Array<PModel.Node> | undefined} mapped The ProseMirror node content that is mapped to the Y type.
|
|
3838
|
-
* @param {PModel.Node | Array<PModel.Node>} pcontent The ProseMirror node content to compare against the mapped content.
|
|
3839
|
-
* @returns {boolean} Returns true if the mapped content is the same as the ProseMirror node content, false otherwise.
|
|
3840
|
-
*/
|
|
3841
|
-
const mappedIdentity = (mapped, pcontent) => mapped === pcontent || mapped instanceof Array && pcontent instanceof Array && mapped.length === pcontent.length && mapped.every((a, i) => pcontent[i] === a);
|
|
3842
|
-
/**
|
|
3843
|
-
* @typedef {{ yIndex: number, pIndex: number, yChild: Y.XmlElement | Y.XmlText | Y.XmlHook }} MappedChildAnchor
|
|
3844
|
-
*/
|
|
3845
|
-
/**
|
|
3846
|
-
* Find mapped children that retain their relative order after a move.
|
|
3847
|
-
*
|
|
3848
|
-
* @param {Array<Y.XmlElement | Y.XmlText | Y.XmlHook>} yChildren
|
|
3849
|
-
* @param {NormalizedPNodeContent} pChildren
|
|
3850
|
-
* @param {BindingMetadata} meta
|
|
3851
|
-
* @return {Array<MappedChildAnchor>}
|
|
3852
|
-
*/
|
|
3853
|
-
const findMappedChildAnchors = (yChildren, pChildren, meta) => {
|
|
3854
|
-
const pChildIndices = /* @__PURE__ */ new Map();
|
|
3855
|
-
pChildren.forEach((pChild, index) => {
|
|
3856
|
-
if (!pChildIndices.has(pChild)) pChildIndices.set(pChild, index);
|
|
3857
|
-
});
|
|
3858
|
-
const candidates = [];
|
|
3859
|
-
yChildren.forEach((yChild, yIndex) => {
|
|
3860
|
-
const mapped = meta.mapping.get(yChild);
|
|
3861
|
-
const pIndex = pChildIndices.get(mapped);
|
|
3862
|
-
if (pIndex !== void 0 && mappedIdentity(mapped, pChildren[pIndex])) candidates.push({
|
|
3863
|
-
yIndex,
|
|
3864
|
-
pIndex,
|
|
3865
|
-
yChild
|
|
3866
|
-
});
|
|
3867
|
-
});
|
|
3868
|
-
if (candidates.length === 0) return [];
|
|
3869
|
-
const predecessors = new Array(candidates.length);
|
|
3870
|
-
const tails = [];
|
|
3871
|
-
for (let index = 0; index < candidates.length; index++) {
|
|
3872
|
-
let low = 0;
|
|
3873
|
-
let high = tails.length;
|
|
3874
|
-
while (low < high) {
|
|
3875
|
-
const middle = low + high >> 1;
|
|
3876
|
-
if (candidates[tails[middle]].pIndex < candidates[index].pIndex) low = middle + 1;
|
|
3877
|
-
else high = middle;
|
|
3878
|
-
}
|
|
3879
|
-
predecessors[index] = low > 0 ? tails[low - 1] : -1;
|
|
3880
|
-
tails[low] = index;
|
|
3881
|
-
}
|
|
3882
|
-
const anchors = [];
|
|
3883
|
-
for (let index = tails[tails.length - 1]; index !== -1; index = predecessors[index]) anchors.push(candidates[index]);
|
|
3884
|
-
return anchors.reverse();
|
|
3885
|
-
};
|
|
3886
|
-
/**
|
|
3887
|
-
* @param {Y.XmlFragment} yDomFragment
|
|
3888
|
-
* @param {number} index
|
|
3889
|
-
* @param {number} length
|
|
3890
|
-
* @param {BindingMetadata} meta
|
|
3891
|
-
*/
|
|
3892
|
-
const deleteYChildren = (yDomFragment, index, length, meta) => {
|
|
3893
|
-
if (length > 0) {
|
|
3894
|
-
yDomFragment.slice(index, index + length).forEach((type) => meta.mapping.delete(type));
|
|
3895
|
-
yDomFragment.delete(index, length);
|
|
3896
|
-
}
|
|
3897
|
-
};
|
|
3898
|
-
/**
|
|
3899
|
-
* @param {Y.XmlFragment} yDomFragment
|
|
3900
|
-
* @param {number} index
|
|
3901
|
-
* @param {NormalizedPNodeContent} pChildren
|
|
3902
|
-
* @param {number} start
|
|
3903
|
-
* @param {number} end
|
|
3904
|
-
* @param {BindingMetadata} meta
|
|
3905
|
-
*/
|
|
3906
|
-
const insertPChildren = (yDomFragment, index, pChildren, start, end, meta) => {
|
|
3907
|
-
if (start < end) {
|
|
3908
|
-
const children = [];
|
|
3909
|
-
for (let childIndex = start; childIndex < end; childIndex++) children.push(createTypeFromTextOrElementNode(pChildren[childIndex], meta));
|
|
3910
|
-
yDomFragment.insert(index, children);
|
|
3911
|
-
}
|
|
3912
|
-
};
|
|
3913
|
-
/**
|
|
3914
|
-
* Updates a Y child in place when it still lines up with the ProseMirror child.
|
|
3915
|
-
* Recreating it instead would drop concurrent remote edits on that child.
|
|
3916
|
-
*
|
|
3917
|
-
* @param {{ transact: Function }} y
|
|
3918
|
-
* @param {Y.XmlElement | Y.XmlText | Y.XmlHook} yChild
|
|
3919
|
-
* @param {PModel.Node | Array<PModel.Node>} pChild
|
|
3920
|
-
* @param {BindingMetadata} meta
|
|
3921
|
-
* @return {boolean}
|
|
3922
|
-
*/
|
|
3923
|
-
const updateMatchingYChild = (y, yChild, pChild, meta) => {
|
|
3924
|
-
if (yChild instanceof Y.XmlText && pChild instanceof Array) {
|
|
3925
|
-
if (equalYTextPText(yChild, pChild)) meta.mapping.set(yChild, pChild);
|
|
3926
|
-
else updateYText(yChild, pChild, meta);
|
|
3927
|
-
return true;
|
|
3928
|
-
}
|
|
3929
|
-
if (yChild instanceof Y.XmlElement && !(pChild instanceof Array) && matchNodeName(yChild, pChild)) {
|
|
3930
|
-
if (equalYTypePNode(yChild, pChild)) meta.mapping.set(yChild, pChild);
|
|
3931
|
-
else updateYFragment(y, yChild, pChild, meta);
|
|
3932
|
-
return true;
|
|
3933
|
-
}
|
|
3934
|
-
return false;
|
|
3935
|
-
};
|
|
3936
|
-
/**
|
|
3937
|
-
* Reconciles the children between two anchors by position, then deletes or
|
|
3938
|
-
* inserts to make up the length difference.
|
|
3939
|
-
*
|
|
3940
|
-
* @param {{ transact: Function }} y
|
|
3941
|
-
* @param {Y.XmlFragment} yDomFragment
|
|
3942
|
-
* @param {NormalizedPNodeContent} pChildren
|
|
3943
|
-
* @param {Array<Y.XmlElement | Y.XmlText | Y.XmlHook>} yChildren
|
|
3944
|
-
* @param {number} yStart
|
|
3945
|
-
* @param {number} yEnd
|
|
3946
|
-
* @param {number} pStart
|
|
3947
|
-
* @param {number} pEnd
|
|
3948
|
-
* @param {BindingMetadata} meta
|
|
3949
|
-
*/
|
|
3950
|
-
const reconcileUnanchoredChildren = (y, yDomFragment, pChildren, yChildren, yStart, yEnd, pStart, pEnd, meta) => {
|
|
3951
|
-
const matchingChildren = math.min(yEnd - yStart, pEnd - pStart);
|
|
3952
|
-
for (let index = 0; index < matchingChildren; index++) {
|
|
3953
|
-
const yChild = yChildren[yStart + index];
|
|
3954
|
-
const pChild = pChildren[pStart + index];
|
|
3955
|
-
if (!updateMatchingYChild(y, yChild, pChild, meta)) {
|
|
3956
|
-
deleteYChildren(yDomFragment, pStart + index, 1, meta);
|
|
3957
|
-
insertPChildren(yDomFragment, pStart + index, pChildren, pStart + index, pStart + index + 1, meta);
|
|
3958
|
-
}
|
|
3959
|
-
}
|
|
3960
|
-
deleteYChildren(yDomFragment, pStart + matchingChildren, yEnd - yStart - matchingChildren, meta);
|
|
3961
|
-
insertPChildren(yDomFragment, pStart + matchingChildren, pChildren, pStart + matchingChildren, pEnd, meta);
|
|
3962
|
-
};
|
|
3963
|
-
/**
|
|
3964
|
-
* Reconciles a fragment whose children were reordered, keeping the Y types of the
|
|
3965
|
-
* anchors. The left/right diff in `updateYFragment` mismatches on both ends after
|
|
3966
|
-
* a move, so it rebuilds the middle and drops text that is still being edited.
|
|
3967
|
-
*
|
|
3968
|
-
* @param {{ transact: Function }} y
|
|
3969
|
-
* @param {Y.XmlFragment} yDomFragment
|
|
3970
|
-
* @param {NormalizedPNodeContent} pChildren
|
|
3971
|
-
* @param {Array<Y.XmlElement | Y.XmlText | Y.XmlHook>} yChildren
|
|
3972
|
-
* @param {BindingMetadata} meta
|
|
3973
|
-
* @return {boolean}
|
|
3974
|
-
*/
|
|
3975
|
-
const reconcileMappedChildren = (y, yDomFragment, pChildren, yChildren, meta) => {
|
|
3976
|
-
const anchors = findMappedChildAnchors(yChildren, pChildren, meta);
|
|
3977
|
-
if (anchors.length < 2 || !anchors.some((anchor) => anchor.yIndex !== anchor.pIndex) || anchors.some((anchor) => {
|
|
3978
|
-
const pChild = pChildren[anchor.pIndex];
|
|
3979
|
-
return !(anchor.yChild instanceof Y.XmlText && pChild instanceof Array || anchor.yChild instanceof Y.XmlElement && !(pChild instanceof Array));
|
|
3980
|
-
})) return false;
|
|
3981
|
-
let yIndex = 0;
|
|
3982
|
-
let pIndex = 0;
|
|
3983
|
-
for (const anchor of anchors) {
|
|
3984
|
-
reconcileUnanchoredChildren(y, yDomFragment, pChildren, yChildren, yIndex, anchor.yIndex, pIndex, anchor.pIndex, meta);
|
|
3985
|
-
const pChild = pChildren[anchor.pIndex];
|
|
3986
|
-
updateMatchingYChild(y, anchor.yChild, pChild, meta);
|
|
3987
|
-
yIndex = anchor.yIndex + 1;
|
|
3988
|
-
pIndex = anchor.pIndex + 1;
|
|
3989
|
-
}
|
|
3990
|
-
reconcileUnanchoredChildren(y, yDomFragment, pChildren, yChildren, yIndex, yChildren.length, pIndex, pChildren.length, meta);
|
|
3991
|
-
return true;
|
|
3992
|
-
};
|
|
3993
|
-
/**
|
|
3994
|
-
* @param {Y.XmlElement} ytype
|
|
3995
|
-
* @param {PModel.Node} pnode
|
|
3996
|
-
* @param {BindingMetadata} meta
|
|
3997
|
-
* @return {{ foundMappedChild: boolean, equalityFactor: number }}
|
|
3998
|
-
*/
|
|
3999
|
-
const computeChildEqualityFactor = (ytype, pnode, meta) => {
|
|
4000
|
-
const yChildren = ytype.toArray();
|
|
4001
|
-
const pChildren = normalizePNodeContent(pnode);
|
|
4002
|
-
const pChildCnt = pChildren.length;
|
|
4003
|
-
const yChildCnt = yChildren.length;
|
|
4004
|
-
const minCnt = math.min(yChildCnt, pChildCnt);
|
|
4005
|
-
let left = 0;
|
|
4006
|
-
let right = 0;
|
|
4007
|
-
let foundMappedChild = false;
|
|
4008
|
-
for (; left < minCnt; left++) {
|
|
4009
|
-
const leftY = yChildren[left];
|
|
4010
|
-
const leftP = pChildren[left];
|
|
4011
|
-
if (mappedIdentity(meta.mapping.get(leftY), leftP)) foundMappedChild = true;
|
|
4012
|
-
else if (!equalYTypePNode(leftY, leftP)) break;
|
|
4013
|
-
}
|
|
4014
|
-
for (; left + right < minCnt; right++) {
|
|
4015
|
-
const rightY = yChildren[yChildCnt - right - 1];
|
|
4016
|
-
const rightP = pChildren[pChildCnt - right - 1];
|
|
4017
|
-
if (mappedIdentity(meta.mapping.get(rightY), rightP)) foundMappedChild = true;
|
|
4018
|
-
else if (!equalYTypePNode(rightY, rightP)) break;
|
|
4019
|
-
}
|
|
4020
|
-
return {
|
|
4021
|
-
equalityFactor: left + right,
|
|
4022
|
-
foundMappedChild
|
|
4023
|
-
};
|
|
4024
|
-
};
|
|
4025
|
-
/**
|
|
4026
|
-
* @param {Y.Text} ytext
|
|
4027
|
-
*/
|
|
4028
|
-
const ytextTrans = (ytext) => {
|
|
4029
|
-
let str = "";
|
|
4030
|
-
/**
|
|
4031
|
-
* @type {Y.Item|null}
|
|
4032
|
-
*/
|
|
4033
|
-
let n = ytext._start;
|
|
4034
|
-
const nAttrs = {};
|
|
4035
|
-
while (n !== null) {
|
|
4036
|
-
if (!n.deleted) {
|
|
4037
|
-
if (n.countable && n.content instanceof Y.ContentString) str += n.content.str;
|
|
4038
|
-
else if (n.content instanceof Y.ContentFormat) nAttrs[n.content.key] = null;
|
|
4039
|
-
}
|
|
4040
|
-
n = n.right;
|
|
4041
|
-
}
|
|
4042
|
-
return {
|
|
4043
|
-
str,
|
|
4044
|
-
nAttrs
|
|
4045
|
-
};
|
|
4046
|
-
};
|
|
4047
|
-
/**
|
|
4048
|
-
* @todo test this more
|
|
4049
|
-
*
|
|
4050
|
-
* @param {Y.Text} ytext
|
|
4051
|
-
* @param {Array<any>} ptexts
|
|
4052
|
-
* @param {BindingMetadata} meta
|
|
4053
|
-
*/
|
|
4054
|
-
const updateYText = (ytext, ptexts, meta) => {
|
|
4055
|
-
meta.mapping.set(ytext, ptexts);
|
|
4056
|
-
const { nAttrs, str } = ytextTrans(ytext);
|
|
4057
|
-
const content = ptexts.map((p) => ({
|
|
4058
|
-
insert: p.text,
|
|
4059
|
-
attributes: Object.assign({}, nAttrs, marksToAttributes(p.marks, meta))
|
|
4060
|
-
}));
|
|
4061
|
-
const { insert, remove, index } = simpleDiff(str, content.map((c) => c.insert).join(""));
|
|
4062
|
-
ytext.delete(index, remove);
|
|
4063
|
-
ytext.insert(index, insert);
|
|
4064
|
-
ytext.applyDelta(content.map((c) => ({
|
|
4065
|
-
retain: c.insert.length,
|
|
4066
|
-
attributes: c.attributes
|
|
4067
|
-
})));
|
|
4068
|
-
};
|
|
4069
|
-
const hashedMarkNameRegex = /(.*)(--[a-zA-Z0-9+/=]{8})$/;
|
|
4070
|
-
/**
|
|
4071
|
-
* @param {string} attrName
|
|
4072
|
-
*/
|
|
4073
|
-
const yattr2markname = (attrName) => hashedMarkNameRegex.exec(attrName)?.[1] ?? attrName;
|
|
4074
|
-
/**
|
|
4075
|
-
* @todo move this to markstoattributes
|
|
4076
|
-
*
|
|
4077
|
-
* @param {Object<string, any>} attrs
|
|
4078
|
-
* @param {import('prosemirror-model').Schema} schema
|
|
4079
|
-
*/
|
|
4080
|
-
const attributesToMarks = (attrs, schema) => {
|
|
4081
|
-
/**
|
|
4082
|
-
* @type {Array<import('prosemirror-model').Mark>}
|
|
4083
|
-
*/
|
|
4084
|
-
const marks = [];
|
|
4085
|
-
for (const markName in attrs) marks.push(schema.mark(yattr2markname(markName), attrs[markName]));
|
|
4086
|
-
return marks;
|
|
4087
|
-
};
|
|
4088
|
-
/**
|
|
4089
|
-
* @param {Array<import('prosemirror-model').Mark>} marks
|
|
4090
|
-
* @param {BindingMetadata} meta
|
|
4091
|
-
*/
|
|
4092
|
-
const marksToAttributes = (marks, meta) => {
|
|
4093
|
-
const pattrs = {};
|
|
4094
|
-
marks.forEach((mark) => {
|
|
4095
|
-
if (mark.type.name !== "ychange") {
|
|
4096
|
-
const isOverlapping = map.setIfUndefined(meta.isOMark, mark.type, () => !mark.type.excludes(mark.type));
|
|
4097
|
-
pattrs[isOverlapping ? `${mark.type.name}--${hashOfJSON(mark.toJSON())}` : mark.type.name] = mark.attrs;
|
|
4098
|
-
}
|
|
4099
|
-
});
|
|
4100
|
-
return pattrs;
|
|
4101
|
-
};
|
|
4102
|
-
/**
|
|
4103
|
-
* Update a yDom node by syncing the current content of the prosemirror node.
|
|
4104
|
-
*
|
|
4105
|
-
* This is a y-prosemirror internal feature that you can use at your own risk.
|
|
4106
|
-
*
|
|
4107
|
-
* @private
|
|
4108
|
-
* @unstable
|
|
4109
|
-
*
|
|
4110
|
-
* @param {{transact: Function}} y
|
|
4111
|
-
* @param {Y.XmlFragment} yDomFragment
|
|
4112
|
-
* @param {any} pNode
|
|
4113
|
-
* @param {BindingMetadata} meta
|
|
4114
|
-
*/
|
|
4115
|
-
const updateYFragment = (y, yDomFragment, pNode, meta) => {
|
|
4116
|
-
if (yDomFragment instanceof Y.XmlElement && yDomFragment.nodeName !== pNode.type.name) throw new Error("node name mismatch!");
|
|
4117
|
-
meta.mapping.set(yDomFragment, pNode);
|
|
4118
|
-
if (yDomFragment instanceof Y.XmlElement) {
|
|
4119
|
-
const yDomAttrs = yDomFragment.getAttributes();
|
|
4120
|
-
const pAttrs = pNode.attrs;
|
|
4121
|
-
for (const key in pAttrs) if (pAttrs[key] !== null) {
|
|
4122
|
-
if (yDomAttrs[key] !== pAttrs[key] && key !== "ychange") yDomFragment.setAttribute(key, pAttrs[key]);
|
|
4123
|
-
} else yDomFragment.removeAttribute(key);
|
|
4124
|
-
for (const key in yDomAttrs) if (pAttrs[key] === void 0) yDomFragment.removeAttribute(key);
|
|
4125
|
-
}
|
|
4126
|
-
const pChildren = normalizePNodeContent(pNode);
|
|
4127
|
-
const pChildCnt = pChildren.length;
|
|
4128
|
-
const yChildren = yDomFragment.toArray();
|
|
4129
|
-
const yChildCnt = yChildren.length;
|
|
4130
|
-
const minCnt = math.min(pChildCnt, yChildCnt);
|
|
4131
|
-
if (reconcileMappedChildren(y, yDomFragment, pChildren, yChildren, meta)) return;
|
|
4132
|
-
let left = 0;
|
|
4133
|
-
let right = 0;
|
|
4134
|
-
for (; left < minCnt; left++) {
|
|
4135
|
-
const leftY = yChildren[left];
|
|
4136
|
-
const leftP = pChildren[left];
|
|
4137
|
-
if (!mappedIdentity(meta.mapping.get(leftY), leftP)) if (equalYTypePNode(leftY, leftP)) meta.mapping.set(leftY, leftP);
|
|
4138
|
-
else break;
|
|
4139
|
-
}
|
|
4140
|
-
for (; right + left + 1 < minCnt; right++) {
|
|
4141
|
-
const rightY = yChildren[yChildCnt - right - 1];
|
|
4142
|
-
const rightP = pChildren[pChildCnt - right - 1];
|
|
4143
|
-
if (!mappedIdentity(meta.mapping.get(rightY), rightP)) if (equalYTypePNode(rightY, rightP)) meta.mapping.set(rightY, rightP);
|
|
4144
|
-
else break;
|
|
4145
|
-
}
|
|
4146
|
-
y.transact(() => {
|
|
4147
|
-
while (yChildCnt - left - right > 0 && pChildCnt - left - right > 0) {
|
|
4148
|
-
const leftY = yChildren[left];
|
|
4149
|
-
const leftP = pChildren[left];
|
|
4150
|
-
const rightY = yChildren[yChildCnt - right - 1];
|
|
4151
|
-
const rightP = pChildren[pChildCnt - right - 1];
|
|
4152
|
-
if (leftY instanceof Y.XmlText && leftP instanceof Array) {
|
|
4153
|
-
if (!equalYTextPText(leftY, leftP)) updateYText(leftY, leftP, meta);
|
|
4154
|
-
left += 1;
|
|
4155
|
-
} else {
|
|
4156
|
-
let updateLeft = leftY instanceof Y.XmlElement && matchNodeName(leftY, leftP);
|
|
4157
|
-
let updateRight = rightY instanceof Y.XmlElement && matchNodeName(rightY, rightP);
|
|
4158
|
-
if (updateLeft && updateRight) {
|
|
4159
|
-
const equalityLeft = computeChildEqualityFactor(leftY, leftP, meta);
|
|
4160
|
-
const equalityRight = computeChildEqualityFactor(rightY, rightP, meta);
|
|
4161
|
-
if (equalityLeft.foundMappedChild && !equalityRight.foundMappedChild) updateRight = false;
|
|
4162
|
-
else if (!equalityLeft.foundMappedChild && equalityRight.foundMappedChild) updateLeft = false;
|
|
4163
|
-
else if (equalityLeft.equalityFactor < equalityRight.equalityFactor) updateLeft = false;
|
|
4164
|
-
else updateRight = false;
|
|
4165
|
-
}
|
|
4166
|
-
if (updateLeft) {
|
|
4167
|
-
updateYFragment(y, leftY, leftP, meta);
|
|
4168
|
-
left += 1;
|
|
4169
|
-
} else if (updateRight) {
|
|
4170
|
-
updateYFragment(y, rightY, rightP, meta);
|
|
4171
|
-
right += 1;
|
|
4172
|
-
} else {
|
|
4173
|
-
meta.mapping.delete(yDomFragment.get(left));
|
|
4174
|
-
yDomFragment.delete(left, 1);
|
|
4175
|
-
yDomFragment.insert(left, [createTypeFromTextOrElementNode(leftP, meta)]);
|
|
4176
|
-
left += 1;
|
|
4177
|
-
}
|
|
4178
|
-
}
|
|
4179
|
-
}
|
|
4180
|
-
const yDelLen = yChildCnt - left - right;
|
|
4181
|
-
if (yChildCnt === 1 && pChildCnt === 0 && yChildren[0] instanceof Y.XmlText) {
|
|
4182
|
-
meta.mapping.delete(yChildren[0]);
|
|
4183
|
-
yChildren[0].delete(0, yChildren[0].length);
|
|
4184
|
-
} else if (yDelLen > 0) {
|
|
4185
|
-
yDomFragment.slice(left, left + yDelLen).forEach((type) => meta.mapping.delete(type));
|
|
4186
|
-
yDomFragment.delete(left, yDelLen);
|
|
4187
|
-
}
|
|
4188
|
-
if (left + right < pChildCnt) {
|
|
4189
|
-
const ins = [];
|
|
4190
|
-
for (let i = left; i < pChildCnt - right; i++) ins.push(createTypeFromTextOrElementNode(pChildren[i], meta));
|
|
4191
|
-
yDomFragment.insert(left, ins);
|
|
4192
|
-
}
|
|
4193
|
-
}, ySyncPluginKey);
|
|
4194
|
-
};
|
|
4195
|
-
/**
|
|
4196
|
-
* @function
|
|
4197
|
-
* @param {Y.XmlElement} yElement
|
|
4198
|
-
* @param {any} pNode Prosemirror Node
|
|
4199
|
-
*/
|
|
4200
|
-
const matchNodeName = (yElement, pNode) => !(pNode instanceof Array) && yElement.nodeName === pNode.type.name;
|
|
4201
|
-
/**
|
|
4202
|
-
* Transforms a Prosemirror based absolute position to a Yjs Cursor (relative position in the Yjs model).
|
|
4203
|
-
*
|
|
4204
|
-
* @param {number} pos
|
|
4205
|
-
* @param {Y.XmlFragment} type
|
|
4206
|
-
* @param {ProsemirrorMapping} mapping
|
|
4207
|
-
* @return {any} relative position
|
|
4208
|
-
*/
|
|
4209
|
-
const absolutePositionToRelativePosition = (pos, type, mapping) => {
|
|
4210
|
-
if (pos === 0) return Y.createRelativePositionFromTypeIndex(type, 0, -1);
|
|
4211
|
-
/**
|
|
4212
|
-
* @type {any}
|
|
4213
|
-
*/
|
|
4214
|
-
let n = type._first === null ? null : /** @type {Y.ContentType} */ type._first.content.type;
|
|
4215
|
-
while (n !== null && type !== n) {
|
|
4216
|
-
if (n instanceof Y.XmlText) {
|
|
4217
|
-
if (n._length >= pos) return Y.createRelativePositionFromTypeIndex(n, pos, -1);
|
|
4218
|
-
else pos -= n._length;
|
|
4219
|
-
if (n._item !== null && n._item.next !== null) n = n._item.next.content.type;
|
|
4220
|
-
else {
|
|
4221
|
-
do {
|
|
4222
|
-
n = n._item === null ? null : n._item.parent;
|
|
4223
|
-
pos--;
|
|
4224
|
-
} while (n !== type && n !== null && n._item !== null && n._item.next === null);
|
|
4225
|
-
if (n !== null && n !== type) n = n._item === null ? null : /** @type {Y.ContentType} */ n._item.next.content.type;
|
|
4226
|
-
}
|
|
4227
|
-
} else {
|
|
4228
|
-
const pNodeSize = (mapping.get(n) || { nodeSize: 0 }).nodeSize;
|
|
4229
|
-
if (n._first !== null && pos < pNodeSize) {
|
|
4230
|
-
n = n._first.content.type;
|
|
4231
|
-
pos--;
|
|
4232
|
-
} else {
|
|
4233
|
-
if (pos === 1 && n._length === 0 && pNodeSize > 1) return new Y.RelativePosition(n._item === null ? null : n._item.id, n._item === null ? Y.findRootTypeKey(n) : null, null);
|
|
4234
|
-
pos -= pNodeSize;
|
|
4235
|
-
if (n._item !== null && n._item.next !== null) n = n._item.next.content.type;
|
|
4236
|
-
else {
|
|
4237
|
-
if (pos === 0) {
|
|
4238
|
-
n = n._item === null ? n : n._item.parent;
|
|
4239
|
-
return new Y.RelativePosition(n._item === null ? null : n._item.id, n._item === null ? Y.findRootTypeKey(n) : null, null);
|
|
4240
|
-
}
|
|
4241
|
-
do {
|
|
4242
|
-
n = n._item.parent;
|
|
4243
|
-
pos--;
|
|
4244
|
-
} while (n !== type && n._item.next === null);
|
|
4245
|
-
if (n !== type) n = n._item.next.content.type;
|
|
4246
|
-
}
|
|
4247
|
-
}
|
|
4248
|
-
}
|
|
4249
|
-
if (n === null) throw error.unexpectedCase();
|
|
4250
|
-
if (pos === 0 && n.constructor !== Y.XmlText && n !== type) return createRelativePosition(n._item.parent, n._item);
|
|
4251
|
-
}
|
|
4252
|
-
return Y.createRelativePositionFromTypeIndex(type, type._length, -1);
|
|
4253
|
-
};
|
|
4254
|
-
/**
|
|
4255
|
-
* Item-id based relative positions can misresolve to the document start after
|
|
4256
|
-
* block reorder during collaborative drag-and-drop.
|
|
4257
|
-
*
|
|
4258
|
-
* @param {Y.Doc} y
|
|
4259
|
-
* @param {Y.RelativePosition} relPos
|
|
4260
|
-
* @param {number|null} absPos
|
|
4261
|
-
* @return {boolean}
|
|
4262
|
-
*/
|
|
4263
|
-
const isMisresolvedTextPosition = (y, relPos, absPos) => {
|
|
4264
|
-
if (absPos === null) return false;
|
|
4265
|
-
const decoded = Y.createAbsolutePositionFromRelativePosition(relPos, y);
|
|
4266
|
-
return decoded !== null && decoded.type instanceof Y.XmlText && relPos.item !== null && absPos <= 1;
|
|
4267
|
-
};
|
|
4268
|
-
const createRelativePosition = (type, item) => {
|
|
4269
|
-
let typeid = null;
|
|
4270
|
-
let tname = null;
|
|
4271
|
-
if (type._item === null) tname = Y.findRootTypeKey(type);
|
|
4272
|
-
else typeid = Y.createID(type._item.id.client, type._item.id.clock);
|
|
4273
|
-
return new Y.RelativePosition(typeid, tname, item.id);
|
|
4274
|
-
};
|
|
4275
|
-
/**
|
|
4276
|
-
* @param {Y.Doc} y
|
|
4277
|
-
* @param {Y.XmlFragment} documentType Top level type that is bound to pView
|
|
4278
|
-
* @param {any} relPos Encoded Yjs based relative position
|
|
4279
|
-
* @param {ProsemirrorMapping} mapping
|
|
4280
|
-
* @return {null|number}
|
|
4281
|
-
*/
|
|
4282
|
-
const relativePositionToAbsolutePosition = (y, documentType, relPos, mapping) => {
|
|
4283
|
-
const decodedPos = Y.createAbsolutePositionFromRelativePosition(relPos, y);
|
|
4284
|
-
if (decodedPos === null || decodedPos.type !== documentType && !Y.isParentOf(documentType, decodedPos.type._item)) return null;
|
|
4285
|
-
let type = decodedPos.type;
|
|
4286
|
-
let pos = 0;
|
|
4287
|
-
if (type.constructor === Y.XmlText) pos = decodedPos.index;
|
|
4288
|
-
else if (type._item === null || !type._item.deleted) {
|
|
4289
|
-
let n = type._first;
|
|
4290
|
-
let i = 0;
|
|
4291
|
-
while (i < type._length && i < decodedPos.index && n !== null) {
|
|
4292
|
-
if (!n.deleted) {
|
|
4293
|
-
const t = n.content.type;
|
|
4294
|
-
i++;
|
|
4295
|
-
if (t instanceof Y.XmlText) pos += t._length;
|
|
4296
|
-
else {
|
|
4297
|
-
const mapped = mapping.get(t);
|
|
4298
|
-
if (mapped == null) return null;
|
|
4299
|
-
pos += mapped.nodeSize;
|
|
4300
|
-
}
|
|
4301
|
-
}
|
|
4302
|
-
n = n.right;
|
|
4303
|
-
}
|
|
4304
|
-
pos += 1;
|
|
4305
|
-
}
|
|
4306
|
-
while (type !== documentType && type._item !== null) {
|
|
4307
|
-
const parent = type._item.parent;
|
|
4308
|
-
if (parent._item === null || !parent._item.deleted) {
|
|
4309
|
-
pos += 1;
|
|
4310
|
-
let n = parent._first;
|
|
4311
|
-
while (n !== null) {
|
|
4312
|
-
const contentType = n.content.type;
|
|
4313
|
-
if (contentType === type) break;
|
|
4314
|
-
if (!n.deleted) if (contentType instanceof Y.XmlText) pos += contentType._length;
|
|
4315
|
-
else {
|
|
4316
|
-
const mapped = mapping.get(contentType);
|
|
4317
|
-
if (mapped == null) return null;
|
|
4318
|
-
pos += mapped.nodeSize;
|
|
4319
|
-
}
|
|
4320
|
-
n = n.right;
|
|
4321
|
-
}
|
|
4322
|
-
}
|
|
4323
|
-
type = parent;
|
|
4324
|
-
}
|
|
4325
|
-
const absPos = pos - 1;
|
|
4326
|
-
if (isMisresolvedTextPosition(y, relPos, absPos)) return null;
|
|
4327
|
-
return absPos;
|
|
4328
|
-
};
|
|
4329
|
-
/**
|
|
4330
|
-
* Shallow attrs comparison. Attr values are primitives in most schemas;
|
|
4331
|
-
* non-primitive values fail the check and callers fall back to text matching.
|
|
4332
|
-
*
|
|
4333
|
-
* @param {Object<string, any>} a
|
|
4334
|
-
* @param {Object<string, any>} b
|
|
4335
|
-
* @return {boolean}
|
|
4336
|
-
*/
|
|
4337
|
-
const attrsEqual = (a, b) => {
|
|
4338
|
-
if (a === b) return true;
|
|
4339
|
-
const aKeys = Object.keys(a);
|
|
4340
|
-
return aKeys.length === Object.keys(b).length && aKeys.every((k) => a[k] === b[k]);
|
|
4341
|
-
};
|
|
4342
|
-
/**
|
|
4343
|
-
* Returns true when any attr deviates from its spec default or has none.
|
|
4344
|
-
* Default-only attrs cannot tell same-type siblings apart.
|
|
4345
|
-
*
|
|
4346
|
-
* @param {import('prosemirror-model').Node} node
|
|
4347
|
-
* @return {boolean}
|
|
4348
|
-
*/
|
|
4349
|
-
const hasDistinctiveAttrs = (node) => {
|
|
4350
|
-
const specAttrs = node.type.spec.attrs || {};
|
|
4351
|
-
return Object.keys(node.attrs).some((key) => {
|
|
4352
|
-
const spec = specAttrs[key];
|
|
4353
|
-
return spec == null || !Object.prototype.hasOwnProperty.call(spec, "default") || spec.default !== node.attrs[key];
|
|
4354
|
-
});
|
|
4355
|
-
};
|
|
4356
|
-
/**
|
|
4357
|
-
* Remaps a position into a matched block by walking the same child-index path
|
|
4358
|
-
* it had in the old block. A raw byte offset would overshoot into a sibling
|
|
4359
|
-
* inner textblock when the old block contains local keystrokes that are not
|
|
4360
|
-
* yet part of the rebuilt document.
|
|
4361
|
-
*
|
|
4362
|
-
* @param {import('prosemirror-model').ResolvedPos} $oldPos
|
|
4363
|
-
* @param {number} newBlockStart
|
|
4364
|
-
* @param {import('prosemirror-model').Node} newBlock
|
|
4365
|
-
* @return {number|null}
|
|
4366
|
-
*/
|
|
4367
|
-
const remapIntoBlock = ($oldPos, newBlockStart, newBlock) => {
|
|
4368
|
-
let pos = newBlockStart + 1;
|
|
4369
|
-
let node = newBlock;
|
|
4370
|
-
for (let depth = 1; depth < $oldPos.depth; depth++) {
|
|
4371
|
-
const idx = $oldPos.index(depth);
|
|
4372
|
-
if (idx >= node.childCount) return null;
|
|
4373
|
-
for (let i = 0; i < idx; i++) pos += node.child(i).nodeSize;
|
|
4374
|
-
pos += 1;
|
|
4375
|
-
node = node.child(idx);
|
|
4376
|
-
if (node.type !== $oldPos.node(depth + 1).type) return null;
|
|
4377
|
-
}
|
|
4378
|
-
if (!node.isTextblock) return null;
|
|
4379
|
-
return pos + Math.min($oldPos.parentOffset, node.content.size);
|
|
4380
|
-
};
|
|
4381
|
-
/**
|
|
4382
|
-
* @param {import('prosemirror-model').Node} oldDoc
|
|
4383
|
-
* @param {import('prosemirror-model').Node} newDoc
|
|
4384
|
-
* @param {number} absPos
|
|
4385
|
-
* @return {number|null}
|
|
4386
|
-
*/
|
|
4387
|
-
const findAbsolutePositionAfterStructuralChange = (oldDoc, newDoc, absPos) => {
|
|
4388
|
-
let pos = 0;
|
|
4389
|
-
let targetIdx = 0;
|
|
4390
|
-
for (; targetIdx < oldDoc.childCount; targetIdx++) {
|
|
4391
|
-
const child = oldDoc.child(targetIdx);
|
|
4392
|
-
if (pos + child.nodeSize > absPos) break;
|
|
4393
|
-
pos += child.nodeSize;
|
|
4394
|
-
}
|
|
4395
|
-
if (targetIdx >= oldDoc.childCount) return null;
|
|
4396
|
-
const targetChild = oldDoc.child(targetIdx);
|
|
4397
|
-
const $oldPos = oldDoc.resolve(absPos);
|
|
4398
|
-
/**
|
|
4399
|
-
* @param {number} newBlockStart
|
|
4400
|
-
* @param {import('prosemirror-model').Node} newBlock
|
|
4401
|
-
* @return {number|null}
|
|
4402
|
-
*/
|
|
4403
|
-
const place = (newBlockStart, newBlock) => {
|
|
4404
|
-
if ($oldPos.depth === 0) {
|
|
4405
|
-
const remapped = newBlockStart + (absPos - pos);
|
|
4406
|
-
const contentStart = newBlockStart + 1;
|
|
4407
|
-
const contentEnd = newBlockStart + newBlock.nodeSize - 1;
|
|
4408
|
-
return Math.max(contentStart, Math.min(remapped, contentEnd));
|
|
4409
|
-
}
|
|
4410
|
-
return remapIntoBlock($oldPos, newBlockStart, newBlock);
|
|
4411
|
-
};
|
|
4412
|
-
/**
|
|
4413
|
-
* Finds the Nth block in newDoc matching `pred`, where N is the number of
|
|
4414
|
-
* matching blocks in oldDoc up to and including the target block.
|
|
4415
|
-
*
|
|
4416
|
-
* @param {function(import('prosemirror-model').Node): boolean} pred
|
|
4417
|
-
* @param {boolean} requireUnique
|
|
4418
|
-
* @return {number|null}
|
|
4419
|
-
*/
|
|
4420
|
-
const findByPredicate = (pred, requireUnique = false) => {
|
|
4421
|
-
let occurrence = 0;
|
|
4422
|
-
for (let i = 0; i <= targetIdx; i++) if (pred(oldDoc.child(i))) occurrence++;
|
|
4423
|
-
let matchCount = 0;
|
|
4424
|
-
let matchStart = -1;
|
|
4425
|
-
let matchBlock = null;
|
|
4426
|
-
let newPos = 0;
|
|
4427
|
-
for (let i = 0; i < newDoc.childCount; i++) {
|
|
4428
|
-
const child = newDoc.child(i);
|
|
4429
|
-
if (pred(child)) {
|
|
4430
|
-
matchCount++;
|
|
4431
|
-
if (matchCount === occurrence) {
|
|
4432
|
-
matchStart = newPos;
|
|
4433
|
-
matchBlock = child;
|
|
4434
|
-
}
|
|
4435
|
-
}
|
|
4436
|
-
newPos += child.nodeSize;
|
|
4437
|
-
}
|
|
4438
|
-
if (matchBlock === null || requireUnique && (occurrence !== 1 || matchCount !== 1)) return null;
|
|
4439
|
-
return place(matchStart, matchBlock);
|
|
4440
|
-
};
|
|
4441
|
-
/**
|
|
4442
|
-
* @param {import('prosemirror-model').Node} child
|
|
4443
|
-
* @return {boolean}
|
|
4444
|
-
*/
|
|
4445
|
-
const sameTypeAndAttrs = (child) => child.type === targetChild.type && attrsEqual(child.attrs, targetChild.attrs);
|
|
4446
|
-
const oldText = targetChild.textContent;
|
|
4447
|
-
const byAll = findByPredicate((child) => sameTypeAndAttrs(child) && child.textContent === oldText);
|
|
4448
|
-
if (byAll !== null) return byAll;
|
|
4449
|
-
const byText = findByPredicate((child) => child.type === targetChild.type && child.textContent === oldText);
|
|
4450
|
-
if (byText !== null) return byText;
|
|
4451
|
-
if (hasDistinctiveAttrs(targetChild)) {
|
|
4452
|
-
const byAttrs = findByPredicate(sameTypeAndAttrs, true);
|
|
4453
|
-
if (byAttrs !== null) return byAttrs;
|
|
4454
|
-
}
|
|
4455
|
-
return findByPredicate((child) => sameTypeAndAttrs(child) && oldText !== "" && child.textContent !== "" && (oldText.startsWith(child.textContent) || child.textContent.startsWith(oldText)), true);
|
|
4456
|
-
};
|
|
4457
|
-
/**
|
|
4458
|
-
* Detect stale relative positions after structural changes that resolve to the
|
|
4459
|
-
* wrong text block or to the start of the correct block.
|
|
4460
|
-
*
|
|
4461
|
-
* @param {import('prosemirror-model').Node} oldDoc
|
|
4462
|
-
* @param {import('prosemirror-model').Node} newDoc
|
|
4463
|
-
* @param {number} oldAbs
|
|
4464
|
-
* @param {number|null} resolvedAbs
|
|
4465
|
-
* @return {boolean}
|
|
4466
|
-
*/
|
|
4467
|
-
const isMisresolvedAfterStructuralChange = (oldDoc, newDoc, oldAbs, resolvedAbs) => {
|
|
4468
|
-
if (resolvedAbs === null) return false;
|
|
4469
|
-
const $old = oldDoc.resolve(oldAbs);
|
|
4470
|
-
const $new = newDoc.resolve(resolvedAbs);
|
|
4471
|
-
if (!$old.parent.isTextblock) return false;
|
|
4472
|
-
if (!$new.parent.isTextblock) return true;
|
|
4473
|
-
if ($old.parent.textContent !== $new.parent.textContent) return true;
|
|
4474
|
-
if ($old.parentOffset !== 0 && $new.parentOffset === 0) return true;
|
|
4475
|
-
const bothAtStart = $old.parentOffset === 0 && $new.parentOffset === 0;
|
|
4476
|
-
const suspicious = $old.parentOffset !== $new.parentOffset || $old.parent.type !== $new.parent.type || !attrsEqual($old.parent.attrs, $new.parent.attrs);
|
|
4477
|
-
if (bothAtStart || suspicious) {
|
|
4478
|
-
const expected = findAbsolutePositionAfterStructuralChange(oldDoc, newDoc, oldAbs);
|
|
4479
|
-
return expected !== null && expected !== resolvedAbs;
|
|
4480
|
-
}
|
|
4481
|
-
return false;
|
|
4482
|
-
};
|
|
4483
|
-
/**
|
|
4484
|
-
* Utility method to convert a Prosemirror Doc Node into a Y.Doc.
|
|
4485
|
-
*
|
|
4486
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
4487
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
4488
|
-
* collaboration has begun as all history will be lost
|
|
4489
|
-
*
|
|
4490
|
-
* @param {Node} doc
|
|
4491
|
-
* @param {string} xmlFragment
|
|
4492
|
-
* @return {Y.Doc}
|
|
4493
|
-
*/
|
|
4494
|
-
function prosemirrorToYDoc(doc, xmlFragment = "prosemirror") {
|
|
4495
|
-
const ydoc = new Y.Doc();
|
|
4496
|
-
const type = ydoc.get(xmlFragment, Y.XmlFragment);
|
|
4497
|
-
if (!type.doc) return ydoc;
|
|
4498
|
-
prosemirrorToYXmlFragment(doc, type);
|
|
4499
|
-
return type.doc;
|
|
4500
|
-
}
|
|
4501
|
-
/**
|
|
4502
|
-
* Utility method to update an empty Y.XmlFragment with content from a Prosemirror Doc Node.
|
|
4503
|
-
*
|
|
4504
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
4505
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
4506
|
-
* collaboration has begun as all history will be lost
|
|
4507
|
-
*
|
|
4508
|
-
* Note: The Y.XmlFragment does not need to be part of a Y.Doc document at the time that this
|
|
4509
|
-
* method is called, but it must be added before any other operations are performed on it.
|
|
4510
|
-
*
|
|
4511
|
-
* @param {Node} doc prosemirror document.
|
|
4512
|
-
* @param {Y.XmlFragment} [xmlFragment] If supplied, an xml fragment to be
|
|
4513
|
-
* populated from the prosemirror state; otherwise a new XmlFragment will be created.
|
|
4514
|
-
* @return {Y.XmlFragment}
|
|
4515
|
-
*/
|
|
4516
|
-
function prosemirrorToYXmlFragment(doc, xmlFragment) {
|
|
4517
|
-
const type = xmlFragment || new Y.XmlFragment();
|
|
4518
|
-
updateYFragment(type.doc ? type.doc : { transact: (transaction) => transaction(void 0) }, type, doc, {
|
|
4519
|
-
mapping: /* @__PURE__ */ new Map(),
|
|
4520
|
-
isOMark: /* @__PURE__ */ new Map()
|
|
4521
|
-
});
|
|
4522
|
-
return type;
|
|
4523
|
-
}
|
|
4524
|
-
/**
|
|
4525
|
-
* Utility method to convert Prosemirror compatible JSON into a Y.Doc.
|
|
4526
|
-
*
|
|
4527
|
-
* This can be used when importing existing content to Y.Doc for the first time,
|
|
4528
|
-
* note that this should not be used to rehydrate a Y.Doc from a database once
|
|
4529
|
-
* collaboration has begun as all history will be lost
|
|
4530
|
-
*
|
|
4531
|
-
* @param {Schema} schema
|
|
4532
|
-
* @param {any} state
|
|
4533
|
-
* @param {string} xmlFragment
|
|
4534
|
-
* @return {Y.Doc}
|
|
4535
|
-
*/
|
|
4536
|
-
function prosemirrorJSONToYDoc(schema, state, xmlFragment = "prosemirror") {
|
|
4537
|
-
return prosemirrorToYDoc(Node$1.fromJSON(schema, state), xmlFragment);
|
|
4538
|
-
}
|
|
4539
|
-
/**
|
|
4540
|
-
*
|
|
4541
|
-
* @deprecated Use `yXmlFragmentToProseMirrorRootNode` instead
|
|
4542
|
-
*
|
|
4543
|
-
* Utility method to convert a Y.Doc to Prosemirror compatible JSON.
|
|
4544
|
-
*
|
|
4545
|
-
* @param {Y.Doc} ydoc
|
|
4546
|
-
* @param {string} xmlFragment
|
|
4547
|
-
* @return {Record<string, any>}
|
|
4548
|
-
*/
|
|
4549
|
-
function yDocToProsemirrorJSON(ydoc, xmlFragment = "prosemirror") {
|
|
4550
|
-
return yXmlFragmentToProsemirrorJSON(ydoc.getXmlFragment(xmlFragment));
|
|
4551
|
-
}
|
|
4552
|
-
/**
|
|
4553
|
-
* @deprecated Use `yXmlFragmentToProseMirrorRootNode` instead
|
|
4554
|
-
*
|
|
4555
|
-
* Utility method to convert a Y.Doc to Prosemirror compatible JSON.
|
|
4556
|
-
*
|
|
4557
|
-
* @param {Y.XmlFragment} xmlFragment The fragment, which must be part of a Y.Doc.
|
|
4558
|
-
* @return {Record<string, any>}
|
|
4559
|
-
*/
|
|
4560
|
-
function yXmlFragmentToProsemirrorJSON(xmlFragment) {
|
|
4561
|
-
const items = xmlFragment.toArray();
|
|
4562
|
-
/**
|
|
4563
|
-
* @param {Y.AbstractType} item
|
|
4564
|
-
*/
|
|
4565
|
-
const serialize = (item) => {
|
|
4566
|
-
/**
|
|
4567
|
-
* @type {Object} NodeObject
|
|
4568
|
-
* @property {string} NodeObject.type
|
|
4569
|
-
* @property {Record<string, string>=} NodeObject.attrs
|
|
4570
|
-
* @property {Array<NodeObject>=} NodeObject.content
|
|
4571
|
-
*/
|
|
4572
|
-
let response;
|
|
4573
|
-
if (item instanceof Y.XmlText) response = item.toDelta().map(
|
|
4574
|
-
/** @param {any} d */
|
|
4575
|
-
(d) => {
|
|
4576
|
-
const text = {
|
|
4577
|
-
type: "text",
|
|
4578
|
-
text: d.insert
|
|
4579
|
-
};
|
|
4580
|
-
if (d.attributes) text.marks = Object.keys(d.attributes).map((type_) => {
|
|
4581
|
-
const attrs = d.attributes[type_];
|
|
4582
|
-
const mark = { type: yattr2markname(type_) };
|
|
4583
|
-
if (Object.keys(attrs)) mark.attrs = attrs;
|
|
4584
|
-
return mark;
|
|
4585
|
-
});
|
|
4586
|
-
return text;
|
|
4587
|
-
}
|
|
4588
|
-
);
|
|
4589
|
-
else if (item instanceof Y.XmlElement) {
|
|
4590
|
-
response = { type: item.nodeName };
|
|
4591
|
-
const attrs = item.getAttributes();
|
|
4592
|
-
if (Object.keys(attrs).length) response.attrs = attrs;
|
|
4593
|
-
const children = item.toArray();
|
|
4594
|
-
if (children.length) response.content = children.map(serialize).flat();
|
|
4595
|
-
} else error.unexpectedCase();
|
|
4596
|
-
return response;
|
|
4597
|
-
};
|
|
4598
|
-
return {
|
|
4599
|
-
type: "doc",
|
|
4600
|
-
content: items.map(serialize)
|
|
4601
|
-
};
|
|
4602
|
-
}
|
|
4603
|
-
const undo = (state) => {
|
|
4604
|
-
const undoManager = yUndoPluginKey.getState(state).undoManager;
|
|
4605
|
-
if (undoManager != null) {
|
|
4606
|
-
undoManager.undo();
|
|
4607
|
-
return true;
|
|
4608
|
-
}
|
|
4609
|
-
};
|
|
4610
|
-
const redo = (state) => {
|
|
4611
|
-
const undoManager = yUndoPluginKey.getState(state).undoManager;
|
|
4612
|
-
if (undoManager != null) {
|
|
4613
|
-
undoManager.redo();
|
|
4614
|
-
return true;
|
|
4615
|
-
}
|
|
4616
|
-
};
|
|
4617
|
-
const defaultProtectedNodes = new Set(["paragraph"]);
|
|
4618
|
-
const defaultDeleteFilter = (item, protectedNodes) => !(item instanceof Item) || !(item.content instanceof ContentType) || !(item.content.type instanceof Text || item.content.type instanceof XmlElement && protectedNodes.has(item.content.type.nodeName)) || item.content.type._length === 0;
|
|
4619
|
-
const yUndoPlugin = ({ protectedNodes = defaultProtectedNodes, trackedOrigins = [], undoManager = null } = {}) => new Plugin({
|
|
4620
|
-
key: yUndoPluginKey,
|
|
4621
|
-
state: {
|
|
4622
|
-
init: (initargs, state) => {
|
|
4623
|
-
const ystate = ySyncPluginKey.getState(state);
|
|
4624
|
-
let _undoManager = undoManager;
|
|
4625
|
-
if (!_undoManager) {
|
|
4626
|
-
const doc = ystate.doc;
|
|
4627
|
-
const destroyListenersBefore = new Set(doc ? doc._observers.get("destroy") : []);
|
|
4628
|
-
_undoManager = new UndoManager(ystate.type, {
|
|
4629
|
-
trackedOrigins: new Set([ySyncPluginKey].concat(trackedOrigins)),
|
|
4630
|
-
deleteFilter: (item) => defaultDeleteFilter(item, protectedNodes),
|
|
4631
|
-
captureTransaction: (tr) => tr.meta.get("addToHistory") !== false
|
|
4632
|
-
});
|
|
4633
|
-
const destroyListenersAfter = doc ? doc._observers.get("destroy") : /* @__PURE__ */ new Set();
|
|
4634
|
-
_undoManager._yTiptapDocDestroyListeners = Array.from(destroyListenersAfter || []).filter((listener) => !destroyListenersBefore.has(listener));
|
|
4635
|
-
}
|
|
4636
|
-
return {
|
|
4637
|
-
undoManager: _undoManager,
|
|
4638
|
-
prevSel: null,
|
|
4639
|
-
hasUndoOps: _undoManager.undoStack.length > 0,
|
|
4640
|
-
hasRedoOps: _undoManager.redoStack.length > 0
|
|
4641
|
-
};
|
|
4642
|
-
},
|
|
4643
|
-
/**
|
|
4644
|
-
* @returns {any}
|
|
4645
|
-
*/
|
|
4646
|
-
apply: (tr, val, oldState, state) => {
|
|
4647
|
-
const binding = ySyncPluginKey.getState(state).binding;
|
|
4648
|
-
const undoManager = val.undoManager;
|
|
4649
|
-
const hasUndoOps = undoManager.undoStack.length > 0;
|
|
4650
|
-
const hasRedoOps = undoManager.redoStack.length > 0;
|
|
4651
|
-
if (binding) return {
|
|
4652
|
-
undoManager,
|
|
4653
|
-
prevSel: getRelativeSelection(binding, oldState),
|
|
4654
|
-
hasUndoOps,
|
|
4655
|
-
hasRedoOps
|
|
4656
|
-
};
|
|
4657
|
-
else if (hasUndoOps !== val.hasUndoOps || hasRedoOps !== val.hasRedoOps) return Object.assign({}, val, {
|
|
4658
|
-
hasUndoOps: undoManager.undoStack.length > 0,
|
|
4659
|
-
hasRedoOps: undoManager.redoStack.length > 0
|
|
4660
|
-
});
|
|
4661
|
-
else return val;
|
|
4662
|
-
}
|
|
4663
|
-
},
|
|
4664
|
-
view: (view) => {
|
|
4665
|
-
const ystate = ySyncPluginKey.getState(view.state);
|
|
4666
|
-
const undoManager = yUndoPluginKey.getState(view.state).undoManager;
|
|
4667
|
-
undoManager.on("stack-item-added", ({ stackItem }) => {
|
|
4668
|
-
const binding = ystate.binding;
|
|
4669
|
-
if (binding) stackItem.meta.set(binding, yUndoPluginKey.getState(view.state).prevSel);
|
|
4670
|
-
});
|
|
4671
|
-
undoManager.on("stack-item-popped", ({ stackItem }) => {
|
|
4672
|
-
const binding = ystate.binding;
|
|
4673
|
-
if (binding) binding.beforeTransactionSelection = stackItem.meta.get(binding) || binding.beforeTransactionSelection;
|
|
4674
|
-
});
|
|
4675
|
-
return { destroy: () => {
|
|
4676
|
-
undoManager.destroy();
|
|
4677
|
-
const leakedDestroyListeners = undoManager._yTiptapDocDestroyListeners;
|
|
4678
|
-
if (leakedDestroyListeners && undoManager.doc) {
|
|
4679
|
-
leakedDestroyListeners.forEach((listener) => undoManager.doc.off("destroy", listener));
|
|
4680
|
-
undoManager._yTiptapDocDestroyListeners = null;
|
|
4681
|
-
}
|
|
4682
|
-
} };
|
|
4683
|
-
}
|
|
4684
|
-
});
|
|
4685
|
-
//#endregion
|
|
4686
|
-
//#region ../internal/src/object.ts
|
|
4687
|
-
function isPlainObject(value) {
|
|
4688
|
-
if (value === null || Object(value) !== value || Array.isArray(value)) return false;
|
|
4689
|
-
try {
|
|
4690
|
-
Function.prototype.toString.call(value);
|
|
4691
|
-
return false;
|
|
4692
|
-
} catch {
|
|
4693
|
-
return true;
|
|
4694
|
-
}
|
|
4695
|
-
}
|
|
4696
|
-
//#endregion
|
|
4697
|
-
//#region src/presence.ts
|
|
4698
|
-
const personColors = [
|
|
4699
|
-
"#E11D48",
|
|
4700
|
-
"#D97706",
|
|
4701
|
-
"#059669",
|
|
4702
|
-
"#0891B2",
|
|
4703
|
-
"#2563EB",
|
|
4704
|
-
"#7C3AED",
|
|
4705
|
-
"#C026D3"
|
|
4706
|
-
];
|
|
4707
|
-
function isString(value) {
|
|
4708
|
-
try {
|
|
4709
|
-
return String(value) === value;
|
|
4710
|
-
} catch {
|
|
4711
|
-
return false;
|
|
4712
|
-
}
|
|
4713
|
-
}
|
|
4714
|
-
function createRealtimeIdentity(user) {
|
|
4715
|
-
if (!isString(user.id) || !user.id || user.id.length > 256) throw new TypeError("Realtime identity requires a valid user id.");
|
|
4716
|
-
let hash = 0;
|
|
4717
|
-
for (let index = 0; index < user.id.length; index++) hash = Math.imul(31, hash) + user.id.charCodeAt(index) | 0;
|
|
4718
|
-
const name = (isString(user.name) && user.name || isString(user.email) && user.email || "Anonymous").slice(0, 256);
|
|
4719
|
-
const identity = {
|
|
4720
|
-
color: personColors[Math.abs(hash) % personColors.length],
|
|
4721
|
-
id: user.id,
|
|
4722
|
-
name
|
|
4723
|
-
};
|
|
4724
|
-
if (isString(user.image) && user.image && user.image.length <= 2048) identity.image = user.image;
|
|
4725
|
-
return identity;
|
|
4726
|
-
}
|
|
4727
|
-
function isRealtimeIdentity(value) {
|
|
4728
|
-
if (!isPlainObject(value)) return false;
|
|
4729
|
-
return isString(value.id) && value.id.length > 0 && value.id.length <= 256 && isString(value.name) && value.name.length > 0 && value.name.length <= 256 && isString(value.color) && /^#[\da-f]{6}$/i.test(value.color) && (value.image === void 0 || isString(value.image) && value.image.length <= 2048);
|
|
4730
|
-
}
|
|
4731
|
-
function getRealtimePeople(states) {
|
|
4732
|
-
const people = [];
|
|
4733
|
-
for (const [clientId, state] of states) if (isRealtimeIdentity(state.user)) {
|
|
4734
|
-
const { color, id, image, name } = state.user;
|
|
4735
|
-
const person = {
|
|
4736
|
-
color,
|
|
4737
|
-
id,
|
|
4738
|
-
name,
|
|
4739
|
-
clientId
|
|
4740
|
-
};
|
|
4741
|
-
if (image !== void 0) person.image = image;
|
|
4742
|
-
people.push(person);
|
|
4743
|
-
}
|
|
4744
|
-
return people;
|
|
4745
|
-
}
|
|
4746
|
-
const workspaceRoomId = "@workspace";
|
|
4747
|
-
const maxAwarenessClients = 1024;
|
|
4748
|
-
const realtimeCheckpointRejectedCode = "REALTIME_CHECKPOINT_REJECTED";
|
|
4749
|
-
const realtimeSyncPendingCode = "REALTIME_SYNC_PENDING";
|
|
4750
|
-
function isRetryableRealtimeCheckpointCode(value) {
|
|
4751
|
-
return value === "REALTIME_SYNC_PENDING" || value === "REALTIME_CHECKPOINT_REJECTED";
|
|
4752
|
-
}
|
|
4753
|
-
function validPath(value) {
|
|
4754
|
-
if (typeof value !== "string" || value.length === 0 || value.length > 4096 || value.includes("\0")) return false;
|
|
4755
|
-
if (value.startsWith("/") || value.endsWith("/") || value.includes("\\")) return false;
|
|
4756
|
-
const parts = value.split("/");
|
|
4757
|
-
return !parts.some((part) => !part || part === "." || part === "..") && parts[0] !== ".git" && parts[0] !== ".vitehub";
|
|
4758
|
-
}
|
|
4759
|
-
function decodeWorkspaceChangePayload(decoder) {
|
|
4760
|
-
try {
|
|
4761
|
-
const value = JSON.parse(decoding.readVarString(decoder));
|
|
4762
|
-
if ((value.operation === "create" || value.operation === "delete" || value.operation === "update") && validPath(value.path)) return {
|
|
4763
|
-
operation: value.operation,
|
|
4764
|
-
path: value.path
|
|
4765
|
-
};
|
|
4766
|
-
if (value.operation === "move" && validPath(value.from) && validPath(value.to)) return {
|
|
4767
|
-
operation: value.operation,
|
|
4768
|
-
from: value.from,
|
|
4769
|
-
to: value.to
|
|
4770
|
-
};
|
|
4771
|
-
} catch {}
|
|
4772
|
-
}
|
|
4773
|
-
function encodeWorkspaceChange(change) {
|
|
4774
|
-
const encoder = encoding.createEncoder();
|
|
4775
|
-
encoding.writeVarUint(encoder, 4);
|
|
4776
|
-
encoding.writeVarString(encoder, JSON.stringify(change));
|
|
4777
|
-
return encoding.toUint8Array(encoder);
|
|
4778
|
-
}
|
|
4779
|
-
function readAwarenessClientIds(update) {
|
|
4780
|
-
const decoder = decoding.createDecoder(update);
|
|
4781
|
-
const length = decoding.readVarUint(decoder);
|
|
4782
|
-
if (length > 1024) throw new TypeError("Awareness update contains too many clients.");
|
|
4783
|
-
const clients = [];
|
|
4784
|
-
for (let index = 0; index < length; index++) {
|
|
4785
|
-
clients.push(decoding.readVarUint(decoder));
|
|
4786
|
-
decoding.readVarUint(decoder);
|
|
4787
|
-
decoding.readVarString(decoder);
|
|
4788
|
-
}
|
|
4789
|
-
return clients;
|
|
4790
|
-
}
|
|
4791
|
-
//#endregion
|
|
4792
|
-
//#region src/frontmatter.ts
|
|
4793
|
-
const frontmatterPattern = /^---\r?\n(?:---(?:\r?\n|$)|([\s\S]*?)\r?\n---(?:\r?\n|$))/;
|
|
4794
|
-
const Frontmatter = Node.create({
|
|
4795
|
-
name: "frontmatter",
|
|
4796
|
-
priority: 1e3,
|
|
4797
|
-
atom: true,
|
|
4798
|
-
selectable: false,
|
|
4799
|
-
addAttributes() {
|
|
4800
|
-
return { value: { default: null } };
|
|
4801
|
-
},
|
|
4802
|
-
parseHTML() {
|
|
4803
|
-
return [{
|
|
4804
|
-
tag: "pre[data-frontmatter]",
|
|
4805
|
-
getAttrs: (element) => ({ value: element.hasAttribute("data-frontmatter-empty") ? null : element.getAttribute("data-frontmatter-value") || "" })
|
|
4806
|
-
}];
|
|
4807
|
-
},
|
|
4808
|
-
renderHTML({ node }) {
|
|
4809
|
-
return [
|
|
4810
|
-
"pre",
|
|
4811
|
-
{
|
|
4812
|
-
"data-frontmatter": "",
|
|
4813
|
-
"data-frontmatter-empty": node.attrs.value === null ? "" : void 0,
|
|
4814
|
-
"data-frontmatter-value": node.attrs.value ?? void 0,
|
|
4815
|
-
contenteditable: "false"
|
|
4816
|
-
},
|
|
4817
|
-
node.attrs.value ?? ""
|
|
4818
|
-
];
|
|
4819
|
-
},
|
|
4820
|
-
markdownTokenName: "frontmatter",
|
|
4821
|
-
markdownTokenizer: {
|
|
4822
|
-
name: "frontmatter",
|
|
4823
|
-
level: "block",
|
|
4824
|
-
start: (src) => frontmatterPattern.test(src) ? 0 : -1,
|
|
4825
|
-
tokenize(src, tokens) {
|
|
4826
|
-
if (tokens.length) return;
|
|
4827
|
-
const match = frontmatterPattern.exec(src);
|
|
4828
|
-
if (!match) return;
|
|
4829
|
-
return {
|
|
4830
|
-
type: "frontmatter",
|
|
4831
|
-
raw: match[0],
|
|
4832
|
-
text: match[1]
|
|
4833
|
-
};
|
|
4834
|
-
}
|
|
4835
|
-
},
|
|
4836
|
-
parseMarkdown(token, helpers) {
|
|
4837
|
-
return helpers.createNode("frontmatter", { value: token.text ?? null });
|
|
4838
|
-
},
|
|
4839
|
-
renderMarkdown(node) {
|
|
4840
|
-
return node.attrs?.value === null ? "---\n---" : `---\n${node.attrs?.value || ""}\n---`;
|
|
4841
|
-
}
|
|
4842
|
-
});
|
|
4843
|
-
//#endregion
|
|
4844
|
-
//#region src/editor-extensions.ts
|
|
4845
|
-
const RealtimeDocument = Document.extend({ content: "frontmatter? block+" });
|
|
4846
|
-
function createRealtimeEditorExtensions() {
|
|
4847
|
-
return [
|
|
4848
|
-
StarterKit.configure({
|
|
4849
|
-
document: false,
|
|
4850
|
-
undoRedo: false
|
|
4851
|
-
}),
|
|
4852
|
-
RealtimeDocument,
|
|
4853
|
-
Image,
|
|
4854
|
-
TableKit,
|
|
4855
|
-
Frontmatter,
|
|
4856
|
-
Markdown.configure({ marked: new Marked() })
|
|
4857
|
-
];
|
|
4858
|
-
}
|
|
4859
|
-
//#endregion
|
|
4860
|
-
export { yUndoPluginKey as S, updateYFragment as _, maxAwarenessClients as a, ySyncPluginKey as b, realtimeSyncPendingCode as c, getRealtimePeople as d, absolutePositionToRelativePosition as f, undo as g, relativePositionToAbsolutePosition as h, isRetryableRealtimeCheckpointCode as i, workspaceRoomId as l, redo as m, decodeWorkspaceChangePayload as n, readAwarenessClientIds as o, prosemirrorJSONToYDoc as p, encodeWorkspaceChange as r, realtimeCheckpointRejectedCode as s, createRealtimeEditorExtensions as t, createRealtimeIdentity as u, yDocToProsemirrorJSON as v, yUndoPlugin as x, ySyncPlugin as y };
|