@tiptap/extension-code-block 2.0.0-beta.212 → 2.0.0-beta.214
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3637 -178
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +3634 -182
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +3652 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/{index.d.ts → packages/extension-code-block/src/code-block.d.ts} +43 -46
- package/dist/packages/extension-code-block/src/index.d.ts +3 -0
- package/package.json +8 -17
package/dist/index.cjs
CHANGED
|
@@ -1,189 +1,3648 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var core = require('@tiptap/core');
|
|
6
|
+
|
|
7
|
+
function findDiffStart(a, b, pos) {
|
|
8
|
+
for (let i = 0;; i++) {
|
|
9
|
+
if (i == a.childCount || i == b.childCount)
|
|
10
|
+
return a.childCount == b.childCount ? null : pos;
|
|
11
|
+
let childA = a.child(i), childB = b.child(i);
|
|
12
|
+
if (childA == childB) {
|
|
13
|
+
pos += childA.nodeSize;
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!childA.sameMarkup(childB))
|
|
17
|
+
return pos;
|
|
18
|
+
if (childA.isText && childA.text != childB.text) {
|
|
19
|
+
for (let j = 0; childA.text[j] == childB.text[j]; j++)
|
|
20
|
+
pos++;
|
|
21
|
+
return pos;
|
|
22
|
+
}
|
|
23
|
+
if (childA.content.size || childB.content.size) {
|
|
24
|
+
let inner = findDiffStart(childA.content, childB.content, pos + 1);
|
|
25
|
+
if (inner != null)
|
|
26
|
+
return inner;
|
|
27
|
+
}
|
|
28
|
+
pos += childA.nodeSize;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function findDiffEnd(a, b, posA, posB) {
|
|
32
|
+
for (let iA = a.childCount, iB = b.childCount;;) {
|
|
33
|
+
if (iA == 0 || iB == 0)
|
|
34
|
+
return iA == iB ? null : { a: posA, b: posB };
|
|
35
|
+
let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
|
|
36
|
+
if (childA == childB) {
|
|
37
|
+
posA -= size;
|
|
38
|
+
posB -= size;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (!childA.sameMarkup(childB))
|
|
42
|
+
return { a: posA, b: posB };
|
|
43
|
+
if (childA.isText && childA.text != childB.text) {
|
|
44
|
+
let same = 0, minSize = Math.min(childA.text.length, childB.text.length);
|
|
45
|
+
while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
|
|
46
|
+
same++;
|
|
47
|
+
posA--;
|
|
48
|
+
posB--;
|
|
49
|
+
}
|
|
50
|
+
return { a: posA, b: posB };
|
|
51
|
+
}
|
|
52
|
+
if (childA.content.size || childB.content.size) {
|
|
53
|
+
let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
|
|
54
|
+
if (inner)
|
|
55
|
+
return inner;
|
|
56
|
+
}
|
|
57
|
+
posA -= size;
|
|
58
|
+
posB -= size;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
A fragment represents a node's collection of child nodes.
|
|
64
|
+
|
|
65
|
+
Like nodes, fragments are persistent data structures, and you
|
|
66
|
+
should not mutate them or their content. Rather, you create new
|
|
67
|
+
instances whenever needed. The API tries to make this easy.
|
|
68
|
+
*/
|
|
69
|
+
class Fragment {
|
|
70
|
+
/**
|
|
71
|
+
@internal
|
|
72
|
+
*/
|
|
73
|
+
constructor(
|
|
74
|
+
/**
|
|
75
|
+
@internal
|
|
76
|
+
*/
|
|
77
|
+
content, size) {
|
|
78
|
+
this.content = content;
|
|
79
|
+
this.size = size || 0;
|
|
80
|
+
if (size == null)
|
|
81
|
+
for (let i = 0; i < content.length; i++)
|
|
82
|
+
this.size += content[i].nodeSize;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
Invoke a callback for all descendant nodes between the given two
|
|
86
|
+
positions (relative to start of this fragment). Doesn't descend
|
|
87
|
+
into a node when the callback returns `false`.
|
|
88
|
+
*/
|
|
89
|
+
nodesBetween(from, to, f, nodeStart = 0, parent) {
|
|
90
|
+
for (let i = 0, pos = 0; pos < to; i++) {
|
|
91
|
+
let child = this.content[i], end = pos + child.nodeSize;
|
|
92
|
+
if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
|
|
93
|
+
let start = pos + 1;
|
|
94
|
+
child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
|
|
95
|
+
}
|
|
96
|
+
pos = end;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
Call the given callback for every descendant node. `pos` will be
|
|
101
|
+
relative to the start of the fragment. The callback may return
|
|
102
|
+
`false` to prevent traversal of a given node's children.
|
|
103
|
+
*/
|
|
104
|
+
descendants(f) {
|
|
105
|
+
this.nodesBetween(0, this.size, f);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
Extract the text between `from` and `to`. See the same method on
|
|
109
|
+
[`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
|
|
110
|
+
*/
|
|
111
|
+
textBetween(from, to, blockSeparator, leafText) {
|
|
112
|
+
let text = "", separated = true;
|
|
113
|
+
this.nodesBetween(from, to, (node, pos) => {
|
|
114
|
+
if (node.isText) {
|
|
115
|
+
text += node.text.slice(Math.max(from, pos) - pos, to - pos);
|
|
116
|
+
separated = !blockSeparator;
|
|
117
|
+
}
|
|
118
|
+
else if (node.isLeaf) {
|
|
119
|
+
if (leafText) {
|
|
120
|
+
text += typeof leafText === "function" ? leafText(node) : leafText;
|
|
121
|
+
}
|
|
122
|
+
else if (node.type.spec.leafText) {
|
|
123
|
+
text += node.type.spec.leafText(node);
|
|
124
|
+
}
|
|
125
|
+
separated = !blockSeparator;
|
|
126
|
+
}
|
|
127
|
+
else if (!separated && node.isBlock) {
|
|
128
|
+
text += blockSeparator;
|
|
129
|
+
separated = true;
|
|
130
|
+
}
|
|
131
|
+
}, 0);
|
|
132
|
+
return text;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
Create a new fragment containing the combined content of this
|
|
136
|
+
fragment and the other.
|
|
137
|
+
*/
|
|
138
|
+
append(other) {
|
|
139
|
+
if (!other.size)
|
|
140
|
+
return this;
|
|
141
|
+
if (!this.size)
|
|
142
|
+
return other;
|
|
143
|
+
let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
|
|
144
|
+
if (last.isText && last.sameMarkup(first)) {
|
|
145
|
+
content[content.length - 1] = last.withText(last.text + first.text);
|
|
146
|
+
i = 1;
|
|
147
|
+
}
|
|
148
|
+
for (; i < other.content.length; i++)
|
|
149
|
+
content.push(other.content[i]);
|
|
150
|
+
return new Fragment(content, this.size + other.size);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
Cut out the sub-fragment between the two given positions.
|
|
154
|
+
*/
|
|
155
|
+
cut(from, to = this.size) {
|
|
156
|
+
if (from == 0 && to == this.size)
|
|
157
|
+
return this;
|
|
158
|
+
let result = [], size = 0;
|
|
159
|
+
if (to > from)
|
|
160
|
+
for (let i = 0, pos = 0; pos < to; i++) {
|
|
161
|
+
let child = this.content[i], end = pos + child.nodeSize;
|
|
162
|
+
if (end > from) {
|
|
163
|
+
if (pos < from || end > to) {
|
|
164
|
+
if (child.isText)
|
|
165
|
+
child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
|
|
166
|
+
else
|
|
167
|
+
child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
|
|
168
|
+
}
|
|
169
|
+
result.push(child);
|
|
170
|
+
size += child.nodeSize;
|
|
171
|
+
}
|
|
172
|
+
pos = end;
|
|
173
|
+
}
|
|
174
|
+
return new Fragment(result, size);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
@internal
|
|
178
|
+
*/
|
|
179
|
+
cutByIndex(from, to) {
|
|
180
|
+
if (from == to)
|
|
181
|
+
return Fragment.empty;
|
|
182
|
+
if (from == 0 && to == this.content.length)
|
|
183
|
+
return this;
|
|
184
|
+
return new Fragment(this.content.slice(from, to));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
Create a new fragment in which the node at the given index is
|
|
188
|
+
replaced by the given node.
|
|
189
|
+
*/
|
|
190
|
+
replaceChild(index, node) {
|
|
191
|
+
let current = this.content[index];
|
|
192
|
+
if (current == node)
|
|
193
|
+
return this;
|
|
194
|
+
let copy = this.content.slice();
|
|
195
|
+
let size = this.size + node.nodeSize - current.nodeSize;
|
|
196
|
+
copy[index] = node;
|
|
197
|
+
return new Fragment(copy, size);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
Create a new fragment by prepending the given node to this
|
|
201
|
+
fragment.
|
|
202
|
+
*/
|
|
203
|
+
addToStart(node) {
|
|
204
|
+
return new Fragment([node].concat(this.content), this.size + node.nodeSize);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
Create a new fragment by appending the given node to this
|
|
208
|
+
fragment.
|
|
209
|
+
*/
|
|
210
|
+
addToEnd(node) {
|
|
211
|
+
return new Fragment(this.content.concat(node), this.size + node.nodeSize);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
Compare this fragment to another one.
|
|
215
|
+
*/
|
|
216
|
+
eq(other) {
|
|
217
|
+
if (this.content.length != other.content.length)
|
|
218
|
+
return false;
|
|
219
|
+
for (let i = 0; i < this.content.length; i++)
|
|
220
|
+
if (!this.content[i].eq(other.content[i]))
|
|
221
|
+
return false;
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
The first child of the fragment, or `null` if it is empty.
|
|
226
|
+
*/
|
|
227
|
+
get firstChild() { return this.content.length ? this.content[0] : null; }
|
|
228
|
+
/**
|
|
229
|
+
The last child of the fragment, or `null` if it is empty.
|
|
230
|
+
*/
|
|
231
|
+
get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; }
|
|
232
|
+
/**
|
|
233
|
+
The number of child nodes in this fragment.
|
|
234
|
+
*/
|
|
235
|
+
get childCount() { return this.content.length; }
|
|
236
|
+
/**
|
|
237
|
+
Get the child node at the given index. Raise an error when the
|
|
238
|
+
index is out of range.
|
|
239
|
+
*/
|
|
240
|
+
child(index) {
|
|
241
|
+
let found = this.content[index];
|
|
242
|
+
if (!found)
|
|
243
|
+
throw new RangeError("Index " + index + " out of range for " + this);
|
|
244
|
+
return found;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
Get the child node at the given index, if it exists.
|
|
248
|
+
*/
|
|
249
|
+
maybeChild(index) {
|
|
250
|
+
return this.content[index] || null;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
Call `f` for every child node, passing the node, its offset
|
|
254
|
+
into this parent node, and its index.
|
|
255
|
+
*/
|
|
256
|
+
forEach(f) {
|
|
257
|
+
for (let i = 0, p = 0; i < this.content.length; i++) {
|
|
258
|
+
let child = this.content[i];
|
|
259
|
+
f(child, p, i);
|
|
260
|
+
p += child.nodeSize;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
Find the first position at which this fragment and another
|
|
265
|
+
fragment differ, or `null` if they are the same.
|
|
266
|
+
*/
|
|
267
|
+
findDiffStart(other, pos = 0) {
|
|
268
|
+
return findDiffStart(this, other, pos);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
Find the first position, searching from the end, at which this
|
|
272
|
+
fragment and the given fragment differ, or `null` if they are
|
|
273
|
+
the same. Since this position will not be the same in both
|
|
274
|
+
nodes, an object with two separate positions is returned.
|
|
275
|
+
*/
|
|
276
|
+
findDiffEnd(other, pos = this.size, otherPos = other.size) {
|
|
277
|
+
return findDiffEnd(this, other, pos, otherPos);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
Find the index and inner offset corresponding to a given relative
|
|
281
|
+
position in this fragment. The result object will be reused
|
|
282
|
+
(overwritten) the next time the function is called. (Not public.)
|
|
283
|
+
*/
|
|
284
|
+
findIndex(pos, round = -1) {
|
|
285
|
+
if (pos == 0)
|
|
286
|
+
return retIndex(0, pos);
|
|
287
|
+
if (pos == this.size)
|
|
288
|
+
return retIndex(this.content.length, pos);
|
|
289
|
+
if (pos > this.size || pos < 0)
|
|
290
|
+
throw new RangeError(`Position ${pos} outside of fragment (${this})`);
|
|
291
|
+
for (let i = 0, curPos = 0;; i++) {
|
|
292
|
+
let cur = this.child(i), end = curPos + cur.nodeSize;
|
|
293
|
+
if (end >= pos) {
|
|
294
|
+
if (end == pos || round > 0)
|
|
295
|
+
return retIndex(i + 1, end);
|
|
296
|
+
return retIndex(i, curPos);
|
|
297
|
+
}
|
|
298
|
+
curPos = end;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
Return a debugging string that describes this fragment.
|
|
303
|
+
*/
|
|
304
|
+
toString() { return "<" + this.toStringInner() + ">"; }
|
|
305
|
+
/**
|
|
306
|
+
@internal
|
|
307
|
+
*/
|
|
308
|
+
toStringInner() { return this.content.join(", "); }
|
|
309
|
+
/**
|
|
310
|
+
Create a JSON-serializeable representation of this fragment.
|
|
311
|
+
*/
|
|
312
|
+
toJSON() {
|
|
313
|
+
return this.content.length ? this.content.map(n => n.toJSON()) : null;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
Deserialize a fragment from its JSON representation.
|
|
317
|
+
*/
|
|
318
|
+
static fromJSON(schema, value) {
|
|
319
|
+
if (!value)
|
|
320
|
+
return Fragment.empty;
|
|
321
|
+
if (!Array.isArray(value))
|
|
322
|
+
throw new RangeError("Invalid input for Fragment.fromJSON");
|
|
323
|
+
return new Fragment(value.map(schema.nodeFromJSON));
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
Build a fragment from an array of nodes. Ensures that adjacent
|
|
327
|
+
text nodes with the same marks are joined together.
|
|
328
|
+
*/
|
|
329
|
+
static fromArray(array) {
|
|
330
|
+
if (!array.length)
|
|
331
|
+
return Fragment.empty;
|
|
332
|
+
let joined, size = 0;
|
|
333
|
+
for (let i = 0; i < array.length; i++) {
|
|
334
|
+
let node = array[i];
|
|
335
|
+
size += node.nodeSize;
|
|
336
|
+
if (i && node.isText && array[i - 1].sameMarkup(node)) {
|
|
337
|
+
if (!joined)
|
|
338
|
+
joined = array.slice(0, i);
|
|
339
|
+
joined[joined.length - 1] = node
|
|
340
|
+
.withText(joined[joined.length - 1].text + node.text);
|
|
341
|
+
}
|
|
342
|
+
else if (joined) {
|
|
343
|
+
joined.push(node);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return new Fragment(joined || array, size);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
Create a fragment from something that can be interpreted as a
|
|
350
|
+
set of nodes. For `null`, it returns the empty fragment. For a
|
|
351
|
+
fragment, the fragment itself. For a node or array of nodes, a
|
|
352
|
+
fragment containing those nodes.
|
|
353
|
+
*/
|
|
354
|
+
static from(nodes) {
|
|
355
|
+
if (!nodes)
|
|
356
|
+
return Fragment.empty;
|
|
357
|
+
if (nodes instanceof Fragment)
|
|
358
|
+
return nodes;
|
|
359
|
+
if (Array.isArray(nodes))
|
|
360
|
+
return this.fromArray(nodes);
|
|
361
|
+
if (nodes.attrs)
|
|
362
|
+
return new Fragment([nodes], nodes.nodeSize);
|
|
363
|
+
throw new RangeError("Can not convert " + nodes + " to a Fragment" +
|
|
364
|
+
(nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
An empty fragment. Intended to be reused whenever a node doesn't
|
|
369
|
+
contain anything (rather than allocating a new empty fragment for
|
|
370
|
+
each leaf node).
|
|
371
|
+
*/
|
|
372
|
+
Fragment.empty = new Fragment([], 0);
|
|
373
|
+
const found = { index: 0, offset: 0 };
|
|
374
|
+
function retIndex(index, offset) {
|
|
375
|
+
found.index = index;
|
|
376
|
+
found.offset = offset;
|
|
377
|
+
return found;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function compareDeep(a, b) {
|
|
381
|
+
if (a === b)
|
|
382
|
+
return true;
|
|
383
|
+
if (!(a && typeof a == "object") ||
|
|
384
|
+
!(b && typeof b == "object"))
|
|
385
|
+
return false;
|
|
386
|
+
let array = Array.isArray(a);
|
|
387
|
+
if (Array.isArray(b) != array)
|
|
388
|
+
return false;
|
|
389
|
+
if (array) {
|
|
390
|
+
if (a.length != b.length)
|
|
391
|
+
return false;
|
|
392
|
+
for (let i = 0; i < a.length; i++)
|
|
393
|
+
if (!compareDeep(a[i], b[i]))
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
for (let p in a)
|
|
398
|
+
if (!(p in b) || !compareDeep(a[p], b[p]))
|
|
399
|
+
return false;
|
|
400
|
+
for (let p in b)
|
|
401
|
+
if (!(p in a))
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
A mark is a piece of information that can be attached to a node,
|
|
409
|
+
such as it being emphasized, in code font, or a link. It has a
|
|
410
|
+
type and optionally a set of attributes that provide further
|
|
411
|
+
information (such as the target of the link). Marks are created
|
|
412
|
+
through a `Schema`, which controls which types exist and which
|
|
413
|
+
attributes they have.
|
|
414
|
+
*/
|
|
415
|
+
class Mark {
|
|
416
|
+
/**
|
|
417
|
+
@internal
|
|
418
|
+
*/
|
|
419
|
+
constructor(
|
|
420
|
+
/**
|
|
421
|
+
The type of this mark.
|
|
422
|
+
*/
|
|
423
|
+
type,
|
|
424
|
+
/**
|
|
425
|
+
The attributes associated with this mark.
|
|
426
|
+
*/
|
|
427
|
+
attrs) {
|
|
428
|
+
this.type = type;
|
|
429
|
+
this.attrs = attrs;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
Given a set of marks, create a new set which contains this one as
|
|
433
|
+
well, in the right position. If this mark is already in the set,
|
|
434
|
+
the set itself is returned. If any marks that are set to be
|
|
435
|
+
[exclusive](https://prosemirror.net/docs/ref/#model.MarkSpec.excludes) with this mark are present,
|
|
436
|
+
those are replaced by this one.
|
|
437
|
+
*/
|
|
438
|
+
addToSet(set) {
|
|
439
|
+
let copy, placed = false;
|
|
440
|
+
for (let i = 0; i < set.length; i++) {
|
|
441
|
+
let other = set[i];
|
|
442
|
+
if (this.eq(other))
|
|
443
|
+
return set;
|
|
444
|
+
if (this.type.excludes(other.type)) {
|
|
445
|
+
if (!copy)
|
|
446
|
+
copy = set.slice(0, i);
|
|
447
|
+
}
|
|
448
|
+
else if (other.type.excludes(this.type)) {
|
|
449
|
+
return set;
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
if (!placed && other.type.rank > this.type.rank) {
|
|
453
|
+
if (!copy)
|
|
454
|
+
copy = set.slice(0, i);
|
|
455
|
+
copy.push(this);
|
|
456
|
+
placed = true;
|
|
457
|
+
}
|
|
458
|
+
if (copy)
|
|
459
|
+
copy.push(other);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (!copy)
|
|
463
|
+
copy = set.slice();
|
|
464
|
+
if (!placed)
|
|
465
|
+
copy.push(this);
|
|
466
|
+
return copy;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
Remove this mark from the given set, returning a new set. If this
|
|
470
|
+
mark is not in the set, the set itself is returned.
|
|
471
|
+
*/
|
|
472
|
+
removeFromSet(set) {
|
|
473
|
+
for (let i = 0; i < set.length; i++)
|
|
474
|
+
if (this.eq(set[i]))
|
|
475
|
+
return set.slice(0, i).concat(set.slice(i + 1));
|
|
476
|
+
return set;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
Test whether this mark is in the given set of marks.
|
|
480
|
+
*/
|
|
481
|
+
isInSet(set) {
|
|
482
|
+
for (let i = 0; i < set.length; i++)
|
|
483
|
+
if (this.eq(set[i]))
|
|
484
|
+
return true;
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
Test whether this mark has the same type and attributes as
|
|
489
|
+
another mark.
|
|
490
|
+
*/
|
|
491
|
+
eq(other) {
|
|
492
|
+
return this == other ||
|
|
493
|
+
(this.type == other.type && compareDeep(this.attrs, other.attrs));
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
Convert this mark to a JSON-serializeable representation.
|
|
497
|
+
*/
|
|
498
|
+
toJSON() {
|
|
499
|
+
let obj = { type: this.type.name };
|
|
500
|
+
for (let _ in this.attrs) {
|
|
501
|
+
obj.attrs = this.attrs;
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
return obj;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
Deserialize a mark from JSON.
|
|
508
|
+
*/
|
|
509
|
+
static fromJSON(schema, json) {
|
|
510
|
+
if (!json)
|
|
511
|
+
throw new RangeError("Invalid input for Mark.fromJSON");
|
|
512
|
+
let type = schema.marks[json.type];
|
|
513
|
+
if (!type)
|
|
514
|
+
throw new RangeError(`There is no mark type ${json.type} in this schema`);
|
|
515
|
+
return type.create(json.attrs);
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
Test whether two sets of marks are identical.
|
|
519
|
+
*/
|
|
520
|
+
static sameSet(a, b) {
|
|
521
|
+
if (a == b)
|
|
522
|
+
return true;
|
|
523
|
+
if (a.length != b.length)
|
|
524
|
+
return false;
|
|
525
|
+
for (let i = 0; i < a.length; i++)
|
|
526
|
+
if (!a[i].eq(b[i]))
|
|
527
|
+
return false;
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
Create a properly sorted mark set from null, a single mark, or an
|
|
532
|
+
unsorted array of marks.
|
|
533
|
+
*/
|
|
534
|
+
static setFrom(marks) {
|
|
535
|
+
if (!marks || Array.isArray(marks) && marks.length == 0)
|
|
536
|
+
return Mark.none;
|
|
537
|
+
if (marks instanceof Mark)
|
|
538
|
+
return [marks];
|
|
539
|
+
let copy = marks.slice();
|
|
540
|
+
copy.sort((a, b) => a.type.rank - b.type.rank);
|
|
541
|
+
return copy;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
The empty set of marks.
|
|
546
|
+
*/
|
|
547
|
+
Mark.none = [];
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
|
|
551
|
+
given an invalid replacement.
|
|
552
|
+
*/
|
|
553
|
+
class ReplaceError extends Error {
|
|
554
|
+
}
|
|
555
|
+
/*
|
|
556
|
+
ReplaceError = function(this: any, message: string) {
|
|
557
|
+
let err = Error.call(this, message)
|
|
558
|
+
;(err as any).__proto__ = ReplaceError.prototype
|
|
559
|
+
return err
|
|
560
|
+
} as any
|
|
561
|
+
|
|
562
|
+
ReplaceError.prototype = Object.create(Error.prototype)
|
|
563
|
+
ReplaceError.prototype.constructor = ReplaceError
|
|
564
|
+
ReplaceError.prototype.name = "ReplaceError"
|
|
565
|
+
*/
|
|
566
|
+
/**
|
|
567
|
+
A slice represents a piece cut out of a larger document. It
|
|
568
|
+
stores not only a fragment, but also the depth up to which nodes on
|
|
569
|
+
both side are ‘open’ (cut through).
|
|
570
|
+
*/
|
|
571
|
+
class Slice {
|
|
572
|
+
/**
|
|
573
|
+
Create a slice. When specifying a non-zero open depth, you must
|
|
574
|
+
make sure that there are nodes of at least that depth at the
|
|
575
|
+
appropriate side of the fragment—i.e. if the fragment is an
|
|
576
|
+
empty paragraph node, `openStart` and `openEnd` can't be greater
|
|
577
|
+
than 1.
|
|
578
|
+
|
|
579
|
+
It is not necessary for the content of open nodes to conform to
|
|
580
|
+
the schema's content constraints, though it should be a valid
|
|
581
|
+
start/end/middle for such a node, depending on which sides are
|
|
582
|
+
open.
|
|
583
|
+
*/
|
|
584
|
+
constructor(
|
|
585
|
+
/**
|
|
586
|
+
The slice's content.
|
|
587
|
+
*/
|
|
588
|
+
content,
|
|
589
|
+
/**
|
|
590
|
+
The open depth at the start of the fragment.
|
|
591
|
+
*/
|
|
592
|
+
openStart,
|
|
593
|
+
/**
|
|
594
|
+
The open depth at the end.
|
|
595
|
+
*/
|
|
596
|
+
openEnd) {
|
|
597
|
+
this.content = content;
|
|
598
|
+
this.openStart = openStart;
|
|
599
|
+
this.openEnd = openEnd;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
The size this slice would add when inserted into a document.
|
|
603
|
+
*/
|
|
604
|
+
get size() {
|
|
605
|
+
return this.content.size - this.openStart - this.openEnd;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
@internal
|
|
609
|
+
*/
|
|
610
|
+
insertAt(pos, fragment) {
|
|
611
|
+
let content = insertInto(this.content, pos + this.openStart, fragment);
|
|
612
|
+
return content && new Slice(content, this.openStart, this.openEnd);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
@internal
|
|
616
|
+
*/
|
|
617
|
+
removeBetween(from, to) {
|
|
618
|
+
return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
Tests whether this slice is equal to another slice.
|
|
622
|
+
*/
|
|
623
|
+
eq(other) {
|
|
624
|
+
return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
@internal
|
|
628
|
+
*/
|
|
629
|
+
toString() {
|
|
630
|
+
return this.content + "(" + this.openStart + "," + this.openEnd + ")";
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
Convert a slice to a JSON-serializable representation.
|
|
634
|
+
*/
|
|
635
|
+
toJSON() {
|
|
636
|
+
if (!this.content.size)
|
|
637
|
+
return null;
|
|
638
|
+
let json = { content: this.content.toJSON() };
|
|
639
|
+
if (this.openStart > 0)
|
|
640
|
+
json.openStart = this.openStart;
|
|
641
|
+
if (this.openEnd > 0)
|
|
642
|
+
json.openEnd = this.openEnd;
|
|
643
|
+
return json;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
Deserialize a slice from its JSON representation.
|
|
647
|
+
*/
|
|
648
|
+
static fromJSON(schema, json) {
|
|
649
|
+
if (!json)
|
|
650
|
+
return Slice.empty;
|
|
651
|
+
let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
|
|
652
|
+
if (typeof openStart != "number" || typeof openEnd != "number")
|
|
653
|
+
throw new RangeError("Invalid input for Slice.fromJSON");
|
|
654
|
+
return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
Create a slice from a fragment by taking the maximum possible
|
|
658
|
+
open value on both side of the fragment.
|
|
659
|
+
*/
|
|
660
|
+
static maxOpen(fragment, openIsolating = true) {
|
|
661
|
+
let openStart = 0, openEnd = 0;
|
|
662
|
+
for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
|
|
663
|
+
openStart++;
|
|
664
|
+
for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
|
|
665
|
+
openEnd++;
|
|
666
|
+
return new Slice(fragment, openStart, openEnd);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
The empty slice.
|
|
671
|
+
*/
|
|
672
|
+
Slice.empty = new Slice(Fragment.empty, 0, 0);
|
|
673
|
+
function removeRange(content, from, to) {
|
|
674
|
+
let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
|
|
675
|
+
let { index: indexTo, offset: offsetTo } = content.findIndex(to);
|
|
676
|
+
if (offset == from || child.isText) {
|
|
677
|
+
if (offsetTo != to && !content.child(indexTo).isText)
|
|
678
|
+
throw new RangeError("Removing non-flat range");
|
|
679
|
+
return content.cut(0, from).append(content.cut(to));
|
|
680
|
+
}
|
|
681
|
+
if (index != indexTo)
|
|
682
|
+
throw new RangeError("Removing non-flat range");
|
|
683
|
+
return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
|
|
684
|
+
}
|
|
685
|
+
function insertInto(content, dist, insert, parent) {
|
|
686
|
+
let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
|
|
687
|
+
if (offset == dist || child.isText) {
|
|
688
|
+
if (parent && !parent.canReplace(index, index, insert))
|
|
32
689
|
return null;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
690
|
+
return content.cut(0, dist).append(insert).append(content.cut(dist));
|
|
691
|
+
}
|
|
692
|
+
let inner = insertInto(child.content, dist - offset - 1, insert);
|
|
693
|
+
return inner && content.replaceChild(index, child.copy(inner));
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// For node types where all attrs have a default value (or which don't
|
|
697
|
+
// have any attributes), build up a single reusable default attribute
|
|
698
|
+
// object, and use it for all nodes that don't specify specific
|
|
699
|
+
// attributes.
|
|
700
|
+
function defaultAttrs(attrs) {
|
|
701
|
+
let defaults = Object.create(null);
|
|
702
|
+
for (let attrName in attrs) {
|
|
703
|
+
let attr = attrs[attrName];
|
|
704
|
+
if (!attr.hasDefault)
|
|
705
|
+
return null;
|
|
706
|
+
defaults[attrName] = attr.default;
|
|
707
|
+
}
|
|
708
|
+
return defaults;
|
|
709
|
+
}
|
|
710
|
+
function computeAttrs(attrs, value) {
|
|
711
|
+
let built = Object.create(null);
|
|
712
|
+
for (let name in attrs) {
|
|
713
|
+
let given = value && value[name];
|
|
714
|
+
if (given === undefined) {
|
|
715
|
+
let attr = attrs[name];
|
|
716
|
+
if (attr.hasDefault)
|
|
717
|
+
given = attr.default;
|
|
718
|
+
else
|
|
719
|
+
throw new RangeError("No value supplied for attribute " + name);
|
|
720
|
+
}
|
|
721
|
+
built[name] = given;
|
|
722
|
+
}
|
|
723
|
+
return built;
|
|
724
|
+
}
|
|
725
|
+
function initAttrs(attrs) {
|
|
726
|
+
let result = Object.create(null);
|
|
727
|
+
if (attrs)
|
|
728
|
+
for (let name in attrs)
|
|
729
|
+
result[name] = new Attribute(attrs[name]);
|
|
730
|
+
return result;
|
|
731
|
+
}
|
|
732
|
+
// Attribute descriptors
|
|
733
|
+
class Attribute {
|
|
734
|
+
constructor(options) {
|
|
735
|
+
this.hasDefault = Object.prototype.hasOwnProperty.call(options, "default");
|
|
736
|
+
this.default = options.default;
|
|
737
|
+
}
|
|
738
|
+
get isRequired() {
|
|
739
|
+
return !this.hasDefault;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
// Marks
|
|
743
|
+
/**
|
|
744
|
+
Like nodes, marks (which are associated with nodes to signify
|
|
745
|
+
things like emphasis or being part of a link) are
|
|
746
|
+
[tagged](https://prosemirror.net/docs/ref/#model.Mark.type) with type objects, which are
|
|
747
|
+
instantiated once per `Schema`.
|
|
748
|
+
*/
|
|
749
|
+
class MarkType {
|
|
750
|
+
/**
|
|
751
|
+
@internal
|
|
752
|
+
*/
|
|
753
|
+
constructor(
|
|
754
|
+
/**
|
|
755
|
+
The name of the mark type.
|
|
756
|
+
*/
|
|
757
|
+
name,
|
|
758
|
+
/**
|
|
759
|
+
@internal
|
|
760
|
+
*/
|
|
761
|
+
rank,
|
|
762
|
+
/**
|
|
763
|
+
The schema that this mark type instance is part of.
|
|
764
|
+
*/
|
|
765
|
+
schema,
|
|
766
|
+
/**
|
|
767
|
+
The spec on which the type is based.
|
|
768
|
+
*/
|
|
769
|
+
spec) {
|
|
770
|
+
this.name = name;
|
|
771
|
+
this.rank = rank;
|
|
772
|
+
this.schema = schema;
|
|
773
|
+
this.spec = spec;
|
|
774
|
+
this.attrs = initAttrs(spec.attrs);
|
|
775
|
+
this.excluded = null;
|
|
776
|
+
let defaults = defaultAttrs(this.attrs);
|
|
777
|
+
this.instance = defaults ? new Mark(this, defaults) : null;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
Create a mark of this type. `attrs` may be `null` or an object
|
|
781
|
+
containing only some of the mark's attributes. The others, if
|
|
782
|
+
they have defaults, will be added.
|
|
783
|
+
*/
|
|
784
|
+
create(attrs = null) {
|
|
785
|
+
if (!attrs && this.instance)
|
|
786
|
+
return this.instance;
|
|
787
|
+
return new Mark(this, computeAttrs(this.attrs, attrs));
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
@internal
|
|
791
|
+
*/
|
|
792
|
+
static compile(marks, schema) {
|
|
793
|
+
let result = Object.create(null), rank = 0;
|
|
794
|
+
marks.forEach((name, spec) => result[name] = new MarkType(name, rank++, schema, spec));
|
|
795
|
+
return result;
|
|
796
|
+
}
|
|
797
|
+
/**
|
|
798
|
+
When there is a mark of this type in the given set, a new set
|
|
799
|
+
without it is returned. Otherwise, the input set is returned.
|
|
800
|
+
*/
|
|
801
|
+
removeFromSet(set) {
|
|
802
|
+
for (var i = 0; i < set.length; i++)
|
|
803
|
+
if (set[i].type == this) {
|
|
804
|
+
set = set.slice(0, i).concat(set.slice(i + 1));
|
|
805
|
+
i--;
|
|
806
|
+
}
|
|
807
|
+
return set;
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
Tests whether there is a mark of this type in the given set.
|
|
811
|
+
*/
|
|
812
|
+
isInSet(set) {
|
|
813
|
+
for (let i = 0; i < set.length; i++)
|
|
814
|
+
if (set[i].type == this)
|
|
815
|
+
return set[i];
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
Queries whether a given mark type is
|
|
819
|
+
[excluded](https://prosemirror.net/docs/ref/#model.MarkSpec.excludes) by this one.
|
|
820
|
+
*/
|
|
821
|
+
excludes(other) {
|
|
822
|
+
return this.excluded.indexOf(other) > -1;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// Recovery values encode a range index and an offset. They are
|
|
827
|
+
// represented as numbers, because tons of them will be created when
|
|
828
|
+
// mapping, for example, a large number of decorations. The number's
|
|
829
|
+
// lower 16 bits provide the index, the remaining bits the offset.
|
|
830
|
+
//
|
|
831
|
+
// Note: We intentionally don't use bit shift operators to en- and
|
|
832
|
+
// decode these, since those clip to 32 bits, which we might in rare
|
|
833
|
+
// cases want to overflow. A 64-bit float can represent 48-bit
|
|
834
|
+
// integers precisely.
|
|
835
|
+
const lower16 = 0xffff;
|
|
836
|
+
const factor16 = Math.pow(2, 16);
|
|
837
|
+
function makeRecover(index, offset) { return index + offset * factor16; }
|
|
838
|
+
function recoverIndex(value) { return value & lower16; }
|
|
839
|
+
function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
|
|
840
|
+
const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
|
|
841
|
+
/**
|
|
842
|
+
An object representing a mapped position with extra
|
|
843
|
+
information.
|
|
844
|
+
*/
|
|
845
|
+
class MapResult {
|
|
846
|
+
/**
|
|
847
|
+
@internal
|
|
848
|
+
*/
|
|
849
|
+
constructor(
|
|
850
|
+
/**
|
|
851
|
+
The mapped version of the position.
|
|
852
|
+
*/
|
|
853
|
+
pos,
|
|
854
|
+
/**
|
|
855
|
+
@internal
|
|
856
|
+
*/
|
|
857
|
+
delInfo,
|
|
858
|
+
/**
|
|
859
|
+
@internal
|
|
860
|
+
*/
|
|
861
|
+
recover) {
|
|
862
|
+
this.pos = pos;
|
|
863
|
+
this.delInfo = delInfo;
|
|
864
|
+
this.recover = recover;
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
Tells you whether the position was deleted, that is, whether the
|
|
868
|
+
step removed the token on the side queried (via the `assoc`)
|
|
869
|
+
argument from the document.
|
|
870
|
+
*/
|
|
871
|
+
get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
|
|
872
|
+
/**
|
|
873
|
+
Tells you whether the token before the mapped position was deleted.
|
|
874
|
+
*/
|
|
875
|
+
get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
|
|
876
|
+
/**
|
|
877
|
+
True when the token after the mapped position was deleted.
|
|
878
|
+
*/
|
|
879
|
+
get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
|
|
880
|
+
/**
|
|
881
|
+
Tells whether any of the steps mapped through deletes across the
|
|
882
|
+
position (including both the token before and after the
|
|
883
|
+
position).
|
|
884
|
+
*/
|
|
885
|
+
get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
A map describing the deletions and insertions made by a step, which
|
|
889
|
+
can be used to find the correspondence between positions in the
|
|
890
|
+
pre-step version of a document and the same position in the
|
|
891
|
+
post-step version.
|
|
892
|
+
*/
|
|
893
|
+
class StepMap {
|
|
894
|
+
/**
|
|
895
|
+
Create a position map. The modifications to the document are
|
|
896
|
+
represented as an array of numbers, in which each group of three
|
|
897
|
+
represents a modified chunk as `[start, oldSize, newSize]`.
|
|
898
|
+
*/
|
|
899
|
+
constructor(
|
|
900
|
+
/**
|
|
901
|
+
@internal
|
|
902
|
+
*/
|
|
903
|
+
ranges,
|
|
904
|
+
/**
|
|
905
|
+
@internal
|
|
906
|
+
*/
|
|
907
|
+
inverted = false) {
|
|
908
|
+
this.ranges = ranges;
|
|
909
|
+
this.inverted = inverted;
|
|
910
|
+
if (!ranges.length && StepMap.empty)
|
|
911
|
+
return StepMap.empty;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
@internal
|
|
915
|
+
*/
|
|
916
|
+
recover(value) {
|
|
917
|
+
let diff = 0, index = recoverIndex(value);
|
|
918
|
+
if (!this.inverted)
|
|
919
|
+
for (let i = 0; i < index; i++)
|
|
920
|
+
diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
|
|
921
|
+
return this.ranges[index * 3] + diff + recoverOffset(value);
|
|
922
|
+
}
|
|
923
|
+
mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
|
|
924
|
+
map(pos, assoc = 1) { return this._map(pos, assoc, true); }
|
|
925
|
+
/**
|
|
926
|
+
@internal
|
|
927
|
+
*/
|
|
928
|
+
_map(pos, assoc, simple) {
|
|
929
|
+
let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
930
|
+
for (let i = 0; i < this.ranges.length; i += 3) {
|
|
931
|
+
let start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
932
|
+
if (start > pos)
|
|
933
|
+
break;
|
|
934
|
+
let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
|
|
935
|
+
if (pos <= end) {
|
|
936
|
+
let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
|
|
937
|
+
let result = start + diff + (side < 0 ? 0 : newSize);
|
|
938
|
+
if (simple)
|
|
939
|
+
return result;
|
|
940
|
+
let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
|
|
941
|
+
let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
|
|
942
|
+
if (assoc < 0 ? pos != start : pos != end)
|
|
943
|
+
del |= DEL_SIDE;
|
|
944
|
+
return new MapResult(result, del, recover);
|
|
945
|
+
}
|
|
946
|
+
diff += newSize - oldSize;
|
|
947
|
+
}
|
|
948
|
+
return simple ? pos + diff : new MapResult(pos + diff, 0, null);
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
@internal
|
|
952
|
+
*/
|
|
953
|
+
touches(pos, recover) {
|
|
954
|
+
let diff = 0, index = recoverIndex(recover);
|
|
955
|
+
let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
956
|
+
for (let i = 0; i < this.ranges.length; i += 3) {
|
|
957
|
+
let start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
958
|
+
if (start > pos)
|
|
959
|
+
break;
|
|
960
|
+
let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
|
|
961
|
+
if (pos <= end && i == index * 3)
|
|
962
|
+
return true;
|
|
963
|
+
diff += this.ranges[i + newIndex] - oldSize;
|
|
82
964
|
}
|
|
83
965
|
return false;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
Calls the given function on each of the changed ranges included in
|
|
969
|
+
this map.
|
|
970
|
+
*/
|
|
971
|
+
forEach(f) {
|
|
972
|
+
let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
973
|
+
for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
|
|
974
|
+
let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
|
|
975
|
+
let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
|
|
976
|
+
f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
|
|
977
|
+
diff += newSize - oldSize;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
Create an inverted version of this map. The result can be used to
|
|
982
|
+
map positions in the post-step document to the pre-step document.
|
|
983
|
+
*/
|
|
984
|
+
invert() {
|
|
985
|
+
return new StepMap(this.ranges, !this.inverted);
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
@internal
|
|
989
|
+
*/
|
|
990
|
+
toString() {
|
|
991
|
+
return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
Create a map that moves all positions by offset `n` (which may be
|
|
995
|
+
negative). This can be useful when applying steps meant for a
|
|
996
|
+
sub-document to a larger document, or vice-versa.
|
|
997
|
+
*/
|
|
998
|
+
static offset(n) {
|
|
999
|
+
return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
A StepMap that contains no changed ranges.
|
|
1004
|
+
*/
|
|
1005
|
+
StepMap.empty = new StepMap([]);
|
|
1006
|
+
/**
|
|
1007
|
+
A mapping represents a pipeline of zero or more [step
|
|
1008
|
+
maps](https://prosemirror.net/docs/ref/#transform.StepMap). It has special provisions for losslessly
|
|
1009
|
+
handling mapping positions through a series of steps in which some
|
|
1010
|
+
steps are inverted versions of earlier steps. (This comes up when
|
|
1011
|
+
‘[rebasing](/docs/guide/#transform.rebasing)’ steps for
|
|
1012
|
+
collaboration or history management.)
|
|
1013
|
+
*/
|
|
1014
|
+
class Mapping {
|
|
1015
|
+
/**
|
|
1016
|
+
Create a new mapping with the given position maps.
|
|
1017
|
+
*/
|
|
1018
|
+
constructor(
|
|
1019
|
+
/**
|
|
1020
|
+
The step maps in this mapping.
|
|
1021
|
+
*/
|
|
1022
|
+
maps = [],
|
|
1023
|
+
/**
|
|
1024
|
+
@internal
|
|
1025
|
+
*/
|
|
1026
|
+
mirror,
|
|
1027
|
+
/**
|
|
1028
|
+
The starting position in the `maps` array, used when `map` or
|
|
1029
|
+
`mapResult` is called.
|
|
1030
|
+
*/
|
|
1031
|
+
from = 0,
|
|
1032
|
+
/**
|
|
1033
|
+
The end position in the `maps` array.
|
|
1034
|
+
*/
|
|
1035
|
+
to = maps.length) {
|
|
1036
|
+
this.maps = maps;
|
|
1037
|
+
this.mirror = mirror;
|
|
1038
|
+
this.from = from;
|
|
1039
|
+
this.to = to;
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
Create a mapping that maps only through a part of this one.
|
|
1043
|
+
*/
|
|
1044
|
+
slice(from = 0, to = this.maps.length) {
|
|
1045
|
+
return new Mapping(this.maps, this.mirror, from, to);
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
@internal
|
|
1049
|
+
*/
|
|
1050
|
+
copy() {
|
|
1051
|
+
return new Mapping(this.maps.slice(), this.mirror && this.mirror.slice(), this.from, this.to);
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
Add a step map to the end of this mapping. If `mirrors` is
|
|
1055
|
+
given, it should be the index of the step map that is the mirror
|
|
1056
|
+
image of this one.
|
|
1057
|
+
*/
|
|
1058
|
+
appendMap(map, mirrors) {
|
|
1059
|
+
this.to = this.maps.push(map);
|
|
1060
|
+
if (mirrors != null)
|
|
1061
|
+
this.setMirror(this.maps.length - 1, mirrors);
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
Add all the step maps in a given mapping to this one (preserving
|
|
1065
|
+
mirroring information).
|
|
1066
|
+
*/
|
|
1067
|
+
appendMapping(mapping) {
|
|
1068
|
+
for (let i = 0, startSize = this.maps.length; i < mapping.maps.length; i++) {
|
|
1069
|
+
let mirr = mapping.getMirror(i);
|
|
1070
|
+
this.appendMap(mapping.maps[i], mirr != null && mirr < i ? startSize + mirr : undefined);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
Finds the offset of the step map that mirrors the map at the
|
|
1075
|
+
given offset, in this mapping (as per the second argument to
|
|
1076
|
+
`appendMap`).
|
|
1077
|
+
*/
|
|
1078
|
+
getMirror(n) {
|
|
1079
|
+
if (this.mirror)
|
|
1080
|
+
for (let i = 0; i < this.mirror.length; i++)
|
|
1081
|
+
if (this.mirror[i] == n)
|
|
1082
|
+
return this.mirror[i + (i % 2 ? -1 : 1)];
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
@internal
|
|
1086
|
+
*/
|
|
1087
|
+
setMirror(n, m) {
|
|
1088
|
+
if (!this.mirror)
|
|
1089
|
+
this.mirror = [];
|
|
1090
|
+
this.mirror.push(n, m);
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
Append the inverse of the given mapping to this one.
|
|
1094
|
+
*/
|
|
1095
|
+
appendMappingInverted(mapping) {
|
|
1096
|
+
for (let i = mapping.maps.length - 1, totalSize = this.maps.length + mapping.maps.length; i >= 0; i--) {
|
|
1097
|
+
let mirr = mapping.getMirror(i);
|
|
1098
|
+
this.appendMap(mapping.maps[i].invert(), mirr != null && mirr > i ? totalSize - mirr - 1 : undefined);
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
Create an inverted version of this mapping.
|
|
1103
|
+
*/
|
|
1104
|
+
invert() {
|
|
1105
|
+
let inverse = new Mapping;
|
|
1106
|
+
inverse.appendMappingInverted(this);
|
|
1107
|
+
return inverse;
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
Map a position through this mapping.
|
|
1111
|
+
*/
|
|
1112
|
+
map(pos, assoc = 1) {
|
|
1113
|
+
if (this.mirror)
|
|
1114
|
+
return this._map(pos, assoc, true);
|
|
1115
|
+
for (let i = this.from; i < this.to; i++)
|
|
1116
|
+
pos = this.maps[i].map(pos, assoc);
|
|
1117
|
+
return pos;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
Map a position through this mapping, returning a mapping
|
|
1121
|
+
result.
|
|
1122
|
+
*/
|
|
1123
|
+
mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
|
|
1124
|
+
/**
|
|
1125
|
+
@internal
|
|
1126
|
+
*/
|
|
1127
|
+
_map(pos, assoc, simple) {
|
|
1128
|
+
let delInfo = 0;
|
|
1129
|
+
for (let i = this.from; i < this.to; i++) {
|
|
1130
|
+
let map = this.maps[i], result = map.mapResult(pos, assoc);
|
|
1131
|
+
if (result.recover != null) {
|
|
1132
|
+
let corr = this.getMirror(i);
|
|
1133
|
+
if (corr != null && corr > i && corr < this.to) {
|
|
1134
|
+
i = corr;
|
|
1135
|
+
pos = this.maps[corr].recover(result.recover);
|
|
1136
|
+
continue;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
delInfo |= result.delInfo;
|
|
1140
|
+
pos = result.pos;
|
|
1141
|
+
}
|
|
1142
|
+
return simple ? pos : new MapResult(pos, delInfo, null);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
const stepsByID = Object.create(null);
|
|
1147
|
+
/**
|
|
1148
|
+
A step object represents an atomic change. It generally applies
|
|
1149
|
+
only to the document it was created for, since the positions
|
|
1150
|
+
stored in it will only make sense for that document.
|
|
1151
|
+
|
|
1152
|
+
New steps are defined by creating classes that extend `Step`,
|
|
1153
|
+
overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
|
|
1154
|
+
methods, and registering your class with a unique
|
|
1155
|
+
JSON-serialization identifier using
|
|
1156
|
+
[`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
|
|
1157
|
+
*/
|
|
1158
|
+
class Step {
|
|
1159
|
+
/**
|
|
1160
|
+
Get the step map that represents the changes made by this step,
|
|
1161
|
+
and which can be used to transform between positions in the old
|
|
1162
|
+
and the new document.
|
|
1163
|
+
*/
|
|
1164
|
+
getMap() { return StepMap.empty; }
|
|
1165
|
+
/**
|
|
1166
|
+
Try to merge this step with another one, to be applied directly
|
|
1167
|
+
after it. Returns the merged step when possible, null if the
|
|
1168
|
+
steps can't be merged.
|
|
1169
|
+
*/
|
|
1170
|
+
merge(other) { return null; }
|
|
1171
|
+
/**
|
|
1172
|
+
Deserialize a step from its JSON representation. Will call
|
|
1173
|
+
through to the step class' own implementation of this method.
|
|
1174
|
+
*/
|
|
1175
|
+
static fromJSON(schema, json) {
|
|
1176
|
+
if (!json || !json.stepType)
|
|
1177
|
+
throw new RangeError("Invalid input for Step.fromJSON");
|
|
1178
|
+
let type = stepsByID[json.stepType];
|
|
1179
|
+
if (!type)
|
|
1180
|
+
throw new RangeError(`No step type ${json.stepType} defined`);
|
|
1181
|
+
return type.fromJSON(schema, json);
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
To be able to serialize steps to JSON, each step needs a string
|
|
1185
|
+
ID to attach to its JSON representation. Use this method to
|
|
1186
|
+
register an ID for your step classes. Try to pick something
|
|
1187
|
+
that's unlikely to clash with steps from other modules.
|
|
1188
|
+
*/
|
|
1189
|
+
static jsonID(id, stepClass) {
|
|
1190
|
+
if (id in stepsByID)
|
|
1191
|
+
throw new RangeError("Duplicate use of step JSON ID " + id);
|
|
1192
|
+
stepsByID[id] = stepClass;
|
|
1193
|
+
stepClass.prototype.jsonID = id;
|
|
1194
|
+
return stepClass;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
|
|
1199
|
+
new document or a failure value.
|
|
1200
|
+
*/
|
|
1201
|
+
class StepResult {
|
|
1202
|
+
/**
|
|
1203
|
+
@internal
|
|
1204
|
+
*/
|
|
1205
|
+
constructor(
|
|
1206
|
+
/**
|
|
1207
|
+
The transformed document, if successful.
|
|
1208
|
+
*/
|
|
1209
|
+
doc,
|
|
1210
|
+
/**
|
|
1211
|
+
The failure message, if unsuccessful.
|
|
1212
|
+
*/
|
|
1213
|
+
failed) {
|
|
1214
|
+
this.doc = doc;
|
|
1215
|
+
this.failed = failed;
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
Create a successful step result.
|
|
1219
|
+
*/
|
|
1220
|
+
static ok(doc) { return new StepResult(doc, null); }
|
|
1221
|
+
/**
|
|
1222
|
+
Create a failed step result.
|
|
1223
|
+
*/
|
|
1224
|
+
static fail(message) { return new StepResult(null, message); }
|
|
1225
|
+
/**
|
|
1226
|
+
Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
|
|
1227
|
+
arguments. Create a successful result if it succeeds, and a
|
|
1228
|
+
failed one if it throws a `ReplaceError`.
|
|
1229
|
+
*/
|
|
1230
|
+
static fromReplace(doc, from, to, slice) {
|
|
1231
|
+
try {
|
|
1232
|
+
return StepResult.ok(doc.replace(from, to, slice));
|
|
1233
|
+
}
|
|
1234
|
+
catch (e) {
|
|
1235
|
+
if (e instanceof ReplaceError)
|
|
1236
|
+
return StepResult.fail(e.message);
|
|
1237
|
+
throw e;
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
function mapFragment(fragment, f, parent) {
|
|
1243
|
+
let mapped = [];
|
|
1244
|
+
for (let i = 0; i < fragment.childCount; i++) {
|
|
1245
|
+
let child = fragment.child(i);
|
|
1246
|
+
if (child.content.size)
|
|
1247
|
+
child = child.copy(mapFragment(child.content, f, child));
|
|
1248
|
+
if (child.isInline)
|
|
1249
|
+
child = f(child, parent, i);
|
|
1250
|
+
mapped.push(child);
|
|
1251
|
+
}
|
|
1252
|
+
return Fragment.fromArray(mapped);
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
Add a mark to all inline content between two positions.
|
|
1256
|
+
*/
|
|
1257
|
+
class AddMarkStep extends Step {
|
|
1258
|
+
/**
|
|
1259
|
+
Create a mark step.
|
|
1260
|
+
*/
|
|
1261
|
+
constructor(
|
|
1262
|
+
/**
|
|
1263
|
+
The start of the marked range.
|
|
1264
|
+
*/
|
|
1265
|
+
from,
|
|
1266
|
+
/**
|
|
1267
|
+
The end of the marked range.
|
|
1268
|
+
*/
|
|
1269
|
+
to,
|
|
1270
|
+
/**
|
|
1271
|
+
The mark to add.
|
|
1272
|
+
*/
|
|
1273
|
+
mark) {
|
|
1274
|
+
super();
|
|
1275
|
+
this.from = from;
|
|
1276
|
+
this.to = to;
|
|
1277
|
+
this.mark = mark;
|
|
1278
|
+
}
|
|
1279
|
+
apply(doc) {
|
|
1280
|
+
let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
|
|
1281
|
+
let parent = $from.node($from.sharedDepth(this.to));
|
|
1282
|
+
let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
|
|
1283
|
+
if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type))
|
|
1284
|
+
return node;
|
|
1285
|
+
return node.mark(this.mark.addToSet(node.marks));
|
|
1286
|
+
}, parent), oldSlice.openStart, oldSlice.openEnd);
|
|
1287
|
+
return StepResult.fromReplace(doc, this.from, this.to, slice);
|
|
1288
|
+
}
|
|
1289
|
+
invert() {
|
|
1290
|
+
return new RemoveMarkStep(this.from, this.to, this.mark);
|
|
1291
|
+
}
|
|
1292
|
+
map(mapping) {
|
|
1293
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1294
|
+
if (from.deleted && to.deleted || from.pos >= to.pos)
|
|
1295
|
+
return null;
|
|
1296
|
+
return new AddMarkStep(from.pos, to.pos, this.mark);
|
|
1297
|
+
}
|
|
1298
|
+
merge(other) {
|
|
1299
|
+
if (other instanceof AddMarkStep &&
|
|
1300
|
+
other.mark.eq(this.mark) &&
|
|
1301
|
+
this.from <= other.to && this.to >= other.from)
|
|
1302
|
+
return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
|
|
1303
|
+
return null;
|
|
1304
|
+
}
|
|
1305
|
+
toJSON() {
|
|
1306
|
+
return { stepType: "addMark", mark: this.mark.toJSON(),
|
|
1307
|
+
from: this.from, to: this.to };
|
|
1308
|
+
}
|
|
1309
|
+
/**
|
|
1310
|
+
@internal
|
|
1311
|
+
*/
|
|
1312
|
+
static fromJSON(schema, json) {
|
|
1313
|
+
if (typeof json.from != "number" || typeof json.to != "number")
|
|
1314
|
+
throw new RangeError("Invalid input for AddMarkStep.fromJSON");
|
|
1315
|
+
return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
Step.jsonID("addMark", AddMarkStep);
|
|
1319
|
+
/**
|
|
1320
|
+
Remove a mark from all inline content between two positions.
|
|
1321
|
+
*/
|
|
1322
|
+
class RemoveMarkStep extends Step {
|
|
1323
|
+
/**
|
|
1324
|
+
Create a mark-removing step.
|
|
1325
|
+
*/
|
|
1326
|
+
constructor(
|
|
1327
|
+
/**
|
|
1328
|
+
The start of the unmarked range.
|
|
1329
|
+
*/
|
|
1330
|
+
from,
|
|
1331
|
+
/**
|
|
1332
|
+
The end of the unmarked range.
|
|
1333
|
+
*/
|
|
1334
|
+
to,
|
|
1335
|
+
/**
|
|
1336
|
+
The mark to remove.
|
|
1337
|
+
*/
|
|
1338
|
+
mark) {
|
|
1339
|
+
super();
|
|
1340
|
+
this.from = from;
|
|
1341
|
+
this.to = to;
|
|
1342
|
+
this.mark = mark;
|
|
1343
|
+
}
|
|
1344
|
+
apply(doc) {
|
|
1345
|
+
let oldSlice = doc.slice(this.from, this.to);
|
|
1346
|
+
let slice = new Slice(mapFragment(oldSlice.content, node => {
|
|
1347
|
+
return node.mark(this.mark.removeFromSet(node.marks));
|
|
1348
|
+
}, doc), oldSlice.openStart, oldSlice.openEnd);
|
|
1349
|
+
return StepResult.fromReplace(doc, this.from, this.to, slice);
|
|
1350
|
+
}
|
|
1351
|
+
invert() {
|
|
1352
|
+
return new AddMarkStep(this.from, this.to, this.mark);
|
|
1353
|
+
}
|
|
1354
|
+
map(mapping) {
|
|
1355
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1356
|
+
if (from.deleted && to.deleted || from.pos >= to.pos)
|
|
1357
|
+
return null;
|
|
1358
|
+
return new RemoveMarkStep(from.pos, to.pos, this.mark);
|
|
1359
|
+
}
|
|
1360
|
+
merge(other) {
|
|
1361
|
+
if (other instanceof RemoveMarkStep &&
|
|
1362
|
+
other.mark.eq(this.mark) &&
|
|
1363
|
+
this.from <= other.to && this.to >= other.from)
|
|
1364
|
+
return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
|
|
1365
|
+
return null;
|
|
1366
|
+
}
|
|
1367
|
+
toJSON() {
|
|
1368
|
+
return { stepType: "removeMark", mark: this.mark.toJSON(),
|
|
1369
|
+
from: this.from, to: this.to };
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
@internal
|
|
1373
|
+
*/
|
|
1374
|
+
static fromJSON(schema, json) {
|
|
1375
|
+
if (typeof json.from != "number" || typeof json.to != "number")
|
|
1376
|
+
throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
|
|
1377
|
+
return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
Step.jsonID("removeMark", RemoveMarkStep);
|
|
1381
|
+
/**
|
|
1382
|
+
Add a mark to a specific node.
|
|
1383
|
+
*/
|
|
1384
|
+
class AddNodeMarkStep extends Step {
|
|
1385
|
+
/**
|
|
1386
|
+
Create a node mark step.
|
|
1387
|
+
*/
|
|
1388
|
+
constructor(
|
|
1389
|
+
/**
|
|
1390
|
+
The position of the target node.
|
|
1391
|
+
*/
|
|
1392
|
+
pos,
|
|
1393
|
+
/**
|
|
1394
|
+
The mark to add.
|
|
1395
|
+
*/
|
|
1396
|
+
mark) {
|
|
1397
|
+
super();
|
|
1398
|
+
this.pos = pos;
|
|
1399
|
+
this.mark = mark;
|
|
1400
|
+
}
|
|
1401
|
+
apply(doc) {
|
|
1402
|
+
let node = doc.nodeAt(this.pos);
|
|
1403
|
+
if (!node)
|
|
1404
|
+
return StepResult.fail("No node at mark step's position");
|
|
1405
|
+
let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
|
|
1406
|
+
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
1407
|
+
}
|
|
1408
|
+
invert(doc) {
|
|
1409
|
+
let node = doc.nodeAt(this.pos);
|
|
1410
|
+
if (node) {
|
|
1411
|
+
let newSet = this.mark.addToSet(node.marks);
|
|
1412
|
+
if (newSet.length == node.marks.length) {
|
|
1413
|
+
for (let i = 0; i < node.marks.length; i++)
|
|
1414
|
+
if (!node.marks[i].isInSet(newSet))
|
|
1415
|
+
return new AddNodeMarkStep(this.pos, node.marks[i]);
|
|
1416
|
+
return new AddNodeMarkStep(this.pos, this.mark);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
return new RemoveNodeMarkStep(this.pos, this.mark);
|
|
1420
|
+
}
|
|
1421
|
+
map(mapping) {
|
|
1422
|
+
let pos = mapping.mapResult(this.pos, 1);
|
|
1423
|
+
return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
|
|
1424
|
+
}
|
|
1425
|
+
toJSON() {
|
|
1426
|
+
return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
|
|
1427
|
+
}
|
|
1428
|
+
/**
|
|
1429
|
+
@internal
|
|
1430
|
+
*/
|
|
1431
|
+
static fromJSON(schema, json) {
|
|
1432
|
+
if (typeof json.pos != "number")
|
|
1433
|
+
throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
|
|
1434
|
+
return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
Step.jsonID("addNodeMark", AddNodeMarkStep);
|
|
1438
|
+
/**
|
|
1439
|
+
Remove a mark from a specific node.
|
|
1440
|
+
*/
|
|
1441
|
+
class RemoveNodeMarkStep extends Step {
|
|
1442
|
+
/**
|
|
1443
|
+
Create a mark-removing step.
|
|
1444
|
+
*/
|
|
1445
|
+
constructor(
|
|
1446
|
+
/**
|
|
1447
|
+
The position of the target node.
|
|
1448
|
+
*/
|
|
1449
|
+
pos,
|
|
1450
|
+
/**
|
|
1451
|
+
The mark to remove.
|
|
1452
|
+
*/
|
|
1453
|
+
mark) {
|
|
1454
|
+
super();
|
|
1455
|
+
this.pos = pos;
|
|
1456
|
+
this.mark = mark;
|
|
1457
|
+
}
|
|
1458
|
+
apply(doc) {
|
|
1459
|
+
let node = doc.nodeAt(this.pos);
|
|
1460
|
+
if (!node)
|
|
1461
|
+
return StepResult.fail("No node at mark step's position");
|
|
1462
|
+
let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
|
|
1463
|
+
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
1464
|
+
}
|
|
1465
|
+
invert(doc) {
|
|
1466
|
+
let node = doc.nodeAt(this.pos);
|
|
1467
|
+
if (!node || !this.mark.isInSet(node.marks))
|
|
1468
|
+
return this;
|
|
1469
|
+
return new AddNodeMarkStep(this.pos, this.mark);
|
|
1470
|
+
}
|
|
1471
|
+
map(mapping) {
|
|
1472
|
+
let pos = mapping.mapResult(this.pos, 1);
|
|
1473
|
+
return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
|
|
1474
|
+
}
|
|
1475
|
+
toJSON() {
|
|
1476
|
+
return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
|
|
1477
|
+
}
|
|
1478
|
+
/**
|
|
1479
|
+
@internal
|
|
1480
|
+
*/
|
|
1481
|
+
static fromJSON(schema, json) {
|
|
1482
|
+
if (typeof json.pos != "number")
|
|
1483
|
+
throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
|
|
1484
|
+
return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
|
|
1488
|
+
|
|
1489
|
+
/**
|
|
1490
|
+
Replace a part of the document with a slice of new content.
|
|
1491
|
+
*/
|
|
1492
|
+
class ReplaceStep extends Step {
|
|
1493
|
+
/**
|
|
1494
|
+
The given `slice` should fit the 'gap' between `from` and
|
|
1495
|
+
`to`—the depths must line up, and the surrounding nodes must be
|
|
1496
|
+
able to be joined with the open sides of the slice. When
|
|
1497
|
+
`structure` is true, the step will fail if the content between
|
|
1498
|
+
from and to is not just a sequence of closing and then opening
|
|
1499
|
+
tokens (this is to guard against rebased replace steps
|
|
1500
|
+
overwriting something they weren't supposed to).
|
|
1501
|
+
*/
|
|
1502
|
+
constructor(
|
|
1503
|
+
/**
|
|
1504
|
+
The start position of the replaced range.
|
|
1505
|
+
*/
|
|
1506
|
+
from,
|
|
1507
|
+
/**
|
|
1508
|
+
The end position of the replaced range.
|
|
1509
|
+
*/
|
|
1510
|
+
to,
|
|
1511
|
+
/**
|
|
1512
|
+
The slice to insert.
|
|
1513
|
+
*/
|
|
1514
|
+
slice,
|
|
1515
|
+
/**
|
|
1516
|
+
@internal
|
|
1517
|
+
*/
|
|
1518
|
+
structure = false) {
|
|
1519
|
+
super();
|
|
1520
|
+
this.from = from;
|
|
1521
|
+
this.to = to;
|
|
1522
|
+
this.slice = slice;
|
|
1523
|
+
this.structure = structure;
|
|
1524
|
+
}
|
|
1525
|
+
apply(doc) {
|
|
1526
|
+
if (this.structure && contentBetween(doc, this.from, this.to))
|
|
1527
|
+
return StepResult.fail("Structure replace would overwrite content");
|
|
1528
|
+
return StepResult.fromReplace(doc, this.from, this.to, this.slice);
|
|
1529
|
+
}
|
|
1530
|
+
getMap() {
|
|
1531
|
+
return new StepMap([this.from, this.to - this.from, this.slice.size]);
|
|
1532
|
+
}
|
|
1533
|
+
invert(doc) {
|
|
1534
|
+
return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
|
|
1535
|
+
}
|
|
1536
|
+
map(mapping) {
|
|
1537
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1538
|
+
if (from.deletedAcross && to.deletedAcross)
|
|
1539
|
+
return null;
|
|
1540
|
+
return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
|
|
1541
|
+
}
|
|
1542
|
+
merge(other) {
|
|
1543
|
+
if (!(other instanceof ReplaceStep) || other.structure || this.structure)
|
|
1544
|
+
return null;
|
|
1545
|
+
if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
|
|
1546
|
+
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
|
|
1547
|
+
: new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
|
|
1548
|
+
return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
|
|
1549
|
+
}
|
|
1550
|
+
else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
|
|
1551
|
+
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
|
|
1552
|
+
: new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
|
|
1553
|
+
return new ReplaceStep(other.from, this.to, slice, this.structure);
|
|
1554
|
+
}
|
|
1555
|
+
else {
|
|
1556
|
+
return null;
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
toJSON() {
|
|
1560
|
+
let json = { stepType: "replace", from: this.from, to: this.to };
|
|
1561
|
+
if (this.slice.size)
|
|
1562
|
+
json.slice = this.slice.toJSON();
|
|
1563
|
+
if (this.structure)
|
|
1564
|
+
json.structure = true;
|
|
1565
|
+
return json;
|
|
1566
|
+
}
|
|
1567
|
+
/**
|
|
1568
|
+
@internal
|
|
1569
|
+
*/
|
|
1570
|
+
static fromJSON(schema, json) {
|
|
1571
|
+
if (typeof json.from != "number" || typeof json.to != "number")
|
|
1572
|
+
throw new RangeError("Invalid input for ReplaceStep.fromJSON");
|
|
1573
|
+
return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
Step.jsonID("replace", ReplaceStep);
|
|
1577
|
+
/**
|
|
1578
|
+
Replace a part of the document with a slice of content, but
|
|
1579
|
+
preserve a range of the replaced content by moving it into the
|
|
1580
|
+
slice.
|
|
1581
|
+
*/
|
|
1582
|
+
class ReplaceAroundStep extends Step {
|
|
1583
|
+
/**
|
|
1584
|
+
Create a replace-around step with the given range and gap.
|
|
1585
|
+
`insert` should be the point in the slice into which the content
|
|
1586
|
+
of the gap should be moved. `structure` has the same meaning as
|
|
1587
|
+
it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
|
|
1588
|
+
*/
|
|
1589
|
+
constructor(
|
|
1590
|
+
/**
|
|
1591
|
+
The start position of the replaced range.
|
|
1592
|
+
*/
|
|
1593
|
+
from,
|
|
1594
|
+
/**
|
|
1595
|
+
The end position of the replaced range.
|
|
1596
|
+
*/
|
|
1597
|
+
to,
|
|
1598
|
+
/**
|
|
1599
|
+
The start of preserved range.
|
|
1600
|
+
*/
|
|
1601
|
+
gapFrom,
|
|
1602
|
+
/**
|
|
1603
|
+
The end of preserved range.
|
|
1604
|
+
*/
|
|
1605
|
+
gapTo,
|
|
1606
|
+
/**
|
|
1607
|
+
The slice to insert.
|
|
1608
|
+
*/
|
|
1609
|
+
slice,
|
|
1610
|
+
/**
|
|
1611
|
+
The position in the slice where the preserved range should be
|
|
1612
|
+
inserted.
|
|
1613
|
+
*/
|
|
1614
|
+
insert,
|
|
1615
|
+
/**
|
|
1616
|
+
@internal
|
|
1617
|
+
*/
|
|
1618
|
+
structure = false) {
|
|
1619
|
+
super();
|
|
1620
|
+
this.from = from;
|
|
1621
|
+
this.to = to;
|
|
1622
|
+
this.gapFrom = gapFrom;
|
|
1623
|
+
this.gapTo = gapTo;
|
|
1624
|
+
this.slice = slice;
|
|
1625
|
+
this.insert = insert;
|
|
1626
|
+
this.structure = structure;
|
|
1627
|
+
}
|
|
1628
|
+
apply(doc) {
|
|
1629
|
+
if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
|
|
1630
|
+
contentBetween(doc, this.gapTo, this.to)))
|
|
1631
|
+
return StepResult.fail("Structure gap-replace would overwrite content");
|
|
1632
|
+
let gap = doc.slice(this.gapFrom, this.gapTo);
|
|
1633
|
+
if (gap.openStart || gap.openEnd)
|
|
1634
|
+
return StepResult.fail("Gap is not a flat range");
|
|
1635
|
+
let inserted = this.slice.insertAt(this.insert, gap.content);
|
|
1636
|
+
if (!inserted)
|
|
1637
|
+
return StepResult.fail("Content does not fit in gap");
|
|
1638
|
+
return StepResult.fromReplace(doc, this.from, this.to, inserted);
|
|
1639
|
+
}
|
|
1640
|
+
getMap() {
|
|
1641
|
+
return new StepMap([this.from, this.gapFrom - this.from, this.insert,
|
|
1642
|
+
this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
|
|
1643
|
+
}
|
|
1644
|
+
invert(doc) {
|
|
1645
|
+
let gap = this.gapTo - this.gapFrom;
|
|
1646
|
+
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);
|
|
1647
|
+
}
|
|
1648
|
+
map(mapping) {
|
|
1649
|
+
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1650
|
+
let gapFrom = mapping.map(this.gapFrom, -1), gapTo = mapping.map(this.gapTo, 1);
|
|
1651
|
+
if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
|
|
1652
|
+
return null;
|
|
1653
|
+
return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
|
|
1654
|
+
}
|
|
1655
|
+
toJSON() {
|
|
1656
|
+
let json = { stepType: "replaceAround", from: this.from, to: this.to,
|
|
1657
|
+
gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert };
|
|
1658
|
+
if (this.slice.size)
|
|
1659
|
+
json.slice = this.slice.toJSON();
|
|
1660
|
+
if (this.structure)
|
|
1661
|
+
json.structure = true;
|
|
1662
|
+
return json;
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
@internal
|
|
1666
|
+
*/
|
|
1667
|
+
static fromJSON(schema, json) {
|
|
1668
|
+
if (typeof json.from != "number" || typeof json.to != "number" ||
|
|
1669
|
+
typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
|
|
1670
|
+
throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
|
|
1671
|
+
return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
Step.jsonID("replaceAround", ReplaceAroundStep);
|
|
1675
|
+
function contentBetween(doc, from, to) {
|
|
1676
|
+
let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
|
|
1677
|
+
while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
|
|
1678
|
+
depth--;
|
|
1679
|
+
dist--;
|
|
1680
|
+
}
|
|
1681
|
+
if (dist > 0) {
|
|
1682
|
+
let next = $from.node(depth).maybeChild($from.indexAfter(depth));
|
|
1683
|
+
while (dist > 0) {
|
|
1684
|
+
if (!next || next.isLeaf)
|
|
1685
|
+
return true;
|
|
1686
|
+
next = next.firstChild;
|
|
1687
|
+
dist--;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
return false;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
function addMark(tr, from, to, mark) {
|
|
1694
|
+
let removed = [], added = [];
|
|
1695
|
+
let removing, adding;
|
|
1696
|
+
tr.doc.nodesBetween(from, to, (node, pos, parent) => {
|
|
1697
|
+
if (!node.isInline)
|
|
1698
|
+
return;
|
|
1699
|
+
let marks = node.marks;
|
|
1700
|
+
if (!mark.isInSet(marks) && parent.type.allowsMarkType(mark.type)) {
|
|
1701
|
+
let start = Math.max(pos, from), end = Math.min(pos + node.nodeSize, to);
|
|
1702
|
+
let newSet = mark.addToSet(marks);
|
|
1703
|
+
for (let i = 0; i < marks.length; i++) {
|
|
1704
|
+
if (!marks[i].isInSet(newSet)) {
|
|
1705
|
+
if (removing && removing.to == start && removing.mark.eq(marks[i]))
|
|
1706
|
+
removing.to = end;
|
|
1707
|
+
else
|
|
1708
|
+
removed.push(removing = new RemoveMarkStep(start, end, marks[i]));
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
if (adding && adding.to == start)
|
|
1712
|
+
adding.to = end;
|
|
1713
|
+
else
|
|
1714
|
+
added.push(adding = new AddMarkStep(start, end, mark));
|
|
1715
|
+
}
|
|
1716
|
+
});
|
|
1717
|
+
removed.forEach(s => tr.step(s));
|
|
1718
|
+
added.forEach(s => tr.step(s));
|
|
1719
|
+
}
|
|
1720
|
+
function removeMark(tr, from, to, mark) {
|
|
1721
|
+
let matched = [], step = 0;
|
|
1722
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
1723
|
+
if (!node.isInline)
|
|
1724
|
+
return;
|
|
1725
|
+
step++;
|
|
1726
|
+
let toRemove = null;
|
|
1727
|
+
if (mark instanceof MarkType) {
|
|
1728
|
+
let set = node.marks, found;
|
|
1729
|
+
while (found = mark.isInSet(set)) {
|
|
1730
|
+
(toRemove || (toRemove = [])).push(found);
|
|
1731
|
+
set = found.removeFromSet(set);
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
else if (mark) {
|
|
1735
|
+
if (mark.isInSet(node.marks))
|
|
1736
|
+
toRemove = [mark];
|
|
1737
|
+
}
|
|
1738
|
+
else {
|
|
1739
|
+
toRemove = node.marks;
|
|
1740
|
+
}
|
|
1741
|
+
if (toRemove && toRemove.length) {
|
|
1742
|
+
let end = Math.min(pos + node.nodeSize, to);
|
|
1743
|
+
for (let i = 0; i < toRemove.length; i++) {
|
|
1744
|
+
let style = toRemove[i], found;
|
|
1745
|
+
for (let j = 0; j < matched.length; j++) {
|
|
1746
|
+
let m = matched[j];
|
|
1747
|
+
if (m.step == step - 1 && style.eq(matched[j].style))
|
|
1748
|
+
found = m;
|
|
1749
|
+
}
|
|
1750
|
+
if (found) {
|
|
1751
|
+
found.to = end;
|
|
1752
|
+
found.step = step;
|
|
1753
|
+
}
|
|
1754
|
+
else {
|
|
1755
|
+
matched.push({ style, from: Math.max(pos, from), to: end, step });
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
});
|
|
1760
|
+
matched.forEach(m => tr.step(new RemoveMarkStep(m.from, m.to, m.style)));
|
|
1761
|
+
}
|
|
1762
|
+
function clearIncompatible(tr, pos, parentType, match = parentType.contentMatch) {
|
|
1763
|
+
let node = tr.doc.nodeAt(pos);
|
|
1764
|
+
let delSteps = [], cur = pos + 1;
|
|
1765
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
1766
|
+
let child = node.child(i), end = cur + child.nodeSize;
|
|
1767
|
+
let allowed = match.matchType(child.type);
|
|
1768
|
+
if (!allowed) {
|
|
1769
|
+
delSteps.push(new ReplaceStep(cur, end, Slice.empty));
|
|
1770
|
+
}
|
|
1771
|
+
else {
|
|
1772
|
+
match = allowed;
|
|
1773
|
+
for (let j = 0; j < child.marks.length; j++)
|
|
1774
|
+
if (!parentType.allowsMarkType(child.marks[j].type))
|
|
1775
|
+
tr.step(new RemoveMarkStep(cur, end, child.marks[j]));
|
|
1776
|
+
}
|
|
1777
|
+
cur = end;
|
|
1778
|
+
}
|
|
1779
|
+
if (!match.validEnd) {
|
|
1780
|
+
let fill = match.fillBefore(Fragment.empty, true);
|
|
1781
|
+
tr.replace(cur, cur, new Slice(fill, 0, 0));
|
|
1782
|
+
}
|
|
1783
|
+
for (let i = delSteps.length - 1; i >= 0; i--)
|
|
1784
|
+
tr.step(delSteps[i]);
|
|
1785
|
+
}
|
|
1786
|
+
function lift(tr, range, target) {
|
|
1787
|
+
let { $from, $to, depth } = range;
|
|
1788
|
+
let gapStart = $from.before(depth + 1), gapEnd = $to.after(depth + 1);
|
|
1789
|
+
let start = gapStart, end = gapEnd;
|
|
1790
|
+
let before = Fragment.empty, openStart = 0;
|
|
1791
|
+
for (let d = depth, splitting = false; d > target; d--)
|
|
1792
|
+
if (splitting || $from.index(d) > 0) {
|
|
1793
|
+
splitting = true;
|
|
1794
|
+
before = Fragment.from($from.node(d).copy(before));
|
|
1795
|
+
openStart++;
|
|
1796
|
+
}
|
|
1797
|
+
else {
|
|
1798
|
+
start--;
|
|
1799
|
+
}
|
|
1800
|
+
let after = Fragment.empty, openEnd = 0;
|
|
1801
|
+
for (let d = depth, splitting = false; d > target; d--)
|
|
1802
|
+
if (splitting || $to.after(d + 1) < $to.end(d)) {
|
|
1803
|
+
splitting = true;
|
|
1804
|
+
after = Fragment.from($to.node(d).copy(after));
|
|
1805
|
+
openEnd++;
|
|
1806
|
+
}
|
|
1807
|
+
else {
|
|
1808
|
+
end++;
|
|
1809
|
+
}
|
|
1810
|
+
tr.step(new ReplaceAroundStep(start, end, gapStart, gapEnd, new Slice(before.append(after), openStart, openEnd), before.size - openStart, true));
|
|
1811
|
+
}
|
|
1812
|
+
function wrap(tr, range, wrappers) {
|
|
1813
|
+
let content = Fragment.empty;
|
|
1814
|
+
for (let i = wrappers.length - 1; i >= 0; i--) {
|
|
1815
|
+
if (content.size) {
|
|
1816
|
+
let match = wrappers[i].type.contentMatch.matchFragment(content);
|
|
1817
|
+
if (!match || !match.validEnd)
|
|
1818
|
+
throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper");
|
|
1819
|
+
}
|
|
1820
|
+
content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
|
|
1821
|
+
}
|
|
1822
|
+
let start = range.start, end = range.end;
|
|
1823
|
+
tr.step(new ReplaceAroundStep(start, end, start, end, new Slice(content, 0, 0), wrappers.length, true));
|
|
1824
|
+
}
|
|
1825
|
+
function setBlockType(tr, from, to, type, attrs) {
|
|
1826
|
+
if (!type.isTextblock)
|
|
1827
|
+
throw new RangeError("Type given to setBlockType should be a textblock");
|
|
1828
|
+
let mapFrom = tr.steps.length;
|
|
1829
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
1830
|
+
if (node.isTextblock && !node.hasMarkup(type, attrs) && canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
|
|
1831
|
+
// Ensure all markup that isn't allowed in the new node type is cleared
|
|
1832
|
+
tr.clearIncompatible(tr.mapping.slice(mapFrom).map(pos, 1), type);
|
|
1833
|
+
let mapping = tr.mapping.slice(mapFrom);
|
|
1834
|
+
let startM = mapping.map(pos, 1), endM = mapping.map(pos + node.nodeSize, 1);
|
|
1835
|
+
tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1, new Slice(Fragment.from(type.create(attrs, null, node.marks)), 0, 0), 1, true));
|
|
1836
|
+
return false;
|
|
1837
|
+
}
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
function canChangeType(doc, pos, type) {
|
|
1841
|
+
let $pos = doc.resolve(pos), index = $pos.index();
|
|
1842
|
+
return $pos.parent.canReplaceWith(index, index + 1, type);
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
Change the type, attributes, and/or marks of the node at `pos`.
|
|
1846
|
+
When `type` isn't given, the existing node type is preserved,
|
|
1847
|
+
*/
|
|
1848
|
+
function setNodeMarkup(tr, pos, type, attrs, marks) {
|
|
1849
|
+
let node = tr.doc.nodeAt(pos);
|
|
1850
|
+
if (!node)
|
|
1851
|
+
throw new RangeError("No node at given position");
|
|
1852
|
+
if (!type)
|
|
1853
|
+
type = node.type;
|
|
1854
|
+
let newNode = type.create(attrs, null, marks || node.marks);
|
|
1855
|
+
if (node.isLeaf)
|
|
1856
|
+
return tr.replaceWith(pos, pos + node.nodeSize, newNode);
|
|
1857
|
+
if (!type.validContent(node.content))
|
|
1858
|
+
throw new RangeError("Invalid content for node type " + type.name);
|
|
1859
|
+
tr.step(new ReplaceAroundStep(pos, pos + node.nodeSize, pos + 1, pos + node.nodeSize - 1, new Slice(Fragment.from(newNode), 0, 0), 1, true));
|
|
1860
|
+
}
|
|
1861
|
+
function split(tr, pos, depth = 1, typesAfter) {
|
|
1862
|
+
let $pos = tr.doc.resolve(pos), before = Fragment.empty, after = Fragment.empty;
|
|
1863
|
+
for (let d = $pos.depth, e = $pos.depth - depth, i = depth - 1; d > e; d--, i--) {
|
|
1864
|
+
before = Fragment.from($pos.node(d).copy(before));
|
|
1865
|
+
let typeAfter = typesAfter && typesAfter[i];
|
|
1866
|
+
after = Fragment.from(typeAfter ? typeAfter.type.create(typeAfter.attrs, after) : $pos.node(d).copy(after));
|
|
1867
|
+
}
|
|
1868
|
+
tr.step(new ReplaceStep(pos, pos, new Slice(before.append(after), depth, depth), true));
|
|
1869
|
+
}
|
|
1870
|
+
function join(tr, pos, depth) {
|
|
1871
|
+
let step = new ReplaceStep(pos - depth, pos + depth, Slice.empty, true);
|
|
1872
|
+
tr.step(step);
|
|
1873
|
+
}
|
|
1874
|
+
/**
|
|
1875
|
+
Try to find a point where a node of the given type can be inserted
|
|
1876
|
+
near `pos`, by searching up the node hierarchy when `pos` itself
|
|
1877
|
+
isn't a valid place but is at the start or end of a node. Return
|
|
1878
|
+
null if no position was found.
|
|
1879
|
+
*/
|
|
1880
|
+
function insertPoint(doc, pos, nodeType) {
|
|
1881
|
+
let $pos = doc.resolve(pos);
|
|
1882
|
+
if ($pos.parent.canReplaceWith($pos.index(), $pos.index(), nodeType))
|
|
1883
|
+
return pos;
|
|
1884
|
+
if ($pos.parentOffset == 0)
|
|
1885
|
+
for (let d = $pos.depth - 1; d >= 0; d--) {
|
|
1886
|
+
let index = $pos.index(d);
|
|
1887
|
+
if ($pos.node(d).canReplaceWith(index, index, nodeType))
|
|
1888
|
+
return $pos.before(d + 1);
|
|
1889
|
+
if (index > 0)
|
|
1890
|
+
return null;
|
|
1891
|
+
}
|
|
1892
|
+
if ($pos.parentOffset == $pos.parent.content.size)
|
|
1893
|
+
for (let d = $pos.depth - 1; d >= 0; d--) {
|
|
1894
|
+
let index = $pos.indexAfter(d);
|
|
1895
|
+
if ($pos.node(d).canReplaceWith(index, index, nodeType))
|
|
1896
|
+
return $pos.after(d + 1);
|
|
1897
|
+
if (index < $pos.node(d).childCount)
|
|
1898
|
+
return null;
|
|
1899
|
+
}
|
|
1900
|
+
return null;
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
/**
|
|
1904
|
+
‘Fit’ a slice into a given position in the document, producing a
|
|
1905
|
+
[step](https://prosemirror.net/docs/ref/#transform.Step) that inserts it. Will return null if
|
|
1906
|
+
there's no meaningful way to insert the slice here, or inserting it
|
|
1907
|
+
would be a no-op (an empty slice over an empty range).
|
|
1908
|
+
*/
|
|
1909
|
+
function replaceStep(doc, from, to = from, slice = Slice.empty) {
|
|
1910
|
+
if (from == to && !slice.size)
|
|
1911
|
+
return null;
|
|
1912
|
+
let $from = doc.resolve(from), $to = doc.resolve(to);
|
|
1913
|
+
// Optimization -- avoid work if it's obvious that it's not needed.
|
|
1914
|
+
if (fitsTrivially($from, $to, slice))
|
|
1915
|
+
return new ReplaceStep(from, to, slice);
|
|
1916
|
+
return new Fitter($from, $to, slice).fit();
|
|
1917
|
+
}
|
|
1918
|
+
function fitsTrivially($from, $to, slice) {
|
|
1919
|
+
return !slice.openStart && !slice.openEnd && $from.start() == $to.start() &&
|
|
1920
|
+
$from.parent.canReplace($from.index(), $to.index(), slice.content);
|
|
1921
|
+
}
|
|
1922
|
+
// Algorithm for 'placing' the elements of a slice into a gap:
|
|
1923
|
+
//
|
|
1924
|
+
// We consider the content of each node that is open to the left to be
|
|
1925
|
+
// independently placeable. I.e. in <p("foo"), p("bar")>, when the
|
|
1926
|
+
// paragraph on the left is open, "foo" can be placed (somewhere on
|
|
1927
|
+
// the left side of the replacement gap) independently from p("bar").
|
|
1928
|
+
//
|
|
1929
|
+
// This class tracks the state of the placement progress in the
|
|
1930
|
+
// following properties:
|
|
1931
|
+
//
|
|
1932
|
+
// - `frontier` holds a stack of `{type, match}` objects that
|
|
1933
|
+
// represent the open side of the replacement. It starts at
|
|
1934
|
+
// `$from`, then moves forward as content is placed, and is finally
|
|
1935
|
+
// reconciled with `$to`.
|
|
1936
|
+
//
|
|
1937
|
+
// - `unplaced` is a slice that represents the content that hasn't
|
|
1938
|
+
// been placed yet.
|
|
1939
|
+
//
|
|
1940
|
+
// - `placed` is a fragment of placed content. Its open-start value
|
|
1941
|
+
// is implicit in `$from`, and its open-end value in `frontier`.
|
|
1942
|
+
class Fitter {
|
|
1943
|
+
constructor($from, $to, unplaced) {
|
|
1944
|
+
this.$from = $from;
|
|
1945
|
+
this.$to = $to;
|
|
1946
|
+
this.unplaced = unplaced;
|
|
1947
|
+
this.frontier = [];
|
|
1948
|
+
this.placed = Fragment.empty;
|
|
1949
|
+
for (let i = 0; i <= $from.depth; i++) {
|
|
1950
|
+
let node = $from.node(i);
|
|
1951
|
+
this.frontier.push({
|
|
1952
|
+
type: node.type,
|
|
1953
|
+
match: node.contentMatchAt($from.indexAfter(i))
|
|
1954
|
+
});
|
|
1955
|
+
}
|
|
1956
|
+
for (let i = $from.depth; i > 0; i--)
|
|
1957
|
+
this.placed = Fragment.from($from.node(i).copy(this.placed));
|
|
1958
|
+
}
|
|
1959
|
+
get depth() { return this.frontier.length - 1; }
|
|
1960
|
+
fit() {
|
|
1961
|
+
// As long as there's unplaced content, try to place some of it.
|
|
1962
|
+
// If that fails, either increase the open score of the unplaced
|
|
1963
|
+
// slice, or drop nodes from it, and then try again.
|
|
1964
|
+
while (this.unplaced.size) {
|
|
1965
|
+
let fit = this.findFittable();
|
|
1966
|
+
if (fit)
|
|
1967
|
+
this.placeNodes(fit);
|
|
1968
|
+
else
|
|
1969
|
+
this.openMore() || this.dropNode();
|
|
1970
|
+
}
|
|
1971
|
+
// When there's inline content directly after the frontier _and_
|
|
1972
|
+
// directly after `this.$to`, we must generate a `ReplaceAround`
|
|
1973
|
+
// step that pulls that content into the node after the frontier.
|
|
1974
|
+
// That means the fitting must be done to the end of the textblock
|
|
1975
|
+
// node after `this.$to`, not `this.$to` itself.
|
|
1976
|
+
let moveInline = this.mustMoveInline(), placedSize = this.placed.size - this.depth - this.$from.depth;
|
|
1977
|
+
let $from = this.$from, $to = this.close(moveInline < 0 ? this.$to : $from.doc.resolve(moveInline));
|
|
1978
|
+
if (!$to)
|
|
1979
|
+
return null;
|
|
1980
|
+
// If closing to `$to` succeeded, create a step
|
|
1981
|
+
let content = this.placed, openStart = $from.depth, openEnd = $to.depth;
|
|
1982
|
+
while (openStart && openEnd && content.childCount == 1) { // Normalize by dropping open parent nodes
|
|
1983
|
+
content = content.firstChild.content;
|
|
1984
|
+
openStart--;
|
|
1985
|
+
openEnd--;
|
|
1986
|
+
}
|
|
1987
|
+
let slice = new Slice(content, openStart, openEnd);
|
|
1988
|
+
if (moveInline > -1)
|
|
1989
|
+
return new ReplaceAroundStep($from.pos, moveInline, this.$to.pos, this.$to.end(), slice, placedSize);
|
|
1990
|
+
if (slice.size || $from.pos != this.$to.pos) // Don't generate no-op steps
|
|
1991
|
+
return new ReplaceStep($from.pos, $to.pos, slice);
|
|
1992
|
+
return null;
|
|
1993
|
+
}
|
|
1994
|
+
// Find a position on the start spine of `this.unplaced` that has
|
|
1995
|
+
// content that can be moved somewhere on the frontier. Returns two
|
|
1996
|
+
// depths, one for the slice and one for the frontier.
|
|
1997
|
+
findFittable() {
|
|
1998
|
+
// Only try wrapping nodes (pass 2) after finding a place without
|
|
1999
|
+
// wrapping failed.
|
|
2000
|
+
for (let pass = 1; pass <= 2; pass++) {
|
|
2001
|
+
for (let sliceDepth = this.unplaced.openStart; sliceDepth >= 0; sliceDepth--) {
|
|
2002
|
+
let fragment, parent = null;
|
|
2003
|
+
if (sliceDepth) {
|
|
2004
|
+
parent = contentAt(this.unplaced.content, sliceDepth - 1).firstChild;
|
|
2005
|
+
fragment = parent.content;
|
|
2006
|
+
}
|
|
2007
|
+
else {
|
|
2008
|
+
fragment = this.unplaced.content;
|
|
2009
|
+
}
|
|
2010
|
+
let first = fragment.firstChild;
|
|
2011
|
+
for (let frontierDepth = this.depth; frontierDepth >= 0; frontierDepth--) {
|
|
2012
|
+
let { type, match } = this.frontier[frontierDepth], wrap, inject = null;
|
|
2013
|
+
// In pass 1, if the next node matches, or there is no next
|
|
2014
|
+
// node but the parents look compatible, we've found a
|
|
2015
|
+
// place.
|
|
2016
|
+
if (pass == 1 && (first ? match.matchType(first.type) || (inject = match.fillBefore(Fragment.from(first), false))
|
|
2017
|
+
: parent && type.compatibleContent(parent.type)))
|
|
2018
|
+
return { sliceDepth, frontierDepth, parent, inject };
|
|
2019
|
+
// In pass 2, look for a set of wrapping nodes that make
|
|
2020
|
+
// `first` fit here.
|
|
2021
|
+
else if (pass == 2 && first && (wrap = match.findWrapping(first.type)))
|
|
2022
|
+
return { sliceDepth, frontierDepth, parent, wrap };
|
|
2023
|
+
// Don't continue looking further up if the parent node
|
|
2024
|
+
// would fit here.
|
|
2025
|
+
if (parent && match.matchType(parent.type))
|
|
2026
|
+
break;
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
openMore() {
|
|
2032
|
+
let { content, openStart, openEnd } = this.unplaced;
|
|
2033
|
+
let inner = contentAt(content, openStart);
|
|
2034
|
+
if (!inner.childCount || inner.firstChild.isLeaf)
|
|
2035
|
+
return false;
|
|
2036
|
+
this.unplaced = new Slice(content, openStart + 1, Math.max(openEnd, inner.size + openStart >= content.size - openEnd ? openStart + 1 : 0));
|
|
2037
|
+
return true;
|
|
2038
|
+
}
|
|
2039
|
+
dropNode() {
|
|
2040
|
+
let { content, openStart, openEnd } = this.unplaced;
|
|
2041
|
+
let inner = contentAt(content, openStart);
|
|
2042
|
+
if (inner.childCount <= 1 && openStart > 0) {
|
|
2043
|
+
let openAtEnd = content.size - openStart <= openStart + inner.size;
|
|
2044
|
+
this.unplaced = new Slice(dropFromFragment(content, openStart - 1, 1), openStart - 1, openAtEnd ? openStart - 1 : openEnd);
|
|
2045
|
+
}
|
|
2046
|
+
else {
|
|
2047
|
+
this.unplaced = new Slice(dropFromFragment(content, openStart, 1), openStart, openEnd);
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
// Move content from the unplaced slice at `sliceDepth` to the
|
|
2051
|
+
// frontier node at `frontierDepth`. Close that frontier node when
|
|
2052
|
+
// applicable.
|
|
2053
|
+
placeNodes({ sliceDepth, frontierDepth, parent, inject, wrap }) {
|
|
2054
|
+
while (this.depth > frontierDepth)
|
|
2055
|
+
this.closeFrontierNode();
|
|
2056
|
+
if (wrap)
|
|
2057
|
+
for (let i = 0; i < wrap.length; i++)
|
|
2058
|
+
this.openFrontierNode(wrap[i]);
|
|
2059
|
+
let slice = this.unplaced, fragment = parent ? parent.content : slice.content;
|
|
2060
|
+
let openStart = slice.openStart - sliceDepth;
|
|
2061
|
+
let taken = 0, add = [];
|
|
2062
|
+
let { match, type } = this.frontier[frontierDepth];
|
|
2063
|
+
if (inject) {
|
|
2064
|
+
for (let i = 0; i < inject.childCount; i++)
|
|
2065
|
+
add.push(inject.child(i));
|
|
2066
|
+
match = match.matchFragment(inject);
|
|
2067
|
+
}
|
|
2068
|
+
// Computes the amount of (end) open nodes at the end of the
|
|
2069
|
+
// fragment. When 0, the parent is open, but no more. When
|
|
2070
|
+
// negative, nothing is open.
|
|
2071
|
+
let openEndCount = (fragment.size + sliceDepth) - (slice.content.size - slice.openEnd);
|
|
2072
|
+
// Scan over the fragment, fitting as many child nodes as
|
|
2073
|
+
// possible.
|
|
2074
|
+
while (taken < fragment.childCount) {
|
|
2075
|
+
let next = fragment.child(taken), matches = match.matchType(next.type);
|
|
2076
|
+
if (!matches)
|
|
2077
|
+
break;
|
|
2078
|
+
taken++;
|
|
2079
|
+
if (taken > 1 || openStart == 0 || next.content.size) { // Drop empty open nodes
|
|
2080
|
+
match = matches;
|
|
2081
|
+
add.push(closeNodeStart(next.mark(type.allowedMarks(next.marks)), taken == 1 ? openStart : 0, taken == fragment.childCount ? openEndCount : -1));
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
let toEnd = taken == fragment.childCount;
|
|
2085
|
+
if (!toEnd)
|
|
2086
|
+
openEndCount = -1;
|
|
2087
|
+
this.placed = addToFragment(this.placed, frontierDepth, Fragment.from(add));
|
|
2088
|
+
this.frontier[frontierDepth].match = match;
|
|
2089
|
+
// If the parent types match, and the entire node was moved, and
|
|
2090
|
+
// it's not open, close this frontier node right away.
|
|
2091
|
+
if (toEnd && openEndCount < 0 && parent && parent.type == this.frontier[this.depth].type && this.frontier.length > 1)
|
|
2092
|
+
this.closeFrontierNode();
|
|
2093
|
+
// Add new frontier nodes for any open nodes at the end.
|
|
2094
|
+
for (let i = 0, cur = fragment; i < openEndCount; i++) {
|
|
2095
|
+
let node = cur.lastChild;
|
|
2096
|
+
this.frontier.push({ type: node.type, match: node.contentMatchAt(node.childCount) });
|
|
2097
|
+
cur = node.content;
|
|
2098
|
+
}
|
|
2099
|
+
// Update `this.unplaced`. Drop the entire node from which we
|
|
2100
|
+
// placed it we got to its end, otherwise just drop the placed
|
|
2101
|
+
// nodes.
|
|
2102
|
+
this.unplaced = !toEnd ? new Slice(dropFromFragment(slice.content, sliceDepth, taken), slice.openStart, slice.openEnd)
|
|
2103
|
+
: sliceDepth == 0 ? Slice.empty
|
|
2104
|
+
: new Slice(dropFromFragment(slice.content, sliceDepth - 1, 1), sliceDepth - 1, openEndCount < 0 ? slice.openEnd : sliceDepth - 1);
|
|
2105
|
+
}
|
|
2106
|
+
mustMoveInline() {
|
|
2107
|
+
if (!this.$to.parent.isTextblock)
|
|
2108
|
+
return -1;
|
|
2109
|
+
let top = this.frontier[this.depth], level;
|
|
2110
|
+
if (!top.type.isTextblock || !contentAfterFits(this.$to, this.$to.depth, top.type, top.match, false) ||
|
|
2111
|
+
(this.$to.depth == this.depth && (level = this.findCloseLevel(this.$to)) && level.depth == this.depth))
|
|
2112
|
+
return -1;
|
|
2113
|
+
let { depth } = this.$to, after = this.$to.after(depth);
|
|
2114
|
+
while (depth > 1 && after == this.$to.end(--depth))
|
|
2115
|
+
++after;
|
|
2116
|
+
return after;
|
|
2117
|
+
}
|
|
2118
|
+
findCloseLevel($to) {
|
|
2119
|
+
scan: for (let i = Math.min(this.depth, $to.depth); i >= 0; i--) {
|
|
2120
|
+
let { match, type } = this.frontier[i];
|
|
2121
|
+
let dropInner = i < $to.depth && $to.end(i + 1) == $to.pos + ($to.depth - (i + 1));
|
|
2122
|
+
let fit = contentAfterFits($to, i, type, match, dropInner);
|
|
2123
|
+
if (!fit)
|
|
2124
|
+
continue;
|
|
2125
|
+
for (let d = i - 1; d >= 0; d--) {
|
|
2126
|
+
let { match, type } = this.frontier[d];
|
|
2127
|
+
let matches = contentAfterFits($to, d, type, match, true);
|
|
2128
|
+
if (!matches || matches.childCount)
|
|
2129
|
+
continue scan;
|
|
2130
|
+
}
|
|
2131
|
+
return { depth: i, fit, move: dropInner ? $to.doc.resolve($to.after(i + 1)) : $to };
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
close($to) {
|
|
2135
|
+
let close = this.findCloseLevel($to);
|
|
2136
|
+
if (!close)
|
|
2137
|
+
return null;
|
|
2138
|
+
while (this.depth > close.depth)
|
|
2139
|
+
this.closeFrontierNode();
|
|
2140
|
+
if (close.fit.childCount)
|
|
2141
|
+
this.placed = addToFragment(this.placed, close.depth, close.fit);
|
|
2142
|
+
$to = close.move;
|
|
2143
|
+
for (let d = close.depth + 1; d <= $to.depth; d++) {
|
|
2144
|
+
let node = $to.node(d), add = node.type.contentMatch.fillBefore(node.content, true, $to.index(d));
|
|
2145
|
+
this.openFrontierNode(node.type, node.attrs, add);
|
|
2146
|
+
}
|
|
2147
|
+
return $to;
|
|
2148
|
+
}
|
|
2149
|
+
openFrontierNode(type, attrs = null, content) {
|
|
2150
|
+
let top = this.frontier[this.depth];
|
|
2151
|
+
top.match = top.match.matchType(type);
|
|
2152
|
+
this.placed = addToFragment(this.placed, this.depth, Fragment.from(type.create(attrs, content)));
|
|
2153
|
+
this.frontier.push({ type, match: type.contentMatch });
|
|
2154
|
+
}
|
|
2155
|
+
closeFrontierNode() {
|
|
2156
|
+
let open = this.frontier.pop();
|
|
2157
|
+
let add = open.match.fillBefore(Fragment.empty, true);
|
|
2158
|
+
if (add.childCount)
|
|
2159
|
+
this.placed = addToFragment(this.placed, this.frontier.length, add);
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
function dropFromFragment(fragment, depth, count) {
|
|
2163
|
+
if (depth == 0)
|
|
2164
|
+
return fragment.cutByIndex(count, fragment.childCount);
|
|
2165
|
+
return fragment.replaceChild(0, fragment.firstChild.copy(dropFromFragment(fragment.firstChild.content, depth - 1, count)));
|
|
2166
|
+
}
|
|
2167
|
+
function addToFragment(fragment, depth, content) {
|
|
2168
|
+
if (depth == 0)
|
|
2169
|
+
return fragment.append(content);
|
|
2170
|
+
return fragment.replaceChild(fragment.childCount - 1, fragment.lastChild.copy(addToFragment(fragment.lastChild.content, depth - 1, content)));
|
|
2171
|
+
}
|
|
2172
|
+
function contentAt(fragment, depth) {
|
|
2173
|
+
for (let i = 0; i < depth; i++)
|
|
2174
|
+
fragment = fragment.firstChild.content;
|
|
2175
|
+
return fragment;
|
|
2176
|
+
}
|
|
2177
|
+
function closeNodeStart(node, openStart, openEnd) {
|
|
2178
|
+
if (openStart <= 0)
|
|
2179
|
+
return node;
|
|
2180
|
+
let frag = node.content;
|
|
2181
|
+
if (openStart > 1)
|
|
2182
|
+
frag = frag.replaceChild(0, closeNodeStart(frag.firstChild, openStart - 1, frag.childCount == 1 ? openEnd - 1 : 0));
|
|
2183
|
+
if (openStart > 0) {
|
|
2184
|
+
frag = node.type.contentMatch.fillBefore(frag).append(frag);
|
|
2185
|
+
if (openEnd <= 0)
|
|
2186
|
+
frag = frag.append(node.type.contentMatch.matchFragment(frag).fillBefore(Fragment.empty, true));
|
|
2187
|
+
}
|
|
2188
|
+
return node.copy(frag);
|
|
2189
|
+
}
|
|
2190
|
+
function contentAfterFits($to, depth, type, match, open) {
|
|
2191
|
+
let node = $to.node(depth), index = open ? $to.indexAfter(depth) : $to.index(depth);
|
|
2192
|
+
if (index == node.childCount && !type.compatibleContent(node.type))
|
|
2193
|
+
return null;
|
|
2194
|
+
let fit = match.fillBefore(node.content, true, index);
|
|
2195
|
+
return fit && !invalidMarks(type, node.content, index) ? fit : null;
|
|
2196
|
+
}
|
|
2197
|
+
function invalidMarks(type, fragment, start) {
|
|
2198
|
+
for (let i = start; i < fragment.childCount; i++)
|
|
2199
|
+
if (!type.allowsMarks(fragment.child(i).marks))
|
|
174
2200
|
return true;
|
|
175
|
-
|
|
2201
|
+
return false;
|
|
2202
|
+
}
|
|
2203
|
+
function definesContent(type) {
|
|
2204
|
+
return type.spec.defining || type.spec.definingForContent;
|
|
2205
|
+
}
|
|
2206
|
+
function replaceRange(tr, from, to, slice) {
|
|
2207
|
+
if (!slice.size)
|
|
2208
|
+
return tr.deleteRange(from, to);
|
|
2209
|
+
let $from = tr.doc.resolve(from), $to = tr.doc.resolve(to);
|
|
2210
|
+
if (fitsTrivially($from, $to, slice))
|
|
2211
|
+
return tr.step(new ReplaceStep(from, to, slice));
|
|
2212
|
+
let targetDepths = coveredDepths($from, tr.doc.resolve(to));
|
|
2213
|
+
// Can't replace the whole document, so remove 0 if it's present
|
|
2214
|
+
if (targetDepths[targetDepths.length - 1] == 0)
|
|
2215
|
+
targetDepths.pop();
|
|
2216
|
+
// Negative numbers represent not expansion over the whole node at
|
|
2217
|
+
// that depth, but replacing from $from.before(-D) to $to.pos.
|
|
2218
|
+
let preferredTarget = -($from.depth + 1);
|
|
2219
|
+
targetDepths.unshift(preferredTarget);
|
|
2220
|
+
// This loop picks a preferred target depth, if one of the covering
|
|
2221
|
+
// depths is not outside of a defining node, and adds negative
|
|
2222
|
+
// depths for any depth that has $from at its start and does not
|
|
2223
|
+
// cross a defining node.
|
|
2224
|
+
for (let d = $from.depth, pos = $from.pos - 1; d > 0; d--, pos--) {
|
|
2225
|
+
let spec = $from.node(d).type.spec;
|
|
2226
|
+
if (spec.defining || spec.definingAsContext || spec.isolating)
|
|
2227
|
+
break;
|
|
2228
|
+
if (targetDepths.indexOf(d) > -1)
|
|
2229
|
+
preferredTarget = d;
|
|
2230
|
+
else if ($from.before(d) == pos)
|
|
2231
|
+
targetDepths.splice(1, 0, -d);
|
|
2232
|
+
}
|
|
2233
|
+
// Try to fit each possible depth of the slice into each possible
|
|
2234
|
+
// target depth, starting with the preferred depths.
|
|
2235
|
+
let preferredTargetIndex = targetDepths.indexOf(preferredTarget);
|
|
2236
|
+
let leftNodes = [], preferredDepth = slice.openStart;
|
|
2237
|
+
for (let content = slice.content, i = 0;; i++) {
|
|
2238
|
+
let node = content.firstChild;
|
|
2239
|
+
leftNodes.push(node);
|
|
2240
|
+
if (i == slice.openStart)
|
|
2241
|
+
break;
|
|
2242
|
+
content = node.content;
|
|
2243
|
+
}
|
|
2244
|
+
// Back up preferredDepth to cover defining textblocks directly
|
|
2245
|
+
// above it, possibly skipping a non-defining textblock.
|
|
2246
|
+
for (let d = preferredDepth - 1; d >= 0; d--) {
|
|
2247
|
+
let type = leftNodes[d].type, def = definesContent(type);
|
|
2248
|
+
if (def && $from.node(preferredTargetIndex).type != type)
|
|
2249
|
+
preferredDepth = d;
|
|
2250
|
+
else if (def || !type.isTextblock)
|
|
2251
|
+
break;
|
|
2252
|
+
}
|
|
2253
|
+
for (let j = slice.openStart; j >= 0; j--) {
|
|
2254
|
+
let openDepth = (j + preferredDepth + 1) % (slice.openStart + 1);
|
|
2255
|
+
let insert = leftNodes[openDepth];
|
|
2256
|
+
if (!insert)
|
|
2257
|
+
continue;
|
|
2258
|
+
for (let i = 0; i < targetDepths.length; i++) {
|
|
2259
|
+
// Loop over possible expansion levels, starting with the
|
|
2260
|
+
// preferred one
|
|
2261
|
+
let targetDepth = targetDepths[(i + preferredTargetIndex) % targetDepths.length], expand = true;
|
|
2262
|
+
if (targetDepth < 0) {
|
|
2263
|
+
expand = false;
|
|
2264
|
+
targetDepth = -targetDepth;
|
|
2265
|
+
}
|
|
2266
|
+
let parent = $from.node(targetDepth - 1), index = $from.index(targetDepth - 1);
|
|
2267
|
+
if (parent.canReplaceWith(index, index, insert.type, insert.marks))
|
|
2268
|
+
return tr.replace($from.before(targetDepth), expand ? $to.after(targetDepth) : to, new Slice(closeFragment(slice.content, 0, slice.openStart, openDepth), openDepth, slice.openEnd));
|
|
176
2269
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
2270
|
+
}
|
|
2271
|
+
let startSteps = tr.steps.length;
|
|
2272
|
+
for (let i = targetDepths.length - 1; i >= 0; i--) {
|
|
2273
|
+
tr.replace(from, to, slice);
|
|
2274
|
+
if (tr.steps.length > startSteps)
|
|
2275
|
+
break;
|
|
2276
|
+
let depth = targetDepths[i];
|
|
2277
|
+
if (depth < 0)
|
|
2278
|
+
continue;
|
|
2279
|
+
from = $from.before(depth);
|
|
2280
|
+
to = $to.after(depth);
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
function closeFragment(fragment, depth, oldOpen, newOpen, parent) {
|
|
2284
|
+
if (depth < oldOpen) {
|
|
2285
|
+
let first = fragment.firstChild;
|
|
2286
|
+
fragment = fragment.replaceChild(0, first.copy(closeFragment(first.content, depth + 1, oldOpen, newOpen, first)));
|
|
2287
|
+
}
|
|
2288
|
+
if (depth > newOpen) {
|
|
2289
|
+
let match = parent.contentMatchAt(0);
|
|
2290
|
+
let start = match.fillBefore(fragment).append(fragment);
|
|
2291
|
+
fragment = start.append(match.matchFragment(start).fillBefore(Fragment.empty, true));
|
|
2292
|
+
}
|
|
2293
|
+
return fragment;
|
|
2294
|
+
}
|
|
2295
|
+
function replaceRangeWith(tr, from, to, node) {
|
|
2296
|
+
if (!node.isInline && from == to && tr.doc.resolve(from).parent.content.size) {
|
|
2297
|
+
let point = insertPoint(tr.doc, from, node.type);
|
|
2298
|
+
if (point != null)
|
|
2299
|
+
from = to = point;
|
|
2300
|
+
}
|
|
2301
|
+
tr.replaceRange(from, to, new Slice(Fragment.from(node), 0, 0));
|
|
2302
|
+
}
|
|
2303
|
+
function deleteRange(tr, from, to) {
|
|
2304
|
+
let $from = tr.doc.resolve(from), $to = tr.doc.resolve(to);
|
|
2305
|
+
let covered = coveredDepths($from, $to);
|
|
2306
|
+
for (let i = 0; i < covered.length; i++) {
|
|
2307
|
+
let depth = covered[i], last = i == covered.length - 1;
|
|
2308
|
+
if ((last && depth == 0) || $from.node(depth).type.contentMatch.validEnd)
|
|
2309
|
+
return tr.delete($from.start(depth), $to.end(depth));
|
|
2310
|
+
if (depth > 0 && (last || $from.node(depth - 1).canReplace($from.index(depth - 1), $to.indexAfter(depth - 1))))
|
|
2311
|
+
return tr.delete($from.before(depth), $to.after(depth));
|
|
2312
|
+
}
|
|
2313
|
+
for (let d = 1; d <= $from.depth && d <= $to.depth; d++) {
|
|
2314
|
+
if (from - $from.start(d) == $from.depth - d && to > $from.end(d) && $to.end(d) - to != $to.depth - d)
|
|
2315
|
+
return tr.delete($from.before(d), to);
|
|
2316
|
+
}
|
|
2317
|
+
tr.delete(from, to);
|
|
2318
|
+
}
|
|
2319
|
+
// Returns an array of all depths for which $from - $to spans the
|
|
2320
|
+
// whole content of the nodes at that depth.
|
|
2321
|
+
function coveredDepths($from, $to) {
|
|
2322
|
+
let result = [], minDepth = Math.min($from.depth, $to.depth);
|
|
2323
|
+
for (let d = minDepth; d >= 0; d--) {
|
|
2324
|
+
let start = $from.start(d);
|
|
2325
|
+
if (start < $from.pos - ($from.depth - d) ||
|
|
2326
|
+
$to.end(d) > $to.pos + ($to.depth - d) ||
|
|
2327
|
+
$from.node(d).type.spec.isolating ||
|
|
2328
|
+
$to.node(d).type.spec.isolating)
|
|
2329
|
+
break;
|
|
2330
|
+
if (start == $to.start(d) ||
|
|
2331
|
+
(d == $from.depth && d == $to.depth && $from.parent.inlineContent && $to.parent.inlineContent &&
|
|
2332
|
+
d && $to.start(d - 1) == start - 1))
|
|
2333
|
+
result.push(d);
|
|
2334
|
+
}
|
|
2335
|
+
return result;
|
|
2336
|
+
}
|
|
181
2337
|
|
|
182
|
-
|
|
183
|
-
|
|
2338
|
+
/**
|
|
2339
|
+
Update an attribute in a specific node.
|
|
2340
|
+
*/
|
|
2341
|
+
class AttrStep extends Step {
|
|
2342
|
+
/**
|
|
2343
|
+
Construct an attribute step.
|
|
2344
|
+
*/
|
|
2345
|
+
constructor(
|
|
2346
|
+
/**
|
|
2347
|
+
The position of the target node.
|
|
2348
|
+
*/
|
|
2349
|
+
pos,
|
|
2350
|
+
/**
|
|
2351
|
+
The attribute to set.
|
|
2352
|
+
*/
|
|
2353
|
+
attr,
|
|
2354
|
+
// The attribute's new value.
|
|
2355
|
+
value) {
|
|
2356
|
+
super();
|
|
2357
|
+
this.pos = pos;
|
|
2358
|
+
this.attr = attr;
|
|
2359
|
+
this.value = value;
|
|
2360
|
+
}
|
|
2361
|
+
apply(doc) {
|
|
2362
|
+
let node = doc.nodeAt(this.pos);
|
|
2363
|
+
if (!node)
|
|
2364
|
+
return StepResult.fail("No node at attribute step's position");
|
|
2365
|
+
let attrs = Object.create(null);
|
|
2366
|
+
for (let name in node.attrs)
|
|
2367
|
+
attrs[name] = node.attrs[name];
|
|
2368
|
+
attrs[this.attr] = this.value;
|
|
2369
|
+
let updated = node.type.create(attrs, null, node.marks);
|
|
2370
|
+
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
|
|
2371
|
+
}
|
|
2372
|
+
getMap() {
|
|
2373
|
+
return StepMap.empty;
|
|
2374
|
+
}
|
|
2375
|
+
invert(doc) {
|
|
2376
|
+
return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
|
|
2377
|
+
}
|
|
2378
|
+
map(mapping) {
|
|
2379
|
+
let pos = mapping.mapResult(this.pos, 1);
|
|
2380
|
+
return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
|
|
2381
|
+
}
|
|
2382
|
+
toJSON() {
|
|
2383
|
+
return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
|
|
2384
|
+
}
|
|
2385
|
+
static fromJSON(schema, json) {
|
|
2386
|
+
if (typeof json.pos != "number" || typeof json.attr != "string")
|
|
2387
|
+
throw new RangeError("Invalid input for AttrStep.fromJSON");
|
|
2388
|
+
return new AttrStep(json.pos, json.attr, json.value);
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
Step.jsonID("attr", AttrStep);
|
|
184
2392
|
|
|
2393
|
+
/**
|
|
2394
|
+
@internal
|
|
2395
|
+
*/
|
|
2396
|
+
let TransformError = class extends Error {
|
|
2397
|
+
};
|
|
2398
|
+
TransformError = function TransformError(message) {
|
|
2399
|
+
let err = Error.call(this, message);
|
|
2400
|
+
err.__proto__ = TransformError.prototype;
|
|
2401
|
+
return err;
|
|
2402
|
+
};
|
|
2403
|
+
TransformError.prototype = Object.create(Error.prototype);
|
|
2404
|
+
TransformError.prototype.constructor = TransformError;
|
|
2405
|
+
TransformError.prototype.name = "TransformError";
|
|
2406
|
+
/**
|
|
2407
|
+
Abstraction to build up and track an array of
|
|
2408
|
+
[steps](https://prosemirror.net/docs/ref/#transform.Step) representing a document transformation.
|
|
185
2409
|
|
|
2410
|
+
Most transforming methods return the `Transform` object itself, so
|
|
2411
|
+
that they can be chained.
|
|
2412
|
+
*/
|
|
2413
|
+
class Transform {
|
|
2414
|
+
/**
|
|
2415
|
+
Create a transform that starts with the given document.
|
|
2416
|
+
*/
|
|
2417
|
+
constructor(
|
|
2418
|
+
/**
|
|
2419
|
+
The current document (the result of applying the steps in the
|
|
2420
|
+
transform).
|
|
2421
|
+
*/
|
|
2422
|
+
doc) {
|
|
2423
|
+
this.doc = doc;
|
|
2424
|
+
/**
|
|
2425
|
+
The steps in this transform.
|
|
2426
|
+
*/
|
|
2427
|
+
this.steps = [];
|
|
2428
|
+
/**
|
|
2429
|
+
The documents before each of the steps.
|
|
2430
|
+
*/
|
|
2431
|
+
this.docs = [];
|
|
2432
|
+
/**
|
|
2433
|
+
A mapping with the maps for each of the steps in this transform.
|
|
2434
|
+
*/
|
|
2435
|
+
this.mapping = new Mapping;
|
|
2436
|
+
}
|
|
2437
|
+
/**
|
|
2438
|
+
The starting document.
|
|
2439
|
+
*/
|
|
2440
|
+
get before() { return this.docs.length ? this.docs[0] : this.doc; }
|
|
2441
|
+
/**
|
|
2442
|
+
Apply a new step in this transform, saving the result. Throws an
|
|
2443
|
+
error when the step fails.
|
|
2444
|
+
*/
|
|
2445
|
+
step(step) {
|
|
2446
|
+
let result = this.maybeStep(step);
|
|
2447
|
+
if (result.failed)
|
|
2448
|
+
throw new TransformError(result.failed);
|
|
2449
|
+
return this;
|
|
2450
|
+
}
|
|
2451
|
+
/**
|
|
2452
|
+
Try to apply a step in this transformation, ignoring it if it
|
|
2453
|
+
fails. Returns the step result.
|
|
2454
|
+
*/
|
|
2455
|
+
maybeStep(step) {
|
|
2456
|
+
let result = step.apply(this.doc);
|
|
2457
|
+
if (!result.failed)
|
|
2458
|
+
this.addStep(step, result.doc);
|
|
2459
|
+
return result;
|
|
2460
|
+
}
|
|
2461
|
+
/**
|
|
2462
|
+
True when the document has been changed (when there are any
|
|
2463
|
+
steps).
|
|
2464
|
+
*/
|
|
2465
|
+
get docChanged() {
|
|
2466
|
+
return this.steps.length > 0;
|
|
2467
|
+
}
|
|
2468
|
+
/**
|
|
2469
|
+
@internal
|
|
2470
|
+
*/
|
|
2471
|
+
addStep(step, doc) {
|
|
2472
|
+
this.docs.push(this.doc);
|
|
2473
|
+
this.steps.push(step);
|
|
2474
|
+
this.mapping.appendMap(step.getMap());
|
|
2475
|
+
this.doc = doc;
|
|
2476
|
+
}
|
|
2477
|
+
/**
|
|
2478
|
+
Replace the part of the document between `from` and `to` with the
|
|
2479
|
+
given `slice`.
|
|
2480
|
+
*/
|
|
2481
|
+
replace(from, to = from, slice = Slice.empty) {
|
|
2482
|
+
let step = replaceStep(this.doc, from, to, slice);
|
|
2483
|
+
if (step)
|
|
2484
|
+
this.step(step);
|
|
2485
|
+
return this;
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
Replace the given range with the given content, which may be a
|
|
2489
|
+
fragment, node, or array of nodes.
|
|
2490
|
+
*/
|
|
2491
|
+
replaceWith(from, to, content) {
|
|
2492
|
+
return this.replace(from, to, new Slice(Fragment.from(content), 0, 0));
|
|
2493
|
+
}
|
|
2494
|
+
/**
|
|
2495
|
+
Delete the content between the given positions.
|
|
2496
|
+
*/
|
|
2497
|
+
delete(from, to) {
|
|
2498
|
+
return this.replace(from, to, Slice.empty);
|
|
2499
|
+
}
|
|
2500
|
+
/**
|
|
2501
|
+
Insert the given content at the given position.
|
|
2502
|
+
*/
|
|
2503
|
+
insert(pos, content) {
|
|
2504
|
+
return this.replaceWith(pos, pos, content);
|
|
2505
|
+
}
|
|
2506
|
+
/**
|
|
2507
|
+
Replace a range of the document with a given slice, using
|
|
2508
|
+
`from`, `to`, and the slice's
|
|
2509
|
+
[`openStart`](https://prosemirror.net/docs/ref/#model.Slice.openStart) property as hints, rather
|
|
2510
|
+
than fixed start and end points. This method may grow the
|
|
2511
|
+
replaced area or close open nodes in the slice in order to get a
|
|
2512
|
+
fit that is more in line with WYSIWYG expectations, by dropping
|
|
2513
|
+
fully covered parent nodes of the replaced region when they are
|
|
2514
|
+
marked [non-defining as
|
|
2515
|
+
context](https://prosemirror.net/docs/ref/#model.NodeSpec.definingAsContext), or including an
|
|
2516
|
+
open parent node from the slice that _is_ marked as [defining
|
|
2517
|
+
its content](https://prosemirror.net/docs/ref/#model.NodeSpec.definingForContent).
|
|
2518
|
+
|
|
2519
|
+
This is the method, for example, to handle paste. The similar
|
|
2520
|
+
[`replace`](https://prosemirror.net/docs/ref/#transform.Transform.replace) method is a more
|
|
2521
|
+
primitive tool which will _not_ move the start and end of its given
|
|
2522
|
+
range, and is useful in situations where you need more precise
|
|
2523
|
+
control over what happens.
|
|
2524
|
+
*/
|
|
2525
|
+
replaceRange(from, to, slice) {
|
|
2526
|
+
replaceRange(this, from, to, slice);
|
|
2527
|
+
return this;
|
|
2528
|
+
}
|
|
2529
|
+
/**
|
|
2530
|
+
Replace the given range with a node, but use `from` and `to` as
|
|
2531
|
+
hints, rather than precise positions. When from and to are the same
|
|
2532
|
+
and are at the start or end of a parent node in which the given
|
|
2533
|
+
node doesn't fit, this method may _move_ them out towards a parent
|
|
2534
|
+
that does allow the given node to be placed. When the given range
|
|
2535
|
+
completely covers a parent node, this method may completely replace
|
|
2536
|
+
that parent node.
|
|
2537
|
+
*/
|
|
2538
|
+
replaceRangeWith(from, to, node) {
|
|
2539
|
+
replaceRangeWith(this, from, to, node);
|
|
2540
|
+
return this;
|
|
2541
|
+
}
|
|
2542
|
+
/**
|
|
2543
|
+
Delete the given range, expanding it to cover fully covered
|
|
2544
|
+
parent nodes until a valid replace is found.
|
|
2545
|
+
*/
|
|
2546
|
+
deleteRange(from, to) {
|
|
2547
|
+
deleteRange(this, from, to);
|
|
2548
|
+
return this;
|
|
2549
|
+
}
|
|
2550
|
+
/**
|
|
2551
|
+
Split the content in the given range off from its parent, if there
|
|
2552
|
+
is sibling content before or after it, and move it up the tree to
|
|
2553
|
+
the depth specified by `target`. You'll probably want to use
|
|
2554
|
+
[`liftTarget`](https://prosemirror.net/docs/ref/#transform.liftTarget) to compute `target`, to make
|
|
2555
|
+
sure the lift is valid.
|
|
2556
|
+
*/
|
|
2557
|
+
lift(range, target) {
|
|
2558
|
+
lift(this, range, target);
|
|
2559
|
+
return this;
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
Join the blocks around the given position. If depth is 2, their
|
|
2563
|
+
last and first siblings are also joined, and so on.
|
|
2564
|
+
*/
|
|
2565
|
+
join(pos, depth = 1) {
|
|
2566
|
+
join(this, pos, depth);
|
|
2567
|
+
return this;
|
|
2568
|
+
}
|
|
2569
|
+
/**
|
|
2570
|
+
Wrap the given [range](https://prosemirror.net/docs/ref/#model.NodeRange) in the given set of wrappers.
|
|
2571
|
+
The wrappers are assumed to be valid in this position, and should
|
|
2572
|
+
probably be computed with [`findWrapping`](https://prosemirror.net/docs/ref/#transform.findWrapping).
|
|
2573
|
+
*/
|
|
2574
|
+
wrap(range, wrappers) {
|
|
2575
|
+
wrap(this, range, wrappers);
|
|
2576
|
+
return this;
|
|
2577
|
+
}
|
|
2578
|
+
/**
|
|
2579
|
+
Set the type of all textblocks (partly) between `from` and `to` to
|
|
2580
|
+
the given node type with the given attributes.
|
|
2581
|
+
*/
|
|
2582
|
+
setBlockType(from, to = from, type, attrs = null) {
|
|
2583
|
+
setBlockType(this, from, to, type, attrs);
|
|
2584
|
+
return this;
|
|
2585
|
+
}
|
|
2586
|
+
/**
|
|
2587
|
+
Change the type, attributes, and/or marks of the node at `pos`.
|
|
2588
|
+
When `type` isn't given, the existing node type is preserved,
|
|
2589
|
+
*/
|
|
2590
|
+
setNodeMarkup(pos, type, attrs = null, marks = []) {
|
|
2591
|
+
setNodeMarkup(this, pos, type, attrs, marks);
|
|
2592
|
+
return this;
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
Set a single attribute on a given node to a new value.
|
|
2596
|
+
*/
|
|
2597
|
+
setNodeAttribute(pos, attr, value) {
|
|
2598
|
+
this.step(new AttrStep(pos, attr, value));
|
|
2599
|
+
return this;
|
|
2600
|
+
}
|
|
2601
|
+
/**
|
|
2602
|
+
Add a mark to the node at position `pos`.
|
|
2603
|
+
*/
|
|
2604
|
+
addNodeMark(pos, mark) {
|
|
2605
|
+
this.step(new AddNodeMarkStep(pos, mark));
|
|
2606
|
+
return this;
|
|
2607
|
+
}
|
|
2608
|
+
/**
|
|
2609
|
+
Remove a mark (or a mark of the given type) from the node at
|
|
2610
|
+
position `pos`.
|
|
2611
|
+
*/
|
|
2612
|
+
removeNodeMark(pos, mark) {
|
|
2613
|
+
if (!(mark instanceof Mark)) {
|
|
2614
|
+
let node = this.doc.nodeAt(pos);
|
|
2615
|
+
if (!node)
|
|
2616
|
+
throw new RangeError("No node at position " + pos);
|
|
2617
|
+
mark = mark.isInSet(node.marks);
|
|
2618
|
+
if (!mark)
|
|
2619
|
+
return this;
|
|
2620
|
+
}
|
|
2621
|
+
this.step(new RemoveNodeMarkStep(pos, mark));
|
|
2622
|
+
return this;
|
|
2623
|
+
}
|
|
2624
|
+
/**
|
|
2625
|
+
Split the node at the given position, and optionally, if `depth` is
|
|
2626
|
+
greater than one, any number of nodes above that. By default, the
|
|
2627
|
+
parts split off will inherit the node type of the original node.
|
|
2628
|
+
This can be changed by passing an array of types and attributes to
|
|
2629
|
+
use after the split.
|
|
2630
|
+
*/
|
|
2631
|
+
split(pos, depth = 1, typesAfter) {
|
|
2632
|
+
split(this, pos, depth, typesAfter);
|
|
2633
|
+
return this;
|
|
2634
|
+
}
|
|
2635
|
+
/**
|
|
2636
|
+
Add the given mark to the inline content between `from` and `to`.
|
|
2637
|
+
*/
|
|
2638
|
+
addMark(from, to, mark) {
|
|
2639
|
+
addMark(this, from, to, mark);
|
|
2640
|
+
return this;
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
Remove marks from inline nodes between `from` and `to`. When
|
|
2644
|
+
`mark` is a single mark, remove precisely that mark. When it is
|
|
2645
|
+
a mark type, remove all marks of that type. When it is null,
|
|
2646
|
+
remove all marks of any type.
|
|
2647
|
+
*/
|
|
2648
|
+
removeMark(from, to, mark) {
|
|
2649
|
+
removeMark(this, from, to, mark);
|
|
2650
|
+
return this;
|
|
2651
|
+
}
|
|
2652
|
+
/**
|
|
2653
|
+
Removes all marks and nodes from the content of the node at
|
|
2654
|
+
`pos` that don't match the given new parent node type. Accepts
|
|
2655
|
+
an optional starting [content match](https://prosemirror.net/docs/ref/#model.ContentMatch) as
|
|
2656
|
+
third argument.
|
|
2657
|
+
*/
|
|
2658
|
+
clearIncompatible(pos, parentType, match) {
|
|
2659
|
+
clearIncompatible(this, pos, parentType, match);
|
|
2660
|
+
return this;
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
186
2663
|
|
|
2664
|
+
const classesById = Object.create(null);
|
|
2665
|
+
/**
|
|
2666
|
+
Superclass for editor selections. Every selection type should
|
|
2667
|
+
extend this. Should not be instantiated directly.
|
|
2668
|
+
*/
|
|
2669
|
+
class Selection {
|
|
2670
|
+
/**
|
|
2671
|
+
Initialize a selection with the head and anchor and ranges. If no
|
|
2672
|
+
ranges are given, constructs a single range across `$anchor` and
|
|
2673
|
+
`$head`.
|
|
2674
|
+
*/
|
|
2675
|
+
constructor(
|
|
2676
|
+
/**
|
|
2677
|
+
The resolved anchor of the selection (the side that stays in
|
|
2678
|
+
place when the selection is modified).
|
|
2679
|
+
*/
|
|
2680
|
+
$anchor,
|
|
2681
|
+
/**
|
|
2682
|
+
The resolved head of the selection (the side that moves when
|
|
2683
|
+
the selection is modified).
|
|
2684
|
+
*/
|
|
2685
|
+
$head, ranges) {
|
|
2686
|
+
this.$anchor = $anchor;
|
|
2687
|
+
this.$head = $head;
|
|
2688
|
+
this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
|
|
2689
|
+
}
|
|
2690
|
+
/**
|
|
2691
|
+
The selection's anchor, as an unresolved position.
|
|
2692
|
+
*/
|
|
2693
|
+
get anchor() { return this.$anchor.pos; }
|
|
2694
|
+
/**
|
|
2695
|
+
The selection's head.
|
|
2696
|
+
*/
|
|
2697
|
+
get head() { return this.$head.pos; }
|
|
2698
|
+
/**
|
|
2699
|
+
The lower bound of the selection's main range.
|
|
2700
|
+
*/
|
|
2701
|
+
get from() { return this.$from.pos; }
|
|
2702
|
+
/**
|
|
2703
|
+
The upper bound of the selection's main range.
|
|
2704
|
+
*/
|
|
2705
|
+
get to() { return this.$to.pos; }
|
|
2706
|
+
/**
|
|
2707
|
+
The resolved lower bound of the selection's main range.
|
|
2708
|
+
*/
|
|
2709
|
+
get $from() {
|
|
2710
|
+
return this.ranges[0].$from;
|
|
2711
|
+
}
|
|
2712
|
+
/**
|
|
2713
|
+
The resolved upper bound of the selection's main range.
|
|
2714
|
+
*/
|
|
2715
|
+
get $to() {
|
|
2716
|
+
return this.ranges[0].$to;
|
|
2717
|
+
}
|
|
2718
|
+
/**
|
|
2719
|
+
Indicates whether the selection contains any content.
|
|
2720
|
+
*/
|
|
2721
|
+
get empty() {
|
|
2722
|
+
let ranges = this.ranges;
|
|
2723
|
+
for (let i = 0; i < ranges.length; i++)
|
|
2724
|
+
if (ranges[i].$from.pos != ranges[i].$to.pos)
|
|
2725
|
+
return false;
|
|
2726
|
+
return true;
|
|
2727
|
+
}
|
|
2728
|
+
/**
|
|
2729
|
+
Get the content of this selection as a slice.
|
|
2730
|
+
*/
|
|
2731
|
+
content() {
|
|
2732
|
+
return this.$from.doc.slice(this.from, this.to, true);
|
|
2733
|
+
}
|
|
2734
|
+
/**
|
|
2735
|
+
Replace the selection with a slice or, if no slice is given,
|
|
2736
|
+
delete the selection. Will append to the given transaction.
|
|
2737
|
+
*/
|
|
2738
|
+
replace(tr, content = Slice.empty) {
|
|
2739
|
+
// Put the new selection at the position after the inserted
|
|
2740
|
+
// content. When that ended in an inline node, search backwards,
|
|
2741
|
+
// to get the position after that node. If not, search forward.
|
|
2742
|
+
let lastNode = content.content.lastChild, lastParent = null;
|
|
2743
|
+
for (let i = 0; i < content.openEnd; i++) {
|
|
2744
|
+
lastParent = lastNode;
|
|
2745
|
+
lastNode = lastNode.lastChild;
|
|
2746
|
+
}
|
|
2747
|
+
let mapFrom = tr.steps.length, ranges = this.ranges;
|
|
2748
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
2749
|
+
let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
2750
|
+
tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
|
|
2751
|
+
if (i == 0)
|
|
2752
|
+
selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
/**
|
|
2756
|
+
Replace the selection with the given node, appending the changes
|
|
2757
|
+
to the given transaction.
|
|
2758
|
+
*/
|
|
2759
|
+
replaceWith(tr, node) {
|
|
2760
|
+
let mapFrom = tr.steps.length, ranges = this.ranges;
|
|
2761
|
+
for (let i = 0; i < ranges.length; i++) {
|
|
2762
|
+
let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
|
|
2763
|
+
let from = mapping.map($from.pos), to = mapping.map($to.pos);
|
|
2764
|
+
if (i) {
|
|
2765
|
+
tr.deleteRange(from, to);
|
|
2766
|
+
}
|
|
2767
|
+
else {
|
|
2768
|
+
tr.replaceRangeWith(from, to, node);
|
|
2769
|
+
selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2772
|
+
}
|
|
2773
|
+
/**
|
|
2774
|
+
Find a valid cursor or leaf node selection starting at the given
|
|
2775
|
+
position and searching back if `dir` is negative, and forward if
|
|
2776
|
+
positive. When `textOnly` is true, only consider cursor
|
|
2777
|
+
selections. Will return null when no valid selection position is
|
|
2778
|
+
found.
|
|
2779
|
+
*/
|
|
2780
|
+
static findFrom($pos, dir, textOnly = false) {
|
|
2781
|
+
let inner = $pos.parent.inlineContent ? new TextSelection($pos)
|
|
2782
|
+
: findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
|
|
2783
|
+
if (inner)
|
|
2784
|
+
return inner;
|
|
2785
|
+
for (let depth = $pos.depth - 1; depth >= 0; depth--) {
|
|
2786
|
+
let found = dir < 0
|
|
2787
|
+
? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly)
|
|
2788
|
+
: findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
|
|
2789
|
+
if (found)
|
|
2790
|
+
return found;
|
|
2791
|
+
}
|
|
2792
|
+
return null;
|
|
2793
|
+
}
|
|
2794
|
+
/**
|
|
2795
|
+
Find a valid cursor or leaf node selection near the given
|
|
2796
|
+
position. Searches forward first by default, but if `bias` is
|
|
2797
|
+
negative, it will search backwards first.
|
|
2798
|
+
*/
|
|
2799
|
+
static near($pos, bias = 1) {
|
|
2800
|
+
return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
|
|
2801
|
+
}
|
|
2802
|
+
/**
|
|
2803
|
+
Find the cursor or leaf node selection closest to the start of
|
|
2804
|
+
the given document. Will return an
|
|
2805
|
+
[`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
|
|
2806
|
+
exists.
|
|
2807
|
+
*/
|
|
2808
|
+
static atStart(doc) {
|
|
2809
|
+
return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
|
|
2810
|
+
}
|
|
2811
|
+
/**
|
|
2812
|
+
Find the cursor or leaf node selection closest to the end of the
|
|
2813
|
+
given document.
|
|
2814
|
+
*/
|
|
2815
|
+
static atEnd(doc) {
|
|
2816
|
+
return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
|
|
2817
|
+
}
|
|
2818
|
+
/**
|
|
2819
|
+
Deserialize the JSON representation of a selection. Must be
|
|
2820
|
+
implemented for custom classes (as a static class method).
|
|
2821
|
+
*/
|
|
2822
|
+
static fromJSON(doc, json) {
|
|
2823
|
+
if (!json || !json.type)
|
|
2824
|
+
throw new RangeError("Invalid input for Selection.fromJSON");
|
|
2825
|
+
let cls = classesById[json.type];
|
|
2826
|
+
if (!cls)
|
|
2827
|
+
throw new RangeError(`No selection type ${json.type} defined`);
|
|
2828
|
+
return cls.fromJSON(doc, json);
|
|
2829
|
+
}
|
|
2830
|
+
/**
|
|
2831
|
+
To be able to deserialize selections from JSON, custom selection
|
|
2832
|
+
classes must register themselves with an ID string, so that they
|
|
2833
|
+
can be disambiguated. Try to pick something that's unlikely to
|
|
2834
|
+
clash with classes from other modules.
|
|
2835
|
+
*/
|
|
2836
|
+
static jsonID(id, selectionClass) {
|
|
2837
|
+
if (id in classesById)
|
|
2838
|
+
throw new RangeError("Duplicate use of selection JSON ID " + id);
|
|
2839
|
+
classesById[id] = selectionClass;
|
|
2840
|
+
selectionClass.prototype.jsonID = id;
|
|
2841
|
+
return selectionClass;
|
|
2842
|
+
}
|
|
2843
|
+
/**
|
|
2844
|
+
Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
|
|
2845
|
+
which is a value that can be mapped without having access to a
|
|
2846
|
+
current document, and later resolved to a real selection for a
|
|
2847
|
+
given document again. (This is used mostly by the history to
|
|
2848
|
+
track and restore old selections.) The default implementation of
|
|
2849
|
+
this method just converts the selection to a text selection and
|
|
2850
|
+
returns the bookmark for that.
|
|
2851
|
+
*/
|
|
2852
|
+
getBookmark() {
|
|
2853
|
+
return TextSelection.between(this.$anchor, this.$head).getBookmark();
|
|
2854
|
+
}
|
|
2855
|
+
}
|
|
2856
|
+
Selection.prototype.visible = true;
|
|
2857
|
+
/**
|
|
2858
|
+
Represents a selected range in a document.
|
|
2859
|
+
*/
|
|
2860
|
+
class SelectionRange {
|
|
2861
|
+
/**
|
|
2862
|
+
Create a range.
|
|
2863
|
+
*/
|
|
2864
|
+
constructor(
|
|
2865
|
+
/**
|
|
2866
|
+
The lower bound of the range.
|
|
2867
|
+
*/
|
|
2868
|
+
$from,
|
|
2869
|
+
/**
|
|
2870
|
+
The upper bound of the range.
|
|
2871
|
+
*/
|
|
2872
|
+
$to) {
|
|
2873
|
+
this.$from = $from;
|
|
2874
|
+
this.$to = $to;
|
|
2875
|
+
}
|
|
2876
|
+
}
|
|
2877
|
+
let warnedAboutTextSelection = false;
|
|
2878
|
+
function checkTextSelection($pos) {
|
|
2879
|
+
if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
|
|
2880
|
+
warnedAboutTextSelection = true;
|
|
2881
|
+
console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
|
|
2882
|
+
}
|
|
2883
|
+
}
|
|
2884
|
+
/**
|
|
2885
|
+
A text selection represents a classical editor selection, with a
|
|
2886
|
+
head (the moving side) and anchor (immobile side), both of which
|
|
2887
|
+
point into textblock nodes. It can be empty (a regular cursor
|
|
2888
|
+
position).
|
|
2889
|
+
*/
|
|
2890
|
+
class TextSelection extends Selection {
|
|
2891
|
+
/**
|
|
2892
|
+
Construct a text selection between the given points.
|
|
2893
|
+
*/
|
|
2894
|
+
constructor($anchor, $head = $anchor) {
|
|
2895
|
+
checkTextSelection($anchor);
|
|
2896
|
+
checkTextSelection($head);
|
|
2897
|
+
super($anchor, $head);
|
|
2898
|
+
}
|
|
2899
|
+
/**
|
|
2900
|
+
Returns a resolved position if this is a cursor selection (an
|
|
2901
|
+
empty text selection), and null otherwise.
|
|
2902
|
+
*/
|
|
2903
|
+
get $cursor() { return this.$anchor.pos == this.$head.pos ? this.$head : null; }
|
|
2904
|
+
map(doc, mapping) {
|
|
2905
|
+
let $head = doc.resolve(mapping.map(this.head));
|
|
2906
|
+
if (!$head.parent.inlineContent)
|
|
2907
|
+
return Selection.near($head);
|
|
2908
|
+
let $anchor = doc.resolve(mapping.map(this.anchor));
|
|
2909
|
+
return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
|
|
2910
|
+
}
|
|
2911
|
+
replace(tr, content = Slice.empty) {
|
|
2912
|
+
super.replace(tr, content);
|
|
2913
|
+
if (content == Slice.empty) {
|
|
2914
|
+
let marks = this.$from.marksAcross(this.$to);
|
|
2915
|
+
if (marks)
|
|
2916
|
+
tr.ensureMarks(marks);
|
|
2917
|
+
}
|
|
2918
|
+
}
|
|
2919
|
+
eq(other) {
|
|
2920
|
+
return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
|
|
2921
|
+
}
|
|
2922
|
+
getBookmark() {
|
|
2923
|
+
return new TextBookmark(this.anchor, this.head);
|
|
2924
|
+
}
|
|
2925
|
+
toJSON() {
|
|
2926
|
+
return { type: "text", anchor: this.anchor, head: this.head };
|
|
2927
|
+
}
|
|
2928
|
+
/**
|
|
2929
|
+
@internal
|
|
2930
|
+
*/
|
|
2931
|
+
static fromJSON(doc, json) {
|
|
2932
|
+
if (typeof json.anchor != "number" || typeof json.head != "number")
|
|
2933
|
+
throw new RangeError("Invalid input for TextSelection.fromJSON");
|
|
2934
|
+
return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
|
|
2935
|
+
}
|
|
2936
|
+
/**
|
|
2937
|
+
Create a text selection from non-resolved positions.
|
|
2938
|
+
*/
|
|
2939
|
+
static create(doc, anchor, head = anchor) {
|
|
2940
|
+
let $anchor = doc.resolve(anchor);
|
|
2941
|
+
return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
|
|
2942
|
+
}
|
|
2943
|
+
/**
|
|
2944
|
+
Return a text selection that spans the given positions or, if
|
|
2945
|
+
they aren't text positions, find a text selection near them.
|
|
2946
|
+
`bias` determines whether the method searches forward (default)
|
|
2947
|
+
or backwards (negative number) first. Will fall back to calling
|
|
2948
|
+
[`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
|
|
2949
|
+
doesn't contain a valid text position.
|
|
2950
|
+
*/
|
|
2951
|
+
static between($anchor, $head, bias) {
|
|
2952
|
+
let dPos = $anchor.pos - $head.pos;
|
|
2953
|
+
if (!bias || dPos)
|
|
2954
|
+
bias = dPos >= 0 ? 1 : -1;
|
|
2955
|
+
if (!$head.parent.inlineContent) {
|
|
2956
|
+
let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
|
|
2957
|
+
if (found)
|
|
2958
|
+
$head = found.$head;
|
|
2959
|
+
else
|
|
2960
|
+
return Selection.near($head, bias);
|
|
2961
|
+
}
|
|
2962
|
+
if (!$anchor.parent.inlineContent) {
|
|
2963
|
+
if (dPos == 0) {
|
|
2964
|
+
$anchor = $head;
|
|
2965
|
+
}
|
|
2966
|
+
else {
|
|
2967
|
+
$anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
|
|
2968
|
+
if (($anchor.pos < $head.pos) != (dPos < 0))
|
|
2969
|
+
$anchor = $head;
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
return new TextSelection($anchor, $head);
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
Selection.jsonID("text", TextSelection);
|
|
2976
|
+
class TextBookmark {
|
|
2977
|
+
constructor(anchor, head) {
|
|
2978
|
+
this.anchor = anchor;
|
|
2979
|
+
this.head = head;
|
|
2980
|
+
}
|
|
2981
|
+
map(mapping) {
|
|
2982
|
+
return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
|
|
2983
|
+
}
|
|
2984
|
+
resolve(doc) {
|
|
2985
|
+
return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2988
|
+
/**
|
|
2989
|
+
A node selection is a selection that points at a single node. All
|
|
2990
|
+
nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
|
|
2991
|
+
target of a node selection. In such a selection, `from` and `to`
|
|
2992
|
+
point directly before and after the selected node, `anchor` equals
|
|
2993
|
+
`from`, and `head` equals `to`..
|
|
2994
|
+
*/
|
|
2995
|
+
class NodeSelection extends Selection {
|
|
2996
|
+
/**
|
|
2997
|
+
Create a node selection. Does not verify the validity of its
|
|
2998
|
+
argument.
|
|
2999
|
+
*/
|
|
3000
|
+
constructor($pos) {
|
|
3001
|
+
let node = $pos.nodeAfter;
|
|
3002
|
+
let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
|
|
3003
|
+
super($pos, $end);
|
|
3004
|
+
this.node = node;
|
|
3005
|
+
}
|
|
3006
|
+
map(doc, mapping) {
|
|
3007
|
+
let { deleted, pos } = mapping.mapResult(this.anchor);
|
|
3008
|
+
let $pos = doc.resolve(pos);
|
|
3009
|
+
if (deleted)
|
|
3010
|
+
return Selection.near($pos);
|
|
3011
|
+
return new NodeSelection($pos);
|
|
3012
|
+
}
|
|
3013
|
+
content() {
|
|
3014
|
+
return new Slice(Fragment.from(this.node), 0, 0);
|
|
3015
|
+
}
|
|
3016
|
+
eq(other) {
|
|
3017
|
+
return other instanceof NodeSelection && other.anchor == this.anchor;
|
|
3018
|
+
}
|
|
3019
|
+
toJSON() {
|
|
3020
|
+
return { type: "node", anchor: this.anchor };
|
|
3021
|
+
}
|
|
3022
|
+
getBookmark() { return new NodeBookmark(this.anchor); }
|
|
3023
|
+
/**
|
|
3024
|
+
@internal
|
|
3025
|
+
*/
|
|
3026
|
+
static fromJSON(doc, json) {
|
|
3027
|
+
if (typeof json.anchor != "number")
|
|
3028
|
+
throw new RangeError("Invalid input for NodeSelection.fromJSON");
|
|
3029
|
+
return new NodeSelection(doc.resolve(json.anchor));
|
|
3030
|
+
}
|
|
3031
|
+
/**
|
|
3032
|
+
Create a node selection from non-resolved positions.
|
|
3033
|
+
*/
|
|
3034
|
+
static create(doc, from) {
|
|
3035
|
+
return new NodeSelection(doc.resolve(from));
|
|
3036
|
+
}
|
|
3037
|
+
/**
|
|
3038
|
+
Determines whether the given node may be selected as a node
|
|
3039
|
+
selection.
|
|
3040
|
+
*/
|
|
3041
|
+
static isSelectable(node) {
|
|
3042
|
+
return !node.isText && node.type.spec.selectable !== false;
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
3045
|
+
NodeSelection.prototype.visible = false;
|
|
3046
|
+
Selection.jsonID("node", NodeSelection);
|
|
3047
|
+
class NodeBookmark {
|
|
3048
|
+
constructor(anchor) {
|
|
3049
|
+
this.anchor = anchor;
|
|
3050
|
+
}
|
|
3051
|
+
map(mapping) {
|
|
3052
|
+
let { deleted, pos } = mapping.mapResult(this.anchor);
|
|
3053
|
+
return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
|
|
3054
|
+
}
|
|
3055
|
+
resolve(doc) {
|
|
3056
|
+
let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
|
|
3057
|
+
if (node && NodeSelection.isSelectable(node))
|
|
3058
|
+
return new NodeSelection($pos);
|
|
3059
|
+
return Selection.near($pos);
|
|
3060
|
+
}
|
|
3061
|
+
}
|
|
3062
|
+
/**
|
|
3063
|
+
A selection type that represents selecting the whole document
|
|
3064
|
+
(which can not necessarily be expressed with a text selection, when
|
|
3065
|
+
there are for example leaf block nodes at the start or end of the
|
|
3066
|
+
document).
|
|
3067
|
+
*/
|
|
3068
|
+
class AllSelection extends Selection {
|
|
3069
|
+
/**
|
|
3070
|
+
Create an all-selection over the given document.
|
|
3071
|
+
*/
|
|
3072
|
+
constructor(doc) {
|
|
3073
|
+
super(doc.resolve(0), doc.resolve(doc.content.size));
|
|
3074
|
+
}
|
|
3075
|
+
replace(tr, content = Slice.empty) {
|
|
3076
|
+
if (content == Slice.empty) {
|
|
3077
|
+
tr.delete(0, tr.doc.content.size);
|
|
3078
|
+
let sel = Selection.atStart(tr.doc);
|
|
3079
|
+
if (!sel.eq(tr.selection))
|
|
3080
|
+
tr.setSelection(sel);
|
|
3081
|
+
}
|
|
3082
|
+
else {
|
|
3083
|
+
super.replace(tr, content);
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
toJSON() { return { type: "all" }; }
|
|
3087
|
+
/**
|
|
3088
|
+
@internal
|
|
3089
|
+
*/
|
|
3090
|
+
static fromJSON(doc) { return new AllSelection(doc); }
|
|
3091
|
+
map(doc) { return new AllSelection(doc); }
|
|
3092
|
+
eq(other) { return other instanceof AllSelection; }
|
|
3093
|
+
getBookmark() { return AllBookmark; }
|
|
3094
|
+
}
|
|
3095
|
+
Selection.jsonID("all", AllSelection);
|
|
3096
|
+
const AllBookmark = {
|
|
3097
|
+
map() { return this; },
|
|
3098
|
+
resolve(doc) { return new AllSelection(doc); }
|
|
3099
|
+
};
|
|
3100
|
+
// FIXME we'll need some awareness of text direction when scanning for selections
|
|
3101
|
+
// Try to find a selection inside the given node. `pos` points at the
|
|
3102
|
+
// position where the search starts. When `text` is true, only return
|
|
3103
|
+
// text selections.
|
|
3104
|
+
function findSelectionIn(doc, node, pos, index, dir, text = false) {
|
|
3105
|
+
if (node.inlineContent)
|
|
3106
|
+
return TextSelection.create(doc, pos);
|
|
3107
|
+
for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
|
|
3108
|
+
let child = node.child(i);
|
|
3109
|
+
if (!child.isAtom) {
|
|
3110
|
+
let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
|
|
3111
|
+
if (inner)
|
|
3112
|
+
return inner;
|
|
3113
|
+
}
|
|
3114
|
+
else if (!text && NodeSelection.isSelectable(child)) {
|
|
3115
|
+
return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
|
|
3116
|
+
}
|
|
3117
|
+
pos += child.nodeSize * dir;
|
|
3118
|
+
}
|
|
3119
|
+
return null;
|
|
3120
|
+
}
|
|
3121
|
+
function selectionToInsertionEnd(tr, startLen, bias) {
|
|
3122
|
+
let last = tr.steps.length - 1;
|
|
3123
|
+
if (last < startLen)
|
|
3124
|
+
return;
|
|
3125
|
+
let step = tr.steps[last];
|
|
3126
|
+
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep))
|
|
3127
|
+
return;
|
|
3128
|
+
let map = tr.mapping.maps[last], end;
|
|
3129
|
+
map.forEach((_from, _to, _newFrom, newTo) => { if (end == null)
|
|
3130
|
+
end = newTo; });
|
|
3131
|
+
tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
|
|
3132
|
+
}
|
|
3133
|
+
|
|
3134
|
+
const UPDATED_SEL = 1, UPDATED_MARKS = 2, UPDATED_SCROLL = 4;
|
|
3135
|
+
/**
|
|
3136
|
+
An editor state transaction, which can be applied to a state to
|
|
3137
|
+
create an updated state. Use
|
|
3138
|
+
[`EditorState.tr`](https://prosemirror.net/docs/ref/#state.EditorState.tr) to create an instance.
|
|
3139
|
+
|
|
3140
|
+
Transactions track changes to the document (they are a subclass of
|
|
3141
|
+
[`Transform`](https://prosemirror.net/docs/ref/#transform.Transform)), but also other state changes,
|
|
3142
|
+
like selection updates and adjustments of the set of [stored
|
|
3143
|
+
marks](https://prosemirror.net/docs/ref/#state.EditorState.storedMarks). In addition, you can store
|
|
3144
|
+
metadata properties in a transaction, which are extra pieces of
|
|
3145
|
+
information that client code or plugins can use to describe what a
|
|
3146
|
+
transaction represents, so that they can update their [own
|
|
3147
|
+
state](https://prosemirror.net/docs/ref/#state.StateField) accordingly.
|
|
187
3148
|
|
|
3149
|
+
The [editor view](https://prosemirror.net/docs/ref/#view.EditorView) uses a few metadata properties:
|
|
3150
|
+
it will attach a property `"pointer"` with the value `true` to
|
|
3151
|
+
selection transactions directly caused by mouse or touch input, and
|
|
3152
|
+
a `"uiEvent"` property of that may be `"paste"`, `"cut"`, or `"drop"`.
|
|
3153
|
+
*/
|
|
3154
|
+
class Transaction extends Transform {
|
|
3155
|
+
/**
|
|
3156
|
+
@internal
|
|
3157
|
+
*/
|
|
3158
|
+
constructor(state) {
|
|
3159
|
+
super(state.doc);
|
|
3160
|
+
// The step count for which the current selection is valid.
|
|
3161
|
+
this.curSelectionFor = 0;
|
|
3162
|
+
// Bitfield to track which aspects of the state were updated by
|
|
3163
|
+
// this transaction.
|
|
3164
|
+
this.updated = 0;
|
|
3165
|
+
// Object used to store metadata properties for the transaction.
|
|
3166
|
+
this.meta = Object.create(null);
|
|
3167
|
+
this.time = Date.now();
|
|
3168
|
+
this.curSelection = state.selection;
|
|
3169
|
+
this.storedMarks = state.storedMarks;
|
|
3170
|
+
}
|
|
3171
|
+
/**
|
|
3172
|
+
The transaction's current selection. This defaults to the editor
|
|
3173
|
+
selection [mapped](https://prosemirror.net/docs/ref/#state.Selection.map) through the steps in the
|
|
3174
|
+
transaction, but can be overwritten with
|
|
3175
|
+
[`setSelection`](https://prosemirror.net/docs/ref/#state.Transaction.setSelection).
|
|
3176
|
+
*/
|
|
3177
|
+
get selection() {
|
|
3178
|
+
if (this.curSelectionFor < this.steps.length) {
|
|
3179
|
+
this.curSelection = this.curSelection.map(this.doc, this.mapping.slice(this.curSelectionFor));
|
|
3180
|
+
this.curSelectionFor = this.steps.length;
|
|
3181
|
+
}
|
|
3182
|
+
return this.curSelection;
|
|
3183
|
+
}
|
|
3184
|
+
/**
|
|
3185
|
+
Update the transaction's current selection. Will determine the
|
|
3186
|
+
selection that the editor gets when the transaction is applied.
|
|
3187
|
+
*/
|
|
3188
|
+
setSelection(selection) {
|
|
3189
|
+
if (selection.$from.doc != this.doc)
|
|
3190
|
+
throw new RangeError("Selection passed to setSelection must point at the current document");
|
|
3191
|
+
this.curSelection = selection;
|
|
3192
|
+
this.curSelectionFor = this.steps.length;
|
|
3193
|
+
this.updated = (this.updated | UPDATED_SEL) & ~UPDATED_MARKS;
|
|
3194
|
+
this.storedMarks = null;
|
|
3195
|
+
return this;
|
|
3196
|
+
}
|
|
3197
|
+
/**
|
|
3198
|
+
Whether the selection was explicitly updated by this transaction.
|
|
3199
|
+
*/
|
|
3200
|
+
get selectionSet() {
|
|
3201
|
+
return (this.updated & UPDATED_SEL) > 0;
|
|
3202
|
+
}
|
|
3203
|
+
/**
|
|
3204
|
+
Set the current stored marks.
|
|
3205
|
+
*/
|
|
3206
|
+
setStoredMarks(marks) {
|
|
3207
|
+
this.storedMarks = marks;
|
|
3208
|
+
this.updated |= UPDATED_MARKS;
|
|
3209
|
+
return this;
|
|
3210
|
+
}
|
|
3211
|
+
/**
|
|
3212
|
+
Make sure the current stored marks or, if that is null, the marks
|
|
3213
|
+
at the selection, match the given set of marks. Does nothing if
|
|
3214
|
+
this is already the case.
|
|
3215
|
+
*/
|
|
3216
|
+
ensureMarks(marks) {
|
|
3217
|
+
if (!Mark.sameSet(this.storedMarks || this.selection.$from.marks(), marks))
|
|
3218
|
+
this.setStoredMarks(marks);
|
|
3219
|
+
return this;
|
|
3220
|
+
}
|
|
3221
|
+
/**
|
|
3222
|
+
Add a mark to the set of stored marks.
|
|
3223
|
+
*/
|
|
3224
|
+
addStoredMark(mark) {
|
|
3225
|
+
return this.ensureMarks(mark.addToSet(this.storedMarks || this.selection.$head.marks()));
|
|
3226
|
+
}
|
|
3227
|
+
/**
|
|
3228
|
+
Remove a mark or mark type from the set of stored marks.
|
|
3229
|
+
*/
|
|
3230
|
+
removeStoredMark(mark) {
|
|
3231
|
+
return this.ensureMarks(mark.removeFromSet(this.storedMarks || this.selection.$head.marks()));
|
|
3232
|
+
}
|
|
3233
|
+
/**
|
|
3234
|
+
Whether the stored marks were explicitly set for this transaction.
|
|
3235
|
+
*/
|
|
3236
|
+
get storedMarksSet() {
|
|
3237
|
+
return (this.updated & UPDATED_MARKS) > 0;
|
|
3238
|
+
}
|
|
3239
|
+
/**
|
|
3240
|
+
@internal
|
|
3241
|
+
*/
|
|
3242
|
+
addStep(step, doc) {
|
|
3243
|
+
super.addStep(step, doc);
|
|
3244
|
+
this.updated = this.updated & ~UPDATED_MARKS;
|
|
3245
|
+
this.storedMarks = null;
|
|
3246
|
+
}
|
|
3247
|
+
/**
|
|
3248
|
+
Update the timestamp for the transaction.
|
|
3249
|
+
*/
|
|
3250
|
+
setTime(time) {
|
|
3251
|
+
this.time = time;
|
|
3252
|
+
return this;
|
|
3253
|
+
}
|
|
3254
|
+
/**
|
|
3255
|
+
Replace the current selection with the given slice.
|
|
3256
|
+
*/
|
|
3257
|
+
replaceSelection(slice) {
|
|
3258
|
+
this.selection.replace(this, slice);
|
|
3259
|
+
return this;
|
|
3260
|
+
}
|
|
3261
|
+
/**
|
|
3262
|
+
Replace the selection with the given node. When `inheritMarks` is
|
|
3263
|
+
true and the content is inline, it inherits the marks from the
|
|
3264
|
+
place where it is inserted.
|
|
3265
|
+
*/
|
|
3266
|
+
replaceSelectionWith(node, inheritMarks = true) {
|
|
3267
|
+
let selection = this.selection;
|
|
3268
|
+
if (inheritMarks)
|
|
3269
|
+
node = node.mark(this.storedMarks || (selection.empty ? selection.$from.marks() : (selection.$from.marksAcross(selection.$to) || Mark.none)));
|
|
3270
|
+
selection.replaceWith(this, node);
|
|
3271
|
+
return this;
|
|
3272
|
+
}
|
|
3273
|
+
/**
|
|
3274
|
+
Delete the selection.
|
|
3275
|
+
*/
|
|
3276
|
+
deleteSelection() {
|
|
3277
|
+
this.selection.replace(this);
|
|
3278
|
+
return this;
|
|
3279
|
+
}
|
|
3280
|
+
/**
|
|
3281
|
+
Replace the given range, or the selection if no range is given,
|
|
3282
|
+
with a text node containing the given string.
|
|
3283
|
+
*/
|
|
3284
|
+
insertText(text, from, to) {
|
|
3285
|
+
let schema = this.doc.type.schema;
|
|
3286
|
+
if (from == null) {
|
|
3287
|
+
if (!text)
|
|
3288
|
+
return this.deleteSelection();
|
|
3289
|
+
return this.replaceSelectionWith(schema.text(text), true);
|
|
3290
|
+
}
|
|
3291
|
+
else {
|
|
3292
|
+
if (to == null)
|
|
3293
|
+
to = from;
|
|
3294
|
+
to = to == null ? from : to;
|
|
3295
|
+
if (!text)
|
|
3296
|
+
return this.deleteRange(from, to);
|
|
3297
|
+
let marks = this.storedMarks;
|
|
3298
|
+
if (!marks) {
|
|
3299
|
+
let $from = this.doc.resolve(from);
|
|
3300
|
+
marks = to == from ? $from.marks() : $from.marksAcross(this.doc.resolve(to));
|
|
3301
|
+
}
|
|
3302
|
+
this.replaceRangeWith(from, to, schema.text(text, marks));
|
|
3303
|
+
if (!this.selection.empty)
|
|
3304
|
+
this.setSelection(Selection.near(this.selection.$to));
|
|
3305
|
+
return this;
|
|
3306
|
+
}
|
|
3307
|
+
}
|
|
3308
|
+
/**
|
|
3309
|
+
Store a metadata property in this transaction, keyed either by
|
|
3310
|
+
name or by plugin.
|
|
3311
|
+
*/
|
|
3312
|
+
setMeta(key, value) {
|
|
3313
|
+
this.meta[typeof key == "string" ? key : key.key] = value;
|
|
3314
|
+
return this;
|
|
3315
|
+
}
|
|
3316
|
+
/**
|
|
3317
|
+
Retrieve a metadata property for a given name or plugin.
|
|
3318
|
+
*/
|
|
3319
|
+
getMeta(key) {
|
|
3320
|
+
return this.meta[typeof key == "string" ? key : key.key];
|
|
3321
|
+
}
|
|
3322
|
+
/**
|
|
3323
|
+
Returns true if this transaction doesn't contain any metadata,
|
|
3324
|
+
and can thus safely be extended.
|
|
3325
|
+
*/
|
|
3326
|
+
get isGeneric() {
|
|
3327
|
+
for (let _ in this.meta)
|
|
3328
|
+
return false;
|
|
3329
|
+
return true;
|
|
3330
|
+
}
|
|
3331
|
+
/**
|
|
3332
|
+
Indicate that the editor should scroll the selection into view
|
|
3333
|
+
when updated to the state produced by this transaction.
|
|
3334
|
+
*/
|
|
3335
|
+
scrollIntoView() {
|
|
3336
|
+
this.updated |= UPDATED_SCROLL;
|
|
3337
|
+
return this;
|
|
3338
|
+
}
|
|
3339
|
+
/**
|
|
3340
|
+
True when this transaction has had `scrollIntoView` called on it.
|
|
3341
|
+
*/
|
|
3342
|
+
get scrolledIntoView() {
|
|
3343
|
+
return (this.updated & UPDATED_SCROLL) > 0;
|
|
3344
|
+
}
|
|
3345
|
+
}
|
|
3346
|
+
|
|
3347
|
+
function bind(f, self) {
|
|
3348
|
+
return !self || !f ? f : f.bind(self);
|
|
3349
|
+
}
|
|
3350
|
+
class FieldDesc {
|
|
3351
|
+
constructor(name, desc, self) {
|
|
3352
|
+
this.name = name;
|
|
3353
|
+
this.init = bind(desc.init, self);
|
|
3354
|
+
this.apply = bind(desc.apply, self);
|
|
3355
|
+
}
|
|
3356
|
+
}
|
|
3357
|
+
[
|
|
3358
|
+
new FieldDesc("doc", {
|
|
3359
|
+
init(config) { return config.doc || config.schema.topNodeType.createAndFill(); },
|
|
3360
|
+
apply(tr) { return tr.doc; }
|
|
3361
|
+
}),
|
|
3362
|
+
new FieldDesc("selection", {
|
|
3363
|
+
init(config, instance) { return config.selection || Selection.atStart(instance.doc); },
|
|
3364
|
+
apply(tr) { return tr.selection; }
|
|
3365
|
+
}),
|
|
3366
|
+
new FieldDesc("storedMarks", {
|
|
3367
|
+
init(config) { return config.storedMarks || null; },
|
|
3368
|
+
apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null; }
|
|
3369
|
+
}),
|
|
3370
|
+
new FieldDesc("scrollToSelection", {
|
|
3371
|
+
init() { return 0; },
|
|
3372
|
+
apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev; }
|
|
3373
|
+
})
|
|
3374
|
+
];
|
|
3375
|
+
|
|
3376
|
+
function bindProps(obj, self, target) {
|
|
3377
|
+
for (let prop in obj) {
|
|
3378
|
+
let val = obj[prop];
|
|
3379
|
+
if (val instanceof Function)
|
|
3380
|
+
val = val.bind(self);
|
|
3381
|
+
else if (prop == "handleDOMEvents")
|
|
3382
|
+
val = bindProps(val, self, {});
|
|
3383
|
+
target[prop] = val;
|
|
3384
|
+
}
|
|
3385
|
+
return target;
|
|
3386
|
+
}
|
|
3387
|
+
/**
|
|
3388
|
+
Plugins bundle functionality that can be added to an editor.
|
|
3389
|
+
They are part of the [editor state](https://prosemirror.net/docs/ref/#state.EditorState) and
|
|
3390
|
+
may influence that state and the view that contains it.
|
|
3391
|
+
*/
|
|
3392
|
+
class Plugin {
|
|
3393
|
+
/**
|
|
3394
|
+
Create a plugin.
|
|
3395
|
+
*/
|
|
3396
|
+
constructor(
|
|
3397
|
+
/**
|
|
3398
|
+
The plugin's [spec object](https://prosemirror.net/docs/ref/#state.PluginSpec).
|
|
3399
|
+
*/
|
|
3400
|
+
spec) {
|
|
3401
|
+
this.spec = spec;
|
|
3402
|
+
/**
|
|
3403
|
+
The [props](https://prosemirror.net/docs/ref/#view.EditorProps) exported by this plugin.
|
|
3404
|
+
*/
|
|
3405
|
+
this.props = {};
|
|
3406
|
+
if (spec.props)
|
|
3407
|
+
bindProps(spec.props, this, this.props);
|
|
3408
|
+
this.key = spec.key ? spec.key.key : createKey("plugin");
|
|
3409
|
+
}
|
|
3410
|
+
/**
|
|
3411
|
+
Extract the plugin's state field from an editor state.
|
|
3412
|
+
*/
|
|
3413
|
+
getState(state) { return state[this.key]; }
|
|
3414
|
+
}
|
|
3415
|
+
const keys = Object.create(null);
|
|
3416
|
+
function createKey(name) {
|
|
3417
|
+
if (name in keys)
|
|
3418
|
+
return name + "$" + ++keys[name];
|
|
3419
|
+
keys[name] = 0;
|
|
3420
|
+
return name + "$";
|
|
3421
|
+
}
|
|
3422
|
+
/**
|
|
3423
|
+
A key is used to [tag](https://prosemirror.net/docs/ref/#state.PluginSpec.key) plugins in a way
|
|
3424
|
+
that makes it possible to find them, given an editor state.
|
|
3425
|
+
Assigning a key does mean only one plugin of that type can be
|
|
3426
|
+
active in a state.
|
|
3427
|
+
*/
|
|
3428
|
+
class PluginKey {
|
|
3429
|
+
/**
|
|
3430
|
+
Create a plugin key.
|
|
3431
|
+
*/
|
|
3432
|
+
constructor(name = "key") { this.key = createKey(name); }
|
|
3433
|
+
/**
|
|
3434
|
+
Get the active plugin with this key, if any, from an editor
|
|
3435
|
+
state.
|
|
3436
|
+
*/
|
|
3437
|
+
get(state) { return state.config.pluginsByKey[this.key]; }
|
|
3438
|
+
/**
|
|
3439
|
+
Get the plugin's state from an editor state.
|
|
3440
|
+
*/
|
|
3441
|
+
getState(state) { return state[this.key]; }
|
|
3442
|
+
}
|
|
3443
|
+
|
|
3444
|
+
const backtickInputRegex = /^```([a-z]+)?[\s\n]$/;
|
|
3445
|
+
const tildeInputRegex = /^~~~([a-z]+)?[\s\n]$/;
|
|
3446
|
+
const CodeBlock = core.Node.create({
|
|
3447
|
+
name: 'codeBlock',
|
|
3448
|
+
addOptions() {
|
|
3449
|
+
return {
|
|
3450
|
+
languageClassPrefix: 'language-',
|
|
3451
|
+
exitOnTripleEnter: true,
|
|
3452
|
+
exitOnArrowDown: true,
|
|
3453
|
+
HTMLAttributes: {},
|
|
3454
|
+
};
|
|
3455
|
+
},
|
|
3456
|
+
content: 'text*',
|
|
3457
|
+
marks: '',
|
|
3458
|
+
group: 'block',
|
|
3459
|
+
code: true,
|
|
3460
|
+
defining: true,
|
|
3461
|
+
addAttributes() {
|
|
3462
|
+
return {
|
|
3463
|
+
language: {
|
|
3464
|
+
default: null,
|
|
3465
|
+
parseHTML: element => {
|
|
3466
|
+
var _a;
|
|
3467
|
+
const { languageClassPrefix } = this.options;
|
|
3468
|
+
const classNames = [...(((_a = element.firstElementChild) === null || _a === void 0 ? void 0 : _a.classList) || [])];
|
|
3469
|
+
const languages = classNames
|
|
3470
|
+
.filter(className => className.startsWith(languageClassPrefix))
|
|
3471
|
+
.map(className => className.replace(languageClassPrefix, ''));
|
|
3472
|
+
const language = languages[0];
|
|
3473
|
+
if (!language) {
|
|
3474
|
+
return null;
|
|
3475
|
+
}
|
|
3476
|
+
return language;
|
|
3477
|
+
},
|
|
3478
|
+
rendered: false,
|
|
3479
|
+
},
|
|
3480
|
+
};
|
|
3481
|
+
},
|
|
3482
|
+
parseHTML() {
|
|
3483
|
+
return [
|
|
3484
|
+
{
|
|
3485
|
+
tag: 'pre',
|
|
3486
|
+
preserveWhitespace: 'full',
|
|
3487
|
+
},
|
|
3488
|
+
];
|
|
3489
|
+
},
|
|
3490
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
3491
|
+
return [
|
|
3492
|
+
'pre',
|
|
3493
|
+
core.mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
|
|
3494
|
+
[
|
|
3495
|
+
'code',
|
|
3496
|
+
{
|
|
3497
|
+
class: node.attrs.language
|
|
3498
|
+
? this.options.languageClassPrefix + node.attrs.language
|
|
3499
|
+
: null,
|
|
3500
|
+
},
|
|
3501
|
+
0,
|
|
3502
|
+
],
|
|
3503
|
+
];
|
|
3504
|
+
},
|
|
3505
|
+
addCommands() {
|
|
3506
|
+
return {
|
|
3507
|
+
setCodeBlock: attributes => ({ commands }) => {
|
|
3508
|
+
return commands.setNode(this.name, attributes);
|
|
3509
|
+
},
|
|
3510
|
+
toggleCodeBlock: attributes => ({ commands }) => {
|
|
3511
|
+
return commands.toggleNode(this.name, 'paragraph', attributes);
|
|
3512
|
+
},
|
|
3513
|
+
};
|
|
3514
|
+
},
|
|
3515
|
+
addKeyboardShortcuts() {
|
|
3516
|
+
return {
|
|
3517
|
+
'Mod-Alt-c': () => this.editor.commands.toggleCodeBlock(),
|
|
3518
|
+
// remove code block when at start of document or code block is empty
|
|
3519
|
+
Backspace: () => {
|
|
3520
|
+
const { empty, $anchor } = this.editor.state.selection;
|
|
3521
|
+
const isAtStart = $anchor.pos === 1;
|
|
3522
|
+
if (!empty || $anchor.parent.type.name !== this.name) {
|
|
3523
|
+
return false;
|
|
3524
|
+
}
|
|
3525
|
+
if (isAtStart || !$anchor.parent.textContent.length) {
|
|
3526
|
+
return this.editor.commands.clearNodes();
|
|
3527
|
+
}
|
|
3528
|
+
return false;
|
|
3529
|
+
},
|
|
3530
|
+
// exit node on triple enter
|
|
3531
|
+
Enter: ({ editor }) => {
|
|
3532
|
+
if (!this.options.exitOnTripleEnter) {
|
|
3533
|
+
return false;
|
|
3534
|
+
}
|
|
3535
|
+
const { state } = editor;
|
|
3536
|
+
const { selection } = state;
|
|
3537
|
+
const { $from, empty } = selection;
|
|
3538
|
+
if (!empty || $from.parent.type !== this.type) {
|
|
3539
|
+
return false;
|
|
3540
|
+
}
|
|
3541
|
+
const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
|
|
3542
|
+
const endsWithDoubleNewline = $from.parent.textContent.endsWith('\n\n');
|
|
3543
|
+
if (!isAtEnd || !endsWithDoubleNewline) {
|
|
3544
|
+
return false;
|
|
3545
|
+
}
|
|
3546
|
+
return editor
|
|
3547
|
+
.chain()
|
|
3548
|
+
.command(({ tr }) => {
|
|
3549
|
+
tr.delete($from.pos - 2, $from.pos);
|
|
3550
|
+
return true;
|
|
3551
|
+
})
|
|
3552
|
+
.exitCode()
|
|
3553
|
+
.run();
|
|
3554
|
+
},
|
|
3555
|
+
// exit node on arrow down
|
|
3556
|
+
ArrowDown: ({ editor }) => {
|
|
3557
|
+
if (!this.options.exitOnArrowDown) {
|
|
3558
|
+
return false;
|
|
3559
|
+
}
|
|
3560
|
+
const { state } = editor;
|
|
3561
|
+
const { selection, doc } = state;
|
|
3562
|
+
const { $from, empty } = selection;
|
|
3563
|
+
if (!empty || $from.parent.type !== this.type) {
|
|
3564
|
+
return false;
|
|
3565
|
+
}
|
|
3566
|
+
const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
|
|
3567
|
+
if (!isAtEnd) {
|
|
3568
|
+
return false;
|
|
3569
|
+
}
|
|
3570
|
+
const after = $from.after();
|
|
3571
|
+
if (after === undefined) {
|
|
3572
|
+
return false;
|
|
3573
|
+
}
|
|
3574
|
+
const nodeAfter = doc.nodeAt(after);
|
|
3575
|
+
if (nodeAfter) {
|
|
3576
|
+
return false;
|
|
3577
|
+
}
|
|
3578
|
+
return editor.commands.exitCode();
|
|
3579
|
+
},
|
|
3580
|
+
};
|
|
3581
|
+
},
|
|
3582
|
+
addInputRules() {
|
|
3583
|
+
return [
|
|
3584
|
+
core.textblockTypeInputRule({
|
|
3585
|
+
find: backtickInputRegex,
|
|
3586
|
+
type: this.type,
|
|
3587
|
+
getAttributes: match => ({
|
|
3588
|
+
language: match[1],
|
|
3589
|
+
}),
|
|
3590
|
+
}),
|
|
3591
|
+
core.textblockTypeInputRule({
|
|
3592
|
+
find: tildeInputRegex,
|
|
3593
|
+
type: this.type,
|
|
3594
|
+
getAttributes: match => ({
|
|
3595
|
+
language: match[1],
|
|
3596
|
+
}),
|
|
3597
|
+
}),
|
|
3598
|
+
];
|
|
3599
|
+
},
|
|
3600
|
+
addProseMirrorPlugins() {
|
|
3601
|
+
return [
|
|
3602
|
+
// this plugin creates a code block for pasted content from VS Code
|
|
3603
|
+
// we can also detect the copied code language
|
|
3604
|
+
new Plugin({
|
|
3605
|
+
key: new PluginKey('codeBlockVSCodeHandler'),
|
|
3606
|
+
props: {
|
|
3607
|
+
handlePaste: (view, event) => {
|
|
3608
|
+
if (!event.clipboardData) {
|
|
3609
|
+
return false;
|
|
3610
|
+
}
|
|
3611
|
+
// don’t create a new code block within code blocks
|
|
3612
|
+
if (this.editor.isActive(this.type.name)) {
|
|
3613
|
+
return false;
|
|
3614
|
+
}
|
|
3615
|
+
const text = event.clipboardData.getData('text/plain');
|
|
3616
|
+
const vscode = event.clipboardData.getData('vscode-editor-data');
|
|
3617
|
+
const vscodeData = vscode ? JSON.parse(vscode) : undefined;
|
|
3618
|
+
const language = vscodeData === null || vscodeData === void 0 ? void 0 : vscodeData.mode;
|
|
3619
|
+
if (!text || !language) {
|
|
3620
|
+
return false;
|
|
3621
|
+
}
|
|
3622
|
+
const { tr } = view.state;
|
|
3623
|
+
// create an empty code block
|
|
3624
|
+
tr.replaceSelectionWith(this.type.create({ language }));
|
|
3625
|
+
// put cursor inside the newly created code block
|
|
3626
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))));
|
|
3627
|
+
// add text to code block
|
|
3628
|
+
// strip carriage return chars from text pasted as code
|
|
3629
|
+
// see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd
|
|
3630
|
+
tr.insertText(text.replace(/\r\n?/g, '\n'));
|
|
3631
|
+
// store meta information
|
|
3632
|
+
// this is useful for other plugins that depends on the paste event
|
|
3633
|
+
// like the paste rule plugin
|
|
3634
|
+
tr.setMeta('paste', true);
|
|
3635
|
+
view.dispatch(tr);
|
|
3636
|
+
return true;
|
|
3637
|
+
},
|
|
3638
|
+
},
|
|
3639
|
+
}),
|
|
3640
|
+
];
|
|
3641
|
+
},
|
|
3642
|
+
});
|
|
188
3643
|
|
|
189
|
-
exports.CodeBlock = CodeBlock;
|
|
3644
|
+
exports.CodeBlock = CodeBlock;
|
|
3645
|
+
exports.backtickInputRegex = backtickInputRegex;
|
|
3646
|
+
exports["default"] = CodeBlock;
|
|
3647
|
+
exports.tildeInputRegex = tildeInputRegex;
|
|
3648
|
+
//# sourceMappingURL=index.cjs.map
|