@jvs-milkdown/components 1.1.4 → 1.1.6

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.
Files changed (48) hide show
  1. package/lib/__internal__/components/image-input.d.ts.map +1 -1
  2. package/lib/code-block/index.js +12 -3382
  3. package/lib/code-block/index.js.map +1 -1
  4. package/lib/image-block/convert-plugin.d.ts +2 -0
  5. package/lib/image-block/convert-plugin.d.ts.map +1 -0
  6. package/lib/image-block/index.d.ts +2 -0
  7. package/lib/image-block/index.d.ts.map +1 -1
  8. package/lib/image-block/index.js +345 -75
  9. package/lib/image-block/index.js.map +1 -1
  10. package/lib/image-block/paste-rule.d.ts +2 -0
  11. package/lib/image-block/paste-rule.d.ts.map +1 -0
  12. package/lib/image-block/schema.d.ts.map +1 -1
  13. package/lib/image-block/view/components/image-block.d.ts +1 -0
  14. package/lib/image-block/view/components/image-block.d.ts.map +1 -1
  15. package/lib/image-block/view/components/image-viewer.d.ts.map +1 -1
  16. package/lib/image-block/view/index.d.ts.map +1 -1
  17. package/lib/image-inline/index.js +27 -6
  18. package/lib/image-inline/index.js.map +1 -1
  19. package/lib/link-tooltip/edit/component.d.ts.map +1 -1
  20. package/lib/link-tooltip/index.js +19 -1861
  21. package/lib/link-tooltip/index.js.map +1 -1
  22. package/lib/list-item-block/index.js +1 -1857
  23. package/lib/list-item-block/index.js.map +1 -1
  24. package/lib/table-block/dnd/drag-over-handler.d.ts.map +1 -1
  25. package/lib/table-block/index.js +140 -2984
  26. package/lib/table-block/index.js.map +1 -1
  27. package/lib/table-block/view/component.d.ts.map +1 -1
  28. package/lib/table-block/view/drag.d.ts +3 -0
  29. package/lib/table-block/view/drag.d.ts.map +1 -1
  30. package/lib/table-block/view/utils.d.ts.map +1 -1
  31. package/lib/tsconfig.tsbuildinfo +1 -1
  32. package/package.json +10 -10
  33. package/src/__internal__/components/image-input.tsx +45 -12
  34. package/src/image-block/__tests__/paste-rule.spec.ts +20 -0
  35. package/src/image-block/convert-plugin.ts +147 -0
  36. package/src/image-block/index.ts +6 -0
  37. package/src/image-block/paste-rule.ts +138 -0
  38. package/src/image-block/schema.ts +15 -0
  39. package/src/image-block/view/components/image-block.tsx +5 -0
  40. package/src/image-block/view/components/image-viewer.tsx +4 -0
  41. package/src/image-block/view/index.ts +8 -0
  42. package/src/link-tooltip/edit/component.tsx +27 -3
  43. package/src/table-block/dnd/create-drag-handler.ts +5 -1
  44. package/src/table-block/dnd/drag-over-handler.ts +29 -1
  45. package/src/table-block/dnd/preview.ts +3 -3
  46. package/src/table-block/view/component.tsx +121 -39
  47. package/src/table-block/view/drag.ts +29 -16
  48. package/src/table-block/view/utils.ts +6 -1
@@ -1,12 +1,14 @@
1
1
  import { $ctx, $view } from '@jvs-milkdown/utils';
2
2
  import { selectColCommand, moveColCommand, selectRowCommand, moveRowCommand, mergeCellsCommand, setAlignCommand, deleteSelectedCellsCommand, addColAfterCommand, addColBeforeCommand, addRowAfterCommand, addRowBeforeCommand, tableSchema } from '@jvs-milkdown/preset-gfm';
3
- import { h, onMounted, onUnmounted, defineComponent, ref, onBeforeUnmount, shallowRef, createApp, triggerRef } from 'vue';
3
+ import { h, defineComponent, ref, onMounted, watch, onBeforeUnmount, shallowRef, createApp, triggerRef } from 'vue';
4
4
  import { computePosition, offset } from '@floating-ui/dom';
5
+ import { findTable, CellSelection, selectedRect, splitCellWithType, mergeCells, splitCell } from '@jvs-milkdown/prose/tables';
5
6
  import clsx from 'clsx';
6
7
  import DOMPurify from 'dompurify';
7
8
  import { editorViewCtx, commandsCtx } from '@jvs-milkdown/core';
8
9
  import { throttle } from 'lodash-es';
9
10
  import { findParent } from '@jvs-milkdown/prose';
11
+ import { TextSelection } from '@jvs-milkdown/prose/state';
10
12
 
11
13
  var __defProp$2 = Object.defineProperty;
12
14
  var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
@@ -86,2938 +88,6 @@ withMeta(tableBlockConfig, {
86
88
  group: "TableBlock"
87
89
  });
88
90
 
89
- function findDiffStart(a, b, pos) {
90
- for (let i = 0;; i++) {
91
- if (i == a.childCount || i == b.childCount)
92
- return a.childCount == b.childCount ? null : pos;
93
- let childA = a.child(i), childB = b.child(i);
94
- if (childA == childB) {
95
- pos += childA.nodeSize;
96
- continue;
97
- }
98
- if (!childA.sameMarkup(childB))
99
- return pos;
100
- if (childA.isText && childA.text != childB.text) {
101
- for (let j = 0; childA.text[j] == childB.text[j]; j++)
102
- pos++;
103
- return pos;
104
- }
105
- if (childA.content.size || childB.content.size) {
106
- let inner = findDiffStart(childA.content, childB.content, pos + 1);
107
- if (inner != null)
108
- return inner;
109
- }
110
- pos += childA.nodeSize;
111
- }
112
- }
113
- function findDiffEnd(a, b, posA, posB) {
114
- for (let iA = a.childCount, iB = b.childCount;;) {
115
- if (iA == 0 || iB == 0)
116
- return iA == iB ? null : { a: posA, b: posB };
117
- let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
118
- if (childA == childB) {
119
- posA -= size;
120
- posB -= size;
121
- continue;
122
- }
123
- if (!childA.sameMarkup(childB))
124
- return { a: posA, b: posB };
125
- if (childA.isText && childA.text != childB.text) {
126
- let same = 0, minSize = Math.min(childA.text.length, childB.text.length);
127
- while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
128
- same++;
129
- posA--;
130
- posB--;
131
- }
132
- return { a: posA, b: posB };
133
- }
134
- if (childA.content.size || childB.content.size) {
135
- let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
136
- if (inner)
137
- return inner;
138
- }
139
- posA -= size;
140
- posB -= size;
141
- }
142
- }
143
-
144
- /**
145
- A fragment represents a node's collection of child nodes.
146
-
147
- Like nodes, fragments are persistent data structures, and you
148
- should not mutate them or their content. Rather, you create new
149
- instances whenever needed. The API tries to make this easy.
150
- */
151
- class Fragment {
152
- /**
153
- @internal
154
- */
155
- constructor(
156
- /**
157
- The child nodes in this fragment.
158
- */
159
- content, size) {
160
- this.content = content;
161
- this.size = size || 0;
162
- if (size == null)
163
- for (let i = 0; i < content.length; i++)
164
- this.size += content[i].nodeSize;
165
- }
166
- /**
167
- Invoke a callback for all descendant nodes between the given two
168
- positions (relative to start of this fragment). Doesn't descend
169
- into a node when the callback returns `false`.
170
- */
171
- nodesBetween(from, to, f, nodeStart = 0, parent) {
172
- for (let i = 0, pos = 0; pos < to; i++) {
173
- let child = this.content[i], end = pos + child.nodeSize;
174
- if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
175
- let start = pos + 1;
176
- child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
177
- }
178
- pos = end;
179
- }
180
- }
181
- /**
182
- Call the given callback for every descendant node. `pos` will be
183
- relative to the start of the fragment. The callback may return
184
- `false` to prevent traversal of a given node's children.
185
- */
186
- descendants(f) {
187
- this.nodesBetween(0, this.size, f);
188
- }
189
- /**
190
- Extract the text between `from` and `to`. See the same method on
191
- [`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
192
- */
193
- textBetween(from, to, blockSeparator, leafText) {
194
- let text = "", first = true;
195
- this.nodesBetween(from, to, (node, pos) => {
196
- let nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos)
197
- : !node.isLeaf ? ""
198
- : leafText ? (typeof leafText === "function" ? leafText(node) : leafText)
199
- : node.type.spec.leafText ? node.type.spec.leafText(node)
200
- : "";
201
- if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) {
202
- if (first)
203
- first = false;
204
- else
205
- text += blockSeparator;
206
- }
207
- text += nodeText;
208
- }, 0);
209
- return text;
210
- }
211
- /**
212
- Create a new fragment containing the combined content of this
213
- fragment and the other.
214
- */
215
- append(other) {
216
- if (!other.size)
217
- return this;
218
- if (!this.size)
219
- return other;
220
- let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
221
- if (last.isText && last.sameMarkup(first)) {
222
- content[content.length - 1] = last.withText(last.text + first.text);
223
- i = 1;
224
- }
225
- for (; i < other.content.length; i++)
226
- content.push(other.content[i]);
227
- return new Fragment(content, this.size + other.size);
228
- }
229
- /**
230
- Cut out the sub-fragment between the two given positions.
231
- */
232
- cut(from, to = this.size) {
233
- if (from == 0 && to == this.size)
234
- return this;
235
- let result = [], size = 0;
236
- if (to > from)
237
- for (let i = 0, pos = 0; pos < to; i++) {
238
- let child = this.content[i], end = pos + child.nodeSize;
239
- if (end > from) {
240
- if (pos < from || end > to) {
241
- if (child.isText)
242
- child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
243
- else
244
- child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
245
- }
246
- result.push(child);
247
- size += child.nodeSize;
248
- }
249
- pos = end;
250
- }
251
- return new Fragment(result, size);
252
- }
253
- /**
254
- @internal
255
- */
256
- cutByIndex(from, to) {
257
- if (from == to)
258
- return Fragment.empty;
259
- if (from == 0 && to == this.content.length)
260
- return this;
261
- return new Fragment(this.content.slice(from, to));
262
- }
263
- /**
264
- Create a new fragment in which the node at the given index is
265
- replaced by the given node.
266
- */
267
- replaceChild(index, node) {
268
- let current = this.content[index];
269
- if (current == node)
270
- return this;
271
- let copy = this.content.slice();
272
- let size = this.size + node.nodeSize - current.nodeSize;
273
- copy[index] = node;
274
- return new Fragment(copy, size);
275
- }
276
- /**
277
- Create a new fragment by prepending the given node to this
278
- fragment.
279
- */
280
- addToStart(node) {
281
- return new Fragment([node].concat(this.content), this.size + node.nodeSize);
282
- }
283
- /**
284
- Create a new fragment by appending the given node to this
285
- fragment.
286
- */
287
- addToEnd(node) {
288
- return new Fragment(this.content.concat(node), this.size + node.nodeSize);
289
- }
290
- /**
291
- Compare this fragment to another one.
292
- */
293
- eq(other) {
294
- if (this.content.length != other.content.length)
295
- return false;
296
- for (let i = 0; i < this.content.length; i++)
297
- if (!this.content[i].eq(other.content[i]))
298
- return false;
299
- return true;
300
- }
301
- /**
302
- The first child of the fragment, or `null` if it is empty.
303
- */
304
- get firstChild() { return this.content.length ? this.content[0] : null; }
305
- /**
306
- The last child of the fragment, or `null` if it is empty.
307
- */
308
- get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; }
309
- /**
310
- The number of child nodes in this fragment.
311
- */
312
- get childCount() { return this.content.length; }
313
- /**
314
- Get the child node at the given index. Raise an error when the
315
- index is out of range.
316
- */
317
- child(index) {
318
- let found = this.content[index];
319
- if (!found)
320
- throw new RangeError("Index " + index + " out of range for " + this);
321
- return found;
322
- }
323
- /**
324
- Get the child node at the given index, if it exists.
325
- */
326
- maybeChild(index) {
327
- return this.content[index] || null;
328
- }
329
- /**
330
- Call `f` for every child node, passing the node, its offset
331
- into this parent node, and its index.
332
- */
333
- forEach(f) {
334
- for (let i = 0, p = 0; i < this.content.length; i++) {
335
- let child = this.content[i];
336
- f(child, p, i);
337
- p += child.nodeSize;
338
- }
339
- }
340
- /**
341
- Find the first position at which this fragment and another
342
- fragment differ, or `null` if they are the same.
343
- */
344
- findDiffStart(other, pos = 0) {
345
- return findDiffStart(this, other, pos);
346
- }
347
- /**
348
- Find the first position, searching from the end, at which this
349
- fragment and the given fragment differ, or `null` if they are
350
- the same. Since this position will not be the same in both
351
- nodes, an object with two separate positions is returned.
352
- */
353
- findDiffEnd(other, pos = this.size, otherPos = other.size) {
354
- return findDiffEnd(this, other, pos, otherPos);
355
- }
356
- /**
357
- Find the index and inner offset corresponding to a given relative
358
- position in this fragment. The result object will be reused
359
- (overwritten) the next time the function is called. @internal
360
- */
361
- findIndex(pos) {
362
- if (pos == 0)
363
- return retIndex(0, pos);
364
- if (pos == this.size)
365
- return retIndex(this.content.length, pos);
366
- if (pos > this.size || pos < 0)
367
- throw new RangeError(`Position ${pos} outside of fragment (${this})`);
368
- for (let i = 0, curPos = 0;; i++) {
369
- let cur = this.child(i), end = curPos + cur.nodeSize;
370
- if (end >= pos) {
371
- if (end == pos)
372
- return retIndex(i + 1, end);
373
- return retIndex(i, curPos);
374
- }
375
- curPos = end;
376
- }
377
- }
378
- /**
379
- Return a debugging string that describes this fragment.
380
- */
381
- toString() { return "<" + this.toStringInner() + ">"; }
382
- /**
383
- @internal
384
- */
385
- toStringInner() { return this.content.join(", "); }
386
- /**
387
- Create a JSON-serializeable representation of this fragment.
388
- */
389
- toJSON() {
390
- return this.content.length ? this.content.map(n => n.toJSON()) : null;
391
- }
392
- /**
393
- Deserialize a fragment from its JSON representation.
394
- */
395
- static fromJSON(schema, value) {
396
- if (!value)
397
- return Fragment.empty;
398
- if (!Array.isArray(value))
399
- throw new RangeError("Invalid input for Fragment.fromJSON");
400
- return new Fragment(value.map(schema.nodeFromJSON));
401
- }
402
- /**
403
- Build a fragment from an array of nodes. Ensures that adjacent
404
- text nodes with the same marks are joined together.
405
- */
406
- static fromArray(array) {
407
- if (!array.length)
408
- return Fragment.empty;
409
- let joined, size = 0;
410
- for (let i = 0; i < array.length; i++) {
411
- let node = array[i];
412
- size += node.nodeSize;
413
- if (i && node.isText && array[i - 1].sameMarkup(node)) {
414
- if (!joined)
415
- joined = array.slice(0, i);
416
- joined[joined.length - 1] = node
417
- .withText(joined[joined.length - 1].text + node.text);
418
- }
419
- else if (joined) {
420
- joined.push(node);
421
- }
422
- }
423
- return new Fragment(joined || array, size);
424
- }
425
- /**
426
- Create a fragment from something that can be interpreted as a
427
- set of nodes. For `null`, it returns the empty fragment. For a
428
- fragment, the fragment itself. For a node or array of nodes, a
429
- fragment containing those nodes.
430
- */
431
- static from(nodes) {
432
- if (!nodes)
433
- return Fragment.empty;
434
- if (nodes instanceof Fragment)
435
- return nodes;
436
- if (Array.isArray(nodes))
437
- return this.fromArray(nodes);
438
- if (nodes.attrs)
439
- return new Fragment([nodes], nodes.nodeSize);
440
- throw new RangeError("Can not convert " + nodes + " to a Fragment" +
441
- (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
442
- }
443
- }
444
- /**
445
- An empty fragment. Intended to be reused whenever a node doesn't
446
- contain anything (rather than allocating a new empty fragment for
447
- each leaf node).
448
- */
449
- Fragment.empty = new Fragment([], 0);
450
- const found = { index: 0, offset: 0 };
451
- function retIndex(index, offset) {
452
- found.index = index;
453
- found.offset = offset;
454
- return found;
455
- }
456
-
457
- /**
458
- Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
459
- given an invalid replacement.
460
- */
461
- class ReplaceError extends Error {
462
- }
463
- /*
464
- ReplaceError = function(this: any, message: string) {
465
- let err = Error.call(this, message)
466
- ;(err as any).__proto__ = ReplaceError.prototype
467
- return err
468
- } as any
469
-
470
- ReplaceError.prototype = Object.create(Error.prototype)
471
- ReplaceError.prototype.constructor = ReplaceError
472
- ReplaceError.prototype.name = "ReplaceError"
473
- */
474
- /**
475
- A slice represents a piece cut out of a larger document. It
476
- stores not only a fragment, but also the depth up to which nodes on
477
- both side are ‘open’ (cut through).
478
- */
479
- class Slice {
480
- /**
481
- Create a slice. When specifying a non-zero open depth, you must
482
- make sure that there are nodes of at least that depth at the
483
- appropriate side of the fragment—i.e. if the fragment is an
484
- empty paragraph node, `openStart` and `openEnd` can't be greater
485
- than 1.
486
-
487
- It is not necessary for the content of open nodes to conform to
488
- the schema's content constraints, though it should be a valid
489
- start/end/middle for such a node, depending on which sides are
490
- open.
491
- */
492
- constructor(
493
- /**
494
- The slice's content.
495
- */
496
- content,
497
- /**
498
- The open depth at the start of the fragment.
499
- */
500
- openStart,
501
- /**
502
- The open depth at the end.
503
- */
504
- openEnd) {
505
- this.content = content;
506
- this.openStart = openStart;
507
- this.openEnd = openEnd;
508
- }
509
- /**
510
- The size this slice would add when inserted into a document.
511
- */
512
- get size() {
513
- return this.content.size - this.openStart - this.openEnd;
514
- }
515
- /**
516
- @internal
517
- */
518
- insertAt(pos, fragment) {
519
- let content = insertInto(this.content, pos + this.openStart, fragment);
520
- return content && new Slice(content, this.openStart, this.openEnd);
521
- }
522
- /**
523
- @internal
524
- */
525
- removeBetween(from, to) {
526
- return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
527
- }
528
- /**
529
- Tests whether this slice is equal to another slice.
530
- */
531
- eq(other) {
532
- return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
533
- }
534
- /**
535
- @internal
536
- */
537
- toString() {
538
- return this.content + "(" + this.openStart + "," + this.openEnd + ")";
539
- }
540
- /**
541
- Convert a slice to a JSON-serializable representation.
542
- */
543
- toJSON() {
544
- if (!this.content.size)
545
- return null;
546
- let json = { content: this.content.toJSON() };
547
- if (this.openStart > 0)
548
- json.openStart = this.openStart;
549
- if (this.openEnd > 0)
550
- json.openEnd = this.openEnd;
551
- return json;
552
- }
553
- /**
554
- Deserialize a slice from its JSON representation.
555
- */
556
- static fromJSON(schema, json) {
557
- if (!json)
558
- return Slice.empty;
559
- let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
560
- if (typeof openStart != "number" || typeof openEnd != "number")
561
- throw new RangeError("Invalid input for Slice.fromJSON");
562
- return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
563
- }
564
- /**
565
- Create a slice from a fragment by taking the maximum possible
566
- open value on both side of the fragment.
567
- */
568
- static maxOpen(fragment, openIsolating = true) {
569
- let openStart = 0, openEnd = 0;
570
- for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
571
- openStart++;
572
- for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
573
- openEnd++;
574
- return new Slice(fragment, openStart, openEnd);
575
- }
576
- }
577
- /**
578
- The empty slice.
579
- */
580
- Slice.empty = new Slice(Fragment.empty, 0, 0);
581
- function removeRange(content, from, to) {
582
- let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
583
- let { index: indexTo, offset: offsetTo } = content.findIndex(to);
584
- if (offset == from || child.isText) {
585
- if (offsetTo != to && !content.child(indexTo).isText)
586
- throw new RangeError("Removing non-flat range");
587
- return content.cut(0, from).append(content.cut(to));
588
- }
589
- if (index != indexTo)
590
- throw new RangeError("Removing non-flat range");
591
- return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
592
- }
593
- function insertInto(content, dist, insert, parent) {
594
- let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
595
- if (offset == dist || child.isText) {
596
- if (parent && !parent.canReplace(index, index, insert))
597
- return null;
598
- return content.cut(0, dist).append(insert).append(content.cut(dist));
599
- }
600
- let inner = insertInto(child.content, dist - offset - 1, insert, child);
601
- return inner && content.replaceChild(index, child.copy(inner));
602
- }
603
-
604
- // Recovery values encode a range index and an offset. They are
605
- // represented as numbers, because tons of them will be created when
606
- // mapping, for example, a large number of decorations. The number's
607
- // lower 16 bits provide the index, the remaining bits the offset.
608
- //
609
- // Note: We intentionally don't use bit shift operators to en- and
610
- // decode these, since those clip to 32 bits, which we might in rare
611
- // cases want to overflow. A 64-bit float can represent 48-bit
612
- // integers precisely.
613
- const lower16 = 0xffff;
614
- const factor16 = Math.pow(2, 16);
615
- function makeRecover(index, offset) { return index + offset * factor16; }
616
- function recoverIndex(value) { return value & lower16; }
617
- function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
618
- const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
619
- /**
620
- An object representing a mapped position with extra
621
- information.
622
- */
623
- class MapResult {
624
- /**
625
- @internal
626
- */
627
- constructor(
628
- /**
629
- The mapped version of the position.
630
- */
631
- pos,
632
- /**
633
- @internal
634
- */
635
- delInfo,
636
- /**
637
- @internal
638
- */
639
- recover) {
640
- this.pos = pos;
641
- this.delInfo = delInfo;
642
- this.recover = recover;
643
- }
644
- /**
645
- Tells you whether the position was deleted, that is, whether the
646
- step removed the token on the side queried (via the `assoc`)
647
- argument from the document.
648
- */
649
- get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
650
- /**
651
- Tells you whether the token before the mapped position was deleted.
652
- */
653
- get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
654
- /**
655
- True when the token after the mapped position was deleted.
656
- */
657
- get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
658
- /**
659
- Tells whether any of the steps mapped through deletes across the
660
- position (including both the token before and after the
661
- position).
662
- */
663
- get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
664
- }
665
- /**
666
- A map describing the deletions and insertions made by a step, which
667
- can be used to find the correspondence between positions in the
668
- pre-step version of a document and the same position in the
669
- post-step version.
670
- */
671
- class StepMap {
672
- /**
673
- Create a position map. The modifications to the document are
674
- represented as an array of numbers, in which each group of three
675
- represents a modified chunk as `[start, oldSize, newSize]`.
676
- */
677
- constructor(
678
- /**
679
- @internal
680
- */
681
- ranges,
682
- /**
683
- @internal
684
- */
685
- inverted = false) {
686
- this.ranges = ranges;
687
- this.inverted = inverted;
688
- if (!ranges.length && StepMap.empty)
689
- return StepMap.empty;
690
- }
691
- /**
692
- @internal
693
- */
694
- recover(value) {
695
- let diff = 0, index = recoverIndex(value);
696
- if (!this.inverted)
697
- for (let i = 0; i < index; i++)
698
- diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
699
- return this.ranges[index * 3] + diff + recoverOffset(value);
700
- }
701
- mapResult(pos, assoc = 1) { return this._map(pos, assoc, false); }
702
- map(pos, assoc = 1) { return this._map(pos, assoc, true); }
703
- /**
704
- @internal
705
- */
706
- _map(pos, assoc, simple) {
707
- let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
708
- for (let i = 0; i < this.ranges.length; i += 3) {
709
- let start = this.ranges[i] - (this.inverted ? diff : 0);
710
- if (start > pos)
711
- break;
712
- let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
713
- if (pos <= end) {
714
- let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
715
- let result = start + diff + (side < 0 ? 0 : newSize);
716
- if (simple)
717
- return result;
718
- let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
719
- let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
720
- if (assoc < 0 ? pos != start : pos != end)
721
- del |= DEL_SIDE;
722
- return new MapResult(result, del, recover);
723
- }
724
- diff += newSize - oldSize;
725
- }
726
- return simple ? pos + diff : new MapResult(pos + diff, 0, null);
727
- }
728
- /**
729
- @internal
730
- */
731
- touches(pos, recover) {
732
- let diff = 0, index = recoverIndex(recover);
733
- let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
734
- for (let i = 0; i < this.ranges.length; i += 3) {
735
- let start = this.ranges[i] - (this.inverted ? diff : 0);
736
- if (start > pos)
737
- break;
738
- let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
739
- if (pos <= end && i == index * 3)
740
- return true;
741
- diff += this.ranges[i + newIndex] - oldSize;
742
- }
743
- return false;
744
- }
745
- /**
746
- Calls the given function on each of the changed ranges included in
747
- this map.
748
- */
749
- forEach(f) {
750
- let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
751
- for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
752
- let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
753
- let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
754
- f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
755
- diff += newSize - oldSize;
756
- }
757
- }
758
- /**
759
- Create an inverted version of this map. The result can be used to
760
- map positions in the post-step document to the pre-step document.
761
- */
762
- invert() {
763
- return new StepMap(this.ranges, !this.inverted);
764
- }
765
- /**
766
- @internal
767
- */
768
- toString() {
769
- return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
770
- }
771
- /**
772
- Create a map that moves all positions by offset `n` (which may be
773
- negative). This can be useful when applying steps meant for a
774
- sub-document to a larger document, or vice-versa.
775
- */
776
- static offset(n) {
777
- return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
778
- }
779
- }
780
- /**
781
- A StepMap that contains no changed ranges.
782
- */
783
- StepMap.empty = new StepMap([]);
784
-
785
- const stepsByID = Object.create(null);
786
- /**
787
- A step object represents an atomic change. It generally applies
788
- only to the document it was created for, since the positions
789
- stored in it will only make sense for that document.
790
-
791
- New steps are defined by creating classes that extend `Step`,
792
- overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
793
- methods, and registering your class with a unique
794
- JSON-serialization identifier using
795
- [`Step.jsonID`](https://prosemirror.net/docs/ref/#transform.Step^jsonID).
796
- */
797
- class Step {
798
- /**
799
- Get the step map that represents the changes made by this step,
800
- and which can be used to transform between positions in the old
801
- and the new document.
802
- */
803
- getMap() { return StepMap.empty; }
804
- /**
805
- Try to merge this step with another one, to be applied directly
806
- after it. Returns the merged step when possible, null if the
807
- steps can't be merged.
808
- */
809
- merge(other) { return null; }
810
- /**
811
- Deserialize a step from its JSON representation. Will call
812
- through to the step class' own implementation of this method.
813
- */
814
- static fromJSON(schema, json) {
815
- if (!json || !json.stepType)
816
- throw new RangeError("Invalid input for Step.fromJSON");
817
- let type = stepsByID[json.stepType];
818
- if (!type)
819
- throw new RangeError(`No step type ${json.stepType} defined`);
820
- return type.fromJSON(schema, json);
821
- }
822
- /**
823
- To be able to serialize steps to JSON, each step needs a string
824
- ID to attach to its JSON representation. Use this method to
825
- register an ID for your step classes. Try to pick something
826
- that's unlikely to clash with steps from other modules.
827
- */
828
- static jsonID(id, stepClass) {
829
- if (id in stepsByID)
830
- throw new RangeError("Duplicate use of step JSON ID " + id);
831
- stepsByID[id] = stepClass;
832
- stepClass.prototype.jsonID = id;
833
- return stepClass;
834
- }
835
- }
836
- /**
837
- The result of [applying](https://prosemirror.net/docs/ref/#transform.Step.apply) a step. Contains either a
838
- new document or a failure value.
839
- */
840
- class StepResult {
841
- /**
842
- @internal
843
- */
844
- constructor(
845
- /**
846
- The transformed document, if successful.
847
- */
848
- doc,
849
- /**
850
- The failure message, if unsuccessful.
851
- */
852
- failed) {
853
- this.doc = doc;
854
- this.failed = failed;
855
- }
856
- /**
857
- Create a successful step result.
858
- */
859
- static ok(doc) { return new StepResult(doc, null); }
860
- /**
861
- Create a failed step result.
862
- */
863
- static fail(message) { return new StepResult(null, message); }
864
- /**
865
- Call [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) with the given
866
- arguments. Create a successful result if it succeeds, and a
867
- failed one if it throws a `ReplaceError`.
868
- */
869
- static fromReplace(doc, from, to, slice) {
870
- try {
871
- return StepResult.ok(doc.replace(from, to, slice));
872
- }
873
- catch (e) {
874
- if (e instanceof ReplaceError)
875
- return StepResult.fail(e.message);
876
- throw e;
877
- }
878
- }
879
- }
880
-
881
- function mapFragment(fragment, f, parent) {
882
- let mapped = [];
883
- for (let i = 0; i < fragment.childCount; i++) {
884
- let child = fragment.child(i);
885
- if (child.content.size)
886
- child = child.copy(mapFragment(child.content, f, child));
887
- if (child.isInline)
888
- child = f(child, parent, i);
889
- mapped.push(child);
890
- }
891
- return Fragment.fromArray(mapped);
892
- }
893
- /**
894
- Add a mark to all inline content between two positions.
895
- */
896
- class AddMarkStep extends Step {
897
- /**
898
- Create a mark step.
899
- */
900
- constructor(
901
- /**
902
- The start of the marked range.
903
- */
904
- from,
905
- /**
906
- The end of the marked range.
907
- */
908
- to,
909
- /**
910
- The mark to add.
911
- */
912
- mark) {
913
- super();
914
- this.from = from;
915
- this.to = to;
916
- this.mark = mark;
917
- }
918
- apply(doc) {
919
- let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
920
- let parent = $from.node($from.sharedDepth(this.to));
921
- let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
922
- if (!node.isAtom || !parent.type.allowsMarkType(this.mark.type))
923
- return node;
924
- return node.mark(this.mark.addToSet(node.marks));
925
- }, parent), oldSlice.openStart, oldSlice.openEnd);
926
- return StepResult.fromReplace(doc, this.from, this.to, slice);
927
- }
928
- invert() {
929
- return new RemoveMarkStep(this.from, this.to, this.mark);
930
- }
931
- map(mapping) {
932
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
933
- if (from.deleted && to.deleted || from.pos >= to.pos)
934
- return null;
935
- return new AddMarkStep(from.pos, to.pos, this.mark);
936
- }
937
- merge(other) {
938
- if (other instanceof AddMarkStep &&
939
- other.mark.eq(this.mark) &&
940
- this.from <= other.to && this.to >= other.from)
941
- return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
942
- return null;
943
- }
944
- toJSON() {
945
- return { stepType: "addMark", mark: this.mark.toJSON(),
946
- from: this.from, to: this.to };
947
- }
948
- /**
949
- @internal
950
- */
951
- static fromJSON(schema, json) {
952
- if (typeof json.from != "number" || typeof json.to != "number")
953
- throw new RangeError("Invalid input for AddMarkStep.fromJSON");
954
- return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
955
- }
956
- }
957
- Step.jsonID("addMark", AddMarkStep);
958
- /**
959
- Remove a mark from all inline content between two positions.
960
- */
961
- class RemoveMarkStep extends Step {
962
- /**
963
- Create a mark-removing step.
964
- */
965
- constructor(
966
- /**
967
- The start of the unmarked range.
968
- */
969
- from,
970
- /**
971
- The end of the unmarked range.
972
- */
973
- to,
974
- /**
975
- The mark to remove.
976
- */
977
- mark) {
978
- super();
979
- this.from = from;
980
- this.to = to;
981
- this.mark = mark;
982
- }
983
- apply(doc) {
984
- let oldSlice = doc.slice(this.from, this.to);
985
- let slice = new Slice(mapFragment(oldSlice.content, node => {
986
- return node.mark(this.mark.removeFromSet(node.marks));
987
- }, doc), oldSlice.openStart, oldSlice.openEnd);
988
- return StepResult.fromReplace(doc, this.from, this.to, slice);
989
- }
990
- invert() {
991
- return new AddMarkStep(this.from, this.to, this.mark);
992
- }
993
- map(mapping) {
994
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
995
- if (from.deleted && to.deleted || from.pos >= to.pos)
996
- return null;
997
- return new RemoveMarkStep(from.pos, to.pos, this.mark);
998
- }
999
- merge(other) {
1000
- if (other instanceof RemoveMarkStep &&
1001
- other.mark.eq(this.mark) &&
1002
- this.from <= other.to && this.to >= other.from)
1003
- return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
1004
- return null;
1005
- }
1006
- toJSON() {
1007
- return { stepType: "removeMark", mark: this.mark.toJSON(),
1008
- from: this.from, to: this.to };
1009
- }
1010
- /**
1011
- @internal
1012
- */
1013
- static fromJSON(schema, json) {
1014
- if (typeof json.from != "number" || typeof json.to != "number")
1015
- throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
1016
- return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
1017
- }
1018
- }
1019
- Step.jsonID("removeMark", RemoveMarkStep);
1020
- /**
1021
- Add a mark to a specific node.
1022
- */
1023
- class AddNodeMarkStep extends Step {
1024
- /**
1025
- Create a node mark step.
1026
- */
1027
- constructor(
1028
- /**
1029
- The position of the target node.
1030
- */
1031
- pos,
1032
- /**
1033
- The mark to add.
1034
- */
1035
- mark) {
1036
- super();
1037
- this.pos = pos;
1038
- this.mark = mark;
1039
- }
1040
- apply(doc) {
1041
- let node = doc.nodeAt(this.pos);
1042
- if (!node)
1043
- return StepResult.fail("No node at mark step's position");
1044
- let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
1045
- return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1046
- }
1047
- invert(doc) {
1048
- let node = doc.nodeAt(this.pos);
1049
- if (node) {
1050
- let newSet = this.mark.addToSet(node.marks);
1051
- if (newSet.length == node.marks.length) {
1052
- for (let i = 0; i < node.marks.length; i++)
1053
- if (!node.marks[i].isInSet(newSet))
1054
- return new AddNodeMarkStep(this.pos, node.marks[i]);
1055
- return new AddNodeMarkStep(this.pos, this.mark);
1056
- }
1057
- }
1058
- return new RemoveNodeMarkStep(this.pos, this.mark);
1059
- }
1060
- map(mapping) {
1061
- let pos = mapping.mapResult(this.pos, 1);
1062
- return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
1063
- }
1064
- toJSON() {
1065
- return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1066
- }
1067
- /**
1068
- @internal
1069
- */
1070
- static fromJSON(schema, json) {
1071
- if (typeof json.pos != "number")
1072
- throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
1073
- return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1074
- }
1075
- }
1076
- Step.jsonID("addNodeMark", AddNodeMarkStep);
1077
- /**
1078
- Remove a mark from a specific node.
1079
- */
1080
- class RemoveNodeMarkStep extends Step {
1081
- /**
1082
- Create a mark-removing step.
1083
- */
1084
- constructor(
1085
- /**
1086
- The position of the target node.
1087
- */
1088
- pos,
1089
- /**
1090
- The mark to remove.
1091
- */
1092
- mark) {
1093
- super();
1094
- this.pos = pos;
1095
- this.mark = mark;
1096
- }
1097
- apply(doc) {
1098
- let node = doc.nodeAt(this.pos);
1099
- if (!node)
1100
- return StepResult.fail("No node at mark step's position");
1101
- let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
1102
- return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1103
- }
1104
- invert(doc) {
1105
- let node = doc.nodeAt(this.pos);
1106
- if (!node || !this.mark.isInSet(node.marks))
1107
- return this;
1108
- return new AddNodeMarkStep(this.pos, this.mark);
1109
- }
1110
- map(mapping) {
1111
- let pos = mapping.mapResult(this.pos, 1);
1112
- return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
1113
- }
1114
- toJSON() {
1115
- return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
1116
- }
1117
- /**
1118
- @internal
1119
- */
1120
- static fromJSON(schema, json) {
1121
- if (typeof json.pos != "number")
1122
- throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
1123
- return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
1124
- }
1125
- }
1126
- Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
1127
-
1128
- /**
1129
- Replace a part of the document with a slice of new content.
1130
- */
1131
- class ReplaceStep extends Step {
1132
- /**
1133
- The given `slice` should fit the 'gap' between `from` and
1134
- `to`—the depths must line up, and the surrounding nodes must be
1135
- able to be joined with the open sides of the slice. When
1136
- `structure` is true, the step will fail if the content between
1137
- from and to is not just a sequence of closing and then opening
1138
- tokens (this is to guard against rebased replace steps
1139
- overwriting something they weren't supposed to).
1140
- */
1141
- constructor(
1142
- /**
1143
- The start position of the replaced range.
1144
- */
1145
- from,
1146
- /**
1147
- The end position of the replaced range.
1148
- */
1149
- to,
1150
- /**
1151
- The slice to insert.
1152
- */
1153
- slice,
1154
- /**
1155
- @internal
1156
- */
1157
- structure = false) {
1158
- super();
1159
- this.from = from;
1160
- this.to = to;
1161
- this.slice = slice;
1162
- this.structure = structure;
1163
- }
1164
- apply(doc) {
1165
- if (this.structure && contentBetween(doc, this.from, this.to))
1166
- return StepResult.fail("Structure replace would overwrite content");
1167
- return StepResult.fromReplace(doc, this.from, this.to, this.slice);
1168
- }
1169
- getMap() {
1170
- return new StepMap([this.from, this.to - this.from, this.slice.size]);
1171
- }
1172
- invert(doc) {
1173
- return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
1174
- }
1175
- map(mapping) {
1176
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1177
- if (from.deletedAcross && to.deletedAcross)
1178
- return null;
1179
- return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
1180
- }
1181
- merge(other) {
1182
- if (!(other instanceof ReplaceStep) || other.structure || this.structure)
1183
- return null;
1184
- if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
1185
- let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1186
- : new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
1187
- return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
1188
- }
1189
- else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
1190
- let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
1191
- : new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
1192
- return new ReplaceStep(other.from, this.to, slice, this.structure);
1193
- }
1194
- else {
1195
- return null;
1196
- }
1197
- }
1198
- toJSON() {
1199
- let json = { stepType: "replace", from: this.from, to: this.to };
1200
- if (this.slice.size)
1201
- json.slice = this.slice.toJSON();
1202
- if (this.structure)
1203
- json.structure = true;
1204
- return json;
1205
- }
1206
- /**
1207
- @internal
1208
- */
1209
- static fromJSON(schema, json) {
1210
- if (typeof json.from != "number" || typeof json.to != "number")
1211
- throw new RangeError("Invalid input for ReplaceStep.fromJSON");
1212
- return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure);
1213
- }
1214
- }
1215
- Step.jsonID("replace", ReplaceStep);
1216
- /**
1217
- Replace a part of the document with a slice of content, but
1218
- preserve a range of the replaced content by moving it into the
1219
- slice.
1220
- */
1221
- class ReplaceAroundStep extends Step {
1222
- /**
1223
- Create a replace-around step with the given range and gap.
1224
- `insert` should be the point in the slice into which the content
1225
- of the gap should be moved. `structure` has the same meaning as
1226
- it has in the [`ReplaceStep`](https://prosemirror.net/docs/ref/#transform.ReplaceStep) class.
1227
- */
1228
- constructor(
1229
- /**
1230
- The start position of the replaced range.
1231
- */
1232
- from,
1233
- /**
1234
- The end position of the replaced range.
1235
- */
1236
- to,
1237
- /**
1238
- The start of preserved range.
1239
- */
1240
- gapFrom,
1241
- /**
1242
- The end of preserved range.
1243
- */
1244
- gapTo,
1245
- /**
1246
- The slice to insert.
1247
- */
1248
- slice,
1249
- /**
1250
- The position in the slice where the preserved range should be
1251
- inserted.
1252
- */
1253
- insert,
1254
- /**
1255
- @internal
1256
- */
1257
- structure = false) {
1258
- super();
1259
- this.from = from;
1260
- this.to = to;
1261
- this.gapFrom = gapFrom;
1262
- this.gapTo = gapTo;
1263
- this.slice = slice;
1264
- this.insert = insert;
1265
- this.structure = structure;
1266
- }
1267
- apply(doc) {
1268
- if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
1269
- contentBetween(doc, this.gapTo, this.to)))
1270
- return StepResult.fail("Structure gap-replace would overwrite content");
1271
- let gap = doc.slice(this.gapFrom, this.gapTo);
1272
- if (gap.openStart || gap.openEnd)
1273
- return StepResult.fail("Gap is not a flat range");
1274
- let inserted = this.slice.insertAt(this.insert, gap.content);
1275
- if (!inserted)
1276
- return StepResult.fail("Content does not fit in gap");
1277
- return StepResult.fromReplace(doc, this.from, this.to, inserted);
1278
- }
1279
- getMap() {
1280
- return new StepMap([this.from, this.gapFrom - this.from, this.insert,
1281
- this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
1282
- }
1283
- invert(doc) {
1284
- let gap = this.gapTo - this.gapFrom;
1285
- 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);
1286
- }
1287
- map(mapping) {
1288
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
1289
- let gapFrom = this.from == this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1);
1290
- let gapTo = this.to == this.gapTo ? to.pos : mapping.map(this.gapTo, 1);
1291
- if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
1292
- return null;
1293
- return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
1294
- }
1295
- toJSON() {
1296
- let json = { stepType: "replaceAround", from: this.from, to: this.to,
1297
- gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert };
1298
- if (this.slice.size)
1299
- json.slice = this.slice.toJSON();
1300
- if (this.structure)
1301
- json.structure = true;
1302
- return json;
1303
- }
1304
- /**
1305
- @internal
1306
- */
1307
- static fromJSON(schema, json) {
1308
- if (typeof json.from != "number" || typeof json.to != "number" ||
1309
- typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
1310
- throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
1311
- return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
1312
- }
1313
- }
1314
- Step.jsonID("replaceAround", ReplaceAroundStep);
1315
- function contentBetween(doc, from, to) {
1316
- let $from = doc.resolve(from), dist = to - from, depth = $from.depth;
1317
- while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
1318
- depth--;
1319
- dist--;
1320
- }
1321
- if (dist > 0) {
1322
- let next = $from.node(depth).maybeChild($from.indexAfter(depth));
1323
- while (dist > 0) {
1324
- if (!next || next.isLeaf)
1325
- return true;
1326
- next = next.firstChild;
1327
- dist--;
1328
- }
1329
- }
1330
- return false;
1331
- }
1332
-
1333
- /**
1334
- Update an attribute in a specific node.
1335
- */
1336
- class AttrStep extends Step {
1337
- /**
1338
- Construct an attribute step.
1339
- */
1340
- constructor(
1341
- /**
1342
- The position of the target node.
1343
- */
1344
- pos,
1345
- /**
1346
- The attribute to set.
1347
- */
1348
- attr,
1349
- // The attribute's new value.
1350
- value) {
1351
- super();
1352
- this.pos = pos;
1353
- this.attr = attr;
1354
- this.value = value;
1355
- }
1356
- apply(doc) {
1357
- let node = doc.nodeAt(this.pos);
1358
- if (!node)
1359
- return StepResult.fail("No node at attribute step's position");
1360
- let attrs = Object.create(null);
1361
- for (let name in node.attrs)
1362
- attrs[name] = node.attrs[name];
1363
- attrs[this.attr] = this.value;
1364
- let updated = node.type.create(attrs, null, node.marks);
1365
- return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1366
- }
1367
- getMap() {
1368
- return StepMap.empty;
1369
- }
1370
- invert(doc) {
1371
- return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1372
- }
1373
- map(mapping) {
1374
- let pos = mapping.mapResult(this.pos, 1);
1375
- return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1376
- }
1377
- toJSON() {
1378
- return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
1379
- }
1380
- static fromJSON(schema, json) {
1381
- if (typeof json.pos != "number" || typeof json.attr != "string")
1382
- throw new RangeError("Invalid input for AttrStep.fromJSON");
1383
- return new AttrStep(json.pos, json.attr, json.value);
1384
- }
1385
- }
1386
- Step.jsonID("attr", AttrStep);
1387
- /**
1388
- Update an attribute in the doc node.
1389
- */
1390
- class DocAttrStep extends Step {
1391
- /**
1392
- Construct an attribute step.
1393
- */
1394
- constructor(
1395
- /**
1396
- The attribute to set.
1397
- */
1398
- attr,
1399
- // The attribute's new value.
1400
- value) {
1401
- super();
1402
- this.attr = attr;
1403
- this.value = value;
1404
- }
1405
- apply(doc) {
1406
- let attrs = Object.create(null);
1407
- for (let name in doc.attrs)
1408
- attrs[name] = doc.attrs[name];
1409
- attrs[this.attr] = this.value;
1410
- let updated = doc.type.create(attrs, doc.content, doc.marks);
1411
- return StepResult.ok(updated);
1412
- }
1413
- getMap() {
1414
- return StepMap.empty;
1415
- }
1416
- invert(doc) {
1417
- return new DocAttrStep(this.attr, doc.attrs[this.attr]);
1418
- }
1419
- map(mapping) {
1420
- return this;
1421
- }
1422
- toJSON() {
1423
- return { stepType: "docAttr", attr: this.attr, value: this.value };
1424
- }
1425
- static fromJSON(schema, json) {
1426
- if (typeof json.attr != "string")
1427
- throw new RangeError("Invalid input for DocAttrStep.fromJSON");
1428
- return new DocAttrStep(json.attr, json.value);
1429
- }
1430
- }
1431
- Step.jsonID("docAttr", DocAttrStep);
1432
-
1433
- /**
1434
- @internal
1435
- */
1436
- let TransformError = class extends Error {
1437
- };
1438
- TransformError = function TransformError(message) {
1439
- let err = Error.call(this, message);
1440
- err.__proto__ = TransformError.prototype;
1441
- return err;
1442
- };
1443
- TransformError.prototype = Object.create(Error.prototype);
1444
- TransformError.prototype.constructor = TransformError;
1445
- TransformError.prototype.name = "TransformError";
1446
-
1447
- const classesById = Object.create(null);
1448
- /**
1449
- Superclass for editor selections. Every selection type should
1450
- extend this. Should not be instantiated directly.
1451
- */
1452
- class Selection {
1453
- /**
1454
- Initialize a selection with the head and anchor and ranges. If no
1455
- ranges are given, constructs a single range across `$anchor` and
1456
- `$head`.
1457
- */
1458
- constructor(
1459
- /**
1460
- The resolved anchor of the selection (the side that stays in
1461
- place when the selection is modified).
1462
- */
1463
- $anchor,
1464
- /**
1465
- The resolved head of the selection (the side that moves when
1466
- the selection is modified).
1467
- */
1468
- $head, ranges) {
1469
- this.$anchor = $anchor;
1470
- this.$head = $head;
1471
- this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
1472
- }
1473
- /**
1474
- The selection's anchor, as an unresolved position.
1475
- */
1476
- get anchor() { return this.$anchor.pos; }
1477
- /**
1478
- The selection's head.
1479
- */
1480
- get head() { return this.$head.pos; }
1481
- /**
1482
- The lower bound of the selection's main range.
1483
- */
1484
- get from() { return this.$from.pos; }
1485
- /**
1486
- The upper bound of the selection's main range.
1487
- */
1488
- get to() { return this.$to.pos; }
1489
- /**
1490
- The resolved lower bound of the selection's main range.
1491
- */
1492
- get $from() {
1493
- return this.ranges[0].$from;
1494
- }
1495
- /**
1496
- The resolved upper bound of the selection's main range.
1497
- */
1498
- get $to() {
1499
- return this.ranges[0].$to;
1500
- }
1501
- /**
1502
- Indicates whether the selection contains any content.
1503
- */
1504
- get empty() {
1505
- let ranges = this.ranges;
1506
- for (let i = 0; i < ranges.length; i++)
1507
- if (ranges[i].$from.pos != ranges[i].$to.pos)
1508
- return false;
1509
- return true;
1510
- }
1511
- /**
1512
- Get the content of this selection as a slice.
1513
- */
1514
- content() {
1515
- return this.$from.doc.slice(this.from, this.to, true);
1516
- }
1517
- /**
1518
- Replace the selection with a slice or, if no slice is given,
1519
- delete the selection. Will append to the given transaction.
1520
- */
1521
- replace(tr, content = Slice.empty) {
1522
- // Put the new selection at the position after the inserted
1523
- // content. When that ended in an inline node, search backwards,
1524
- // to get the position after that node. If not, search forward.
1525
- let lastNode = content.content.lastChild, lastParent = null;
1526
- for (let i = 0; i < content.openEnd; i++) {
1527
- lastParent = lastNode;
1528
- lastNode = lastNode.lastChild;
1529
- }
1530
- let mapFrom = tr.steps.length, ranges = this.ranges;
1531
- for (let i = 0; i < ranges.length; i++) {
1532
- let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
1533
- tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
1534
- if (i == 0)
1535
- selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
1536
- }
1537
- }
1538
- /**
1539
- Replace the selection with the given node, appending the changes
1540
- to the given transaction.
1541
- */
1542
- replaceWith(tr, node) {
1543
- let mapFrom = tr.steps.length, ranges = this.ranges;
1544
- for (let i = 0; i < ranges.length; i++) {
1545
- let { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
1546
- let from = mapping.map($from.pos), to = mapping.map($to.pos);
1547
- if (i) {
1548
- tr.deleteRange(from, to);
1549
- }
1550
- else {
1551
- tr.replaceRangeWith(from, to, node);
1552
- selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
1553
- }
1554
- }
1555
- }
1556
- /**
1557
- Find a valid cursor or leaf node selection starting at the given
1558
- position and searching back if `dir` is negative, and forward if
1559
- positive. When `textOnly` is true, only consider cursor
1560
- selections. Will return null when no valid selection position is
1561
- found.
1562
- */
1563
- static findFrom($pos, dir, textOnly = false) {
1564
- let inner = $pos.parent.inlineContent ? new TextSelection($pos)
1565
- : findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
1566
- if (inner)
1567
- return inner;
1568
- for (let depth = $pos.depth - 1; depth >= 0; depth--) {
1569
- let found = dir < 0
1570
- ? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly)
1571
- : findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
1572
- if (found)
1573
- return found;
1574
- }
1575
- return null;
1576
- }
1577
- /**
1578
- Find a valid cursor or leaf node selection near the given
1579
- position. Searches forward first by default, but if `bias` is
1580
- negative, it will search backwards first.
1581
- */
1582
- static near($pos, bias = 1) {
1583
- return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
1584
- }
1585
- /**
1586
- Find the cursor or leaf node selection closest to the start of
1587
- the given document. Will return an
1588
- [`AllSelection`](https://prosemirror.net/docs/ref/#state.AllSelection) if no valid position
1589
- exists.
1590
- */
1591
- static atStart(doc) {
1592
- return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
1593
- }
1594
- /**
1595
- Find the cursor or leaf node selection closest to the end of the
1596
- given document.
1597
- */
1598
- static atEnd(doc) {
1599
- return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
1600
- }
1601
- /**
1602
- Deserialize the JSON representation of a selection. Must be
1603
- implemented for custom classes (as a static class method).
1604
- */
1605
- static fromJSON(doc, json) {
1606
- if (!json || !json.type)
1607
- throw new RangeError("Invalid input for Selection.fromJSON");
1608
- let cls = classesById[json.type];
1609
- if (!cls)
1610
- throw new RangeError(`No selection type ${json.type} defined`);
1611
- return cls.fromJSON(doc, json);
1612
- }
1613
- /**
1614
- To be able to deserialize selections from JSON, custom selection
1615
- classes must register themselves with an ID string, so that they
1616
- can be disambiguated. Try to pick something that's unlikely to
1617
- clash with classes from other modules.
1618
- */
1619
- static jsonID(id, selectionClass) {
1620
- if (id in classesById)
1621
- throw new RangeError("Duplicate use of selection JSON ID " + id);
1622
- classesById[id] = selectionClass;
1623
- selectionClass.prototype.jsonID = id;
1624
- return selectionClass;
1625
- }
1626
- /**
1627
- Get a [bookmark](https://prosemirror.net/docs/ref/#state.SelectionBookmark) for this selection,
1628
- which is a value that can be mapped without having access to a
1629
- current document, and later resolved to a real selection for a
1630
- given document again. (This is used mostly by the history to
1631
- track and restore old selections.) The default implementation of
1632
- this method just converts the selection to a text selection and
1633
- returns the bookmark for that.
1634
- */
1635
- getBookmark() {
1636
- return TextSelection.between(this.$anchor, this.$head).getBookmark();
1637
- }
1638
- }
1639
- Selection.prototype.visible = true;
1640
- /**
1641
- Represents a selected range in a document.
1642
- */
1643
- class SelectionRange {
1644
- /**
1645
- Create a range.
1646
- */
1647
- constructor(
1648
- /**
1649
- The lower bound of the range.
1650
- */
1651
- $from,
1652
- /**
1653
- The upper bound of the range.
1654
- */
1655
- $to) {
1656
- this.$from = $from;
1657
- this.$to = $to;
1658
- }
1659
- }
1660
- let warnedAboutTextSelection = false;
1661
- function checkTextSelection($pos) {
1662
- if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
1663
- warnedAboutTextSelection = true;
1664
- console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
1665
- }
1666
- }
1667
- /**
1668
- A text selection represents a classical editor selection, with a
1669
- head (the moving side) and anchor (immobile side), both of which
1670
- point into textblock nodes. It can be empty (a regular cursor
1671
- position).
1672
- */
1673
- class TextSelection extends Selection {
1674
- /**
1675
- Construct a text selection between the given points.
1676
- */
1677
- constructor($anchor, $head = $anchor) {
1678
- checkTextSelection($anchor);
1679
- checkTextSelection($head);
1680
- super($anchor, $head);
1681
- }
1682
- /**
1683
- Returns a resolved position if this is a cursor selection (an
1684
- empty text selection), and null otherwise.
1685
- */
1686
- get $cursor() { return this.$anchor.pos == this.$head.pos ? this.$head : null; }
1687
- map(doc, mapping) {
1688
- let $head = doc.resolve(mapping.map(this.head));
1689
- if (!$head.parent.inlineContent)
1690
- return Selection.near($head);
1691
- let $anchor = doc.resolve(mapping.map(this.anchor));
1692
- return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
1693
- }
1694
- replace(tr, content = Slice.empty) {
1695
- super.replace(tr, content);
1696
- if (content == Slice.empty) {
1697
- let marks = this.$from.marksAcross(this.$to);
1698
- if (marks)
1699
- tr.ensureMarks(marks);
1700
- }
1701
- }
1702
- eq(other) {
1703
- return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
1704
- }
1705
- getBookmark() {
1706
- return new TextBookmark(this.anchor, this.head);
1707
- }
1708
- toJSON() {
1709
- return { type: "text", anchor: this.anchor, head: this.head };
1710
- }
1711
- /**
1712
- @internal
1713
- */
1714
- static fromJSON(doc, json) {
1715
- if (typeof json.anchor != "number" || typeof json.head != "number")
1716
- throw new RangeError("Invalid input for TextSelection.fromJSON");
1717
- return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
1718
- }
1719
- /**
1720
- Create a text selection from non-resolved positions.
1721
- */
1722
- static create(doc, anchor, head = anchor) {
1723
- let $anchor = doc.resolve(anchor);
1724
- return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
1725
- }
1726
- /**
1727
- Return a text selection that spans the given positions or, if
1728
- they aren't text positions, find a text selection near them.
1729
- `bias` determines whether the method searches forward (default)
1730
- or backwards (negative number) first. Will fall back to calling
1731
- [`Selection.near`](https://prosemirror.net/docs/ref/#state.Selection^near) when the document
1732
- doesn't contain a valid text position.
1733
- */
1734
- static between($anchor, $head, bias) {
1735
- let dPos = $anchor.pos - $head.pos;
1736
- if (!bias || dPos)
1737
- bias = dPos >= 0 ? 1 : -1;
1738
- if (!$head.parent.inlineContent) {
1739
- let found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
1740
- if (found)
1741
- $head = found.$head;
1742
- else
1743
- return Selection.near($head, bias);
1744
- }
1745
- if (!$anchor.parent.inlineContent) {
1746
- if (dPos == 0) {
1747
- $anchor = $head;
1748
- }
1749
- else {
1750
- $anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
1751
- if (($anchor.pos < $head.pos) != (dPos < 0))
1752
- $anchor = $head;
1753
- }
1754
- }
1755
- return new TextSelection($anchor, $head);
1756
- }
1757
- }
1758
- Selection.jsonID("text", TextSelection);
1759
- class TextBookmark {
1760
- constructor(anchor, head) {
1761
- this.anchor = anchor;
1762
- this.head = head;
1763
- }
1764
- map(mapping) {
1765
- return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
1766
- }
1767
- resolve(doc) {
1768
- return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
1769
- }
1770
- }
1771
- /**
1772
- A node selection is a selection that points at a single node. All
1773
- nodes marked [selectable](https://prosemirror.net/docs/ref/#model.NodeSpec.selectable) can be the
1774
- target of a node selection. In such a selection, `from` and `to`
1775
- point directly before and after the selected node, `anchor` equals
1776
- `from`, and `head` equals `to`..
1777
- */
1778
- class NodeSelection extends Selection {
1779
- /**
1780
- Create a node selection. Does not verify the validity of its
1781
- argument.
1782
- */
1783
- constructor($pos) {
1784
- let node = $pos.nodeAfter;
1785
- let $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
1786
- super($pos, $end);
1787
- this.node = node;
1788
- }
1789
- map(doc, mapping) {
1790
- let { deleted, pos } = mapping.mapResult(this.anchor);
1791
- let $pos = doc.resolve(pos);
1792
- if (deleted)
1793
- return Selection.near($pos);
1794
- return new NodeSelection($pos);
1795
- }
1796
- content() {
1797
- return new Slice(Fragment.from(this.node), 0, 0);
1798
- }
1799
- eq(other) {
1800
- return other instanceof NodeSelection && other.anchor == this.anchor;
1801
- }
1802
- toJSON() {
1803
- return { type: "node", anchor: this.anchor };
1804
- }
1805
- getBookmark() { return new NodeBookmark(this.anchor); }
1806
- /**
1807
- @internal
1808
- */
1809
- static fromJSON(doc, json) {
1810
- if (typeof json.anchor != "number")
1811
- throw new RangeError("Invalid input for NodeSelection.fromJSON");
1812
- return new NodeSelection(doc.resolve(json.anchor));
1813
- }
1814
- /**
1815
- Create a node selection from non-resolved positions.
1816
- */
1817
- static create(doc, from) {
1818
- return new NodeSelection(doc.resolve(from));
1819
- }
1820
- /**
1821
- Determines whether the given node may be selected as a node
1822
- selection.
1823
- */
1824
- static isSelectable(node) {
1825
- return !node.isText && node.type.spec.selectable !== false;
1826
- }
1827
- }
1828
- NodeSelection.prototype.visible = false;
1829
- Selection.jsonID("node", NodeSelection);
1830
- class NodeBookmark {
1831
- constructor(anchor) {
1832
- this.anchor = anchor;
1833
- }
1834
- map(mapping) {
1835
- let { deleted, pos } = mapping.mapResult(this.anchor);
1836
- return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
1837
- }
1838
- resolve(doc) {
1839
- let $pos = doc.resolve(this.anchor), node = $pos.nodeAfter;
1840
- if (node && NodeSelection.isSelectable(node))
1841
- return new NodeSelection($pos);
1842
- return Selection.near($pos);
1843
- }
1844
- }
1845
- /**
1846
- A selection type that represents selecting the whole document
1847
- (which can not necessarily be expressed with a text selection, when
1848
- there are for example leaf block nodes at the start or end of the
1849
- document).
1850
- */
1851
- class AllSelection extends Selection {
1852
- /**
1853
- Create an all-selection over the given document.
1854
- */
1855
- constructor(doc) {
1856
- super(doc.resolve(0), doc.resolve(doc.content.size));
1857
- }
1858
- replace(tr, content = Slice.empty) {
1859
- if (content == Slice.empty) {
1860
- tr.delete(0, tr.doc.content.size);
1861
- let sel = Selection.atStart(tr.doc);
1862
- if (!sel.eq(tr.selection))
1863
- tr.setSelection(sel);
1864
- }
1865
- else {
1866
- super.replace(tr, content);
1867
- }
1868
- }
1869
- toJSON() { return { type: "all" }; }
1870
- /**
1871
- @internal
1872
- */
1873
- static fromJSON(doc) { return new AllSelection(doc); }
1874
- map(doc) { return new AllSelection(doc); }
1875
- eq(other) { return other instanceof AllSelection; }
1876
- getBookmark() { return AllBookmark; }
1877
- }
1878
- Selection.jsonID("all", AllSelection);
1879
- const AllBookmark = {
1880
- map() { return this; },
1881
- resolve(doc) { return new AllSelection(doc); }
1882
- };
1883
- // FIXME we'll need some awareness of text direction when scanning for selections
1884
- // Try to find a selection inside the given node. `pos` points at the
1885
- // position where the search starts. When `text` is true, only return
1886
- // text selections.
1887
- function findSelectionIn(doc, node, pos, index, dir, text = false) {
1888
- if (node.inlineContent)
1889
- return TextSelection.create(doc, pos);
1890
- for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
1891
- let child = node.child(i);
1892
- if (!child.isAtom) {
1893
- let inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
1894
- if (inner)
1895
- return inner;
1896
- }
1897
- else if (!text && NodeSelection.isSelectable(child)) {
1898
- return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
1899
- }
1900
- pos += child.nodeSize * dir;
1901
- }
1902
- return null;
1903
- }
1904
- function selectionToInsertionEnd(tr, startLen, bias) {
1905
- let last = tr.steps.length - 1;
1906
- if (last < startLen)
1907
- return;
1908
- let step = tr.steps[last];
1909
- if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep))
1910
- return;
1911
- let map = tr.mapping.maps[last], end;
1912
- map.forEach((_from, _to, _newFrom, newTo) => { if (end == null)
1913
- end = newTo; });
1914
- tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
1915
- }
1916
-
1917
- function bind(f, self) {
1918
- return !self || !f ? f : f.bind(self);
1919
- }
1920
- class FieldDesc {
1921
- constructor(name, desc, self) {
1922
- this.name = name;
1923
- this.init = bind(desc.init, self);
1924
- this.apply = bind(desc.apply, self);
1925
- }
1926
- }
1927
- [
1928
- new FieldDesc("doc", {
1929
- init(config) { return config.doc || config.schema.topNodeType.createAndFill(); },
1930
- apply(tr) { return tr.doc; }
1931
- }),
1932
- new FieldDesc("selection", {
1933
- init(config, instance) { return config.selection || Selection.atStart(instance.doc); },
1934
- apply(tr) { return tr.selection; }
1935
- }),
1936
- new FieldDesc("storedMarks", {
1937
- init(config) { return config.storedMarks || null; },
1938
- apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null; }
1939
- }),
1940
- new FieldDesc("scrollToSelection", {
1941
- init() { return 0; },
1942
- apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev; }
1943
- })
1944
- ];
1945
- const keys = Object.create(null);
1946
- function createKey(name) {
1947
- if (name in keys)
1948
- return name + "$" + ++keys[name];
1949
- keys[name] = 0;
1950
- return name + "$";
1951
- }
1952
- /**
1953
- A key is used to [tag](https://prosemirror.net/docs/ref/#state.PluginSpec.key) plugins in a way
1954
- that makes it possible to find them, given an editor state.
1955
- Assigning a key does mean only one plugin of that type can be
1956
- active in a state.
1957
- */
1958
- class PluginKey {
1959
- /**
1960
- Create a plugin key.
1961
- */
1962
- constructor(name = "key") { this.key = createKey(name); }
1963
- /**
1964
- Get the active plugin with this key, if any, from an editor
1965
- state.
1966
- */
1967
- get(state) { return state.config.pluginsByKey[this.key]; }
1968
- /**
1969
- Get the plugin's state from an editor state.
1970
- */
1971
- getState(state) { return state[this.key]; }
1972
- }
1973
-
1974
- var base = {
1975
- 8: "Backspace",
1976
- 9: "Tab",
1977
- 10: "Enter",
1978
- 12: "NumLock",
1979
- 13: "Enter",
1980
- 16: "Shift",
1981
- 17: "Control",
1982
- 18: "Alt",
1983
- 20: "CapsLock",
1984
- 27: "Escape",
1985
- 32: " ",
1986
- 33: "PageUp",
1987
- 34: "PageDown",
1988
- 35: "End",
1989
- 36: "Home",
1990
- 37: "ArrowLeft",
1991
- 38: "ArrowUp",
1992
- 39: "ArrowRight",
1993
- 40: "ArrowDown",
1994
- 44: "PrintScreen",
1995
- 45: "Insert",
1996
- 46: "Delete",
1997
- 59: ";",
1998
- 61: "=",
1999
- 91: "Meta",
2000
- 92: "Meta",
2001
- 106: "*",
2002
- 107: "+",
2003
- 108: ",",
2004
- 109: "-",
2005
- 110: ".",
2006
- 111: "/",
2007
- 144: "NumLock",
2008
- 145: "ScrollLock",
2009
- 160: "Shift",
2010
- 161: "Shift",
2011
- 162: "Control",
2012
- 163: "Control",
2013
- 164: "Alt",
2014
- 165: "Alt",
2015
- 173: "-",
2016
- 186: ";",
2017
- 187: "=",
2018
- 188: ",",
2019
- 189: "-",
2020
- 190: ".",
2021
- 191: "/",
2022
- 192: "`",
2023
- 219: "[",
2024
- 220: "\\",
2025
- 221: "]",
2026
- 222: "'"
2027
- };
2028
-
2029
- var shift = {
2030
- 48: ")",
2031
- 49: "!",
2032
- 50: "@",
2033
- 51: "#",
2034
- 52: "$",
2035
- 53: "%",
2036
- 54: "^",
2037
- 55: "&",
2038
- 56: "*",
2039
- 57: "(",
2040
- 59: ":",
2041
- 61: "+",
2042
- 173: "_",
2043
- 186: ":",
2044
- 187: "+",
2045
- 188: "<",
2046
- 189: "_",
2047
- 190: ">",
2048
- 191: "?",
2049
- 192: "~",
2050
- 219: "{",
2051
- 220: "|",
2052
- 221: "}",
2053
- 222: "\""
2054
- };
2055
-
2056
- var mac$1 = typeof navigator != "undefined" && /Mac/.test(navigator.platform);
2057
- var ie = typeof navigator != "undefined" && /MSIE \d|Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(navigator.userAgent);
2058
-
2059
- // Fill in the digit keys
2060
- for (var i = 0; i < 10; i++) base[48 + i] = base[96 + i] = String(i);
2061
-
2062
- // The function keys
2063
- for (var i = 1; i <= 24; i++) base[i + 111] = "F" + i;
2064
-
2065
- // And the alphabetic keys
2066
- for (var i = 65; i <= 90; i++) {
2067
- base[i] = String.fromCharCode(i + 32);
2068
- shift[i] = String.fromCharCode(i);
2069
- }
2070
-
2071
- // For each code that doesn't have a shift-equivalent, copy the base name
2072
- for (var code in base) if (!shift.hasOwnProperty(code)) shift[code] = base[code];
2073
-
2074
- function keyName(event) {
2075
- // On macOS, keys held with Shift and Cmd don't reflect the effect of Shift in `.key`.
2076
- // On IE, shift effect is never included in `.key`.
2077
- var ignoreKey = mac$1 && event.metaKey && event.shiftKey && !event.ctrlKey && !event.altKey ||
2078
- ie && event.shiftKey && event.key && event.key.length == 1 ||
2079
- event.key == "Unidentified";
2080
- var name = (!ignoreKey && event.key) ||
2081
- (event.shiftKey ? shift : base)[event.keyCode] ||
2082
- event.key || "Unidentified";
2083
- // Edge sometimes produces wrong names (Issue #3)
2084
- if (name == "Esc") name = "Escape";
2085
- if (name == "Del") name = "Delete";
2086
- // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/
2087
- if (name == "Left") name = "ArrowLeft";
2088
- if (name == "Up") name = "ArrowUp";
2089
- if (name == "Right") name = "ArrowRight";
2090
- if (name == "Down") name = "ArrowDown";
2091
- return name
2092
- }
2093
-
2094
- const mac = typeof navigator != "undefined" && /Mac|iP(hone|[oa]d)/.test(navigator.platform);
2095
- const windows = typeof navigator != "undefined" && /Win/.test(navigator.platform);
2096
- function normalizeKeyName(name) {
2097
- let parts = name.split(/-(?!$)/), result = parts[parts.length - 1];
2098
- if (result == "Space")
2099
- result = " ";
2100
- let alt, ctrl, shift, meta;
2101
- for (let i = 0; i < parts.length - 1; i++) {
2102
- let mod = parts[i];
2103
- if (/^(cmd|meta|m)$/i.test(mod))
2104
- meta = true;
2105
- else if (/^a(lt)?$/i.test(mod))
2106
- alt = true;
2107
- else if (/^(c|ctrl|control)$/i.test(mod))
2108
- ctrl = true;
2109
- else if (/^s(hift)?$/i.test(mod))
2110
- shift = true;
2111
- else if (/^mod$/i.test(mod)) {
2112
- if (mac)
2113
- meta = true;
2114
- else
2115
- ctrl = true;
2116
- }
2117
- else
2118
- throw new Error("Unrecognized modifier name: " + mod);
2119
- }
2120
- if (alt)
2121
- result = "Alt-" + result;
2122
- if (ctrl)
2123
- result = "Ctrl-" + result;
2124
- if (meta)
2125
- result = "Meta-" + result;
2126
- if (shift)
2127
- result = "Shift-" + result;
2128
- return result;
2129
- }
2130
- function normalize(map) {
2131
- let copy = Object.create(null);
2132
- for (let prop in map)
2133
- copy[normalizeKeyName(prop)] = map[prop];
2134
- return copy;
2135
- }
2136
- function modifiers(name, event, shift = true) {
2137
- if (event.altKey)
2138
- name = "Alt-" + name;
2139
- if (event.ctrlKey)
2140
- name = "Ctrl-" + name;
2141
- if (event.metaKey)
2142
- name = "Meta-" + name;
2143
- if (shift && event.shiftKey)
2144
- name = "Shift-" + name;
2145
- return name;
2146
- }
2147
- /**
2148
- Given a set of bindings (using the same format as
2149
- [`keymap`](https://prosemirror.net/docs/ref/#keymap.keymap)), return a [keydown
2150
- handler](https://prosemirror.net/docs/ref/#view.EditorProps.handleKeyDown) that handles them.
2151
- */
2152
- function keydownHandler(bindings) {
2153
- let map = normalize(bindings);
2154
- return function (view, event) {
2155
- let name = keyName(event), baseName, direct = map[modifiers(name, event)];
2156
- if (direct && direct(view.state, view.dispatch, view))
2157
- return true;
2158
- // A character key
2159
- if (name.length == 1 && name != " ") {
2160
- if (event.shiftKey) {
2161
- // In case the name was already modified by shift, try looking
2162
- // it up without its shift modifier
2163
- let noShift = map[modifiers(name, event, false)];
2164
- if (noShift && noShift(view.state, view.dispatch, view))
2165
- return true;
2166
- }
2167
- if ((event.altKey || event.metaKey || event.ctrlKey) &&
2168
- // Ctrl-Alt may be used for AltGr on Windows
2169
- !(windows && event.ctrlKey && event.altKey) &&
2170
- (baseName = base[event.keyCode]) && baseName != name) {
2171
- // Try falling back to the keyCode when there's a modifier
2172
- // active or the character produced isn't ASCII, and our table
2173
- // produces a different name from the the keyCode. See #668,
2174
- // #1060, #1529.
2175
- let fromCode = map[modifiers(baseName, event)];
2176
- if (fromCode && fromCode(view.state, view.dispatch, view))
2177
- return true;
2178
- }
2179
- }
2180
- return false;
2181
- };
2182
- }
2183
-
2184
- //#region src/tablemap.ts
2185
- let readFromCache;
2186
- let addToCache;
2187
- if (typeof WeakMap != "undefined") {
2188
- let cache = /* @__PURE__ */ new WeakMap();
2189
- readFromCache = (key) => cache.get(key);
2190
- addToCache = (key, value) => {
2191
- cache.set(key, value);
2192
- return value;
2193
- };
2194
- } else {
2195
- const cache = [];
2196
- const cacheSize = 10;
2197
- let cachePos = 0;
2198
- readFromCache = (key) => {
2199
- for (let i = 0; i < cache.length; i += 2) if (cache[i] == key) return cache[i + 1];
2200
- };
2201
- addToCache = (key, value) => {
2202
- if (cachePos == cacheSize) cachePos = 0;
2203
- cache[cachePos++] = key;
2204
- return cache[cachePos++] = value;
2205
- };
2206
- }
2207
- /**
2208
- * A table map describes the structure of a given table. To avoid
2209
- * recomputing them all the time, they are cached per table node. To
2210
- * be able to do that, positions saved in the map are relative to the
2211
- * start of the table, rather than the start of the document.
2212
- *
2213
- * @public
2214
- */
2215
- var TableMap = class {
2216
- constructor(width, height, map, problems) {
2217
- this.width = width;
2218
- this.height = height;
2219
- this.map = map;
2220
- this.problems = problems;
2221
- }
2222
- findCell(pos) {
2223
- for (let i = 0; i < this.map.length; i++) {
2224
- const curPos = this.map[i];
2225
- if (curPos != pos) continue;
2226
- const left = i % this.width;
2227
- const top = i / this.width | 0;
2228
- let right = left + 1;
2229
- let bottom = top + 1;
2230
- for (let j = 1; right < this.width && this.map[i + j] == curPos; j++) right++;
2231
- for (let j = 1; bottom < this.height && this.map[i + this.width * j] == curPos; j++) bottom++;
2232
- return {
2233
- left,
2234
- top,
2235
- right,
2236
- bottom
2237
- };
2238
- }
2239
- throw new RangeError(`No cell with offset ${pos} found`);
2240
- }
2241
- colCount(pos) {
2242
- for (let i = 0; i < this.map.length; i++) if (this.map[i] == pos) return i % this.width;
2243
- throw new RangeError(`No cell with offset ${pos} found`);
2244
- }
2245
- nextCell(pos, axis, dir) {
2246
- const { left, right, top, bottom } = this.findCell(pos);
2247
- if (axis == "horiz") {
2248
- if (dir < 0 ? left == 0 : right == this.width) return null;
2249
- return this.map[top * this.width + (dir < 0 ? left - 1 : right)];
2250
- } else {
2251
- if (dir < 0 ? top == 0 : bottom == this.height) return null;
2252
- return this.map[left + this.width * (dir < 0 ? top - 1 : bottom)];
2253
- }
2254
- }
2255
- rectBetween(a, b) {
2256
- const { left: leftA, right: rightA, top: topA, bottom: bottomA } = this.findCell(a);
2257
- const { left: leftB, right: rightB, top: topB, bottom: bottomB } = this.findCell(b);
2258
- return {
2259
- left: Math.min(leftA, leftB),
2260
- top: Math.min(topA, topB),
2261
- right: Math.max(rightA, rightB),
2262
- bottom: Math.max(bottomA, bottomB)
2263
- };
2264
- }
2265
- cellsInRect(rect) {
2266
- const result = [];
2267
- const seen = {};
2268
- for (let row = rect.top; row < rect.bottom; row++) for (let col = rect.left; col < rect.right; col++) {
2269
- const index = row * this.width + col;
2270
- const pos = this.map[index];
2271
- if (seen[pos]) continue;
2272
- seen[pos] = true;
2273
- if (col == rect.left && col && this.map[index - 1] == pos || row == rect.top && row && this.map[index - this.width] == pos) continue;
2274
- result.push(pos);
2275
- }
2276
- return result;
2277
- }
2278
- positionAt(row, col, table) {
2279
- for (let i = 0, rowStart = 0;; i++) {
2280
- const rowEnd = rowStart + table.child(i).nodeSize;
2281
- if (i == row) {
2282
- let index = col + row * this.width;
2283
- const rowEndIndex = (row + 1) * this.width;
2284
- while (index < rowEndIndex && this.map[index] < rowStart) index++;
2285
- return index == rowEndIndex ? rowEnd - 1 : this.map[index];
2286
- }
2287
- rowStart = rowEnd;
2288
- }
2289
- }
2290
- static get(table) {
2291
- return readFromCache(table) || addToCache(table, computeMap(table));
2292
- }
2293
- };
2294
- function computeMap(table) {
2295
- if (table.type.spec.tableRole != "table") throw new RangeError("Not a table node: " + table.type.name);
2296
- const width = findWidth(table), height = table.childCount;
2297
- const map = [];
2298
- let mapPos = 0;
2299
- let problems = null;
2300
- const colWidths = [];
2301
- for (let i = 0, e = width * height; i < e; i++) map[i] = 0;
2302
- for (let row = 0, pos = 0; row < height; row++) {
2303
- const rowNode = table.child(row);
2304
- pos++;
2305
- for (let i = 0;; i++) {
2306
- while (mapPos < map.length && map[mapPos] != 0) mapPos++;
2307
- if (i == rowNode.childCount) break;
2308
- const cellNode = rowNode.child(i);
2309
- const { colspan, rowspan, colwidth } = cellNode.attrs;
2310
- for (let h = 0; h < rowspan; h++) {
2311
- if (h + row >= height) {
2312
- (problems || (problems = [])).push({
2313
- type: "overlong_rowspan",
2314
- pos,
2315
- n: rowspan - h
2316
- });
2317
- break;
2318
- }
2319
- const start = mapPos + h * width;
2320
- for (let w = 0; w < colspan; w++) {
2321
- if (map[start + w] == 0) map[start + w] = pos;
2322
- else (problems || (problems = [])).push({
2323
- type: "collision",
2324
- row,
2325
- pos,
2326
- n: colspan - w
2327
- });
2328
- const colW = colwidth && colwidth[w];
2329
- if (colW) {
2330
- const widthIndex = (start + w) % width * 2, prev = colWidths[widthIndex];
2331
- if (prev == null || prev != colW && colWidths[widthIndex + 1] == 1) {
2332
- colWidths[widthIndex] = colW;
2333
- colWidths[widthIndex + 1] = 1;
2334
- } else if (prev == colW) colWidths[widthIndex + 1]++;
2335
- }
2336
- }
2337
- }
2338
- mapPos += colspan;
2339
- pos += cellNode.nodeSize;
2340
- }
2341
- const expectedPos = (row + 1) * width;
2342
- let missing = 0;
2343
- while (mapPos < expectedPos) if (map[mapPos++] == 0) missing++;
2344
- if (missing) (problems || (problems = [])).push({
2345
- type: "missing",
2346
- row,
2347
- n: missing
2348
- });
2349
- pos++;
2350
- }
2351
- if (width === 0 || height === 0) (problems || (problems = [])).push({ type: "zero_sized" });
2352
- const tableMap = new TableMap(width, height, map, problems);
2353
- let badWidths = false;
2354
- for (let i = 0; !badWidths && i < colWidths.length; i += 2) if (colWidths[i] != null && colWidths[i + 1] < height) badWidths = true;
2355
- if (badWidths) findBadColWidths(tableMap, colWidths, table);
2356
- return tableMap;
2357
- }
2358
- function findWidth(table) {
2359
- let width = -1;
2360
- let hasRowSpan = false;
2361
- for (let row = 0; row < table.childCount; row++) {
2362
- const rowNode = table.child(row);
2363
- let rowWidth = 0;
2364
- if (hasRowSpan) for (let j = 0; j < row; j++) {
2365
- const prevRow = table.child(j);
2366
- for (let i = 0; i < prevRow.childCount; i++) {
2367
- const cell = prevRow.child(i);
2368
- if (j + cell.attrs.rowspan > row) rowWidth += cell.attrs.colspan;
2369
- }
2370
- }
2371
- for (let i = 0; i < rowNode.childCount; i++) {
2372
- const cell = rowNode.child(i);
2373
- rowWidth += cell.attrs.colspan;
2374
- if (cell.attrs.rowspan > 1) hasRowSpan = true;
2375
- }
2376
- if (width == -1) width = rowWidth;
2377
- else if (width != rowWidth) width = Math.max(width, rowWidth);
2378
- }
2379
- return width;
2380
- }
2381
- function findBadColWidths(map, colWidths, table) {
2382
- if (!map.problems) map.problems = [];
2383
- const seen = {};
2384
- for (let i = 0; i < map.map.length; i++) {
2385
- const pos = map.map[i];
2386
- if (seen[pos]) continue;
2387
- seen[pos] = true;
2388
- const node = table.nodeAt(pos);
2389
- if (!node) throw new RangeError(`No cell with offset ${pos} found`);
2390
- let updated = null;
2391
- const attrs = node.attrs;
2392
- for (let j = 0; j < attrs.colspan; j++) {
2393
- const colWidth = colWidths[(i + j) % map.width * 2];
2394
- if (colWidth != null && (!attrs.colwidth || attrs.colwidth[j] != colWidth)) (updated || (updated = freshColWidth(attrs)))[j] = colWidth;
2395
- }
2396
- if (updated) map.problems.unshift({
2397
- type: "colwidth mismatch",
2398
- pos,
2399
- colwidth: updated
2400
- });
2401
- }
2402
- }
2403
- function freshColWidth(attrs) {
2404
- if (attrs.colwidth) return attrs.colwidth.slice();
2405
- const result = [];
2406
- for (let i = 0; i < attrs.colspan; i++) result.push(0);
2407
- return result;
2408
- }
2409
- /**
2410
- * @public
2411
- */
2412
- function tableNodeTypes(schema) {
2413
- let result = schema.cached.tableNodeTypes;
2414
- if (!result) {
2415
- result = schema.cached.tableNodeTypes = {};
2416
- for (const name in schema.nodes) {
2417
- const type = schema.nodes[name], role = type.spec.tableRole;
2418
- if (role) result[role] = type;
2419
- }
2420
- }
2421
- return result;
2422
- }
2423
-
2424
- //#endregion
2425
- //#region src/util.ts
2426
- /**
2427
- * @public
2428
- */
2429
- new PluginKey("selectingCells");
2430
- /**
2431
- * @public
2432
- */
2433
- function cellAround($pos) {
2434
- for (let d = $pos.depth - 1; d > 0; d--) if ($pos.node(d).type.spec.tableRole == "row") return $pos.node(0).resolve($pos.before(d + 1));
2435
- return null;
2436
- }
2437
- function cellWrapping($pos) {
2438
- for (let d = $pos.depth; d > 0; d--) {
2439
- const role = $pos.node(d).type.spec.tableRole;
2440
- if (role === "cell" || role === "header_cell") return $pos.node(d);
2441
- }
2442
- return null;
2443
- }
2444
- /**
2445
- * @public
2446
- */
2447
- function isInTable(state) {
2448
- const $head = state.selection.$head;
2449
- for (let d = $head.depth; d > 0; d--) if ($head.node(d).type.spec.tableRole == "row") return true;
2450
- return false;
2451
- }
2452
- /**
2453
- * @internal
2454
- */
2455
- function selectionCell(state) {
2456
- const sel = state.selection;
2457
- if ("$anchorCell" in sel && sel.$anchorCell) return sel.$anchorCell.pos > sel.$headCell.pos ? sel.$anchorCell : sel.$headCell;
2458
- else if ("node" in sel && sel.node && sel.node.type.spec.tableRole == "cell") return sel.$anchor;
2459
- const $cell = cellAround(sel.$head) || cellNear(sel.$head);
2460
- if ($cell) return $cell;
2461
- throw new RangeError(`No cell found around position ${sel.head}`);
2462
- }
2463
- /**
2464
- * @public
2465
- */
2466
- function cellNear($pos) {
2467
- for (let after = $pos.nodeAfter, pos = $pos.pos; after; after = after.firstChild, pos++) {
2468
- const role = after.type.spec.tableRole;
2469
- if (role == "cell" || role == "header_cell") return $pos.doc.resolve(pos);
2470
- }
2471
- for (let before = $pos.nodeBefore, pos = $pos.pos; before; before = before.lastChild, pos--) {
2472
- const role = before.type.spec.tableRole;
2473
- if (role == "cell" || role == "header_cell") return $pos.doc.resolve(pos - before.nodeSize);
2474
- }
2475
- }
2476
- /**
2477
- * @public
2478
- */
2479
- function pointsAtCell($pos) {
2480
- return $pos.parent.type.spec.tableRole == "row" && !!$pos.nodeAfter;
2481
- }
2482
- /**
2483
- * @internal
2484
- */
2485
- function inSameTable($cellA, $cellB) {
2486
- return $cellA.depth == $cellB.depth && $cellA.pos >= $cellB.start(-1) && $cellA.pos <= $cellB.end(-1);
2487
- }
2488
- /**
2489
- * @public
2490
- */
2491
- function nextCell($pos, axis, dir) {
2492
- const table = $pos.node(-1);
2493
- const map = TableMap.get(table);
2494
- const tableStart = $pos.start(-1);
2495
- const moved = map.nextCell($pos.pos - tableStart, axis, dir);
2496
- return moved == null ? null : $pos.node(0).resolve(tableStart + moved);
2497
- }
2498
- /**
2499
- * @public
2500
- */
2501
- function removeColSpan(attrs, pos, n = 1) {
2502
- const result = {
2503
- ...attrs,
2504
- colspan: attrs.colspan - n
2505
- };
2506
- if (result.colwidth) {
2507
- result.colwidth = result.colwidth.slice();
2508
- result.colwidth.splice(pos, n);
2509
- if (!result.colwidth.some((w) => w > 0)) result.colwidth = null;
2510
- }
2511
- return result;
2512
- }
2513
-
2514
- //#endregion
2515
- //#region src/cellselection.ts
2516
- /**
2517
- * A [`Selection`](http://prosemirror.net/docs/ref/#state.Selection)
2518
- * subclass that represents a cell selection spanning part of a table.
2519
- * With the plugin enabled, these will be created when the user
2520
- * selects across cells, and will be drawn by giving selected cells a
2521
- * `selectedCell` CSS class.
2522
- *
2523
- * @public
2524
- */
2525
- var CellSelection = class CellSelection extends Selection {
2526
- constructor($anchorCell, $headCell = $anchorCell) {
2527
- const table = $anchorCell.node(-1);
2528
- const map = TableMap.get(table);
2529
- const tableStart = $anchorCell.start(-1);
2530
- const rect = map.rectBetween($anchorCell.pos - tableStart, $headCell.pos - tableStart);
2531
- const doc = $anchorCell.node(0);
2532
- const cells = map.cellsInRect(rect).filter((p) => p != $headCell.pos - tableStart);
2533
- cells.unshift($headCell.pos - tableStart);
2534
- const ranges = cells.map((pos) => {
2535
- const cell = table.nodeAt(pos);
2536
- if (!cell) throw new RangeError(`No cell with offset ${pos} found`);
2537
- const from = tableStart + pos + 1;
2538
- return new SelectionRange(doc.resolve(from), doc.resolve(from + cell.content.size));
2539
- });
2540
- super(ranges[0].$from, ranges[0].$to, ranges);
2541
- this.$anchorCell = $anchorCell;
2542
- this.$headCell = $headCell;
2543
- }
2544
- map(doc, mapping) {
2545
- const $anchorCell = doc.resolve(mapping.map(this.$anchorCell.pos));
2546
- const $headCell = doc.resolve(mapping.map(this.$headCell.pos));
2547
- if (pointsAtCell($anchorCell) && pointsAtCell($headCell) && inSameTable($anchorCell, $headCell)) {
2548
- const tableChanged = this.$anchorCell.node(-1) != $anchorCell.node(-1);
2549
- if (tableChanged && this.isRowSelection()) return CellSelection.rowSelection($anchorCell, $headCell);
2550
- else if (tableChanged && this.isColSelection()) return CellSelection.colSelection($anchorCell, $headCell);
2551
- else return new CellSelection($anchorCell, $headCell);
2552
- }
2553
- return TextSelection.between($anchorCell, $headCell);
2554
- }
2555
- content() {
2556
- const table = this.$anchorCell.node(-1);
2557
- const map = TableMap.get(table);
2558
- const tableStart = this.$anchorCell.start(-1);
2559
- const rect = map.rectBetween(this.$anchorCell.pos - tableStart, this.$headCell.pos - tableStart);
2560
- const seen = {};
2561
- const rows = [];
2562
- for (let row = rect.top; row < rect.bottom; row++) {
2563
- const rowContent = [];
2564
- for (let index = row * map.width + rect.left, col = rect.left; col < rect.right; col++, index++) {
2565
- const pos = map.map[index];
2566
- if (seen[pos]) continue;
2567
- seen[pos] = true;
2568
- const cellRect = map.findCell(pos);
2569
- let cell = table.nodeAt(pos);
2570
- if (!cell) throw new RangeError(`No cell with offset ${pos} found`);
2571
- const extraLeft = rect.left - cellRect.left;
2572
- const extraRight = cellRect.right - rect.right;
2573
- if (extraLeft > 0 || extraRight > 0) {
2574
- let attrs = cell.attrs;
2575
- if (extraLeft > 0) attrs = removeColSpan(attrs, 0, extraLeft);
2576
- if (extraRight > 0) attrs = removeColSpan(attrs, attrs.colspan - extraRight, extraRight);
2577
- if (cellRect.left < rect.left) {
2578
- cell = cell.type.createAndFill(attrs);
2579
- if (!cell) throw new RangeError(`Could not create cell with attrs ${JSON.stringify(attrs)}`);
2580
- } else cell = cell.type.create(attrs, cell.content);
2581
- }
2582
- if (cellRect.top < rect.top || cellRect.bottom > rect.bottom) {
2583
- const attrs = {
2584
- ...cell.attrs,
2585
- rowspan: Math.min(cellRect.bottom, rect.bottom) - Math.max(cellRect.top, rect.top)
2586
- };
2587
- if (cellRect.top < rect.top) cell = cell.type.createAndFill(attrs);
2588
- else cell = cell.type.create(attrs, cell.content);
2589
- }
2590
- rowContent.push(cell);
2591
- }
2592
- rows.push(table.child(row).copy(Fragment.from(rowContent)));
2593
- }
2594
- const fragment = this.isColSelection() && this.isRowSelection() ? table : rows;
2595
- return new Slice(Fragment.from(fragment), 1, 1);
2596
- }
2597
- replace(tr, content = Slice.empty) {
2598
- const mapFrom = tr.steps.length, ranges = this.ranges;
2599
- for (let i = 0; i < ranges.length; i++) {
2600
- const { $from, $to } = ranges[i], mapping = tr.mapping.slice(mapFrom);
2601
- tr.replace(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content);
2602
- }
2603
- const sel = Selection.findFrom(tr.doc.resolve(tr.mapping.slice(mapFrom).map(this.to)), -1);
2604
- if (sel) tr.setSelection(sel);
2605
- }
2606
- replaceWith(tr, node) {
2607
- this.replace(tr, new Slice(Fragment.from(node), 0, 0));
2608
- }
2609
- forEachCell(f) {
2610
- const table = this.$anchorCell.node(-1);
2611
- const map = TableMap.get(table);
2612
- const tableStart = this.$anchorCell.start(-1);
2613
- const cells = map.cellsInRect(map.rectBetween(this.$anchorCell.pos - tableStart, this.$headCell.pos - tableStart));
2614
- for (let i = 0; i < cells.length; i++) f(table.nodeAt(cells[i]), tableStart + cells[i]);
2615
- }
2616
- isColSelection() {
2617
- const anchorTop = this.$anchorCell.index(-1);
2618
- const headTop = this.$headCell.index(-1);
2619
- if (Math.min(anchorTop, headTop) > 0) return false;
2620
- const anchorBottom = anchorTop + this.$anchorCell.nodeAfter.attrs.rowspan;
2621
- const headBottom = headTop + this.$headCell.nodeAfter.attrs.rowspan;
2622
- return Math.max(anchorBottom, headBottom) == this.$headCell.node(-1).childCount;
2623
- }
2624
- static colSelection($anchorCell, $headCell = $anchorCell) {
2625
- const table = $anchorCell.node(-1);
2626
- const map = TableMap.get(table);
2627
- const tableStart = $anchorCell.start(-1);
2628
- const anchorRect = map.findCell($anchorCell.pos - tableStart);
2629
- const headRect = map.findCell($headCell.pos - tableStart);
2630
- const doc = $anchorCell.node(0);
2631
- if (anchorRect.top <= headRect.top) {
2632
- if (anchorRect.top > 0) $anchorCell = doc.resolve(tableStart + map.map[anchorRect.left]);
2633
- if (headRect.bottom < map.height) $headCell = doc.resolve(tableStart + map.map[map.width * (map.height - 1) + headRect.right - 1]);
2634
- } else {
2635
- if (headRect.top > 0) $headCell = doc.resolve(tableStart + map.map[headRect.left]);
2636
- if (anchorRect.bottom < map.height) $anchorCell = doc.resolve(tableStart + map.map[map.width * (map.height - 1) + anchorRect.right - 1]);
2637
- }
2638
- return new CellSelection($anchorCell, $headCell);
2639
- }
2640
- isRowSelection() {
2641
- const table = this.$anchorCell.node(-1);
2642
- const map = TableMap.get(table);
2643
- const tableStart = this.$anchorCell.start(-1);
2644
- const anchorLeft = map.colCount(this.$anchorCell.pos - tableStart);
2645
- const headLeft = map.colCount(this.$headCell.pos - tableStart);
2646
- if (Math.min(anchorLeft, headLeft) > 0) return false;
2647
- const anchorRight = anchorLeft + this.$anchorCell.nodeAfter.attrs.colspan;
2648
- const headRight = headLeft + this.$headCell.nodeAfter.attrs.colspan;
2649
- return Math.max(anchorRight, headRight) == map.width;
2650
- }
2651
- eq(other) {
2652
- return other instanceof CellSelection && other.$anchorCell.pos == this.$anchorCell.pos && other.$headCell.pos == this.$headCell.pos;
2653
- }
2654
- static rowSelection($anchorCell, $headCell = $anchorCell) {
2655
- const table = $anchorCell.node(-1);
2656
- const map = TableMap.get(table);
2657
- const tableStart = $anchorCell.start(-1);
2658
- const anchorRect = map.findCell($anchorCell.pos - tableStart);
2659
- const headRect = map.findCell($headCell.pos - tableStart);
2660
- const doc = $anchorCell.node(0);
2661
- if (anchorRect.left <= headRect.left) {
2662
- if (anchorRect.left > 0) $anchorCell = doc.resolve(tableStart + map.map[anchorRect.top * map.width]);
2663
- if (headRect.right < map.width) $headCell = doc.resolve(tableStart + map.map[map.width * (headRect.top + 1) - 1]);
2664
- } else {
2665
- if (headRect.left > 0) $headCell = doc.resolve(tableStart + map.map[headRect.top * map.width]);
2666
- if (anchorRect.right < map.width) $anchorCell = doc.resolve(tableStart + map.map[map.width * (anchorRect.top + 1) - 1]);
2667
- }
2668
- return new CellSelection($anchorCell, $headCell);
2669
- }
2670
- toJSON() {
2671
- return {
2672
- type: "cell",
2673
- anchor: this.$anchorCell.pos,
2674
- head: this.$headCell.pos
2675
- };
2676
- }
2677
- static fromJSON(doc, json) {
2678
- return new CellSelection(doc.resolve(json.anchor), doc.resolve(json.head));
2679
- }
2680
- static create(doc, anchorCell, headCell = anchorCell) {
2681
- return new CellSelection(doc.resolve(anchorCell), doc.resolve(headCell));
2682
- }
2683
- getBookmark() {
2684
- return new CellBookmark(this.$anchorCell.pos, this.$headCell.pos);
2685
- }
2686
- };
2687
- CellSelection.prototype.visible = false;
2688
- Selection.jsonID("cell", CellSelection);
2689
- /**
2690
- * @public
2691
- */
2692
- var CellBookmark = class CellBookmark {
2693
- constructor(anchor, head) {
2694
- this.anchor = anchor;
2695
- this.head = head;
2696
- }
2697
- map(mapping) {
2698
- return new CellBookmark(mapping.map(this.anchor), mapping.map(this.head));
2699
- }
2700
- resolve(doc) {
2701
- const $anchorCell = doc.resolve(this.anchor), $headCell = doc.resolve(this.head);
2702
- if ($anchorCell.parent.type.spec.tableRole == "row" && $headCell.parent.type.spec.tableRole == "row" && $anchorCell.index() < $anchorCell.parent.childCount && $headCell.index() < $headCell.parent.childCount && inSameTable($anchorCell, $headCell)) return new CellSelection($anchorCell, $headCell);
2703
- else return Selection.near($headCell, 1);
2704
- }
2705
- };
2706
-
2707
- //#endregion
2708
- //#region src/fixtables.ts
2709
- /**
2710
- * @public
2711
- */
2712
- new PluginKey("fix-tables");
2713
- /**
2714
- * Find the closest table node for a given position.
2715
- *
2716
- * @public
2717
- */
2718
- function findTable($pos) {
2719
- return findParentNode((node) => node.type.spec.tableRole === "table", $pos);
2720
- }
2721
- /**
2722
- * Find the closest parent node that satisfies the predicate.
2723
- *
2724
- * @internal
2725
- */
2726
- function findParentNode(predicate, $pos) {
2727
- for (let depth = $pos.depth; depth >= 0; depth -= 1) {
2728
- const node = $pos.node(depth);
2729
- if (predicate(node)) return {
2730
- node,
2731
- pos: depth === 0 ? 0 : $pos.before(depth),
2732
- start: $pos.start(depth),
2733
- depth
2734
- };
2735
- }
2736
- return null;
2737
- }
2738
-
2739
- //#endregion
2740
- //#region src/commands.ts
2741
- /**
2742
- * Helper to get the selected rectangle in a table, if any. Adds table
2743
- * map, table node, and table start offset to the object for
2744
- * convenience.
2745
- *
2746
- * @public
2747
- */
2748
- function selectedRect(state) {
2749
- const sel = state.selection;
2750
- const $pos = selectionCell(state);
2751
- const table = $pos.node(-1);
2752
- const tableStart = $pos.start(-1);
2753
- const map = TableMap.get(table);
2754
- return {
2755
- ...sel instanceof CellSelection ? map.rectBetween(sel.$anchorCell.pos - tableStart, sel.$headCell.pos - tableStart) : map.findCell($pos.pos - tableStart),
2756
- tableStart,
2757
- map,
2758
- table
2759
- };
2760
- }
2761
- /**
2762
- * Split a selected cell, whose rowpan or colspan is greater than one,
2763
- * into smaller cells with the cell type (th, td) returned by getType function.
2764
- *
2765
- * @public
2766
- */
2767
- function splitCellWithType(getCellType) {
2768
- return (state, dispatch) => {
2769
- const sel = state.selection;
2770
- let cellNode;
2771
- let cellPos;
2772
- if (!(sel instanceof CellSelection)) {
2773
- var _cellAround;
2774
- cellNode = cellWrapping(sel.$from);
2775
- if (!cellNode) return false;
2776
- cellPos = (_cellAround = cellAround(sel.$from)) === null || _cellAround === void 0 ? void 0 : _cellAround.pos;
2777
- } else {
2778
- if (sel.$anchorCell.pos != sel.$headCell.pos) return false;
2779
- cellNode = sel.$anchorCell.nodeAfter;
2780
- cellPos = sel.$anchorCell.pos;
2781
- }
2782
- if (cellNode == null || cellPos == null) return false;
2783
- if (cellNode.attrs.colspan == 1 && cellNode.attrs.rowspan == 1) return false;
2784
- if (dispatch) {
2785
- let baseAttrs = cellNode.attrs;
2786
- const attrs = [];
2787
- const colwidth = baseAttrs.colwidth;
2788
- if (baseAttrs.rowspan > 1) baseAttrs = {
2789
- ...baseAttrs,
2790
- rowspan: 1
2791
- };
2792
- if (baseAttrs.colspan > 1) baseAttrs = {
2793
- ...baseAttrs,
2794
- colspan: 1
2795
- };
2796
- const rect = selectedRect(state), tr = state.tr;
2797
- for (let i = 0; i < rect.right - rect.left; i++) attrs.push(colwidth ? {
2798
- ...baseAttrs,
2799
- colwidth: colwidth && colwidth[i] ? [colwidth[i]] : null
2800
- } : baseAttrs);
2801
- let lastCell;
2802
- for (let row = rect.top; row < rect.bottom; row++) {
2803
- let pos = rect.map.positionAt(row, rect.left, rect.table);
2804
- if (row == rect.top) pos += cellNode.nodeSize;
2805
- for (let col = rect.left, i = 0; col < rect.right; col++, i++) {
2806
- if (col == rect.left && row == rect.top) continue;
2807
- tr.insert(lastCell = tr.mapping.map(pos + rect.tableStart, 1), getCellType({
2808
- node: cellNode,
2809
- row,
2810
- col
2811
- }).createAndFill(attrs[i]));
2812
- }
2813
- }
2814
- tr.setNodeMarkup(cellPos, getCellType({
2815
- node: cellNode,
2816
- row: rect.top,
2817
- col: rect.left
2818
- }), attrs[0]);
2819
- if (sel instanceof CellSelection) tr.setSelection(new CellSelection(tr.doc.resolve(sel.$anchorCell.pos), lastCell ? tr.doc.resolve(lastCell) : void 0));
2820
- dispatch(tr);
2821
- }
2822
- return true;
2823
- };
2824
- }
2825
- function deprecated_toggleHeader(type) {
2826
- return function(state, dispatch) {
2827
- if (!isInTable(state)) return false;
2828
- if (dispatch) {
2829
- const types = tableNodeTypes(state.schema);
2830
- const rect = selectedRect(state), tr = state.tr;
2831
- const cells = rect.map.cellsInRect(type == "column" ? {
2832
- left: rect.left,
2833
- top: 0,
2834
- right: rect.right,
2835
- bottom: rect.map.height
2836
- } : type == "row" ? {
2837
- left: 0,
2838
- top: rect.top,
2839
- right: rect.map.width,
2840
- bottom: rect.bottom
2841
- } : rect);
2842
- const nodes = cells.map((pos) => rect.table.nodeAt(pos));
2843
- for (let i = 0; i < cells.length; i++) if (nodes[i].type == types.header_cell) tr.setNodeMarkup(rect.tableStart + cells[i], types.cell, nodes[i].attrs);
2844
- if (tr.steps.length === 0) for (let i = 0; i < cells.length; i++) tr.setNodeMarkup(rect.tableStart + cells[i], types.header_cell, nodes[i].attrs);
2845
- dispatch(tr);
2846
- }
2847
- return true;
2848
- };
2849
- }
2850
- function isHeaderEnabledByType(type, rect, types) {
2851
- const cellPositions = rect.map.cellsInRect({
2852
- left: 0,
2853
- top: 0,
2854
- right: type == "row" ? rect.map.width : 1,
2855
- bottom: type == "column" ? rect.map.height : 1
2856
- });
2857
- for (let i = 0; i < cellPositions.length; i++) {
2858
- const cell = rect.table.nodeAt(cellPositions[i]);
2859
- if (cell && cell.type !== types.header_cell) return false;
2860
- }
2861
- return true;
2862
- }
2863
- /**
2864
- * Toggles between row/column header and normal cells (Only applies to first row/column).
2865
- * For deprecated behavior pass `useDeprecatedLogic` in options with true.
2866
- *
2867
- * @public
2868
- */
2869
- function toggleHeader(type, options) {
2870
- options = options || { useDeprecatedLogic: false };
2871
- if (options.useDeprecatedLogic) return deprecated_toggleHeader(type);
2872
- return function(state, dispatch) {
2873
- if (!isInTable(state)) return false;
2874
- if (dispatch) {
2875
- const types = tableNodeTypes(state.schema);
2876
- const rect = selectedRect(state), tr = state.tr;
2877
- const isHeaderRowEnabled = isHeaderEnabledByType("row", rect, types);
2878
- const isHeaderColumnEnabled = isHeaderEnabledByType("column", rect, types);
2879
- const selectionStartsAt = (type === "column" ? isHeaderRowEnabled : type === "row" ? isHeaderColumnEnabled : false) ? 1 : 0;
2880
- const cellsRect = type == "column" ? {
2881
- left: 0,
2882
- top: selectionStartsAt,
2883
- right: 1,
2884
- bottom: rect.map.height
2885
- } : type == "row" ? {
2886
- left: selectionStartsAt,
2887
- top: 0,
2888
- right: rect.map.width,
2889
- bottom: 1
2890
- } : rect;
2891
- const newType = type == "column" ? isHeaderColumnEnabled ? types.cell : types.header_cell : type == "row" ? isHeaderRowEnabled ? types.cell : types.header_cell : types.cell;
2892
- rect.map.cellsInRect(cellsRect).forEach((relativeCellPos) => {
2893
- const cellPos = relativeCellPos + rect.tableStart;
2894
- const cell = tr.doc.nodeAt(cellPos);
2895
- if (cell) tr.setNodeMarkup(cellPos, newType, cell.attrs);
2896
- });
2897
- dispatch(tr);
2898
- }
2899
- return true;
2900
- };
2901
- }
2902
- /**
2903
- * Toggles whether the selected row contains header cells.
2904
- *
2905
- * @public
2906
- */
2907
- toggleHeader("row", { useDeprecatedLogic: true });
2908
- /**
2909
- * Toggles whether the selected column contains header cells.
2910
- *
2911
- * @public
2912
- */
2913
- toggleHeader("column", { useDeprecatedLogic: true });
2914
- /**
2915
- * Toggles whether the selected cells are header cells.
2916
- *
2917
- * @public
2918
- */
2919
- toggleHeader("cell", { useDeprecatedLogic: true });
2920
- /**
2921
- * Deletes the content of the selected cells, if they are not empty.
2922
- *
2923
- * @public
2924
- */
2925
- function deleteCellSelection(state, dispatch) {
2926
- const sel = state.selection;
2927
- if (!(sel instanceof CellSelection)) return false;
2928
- if (dispatch) {
2929
- const tr = state.tr;
2930
- const baseContent = tableNodeTypes(state.schema).cell.createAndFill().content;
2931
- sel.forEachCell((cell, pos) => {
2932
- if (!cell.content.eq(baseContent)) tr.replace(tr.mapping.map(pos + 1), tr.mapping.map(pos + cell.nodeSize - 1), new Slice(baseContent, 0, 0));
2933
- });
2934
- if (tr.docChanged) dispatch(tr);
2935
- }
2936
- return true;
2937
- }
2938
-
2939
- //#endregion
2940
- //#region src/input.ts
2941
- keydownHandler({
2942
- ArrowLeft: arrow("horiz", -1),
2943
- ArrowRight: arrow("horiz", 1),
2944
- ArrowUp: arrow("vert", -1),
2945
- ArrowDown: arrow("vert", 1),
2946
- "Shift-ArrowLeft": shiftArrow("horiz", -1),
2947
- "Shift-ArrowRight": shiftArrow("horiz", 1),
2948
- "Shift-ArrowUp": shiftArrow("vert", -1),
2949
- "Shift-ArrowDown": shiftArrow("vert", 1),
2950
- Backspace: deleteCellSelection,
2951
- "Mod-Backspace": deleteCellSelection,
2952
- Delete: deleteCellSelection,
2953
- "Mod-Delete": deleteCellSelection
2954
- });
2955
- function maybeSetSelection(state, dispatch, selection) {
2956
- if (selection.eq(state.selection)) return false;
2957
- if (dispatch) dispatch(state.tr.setSelection(selection).scrollIntoView());
2958
- return true;
2959
- }
2960
- /**
2961
- * @internal
2962
- */
2963
- function arrow(axis, dir) {
2964
- return (state, dispatch, view) => {
2965
- if (!view) return false;
2966
- const sel = state.selection;
2967
- if (sel instanceof CellSelection) return maybeSetSelection(state, dispatch, Selection.near(sel.$headCell, dir));
2968
- if (axis != "horiz" && !sel.empty) return false;
2969
- const end = atEndOfCell(view, axis, dir);
2970
- if (end == null) return false;
2971
- if (axis == "horiz") return maybeSetSelection(state, dispatch, Selection.near(state.doc.resolve(sel.head + dir), dir));
2972
- else {
2973
- const $cell = state.doc.resolve(end);
2974
- const $next = nextCell($cell, axis, dir);
2975
- let newSel;
2976
- if ($next) newSel = Selection.near($next, 1);
2977
- else if (dir < 0) newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1);
2978
- else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1);
2979
- return maybeSetSelection(state, dispatch, newSel);
2980
- }
2981
- };
2982
- }
2983
- function shiftArrow(axis, dir) {
2984
- return (state, dispatch, view) => {
2985
- if (!view) return false;
2986
- const sel = state.selection;
2987
- let cellSel;
2988
- if (sel instanceof CellSelection) cellSel = sel;
2989
- else {
2990
- const end = atEndOfCell(view, axis, dir);
2991
- if (end == null) return false;
2992
- cellSel = new CellSelection(state.doc.resolve(end));
2993
- }
2994
- const $head = nextCell(cellSel.$headCell, axis, dir);
2995
- if (!$head) return false;
2996
- return maybeSetSelection(state, dispatch, new CellSelection(cellSel.$anchorCell, $head));
2997
- };
2998
- }
2999
- function atEndOfCell(view, axis, dir) {
3000
- if (!(view.state.selection instanceof TextSelection)) return null;
3001
- const { $head } = view.state.selection;
3002
- for (let d = $head.depth - 1; d >= 0; d--) {
3003
- const parent = $head.node(d);
3004
- if ((dir < 0 ? $head.index(d) : $head.indexAfter(d)) != (dir < 0 ? 0 : parent.childCount)) return null;
3005
- if (parent.type.spec.tableRole == "cell" || parent.type.spec.tableRole == "header_cell") {
3006
- const cellPos = $head.before(d);
3007
- const dirStr = axis == "vert" ? dir > 0 ? "down" : "up" : dir > 0 ? "right" : "left";
3008
- return view.endOfTextblock(dirStr) ? cellPos : null;
3009
- }
3010
- }
3011
- return null;
3012
- }
3013
-
3014
- //#endregion
3015
- //#region src/columnresizing.ts
3016
- /**
3017
- * @public
3018
- */
3019
- new PluginKey("tableColumnResizing");
3020
-
3021
91
  function keepAlive(..._args) {
3022
92
  }
3023
93
 
@@ -3085,7 +155,8 @@ function clearPreview(previewRoot) {
3085
155
  while (previewRoot.firstChild) previewRoot.removeChild(previewRoot.firstChild);
3086
156
  }
3087
157
  function renderPreview(axis, preview, previewRoot, tableContent, index) {
3088
- const { width: tableWidth, height: tableHeight } = tableContent.querySelector("tbody").getBoundingClientRect();
158
+ const tableBodyOrContent = tableContent.querySelector("tbody") || tableContent;
159
+ const { width: tableWidth, height: tableHeight } = tableBodyOrContent.getBoundingClientRect();
3089
160
  if (axis === "y") {
3090
161
  const rows = tableContent.querySelectorAll("tr");
3091
162
  const row = rows[index];
@@ -3162,7 +233,10 @@ function handleDrag(refs, event, ctx, fn) {
3162
233
  const view = ctx == null ? void 0 : ctx.get(editorViewCtx);
3163
234
  if (!(view == null ? void 0 : view.editable)) return;
3164
235
  event.stopPropagation();
3165
- if (event.dataTransfer) event.dataTransfer.effectAllowed = "move";
236
+ if (event.dataTransfer) {
237
+ event.dataTransfer.effectAllowed = "move";
238
+ event.dataTransfer.setData("text/plain", "milkdown-table-drag");
239
+ }
3166
240
  const context = prepareDndContext(refs);
3167
241
  if (!context) return;
3168
242
  requestAnimationFrame(() => {
@@ -3198,7 +272,8 @@ function recoveryStateBetweenUpdate(refs, view, node) {
3198
272
  if (!node) return;
3199
273
  if (!view) return;
3200
274
  const { selection } = view.state;
3201
- if (!(selection instanceof CellSelection)) return;
275
+ const isCellSelection = (sel) => sel && typeof sel.isColSelection === "function" && typeof sel.forEachCell === "function";
276
+ if (!isCellSelection(selection)) return;
3202
277
  const { $from } = selection;
3203
278
  const table = findTable($from);
3204
279
  if (!table || table.node !== node) return;
@@ -3247,7 +322,7 @@ function getDragOverRow(table, pointerY) {
3247
322
  }
3248
323
 
3249
324
  function createDragOverHandler(refs) {
3250
- return throttle((e) => {
325
+ const updatePosition = throttle((e) => {
3251
326
  const context = prepareDndContext(refs);
3252
327
  if (!context) return;
3253
328
  const { preview, content, contentRoot, xHandle, yHandle } = context;
@@ -3319,22 +394,48 @@ function createDragOverHandler(refs) {
3319
394
  }
3320
395
  }
3321
396
  }, 20);
397
+ return (e) => {
398
+ const context = prepareDndContext(refs);
399
+ if (!context) return;
400
+ const { preview, contentRoot } = context;
401
+ if (preview.dataset.show === "false") return;
402
+ e.preventDefault();
403
+ e.stopPropagation();
404
+ const info = refs.dragInfo.value;
405
+ if (info) {
406
+ if (info.type === "col") {
407
+ const dragOverColumn = getDragOverColumn(contentRoot, e.clientX);
408
+ if (dragOverColumn) {
409
+ info.endIndex = dragOverColumn[1];
410
+ }
411
+ } else if (info.type === "row") {
412
+ const dragOverRow = getDragOverRow(contentRoot, e.clientY);
413
+ if (dragOverRow) {
414
+ info.endIndex = dragOverRow[1];
415
+ }
416
+ }
417
+ }
418
+ updatePosition(e);
419
+ };
3322
420
  }
3323
421
 
3324
422
  function useDragHandlers(refs, ctx, getPos) {
3325
423
  const { dragPreviewRef, yLineHandleRef, xLineHandleRef, dragInfo } = refs;
3326
424
  const dragRow = createDragRowHandler(refs, ctx);
3327
425
  const dragCol = createDragColHandler(refs, ctx);
3328
- const onDragEnd = () => {
426
+ const onDragEnd = (e) => {
3329
427
  const preview = dragPreviewRef.value;
3330
428
  if (!preview) return;
3331
429
  if (preview.dataset.show === "false") return;
430
+ e.preventDefault();
431
+ e.stopPropagation();
3332
432
  const previewRoot = preview == null ? void 0 : preview.querySelector("tbody");
3333
433
  while (previewRoot == null ? void 0 : previewRoot.firstChild)
3334
434
  previewRoot == null ? void 0 : previewRoot.removeChild(previewRoot.firstChild);
3335
435
  if (preview) preview.dataset.show = "false";
436
+ dragInfo.value = void 0;
3336
437
  };
3337
- const onDrop = () => {
438
+ const onDrop = (e) => {
3338
439
  var _a;
3339
440
  const preview = dragPreviewRef.value;
3340
441
  if (!preview) return;
@@ -3346,9 +447,18 @@ function useDragHandlers(refs, ctx, getPos) {
3346
447
  if (!info) return;
3347
448
  if (!ctx) return;
3348
449
  if (preview.dataset.show === "false") return;
450
+ e.preventDefault();
451
+ e.stopPropagation();
3349
452
  yHandle.dataset.show = "false";
3350
453
  xHandle.dataset.show = "false";
3351
- if (info.startIndex === info.endIndex) return;
454
+ if (info.startIndex === info.endIndex) {
455
+ const previewRoot2 = preview == null ? void 0 : preview.querySelector("tbody");
456
+ while (previewRoot2 == null ? void 0 : previewRoot2.firstChild)
457
+ previewRoot2 == null ? void 0 : previewRoot2.removeChild(previewRoot2.firstChild);
458
+ preview.dataset.show = "false";
459
+ dragInfo.value = void 0;
460
+ return;
461
+ }
3352
462
  const commands = ctx.get(commandsCtx);
3353
463
  const payload = {
3354
464
  from: info.startIndex,
@@ -3372,24 +482,22 @@ function useDragHandlers(refs, ctx, getPos) {
3372
482
  const index = [info.endIndex, 0];
3373
483
  refs.hoverIndex.value = index;
3374
484
  }
485
+ const previewRoot = preview == null ? void 0 : preview.querySelector("tbody");
486
+ while (previewRoot == null ? void 0 : previewRoot.firstChild)
487
+ previewRoot == null ? void 0 : previewRoot.removeChild(previewRoot.firstChild);
488
+ preview.dataset.show = "false";
489
+ dragInfo.value = void 0;
3375
490
  requestAnimationFrame(() => {
3376
491
  ctx.get(editorViewCtx).focus();
3377
492
  });
3378
493
  };
3379
494
  const onDragOver = createDragOverHandler(refs);
3380
- onMounted(() => {
3381
- window.addEventListener("dragover", onDragOver);
3382
- window.addEventListener("dragend", onDragEnd);
3383
- window.addEventListener("drop", onDrop);
3384
- });
3385
- onUnmounted(() => {
3386
- window.removeEventListener("dragover", onDragOver);
3387
- window.removeEventListener("dragend", onDragEnd);
3388
- window.removeEventListener("drop", onDrop);
3389
- });
3390
495
  return {
3391
496
  dragRow,
3392
- dragCol
497
+ dragCol,
498
+ onDragOver,
499
+ onDrop,
500
+ onDragEnd
3393
501
  };
3394
502
  }
3395
503
 
@@ -3650,7 +758,11 @@ const TableBlock = defineComponent({
3650
758
  dragInfo
3651
759
  };
3652
760
  const { pointerLeave, pointerMove } = usePointerHandlers();
3653
- const { dragRow, dragCol } = useDragHandlers(refs, ctx, getPos);
761
+ const { dragRow, dragCol, onDragOver, onDragEnd, onDrop } = useDragHandlers(
762
+ refs,
763
+ ctx,
764
+ getPos
765
+ );
3654
766
  const {
3655
767
  addRowByIndex,
3656
768
  addColByIndex,
@@ -3677,18 +789,10 @@ const TableBlock = defineComponent({
3677
789
  showCellToolbar.value = false;
3678
790
  return;
3679
791
  }
3680
- if (selection instanceof CellSelection) {
3681
- let _cellCount = 0;
3682
- let hasMerged = false;
3683
- selection.forEachCell((cell) => {
3684
- var _a, _b;
3685
- _cellCount++;
3686
- if (((_a = cell.attrs.rowspan) != null ? _a : 1) > 1 || ((_b = cell.attrs.colspan) != null ? _b : 1) > 1) {
3687
- hasMerged = true;
3688
- }
3689
- });
3690
- canMerge.value = _cellCount > 1;
3691
- canSplit.value = hasMerged;
792
+ const isCellSelection = (sel) => sel && typeof sel.isColSelection === "function" && typeof sel.forEachCell === "function";
793
+ if (isCellSelection(selection)) {
794
+ canMerge.value = mergeCells(view.state);
795
+ canSplit.value = splitCell(view.state);
3692
796
  } else {
3693
797
  showCellToolbar.value = false;
3694
798
  return;
@@ -3697,6 +801,10 @@ const TableBlock = defineComponent({
3697
801
  showCellToolbar.value = false;
3698
802
  return;
3699
803
  }
804
+ if (activeColIndex.value !== -1 || activeRowIndex.value !== -1) {
805
+ showCellToolbar.value = false;
806
+ return;
807
+ }
3700
808
  showCellToolbar.value = true;
3701
809
  requestAnimationFrame(() => {
3702
810
  const toolbar = cellToolbarRef.value;
@@ -3797,23 +905,29 @@ const TableBlock = defineComponent({
3797
905
  const activeColIndex = ref(-1);
3798
906
  const activeRowIndex = ref(-1);
3799
907
  const dispatchListener = () => {
3800
- updateCellToolbar();
3801
- const { selection } = view.state;
3802
- if (selection instanceof CellSelection) {
3803
- if (selection.isColSelection()) {
3804
- const { $head } = selection;
3805
- activeColIndex.value = $head.index($head.depth - 1);
3806
- activeRowIndex.value = -1;
3807
- } else if (selection.isRowSelection()) {
3808
- activeColIndex.value = -1;
908
+ setTimeout(() => {
909
+ const { selection } = view.state;
910
+ const isCellSelection = (sel) => sel && typeof sel.isColSelection === "function" && typeof sel.forEachCell === "function";
911
+ if (isCellSelection(selection)) {
912
+ const selAny = selection;
913
+ if (selAny.isColSelection()) {
914
+ const $headCell = selAny.$headCell;
915
+ activeColIndex.value = $headCell ? $headCell.index($headCell.depth) : -1;
916
+ activeRowIndex.value = -1;
917
+ } else if (selAny.isRowSelection()) {
918
+ const $headCell = selAny.$headCell;
919
+ activeColIndex.value = -1;
920
+ activeRowIndex.value = $headCell ? $headCell.index($headCell.depth - 1) : -1;
921
+ } else {
922
+ activeColIndex.value = -1;
923
+ activeRowIndex.value = -1;
924
+ }
3809
925
  } else {
3810
926
  activeColIndex.value = -1;
3811
927
  activeRowIndex.value = -1;
3812
928
  }
3813
- } else {
3814
- activeColIndex.value = -1;
3815
- activeRowIndex.value = -1;
3816
- }
929
+ updateCellToolbar();
930
+ }, 0);
3817
931
  };
3818
932
  onMounted(() => {
3819
933
  requestAnimationFrame(() => {
@@ -3855,6 +969,9 @@ const TableBlock = defineComponent({
3855
969
  });
3856
970
  }
3857
971
  });
972
+ watch(node, () => {
973
+ dispatchListener();
974
+ });
3858
975
  onBeforeUnmount(() => {
3859
976
  view.dom.removeEventListener("keyup", dispatchListener);
3860
977
  view.dom.removeEventListener("pointerup", dispatchListener);
@@ -3862,12 +979,25 @@ const TableBlock = defineComponent({
3862
979
  if (mo) mo.disconnect();
3863
980
  });
3864
981
  return () => {
3865
- updateCellToolbar();
3866
982
  return /* @__PURE__ */ h(
3867
983
  "div",
3868
984
  {
3869
- onDragstart: (e) => e.preventDefault(),
3870
- onDragover: (e) => e.preventDefault(),
985
+ onDragend: onDragEnd,
986
+ onDragover: (e) => {
987
+ e.preventDefault();
988
+ onDragOver(e);
989
+ if (dragInfo.value) {
990
+ e.stopPropagation();
991
+ }
992
+ },
993
+ onDrop: (e) => {
994
+ const isDragging = !!dragInfo.value;
995
+ onDrop(e);
996
+ if (isDragging) {
997
+ e.preventDefault();
998
+ e.stopPropagation();
999
+ }
1000
+ },
3871
1001
  onDragleave: (e) => e.preventDefault(),
3872
1002
  onPointermove: pointerMove,
3873
1003
  onPointerleave: pointerLeave,
@@ -3882,7 +1012,7 @@ const TableBlock = defineComponent({
3882
1012
  contenteditable: "false",
3883
1013
  onPointerdown: (e) => e.stopPropagation()
3884
1014
  },
3885
- canMerge.value && /* @__PURE__ */ h(
1015
+ /* @__PURE__ */ h(
3886
1016
  "button",
3887
1017
  {
3888
1018
  type: "button",
@@ -3891,7 +1021,7 @@ const TableBlock = defineComponent({
3891
1021
  },
3892
1022
  /* @__PURE__ */ h(Icon, { icon: config.renderButton("merge_cells") })
3893
1023
  ),
3894
- canSplit.value && /* @__PURE__ */ h(
1024
+ /* @__PURE__ */ h(
3895
1025
  "button",
3896
1026
  {
3897
1027
  type: "button",
@@ -3916,6 +1046,7 @@ const TableBlock = defineComponent({
3916
1046
  onClick: () => {
3917
1047
  hoverIndex.value = [0, i];
3918
1048
  selectCol();
1049
+ dispatchListener();
3919
1050
  activeColIndex.value = i;
3920
1051
  activeRowIndex.value = -1;
3921
1052
  },
@@ -3972,7 +1103,9 @@ const TableBlock = defineComponent({
3972
1103
  }
3973
1104
  },
3974
1105
  /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_col") })
3975
- )
1106
+ ),
1107
+ canMerge.value && /* @__PURE__ */ h("button", { type: "button", onPointerdown: onMergeCells }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("merge_cells") })),
1108
+ canSplit.value && /* @__PURE__ */ h("button", { type: "button", onPointerdown: onSplitCell }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("split_cell") }))
3976
1109
  )
3977
1110
  ))), /* @__PURE__ */ h("div", { contenteditable: "false", class: "add-dots-col" }, [
3978
1111
  ...colBounds.value.map((b) => b.left),
@@ -4015,6 +1148,7 @@ const TableBlock = defineComponent({
4015
1148
  onClick: () => {
4016
1149
  hoverIndex.value = [i, 0];
4017
1150
  selectRow();
1151
+ dispatchListener();
4018
1152
  activeRowIndex.value = i;
4019
1153
  activeColIndex.value = -1;
4020
1154
  },
@@ -4037,7 +1171,9 @@ const TableBlock = defineComponent({
4037
1171
  }
4038
1172
  },
4039
1173
  /* @__PURE__ */ h(Icon, { icon: config.renderButton("delete_row") })
4040
- )
1174
+ ),
1175
+ canMerge.value && /* @__PURE__ */ h("button", { type: "button", onPointerdown: onMergeCells }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("merge_cells") })),
1176
+ canSplit.value && /* @__PURE__ */ h("button", { type: "button", onPointerdown: onSplitCell }, /* @__PURE__ */ h(Icon, { icon: config.renderButton("split_cell") }))
4041
1177
  )
4042
1178
  ))), /* @__PURE__ */ h("div", { contenteditable: "false", class: "add-dots-row" }, [
4043
1179
  ...rowBounds.value.map((b) => b.top),
@@ -4061,6 +1197,26 @@ const TableBlock = defineComponent({
4061
1197
  ref: dragPreviewRef
4062
1198
  },
4063
1199
  /* @__PURE__ */ h("table", null, /* @__PURE__ */ h("tbody", null))
1200
+ ), /* @__PURE__ */ h(
1201
+ "div",
1202
+ {
1203
+ "data-show": "false",
1204
+ contenteditable: "false",
1205
+ "data-display-type": "indicator",
1206
+ "data-role": "x-line-drag-handle",
1207
+ class: "handle line-handle",
1208
+ ref: xLineHandleRef
1209
+ }
1210
+ ), /* @__PURE__ */ h(
1211
+ "div",
1212
+ {
1213
+ "data-show": "false",
1214
+ contenteditable: "false",
1215
+ "data-display-type": "indicator",
1216
+ "data-role": "y-line-drag-handle",
1217
+ class: "handle line-handle",
1218
+ ref: yLineHandleRef
1219
+ }
4064
1220
  ), /* @__PURE__ */ h("table", { ref: contentWrapperFunctionRef, class: "children" }, /* @__PURE__ */ h("colgroup", null, colWidths.value.map((w, i) => /* @__PURE__ */ h(
4065
1221
  "col",
4066
1222
  {