@tiptap/extension-dropcursor 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 CHANGED
@@ -1,25 +1,2045 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/dropcursor.ts
2
- var _core = require('@tiptap/core');
3
- var _dropcursor = require('@tiptap/pm/dropcursor');
4
- var Dropcursor = _core.Extension.create({
5
- name: "dropCursor",
6
- addOptions() {
7
- return {
8
- color: "currentColor",
9
- width: 1,
10
- class: void 0
11
- };
12
- },
13
- addProseMirrorPlugins() {
14
- return [
15
- _dropcursor.dropCursor.call(void 0, this.options)
16
- ];
17
- }
18
- });
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
+ /**
381
+ Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
382
+ given an invalid replacement.
383
+ */
384
+ class ReplaceError extends Error {
385
+ }
386
+ /*
387
+ ReplaceError = function(this: any, message: string) {
388
+ let err = Error.call(this, message)
389
+ ;(err as any).__proto__ = ReplaceError.prototype
390
+ return err
391
+ } as any
392
+
393
+ ReplaceError.prototype = Object.create(Error.prototype)
394
+ ReplaceError.prototype.constructor = ReplaceError
395
+ ReplaceError.prototype.name = "ReplaceError"
396
+ */
397
+ /**
398
+ A slice represents a piece cut out of a larger document. It
399
+ stores not only a fragment, but also the depth up to which nodes on
400
+ both side are ‘open’ (cut through).
401
+ */
402
+ class Slice {
403
+ /**
404
+ Create a slice. When specifying a non-zero open depth, you must
405
+ make sure that there are nodes of at least that depth at the
406
+ appropriate side of the fragment—i.e. if the fragment is an
407
+ empty paragraph node, `openStart` and `openEnd` can't be greater
408
+ than 1.
409
+
410
+ It is not necessary for the content of open nodes to conform to
411
+ the schema's content constraints, though it should be a valid
412
+ start/end/middle for such a node, depending on which sides are
413
+ open.
414
+ */
415
+ constructor(
416
+ /**
417
+ The slice's content.
418
+ */
419
+ content,
420
+ /**
421
+ The open depth at the start of the fragment.
422
+ */
423
+ openStart,
424
+ /**
425
+ The open depth at the end.
426
+ */
427
+ openEnd) {
428
+ this.content = content;
429
+ this.openStart = openStart;
430
+ this.openEnd = openEnd;
431
+ }
432
+ /**
433
+ The size this slice would add when inserted into a document.
434
+ */
435
+ get size() {
436
+ return this.content.size - this.openStart - this.openEnd;
437
+ }
438
+ /**
439
+ @internal
440
+ */
441
+ insertAt(pos, fragment) {
442
+ let content = insertInto(this.content, pos + this.openStart, fragment);
443
+ return content && new Slice(content, this.openStart, this.openEnd);
444
+ }
445
+ /**
446
+ @internal
447
+ */
448
+ removeBetween(from, to) {
449
+ return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
450
+ }
451
+ /**
452
+ Tests whether this slice is equal to another slice.
453
+ */
454
+ eq(other) {
455
+ return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
456
+ }
457
+ /**
458
+ @internal
459
+ */
460
+ toString() {
461
+ return this.content + "(" + this.openStart + "," + this.openEnd + ")";
462
+ }
463
+ /**
464
+ Convert a slice to a JSON-serializable representation.
465
+ */
466
+ toJSON() {
467
+ if (!this.content.size)
468
+ return null;
469
+ let json = { content: this.content.toJSON() };
470
+ if (this.openStart > 0)
471
+ json.openStart = this.openStart;
472
+ if (this.openEnd > 0)
473
+ json.openEnd = this.openEnd;
474
+ return json;
475
+ }
476
+ /**
477
+ Deserialize a slice from its JSON representation.
478
+ */
479
+ static fromJSON(schema, json) {
480
+ if (!json)
481
+ return Slice.empty;
482
+ let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
483
+ if (typeof openStart != "number" || typeof openEnd != "number")
484
+ throw new RangeError("Invalid input for Slice.fromJSON");
485
+ return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
486
+ }
487
+ /**
488
+ Create a slice from a fragment by taking the maximum possible
489
+ open value on both side of the fragment.
490
+ */
491
+ static maxOpen(fragment, openIsolating = true) {
492
+ let openStart = 0, openEnd = 0;
493
+ for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
494
+ openStart++;
495
+ for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
496
+ openEnd++;
497
+ return new Slice(fragment, openStart, openEnd);
498
+ }
499
+ }
500
+ /**
501
+ The empty slice.
502
+ */
503
+ Slice.empty = new Slice(Fragment.empty, 0, 0);
504
+ function removeRange(content, from, to) {
505
+ let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
506
+ let { index: indexTo, offset: offsetTo } = content.findIndex(to);
507
+ if (offset == from || child.isText) {
508
+ if (offsetTo != to && !content.child(indexTo).isText)
509
+ throw new RangeError("Removing non-flat range");
510
+ return content.cut(0, from).append(content.cut(to));
511
+ }
512
+ if (index != indexTo)
513
+ throw new RangeError("Removing non-flat range");
514
+ return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
515
+ }
516
+ function insertInto(content, dist, insert, parent) {
517
+ let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
518
+ if (offset == dist || child.isText) {
519
+ if (parent && !parent.canReplace(index, index, insert))
520
+ return null;
521
+ return content.cut(0, dist).append(insert).append(content.cut(dist));
522
+ }
523
+ let inner = insertInto(child.content, dist - offset - 1, insert);
524
+ return inner && content.replaceChild(index, child.copy(inner));
525
+ }
526
+
527
+ // Recovery values encode a range index and an offset. They are
528
+ // represented as numbers, because tons of them will be created when
529
+ // mapping, for example, a large number of decorations. The number's
530
+ // lower 16 bits provide the index, the remaining bits the offset.
531
+ //
532
+ // Note: We intentionally don't use bit shift operators to en- and
533
+ // decode these, since those clip to 32 bits, which we might in rare
534
+ // cases want to overflow. A 64-bit float can represent 48-bit
535
+ // integers precisely.
536
+ const lower16 = 0xffff;
537
+ const factor16 = Math.pow(2, 16);
538
+ function makeRecover(index, offset) { return index + offset * factor16; }
539
+ function recoverIndex(value) { return value & lower16; }
540
+ function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
541
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
542
+ /**
543
+ An object representing a mapped position with extra
544
+ information.
545
+ */
546
+ class MapResult {
547
+ /**
548
+ @internal
549
+ */
550
+ constructor(
551
+ /**
552
+ The mapped version of the position.
553
+ */
554
+ pos,
555
+ /**
556
+ @internal
557
+ */
558
+ delInfo,
559
+ /**
560
+ @internal
561
+ */
562
+ recover) {
563
+ this.pos = pos;
564
+ this.delInfo = delInfo;
565
+ this.recover = recover;
566
+ }
567
+ /**
568
+ Tells you whether the position was deleted, that is, whether the
569
+ step removed the token on the side queried (via the `assoc`)
570
+ argument from the document.
571
+ */
572
+ get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
573
+ /**
574
+ Tells you whether the token before the mapped position was deleted.
575
+ */
576
+ get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
577
+ /**
578
+ True when the token after the mapped position was deleted.
579
+ */
580
+ get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
581
+ /**
582
+ Tells whether any of the steps mapped through deletes across the
583
+ position (including both the token before and after the
584
+ position).
585
+ */
586
+ get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
587
+ }
588
+ /**
589
+ A map describing the deletions and insertions made by a step, which
590
+ can be used to find the correspondence between positions in the
591
+ pre-step version of a document and the same position in the
592
+ post-step version.
593
+ */
594
+ class StepMap {
595
+ /**
596
+ Create a position map. The modifications to the document are
597
+ represented as an array of numbers, in which each group of three
598
+ represents a modified chunk as `[start, oldSize, newSize]`.
599
+ */
600
+ constructor(
601
+ /**
602
+ @internal
603
+ */
604
+ ranges,
605
+ /**
606
+ @internal
607
+ */
608
+ inverted = false) {
609
+ this.ranges = ranges;
610
+ this.inverted = inverted;
611
+ if (!ranges.length && StepMap.empty)
612
+ return StepMap.empty;
613
+ }
614
+ /**
615
+ @internal
616
+ */
617
+ recover(value) {
618
+ let diff = 0, index = recoverIndex(value);
619
+ if (!this.inverted)
620
+ for (let i = 0; i < index; i++)
621
+ diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
622
+ return this.ranges[index * 3] + diff + recoverOffset(value);
623
+ }
624
+ mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
625
+ map(pos, assoc = 1) { return this._map(pos, assoc, true); }
626
+ /**
627
+ @internal
628
+ */
629
+ _map(pos, assoc, simple) {
630
+ let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
631
+ for (let i = 0; i < this.ranges.length; i += 3) {
632
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
633
+ if (start > pos)
634
+ break;
635
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
636
+ if (pos <= end) {
637
+ let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
638
+ let result = start + diff + (side < 0 ? 0 : newSize);
639
+ if (simple)
640
+ return result;
641
+ let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
642
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
643
+ if (assoc < 0 ? pos != start : pos != end)
644
+ del |= DEL_SIDE;
645
+ return new MapResult(result, del, recover);
646
+ }
647
+ diff += newSize - oldSize;
648
+ }
649
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
650
+ }
651
+ /**
652
+ @internal
653
+ */
654
+ touches(pos, recover) {
655
+ let diff = 0, index = recoverIndex(recover);
656
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
657
+ for (let i = 0; i < this.ranges.length; i += 3) {
658
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
659
+ if (start > pos)
660
+ break;
661
+ let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
662
+ if (pos <= end && i == index * 3)
663
+ return true;
664
+ diff += this.ranges[i + newIndex] - oldSize;
665
+ }
666
+ return false;
667
+ }
668
+ /**
669
+ Calls the given function on each of the changed ranges included in
670
+ this map.
671
+ */
672
+ forEach(f) {
673
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
674
+ for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
675
+ let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
676
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
677
+ f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
678
+ diff += newSize - oldSize;
679
+ }
680
+ }
681
+ /**
682
+ Create an inverted version of this map. The result can be used to
683
+ map positions in the post-step document to the pre-step document.
684
+ */
685
+ invert() {
686
+ return new StepMap(this.ranges, !this.inverted);
687
+ }
688
+ /**
689
+ @internal
690
+ */
691
+ toString() {
692
+ return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
693
+ }
694
+ /**
695
+ Create a map that moves all positions by offset `n` (which may be
696
+ negative). This can be useful when applying steps meant for a
697
+ sub-document to a larger document, or vice-versa.
698
+ */
699
+ static offset(n) {
700
+ return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
701
+ }
702
+ }
703
+ /**
704
+ A StepMap that contains no changed ranges.
705
+ */
706
+ StepMap.empty = new StepMap([]);
707
+
708
+ const stepsByID = Object.create(null);
709
+ /**
710
+ A step object represents an atomic change. It generally applies
711
+ only to the document it was created for, since the positions
712
+ stored in it will only make sense for that document.
19
713
 
20
- // src/index.ts
21
- var src_default = Dropcursor;
714
+ New steps are defined by creating classes that extend `Step`,
715
+ overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
716
+ methods, and registering your class with a unique
717
+ JSON-serialization identifier using
718
+ [`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
719
+ */
720
+ class Step {
721
+ /**
722
+ Get the step map that represents the changes made by this step,
723
+ and which can be used to transform between positions in the old
724
+ and the new document.
725
+ */
726
+ getMap() { return StepMap.empty; }
727
+ /**
728
+ Try to merge this step with another one, to be applied directly
729
+ after it. Returns the merged step when possible, null if the
730
+ steps can't be merged.
731
+ */
732
+ merge(other) { return null; }
733
+ /**
734
+ Deserialize a step from its JSON representation. Will call
735
+ through to the step class' own implementation of this method.
736
+ */
737
+ static fromJSON(schema, json) {
738
+ if (!json || !json.stepType)
739
+ throw new RangeError("Invalid input for Step.fromJSON");
740
+ let type = stepsByID[json.stepType];
741
+ if (!type)
742
+ throw new RangeError(`No step type ${json.stepType} defined`);
743
+ return type.fromJSON(schema, json);
744
+ }
745
+ /**
746
+ To be able to serialize steps to JSON, each step needs a string
747
+ ID to attach to its JSON representation. Use this method to
748
+ register an ID for your step classes. Try to pick something
749
+ that's unlikely to clash with steps from other modules.
750
+ */
751
+ static jsonID(id, stepClass) {
752
+ if (id in stepsByID)
753
+ throw new RangeError("Duplicate use of step JSON ID " + id);
754
+ stepsByID[id] = stepClass;
755
+ stepClass.prototype.jsonID = id;
756
+ return stepClass;
757
+ }
758
+ }
759
+ /**
760
+ The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
761
+ new document or a failure value.
762
+ */
763
+ class StepResult {
764
+ /**
765
+ @internal
766
+ */
767
+ constructor(
768
+ /**
769
+ The transformed document, if successful.
770
+ */
771
+ doc,
772
+ /**
773
+ The failure message, if unsuccessful.
774
+ */
775
+ failed) {
776
+ this.doc = doc;
777
+ this.failed = failed;
778
+ }
779
+ /**
780
+ Create a successful step result.
781
+ */
782
+ static ok(doc) { return new StepResult(doc, null); }
783
+ /**
784
+ Create a failed step result.
785
+ */
786
+ static fail(message) { return new StepResult(null, message); }
787
+ /**
788
+ Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
789
+ arguments. Create a successful result if it succeeds, and a
790
+ failed one if it throws a `ReplaceError`.
791
+ */
792
+ static fromReplace(doc, from, to, slice) {
793
+ try {
794
+ return StepResult.ok(doc.replace(from, to, slice));
795
+ }
796
+ catch (e) {
797
+ if (e instanceof ReplaceError)
798
+ return StepResult.fail(e.message);
799
+ throw e;
800
+ }
801
+ }
802
+ }
22
803
 
804
+ function mapFragment(fragment, f, parent) {
805
+ let mapped = [];
806
+ for (let i = 0; i < fragment.childCount; i++) {
807
+ let child = fragment.child(i);
808
+ if (child.content.size)
809
+ child = child.copy(mapFragment(child.content, f, child));
810
+ if (child.isInline)
811
+ child = f(child, parent, i);
812
+ mapped.push(child);
813
+ }
814
+ return Fragment.fromArray(mapped);
815
+ }
816
+ /**
817
+ Add a mark to all inline content between two positions.
818
+ */
819
+ class AddMarkStep extends Step {
820
+ /**
821
+ Create a mark step.
822
+ */
823
+ constructor(
824
+ /**
825
+ The start of the marked range.
826
+ */
827
+ from,
828
+ /**
829
+ The end of the marked range.
830
+ */
831
+ to,
832
+ /**
833
+ The mark to add.
834
+ */
835
+ mark) {
836
+ super();
837
+ this.from = from;
838
+ this.to = to;
839
+ this.mark = mark;
840
+ }
841
+ apply(doc) {
842
+ let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
843
+ let parent = $from.node($from.sharedDepth(this.to));
844
+ let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
845
+ if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type))
846
+ return node;
847
+ return node.mark(this.mark.addToSet(node.marks));
848
+ }, parent), oldSlice.openStart, oldSlice.openEnd);
849
+ return StepResult.fromReplace(doc, this.from, this.to, slice);
850
+ }
851
+ invert() {
852
+ return new RemoveMarkStep(this.from, this.to, this.mark);
853
+ }
854
+ map(mapping) {
855
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
856
+ if (from.deleted && to.deleted || from.pos >= to.pos)
857
+ return null;
858
+ return new AddMarkStep(from.pos, to.pos, this.mark);
859
+ }
860
+ merge(other) {
861
+ if (other instanceof AddMarkStep &&
862
+ other.mark.eq(this.mark) &&
863
+ this.from <= other.to && this.to >= other.from)
864
+ return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
865
+ return null;
866
+ }
867
+ toJSON() {
868
+ return { stepType: "addMark", mark: this.mark.toJSON(),
869
+ from: this.from, to: this.to };
870
+ }
871
+ /**
872
+ @internal
873
+ */
874
+ static fromJSON(schema, json) {
875
+ if (typeof json.from != "number" || typeof json.to != "number")
876
+ throw new RangeError("Invalid input for AddMarkStep.fromJSON");
877
+ return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
878
+ }
879
+ }
880
+ Step.jsonID("addMark", AddMarkStep);
881
+ /**
882
+ Remove a mark from all inline content between two positions.
883
+ */
884
+ class RemoveMarkStep extends Step {
885
+ /**
886
+ Create a mark-removing step.
887
+ */
888
+ constructor(
889
+ /**
890
+ The start of the unmarked range.
891
+ */
892
+ from,
893
+ /**
894
+ The end of the unmarked range.
895
+ */
896
+ to,
897
+ /**
898
+ The mark to remove.
899
+ */
900
+ mark) {
901
+ super();
902
+ this.from = from;
903
+ this.to = to;
904
+ this.mark = mark;
905
+ }
906
+ apply(doc) {
907
+ let oldSlice = doc.slice(this.from, this.to);
908
+ let slice = new Slice(mapFragment(oldSlice.content, node => {
909
+ return node.mark(this.mark.removeFromSet(node.marks));
910
+ }, doc), oldSlice.openStart, oldSlice.openEnd);
911
+ return StepResult.fromReplace(doc, this.from, this.to, slice);
912
+ }
913
+ invert() {
914
+ return new AddMarkStep(this.from, this.to, this.mark);
915
+ }
916
+ map(mapping) {
917
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
918
+ if (from.deleted && to.deleted || from.pos >= to.pos)
919
+ return null;
920
+ return new RemoveMarkStep(from.pos, to.pos, this.mark);
921
+ }
922
+ merge(other) {
923
+ if (other instanceof RemoveMarkStep &&
924
+ other.mark.eq(this.mark) &&
925
+ this.from <= other.to && this.to >= other.from)
926
+ return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
927
+ return null;
928
+ }
929
+ toJSON() {
930
+ return { stepType: "removeMark", mark: this.mark.toJSON(),
931
+ from: this.from, to: this.to };
932
+ }
933
+ /**
934
+ @internal
935
+ */
936
+ static fromJSON(schema, json) {
937
+ if (typeof json.from != "number" || typeof json.to != "number")
938
+ throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
939
+ return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
940
+ }
941
+ }
942
+ Step.jsonID("removeMark", RemoveMarkStep);
943
+ /**
944
+ Add a mark to a specific node.
945
+ */
946
+ class AddNodeMarkStep extends Step {
947
+ /**
948
+ Create a node mark step.
949
+ */
950
+ constructor(
951
+ /**
952
+ The position of the target node.
953
+ */
954
+ pos,
955
+ /**
956
+ The mark to add.
957
+ */
958
+ mark) {
959
+ super();
960
+ this.pos = pos;
961
+ this.mark = mark;
962
+ }
963
+ apply(doc) {
964
+ let node = doc.nodeAt(this.pos);
965
+ if (!node)
966
+ return StepResult.fail("No node at mark step's position");
967
+ let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
968
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
969
+ }
970
+ invert(doc) {
971
+ let node = doc.nodeAt(this.pos);
972
+ if (node) {
973
+ let newSet = this.mark.addToSet(node.marks);
974
+ if (newSet.length == node.marks.length) {
975
+ for (let i = 0; i < node.marks.length; i++)
976
+ if (!node.marks[i].isInSet(newSet))
977
+ return new AddNodeMarkStep(this.pos, node.marks[i]);
978
+ return new AddNodeMarkStep(this.pos, this.mark);
979
+ }
980
+ }
981
+ return new RemoveNodeMarkStep(this.pos, this.mark);
982
+ }
983
+ map(mapping) {
984
+ let pos = mapping.mapResult(this.pos, 1);
985
+ return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
986
+ }
987
+ toJSON() {
988
+ return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
989
+ }
990
+ /**
991
+ @internal
992
+ */
993
+ static fromJSON(schema, json) {
994
+ if (typeof json.pos != "number")
995
+ throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
996
+ return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
997
+ }
998
+ }
999
+ Step.jsonID("addNodeMark", AddNodeMarkStep);
1000
+ /**
1001
+ Remove a mark from a specific node.
1002
+ */
1003
+ class RemoveNodeMarkStep extends Step {
1004
+ /**
1005
+ Create a mark-removing step.
1006
+ */
1007
+ constructor(
1008
+ /**
1009
+ The position of the target node.
1010
+ */
1011
+ pos,
1012
+ /**
1013
+ The mark to remove.
1014
+ */
1015
+ mark) {
1016
+ super();
1017
+ this.pos = pos;
1018
+ this.mark = mark;
1019
+ }
1020
+ apply(doc) {
1021
+ let node = doc.nodeAt(this.pos);
1022
+ if (!node)
1023
+ return StepResult.fail("No node at mark step's position");
1024
+ let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
1025
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1026
+ }
1027
+ invert(doc) {
1028
+ let node = doc.nodeAt(this.pos);
1029
+ if (!node || !this.mark.isInSet(node.marks))
1030
+ return this;
1031
+ return new AddNodeMarkStep(this.pos, this.mark);
1032
+ }
1033
+ map(mapping) {
1034
+ let pos = mapping.mapResult(this.pos, 1);
1035
+ return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
1036
+ }
1037
+ toJSON() {
1038
+ return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1039
+ }
1040
+ /**
1041
+ @internal
1042
+ */
1043
+ static fromJSON(schema, json) {
1044
+ if (typeof json.pos != "number")
1045
+ throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
1046
+ return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1047
+ }
1048
+ }
1049
+ Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
23
1050
 
1051
+ /**
1052
+ Replace a part of the document with a slice of new content.
1053
+ */
1054
+ class ReplaceStep extends Step {
1055
+ /**
1056
+ The given `slice` should fit the 'gap' between `from` and
1057
+ `to`—the depths must line up, and the surrounding nodes must be
1058
+ able to be joined with the open sides of the slice. When
1059
+ `structure` is true, the step will fail if the content between
1060
+ from and to is not just a sequence of closing and then opening
1061
+ tokens (this is to guard against rebased replace steps
1062
+ overwriting something they weren't supposed to).
1063
+ */
1064
+ constructor(
1065
+ /**
1066
+ The start position of the replaced range.
1067
+ */
1068
+ from,
1069
+ /**
1070
+ The end position of the replaced range.
1071
+ */
1072
+ to,
1073
+ /**
1074
+ The slice to insert.
1075
+ */
1076
+ slice,
1077
+ /**
1078
+ @internal
1079
+ */
1080
+ structure = false) {
1081
+ super();
1082
+ this.from = from;
1083
+ this.to = to;
1084
+ this.slice = slice;
1085
+ this.structure = structure;
1086
+ }
1087
+ apply(doc) {
1088
+ if (this.structure && contentBetween(doc, this.from, this.to))
1089
+ return StepResult.fail("Structure replace would overwrite content");
1090
+ return StepResult.fromReplace(doc, this.from, this.to, this.slice);
1091
+ }
1092
+ getMap() {
1093
+ return new StepMap([this.from, this.to - this.from, this.slice.size]);
1094
+ }
1095
+ invert(doc) {
1096
+ return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
1097
+ }
1098
+ map(mapping) {
1099
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1100
+ if (from.deletedAcross && to.deletedAcross)
1101
+ return null;
1102
+ return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
1103
+ }
1104
+ merge(other) {
1105
+ if (!(other instanceof ReplaceStep) || other.structure || this.structure)
1106
+ return null;
1107
+ if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
1108
+ let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1109
+ : new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
1110
+ return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
1111
+ }
1112
+ else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
1113
+ let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1114
+ : new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
1115
+ return new ReplaceStep(other.from, this.to, slice, this.structure);
1116
+ }
1117
+ else {
1118
+ return null;
1119
+ }
1120
+ }
1121
+ toJSON() {
1122
+ let json = { stepType: "replace", from: this.from, to: this.to };
1123
+ if (this.slice.size)
1124
+ json.slice = this.slice.toJSON();
1125
+ if (this.structure)
1126
+ json.structure = true;
1127
+ return json;
1128
+ }
1129
+ /**
1130
+ @internal
1131
+ */
1132
+ static fromJSON(schema, json) {
1133
+ if (typeof json.from != "number" || typeof json.to != "number")
1134
+ throw new RangeError("Invalid input for ReplaceStep.fromJSON");
1135
+ return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
1136
+ }
1137
+ }
1138
+ Step.jsonID("replace", ReplaceStep);
1139
+ /**
1140
+ Replace a part of the document with a slice of content, but
1141
+ preserve a range of the replaced content by moving it into the
1142
+ slice.
1143
+ */
1144
+ class ReplaceAroundStep extends Step {
1145
+ /**
1146
+ Create a replace-around step with the given range and gap.
1147
+ `insert` should be the point in the slice into which the content
1148
+ of the gap should be moved. `structure` has the same meaning as
1149
+ it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
1150
+ */
1151
+ constructor(
1152
+ /**
1153
+ The start position of the replaced range.
1154
+ */
1155
+ from,
1156
+ /**
1157
+ The end position of the replaced range.
1158
+ */
1159
+ to,
1160
+ /**
1161
+ The start of preserved range.
1162
+ */
1163
+ gapFrom,
1164
+ /**
1165
+ The end of preserved range.
1166
+ */
1167
+ gapTo,
1168
+ /**
1169
+ The slice to insert.
1170
+ */
1171
+ slice,
1172
+ /**
1173
+ The position in the slice where the preserved range should be
1174
+ inserted.
1175
+ */
1176
+ insert,
1177
+ /**
1178
+ @internal
1179
+ */
1180
+ structure = false) {
1181
+ super();
1182
+ this.from = from;
1183
+ this.to = to;
1184
+ this.gapFrom = gapFrom;
1185
+ this.gapTo = gapTo;
1186
+ this.slice = slice;
1187
+ this.insert = insert;
1188
+ this.structure = structure;
1189
+ }
1190
+ apply(doc) {
1191
+ if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
1192
+ contentBetween(doc, this.gapTo, this.to)))
1193
+ return StepResult.fail("Structure gap-replace would overwrite content");
1194
+ let gap = doc.slice(this.gapFrom, this.gapTo);
1195
+ if (gap.openStart || gap.openEnd)
1196
+ return StepResult.fail("Gap is not a flat range");
1197
+ let inserted = this.slice.insertAt(this.insert, gap.content);
1198
+ if (!inserted)
1199
+ return StepResult.fail("Content does not fit in gap");
1200
+ return StepResult.fromReplace(doc, this.from, this.to, inserted);
1201
+ }
1202
+ getMap() {
1203
+ return new StepMap([this.from, this.gapFrom - this.from, this.insert,
1204
+ this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
1205
+ }
1206
+ invert(doc) {
1207
+ let gap = this.gapTo - this.gapFrom;
1208
+ 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);
1209
+ }
1210
+ map(mapping) {
1211
+ let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1212
+ let gapFrom = mapping.map(this.gapFrom, -1), gapTo = mapping.map(this.gapTo, 1);
1213
+ if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
1214
+ return null;
1215
+ return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
1216
+ }
1217
+ toJSON() {
1218
+ let json = { stepType: "replaceAround", from: this.from, to: this.to,
1219
+ gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert };
1220
+ if (this.slice.size)
1221
+ json.slice = this.slice.toJSON();
1222
+ if (this.structure)
1223
+ json.structure = true;
1224
+ return json;
1225
+ }
1226
+ /**
1227
+ @internal
1228
+ */
1229
+ static fromJSON(schema, json) {
1230
+ if (typeof json.from != "number" || typeof json.to != "number" ||
1231
+ typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
1232
+ throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
1233
+ return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
1234
+ }
1235
+ }
1236
+ Step.jsonID("replaceAround", ReplaceAroundStep);
1237
+ function contentBetween(doc, from, to) {
1238
+ let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
1239
+ while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
1240
+ depth--;
1241
+ dist--;
1242
+ }
1243
+ if (dist > 0) {
1244
+ let next = $from.node(depth).maybeChild($from.indexAfter(depth));
1245
+ while (dist > 0) {
1246
+ if (!next || next.isLeaf)
1247
+ return true;
1248
+ next = next.firstChild;
1249
+ dist--;
1250
+ }
1251
+ }
1252
+ return false;
1253
+ }
1254
+ /**
1255
+ Finds a position at or around the given position where the given
1256
+ slice can be inserted. Will look at parent nodes' nearest boundary
1257
+ and try there, even if the original position wasn't directly at the
1258
+ start or end of that node. Returns null when no position was found.
1259
+ */
1260
+ function dropPoint(doc, pos, slice) {
1261
+ let $pos = doc.resolve(pos);
1262
+ if (!slice.content.size)
1263
+ return pos;
1264
+ let content = slice.content;
1265
+ for (let i = 0; i < slice.openStart; i++)
1266
+ content = content.firstChild.content;
1267
+ for (let pass = 1; pass <= (slice.openStart == 0 && slice.size ? 2 : 1); pass++) {
1268
+ for (let d = $pos.depth; d >= 0; d--) {
1269
+ let bias = d == $pos.depth ? 0 : $pos.pos <= ($pos.start(d + 1) + $pos.end(d + 1)) / 2 ? -1 : 1;
1270
+ let insertPos = $pos.index(d) + (bias > 0 ? 1 : 0);
1271
+ let parent = $pos.node(d), fits = false;
1272
+ if (pass == 1) {
1273
+ fits = parent.canReplace(insertPos, insertPos, content);
1274
+ }
1275
+ else {
1276
+ let wrapping = parent.contentMatchAt(insertPos).findWrapping(content.firstChild.type);
1277
+ fits = wrapping && parent.canReplaceWith(insertPos, insertPos, wrapping[0]);
1278
+ }
1279
+ if (fits)
1280
+ return bias == 0 ? $pos.pos : bias < 0 ? $pos.before(d + 1) : $pos.after(d + 1);
1281
+ }
1282
+ }
1283
+ return null;
1284
+ }
1285
+
1286
+ /**
1287
+ Update an attribute in a specific node.
1288
+ */
1289
+ class AttrStep extends Step {
1290
+ /**
1291
+ Construct an attribute step.
1292
+ */
1293
+ constructor(
1294
+ /**
1295
+ The position of the target node.
1296
+ */
1297
+ pos,
1298
+ /**
1299
+ The attribute to set.
1300
+ */
1301
+ attr,
1302
+ // The attribute's new value.
1303
+ value) {
1304
+ super();
1305
+ this.pos = pos;
1306
+ this.attr = attr;
1307
+ this.value = value;
1308
+ }
1309
+ apply(doc) {
1310
+ let node = doc.nodeAt(this.pos);
1311
+ if (!node)
1312
+ return StepResult.fail("No node at attribute step's position");
1313
+ let attrs = Object.create(null);
1314
+ for (let name in node.attrs)
1315
+ attrs[name] = node.attrs[name];
1316
+ attrs[this.attr] = this.value;
1317
+ let updated = node.type.create(attrs, null, node.marks);
1318
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1319
+ }
1320
+ getMap() {
1321
+ return StepMap.empty;
1322
+ }
1323
+ invert(doc) {
1324
+ return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1325
+ }
1326
+ map(mapping) {
1327
+ let pos = mapping.mapResult(this.pos, 1);
1328
+ return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1329
+ }
1330
+ toJSON() {
1331
+ return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
1332
+ }
1333
+ static fromJSON(schema, json) {
1334
+ if (typeof json.pos != "number" || typeof json.attr != "string")
1335
+ throw new RangeError("Invalid input for AttrStep.fromJSON");
1336
+ return new AttrStep(json.pos, json.attr, json.value);
1337
+ }
1338
+ }
1339
+ Step.jsonID("attr", AttrStep);
1340
+
1341
+ /**
1342
+ @internal
1343
+ */
1344
+ let TransformError = class extends Error {
1345
+ };
1346
+ TransformError = function TransformError(message) {
1347
+ let err = Error.call(this, message);
1348
+ err.__proto__ = TransformError.prototype;
1349
+ return err;
1350
+ };
1351
+ TransformError.prototype = Object.create(Error.prototype);
1352
+ TransformError.prototype.constructor = TransformError;
1353
+ TransformError.prototype.name = "TransformError";
1354
+
1355
+ const classesById = Object.create(null);
1356
+ /**
1357
+ Superclass for editor selections. Every selection type should
1358
+ extend this. Should not be instantiated directly.
1359
+ */
1360
+ class Selection {
1361
+ /**
1362
+ Initialize a selection with the head and anchor and ranges. If no
1363
+ ranges are given, constructs a single range across `$anchor` and
1364
+ `$head`.
1365
+ */
1366
+ constructor(
1367
+ /**
1368
+ The resolved anchor of the selection (the side that stays in
1369
+ place when the selection is modified).
1370
+ */
1371
+ $anchor,
1372
+ /**
1373
+ The resolved head of the selection (the side that moves when
1374
+ the selection is modified).
1375
+ */
1376
+ $head, ranges) {
1377
+ this.$anchor = $anchor;
1378
+ this.$head = $head;
1379
+ this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
1380
+ }
1381
+ /**
1382
+ The selection's anchor, as an unresolved position.
1383
+ */
1384
+ get anchor() { return this.$anchor.pos; }
1385
+ /**
1386
+ The selection's head.
1387
+ */
1388
+ get head() { return this.$head.pos; }
1389
+ /**
1390
+ The lower bound of the selection's main range.
1391
+ */
1392
+ get from() { return this.$from.pos; }
1393
+ /**
1394
+ The upper bound of the selection's main range.
1395
+ */
1396
+ get to() { return this.$to.pos; }
1397
+ /**
1398
+ The resolved lower bound of the selection's main range.
1399
+ */
1400
+ get $from() {
1401
+ return this.ranges[0].$from;
1402
+ }
1403
+ /**
1404
+ The resolved upper bound of the selection's main range.
1405
+ */
1406
+ get $to() {
1407
+ return this.ranges[0].$to;
1408
+ }
1409
+ /**
1410
+ Indicates whether the selection contains any content.
1411
+ */
1412
+ get empty() {
1413
+ let ranges = this.ranges;
1414
+ for (let i = 0; i < ranges.length; i++)
1415
+ if (ranges[i].$from.pos != ranges[i].$to.pos)
1416
+ return false;
1417
+ return true;
1418
+ }
1419
+ /**
1420
+ Get the content of this selection as a slice.
1421
+ */
1422
+ content() {
1423
+ return this.$from.doc.slice(this.from, this.to, true);
1424
+ }
1425
+ /**
1426
+ Replace the selection with a slice or, if no slice is given,
1427
+ delete the selection. Will append to the given transaction.
1428
+ */
1429
+ replace(tr, content = Slice.empty) {
1430
+ // Put the new selection at the position after the inserted
1431
+ // content. When that ended in an inline node, search backwards,
1432
+ // to get the position after that node. If not, search forward.
1433
+ let lastNode = content.content.lastChild, lastParent = null;
1434
+ for (let i = 0; i < content.openEnd; i++) {
1435
+ lastParent = lastNode;
1436
+ lastNode = lastNode.lastChild;
1437
+ }
1438
+ let mapFrom = tr.steps.length, ranges = this.ranges;
1439
+ for (let i = 0; i < ranges.length; i++) {
1440
+ let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
1441
+ tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
1442
+ if (i == 0)
1443
+ selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
1444
+ }
1445
+ }
1446
+ /**
1447
+ Replace the selection with the given node, appending the changes
1448
+ to the given transaction.
1449
+ */
1450
+ replaceWith(tr, node) {
1451
+ let mapFrom = tr.steps.length, ranges = this.ranges;
1452
+ for (let i = 0; i < ranges.length; i++) {
1453
+ let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
1454
+ let from = mapping.map($from.pos), to = mapping.map($to.pos);
1455
+ if (i) {
1456
+ tr.deleteRange(from, to);
1457
+ }
1458
+ else {
1459
+ tr.replaceRangeWith(from, to, node);
1460
+ selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
1461
+ }
1462
+ }
1463
+ }
1464
+ /**
1465
+ Find a valid cursor or leaf node selection starting at the given
1466
+ position and searching back if `dir` is negative, and forward if
1467
+ positive. When `textOnly` is true, only consider cursor
1468
+ selections. Will return null when no valid selection position is
1469
+ found.
1470
+ */
1471
+ static findFrom($pos, dir, textOnly = false) {
1472
+ let inner = $pos.parent.inlineContent ? new TextSelection($pos)
1473
+ : findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
1474
+ if (inner)
1475
+ return inner;
1476
+ for (let depth = $pos.depth - 1; depth >= 0; depth--) {
1477
+ let found = dir < 0
1478
+ ? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly)
1479
+ : findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
1480
+ if (found)
1481
+ return found;
1482
+ }
1483
+ return null;
1484
+ }
1485
+ /**
1486
+ Find a valid cursor or leaf node selection near the given
1487
+ position. Searches forward first by default, but if `bias` is
1488
+ negative, it will search backwards first.
1489
+ */
1490
+ static near($pos, bias = 1) {
1491
+ return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
1492
+ }
1493
+ /**
1494
+ Find the cursor or leaf node selection closest to the start of
1495
+ the given document. Will return an
1496
+ [`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
1497
+ exists.
1498
+ */
1499
+ static atStart(doc) {
1500
+ return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
1501
+ }
1502
+ /**
1503
+ Find the cursor or leaf node selection closest to the end of the
1504
+ given document.
1505
+ */
1506
+ static atEnd(doc) {
1507
+ return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
1508
+ }
1509
+ /**
1510
+ Deserialize the JSON representation of a selection. Must be
1511
+ implemented for custom classes (as a static class method).
1512
+ */
1513
+ static fromJSON(doc, json) {
1514
+ if (!json || !json.type)
1515
+ throw new RangeError("Invalid input for Selection.fromJSON");
1516
+ let cls = classesById[json.type];
1517
+ if (!cls)
1518
+ throw new RangeError(`No selection type ${json.type} defined`);
1519
+ return cls.fromJSON(doc, json);
1520
+ }
1521
+ /**
1522
+ To be able to deserialize selections from JSON, custom selection
1523
+ classes must register themselves with an ID string, so that they
1524
+ can be disambiguated. Try to pick something that's unlikely to
1525
+ clash with classes from other modules.
1526
+ */
1527
+ static jsonID(id, selectionClass) {
1528
+ if (id in classesById)
1529
+ throw new RangeError("Duplicate use of selection JSON ID " + id);
1530
+ classesById[id] = selectionClass;
1531
+ selectionClass.prototype.jsonID = id;
1532
+ return selectionClass;
1533
+ }
1534
+ /**
1535
+ Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
1536
+ which is a value that can be mapped without having access to a
1537
+ current document, and later resolved to a real selection for a
1538
+ given document again. (This is used mostly by the history to
1539
+ track and restore old selections.) The default implementation of
1540
+ this method just converts the selection to a text selection and
1541
+ returns the bookmark for that.
1542
+ */
1543
+ getBookmark() {
1544
+ return TextSelection.between(this.$anchor, this.$head).getBookmark();
1545
+ }
1546
+ }
1547
+ Selection.prototype.visible = true;
1548
+ /**
1549
+ Represents a selected range in a document.
1550
+ */
1551
+ class SelectionRange {
1552
+ /**
1553
+ Create a range.
1554
+ */
1555
+ constructor(
1556
+ /**
1557
+ The lower bound of the range.
1558
+ */
1559
+ $from,
1560
+ /**
1561
+ The upper bound of the range.
1562
+ */
1563
+ $to) {
1564
+ this.$from = $from;
1565
+ this.$to = $to;
1566
+ }
1567
+ }
1568
+ let warnedAboutTextSelection = false;
1569
+ function checkTextSelection($pos) {
1570
+ if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
1571
+ warnedAboutTextSelection = true;
1572
+ console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
1573
+ }
1574
+ }
1575
+ /**
1576
+ A text selection represents a classical editor selection, with a
1577
+ head (the moving side) and anchor (immobile side), both of which
1578
+ point into textblock nodes. It can be empty (a regular cursor
1579
+ position).
1580
+ */
1581
+ class TextSelection extends Selection {
1582
+ /**
1583
+ Construct a text selection between the given points.
1584
+ */
1585
+ constructor($anchor, $head = $anchor) {
1586
+ checkTextSelection($anchor);
1587
+ checkTextSelection($head);
1588
+ super($anchor, $head);
1589
+ }
1590
+ /**
1591
+ Returns a resolved position if this is a cursor selection (an
1592
+ empty text selection), and null otherwise.
1593
+ */
1594
+ get $cursor() { return this.$anchor.pos == this.$head.pos ? this.$head : null; }
1595
+ map(doc, mapping) {
1596
+ let $head = doc.resolve(mapping.map(this.head));
1597
+ if (!$head.parent.inlineContent)
1598
+ return Selection.near($head);
1599
+ let $anchor = doc.resolve(mapping.map(this.anchor));
1600
+ return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
1601
+ }
1602
+ replace(tr, content = Slice.empty) {
1603
+ super.replace(tr, content);
1604
+ if (content == Slice.empty) {
1605
+ let marks = this.$from.marksAcross(this.$to);
1606
+ if (marks)
1607
+ tr.ensureMarks(marks);
1608
+ }
1609
+ }
1610
+ eq(other) {
1611
+ return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
1612
+ }
1613
+ getBookmark() {
1614
+ return new TextBookmark(this.anchor, this.head);
1615
+ }
1616
+ toJSON() {
1617
+ return { type: "text", anchor: this.anchor, head: this.head };
1618
+ }
1619
+ /**
1620
+ @internal
1621
+ */
1622
+ static fromJSON(doc, json) {
1623
+ if (typeof json.anchor != "number" || typeof json.head != "number")
1624
+ throw new RangeError("Invalid input for TextSelection.fromJSON");
1625
+ return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
1626
+ }
1627
+ /**
1628
+ Create a text selection from non-resolved positions.
1629
+ */
1630
+ static create(doc, anchor, head = anchor) {
1631
+ let $anchor = doc.resolve(anchor);
1632
+ return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
1633
+ }
1634
+ /**
1635
+ Return a text selection that spans the given positions or, if
1636
+ they aren't text positions, find a text selection near them.
1637
+ `bias` determines whether the method searches forward (default)
1638
+ or backwards (negative number) first. Will fall back to calling
1639
+ [`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
1640
+ doesn't contain a valid text position.
1641
+ */
1642
+ static between($anchor, $head, bias) {
1643
+ let dPos = $anchor.pos - $head.pos;
1644
+ if (!bias || dPos)
1645
+ bias = dPos >= 0 ? 1 : -1;
1646
+ if (!$head.parent.inlineContent) {
1647
+ let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
1648
+ if (found)
1649
+ $head = found.$head;
1650
+ else
1651
+ return Selection.near($head, bias);
1652
+ }
1653
+ if (!$anchor.parent.inlineContent) {
1654
+ if (dPos == 0) {
1655
+ $anchor = $head;
1656
+ }
1657
+ else {
1658
+ $anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
1659
+ if (($anchor.pos < $head.pos) != (dPos < 0))
1660
+ $anchor = $head;
1661
+ }
1662
+ }
1663
+ return new TextSelection($anchor, $head);
1664
+ }
1665
+ }
1666
+ Selection.jsonID("text", TextSelection);
1667
+ class TextBookmark {
1668
+ constructor(anchor, head) {
1669
+ this.anchor = anchor;
1670
+ this.head = head;
1671
+ }
1672
+ map(mapping) {
1673
+ return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
1674
+ }
1675
+ resolve(doc) {
1676
+ return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
1677
+ }
1678
+ }
1679
+ /**
1680
+ A node selection is a selection that points at a single node. All
1681
+ nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
1682
+ target of a node selection. In such a selection, `from` and `to`
1683
+ point directly before and after the selected node, `anchor` equals
1684
+ `from`, and `head` equals `to`..
1685
+ */
1686
+ class NodeSelection extends Selection {
1687
+ /**
1688
+ Create a node selection. Does not verify the validity of its
1689
+ argument.
1690
+ */
1691
+ constructor($pos) {
1692
+ let node = $pos.nodeAfter;
1693
+ let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
1694
+ super($pos, $end);
1695
+ this.node = node;
1696
+ }
1697
+ map(doc, mapping) {
1698
+ let { deleted, pos } = mapping.mapResult(this.anchor);
1699
+ let $pos = doc.resolve(pos);
1700
+ if (deleted)
1701
+ return Selection.near($pos);
1702
+ return new NodeSelection($pos);
1703
+ }
1704
+ content() {
1705
+ return new Slice(Fragment.from(this.node), 0, 0);
1706
+ }
1707
+ eq(other) {
1708
+ return other instanceof NodeSelection && other.anchor == this.anchor;
1709
+ }
1710
+ toJSON() {
1711
+ return { type: "node", anchor: this.anchor };
1712
+ }
1713
+ getBookmark() { return new NodeBookmark(this.anchor); }
1714
+ /**
1715
+ @internal
1716
+ */
1717
+ static fromJSON(doc, json) {
1718
+ if (typeof json.anchor != "number")
1719
+ throw new RangeError("Invalid input for NodeSelection.fromJSON");
1720
+ return new NodeSelection(doc.resolve(json.anchor));
1721
+ }
1722
+ /**
1723
+ Create a node selection from non-resolved positions.
1724
+ */
1725
+ static create(doc, from) {
1726
+ return new NodeSelection(doc.resolve(from));
1727
+ }
1728
+ /**
1729
+ Determines whether the given node may be selected as a node
1730
+ selection.
1731
+ */
1732
+ static isSelectable(node) {
1733
+ return !node.isText && node.type.spec.selectable !== false;
1734
+ }
1735
+ }
1736
+ NodeSelection.prototype.visible = false;
1737
+ Selection.jsonID("node", NodeSelection);
1738
+ class NodeBookmark {
1739
+ constructor(anchor) {
1740
+ this.anchor = anchor;
1741
+ }
1742
+ map(mapping) {
1743
+ let { deleted, pos } = mapping.mapResult(this.anchor);
1744
+ return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
1745
+ }
1746
+ resolve(doc) {
1747
+ let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
1748
+ if (node && NodeSelection.isSelectable(node))
1749
+ return new NodeSelection($pos);
1750
+ return Selection.near($pos);
1751
+ }
1752
+ }
1753
+ /**
1754
+ A selection type that represents selecting the whole document
1755
+ (which can not necessarily be expressed with a text selection, when
1756
+ there are for example leaf block nodes at the start or end of the
1757
+ document).
1758
+ */
1759
+ class AllSelection extends Selection {
1760
+ /**
1761
+ Create an all-selection over the given document.
1762
+ */
1763
+ constructor(doc) {
1764
+ super(doc.resolve(0), doc.resolve(doc.content.size));
1765
+ }
1766
+ replace(tr, content = Slice.empty) {
1767
+ if (content == Slice.empty) {
1768
+ tr.delete(0, tr.doc.content.size);
1769
+ let sel = Selection.atStart(tr.doc);
1770
+ if (!sel.eq(tr.selection))
1771
+ tr.setSelection(sel);
1772
+ }
1773
+ else {
1774
+ super.replace(tr, content);
1775
+ }
1776
+ }
1777
+ toJSON() { return { type: "all" }; }
1778
+ /**
1779
+ @internal
1780
+ */
1781
+ static fromJSON(doc) { return new AllSelection(doc); }
1782
+ map(doc) { return new AllSelection(doc); }
1783
+ eq(other) { return other instanceof AllSelection; }
1784
+ getBookmark() { return AllBookmark; }
1785
+ }
1786
+ Selection.jsonID("all", AllSelection);
1787
+ const AllBookmark = {
1788
+ map() { return this; },
1789
+ resolve(doc) { return new AllSelection(doc); }
1790
+ };
1791
+ // FIXME we'll need some awareness of text direction when scanning for selections
1792
+ // Try to find a selection inside the given node. `pos` points at the
1793
+ // position where the search starts. When `text` is true, only return
1794
+ // text selections.
1795
+ function findSelectionIn(doc, node, pos, index, dir, text = false) {
1796
+ if (node.inlineContent)
1797
+ return TextSelection.create(doc, pos);
1798
+ for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
1799
+ let child = node.child(i);
1800
+ if (!child.isAtom) {
1801
+ let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
1802
+ if (inner)
1803
+ return inner;
1804
+ }
1805
+ else if (!text && NodeSelection.isSelectable(child)) {
1806
+ return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
1807
+ }
1808
+ pos += child.nodeSize * dir;
1809
+ }
1810
+ return null;
1811
+ }
1812
+ function selectionToInsertionEnd(tr, startLen, bias) {
1813
+ let last = tr.steps.length - 1;
1814
+ if (last < startLen)
1815
+ return;
1816
+ let step = tr.steps[last];
1817
+ if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep))
1818
+ return;
1819
+ let map = tr.mapping.maps[last], end;
1820
+ map.forEach((_from, _to, _newFrom, newTo) => { if (end == null)
1821
+ end = newTo; });
1822
+ tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
1823
+ }
1824
+
1825
+ function bind(f, self) {
1826
+ return !self || !f ? f : f.bind(self);
1827
+ }
1828
+ class FieldDesc {
1829
+ constructor(name, desc, self) {
1830
+ this.name = name;
1831
+ this.init = bind(desc.init, self);
1832
+ this.apply = bind(desc.apply, self);
1833
+ }
1834
+ }
1835
+ [
1836
+ new FieldDesc("doc", {
1837
+ init(config) { return config.doc || config.schema.topNodeType.createAndFill(); },
1838
+ apply(tr) { return tr.doc; }
1839
+ }),
1840
+ new FieldDesc("selection", {
1841
+ init(config, instance) { return config.selection || Selection.atStart(instance.doc); },
1842
+ apply(tr) { return tr.selection; }
1843
+ }),
1844
+ new FieldDesc("storedMarks", {
1845
+ init(config) { return config.storedMarks || null; },
1846
+ apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null; }
1847
+ }),
1848
+ new FieldDesc("scrollToSelection", {
1849
+ init() { return 0; },
1850
+ apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev; }
1851
+ })
1852
+ ];
1853
+
1854
+ function bindProps(obj, self, target) {
1855
+ for (let prop in obj) {
1856
+ let val = obj[prop];
1857
+ if (val instanceof Function)
1858
+ val = val.bind(self);
1859
+ else if (prop == "handleDOMEvents")
1860
+ val = bindProps(val, self, {});
1861
+ target[prop] = val;
1862
+ }
1863
+ return target;
1864
+ }
1865
+ /**
1866
+ Plugins bundle functionality that can be added to an editor.
1867
+ They are part of the [editor state](https://prosemirror.net/docs/ref/#state.EditorState) and
1868
+ may influence that state and the view that contains it.
1869
+ */
1870
+ class Plugin {
1871
+ /**
1872
+ Create a plugin.
1873
+ */
1874
+ constructor(
1875
+ /**
1876
+ The plugin's [spec object](https://prosemirror.net/docs/ref/#state.PluginSpec).
1877
+ */
1878
+ spec) {
1879
+ this.spec = spec;
1880
+ /**
1881
+ The [props](https://prosemirror.net/docs/ref/#view.EditorProps) exported by this plugin.
1882
+ */
1883
+ this.props = {};
1884
+ if (spec.props)
1885
+ bindProps(spec.props, this, this.props);
1886
+ this.key = spec.key ? spec.key.key : createKey("plugin");
1887
+ }
1888
+ /**
1889
+ Extract the plugin's state field from an editor state.
1890
+ */
1891
+ getState(state) { return state[this.key]; }
1892
+ }
1893
+ const keys = Object.create(null);
1894
+ function createKey(name) {
1895
+ if (name in keys)
1896
+ return name + "$" + ++keys[name];
1897
+ keys[name] = 0;
1898
+ return name + "$";
1899
+ }
1900
+
1901
+ /**
1902
+ Create a plugin that, when added to a ProseMirror instance,
1903
+ causes a decoration to show up at the drop position when something
1904
+ is dragged over the editor.
1905
+
1906
+ Nodes may add a `disableDropCursor` property to their spec to
1907
+ control the showing of a drop cursor inside them. This may be a
1908
+ boolean or a function, which will be called with a view and a
1909
+ position, and should return a boolean.
1910
+ */
1911
+ function dropCursor(options = {}) {
1912
+ return new Plugin({
1913
+ view(editorView) { return new DropCursorView(editorView, options); }
1914
+ });
1915
+ }
1916
+ class DropCursorView {
1917
+ constructor(editorView, options) {
1918
+ this.editorView = editorView;
1919
+ this.cursorPos = null;
1920
+ this.element = null;
1921
+ this.timeout = -1;
1922
+ this.width = options.width || 1;
1923
+ this.color = options.color || "black";
1924
+ this.class = options.class;
1925
+ this.handlers = ["dragover", "dragend", "drop", "dragleave"].map(name => {
1926
+ let handler = (e) => { this[name](e); };
1927
+ editorView.dom.addEventListener(name, handler);
1928
+ return { name, handler };
1929
+ });
1930
+ }
1931
+ destroy() {
1932
+ this.handlers.forEach(({ name, handler }) => this.editorView.dom.removeEventListener(name, handler));
1933
+ }
1934
+ update(editorView, prevState) {
1935
+ if (this.cursorPos != null && prevState.doc != editorView.state.doc) {
1936
+ if (this.cursorPos > editorView.state.doc.content.size)
1937
+ this.setCursor(null);
1938
+ else
1939
+ this.updateOverlay();
1940
+ }
1941
+ }
1942
+ setCursor(pos) {
1943
+ if (pos == this.cursorPos)
1944
+ return;
1945
+ this.cursorPos = pos;
1946
+ if (pos == null) {
1947
+ this.element.parentNode.removeChild(this.element);
1948
+ this.element = null;
1949
+ }
1950
+ else {
1951
+ this.updateOverlay();
1952
+ }
1953
+ }
1954
+ updateOverlay() {
1955
+ let $pos = this.editorView.state.doc.resolve(this.cursorPos), rect;
1956
+ if (!$pos.parent.inlineContent) {
1957
+ let before = $pos.nodeBefore, after = $pos.nodeAfter;
1958
+ if (before || after) {
1959
+ let nodeRect = this.editorView.nodeDOM(this.cursorPos - (before ? before.nodeSize : 0))
1960
+ .getBoundingClientRect();
1961
+ let top = before ? nodeRect.bottom : nodeRect.top;
1962
+ if (before && after)
1963
+ top = (top + this.editorView.nodeDOM(this.cursorPos).getBoundingClientRect().top) / 2;
1964
+ rect = { left: nodeRect.left, right: nodeRect.right, top: top - this.width / 2, bottom: top + this.width / 2 };
1965
+ }
1966
+ }
1967
+ if (!rect) {
1968
+ let coords = this.editorView.coordsAtPos(this.cursorPos);
1969
+ rect = { left: coords.left - this.width / 2, right: coords.left + this.width / 2, top: coords.top, bottom: coords.bottom };
1970
+ }
1971
+ let parent = this.editorView.dom.offsetParent;
1972
+ if (!this.element) {
1973
+ this.element = parent.appendChild(document.createElement("div"));
1974
+ if (this.class)
1975
+ this.element.className = this.class;
1976
+ this.element.style.cssText = "position: absolute; z-index: 50; pointer-events: none; background-color: " + this.color;
1977
+ }
1978
+ let parentLeft, parentTop;
1979
+ if (!parent || parent == document.body && getComputedStyle(parent).position == "static") {
1980
+ parentLeft = -pageXOffset;
1981
+ parentTop = -pageYOffset;
1982
+ }
1983
+ else {
1984
+ let rect = parent.getBoundingClientRect();
1985
+ parentLeft = rect.left - parent.scrollLeft;
1986
+ parentTop = rect.top - parent.scrollTop;
1987
+ }
1988
+ this.element.style.left = (rect.left - parentLeft) + "px";
1989
+ this.element.style.top = (rect.top - parentTop) + "px";
1990
+ this.element.style.width = (rect.right - rect.left) + "px";
1991
+ this.element.style.height = (rect.bottom - rect.top) + "px";
1992
+ }
1993
+ scheduleRemoval(timeout) {
1994
+ clearTimeout(this.timeout);
1995
+ this.timeout = setTimeout(() => this.setCursor(null), timeout);
1996
+ }
1997
+ dragover(event) {
1998
+ if (!this.editorView.editable)
1999
+ return;
2000
+ let pos = this.editorView.posAtCoords({ left: event.clientX, top: event.clientY });
2001
+ let node = pos && pos.inside >= 0 && this.editorView.state.doc.nodeAt(pos.inside);
2002
+ let disableDropCursor = node && node.type.spec.disableDropCursor;
2003
+ let disabled = typeof disableDropCursor == "function" ? disableDropCursor(this.editorView, pos) : disableDropCursor;
2004
+ if (pos && !disabled) {
2005
+ let target = pos.pos;
2006
+ if (this.editorView.dragging && this.editorView.dragging.slice) {
2007
+ target = dropPoint(this.editorView.state.doc, target, this.editorView.dragging.slice);
2008
+ if (target == null)
2009
+ return this.setCursor(null);
2010
+ }
2011
+ this.setCursor(target);
2012
+ this.scheduleRemoval(5000);
2013
+ }
2014
+ }
2015
+ dragend() {
2016
+ this.scheduleRemoval(20);
2017
+ }
2018
+ drop() {
2019
+ this.scheduleRemoval(20);
2020
+ }
2021
+ dragleave(event) {
2022
+ if (event.target == this.editorView.dom || !this.editorView.dom.contains(event.relatedTarget))
2023
+ this.setCursor(null);
2024
+ }
2025
+ }
2026
+
2027
+ const Dropcursor = core.Extension.create({
2028
+ name: 'dropCursor',
2029
+ addOptions() {
2030
+ return {
2031
+ color: 'currentColor',
2032
+ width: 1,
2033
+ class: undefined,
2034
+ };
2035
+ },
2036
+ addProseMirrorPlugins() {
2037
+ return [
2038
+ dropCursor(this.options),
2039
+ ];
2040
+ },
2041
+ });
24
2042
 
25
- exports.Dropcursor = Dropcursor; exports.default = src_default;
2043
+ exports.Dropcursor = Dropcursor;
2044
+ exports["default"] = Dropcursor;
2045
+ //# sourceMappingURL=index.cjs.map