@vonage/vivid 5.7.0 → 5.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/bundled/data-grid.options.cjs +1 -0
  2. package/bundled/data-grid.options.js +29 -0
  3. package/bundled/definition19.cjs +3 -4
  4. package/bundled/definition19.js +35 -52
  5. package/bundled/definition2.cjs +1 -1
  6. package/bundled/definition2.js +1 -1
  7. package/bundled/definition3.cjs +1 -1
  8. package/bundled/definition3.js +1 -1
  9. package/bundled/definition8.cjs +1 -1
  10. package/bundled/definition8.js +1 -1
  11. package/bundled/definition9.cjs +9 -9
  12. package/bundled/definition9.js +113 -1342
  13. package/bundled/feature.cjs +4 -0
  14. package/bundled/feature.js +2886 -0
  15. package/bundled/floating-ui.dom.cjs +1 -0
  16. package/bundled/floating-ui.dom.js +1242 -0
  17. package/bundled/listbox.cjs +1 -1
  18. package/bundled/listbox.js +49 -22
  19. package/bundled/localized.cjs +1 -1
  20. package/bundled/localized.js +15 -13
  21. package/bundled/vivid-element.cjs +3 -3
  22. package/bundled/vivid-element.js +89 -69
  23. package/checkbox/definition.cjs +1 -1
  24. package/checkbox/definition.js +1 -1
  25. package/combobox/definition.cjs +14 -15
  26. package/combobox/definition.js +15 -16
  27. package/combobox/index.cjs +4 -4
  28. package/combobox/index.js +82 -87
  29. package/contextual-help/definition.cjs +1 -0
  30. package/contextual-help/definition.js +1 -1
  31. package/custom-elements.json +5145 -3781
  32. package/data-grid/definition.cjs +23 -49
  33. package/data-grid/definition.js +1 -27
  34. package/data-grid/index.cjs +27 -27
  35. package/data-grid/index.js +44 -64
  36. package/data-table/definition.cjs +204 -0
  37. package/data-table/definition.js +188 -0
  38. package/data-table/index.cjs +37 -0
  39. package/data-table/index.js +143 -0
  40. package/file-picker/definition.cjs +10 -8
  41. package/file-picker/definition.js +10 -8
  42. package/file-picker/index.cjs +5 -5
  43. package/file-picker/index.js +12 -12
  44. package/icon/definition.cjs +1 -1
  45. package/icon/definition.js +1 -1
  46. package/index.cjs +26 -0
  47. package/index.js +4 -0
  48. package/lib/components.d.ts +4 -0
  49. package/lib/contextual-help/definition.d.ts +2 -0
  50. package/lib/data-table/definition.d.ts +8 -0
  51. package/lib/data-table/table-body.d.ts +3 -0
  52. package/lib/data-table/table-body.template.d.ts +3 -0
  53. package/lib/data-table/table-cell.d.ts +381 -0
  54. package/lib/data-table/table-cell.template.d.ts +3 -0
  55. package/lib/data-table/table-head.d.ts +3 -0
  56. package/lib/data-table/table-head.template.d.ts +3 -0
  57. package/lib/data-table/table-header-cell.d.ts +381 -0
  58. package/lib/data-table/table-header-cell.template.d.ts +3 -0
  59. package/lib/data-table/table-row.d.ts +381 -0
  60. package/lib/data-table/table-row.template.d.ts +3 -0
  61. package/lib/data-table/table.d.ts +3 -0
  62. package/lib/data-table/table.template.d.ts +3 -0
  63. package/lib/popover/definition.d.ts +4 -0
  64. package/lib/popover/locale.d.ts +3 -0
  65. package/lib/popover/popover.d.ts +781 -0
  66. package/lib/popover/popover.template.d.ts +3 -0
  67. package/lib/rich-text-editor/locale.d.ts +0 -1
  68. package/lib/rich-text-editor/rte/config.d.ts +3 -0
  69. package/lib/rich-text-editor/rte/document.d.ts +2 -0
  70. package/lib/rich-text-editor/rte/exports.d.ts +1 -0
  71. package/lib/rich-text-editor/rte/view.d.ts +30 -0
  72. package/lib/rich-text-view/definition.d.ts +4 -0
  73. package/lib/rich-text-view/rich-text-view.d.ts +15 -0
  74. package/lib/rich-text-view/rich-text-view.template.d.ts +3 -0
  75. package/lib/tag-name-map.d.ts +10 -1
  76. package/locales/de-DE.cjs +3 -1
  77. package/locales/de-DE.js +3 -1
  78. package/locales/en-GB.cjs +3 -1
  79. package/locales/en-GB.js +3 -1
  80. package/locales/en-US.cjs +3 -1
  81. package/locales/en-US.js +3 -1
  82. package/locales/ja-JP.cjs +3 -1
  83. package/locales/ja-JP.js +3 -1
  84. package/locales/zh-CN.cjs +3 -1
  85. package/locales/zh-CN.js +3 -1
  86. package/package.json +8 -12
  87. package/popover/definition.cjs +363 -0
  88. package/popover/definition.js +357 -0
  89. package/popover/index.cjs +27 -0
  90. package/popover/index.js +263 -0
  91. package/rich-text-editor/definition.cjs +328 -3882
  92. package/rich-text-editor/definition.js +143 -3697
  93. package/rich-text-editor/index.cjs +12 -15
  94. package/rich-text-editor/index.js +3489 -6291
  95. package/rich-text-view/definition.cjs +159 -0
  96. package/rich-text-view/definition.js +153 -0
  97. package/rich-text-view/index.cjs +1 -0
  98. package/rich-text-view/index.js +95 -0
  99. package/select/definition.cjs +27 -15
  100. package/select/definition.js +27 -15
  101. package/shared/foundation/listbox/listbox.d.ts +0 -1
  102. package/shared/localization/Locale.d.ts +2 -0
  103. package/tag/definition.cjs +1 -1
  104. package/tag/definition.js +1 -1
  105. package/tag/index.cjs +1 -1
  106. package/tag/index.js +1 -1
  107. package/unbundled/_commonjsHelpers.cjs +26 -0
  108. package/unbundled/_commonjsHelpers.js +26 -1
  109. package/unbundled/data-grid.options.cjs +34 -0
  110. package/unbundled/data-grid.options.js +28 -0
  111. package/unbundled/definition.cjs +1 -1
  112. package/unbundled/definition.js +1 -1
  113. package/unbundled/feature.cjs +3678 -0
  114. package/unbundled/feature.js +3662 -0
  115. package/unbundled/listbox.cjs +29 -2
  116. package/unbundled/listbox.js +29 -2
  117. package/unbundled/vivid-element.cjs +1 -1
  118. package/unbundled/vivid-element.js +1 -1
  119. package/video-player/definition.cjs +11 -1
  120. package/video-player/definition.js +12 -2
  121. package/video-player/index.cjs +30 -30
  122. package/video-player/index.js +705 -702
  123. package/vivid.api.json +1478 -39
@@ -1,5 +1,6 @@
1
1
  import { d as defineVividComponent, V as VividElement, c as createRegisterFunction } from '../unbundled/vivid-element.js';
2
2
  import { html, observable, volatile, ElementStyles, ref } from '@microsoft/fast-element';
3
+ import { S as Slice, F as Fragment, M as Mark, a as MarkType, R as ReplaceError, N as Node, D as DOMSerializer, b as DOMParser, c as RteFeatureImpl, i as impl, f as featureFacade, d as contributionPriority, s as sortedContributions, e as Schema, g as NodeRange } from '../unbundled/feature.js';
3
4
  import { W as WithObservableLocale } from '../unbundled/localized.js';
4
5
  import { p as popupDefinition, P as Popup, a as PlacementStrategy } from '../unbundled/definition5.js';
5
6
  import { dividerDefinition } from '../divider/definition.js';
@@ -16,3603 +17,6 @@ import { g as generateRandomId } from '../unbundled/mixins.js';
16
17
 
17
18
  const styles = ":host{display:block}.rte{display:flex;flex-direction:column;block-size:100%}.editor{display:contents}.editor-viewport{position:relative;overflow:hidden;flex:1;contain:layout}.editor-scroll-area{--editor-padding-block: 8px;--editor-padding-inline: 16px;display:flex;flex-direction:column;background-color:var(--vvd-color-canvas);block-size:100%;color:var(--vvd-color-canvas-text);font:var(--vvd-typography-base);overflow-y:auto;--scrollbar-track-color: transparent;--scrollbar-thumb-color: color-mix(in srgb, var(--vvd-color-neutral-950), transparent 70%)}.editor-scroll-area{scrollbar-color:var(--scrollbar-thumb-color) var(--scrollbar-track-color);scrollbar-width:thin}.editor-scroll-area ::-webkit-scrollbar{width:4px}.editor-scroll-area ::-webkit-scrollbar-track{background:var(--scrollbar-track-color)}.editor-scroll-area ::-webkit-scrollbar-thumb{border:0;border-radius:4px;background-color:var(--scrollbar-fallback-track-color, var(--scrollbar-thumb-color))}";
18
19
 
19
- // ::- Persistent data structure representing an ordered mapping from
20
- // strings to values, with some convenient update methods.
21
- function OrderedMap(content) {
22
- this.content = content;
23
- }
24
-
25
- OrderedMap.prototype = {
26
- constructor: OrderedMap,
27
-
28
- find: function(key) {
29
- for (var i = 0; i < this.content.length; i += 2)
30
- if (this.content[i] === key) return i
31
- return -1
32
- },
33
-
34
- // :: (string) → ?any
35
- // Retrieve the value stored under `key`, or return undefined when
36
- // no such key exists.
37
- get: function(key) {
38
- var found = this.find(key);
39
- return found == -1 ? undefined : this.content[found + 1]
40
- },
41
-
42
- // :: (string, any, ?string) → OrderedMap
43
- // Create a new map by replacing the value of `key` with a new
44
- // value, or adding a binding to the end of the map. If `newKey` is
45
- // given, the key of the binding will be replaced with that key.
46
- update: function(key, value, newKey) {
47
- var self = newKey && newKey != key ? this.remove(newKey) : this;
48
- var found = self.find(key), content = self.content.slice();
49
- if (found == -1) {
50
- content.push(newKey || key, value);
51
- } else {
52
- content[found + 1] = value;
53
- if (newKey) content[found] = newKey;
54
- }
55
- return new OrderedMap(content)
56
- },
57
-
58
- // :: (string) → OrderedMap
59
- // Return a map with the given key removed, if it existed.
60
- remove: function(key) {
61
- var found = this.find(key);
62
- if (found == -1) return this
63
- var content = this.content.slice();
64
- content.splice(found, 2);
65
- return new OrderedMap(content)
66
- },
67
-
68
- // :: (string, any) → OrderedMap
69
- // Add a new key to the start of the map.
70
- addToStart: function(key, value) {
71
- return new OrderedMap([key, value].concat(this.remove(key).content))
72
- },
73
-
74
- // :: (string, any) → OrderedMap
75
- // Add a new key to the end of the map.
76
- addToEnd: function(key, value) {
77
- var content = this.remove(key).content.slice();
78
- content.push(key, value);
79
- return new OrderedMap(content)
80
- },
81
-
82
- // :: (string, string, any) → OrderedMap
83
- // Add a key after the given key. If `place` is not found, the new
84
- // key is added to the end.
85
- addBefore: function(place, key, value) {
86
- var without = this.remove(key), content = without.content.slice();
87
- var found = without.find(place);
88
- content.splice(found == -1 ? content.length : found, 0, key, value);
89
- return new OrderedMap(content)
90
- },
91
-
92
- // :: ((key: string, value: any))
93
- // Call the given function for each key/value pair in the map, in
94
- // order.
95
- forEach: function(f) {
96
- for (var i = 0; i < this.content.length; i += 2)
97
- f(this.content[i], this.content[i + 1]);
98
- },
99
-
100
- // :: (union<Object, OrderedMap>) → OrderedMap
101
- // Create a new map by prepending the keys in this map that don't
102
- // appear in `map` before the keys in `map`.
103
- prepend: function(map) {
104
- map = OrderedMap.from(map);
105
- if (!map.size) return this
106
- return new OrderedMap(map.content.concat(this.subtract(map).content))
107
- },
108
-
109
- // :: (union<Object, OrderedMap>) → OrderedMap
110
- // Create a new map by appending the keys in this map that don't
111
- // appear in `map` after the keys in `map`.
112
- append: function(map) {
113
- map = OrderedMap.from(map);
114
- if (!map.size) return this
115
- return new OrderedMap(this.subtract(map).content.concat(map.content))
116
- },
117
-
118
- // :: (union<Object, OrderedMap>) → OrderedMap
119
- // Create a map containing all the keys in this map that don't
120
- // appear in `map`.
121
- subtract: function(map) {
122
- var result = this;
123
- map = OrderedMap.from(map);
124
- for (var i = 0; i < map.content.length; i += 2)
125
- result = result.remove(map.content[i]);
126
- return result
127
- },
128
-
129
- // :: () → Object
130
- // Turn ordered map into a plain object.
131
- toObject: function() {
132
- var result = {};
133
- this.forEach(function(key, value) { result[key] = value; });
134
- return result
135
- },
136
-
137
- // :: number
138
- // The amount of keys in this map.
139
- get size() {
140
- return this.content.length >> 1
141
- }
142
- };
143
-
144
- // :: (?union<Object, OrderedMap>) → OrderedMap
145
- // Return a map with the given content. If null, create an empty
146
- // map. If given an ordered map, return that map itself. If given an
147
- // object, create a map from the object's properties.
148
- OrderedMap.from = function(value) {
149
- if (value instanceof OrderedMap) return value
150
- var content = [];
151
- if (value) for (var prop in value) content.push(prop, value[prop]);
152
- return new OrderedMap(content)
153
- };
154
-
155
- function findDiffStart(a, b, pos) {
156
- for (let i = 0;; i++) {
157
- if (i == a.childCount || i == b.childCount)
158
- return a.childCount == b.childCount ? null : pos;
159
- let childA = a.child(i), childB = b.child(i);
160
- if (childA == childB) {
161
- pos += childA.nodeSize;
162
- continue;
163
- }
164
- if (!childA.sameMarkup(childB))
165
- return pos;
166
- if (childA.isText && childA.text != childB.text) {
167
- for (let j = 0; childA.text[j] == childB.text[j]; j++)
168
- pos++;
169
- return pos;
170
- }
171
- if (childA.content.size || childB.content.size) {
172
- let inner = findDiffStart(childA.content, childB.content, pos + 1);
173
- if (inner != null)
174
- return inner;
175
- }
176
- pos += childA.nodeSize;
177
- }
178
- }
179
- function findDiffEnd(a, b, posA, posB) {
180
- for (let iA = a.childCount, iB = b.childCount;;) {
181
- if (iA == 0 || iB == 0)
182
- return iA == iB ? null : { a: posA, b: posB };
183
- let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize;
184
- if (childA == childB) {
185
- posA -= size;
186
- posB -= size;
187
- continue;
188
- }
189
- if (!childA.sameMarkup(childB))
190
- return { a: posA, b: posB };
191
- if (childA.isText && childA.text != childB.text) {
192
- let same = 0, minSize = Math.min(childA.text.length, childB.text.length);
193
- while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) {
194
- same++;
195
- posA--;
196
- posB--;
197
- }
198
- return { a: posA, b: posB };
199
- }
200
- if (childA.content.size || childB.content.size) {
201
- let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1);
202
- if (inner)
203
- return inner;
204
- }
205
- posA -= size;
206
- posB -= size;
207
- }
208
- }
209
-
210
- /**
211
- A fragment represents a node's collection of child nodes.
212
-
213
- Like nodes, fragments are persistent data structures, and you
214
- should not mutate them or their content. Rather, you create new
215
- instances whenever needed. The API tries to make this easy.
216
- */
217
- class Fragment {
218
- /**
219
- @internal
220
- */
221
- constructor(
222
- /**
223
- The child nodes in this fragment.
224
- */
225
- content, size) {
226
- this.content = content;
227
- this.size = size || 0;
228
- if (size == null)
229
- for (let i = 0; i < content.length; i++)
230
- this.size += content[i].nodeSize;
231
- }
232
- /**
233
- Invoke a callback for all descendant nodes between the given two
234
- positions (relative to start of this fragment). Doesn't descend
235
- into a node when the callback returns `false`.
236
- */
237
- nodesBetween(from, to, f, nodeStart = 0, parent) {
238
- for (let i = 0, pos = 0; pos < to; i++) {
239
- let child = this.content[i], end = pos + child.nodeSize;
240
- if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) {
241
- let start = pos + 1;
242
- child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start);
243
- }
244
- pos = end;
245
- }
246
- }
247
- /**
248
- Call the given callback for every descendant node. `pos` will be
249
- relative to the start of the fragment. The callback may return
250
- `false` to prevent traversal of a given node's children.
251
- */
252
- descendants(f) {
253
- this.nodesBetween(0, this.size, f);
254
- }
255
- /**
256
- Extract the text between `from` and `to`. See the same method on
257
- [`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween).
258
- */
259
- textBetween(from, to, blockSeparator, leafText) {
260
- let text = "", first = true;
261
- this.nodesBetween(from, to, (node, pos) => {
262
- let nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos)
263
- : !node.isLeaf ? ""
264
- : leafText ? (typeof leafText === "function" ? leafText(node) : leafText)
265
- : node.type.spec.leafText ? node.type.spec.leafText(node)
266
- : "";
267
- if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) {
268
- if (first)
269
- first = false;
270
- else
271
- text += blockSeparator;
272
- }
273
- text += nodeText;
274
- }, 0);
275
- return text;
276
- }
277
- /**
278
- Create a new fragment containing the combined content of this
279
- fragment and the other.
280
- */
281
- append(other) {
282
- if (!other.size)
283
- return this;
284
- if (!this.size)
285
- return other;
286
- let last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0;
287
- if (last.isText && last.sameMarkup(first)) {
288
- content[content.length - 1] = last.withText(last.text + first.text);
289
- i = 1;
290
- }
291
- for (; i < other.content.length; i++)
292
- content.push(other.content[i]);
293
- return new Fragment(content, this.size + other.size);
294
- }
295
- /**
296
- Cut out the sub-fragment between the two given positions.
297
- */
298
- cut(from, to = this.size) {
299
- if (from == 0 && to == this.size)
300
- return this;
301
- let result = [], size = 0;
302
- if (to > from)
303
- for (let i = 0, pos = 0; pos < to; i++) {
304
- let child = this.content[i], end = pos + child.nodeSize;
305
- if (end > from) {
306
- if (pos < from || end > to) {
307
- if (child.isText)
308
- child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));
309
- else
310
- child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1));
311
- }
312
- result.push(child);
313
- size += child.nodeSize;
314
- }
315
- pos = end;
316
- }
317
- return new Fragment(result, size);
318
- }
319
- /**
320
- @internal
321
- */
322
- cutByIndex(from, to) {
323
- if (from == to)
324
- return Fragment.empty;
325
- if (from == 0 && to == this.content.length)
326
- return this;
327
- return new Fragment(this.content.slice(from, to));
328
- }
329
- /**
330
- Create a new fragment in which the node at the given index is
331
- replaced by the given node.
332
- */
333
- replaceChild(index, node) {
334
- let current = this.content[index];
335
- if (current == node)
336
- return this;
337
- let copy = this.content.slice();
338
- let size = this.size + node.nodeSize - current.nodeSize;
339
- copy[index] = node;
340
- return new Fragment(copy, size);
341
- }
342
- /**
343
- Create a new fragment by prepending the given node to this
344
- fragment.
345
- */
346
- addToStart(node) {
347
- return new Fragment([node].concat(this.content), this.size + node.nodeSize);
348
- }
349
- /**
350
- Create a new fragment by appending the given node to this
351
- fragment.
352
- */
353
- addToEnd(node) {
354
- return new Fragment(this.content.concat(node), this.size + node.nodeSize);
355
- }
356
- /**
357
- Compare this fragment to another one.
358
- */
359
- eq(other) {
360
- if (this.content.length != other.content.length)
361
- return false;
362
- for (let i = 0; i < this.content.length; i++)
363
- if (!this.content[i].eq(other.content[i]))
364
- return false;
365
- return true;
366
- }
367
- /**
368
- The first child of the fragment, or `null` if it is empty.
369
- */
370
- get firstChild() { return this.content.length ? this.content[0] : null; }
371
- /**
372
- The last child of the fragment, or `null` if it is empty.
373
- */
374
- get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; }
375
- /**
376
- The number of child nodes in this fragment.
377
- */
378
- get childCount() { return this.content.length; }
379
- /**
380
- Get the child node at the given index. Raise an error when the
381
- index is out of range.
382
- */
383
- child(index) {
384
- let found = this.content[index];
385
- if (!found)
386
- throw new RangeError("Index " + index + " out of range for " + this);
387
- return found;
388
- }
389
- /**
390
- Get the child node at the given index, if it exists.
391
- */
392
- maybeChild(index) {
393
- return this.content[index] || null;
394
- }
395
- /**
396
- Call `f` for every child node, passing the node, its offset
397
- into this parent node, and its index.
398
- */
399
- forEach(f) {
400
- for (let i = 0, p = 0; i < this.content.length; i++) {
401
- let child = this.content[i];
402
- f(child, p, i);
403
- p += child.nodeSize;
404
- }
405
- }
406
- /**
407
- Find the first position at which this fragment and another
408
- fragment differ, or `null` if they are the same.
409
- */
410
- findDiffStart(other, pos = 0) {
411
- return findDiffStart(this, other, pos);
412
- }
413
- /**
414
- Find the first position, searching from the end, at which this
415
- fragment and the given fragment differ, or `null` if they are
416
- the same. Since this position will not be the same in both
417
- nodes, an object with two separate positions is returned.
418
- */
419
- findDiffEnd(other, pos = this.size, otherPos = other.size) {
420
- return findDiffEnd(this, other, pos, otherPos);
421
- }
422
- /**
423
- Find the index and inner offset corresponding to a given relative
424
- position in this fragment. The result object will be reused
425
- (overwritten) the next time the function is called. @internal
426
- */
427
- findIndex(pos) {
428
- if (pos == 0)
429
- return retIndex(0, pos);
430
- if (pos == this.size)
431
- return retIndex(this.content.length, pos);
432
- if (pos > this.size || pos < 0)
433
- throw new RangeError(`Position ${pos} outside of fragment (${this})`);
434
- for (let i = 0, curPos = 0;; i++) {
435
- let cur = this.child(i), end = curPos + cur.nodeSize;
436
- if (end >= pos) {
437
- if (end == pos)
438
- return retIndex(i + 1, end);
439
- return retIndex(i, curPos);
440
- }
441
- curPos = end;
442
- }
443
- }
444
- /**
445
- Return a debugging string that describes this fragment.
446
- */
447
- toString() { return "<" + this.toStringInner() + ">"; }
448
- /**
449
- @internal
450
- */
451
- toStringInner() { return this.content.join(", "); }
452
- /**
453
- Create a JSON-serializeable representation of this fragment.
454
- */
455
- toJSON() {
456
- return this.content.length ? this.content.map(n => n.toJSON()) : null;
457
- }
458
- /**
459
- Deserialize a fragment from its JSON representation.
460
- */
461
- static fromJSON(schema, value) {
462
- if (!value)
463
- return Fragment.empty;
464
- if (!Array.isArray(value))
465
- throw new RangeError("Invalid input for Fragment.fromJSON");
466
- return new Fragment(value.map(schema.nodeFromJSON));
467
- }
468
- /**
469
- Build a fragment from an array of nodes. Ensures that adjacent
470
- text nodes with the same marks are joined together.
471
- */
472
- static fromArray(array) {
473
- if (!array.length)
474
- return Fragment.empty;
475
- let joined, size = 0;
476
- for (let i = 0; i < array.length; i++) {
477
- let node = array[i];
478
- size += node.nodeSize;
479
- if (i && node.isText && array[i - 1].sameMarkup(node)) {
480
- if (!joined)
481
- joined = array.slice(0, i);
482
- joined[joined.length - 1] = node
483
- .withText(joined[joined.length - 1].text + node.text);
484
- }
485
- else if (joined) {
486
- joined.push(node);
487
- }
488
- }
489
- return new Fragment(joined || array, size);
490
- }
491
- /**
492
- Create a fragment from something that can be interpreted as a
493
- set of nodes. For `null`, it returns the empty fragment. For a
494
- fragment, the fragment itself. For a node or array of nodes, a
495
- fragment containing those nodes.
496
- */
497
- static from(nodes) {
498
- if (!nodes)
499
- return Fragment.empty;
500
- if (nodes instanceof Fragment)
501
- return nodes;
502
- if (Array.isArray(nodes))
503
- return this.fromArray(nodes);
504
- if (nodes.attrs)
505
- return new Fragment([nodes], nodes.nodeSize);
506
- throw new RangeError("Can not convert " + nodes + " to a Fragment" +
507
- (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : ""));
508
- }
509
- }
510
- /**
511
- An empty fragment. Intended to be reused whenever a node doesn't
512
- contain anything (rather than allocating a new empty fragment for
513
- each leaf node).
514
- */
515
- Fragment.empty = new Fragment([], 0);
516
- const found = { index: 0, offset: 0 };
517
- function retIndex(index, offset) {
518
- found.index = index;
519
- found.offset = offset;
520
- return found;
521
- }
522
-
523
- function compareDeep(a, b) {
524
- if (a === b)
525
- return true;
526
- if (!(a && typeof a == "object") ||
527
- !(b && typeof b == "object"))
528
- return false;
529
- let array = Array.isArray(a);
530
- if (Array.isArray(b) != array)
531
- return false;
532
- if (array) {
533
- if (a.length != b.length)
534
- return false;
535
- for (let i = 0; i < a.length; i++)
536
- if (!compareDeep(a[i], b[i]))
537
- return false;
538
- }
539
- else {
540
- for (let p in a)
541
- if (!(p in b) || !compareDeep(a[p], b[p]))
542
- return false;
543
- for (let p in b)
544
- if (!(p in a))
545
- return false;
546
- }
547
- return true;
548
- }
549
-
550
- /**
551
- A mark is a piece of information that can be attached to a node,
552
- such as it being emphasized, in code font, or a link. It has a
553
- type and optionally a set of attributes that provide further
554
- information (such as the target of the link). Marks are created
555
- through a `Schema`, which controls which types exist and which
556
- attributes they have.
557
- */
558
- class Mark {
559
- /**
560
- @internal
561
- */
562
- constructor(
563
- /**
564
- The type of this mark.
565
- */
566
- type,
567
- /**
568
- The attributes associated with this mark.
569
- */
570
- attrs) {
571
- this.type = type;
572
- this.attrs = attrs;
573
- }
574
- /**
575
- Given a set of marks, create a new set which contains this one as
576
- well, in the right position. If this mark is already in the set,
577
- the set itself is returned. If any marks that are set to be
578
- [exclusive](https://prosemirror.net/docs/ref/#model.MarkSpec.excludes) with this mark are present,
579
- those are replaced by this one.
580
- */
581
- addToSet(set) {
582
- let copy, placed = false;
583
- for (let i = 0; i < set.length; i++) {
584
- let other = set[i];
585
- if (this.eq(other))
586
- return set;
587
- if (this.type.excludes(other.type)) {
588
- if (!copy)
589
- copy = set.slice(0, i);
590
- }
591
- else if (other.type.excludes(this.type)) {
592
- return set;
593
- }
594
- else {
595
- if (!placed && other.type.rank > this.type.rank) {
596
- if (!copy)
597
- copy = set.slice(0, i);
598
- copy.push(this);
599
- placed = true;
600
- }
601
- if (copy)
602
- copy.push(other);
603
- }
604
- }
605
- if (!copy)
606
- copy = set.slice();
607
- if (!placed)
608
- copy.push(this);
609
- return copy;
610
- }
611
- /**
612
- Remove this mark from the given set, returning a new set. If this
613
- mark is not in the set, the set itself is returned.
614
- */
615
- removeFromSet(set) {
616
- for (let i = 0; i < set.length; i++)
617
- if (this.eq(set[i]))
618
- return set.slice(0, i).concat(set.slice(i + 1));
619
- return set;
620
- }
621
- /**
622
- Test whether this mark is in the given set of marks.
623
- */
624
- isInSet(set) {
625
- for (let i = 0; i < set.length; i++)
626
- if (this.eq(set[i]))
627
- return true;
628
- return false;
629
- }
630
- /**
631
- Test whether this mark has the same type and attributes as
632
- another mark.
633
- */
634
- eq(other) {
635
- return this == other ||
636
- (this.type == other.type && compareDeep(this.attrs, other.attrs));
637
- }
638
- /**
639
- Convert this mark to a JSON-serializeable representation.
640
- */
641
- toJSON() {
642
- let obj = { type: this.type.name };
643
- for (let _ in this.attrs) {
644
- obj.attrs = this.attrs;
645
- break;
646
- }
647
- return obj;
648
- }
649
- /**
650
- Deserialize a mark from JSON.
651
- */
652
- static fromJSON(schema, json) {
653
- if (!json)
654
- throw new RangeError("Invalid input for Mark.fromJSON");
655
- let type = schema.marks[json.type];
656
- if (!type)
657
- throw new RangeError(`There is no mark type ${json.type} in this schema`);
658
- let mark = type.create(json.attrs);
659
- type.checkAttrs(mark.attrs);
660
- return mark;
661
- }
662
- /**
663
- Test whether two sets of marks are identical.
664
- */
665
- static sameSet(a, b) {
666
- if (a == b)
667
- return true;
668
- if (a.length != b.length)
669
- return false;
670
- for (let i = 0; i < a.length; i++)
671
- if (!a[i].eq(b[i]))
672
- return false;
673
- return true;
674
- }
675
- /**
676
- Create a properly sorted mark set from null, a single mark, or an
677
- unsorted array of marks.
678
- */
679
- static setFrom(marks) {
680
- if (!marks || Array.isArray(marks) && marks.length == 0)
681
- return Mark.none;
682
- if (marks instanceof Mark)
683
- return [marks];
684
- let copy = marks.slice();
685
- copy.sort((a, b) => a.type.rank - b.type.rank);
686
- return copy;
687
- }
688
- }
689
- /**
690
- The empty set of marks.
691
- */
692
- Mark.none = [];
693
-
694
- /**
695
- Error type raised by [`Node.replace`](https://prosemirror.net/docs/ref/#model.Node.replace) when
696
- given an invalid replacement.
697
- */
698
- class ReplaceError extends Error {
699
- }
700
- /*
701
- ReplaceError = function(this: any, message: string) {
702
- let err = Error.call(this, message)
703
- ;(err as any).__proto__ = ReplaceError.prototype
704
- return err
705
- } as any
706
-
707
- ReplaceError.prototype = Object.create(Error.prototype)
708
- ReplaceError.prototype.constructor = ReplaceError
709
- ReplaceError.prototype.name = "ReplaceError"
710
- */
711
- /**
712
- A slice represents a piece cut out of a larger document. It
713
- stores not only a fragment, but also the depth up to which nodes on
714
- both side are ‘open’ (cut through).
715
- */
716
- class Slice {
717
- /**
718
- Create a slice. When specifying a non-zero open depth, you must
719
- make sure that there are nodes of at least that depth at the
720
- appropriate side of the fragment—i.e. if the fragment is an
721
- empty paragraph node, `openStart` and `openEnd` can't be greater
722
- than 1.
723
-
724
- It is not necessary for the content of open nodes to conform to
725
- the schema's content constraints, though it should be a valid
726
- start/end/middle for such a node, depending on which sides are
727
- open.
728
- */
729
- constructor(
730
- /**
731
- The slice's content.
732
- */
733
- content,
734
- /**
735
- The open depth at the start of the fragment.
736
- */
737
- openStart,
738
- /**
739
- The open depth at the end.
740
- */
741
- openEnd) {
742
- this.content = content;
743
- this.openStart = openStart;
744
- this.openEnd = openEnd;
745
- }
746
- /**
747
- The size this slice would add when inserted into a document.
748
- */
749
- get size() {
750
- return this.content.size - this.openStart - this.openEnd;
751
- }
752
- /**
753
- @internal
754
- */
755
- insertAt(pos, fragment) {
756
- let content = insertInto(this.content, pos + this.openStart, fragment);
757
- return content && new Slice(content, this.openStart, this.openEnd);
758
- }
759
- /**
760
- @internal
761
- */
762
- removeBetween(from, to) {
763
- return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd);
764
- }
765
- /**
766
- Tests whether this slice is equal to another slice.
767
- */
768
- eq(other) {
769
- return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd;
770
- }
771
- /**
772
- @internal
773
- */
774
- toString() {
775
- return this.content + "(" + this.openStart + "," + this.openEnd + ")";
776
- }
777
- /**
778
- Convert a slice to a JSON-serializable representation.
779
- */
780
- toJSON() {
781
- if (!this.content.size)
782
- return null;
783
- let json = { content: this.content.toJSON() };
784
- if (this.openStart > 0)
785
- json.openStart = this.openStart;
786
- if (this.openEnd > 0)
787
- json.openEnd = this.openEnd;
788
- return json;
789
- }
790
- /**
791
- Deserialize a slice from its JSON representation.
792
- */
793
- static fromJSON(schema, json) {
794
- if (!json)
795
- return Slice.empty;
796
- let openStart = json.openStart || 0, openEnd = json.openEnd || 0;
797
- if (typeof openStart != "number" || typeof openEnd != "number")
798
- throw new RangeError("Invalid input for Slice.fromJSON");
799
- return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd);
800
- }
801
- /**
802
- Create a slice from a fragment by taking the maximum possible
803
- open value on both side of the fragment.
804
- */
805
- static maxOpen(fragment, openIsolating = true) {
806
- let openStart = 0, openEnd = 0;
807
- for (let n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild)
808
- openStart++;
809
- for (let n = fragment.lastChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.lastChild)
810
- openEnd++;
811
- return new Slice(fragment, openStart, openEnd);
812
- }
813
- }
814
- /**
815
- The empty slice.
816
- */
817
- Slice.empty = new Slice(Fragment.empty, 0, 0);
818
- function removeRange(content, from, to) {
819
- let { index, offset } = content.findIndex(from), child = content.maybeChild(index);
820
- let { index: indexTo, offset: offsetTo } = content.findIndex(to);
821
- if (offset == from || child.isText) {
822
- if (offsetTo != to && !content.child(indexTo).isText)
823
- throw new RangeError("Removing non-flat range");
824
- return content.cut(0, from).append(content.cut(to));
825
- }
826
- if (index != indexTo)
827
- throw new RangeError("Removing non-flat range");
828
- return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)));
829
- }
830
- function insertInto(content, dist, insert, parent) {
831
- let { index, offset } = content.findIndex(dist), child = content.maybeChild(index);
832
- if (offset == dist || child.isText) {
833
- if (parent && !parent.canReplace(index, index, insert))
834
- return null;
835
- return content.cut(0, dist).append(insert).append(content.cut(dist));
836
- }
837
- let inner = insertInto(child.content, dist - offset - 1, insert, child);
838
- return inner && content.replaceChild(index, child.copy(inner));
839
- }
840
- function replace($from, $to, slice) {
841
- if (slice.openStart > $from.depth)
842
- throw new ReplaceError("Inserted content deeper than insertion position");
843
- if ($from.depth - slice.openStart != $to.depth - slice.openEnd)
844
- throw new ReplaceError("Inconsistent open depths");
845
- return replaceOuter($from, $to, slice, 0);
846
- }
847
- function replaceOuter($from, $to, slice, depth) {
848
- let index = $from.index(depth), node = $from.node(depth);
849
- if (index == $to.index(depth) && depth < $from.depth - slice.openStart) {
850
- let inner = replaceOuter($from, $to, slice, depth + 1);
851
- return node.copy(node.content.replaceChild(index, inner));
852
- }
853
- else if (!slice.content.size) {
854
- return close(node, replaceTwoWay($from, $to, depth));
855
- }
856
- else if (!slice.openStart && !slice.openEnd && $from.depth == depth && $to.depth == depth) { // Simple, flat case
857
- let parent = $from.parent, content = parent.content;
858
- return close(parent, content.cut(0, $from.parentOffset).append(slice.content).append(content.cut($to.parentOffset)));
859
- }
860
- else {
861
- let { start, end } = prepareSliceForReplace(slice, $from);
862
- return close(node, replaceThreeWay($from, start, end, $to, depth));
863
- }
864
- }
865
- function checkJoin(main, sub) {
866
- if (!sub.type.compatibleContent(main.type))
867
- throw new ReplaceError("Cannot join " + sub.type.name + " onto " + main.type.name);
868
- }
869
- function joinable$1($before, $after, depth) {
870
- let node = $before.node(depth);
871
- checkJoin(node, $after.node(depth));
872
- return node;
873
- }
874
- function addNode(child, target) {
875
- let last = target.length - 1;
876
- if (last >= 0 && child.isText && child.sameMarkup(target[last]))
877
- target[last] = child.withText(target[last].text + child.text);
878
- else
879
- target.push(child);
880
- }
881
- function addRange($start, $end, depth, target) {
882
- let node = ($end || $start).node(depth);
883
- let startIndex = 0, endIndex = $end ? $end.index(depth) : node.childCount;
884
- if ($start) {
885
- startIndex = $start.index(depth);
886
- if ($start.depth > depth) {
887
- startIndex++;
888
- }
889
- else if ($start.textOffset) {
890
- addNode($start.nodeAfter, target);
891
- startIndex++;
892
- }
893
- }
894
- for (let i = startIndex; i < endIndex; i++)
895
- addNode(node.child(i), target);
896
- if ($end && $end.depth == depth && $end.textOffset)
897
- addNode($end.nodeBefore, target);
898
- }
899
- function close(node, content) {
900
- node.type.checkContent(content);
901
- return node.copy(content);
902
- }
903
- function replaceThreeWay($from, $start, $end, $to, depth) {
904
- let openStart = $from.depth > depth && joinable$1($from, $start, depth + 1);
905
- let openEnd = $to.depth > depth && joinable$1($end, $to, depth + 1);
906
- let content = [];
907
- addRange(null, $from, depth, content);
908
- if (openStart && openEnd && $start.index(depth) == $end.index(depth)) {
909
- checkJoin(openStart, openEnd);
910
- addNode(close(openStart, replaceThreeWay($from, $start, $end, $to, depth + 1)), content);
911
- }
912
- else {
913
- if (openStart)
914
- addNode(close(openStart, replaceTwoWay($from, $start, depth + 1)), content);
915
- addRange($start, $end, depth, content);
916
- if (openEnd)
917
- addNode(close(openEnd, replaceTwoWay($end, $to, depth + 1)), content);
918
- }
919
- addRange($to, null, depth, content);
920
- return new Fragment(content);
921
- }
922
- function replaceTwoWay($from, $to, depth) {
923
- let content = [];
924
- addRange(null, $from, depth, content);
925
- if ($from.depth > depth) {
926
- let type = joinable$1($from, $to, depth + 1);
927
- addNode(close(type, replaceTwoWay($from, $to, depth + 1)), content);
928
- }
929
- addRange($to, null, depth, content);
930
- return new Fragment(content);
931
- }
932
- function prepareSliceForReplace(slice, $along) {
933
- let extra = $along.depth - slice.openStart, parent = $along.node(extra);
934
- let node = parent.copy(slice.content);
935
- for (let i = extra - 1; i >= 0; i--)
936
- node = $along.node(i).copy(Fragment.from(node));
937
- return { start: node.resolveNoCache(slice.openStart + extra),
938
- end: node.resolveNoCache(node.content.size - slice.openEnd - extra) };
939
- }
940
-
941
- /**
942
- You can [_resolve_](https://prosemirror.net/docs/ref/#model.Node.resolve) a position to get more
943
- information about it. Objects of this class represent such a
944
- resolved position, providing various pieces of context
945
- information, and some helper methods.
946
-
947
- Throughout this interface, methods that take an optional `depth`
948
- parameter will interpret undefined as `this.depth` and negative
949
- numbers as `this.depth + value`.
950
- */
951
- class ResolvedPos {
952
- /**
953
- @internal
954
- */
955
- constructor(
956
- /**
957
- The position that was resolved.
958
- */
959
- pos,
960
- /**
961
- @internal
962
- */
963
- path,
964
- /**
965
- The offset this position has into its parent node.
966
- */
967
- parentOffset) {
968
- this.pos = pos;
969
- this.path = path;
970
- this.parentOffset = parentOffset;
971
- this.depth = path.length / 3 - 1;
972
- }
973
- /**
974
- @internal
975
- */
976
- resolveDepth(val) {
977
- if (val == null)
978
- return this.depth;
979
- if (val < 0)
980
- return this.depth + val;
981
- return val;
982
- }
983
- /**
984
- The parent node that the position points into. Note that even if
985
- a position points into a text node, that node is not considered
986
- the parent—text nodes are ‘flat’ in this model, and have no content.
987
- */
988
- get parent() { return this.node(this.depth); }
989
- /**
990
- The root node in which the position was resolved.
991
- */
992
- get doc() { return this.node(0); }
993
- /**
994
- The ancestor node at the given level. `p.node(p.depth)` is the
995
- same as `p.parent`.
996
- */
997
- node(depth) { return this.path[this.resolveDepth(depth) * 3]; }
998
- /**
999
- The index into the ancestor at the given level. If this points
1000
- at the 3rd node in the 2nd paragraph on the top level, for
1001
- example, `p.index(0)` is 1 and `p.index(1)` is 2.
1002
- */
1003
- index(depth) { return this.path[this.resolveDepth(depth) * 3 + 1]; }
1004
- /**
1005
- The index pointing after this position into the ancestor at the
1006
- given level.
1007
- */
1008
- indexAfter(depth) {
1009
- depth = this.resolveDepth(depth);
1010
- return this.index(depth) + (depth == this.depth && !this.textOffset ? 0 : 1);
1011
- }
1012
- /**
1013
- The (absolute) position at the start of the node at the given
1014
- level.
1015
- */
1016
- start(depth) {
1017
- depth = this.resolveDepth(depth);
1018
- return depth == 0 ? 0 : this.path[depth * 3 - 1] + 1;
1019
- }
1020
- /**
1021
- The (absolute) position at the end of the node at the given
1022
- level.
1023
- */
1024
- end(depth) {
1025
- depth = this.resolveDepth(depth);
1026
- return this.start(depth) + this.node(depth).content.size;
1027
- }
1028
- /**
1029
- The (absolute) position directly before the wrapping node at the
1030
- given level, or, when `depth` is `this.depth + 1`, the original
1031
- position.
1032
- */
1033
- before(depth) {
1034
- depth = this.resolveDepth(depth);
1035
- if (!depth)
1036
- throw new RangeError("There is no position before the top-level node");
1037
- return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1];
1038
- }
1039
- /**
1040
- The (absolute) position directly after the wrapping node at the
1041
- given level, or the original position when `depth` is `this.depth + 1`.
1042
- */
1043
- after(depth) {
1044
- depth = this.resolveDepth(depth);
1045
- if (!depth)
1046
- throw new RangeError("There is no position after the top-level node");
1047
- return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1] + this.path[depth * 3].nodeSize;
1048
- }
1049
- /**
1050
- When this position points into a text node, this returns the
1051
- distance between the position and the start of the text node.
1052
- Will be zero for positions that point between nodes.
1053
- */
1054
- get textOffset() { return this.pos - this.path[this.path.length - 1]; }
1055
- /**
1056
- Get the node directly after the position, if any. If the position
1057
- points into a text node, only the part of that node after the
1058
- position is returned.
1059
- */
1060
- get nodeAfter() {
1061
- let parent = this.parent, index = this.index(this.depth);
1062
- if (index == parent.childCount)
1063
- return null;
1064
- let dOff = this.pos - this.path[this.path.length - 1], child = parent.child(index);
1065
- return dOff ? parent.child(index).cut(dOff) : child;
1066
- }
1067
- /**
1068
- Get the node directly before the position, if any. If the
1069
- position points into a text node, only the part of that node
1070
- before the position is returned.
1071
- */
1072
- get nodeBefore() {
1073
- let index = this.index(this.depth);
1074
- let dOff = this.pos - this.path[this.path.length - 1];
1075
- if (dOff)
1076
- return this.parent.child(index).cut(0, dOff);
1077
- return index == 0 ? null : this.parent.child(index - 1);
1078
- }
1079
- /**
1080
- Get the position at the given index in the parent node at the
1081
- given depth (which defaults to `this.depth`).
1082
- */
1083
- posAtIndex(index, depth) {
1084
- depth = this.resolveDepth(depth);
1085
- let node = this.path[depth * 3], pos = depth == 0 ? 0 : this.path[depth * 3 - 1] + 1;
1086
- for (let i = 0; i < index; i++)
1087
- pos += node.child(i).nodeSize;
1088
- return pos;
1089
- }
1090
- /**
1091
- Get the marks at this position, factoring in the surrounding
1092
- marks' [`inclusive`](https://prosemirror.net/docs/ref/#model.MarkSpec.inclusive) property. If the
1093
- position is at the start of a non-empty node, the marks of the
1094
- node after it (if any) are returned.
1095
- */
1096
- marks() {
1097
- let parent = this.parent, index = this.index();
1098
- // In an empty parent, return the empty array
1099
- if (parent.content.size == 0)
1100
- return Mark.none;
1101
- // When inside a text node, just return the text node's marks
1102
- if (this.textOffset)
1103
- return parent.child(index).marks;
1104
- let main = parent.maybeChild(index - 1), other = parent.maybeChild(index);
1105
- // If the `after` flag is true of there is no node before, make
1106
- // the node after this position the main reference.
1107
- if (!main) {
1108
- let tmp = main;
1109
- main = other;
1110
- other = tmp;
1111
- }
1112
- // Use all marks in the main node, except those that have
1113
- // `inclusive` set to false and are not present in the other node.
1114
- let marks = main.marks;
1115
- for (var i = 0; i < marks.length; i++)
1116
- if (marks[i].type.spec.inclusive === false && (!other || !marks[i].isInSet(other.marks)))
1117
- marks = marks[i--].removeFromSet(marks);
1118
- return marks;
1119
- }
1120
- /**
1121
- Get the marks after the current position, if any, except those
1122
- that are non-inclusive and not present at position `$end`. This
1123
- is mostly useful for getting the set of marks to preserve after a
1124
- deletion. Will return `null` if this position is at the end of
1125
- its parent node or its parent node isn't a textblock (in which
1126
- case no marks should be preserved).
1127
- */
1128
- marksAcross($end) {
1129
- let after = this.parent.maybeChild(this.index());
1130
- if (!after || !after.isInline)
1131
- return null;
1132
- let marks = after.marks, next = $end.parent.maybeChild($end.index());
1133
- for (var i = 0; i < marks.length; i++)
1134
- if (marks[i].type.spec.inclusive === false && (!next || !marks[i].isInSet(next.marks)))
1135
- marks = marks[i--].removeFromSet(marks);
1136
- return marks;
1137
- }
1138
- /**
1139
- The depth up to which this position and the given (non-resolved)
1140
- position share the same parent nodes.
1141
- */
1142
- sharedDepth(pos) {
1143
- for (let depth = this.depth; depth > 0; depth--)
1144
- if (this.start(depth) <= pos && this.end(depth) >= pos)
1145
- return depth;
1146
- return 0;
1147
- }
1148
- /**
1149
- Returns a range based on the place where this position and the
1150
- given position diverge around block content. If both point into
1151
- the same textblock, for example, a range around that textblock
1152
- will be returned. If they point into different blocks, the range
1153
- around those blocks in their shared ancestor is returned. You can
1154
- pass in an optional predicate that will be called with a parent
1155
- node to see if a range into that parent is acceptable.
1156
- */
1157
- blockRange(other = this, pred) {
1158
- if (other.pos < this.pos)
1159
- return other.blockRange(this);
1160
- for (let d = this.depth - (this.parent.inlineContent || this.pos == other.pos ? 1 : 0); d >= 0; d--)
1161
- if (other.pos <= this.end(d) && (!pred || pred(this.node(d))))
1162
- return new NodeRange(this, other, d);
1163
- return null;
1164
- }
1165
- /**
1166
- Query whether the given position shares the same parent node.
1167
- */
1168
- sameParent(other) {
1169
- return this.pos - this.parentOffset == other.pos - other.parentOffset;
1170
- }
1171
- /**
1172
- Return the greater of this and the given position.
1173
- */
1174
- max(other) {
1175
- return other.pos > this.pos ? other : this;
1176
- }
1177
- /**
1178
- Return the smaller of this and the given position.
1179
- */
1180
- min(other) {
1181
- return other.pos < this.pos ? other : this;
1182
- }
1183
- /**
1184
- @internal
1185
- */
1186
- toString() {
1187
- let str = "";
1188
- for (let i = 1; i <= this.depth; i++)
1189
- str += (str ? "/" : "") + this.node(i).type.name + "_" + this.index(i - 1);
1190
- return str + ":" + this.parentOffset;
1191
- }
1192
- /**
1193
- @internal
1194
- */
1195
- static resolve(doc, pos) {
1196
- if (!(pos >= 0 && pos <= doc.content.size))
1197
- throw new RangeError("Position " + pos + " out of range");
1198
- let path = [];
1199
- let start = 0, parentOffset = pos;
1200
- for (let node = doc;;) {
1201
- let { index, offset } = node.content.findIndex(parentOffset);
1202
- let rem = parentOffset - offset;
1203
- path.push(node, index, start + offset);
1204
- if (!rem)
1205
- break;
1206
- node = node.child(index);
1207
- if (node.isText)
1208
- break;
1209
- parentOffset = rem - 1;
1210
- start += offset + 1;
1211
- }
1212
- return new ResolvedPos(pos, path, parentOffset);
1213
- }
1214
- /**
1215
- @internal
1216
- */
1217
- static resolveCached(doc, pos) {
1218
- let cache = resolveCache.get(doc);
1219
- if (cache) {
1220
- for (let i = 0; i < cache.elts.length; i++) {
1221
- let elt = cache.elts[i];
1222
- if (elt.pos == pos)
1223
- return elt;
1224
- }
1225
- }
1226
- else {
1227
- resolveCache.set(doc, cache = new ResolveCache);
1228
- }
1229
- let result = cache.elts[cache.i] = ResolvedPos.resolve(doc, pos);
1230
- cache.i = (cache.i + 1) % resolveCacheSize;
1231
- return result;
1232
- }
1233
- }
1234
- class ResolveCache {
1235
- constructor() {
1236
- this.elts = [];
1237
- this.i = 0;
1238
- }
1239
- }
1240
- const resolveCacheSize = 12, resolveCache = new WeakMap();
1241
- /**
1242
- Represents a flat range of content, i.e. one that starts and
1243
- ends in the same node.
1244
- */
1245
- class NodeRange {
1246
- /**
1247
- Construct a node range. `$from` and `$to` should point into the
1248
- same node until at least the given `depth`, since a node range
1249
- denotes an adjacent set of nodes in a single parent node.
1250
- */
1251
- constructor(
1252
- /**
1253
- A resolved position along the start of the content. May have a
1254
- `depth` greater than this object's `depth` property, since
1255
- these are the positions that were used to compute the range,
1256
- not re-resolved positions directly at its boundaries.
1257
- */
1258
- $from,
1259
- /**
1260
- A position along the end of the content. See
1261
- caveat for [`$from`](https://prosemirror.net/docs/ref/#model.NodeRange.$from).
1262
- */
1263
- $to,
1264
- /**
1265
- The depth of the node that this range points into.
1266
- */
1267
- depth) {
1268
- this.$from = $from;
1269
- this.$to = $to;
1270
- this.depth = depth;
1271
- }
1272
- /**
1273
- The position at the start of the range.
1274
- */
1275
- get start() { return this.$from.before(this.depth + 1); }
1276
- /**
1277
- The position at the end of the range.
1278
- */
1279
- get end() { return this.$to.after(this.depth + 1); }
1280
- /**
1281
- The parent node that the range points into.
1282
- */
1283
- get parent() { return this.$from.node(this.depth); }
1284
- /**
1285
- The start index of the range in the parent node.
1286
- */
1287
- get startIndex() { return this.$from.index(this.depth); }
1288
- /**
1289
- The end index of the range in the parent node.
1290
- */
1291
- get endIndex() { return this.$to.indexAfter(this.depth); }
1292
- }
1293
-
1294
- const emptyAttrs = Object.create(null);
1295
- /**
1296
- This class represents a node in the tree that makes up a
1297
- ProseMirror document. So a document is an instance of `Node`, with
1298
- children that are also instances of `Node`.
1299
-
1300
- Nodes are persistent data structures. Instead of changing them, you
1301
- create new ones with the content you want. Old ones keep pointing
1302
- at the old document shape. This is made cheaper by sharing
1303
- structure between the old and new data as much as possible, which a
1304
- tree shape like this (without back pointers) makes easy.
1305
-
1306
- **Do not** directly mutate the properties of a `Node` object. See
1307
- [the guide](https://prosemirror.net/docs/guide/#doc) for more information.
1308
- */
1309
- class Node {
1310
- /**
1311
- @internal
1312
- */
1313
- constructor(
1314
- /**
1315
- The type of node that this is.
1316
- */
1317
- type,
1318
- /**
1319
- An object mapping attribute names to values. The kind of
1320
- attributes allowed and required are
1321
- [determined](https://prosemirror.net/docs/ref/#model.NodeSpec.attrs) by the node type.
1322
- */
1323
- attrs,
1324
- // A fragment holding the node's children.
1325
- content,
1326
- /**
1327
- The marks (things like whether it is emphasized or part of a
1328
- link) applied to this node.
1329
- */
1330
- marks = Mark.none) {
1331
- this.type = type;
1332
- this.attrs = attrs;
1333
- this.marks = marks;
1334
- this.content = content || Fragment.empty;
1335
- }
1336
- /**
1337
- The array of this node's child nodes.
1338
- */
1339
- get children() { return this.content.content; }
1340
- /**
1341
- The size of this node, as defined by the integer-based [indexing
1342
- scheme](https://prosemirror.net/docs/guide/#doc.indexing). For text nodes, this is the
1343
- amount of characters. For other leaf nodes, it is one. For
1344
- non-leaf nodes, it is the size of the content plus two (the
1345
- start and end token).
1346
- */
1347
- get nodeSize() { return this.isLeaf ? 1 : 2 + this.content.size; }
1348
- /**
1349
- The number of children that the node has.
1350
- */
1351
- get childCount() { return this.content.childCount; }
1352
- /**
1353
- Get the child node at the given index. Raises an error when the
1354
- index is out of range.
1355
- */
1356
- child(index) { return this.content.child(index); }
1357
- /**
1358
- Get the child node at the given index, if it exists.
1359
- */
1360
- maybeChild(index) { return this.content.maybeChild(index); }
1361
- /**
1362
- Call `f` for every child node, passing the node, its offset
1363
- into this parent node, and its index.
1364
- */
1365
- forEach(f) { this.content.forEach(f); }
1366
- /**
1367
- Invoke a callback for all descendant nodes recursively between
1368
- the given two positions that are relative to start of this
1369
- node's content. The callback is invoked with the node, its
1370
- position relative to the original node (method receiver),
1371
- its parent node, and its child index. When the callback returns
1372
- false for a given node, that node's children will not be
1373
- recursed over. The last parameter can be used to specify a
1374
- starting position to count from.
1375
- */
1376
- nodesBetween(from, to, f, startPos = 0) {
1377
- this.content.nodesBetween(from, to, f, startPos, this);
1378
- }
1379
- /**
1380
- Call the given callback for every descendant node. Doesn't
1381
- descend into a node when the callback returns `false`.
1382
- */
1383
- descendants(f) {
1384
- this.nodesBetween(0, this.content.size, f);
1385
- }
1386
- /**
1387
- Concatenates all the text nodes found in this fragment and its
1388
- children.
1389
- */
1390
- get textContent() {
1391
- return (this.isLeaf && this.type.spec.leafText)
1392
- ? this.type.spec.leafText(this)
1393
- : this.textBetween(0, this.content.size, "");
1394
- }
1395
- /**
1396
- Get all text between positions `from` and `to`. When
1397
- `blockSeparator` is given, it will be inserted to separate text
1398
- from different block nodes. If `leafText` is given, it'll be
1399
- inserted for every non-text leaf node encountered, otherwise
1400
- [`leafText`](https://prosemirror.net/docs/ref/#model.NodeSpec.leafText) will be used.
1401
- */
1402
- textBetween(from, to, blockSeparator, leafText) {
1403
- return this.content.textBetween(from, to, blockSeparator, leafText);
1404
- }
1405
- /**
1406
- Returns this node's first child, or `null` if there are no
1407
- children.
1408
- */
1409
- get firstChild() { return this.content.firstChild; }
1410
- /**
1411
- Returns this node's last child, or `null` if there are no
1412
- children.
1413
- */
1414
- get lastChild() { return this.content.lastChild; }
1415
- /**
1416
- Test whether two nodes represent the same piece of document.
1417
- */
1418
- eq(other) {
1419
- return this == other || (this.sameMarkup(other) && this.content.eq(other.content));
1420
- }
1421
- /**
1422
- Compare the markup (type, attributes, and marks) of this node to
1423
- those of another. Returns `true` if both have the same markup.
1424
- */
1425
- sameMarkup(other) {
1426
- return this.hasMarkup(other.type, other.attrs, other.marks);
1427
- }
1428
- /**
1429
- Check whether this node's markup correspond to the given type,
1430
- attributes, and marks.
1431
- */
1432
- hasMarkup(type, attrs, marks) {
1433
- return this.type == type &&
1434
- compareDeep(this.attrs, attrs || type.defaultAttrs || emptyAttrs) &&
1435
- Mark.sameSet(this.marks, marks || Mark.none);
1436
- }
1437
- /**
1438
- Create a new node with the same markup as this node, containing
1439
- the given content (or empty, if no content is given).
1440
- */
1441
- copy(content = null) {
1442
- if (content == this.content)
1443
- return this;
1444
- return new Node(this.type, this.attrs, content, this.marks);
1445
- }
1446
- /**
1447
- Create a copy of this node, with the given set of marks instead
1448
- of the node's own marks.
1449
- */
1450
- mark(marks) {
1451
- return marks == this.marks ? this : new Node(this.type, this.attrs, this.content, marks);
1452
- }
1453
- /**
1454
- Create a copy of this node with only the content between the
1455
- given positions. If `to` is not given, it defaults to the end of
1456
- the node.
1457
- */
1458
- cut(from, to = this.content.size) {
1459
- if (from == 0 && to == this.content.size)
1460
- return this;
1461
- return this.copy(this.content.cut(from, to));
1462
- }
1463
- /**
1464
- Cut out the part of the document between the given positions, and
1465
- return it as a `Slice` object.
1466
- */
1467
- slice(from, to = this.content.size, includeParents = false) {
1468
- if (from == to)
1469
- return Slice.empty;
1470
- let $from = this.resolve(from), $to = this.resolve(to);
1471
- let depth = includeParents ? 0 : $from.sharedDepth(to);
1472
- let start = $from.start(depth), node = $from.node(depth);
1473
- let content = node.content.cut($from.pos - start, $to.pos - start);
1474
- return new Slice(content, $from.depth - depth, $to.depth - depth);
1475
- }
1476
- /**
1477
- Replace the part of the document between the given positions with
1478
- the given slice. The slice must 'fit', meaning its open sides
1479
- must be able to connect to the surrounding content, and its
1480
- content nodes must be valid children for the node they are placed
1481
- into. If any of this is violated, an error of type
1482
- [`ReplaceError`](https://prosemirror.net/docs/ref/#model.ReplaceError) is thrown.
1483
- */
1484
- replace(from, to, slice) {
1485
- return replace(this.resolve(from), this.resolve(to), slice);
1486
- }
1487
- /**
1488
- Find the node directly after the given position.
1489
- */
1490
- nodeAt(pos) {
1491
- for (let node = this;;) {
1492
- let { index, offset } = node.content.findIndex(pos);
1493
- node = node.maybeChild(index);
1494
- if (!node)
1495
- return null;
1496
- if (offset == pos || node.isText)
1497
- return node;
1498
- pos -= offset + 1;
1499
- }
1500
- }
1501
- /**
1502
- Find the (direct) child node after the given offset, if any,
1503
- and return it along with its index and offset relative to this
1504
- node.
1505
- */
1506
- childAfter(pos) {
1507
- let { index, offset } = this.content.findIndex(pos);
1508
- return { node: this.content.maybeChild(index), index, offset };
1509
- }
1510
- /**
1511
- Find the (direct) child node before the given offset, if any,
1512
- and return it along with its index and offset relative to this
1513
- node.
1514
- */
1515
- childBefore(pos) {
1516
- if (pos == 0)
1517
- return { node: null, index: 0, offset: 0 };
1518
- let { index, offset } = this.content.findIndex(pos);
1519
- if (offset < pos)
1520
- return { node: this.content.child(index), index, offset };
1521
- let node = this.content.child(index - 1);
1522
- return { node, index: index - 1, offset: offset - node.nodeSize };
1523
- }
1524
- /**
1525
- Resolve the given position in the document, returning an
1526
- [object](https://prosemirror.net/docs/ref/#model.ResolvedPos) with information about its context.
1527
- */
1528
- resolve(pos) { return ResolvedPos.resolveCached(this, pos); }
1529
- /**
1530
- @internal
1531
- */
1532
- resolveNoCache(pos) { return ResolvedPos.resolve(this, pos); }
1533
- /**
1534
- Test whether a given mark or mark type occurs in this document
1535
- between the two given positions.
1536
- */
1537
- rangeHasMark(from, to, type) {
1538
- let found = false;
1539
- if (to > from)
1540
- this.nodesBetween(from, to, node => {
1541
- if (type.isInSet(node.marks))
1542
- found = true;
1543
- return !found;
1544
- });
1545
- return found;
1546
- }
1547
- /**
1548
- True when this is a block (non-inline node)
1549
- */
1550
- get isBlock() { return this.type.isBlock; }
1551
- /**
1552
- True when this is a textblock node, a block node with inline
1553
- content.
1554
- */
1555
- get isTextblock() { return this.type.isTextblock; }
1556
- /**
1557
- True when this node allows inline content.
1558
- */
1559
- get inlineContent() { return this.type.inlineContent; }
1560
- /**
1561
- True when this is an inline node (a text node or a node that can
1562
- appear among text).
1563
- */
1564
- get isInline() { return this.type.isInline; }
1565
- /**
1566
- True when this is a text node.
1567
- */
1568
- get isText() { return this.type.isText; }
1569
- /**
1570
- True when this is a leaf node.
1571
- */
1572
- get isLeaf() { return this.type.isLeaf; }
1573
- /**
1574
- True when this is an atom, i.e. when it does not have directly
1575
- editable content. This is usually the same as `isLeaf`, but can
1576
- be configured with the [`atom` property](https://prosemirror.net/docs/ref/#model.NodeSpec.atom)
1577
- on a node's spec (typically used when the node is displayed as
1578
- an uneditable [node view](https://prosemirror.net/docs/ref/#view.NodeView)).
1579
- */
1580
- get isAtom() { return this.type.isAtom; }
1581
- /**
1582
- Return a string representation of this node for debugging
1583
- purposes.
1584
- */
1585
- toString() {
1586
- if (this.type.spec.toDebugString)
1587
- return this.type.spec.toDebugString(this);
1588
- let name = this.type.name;
1589
- if (this.content.size)
1590
- name += "(" + this.content.toStringInner() + ")";
1591
- return wrapMarks(this.marks, name);
1592
- }
1593
- /**
1594
- Get the content match in this node at the given index.
1595
- */
1596
- contentMatchAt(index) {
1597
- let match = this.type.contentMatch.matchFragment(this.content, 0, index);
1598
- if (!match)
1599
- throw new Error("Called contentMatchAt on a node with invalid content");
1600
- return match;
1601
- }
1602
- /**
1603
- Test whether replacing the range between `from` and `to` (by
1604
- child index) with the given replacement fragment (which defaults
1605
- to the empty fragment) would leave the node's content valid. You
1606
- can optionally pass `start` and `end` indices into the
1607
- replacement fragment.
1608
- */
1609
- canReplace(from, to, replacement = Fragment.empty, start = 0, end = replacement.childCount) {
1610
- let one = this.contentMatchAt(from).matchFragment(replacement, start, end);
1611
- let two = one && one.matchFragment(this.content, to);
1612
- if (!two || !two.validEnd)
1613
- return false;
1614
- for (let i = start; i < end; i++)
1615
- if (!this.type.allowsMarks(replacement.child(i).marks))
1616
- return false;
1617
- return true;
1618
- }
1619
- /**
1620
- Test whether replacing the range `from` to `to` (by index) with
1621
- a node of the given type would leave the node's content valid.
1622
- */
1623
- canReplaceWith(from, to, type, marks) {
1624
- if (marks && !this.type.allowsMarks(marks))
1625
- return false;
1626
- let start = this.contentMatchAt(from).matchType(type);
1627
- let end = start && start.matchFragment(this.content, to);
1628
- return end ? end.validEnd : false;
1629
- }
1630
- /**
1631
- Test whether the given node's content could be appended to this
1632
- node. If that node is empty, this will only return true if there
1633
- is at least one node type that can appear in both nodes (to avoid
1634
- merging completely incompatible nodes).
1635
- */
1636
- canAppend(other) {
1637
- if (other.content.size)
1638
- return this.canReplace(this.childCount, this.childCount, other.content);
1639
- else
1640
- return this.type.compatibleContent(other.type);
1641
- }
1642
- /**
1643
- Check whether this node and its descendants conform to the
1644
- schema, and raise an exception when they do not.
1645
- */
1646
- check() {
1647
- this.type.checkContent(this.content);
1648
- this.type.checkAttrs(this.attrs);
1649
- let copy = Mark.none;
1650
- for (let i = 0; i < this.marks.length; i++) {
1651
- let mark = this.marks[i];
1652
- mark.type.checkAttrs(mark.attrs);
1653
- copy = mark.addToSet(copy);
1654
- }
1655
- if (!Mark.sameSet(copy, this.marks))
1656
- throw new RangeError(`Invalid collection of marks for node ${this.type.name}: ${this.marks.map(m => m.type.name)}`);
1657
- this.content.forEach(node => node.check());
1658
- }
1659
- /**
1660
- Return a JSON-serializeable representation of this node.
1661
- */
1662
- toJSON() {
1663
- let obj = { type: this.type.name };
1664
- for (let _ in this.attrs) {
1665
- obj.attrs = this.attrs;
1666
- break;
1667
- }
1668
- if (this.content.size)
1669
- obj.content = this.content.toJSON();
1670
- if (this.marks.length)
1671
- obj.marks = this.marks.map(n => n.toJSON());
1672
- return obj;
1673
- }
1674
- /**
1675
- Deserialize a node from its JSON representation.
1676
- */
1677
- static fromJSON(schema, json) {
1678
- if (!json)
1679
- throw new RangeError("Invalid input for Node.fromJSON");
1680
- let marks = undefined;
1681
- if (json.marks) {
1682
- if (!Array.isArray(json.marks))
1683
- throw new RangeError("Invalid mark data for Node.fromJSON");
1684
- marks = json.marks.map(schema.markFromJSON);
1685
- }
1686
- if (json.type == "text") {
1687
- if (typeof json.text != "string")
1688
- throw new RangeError("Invalid text node in JSON");
1689
- return schema.text(json.text, marks);
1690
- }
1691
- let content = Fragment.fromJSON(schema, json.content);
1692
- let node = schema.nodeType(json.type).create(json.attrs, content, marks);
1693
- node.type.checkAttrs(node.attrs);
1694
- return node;
1695
- }
1696
- }
1697
- Node.prototype.text = undefined;
1698
- class TextNode extends Node {
1699
- /**
1700
- @internal
1701
- */
1702
- constructor(type, attrs, content, marks) {
1703
- super(type, attrs, null, marks);
1704
- if (!content)
1705
- throw new RangeError("Empty text nodes are not allowed");
1706
- this.text = content;
1707
- }
1708
- toString() {
1709
- if (this.type.spec.toDebugString)
1710
- return this.type.spec.toDebugString(this);
1711
- return wrapMarks(this.marks, JSON.stringify(this.text));
1712
- }
1713
- get textContent() { return this.text; }
1714
- textBetween(from, to) { return this.text.slice(from, to); }
1715
- get nodeSize() { return this.text.length; }
1716
- mark(marks) {
1717
- return marks == this.marks ? this : new TextNode(this.type, this.attrs, this.text, marks);
1718
- }
1719
- withText(text) {
1720
- if (text == this.text)
1721
- return this;
1722
- return new TextNode(this.type, this.attrs, text, this.marks);
1723
- }
1724
- cut(from = 0, to = this.text.length) {
1725
- if (from == 0 && to == this.text.length)
1726
- return this;
1727
- return this.withText(this.text.slice(from, to));
1728
- }
1729
- eq(other) {
1730
- return this.sameMarkup(other) && this.text == other.text;
1731
- }
1732
- toJSON() {
1733
- let base = super.toJSON();
1734
- base.text = this.text;
1735
- return base;
1736
- }
1737
- }
1738
- function wrapMarks(marks, str) {
1739
- for (let i = marks.length - 1; i >= 0; i--)
1740
- str = marks[i].type.name + "(" + str + ")";
1741
- return str;
1742
- }
1743
-
1744
- /**
1745
- Instances of this class represent a match state of a node type's
1746
- [content expression](https://prosemirror.net/docs/ref/#model.NodeSpec.content), and can be used to
1747
- find out whether further content matches here, and whether a given
1748
- position is a valid end of the node.
1749
- */
1750
- class ContentMatch {
1751
- /**
1752
- @internal
1753
- */
1754
- constructor(
1755
- /**
1756
- True when this match state represents a valid end of the node.
1757
- */
1758
- validEnd) {
1759
- this.validEnd = validEnd;
1760
- /**
1761
- @internal
1762
- */
1763
- this.next = [];
1764
- /**
1765
- @internal
1766
- */
1767
- this.wrapCache = [];
1768
- }
1769
- /**
1770
- @internal
1771
- */
1772
- static parse(string, nodeTypes) {
1773
- let stream = new TokenStream(string, nodeTypes);
1774
- if (stream.next == null)
1775
- return ContentMatch.empty;
1776
- let expr = parseExpr(stream);
1777
- if (stream.next)
1778
- stream.err("Unexpected trailing text");
1779
- let match = dfa(nfa(expr));
1780
- checkForDeadEnds(match, stream);
1781
- return match;
1782
- }
1783
- /**
1784
- Match a node type, returning a match after that node if
1785
- successful.
1786
- */
1787
- matchType(type) {
1788
- for (let i = 0; i < this.next.length; i++)
1789
- if (this.next[i].type == type)
1790
- return this.next[i].next;
1791
- return null;
1792
- }
1793
- /**
1794
- Try to match a fragment. Returns the resulting match when
1795
- successful.
1796
- */
1797
- matchFragment(frag, start = 0, end = frag.childCount) {
1798
- let cur = this;
1799
- for (let i = start; cur && i < end; i++)
1800
- cur = cur.matchType(frag.child(i).type);
1801
- return cur;
1802
- }
1803
- /**
1804
- @internal
1805
- */
1806
- get inlineContent() {
1807
- return this.next.length != 0 && this.next[0].type.isInline;
1808
- }
1809
- /**
1810
- Get the first matching node type at this match position that can
1811
- be generated.
1812
- */
1813
- get defaultType() {
1814
- for (let i = 0; i < this.next.length; i++) {
1815
- let { type } = this.next[i];
1816
- if (!(type.isText || type.hasRequiredAttrs()))
1817
- return type;
1818
- }
1819
- return null;
1820
- }
1821
- /**
1822
- @internal
1823
- */
1824
- compatible(other) {
1825
- for (let i = 0; i < this.next.length; i++)
1826
- for (let j = 0; j < other.next.length; j++)
1827
- if (this.next[i].type == other.next[j].type)
1828
- return true;
1829
- return false;
1830
- }
1831
- /**
1832
- Try to match the given fragment, and if that fails, see if it can
1833
- be made to match by inserting nodes in front of it. When
1834
- successful, return a fragment of inserted nodes (which may be
1835
- empty if nothing had to be inserted). When `toEnd` is true, only
1836
- return a fragment if the resulting match goes to the end of the
1837
- content expression.
1838
- */
1839
- fillBefore(after, toEnd = false, startIndex = 0) {
1840
- let seen = [this];
1841
- function search(match, types) {
1842
- let finished = match.matchFragment(after, startIndex);
1843
- if (finished && (!toEnd || finished.validEnd))
1844
- return Fragment.from(types.map(tp => tp.createAndFill()));
1845
- for (let i = 0; i < match.next.length; i++) {
1846
- let { type, next } = match.next[i];
1847
- if (!(type.isText || type.hasRequiredAttrs()) && seen.indexOf(next) == -1) {
1848
- seen.push(next);
1849
- let found = search(next, types.concat(type));
1850
- if (found)
1851
- return found;
1852
- }
1853
- }
1854
- return null;
1855
- }
1856
- return search(this, []);
1857
- }
1858
- /**
1859
- Find a set of wrapping node types that would allow a node of the
1860
- given type to appear at this position. The result may be empty
1861
- (when it fits directly) and will be null when no such wrapping
1862
- exists.
1863
- */
1864
- findWrapping(target) {
1865
- for (let i = 0; i < this.wrapCache.length; i += 2)
1866
- if (this.wrapCache[i] == target)
1867
- return this.wrapCache[i + 1];
1868
- let computed = this.computeWrapping(target);
1869
- this.wrapCache.push(target, computed);
1870
- return computed;
1871
- }
1872
- /**
1873
- @internal
1874
- */
1875
- computeWrapping(target) {
1876
- let seen = Object.create(null), active = [{ match: this, type: null, via: null }];
1877
- while (active.length) {
1878
- let current = active.shift(), match = current.match;
1879
- if (match.matchType(target)) {
1880
- let result = [];
1881
- for (let obj = current; obj.type; obj = obj.via)
1882
- result.push(obj.type);
1883
- return result.reverse();
1884
- }
1885
- for (let i = 0; i < match.next.length; i++) {
1886
- let { type, next } = match.next[i];
1887
- if (!type.isLeaf && !type.hasRequiredAttrs() && !(type.name in seen) && (!current.type || next.validEnd)) {
1888
- active.push({ match: type.contentMatch, type, via: current });
1889
- seen[type.name] = true;
1890
- }
1891
- }
1892
- }
1893
- return null;
1894
- }
1895
- /**
1896
- The number of outgoing edges this node has in the finite
1897
- automaton that describes the content expression.
1898
- */
1899
- get edgeCount() {
1900
- return this.next.length;
1901
- }
1902
- /**
1903
- Get the _n_​th outgoing edge from this node in the finite
1904
- automaton that describes the content expression.
1905
- */
1906
- edge(n) {
1907
- if (n >= this.next.length)
1908
- throw new RangeError(`There's no ${n}th edge in this content match`);
1909
- return this.next[n];
1910
- }
1911
- /**
1912
- @internal
1913
- */
1914
- toString() {
1915
- let seen = [];
1916
- function scan(m) {
1917
- seen.push(m);
1918
- for (let i = 0; i < m.next.length; i++)
1919
- if (seen.indexOf(m.next[i].next) == -1)
1920
- scan(m.next[i].next);
1921
- }
1922
- scan(this);
1923
- return seen.map((m, i) => {
1924
- let out = i + (m.validEnd ? "*" : " ") + " ";
1925
- for (let i = 0; i < m.next.length; i++)
1926
- out += (i ? ", " : "") + m.next[i].type.name + "->" + seen.indexOf(m.next[i].next);
1927
- return out;
1928
- }).join("\n");
1929
- }
1930
- }
1931
- /**
1932
- @internal
1933
- */
1934
- ContentMatch.empty = new ContentMatch(true);
1935
- class TokenStream {
1936
- constructor(string, nodeTypes) {
1937
- this.string = string;
1938
- this.nodeTypes = nodeTypes;
1939
- this.inline = null;
1940
- this.pos = 0;
1941
- this.tokens = string.split(/\s*(?=\b|\W|$)/);
1942
- if (this.tokens[this.tokens.length - 1] == "")
1943
- this.tokens.pop();
1944
- if (this.tokens[0] == "")
1945
- this.tokens.shift();
1946
- }
1947
- get next() { return this.tokens[this.pos]; }
1948
- eat(tok) { return this.next == tok && (this.pos++ || true); }
1949
- err(str) { throw new SyntaxError(str + " (in content expression '" + this.string + "')"); }
1950
- }
1951
- function parseExpr(stream) {
1952
- let exprs = [];
1953
- do {
1954
- exprs.push(parseExprSeq(stream));
1955
- } while (stream.eat("|"));
1956
- return exprs.length == 1 ? exprs[0] : { type: "choice", exprs };
1957
- }
1958
- function parseExprSeq(stream) {
1959
- let exprs = [];
1960
- do {
1961
- exprs.push(parseExprSubscript(stream));
1962
- } while (stream.next && stream.next != ")" && stream.next != "|");
1963
- return exprs.length == 1 ? exprs[0] : { type: "seq", exprs };
1964
- }
1965
- function parseExprSubscript(stream) {
1966
- let expr = parseExprAtom(stream);
1967
- for (;;) {
1968
- if (stream.eat("+"))
1969
- expr = { type: "plus", expr };
1970
- else if (stream.eat("*"))
1971
- expr = { type: "star", expr };
1972
- else if (stream.eat("?"))
1973
- expr = { type: "opt", expr };
1974
- else if (stream.eat("{"))
1975
- expr = parseExprRange(stream, expr);
1976
- else
1977
- break;
1978
- }
1979
- return expr;
1980
- }
1981
- function parseNum(stream) {
1982
- if (/\D/.test(stream.next))
1983
- stream.err("Expected number, got '" + stream.next + "'");
1984
- let result = Number(stream.next);
1985
- stream.pos++;
1986
- return result;
1987
- }
1988
- function parseExprRange(stream, expr) {
1989
- let min = parseNum(stream), max = min;
1990
- if (stream.eat(",")) {
1991
- if (stream.next != "}")
1992
- max = parseNum(stream);
1993
- else
1994
- max = -1;
1995
- }
1996
- if (!stream.eat("}"))
1997
- stream.err("Unclosed braced range");
1998
- return { type: "range", min, max, expr };
1999
- }
2000
- function resolveName(stream, name) {
2001
- let types = stream.nodeTypes, type = types[name];
2002
- if (type)
2003
- return [type];
2004
- let result = [];
2005
- for (let typeName in types) {
2006
- let type = types[typeName];
2007
- if (type.isInGroup(name))
2008
- result.push(type);
2009
- }
2010
- if (result.length == 0)
2011
- stream.err("No node type or group '" + name + "' found");
2012
- return result;
2013
- }
2014
- function parseExprAtom(stream) {
2015
- if (stream.eat("(")) {
2016
- let expr = parseExpr(stream);
2017
- if (!stream.eat(")"))
2018
- stream.err("Missing closing paren");
2019
- return expr;
2020
- }
2021
- else if (!/\W/.test(stream.next)) {
2022
- let exprs = resolveName(stream, stream.next).map(type => {
2023
- if (stream.inline == null)
2024
- stream.inline = type.isInline;
2025
- else if (stream.inline != type.isInline)
2026
- stream.err("Mixing inline and block content");
2027
- return { type: "name", value: type };
2028
- });
2029
- stream.pos++;
2030
- return exprs.length == 1 ? exprs[0] : { type: "choice", exprs };
2031
- }
2032
- else {
2033
- stream.err("Unexpected token '" + stream.next + "'");
2034
- }
2035
- }
2036
- // Construct an NFA from an expression as returned by the parser. The
2037
- // NFA is represented as an array of states, which are themselves
2038
- // arrays of edges, which are `{term, to}` objects. The first state is
2039
- // the entry state and the last node is the success state.
2040
- //
2041
- // Note that unlike typical NFAs, the edge ordering in this one is
2042
- // significant, in that it is used to contruct filler content when
2043
- // necessary.
2044
- function nfa(expr) {
2045
- let nfa = [[]];
2046
- connect(compile(expr, 0), node());
2047
- return nfa;
2048
- function node() { return nfa.push([]) - 1; }
2049
- function edge(from, to, term) {
2050
- let edge = { term, to };
2051
- nfa[from].push(edge);
2052
- return edge;
2053
- }
2054
- function connect(edges, to) {
2055
- edges.forEach(edge => edge.to = to);
2056
- }
2057
- function compile(expr, from) {
2058
- if (expr.type == "choice") {
2059
- return expr.exprs.reduce((out, expr) => out.concat(compile(expr, from)), []);
2060
- }
2061
- else if (expr.type == "seq") {
2062
- for (let i = 0;; i++) {
2063
- let next = compile(expr.exprs[i], from);
2064
- if (i == expr.exprs.length - 1)
2065
- return next;
2066
- connect(next, from = node());
2067
- }
2068
- }
2069
- else if (expr.type == "star") {
2070
- let loop = node();
2071
- edge(from, loop);
2072
- connect(compile(expr.expr, loop), loop);
2073
- return [edge(loop)];
2074
- }
2075
- else if (expr.type == "plus") {
2076
- let loop = node();
2077
- connect(compile(expr.expr, from), loop);
2078
- connect(compile(expr.expr, loop), loop);
2079
- return [edge(loop)];
2080
- }
2081
- else if (expr.type == "opt") {
2082
- return [edge(from)].concat(compile(expr.expr, from));
2083
- }
2084
- else if (expr.type == "range") {
2085
- let cur = from;
2086
- for (let i = 0; i < expr.min; i++) {
2087
- let next = node();
2088
- connect(compile(expr.expr, cur), next);
2089
- cur = next;
2090
- }
2091
- if (expr.max == -1) {
2092
- connect(compile(expr.expr, cur), cur);
2093
- }
2094
- else {
2095
- for (let i = expr.min; i < expr.max; i++) {
2096
- let next = node();
2097
- edge(cur, next);
2098
- connect(compile(expr.expr, cur), next);
2099
- cur = next;
2100
- }
2101
- }
2102
- return [edge(cur)];
2103
- }
2104
- else if (expr.type == "name") {
2105
- return [edge(from, undefined, expr.value)];
2106
- }
2107
- else {
2108
- throw new Error("Unknown expr type");
2109
- }
2110
- }
2111
- }
2112
- function cmp(a, b) { return b - a; }
2113
- // Get the set of nodes reachable by null edges from `node`. Omit
2114
- // nodes with only a single null-out-edge, since they may lead to
2115
- // needless duplicated nodes.
2116
- function nullFrom(nfa, node) {
2117
- let result = [];
2118
- scan(node);
2119
- return result.sort(cmp);
2120
- function scan(node) {
2121
- let edges = nfa[node];
2122
- if (edges.length == 1 && !edges[0].term)
2123
- return scan(edges[0].to);
2124
- result.push(node);
2125
- for (let i = 0; i < edges.length; i++) {
2126
- let { term, to } = edges[i];
2127
- if (!term && result.indexOf(to) == -1)
2128
- scan(to);
2129
- }
2130
- }
2131
- }
2132
- // Compiles an NFA as produced by `nfa` into a DFA, modeled as a set
2133
- // of state objects (`ContentMatch` instances) with transitions
2134
- // between them.
2135
- function dfa(nfa) {
2136
- let labeled = Object.create(null);
2137
- return explore(nullFrom(nfa, 0));
2138
- function explore(states) {
2139
- let out = [];
2140
- states.forEach(node => {
2141
- nfa[node].forEach(({ term, to }) => {
2142
- if (!term)
2143
- return;
2144
- let set;
2145
- for (let i = 0; i < out.length; i++)
2146
- if (out[i][0] == term)
2147
- set = out[i][1];
2148
- nullFrom(nfa, to).forEach(node => {
2149
- if (!set)
2150
- out.push([term, set = []]);
2151
- if (set.indexOf(node) == -1)
2152
- set.push(node);
2153
- });
2154
- });
2155
- });
2156
- let state = labeled[states.join(",")] = new ContentMatch(states.indexOf(nfa.length - 1) > -1);
2157
- for (let i = 0; i < out.length; i++) {
2158
- let states = out[i][1].sort(cmp);
2159
- state.next.push({ type: out[i][0], next: labeled[states.join(",")] || explore(states) });
2160
- }
2161
- return state;
2162
- }
2163
- }
2164
- function checkForDeadEnds(match, stream) {
2165
- for (let i = 0, work = [match]; i < work.length; i++) {
2166
- let state = work[i], dead = !state.validEnd, nodes = [];
2167
- for (let j = 0; j < state.next.length; j++) {
2168
- let { type, next } = state.next[j];
2169
- nodes.push(type.name);
2170
- if (dead && !(type.isText || type.hasRequiredAttrs()))
2171
- dead = false;
2172
- if (work.indexOf(next) == -1)
2173
- work.push(next);
2174
- }
2175
- if (dead)
2176
- stream.err("Only non-generatable nodes (" + nodes.join(", ") + ") in a required position (see https://prosemirror.net/docs/guide/#generatable)");
2177
- }
2178
- }
2179
-
2180
- // For node types where all attrs have a default value (or which don't
2181
- // have any attributes), build up a single reusable default attribute
2182
- // object, and use it for all nodes that don't specify specific
2183
- // attributes.
2184
- function defaultAttrs(attrs) {
2185
- let defaults = Object.create(null);
2186
- for (let attrName in attrs) {
2187
- let attr = attrs[attrName];
2188
- if (!attr.hasDefault)
2189
- return null;
2190
- defaults[attrName] = attr.default;
2191
- }
2192
- return defaults;
2193
- }
2194
- function computeAttrs(attrs, value) {
2195
- let built = Object.create(null);
2196
- for (let name in attrs) {
2197
- let given = value && value[name];
2198
- if (given === undefined) {
2199
- let attr = attrs[name];
2200
- if (attr.hasDefault)
2201
- given = attr.default;
2202
- else
2203
- throw new RangeError("No value supplied for attribute " + name);
2204
- }
2205
- built[name] = given;
2206
- }
2207
- return built;
2208
- }
2209
- function checkAttrs(attrs, values, type, name) {
2210
- for (let name in values)
2211
- if (!(name in attrs))
2212
- throw new RangeError(`Unsupported attribute ${name} for ${type} of type ${name}`);
2213
- for (let name in attrs) {
2214
- let attr = attrs[name];
2215
- if (attr.validate)
2216
- attr.validate(values[name]);
2217
- }
2218
- }
2219
- function initAttrs(typeName, attrs) {
2220
- let result = Object.create(null);
2221
- if (attrs)
2222
- for (let name in attrs)
2223
- result[name] = new Attribute(typeName, name, attrs[name]);
2224
- return result;
2225
- }
2226
- /**
2227
- Node types are objects allocated once per `Schema` and used to
2228
- [tag](https://prosemirror.net/docs/ref/#model.Node.type) `Node` instances. They contain information
2229
- about the node type, such as its name and what kind of node it
2230
- represents.
2231
- */
2232
- let NodeType$1 = class NodeType {
2233
- /**
2234
- @internal
2235
- */
2236
- constructor(
2237
- /**
2238
- The name the node type has in this schema.
2239
- */
2240
- name,
2241
- /**
2242
- A link back to the `Schema` the node type belongs to.
2243
- */
2244
- schema,
2245
- /**
2246
- The spec that this type is based on
2247
- */
2248
- spec) {
2249
- this.name = name;
2250
- this.schema = schema;
2251
- this.spec = spec;
2252
- /**
2253
- The set of marks allowed in this node. `null` means all marks
2254
- are allowed.
2255
- */
2256
- this.markSet = null;
2257
- this.groups = spec.group ? spec.group.split(" ") : [];
2258
- this.attrs = initAttrs(name, spec.attrs);
2259
- this.defaultAttrs = defaultAttrs(this.attrs);
2260
- this.contentMatch = null;
2261
- this.inlineContent = null;
2262
- this.isBlock = !(spec.inline || name == "text");
2263
- this.isText = name == "text";
2264
- }
2265
- /**
2266
- True if this is an inline type.
2267
- */
2268
- get isInline() { return !this.isBlock; }
2269
- /**
2270
- True if this is a textblock type, a block that contains inline
2271
- content.
2272
- */
2273
- get isTextblock() { return this.isBlock && this.inlineContent; }
2274
- /**
2275
- True for node types that allow no content.
2276
- */
2277
- get isLeaf() { return this.contentMatch == ContentMatch.empty; }
2278
- /**
2279
- True when this node is an atom, i.e. when it does not have
2280
- directly editable content.
2281
- */
2282
- get isAtom() { return this.isLeaf || !!this.spec.atom; }
2283
- /**
2284
- Return true when this node type is part of the given
2285
- [group](https://prosemirror.net/docs/ref/#model.NodeSpec.group).
2286
- */
2287
- isInGroup(group) {
2288
- return this.groups.indexOf(group) > -1;
2289
- }
2290
- /**
2291
- The node type's [whitespace](https://prosemirror.net/docs/ref/#model.NodeSpec.whitespace) option.
2292
- */
2293
- get whitespace() {
2294
- return this.spec.whitespace || (this.spec.code ? "pre" : "normal");
2295
- }
2296
- /**
2297
- Tells you whether this node type has any required attributes.
2298
- */
2299
- hasRequiredAttrs() {
2300
- for (let n in this.attrs)
2301
- if (this.attrs[n].isRequired)
2302
- return true;
2303
- return false;
2304
- }
2305
- /**
2306
- Indicates whether this node allows some of the same content as
2307
- the given node type.
2308
- */
2309
- compatibleContent(other) {
2310
- return this == other || this.contentMatch.compatible(other.contentMatch);
2311
- }
2312
- /**
2313
- @internal
2314
- */
2315
- computeAttrs(attrs) {
2316
- if (!attrs && this.defaultAttrs)
2317
- return this.defaultAttrs;
2318
- else
2319
- return computeAttrs(this.attrs, attrs);
2320
- }
2321
- /**
2322
- Create a `Node` of this type. The given attributes are
2323
- checked and defaulted (you can pass `null` to use the type's
2324
- defaults entirely, if no required attributes exist). `content`
2325
- may be a `Fragment`, a node, an array of nodes, or
2326
- `null`. Similarly `marks` may be `null` to default to the empty
2327
- set of marks.
2328
- */
2329
- create(attrs = null, content, marks) {
2330
- if (this.isText)
2331
- throw new Error("NodeType.create can't construct text nodes");
2332
- return new Node(this, this.computeAttrs(attrs), Fragment.from(content), Mark.setFrom(marks));
2333
- }
2334
- /**
2335
- Like [`create`](https://prosemirror.net/docs/ref/#model.NodeType.create), but check the given content
2336
- against the node type's content restrictions, and throw an error
2337
- if it doesn't match.
2338
- */
2339
- createChecked(attrs = null, content, marks) {
2340
- content = Fragment.from(content);
2341
- this.checkContent(content);
2342
- return new Node(this, this.computeAttrs(attrs), content, Mark.setFrom(marks));
2343
- }
2344
- /**
2345
- Like [`create`](https://prosemirror.net/docs/ref/#model.NodeType.create), but see if it is
2346
- necessary to add nodes to the start or end of the given fragment
2347
- to make it fit the node. If no fitting wrapping can be found,
2348
- return null. Note that, due to the fact that required nodes can
2349
- always be created, this will always succeed if you pass null or
2350
- `Fragment.empty` as content.
2351
- */
2352
- createAndFill(attrs = null, content, marks) {
2353
- attrs = this.computeAttrs(attrs);
2354
- content = Fragment.from(content);
2355
- if (content.size) {
2356
- let before = this.contentMatch.fillBefore(content);
2357
- if (!before)
2358
- return null;
2359
- content = before.append(content);
2360
- }
2361
- let matched = this.contentMatch.matchFragment(content);
2362
- let after = matched && matched.fillBefore(Fragment.empty, true);
2363
- if (!after)
2364
- return null;
2365
- return new Node(this, attrs, content.append(after), Mark.setFrom(marks));
2366
- }
2367
- /**
2368
- Returns true if the given fragment is valid content for this node
2369
- type.
2370
- */
2371
- validContent(content) {
2372
- let result = this.contentMatch.matchFragment(content);
2373
- if (!result || !result.validEnd)
2374
- return false;
2375
- for (let i = 0; i < content.childCount; i++)
2376
- if (!this.allowsMarks(content.child(i).marks))
2377
- return false;
2378
- return true;
2379
- }
2380
- /**
2381
- Throws a RangeError if the given fragment is not valid content for this
2382
- node type.
2383
- @internal
2384
- */
2385
- checkContent(content) {
2386
- if (!this.validContent(content))
2387
- throw new RangeError(`Invalid content for node ${this.name}: ${content.toString().slice(0, 50)}`);
2388
- }
2389
- /**
2390
- @internal
2391
- */
2392
- checkAttrs(attrs) {
2393
- checkAttrs(this.attrs, attrs, "node", this.name);
2394
- }
2395
- /**
2396
- Check whether the given mark type is allowed in this node.
2397
- */
2398
- allowsMarkType(markType) {
2399
- return this.markSet == null || this.markSet.indexOf(markType) > -1;
2400
- }
2401
- /**
2402
- Test whether the given set of marks are allowed in this node.
2403
- */
2404
- allowsMarks(marks) {
2405
- if (this.markSet == null)
2406
- return true;
2407
- for (let i = 0; i < marks.length; i++)
2408
- if (!this.allowsMarkType(marks[i].type))
2409
- return false;
2410
- return true;
2411
- }
2412
- /**
2413
- Removes the marks that are not allowed in this node from the given set.
2414
- */
2415
- allowedMarks(marks) {
2416
- if (this.markSet == null)
2417
- return marks;
2418
- let copy;
2419
- for (let i = 0; i < marks.length; i++) {
2420
- if (!this.allowsMarkType(marks[i].type)) {
2421
- if (!copy)
2422
- copy = marks.slice(0, i);
2423
- }
2424
- else if (copy) {
2425
- copy.push(marks[i]);
2426
- }
2427
- }
2428
- return !copy ? marks : copy.length ? copy : Mark.none;
2429
- }
2430
- /**
2431
- @internal
2432
- */
2433
- static compile(nodes, schema) {
2434
- let result = Object.create(null);
2435
- nodes.forEach((name, spec) => result[name] = new NodeType(name, schema, spec));
2436
- let topType = schema.spec.topNode || "doc";
2437
- if (!result[topType])
2438
- throw new RangeError("Schema is missing its top node type ('" + topType + "')");
2439
- if (!result.text)
2440
- throw new RangeError("Every schema needs a 'text' type");
2441
- for (let _ in result.text.attrs)
2442
- throw new RangeError("The text node type should not have attributes");
2443
- return result;
2444
- }
2445
- };
2446
- function validateType(typeName, attrName, type) {
2447
- let types = type.split("|");
2448
- return (value) => {
2449
- let name = value === null ? "null" : typeof value;
2450
- if (types.indexOf(name) < 0)
2451
- throw new RangeError(`Expected value of type ${types} for attribute ${attrName} on type ${typeName}, got ${name}`);
2452
- };
2453
- }
2454
- // Attribute descriptors
2455
- class Attribute {
2456
- constructor(typeName, attrName, options) {
2457
- this.hasDefault = Object.prototype.hasOwnProperty.call(options, "default");
2458
- this.default = options.default;
2459
- this.validate = typeof options.validate == "string" ? validateType(typeName, attrName, options.validate) : options.validate;
2460
- }
2461
- get isRequired() {
2462
- return !this.hasDefault;
2463
- }
2464
- }
2465
- // Marks
2466
- /**
2467
- Like nodes, marks (which are associated with nodes to signify
2468
- things like emphasis or being part of a link) are
2469
- [tagged](https://prosemirror.net/docs/ref/#model.Mark.type) with type objects, which are
2470
- instantiated once per `Schema`.
2471
- */
2472
- class MarkType {
2473
- /**
2474
- @internal
2475
- */
2476
- constructor(
2477
- /**
2478
- The name of the mark type.
2479
- */
2480
- name,
2481
- /**
2482
- @internal
2483
- */
2484
- rank,
2485
- /**
2486
- The schema that this mark type instance is part of.
2487
- */
2488
- schema,
2489
- /**
2490
- The spec on which the type is based.
2491
- */
2492
- spec) {
2493
- this.name = name;
2494
- this.rank = rank;
2495
- this.schema = schema;
2496
- this.spec = spec;
2497
- this.attrs = initAttrs(name, spec.attrs);
2498
- this.excluded = null;
2499
- let defaults = defaultAttrs(this.attrs);
2500
- this.instance = defaults ? new Mark(this, defaults) : null;
2501
- }
2502
- /**
2503
- Create a mark of this type. `attrs` may be `null` or an object
2504
- containing only some of the mark's attributes. The others, if
2505
- they have defaults, will be added.
2506
- */
2507
- create(attrs = null) {
2508
- if (!attrs && this.instance)
2509
- return this.instance;
2510
- return new Mark(this, computeAttrs(this.attrs, attrs));
2511
- }
2512
- /**
2513
- @internal
2514
- */
2515
- static compile(marks, schema) {
2516
- let result = Object.create(null), rank = 0;
2517
- marks.forEach((name, spec) => result[name] = new MarkType(name, rank++, schema, spec));
2518
- return result;
2519
- }
2520
- /**
2521
- When there is a mark of this type in the given set, a new set
2522
- without it is returned. Otherwise, the input set is returned.
2523
- */
2524
- removeFromSet(set) {
2525
- for (var i = 0; i < set.length; i++)
2526
- if (set[i].type == this) {
2527
- set = set.slice(0, i).concat(set.slice(i + 1));
2528
- i--;
2529
- }
2530
- return set;
2531
- }
2532
- /**
2533
- Tests whether there is a mark of this type in the given set.
2534
- */
2535
- isInSet(set) {
2536
- for (let i = 0; i < set.length; i++)
2537
- if (set[i].type == this)
2538
- return set[i];
2539
- }
2540
- /**
2541
- @internal
2542
- */
2543
- checkAttrs(attrs) {
2544
- checkAttrs(this.attrs, attrs, "mark", this.name);
2545
- }
2546
- /**
2547
- Queries whether a given mark type is
2548
- [excluded](https://prosemirror.net/docs/ref/#model.MarkSpec.excludes) by this one.
2549
- */
2550
- excludes(other) {
2551
- return this.excluded.indexOf(other) > -1;
2552
- }
2553
- }
2554
- /**
2555
- A document schema. Holds [node](https://prosemirror.net/docs/ref/#model.NodeType) and [mark
2556
- type](https://prosemirror.net/docs/ref/#model.MarkType) objects for the nodes and marks that may
2557
- occur in conforming documents, and provides functionality for
2558
- creating and deserializing such documents.
2559
-
2560
- When given, the type parameters provide the names of the nodes and
2561
- marks in this schema.
2562
- */
2563
- class Schema {
2564
- /**
2565
- Construct a schema from a schema [specification](https://prosemirror.net/docs/ref/#model.SchemaSpec).
2566
- */
2567
- constructor(spec) {
2568
- /**
2569
- The [linebreak
2570
- replacement](https://prosemirror.net/docs/ref/#model.NodeSpec.linebreakReplacement) node defined
2571
- in this schema, if any.
2572
- */
2573
- this.linebreakReplacement = null;
2574
- /**
2575
- An object for storing whatever values modules may want to
2576
- compute and cache per schema. (If you want to store something
2577
- in it, try to use property names unlikely to clash.)
2578
- */
2579
- this.cached = Object.create(null);
2580
- let instanceSpec = this.spec = {};
2581
- for (let prop in spec)
2582
- instanceSpec[prop] = spec[prop];
2583
- instanceSpec.nodes = OrderedMap.from(spec.nodes),
2584
- instanceSpec.marks = OrderedMap.from(spec.marks || {}),
2585
- this.nodes = NodeType$1.compile(this.spec.nodes, this);
2586
- this.marks = MarkType.compile(this.spec.marks, this);
2587
- let contentExprCache = Object.create(null);
2588
- for (let prop in this.nodes) {
2589
- if (prop in this.marks)
2590
- throw new RangeError(prop + " can not be both a node and a mark");
2591
- let type = this.nodes[prop], contentExpr = type.spec.content || "", markExpr = type.spec.marks;
2592
- type.contentMatch = contentExprCache[contentExpr] ||
2593
- (contentExprCache[contentExpr] = ContentMatch.parse(contentExpr, this.nodes));
2594
- type.inlineContent = type.contentMatch.inlineContent;
2595
- if (type.spec.linebreakReplacement) {
2596
- if (this.linebreakReplacement)
2597
- throw new RangeError("Multiple linebreak nodes defined");
2598
- if (!type.isInline || !type.isLeaf)
2599
- throw new RangeError("Linebreak replacement nodes must be inline leaf nodes");
2600
- this.linebreakReplacement = type;
2601
- }
2602
- type.markSet = markExpr == "_" ? null :
2603
- markExpr ? gatherMarks(this, markExpr.split(" ")) :
2604
- markExpr == "" || !type.inlineContent ? [] : null;
2605
- }
2606
- for (let prop in this.marks) {
2607
- let type = this.marks[prop], excl = type.spec.excludes;
2608
- type.excluded = excl == null ? [type] : excl == "" ? [] : gatherMarks(this, excl.split(" "));
2609
- }
2610
- this.nodeFromJSON = json => Node.fromJSON(this, json);
2611
- this.markFromJSON = json => Mark.fromJSON(this, json);
2612
- this.topNodeType = this.nodes[this.spec.topNode || "doc"];
2613
- this.cached.wrappings = Object.create(null);
2614
- }
2615
- /**
2616
- Create a node in this schema. The `type` may be a string or a
2617
- `NodeType` instance. Attributes will be extended with defaults,
2618
- `content` may be a `Fragment`, `null`, a `Node`, or an array of
2619
- nodes.
2620
- */
2621
- node(type, attrs = null, content, marks) {
2622
- if (typeof type == "string")
2623
- type = this.nodeType(type);
2624
- else if (!(type instanceof NodeType$1))
2625
- throw new RangeError("Invalid node type: " + type);
2626
- else if (type.schema != this)
2627
- throw new RangeError("Node type from different schema used (" + type.name + ")");
2628
- return type.createChecked(attrs, content, marks);
2629
- }
2630
- /**
2631
- Create a text node in the schema. Empty text nodes are not
2632
- allowed.
2633
- */
2634
- text(text, marks) {
2635
- let type = this.nodes.text;
2636
- return new TextNode(type, type.defaultAttrs, text, Mark.setFrom(marks));
2637
- }
2638
- /**
2639
- Create a mark with the given type and attributes.
2640
- */
2641
- mark(type, attrs) {
2642
- if (typeof type == "string")
2643
- type = this.marks[type];
2644
- return type.create(attrs);
2645
- }
2646
- /**
2647
- @internal
2648
- */
2649
- nodeType(name) {
2650
- let found = this.nodes[name];
2651
- if (!found)
2652
- throw new RangeError("Unknown node type: " + name);
2653
- return found;
2654
- }
2655
- }
2656
- function gatherMarks(schema, marks) {
2657
- let found = [];
2658
- for (let i = 0; i < marks.length; i++) {
2659
- let name = marks[i], mark = schema.marks[name], ok = mark;
2660
- if (mark) {
2661
- found.push(mark);
2662
- }
2663
- else {
2664
- for (let prop in schema.marks) {
2665
- let mark = schema.marks[prop];
2666
- if (name == "_" || (mark.spec.group && mark.spec.group.split(" ").indexOf(name) > -1))
2667
- found.push(ok = mark);
2668
- }
2669
- }
2670
- if (!ok)
2671
- throw new SyntaxError("Unknown mark type: '" + marks[i] + "'");
2672
- }
2673
- return found;
2674
- }
2675
-
2676
- function isTagRule(rule) { return rule.tag != null; }
2677
- function isStyleRule(rule) { return rule.style != null; }
2678
- /**
2679
- A DOM parser represents a strategy for parsing DOM content into a
2680
- ProseMirror document conforming to a given schema. Its behavior is
2681
- defined by an array of [rules](https://prosemirror.net/docs/ref/#model.ParseRule).
2682
- */
2683
- class DOMParser {
2684
- /**
2685
- Create a parser that targets the given schema, using the given
2686
- parsing rules.
2687
- */
2688
- constructor(
2689
- /**
2690
- The schema into which the parser parses.
2691
- */
2692
- schema,
2693
- /**
2694
- The set of [parse rules](https://prosemirror.net/docs/ref/#model.ParseRule) that the parser
2695
- uses, in order of precedence.
2696
- */
2697
- rules) {
2698
- this.schema = schema;
2699
- this.rules = rules;
2700
- /**
2701
- @internal
2702
- */
2703
- this.tags = [];
2704
- /**
2705
- @internal
2706
- */
2707
- this.styles = [];
2708
- let matchedStyles = this.matchedStyles = [];
2709
- rules.forEach(rule => {
2710
- if (isTagRule(rule)) {
2711
- this.tags.push(rule);
2712
- }
2713
- else if (isStyleRule(rule)) {
2714
- let prop = /[^=]*/.exec(rule.style)[0];
2715
- if (matchedStyles.indexOf(prop) < 0)
2716
- matchedStyles.push(prop);
2717
- this.styles.push(rule);
2718
- }
2719
- });
2720
- // Only normalize list elements when lists in the schema can't directly contain themselves
2721
- this.normalizeLists = !this.tags.some(r => {
2722
- if (!/^(ul|ol)\b/.test(r.tag) || !r.node)
2723
- return false;
2724
- let node = schema.nodes[r.node];
2725
- return node.contentMatch.matchType(node);
2726
- });
2727
- }
2728
- /**
2729
- Parse a document from the content of a DOM node.
2730
- */
2731
- parse(dom, options = {}) {
2732
- let context = new ParseContext(this, options, false);
2733
- context.addAll(dom, Mark.none, options.from, options.to);
2734
- return context.finish();
2735
- }
2736
- /**
2737
- Parses the content of the given DOM node, like
2738
- [`parse`](https://prosemirror.net/docs/ref/#model.DOMParser.parse), and takes the same set of
2739
- options. But unlike that method, which produces a whole node,
2740
- this one returns a slice that is open at the sides, meaning that
2741
- the schema constraints aren't applied to the start of nodes to
2742
- the left of the input and the end of nodes at the end.
2743
- */
2744
- parseSlice(dom, options = {}) {
2745
- let context = new ParseContext(this, options, true);
2746
- context.addAll(dom, Mark.none, options.from, options.to);
2747
- return Slice.maxOpen(context.finish());
2748
- }
2749
- /**
2750
- @internal
2751
- */
2752
- matchTag(dom, context, after) {
2753
- for (let i = after ? this.tags.indexOf(after) + 1 : 0; i < this.tags.length; i++) {
2754
- let rule = this.tags[i];
2755
- if (matches(dom, rule.tag) &&
2756
- (rule.namespace === undefined || dom.namespaceURI == rule.namespace) &&
2757
- (!rule.context || context.matchesContext(rule.context))) {
2758
- if (rule.getAttrs) {
2759
- let result = rule.getAttrs(dom);
2760
- if (result === false)
2761
- continue;
2762
- rule.attrs = result || undefined;
2763
- }
2764
- return rule;
2765
- }
2766
- }
2767
- }
2768
- /**
2769
- @internal
2770
- */
2771
- matchStyle(prop, value, context, after) {
2772
- for (let i = after ? this.styles.indexOf(after) + 1 : 0; i < this.styles.length; i++) {
2773
- let rule = this.styles[i], style = rule.style;
2774
- if (style.indexOf(prop) != 0 ||
2775
- rule.context && !context.matchesContext(rule.context) ||
2776
- // Test that the style string either precisely matches the prop,
2777
- // or has an '=' sign after the prop, followed by the given
2778
- // value.
2779
- style.length > prop.length &&
2780
- (style.charCodeAt(prop.length) != 61 || style.slice(prop.length + 1) != value))
2781
- continue;
2782
- if (rule.getAttrs) {
2783
- let result = rule.getAttrs(value);
2784
- if (result === false)
2785
- continue;
2786
- rule.attrs = result || undefined;
2787
- }
2788
- return rule;
2789
- }
2790
- }
2791
- /**
2792
- @internal
2793
- */
2794
- static schemaRules(schema) {
2795
- let result = [];
2796
- function insert(rule) {
2797
- let priority = rule.priority == null ? 50 : rule.priority, i = 0;
2798
- for (; i < result.length; i++) {
2799
- let next = result[i], nextPriority = next.priority == null ? 50 : next.priority;
2800
- if (nextPriority < priority)
2801
- break;
2802
- }
2803
- result.splice(i, 0, rule);
2804
- }
2805
- for (let name in schema.marks) {
2806
- let rules = schema.marks[name].spec.parseDOM;
2807
- if (rules)
2808
- rules.forEach(rule => {
2809
- insert(rule = copy$1(rule));
2810
- if (!(rule.mark || rule.ignore || rule.clearMark))
2811
- rule.mark = name;
2812
- });
2813
- }
2814
- for (let name in schema.nodes) {
2815
- let rules = schema.nodes[name].spec.parseDOM;
2816
- if (rules)
2817
- rules.forEach(rule => {
2818
- insert(rule = copy$1(rule));
2819
- if (!(rule.node || rule.ignore || rule.mark))
2820
- rule.node = name;
2821
- });
2822
- }
2823
- return result;
2824
- }
2825
- /**
2826
- Construct a DOM parser using the parsing rules listed in a
2827
- schema's [node specs](https://prosemirror.net/docs/ref/#model.NodeSpec.parseDOM), reordered by
2828
- [priority](https://prosemirror.net/docs/ref/#model.GenericParseRule.priority).
2829
- */
2830
- static fromSchema(schema) {
2831
- return schema.cached.domParser ||
2832
- (schema.cached.domParser = new DOMParser(schema, DOMParser.schemaRules(schema)));
2833
- }
2834
- }
2835
- const blockTags = {
2836
- address: true, article: true, aside: true, blockquote: true, canvas: true,
2837
- dd: true, div: true, dl: true, fieldset: true, figcaption: true, figure: true,
2838
- footer: true, form: true, h1: true, h2: true, h3: true, h4: true, h5: true,
2839
- h6: true, header: true, hgroup: true, hr: true, li: true, noscript: true, ol: true,
2840
- output: true, p: true, pre: true, section: true, table: true, tfoot: true, ul: true
2841
- };
2842
- const ignoreTags = {
2843
- head: true, noscript: true, object: true, script: true, style: true, title: true
2844
- };
2845
- const listTags = { ol: true, ul: true };
2846
- // Using a bitfield for node context options
2847
- const OPT_PRESERVE_WS = 1, OPT_PRESERVE_WS_FULL = 2, OPT_OPEN_LEFT = 4;
2848
- function wsOptionsFor(type, preserveWhitespace, base) {
2849
- if (preserveWhitespace != null)
2850
- return (preserveWhitespace ? OPT_PRESERVE_WS : 0) |
2851
- (preserveWhitespace === "full" ? OPT_PRESERVE_WS_FULL : 0);
2852
- return type && type.whitespace == "pre" ? OPT_PRESERVE_WS | OPT_PRESERVE_WS_FULL : base & ~OPT_OPEN_LEFT;
2853
- }
2854
- class NodeContext {
2855
- constructor(type, attrs, marks, solid, match, options) {
2856
- this.type = type;
2857
- this.attrs = attrs;
2858
- this.marks = marks;
2859
- this.solid = solid;
2860
- this.options = options;
2861
- this.content = [];
2862
- // Marks applied to the node's children
2863
- this.activeMarks = Mark.none;
2864
- this.match = match || (options & OPT_OPEN_LEFT ? null : type.contentMatch);
2865
- }
2866
- findWrapping(node) {
2867
- if (!this.match) {
2868
- if (!this.type)
2869
- return [];
2870
- let fill = this.type.contentMatch.fillBefore(Fragment.from(node));
2871
- if (fill) {
2872
- this.match = this.type.contentMatch.matchFragment(fill);
2873
- }
2874
- else {
2875
- let start = this.type.contentMatch, wrap;
2876
- if (wrap = start.findWrapping(node.type)) {
2877
- this.match = start;
2878
- return wrap;
2879
- }
2880
- else {
2881
- return null;
2882
- }
2883
- }
2884
- }
2885
- return this.match.findWrapping(node.type);
2886
- }
2887
- finish(openEnd) {
2888
- if (!(this.options & OPT_PRESERVE_WS)) { // Strip trailing whitespace
2889
- let last = this.content[this.content.length - 1], m;
2890
- if (last && last.isText && (m = /[ \t\r\n\u000c]+$/.exec(last.text))) {
2891
- let text = last;
2892
- if (last.text.length == m[0].length)
2893
- this.content.pop();
2894
- else
2895
- this.content[this.content.length - 1] = text.withText(text.text.slice(0, text.text.length - m[0].length));
2896
- }
2897
- }
2898
- let content = Fragment.from(this.content);
2899
- if (!openEnd && this.match)
2900
- content = content.append(this.match.fillBefore(Fragment.empty, true));
2901
- return this.type ? this.type.create(this.attrs, content, this.marks) : content;
2902
- }
2903
- inlineContext(node) {
2904
- if (this.type)
2905
- return this.type.inlineContent;
2906
- if (this.content.length)
2907
- return this.content[0].isInline;
2908
- return node.parentNode && !blockTags.hasOwnProperty(node.parentNode.nodeName.toLowerCase());
2909
- }
2910
- }
2911
- class ParseContext {
2912
- constructor(
2913
- // The parser we are using.
2914
- parser,
2915
- // The options passed to this parse.
2916
- options, isOpen) {
2917
- this.parser = parser;
2918
- this.options = options;
2919
- this.isOpen = isOpen;
2920
- this.open = 0;
2921
- this.localPreserveWS = false;
2922
- let topNode = options.topNode, topContext;
2923
- let topOptions = wsOptionsFor(null, options.preserveWhitespace, 0) | (isOpen ? OPT_OPEN_LEFT : 0);
2924
- if (topNode)
2925
- topContext = new NodeContext(topNode.type, topNode.attrs, Mark.none, true, options.topMatch || topNode.type.contentMatch, topOptions);
2926
- else if (isOpen)
2927
- topContext = new NodeContext(null, null, Mark.none, true, null, topOptions);
2928
- else
2929
- topContext = new NodeContext(parser.schema.topNodeType, null, Mark.none, true, null, topOptions);
2930
- this.nodes = [topContext];
2931
- this.find = options.findPositions;
2932
- this.needsBlock = false;
2933
- }
2934
- get top() {
2935
- return this.nodes[this.open];
2936
- }
2937
- // Add a DOM node to the content. Text is inserted as text node,
2938
- // otherwise, the node is passed to `addElement` or, if it has a
2939
- // `style` attribute, `addElementWithStyles`.
2940
- addDOM(dom, marks) {
2941
- if (dom.nodeType == 3)
2942
- this.addTextNode(dom, marks);
2943
- else if (dom.nodeType == 1)
2944
- this.addElement(dom, marks);
2945
- }
2946
- addTextNode(dom, marks) {
2947
- let value = dom.nodeValue;
2948
- let top = this.top, preserveWS = (top.options & OPT_PRESERVE_WS_FULL) ? "full"
2949
- : this.localPreserveWS || (top.options & OPT_PRESERVE_WS) > 0;
2950
- let { schema } = this.parser;
2951
- if (preserveWS === "full" ||
2952
- top.inlineContext(dom) ||
2953
- /[^ \t\r\n\u000c]/.test(value)) {
2954
- if (!preserveWS) {
2955
- value = value.replace(/[ \t\r\n\u000c]+/g, " ");
2956
- // If this starts with whitespace, and there is no node before it, or
2957
- // a hard break, or a text node that ends with whitespace, strip the
2958
- // leading space.
2959
- if (/^[ \t\r\n\u000c]/.test(value) && this.open == this.nodes.length - 1) {
2960
- let nodeBefore = top.content[top.content.length - 1];
2961
- let domNodeBefore = dom.previousSibling;
2962
- if (!nodeBefore ||
2963
- (domNodeBefore && domNodeBefore.nodeName == 'BR') ||
2964
- (nodeBefore.isText && /[ \t\r\n\u000c]$/.test(nodeBefore.text)))
2965
- value = value.slice(1);
2966
- }
2967
- }
2968
- else if (preserveWS === "full") {
2969
- value = value.replace(/\r\n?/g, "\n");
2970
- }
2971
- else if (schema.linebreakReplacement && /[\r\n]/.test(value) && this.top.findWrapping(schema.linebreakReplacement.create())) {
2972
- let lines = value.split(/\r?\n|\r/);
2973
- for (let i = 0; i < lines.length; i++) {
2974
- if (i)
2975
- this.insertNode(schema.linebreakReplacement.create(), marks, true);
2976
- if (lines[i])
2977
- this.insertNode(schema.text(lines[i]), marks, !/\S/.test(lines[i]));
2978
- }
2979
- value = "";
2980
- }
2981
- else {
2982
- value = value.replace(/\r?\n|\r/g, " ");
2983
- }
2984
- if (value)
2985
- this.insertNode(schema.text(value), marks, !/\S/.test(value));
2986
- this.findInText(dom);
2987
- }
2988
- else {
2989
- this.findInside(dom);
2990
- }
2991
- }
2992
- // Try to find a handler for the given tag and use that to parse. If
2993
- // none is found, the element's content nodes are added directly.
2994
- addElement(dom, marks, matchAfter) {
2995
- let outerWS = this.localPreserveWS, top = this.top;
2996
- if (dom.tagName == "PRE" || /pre/.test(dom.style && dom.style.whiteSpace))
2997
- this.localPreserveWS = true;
2998
- let name = dom.nodeName.toLowerCase(), ruleID;
2999
- if (listTags.hasOwnProperty(name) && this.parser.normalizeLists)
3000
- normalizeList(dom);
3001
- let rule = (this.options.ruleFromNode && this.options.ruleFromNode(dom)) ||
3002
- (ruleID = this.parser.matchTag(dom, this, matchAfter));
3003
- out: if (rule ? rule.ignore : ignoreTags.hasOwnProperty(name)) {
3004
- this.findInside(dom);
3005
- this.ignoreFallback(dom, marks);
3006
- }
3007
- else if (!rule || rule.skip || rule.closeParent) {
3008
- if (rule && rule.closeParent)
3009
- this.open = Math.max(0, this.open - 1);
3010
- else if (rule && rule.skip.nodeType)
3011
- dom = rule.skip;
3012
- let sync, oldNeedsBlock = this.needsBlock;
3013
- if (blockTags.hasOwnProperty(name)) {
3014
- if (top.content.length && top.content[0].isInline && this.open) {
3015
- this.open--;
3016
- top = this.top;
3017
- }
3018
- sync = true;
3019
- if (!top.type)
3020
- this.needsBlock = true;
3021
- }
3022
- else if (!dom.firstChild) {
3023
- this.leafFallback(dom, marks);
3024
- break out;
3025
- }
3026
- let innerMarks = rule && rule.skip ? marks : this.readStyles(dom, marks);
3027
- if (innerMarks)
3028
- this.addAll(dom, innerMarks);
3029
- if (sync)
3030
- this.sync(top);
3031
- this.needsBlock = oldNeedsBlock;
3032
- }
3033
- else {
3034
- let innerMarks = this.readStyles(dom, marks);
3035
- if (innerMarks)
3036
- this.addElementByRule(dom, rule, innerMarks, rule.consuming === false ? ruleID : undefined);
3037
- }
3038
- this.localPreserveWS = outerWS;
3039
- }
3040
- // Called for leaf DOM nodes that would otherwise be ignored
3041
- leafFallback(dom, marks) {
3042
- if (dom.nodeName == "BR" && this.top.type && this.top.type.inlineContent)
3043
- this.addTextNode(dom.ownerDocument.createTextNode("\n"), marks);
3044
- }
3045
- // Called for ignored nodes
3046
- ignoreFallback(dom, marks) {
3047
- // Ignored BR nodes should at least create an inline context
3048
- if (dom.nodeName == "BR" && (!this.top.type || !this.top.type.inlineContent))
3049
- this.findPlace(this.parser.schema.text("-"), marks, true);
3050
- }
3051
- // Run any style parser associated with the node's styles. Either
3052
- // return an updated array of marks, or null to indicate some of the
3053
- // styles had a rule with `ignore` set.
3054
- readStyles(dom, marks) {
3055
- let styles = dom.style;
3056
- // Because many properties will only show up in 'normalized' form
3057
- // in `style.item` (i.e. text-decoration becomes
3058
- // text-decoration-line, text-decoration-color, etc), we directly
3059
- // query the styles mentioned in our rules instead of iterating
3060
- // over the items.
3061
- if (styles && styles.length)
3062
- for (let i = 0; i < this.parser.matchedStyles.length; i++) {
3063
- let name = this.parser.matchedStyles[i], value = styles.getPropertyValue(name);
3064
- if (value)
3065
- for (let after = undefined;;) {
3066
- let rule = this.parser.matchStyle(name, value, this, after);
3067
- if (!rule)
3068
- break;
3069
- if (rule.ignore)
3070
- return null;
3071
- if (rule.clearMark)
3072
- marks = marks.filter(m => !rule.clearMark(m));
3073
- else
3074
- marks = marks.concat(this.parser.schema.marks[rule.mark].create(rule.attrs));
3075
- if (rule.consuming === false)
3076
- after = rule;
3077
- else
3078
- break;
3079
- }
3080
- }
3081
- return marks;
3082
- }
3083
- // Look up a handler for the given node. If none are found, return
3084
- // false. Otherwise, apply it, use its return value to drive the way
3085
- // the node's content is wrapped, and return true.
3086
- addElementByRule(dom, rule, marks, continueAfter) {
3087
- let sync, nodeType;
3088
- if (rule.node) {
3089
- nodeType = this.parser.schema.nodes[rule.node];
3090
- if (!nodeType.isLeaf) {
3091
- let inner = this.enter(nodeType, rule.attrs || null, marks, rule.preserveWhitespace);
3092
- if (inner) {
3093
- sync = true;
3094
- marks = inner;
3095
- }
3096
- }
3097
- else if (!this.insertNode(nodeType.create(rule.attrs), marks, dom.nodeName == "BR")) {
3098
- this.leafFallback(dom, marks);
3099
- }
3100
- }
3101
- else {
3102
- let markType = this.parser.schema.marks[rule.mark];
3103
- marks = marks.concat(markType.create(rule.attrs));
3104
- }
3105
- let startIn = this.top;
3106
- if (nodeType && nodeType.isLeaf) {
3107
- this.findInside(dom);
3108
- }
3109
- else if (continueAfter) {
3110
- this.addElement(dom, marks, continueAfter);
3111
- }
3112
- else if (rule.getContent) {
3113
- this.findInside(dom);
3114
- rule.getContent(dom, this.parser.schema).forEach(node => this.insertNode(node, marks, false));
3115
- }
3116
- else {
3117
- let contentDOM = dom;
3118
- if (typeof rule.contentElement == "string")
3119
- contentDOM = dom.querySelector(rule.contentElement);
3120
- else if (typeof rule.contentElement == "function")
3121
- contentDOM = rule.contentElement(dom);
3122
- else if (rule.contentElement)
3123
- contentDOM = rule.contentElement;
3124
- this.findAround(dom, contentDOM, true);
3125
- this.addAll(contentDOM, marks);
3126
- this.findAround(dom, contentDOM, false);
3127
- }
3128
- if (sync && this.sync(startIn))
3129
- this.open--;
3130
- }
3131
- // Add all child nodes between `startIndex` and `endIndex` (or the
3132
- // whole node, if not given). If `sync` is passed, use it to
3133
- // synchronize after every block element.
3134
- addAll(parent, marks, startIndex, endIndex) {
3135
- let index = startIndex || 0;
3136
- for (let dom = startIndex ? parent.childNodes[startIndex] : parent.firstChild, end = endIndex == null ? null : parent.childNodes[endIndex]; dom != end; dom = dom.nextSibling, ++index) {
3137
- this.findAtPoint(parent, index);
3138
- this.addDOM(dom, marks);
3139
- }
3140
- this.findAtPoint(parent, index);
3141
- }
3142
- // Try to find a way to fit the given node type into the current
3143
- // context. May add intermediate wrappers and/or leave non-solid
3144
- // nodes that we're in.
3145
- findPlace(node, marks, cautious) {
3146
- let route, sync;
3147
- for (let depth = this.open, penalty = 0; depth >= 0; depth--) {
3148
- let cx = this.nodes[depth];
3149
- let found = cx.findWrapping(node);
3150
- if (found && (!route || route.length > found.length + penalty)) {
3151
- route = found;
3152
- sync = cx;
3153
- if (!found.length)
3154
- break;
3155
- }
3156
- if (cx.solid) {
3157
- if (cautious)
3158
- break;
3159
- penalty += 2;
3160
- }
3161
- }
3162
- if (!route)
3163
- return null;
3164
- this.sync(sync);
3165
- for (let i = 0; i < route.length; i++)
3166
- marks = this.enterInner(route[i], null, marks, false);
3167
- return marks;
3168
- }
3169
- // Try to insert the given node, adjusting the context when needed.
3170
- insertNode(node, marks, cautious) {
3171
- if (node.isInline && this.needsBlock && !this.top.type) {
3172
- let block = this.textblockFromContext();
3173
- if (block)
3174
- marks = this.enterInner(block, null, marks);
3175
- }
3176
- let innerMarks = this.findPlace(node, marks, cautious);
3177
- if (innerMarks) {
3178
- this.closeExtra();
3179
- let top = this.top;
3180
- if (top.match)
3181
- top.match = top.match.matchType(node.type);
3182
- let nodeMarks = Mark.none;
3183
- for (let m of innerMarks.concat(node.marks))
3184
- if (top.type ? top.type.allowsMarkType(m.type) : markMayApply(m.type, node.type))
3185
- nodeMarks = m.addToSet(nodeMarks);
3186
- top.content.push(node.mark(nodeMarks));
3187
- return true;
3188
- }
3189
- return false;
3190
- }
3191
- // Try to start a node of the given type, adjusting the context when
3192
- // necessary.
3193
- enter(type, attrs, marks, preserveWS) {
3194
- let innerMarks = this.findPlace(type.create(attrs), marks, false);
3195
- if (innerMarks)
3196
- innerMarks = this.enterInner(type, attrs, marks, true, preserveWS);
3197
- return innerMarks;
3198
- }
3199
- // Open a node of the given type
3200
- enterInner(type, attrs, marks, solid = false, preserveWS) {
3201
- this.closeExtra();
3202
- let top = this.top;
3203
- top.match = top.match && top.match.matchType(type);
3204
- let options = wsOptionsFor(type, preserveWS, top.options);
3205
- if ((top.options & OPT_OPEN_LEFT) && top.content.length == 0)
3206
- options |= OPT_OPEN_LEFT;
3207
- let applyMarks = Mark.none;
3208
- marks = marks.filter(m => {
3209
- if (top.type ? top.type.allowsMarkType(m.type) : markMayApply(m.type, type)) {
3210
- applyMarks = m.addToSet(applyMarks);
3211
- return false;
3212
- }
3213
- return true;
3214
- });
3215
- this.nodes.push(new NodeContext(type, attrs, applyMarks, solid, null, options));
3216
- this.open++;
3217
- return marks;
3218
- }
3219
- // Make sure all nodes above this.open are finished and added to
3220
- // their parents
3221
- closeExtra(openEnd = false) {
3222
- let i = this.nodes.length - 1;
3223
- if (i > this.open) {
3224
- for (; i > this.open; i--)
3225
- this.nodes[i - 1].content.push(this.nodes[i].finish(openEnd));
3226
- this.nodes.length = this.open + 1;
3227
- }
3228
- }
3229
- finish() {
3230
- this.open = 0;
3231
- this.closeExtra(this.isOpen);
3232
- return this.nodes[0].finish(!!(this.isOpen || this.options.topOpen));
3233
- }
3234
- sync(to) {
3235
- for (let i = this.open; i >= 0; i--) {
3236
- if (this.nodes[i] == to) {
3237
- this.open = i;
3238
- return true;
3239
- }
3240
- else if (this.localPreserveWS) {
3241
- this.nodes[i].options |= OPT_PRESERVE_WS;
3242
- }
3243
- }
3244
- return false;
3245
- }
3246
- get currentPos() {
3247
- this.closeExtra();
3248
- let pos = 0;
3249
- for (let i = this.open; i >= 0; i--) {
3250
- let content = this.nodes[i].content;
3251
- for (let j = content.length - 1; j >= 0; j--)
3252
- pos += content[j].nodeSize;
3253
- if (i)
3254
- pos++;
3255
- }
3256
- return pos;
3257
- }
3258
- findAtPoint(parent, offset) {
3259
- if (this.find)
3260
- for (let i = 0; i < this.find.length; i++) {
3261
- if (this.find[i].node == parent && this.find[i].offset == offset)
3262
- this.find[i].pos = this.currentPos;
3263
- }
3264
- }
3265
- findInside(parent) {
3266
- if (this.find)
3267
- for (let i = 0; i < this.find.length; i++) {
3268
- if (this.find[i].pos == null && parent.nodeType == 1 && parent.contains(this.find[i].node))
3269
- this.find[i].pos = this.currentPos;
3270
- }
3271
- }
3272
- findAround(parent, content, before) {
3273
- if (parent != content && this.find)
3274
- for (let i = 0; i < this.find.length; i++) {
3275
- if (this.find[i].pos == null && parent.nodeType == 1 && parent.contains(this.find[i].node)) {
3276
- let pos = content.compareDocumentPosition(this.find[i].node);
3277
- if (pos & (before ? 2 : 4))
3278
- this.find[i].pos = this.currentPos;
3279
- }
3280
- }
3281
- }
3282
- findInText(textNode) {
3283
- if (this.find)
3284
- for (let i = 0; i < this.find.length; i++) {
3285
- if (this.find[i].node == textNode)
3286
- this.find[i].pos = this.currentPos - (textNode.nodeValue.length - this.find[i].offset);
3287
- }
3288
- }
3289
- // Determines whether the given context string matches this context.
3290
- matchesContext(context) {
3291
- if (context.indexOf("|") > -1)
3292
- return context.split(/\s*\|\s*/).some(this.matchesContext, this);
3293
- let parts = context.split("/");
3294
- let option = this.options.context;
3295
- let useRoot = !this.isOpen && (!option || option.parent.type == this.nodes[0].type);
3296
- let minDepth = -(option ? option.depth + 1 : 0) + (useRoot ? 0 : 1);
3297
- let match = (i, depth) => {
3298
- for (; i >= 0; i--) {
3299
- let part = parts[i];
3300
- if (part == "") {
3301
- if (i == parts.length - 1 || i == 0)
3302
- continue;
3303
- for (; depth >= minDepth; depth--)
3304
- if (match(i - 1, depth))
3305
- return true;
3306
- return false;
3307
- }
3308
- else {
3309
- let next = depth > 0 || (depth == 0 && useRoot) ? this.nodes[depth].type
3310
- : option && depth >= minDepth ? option.node(depth - minDepth).type
3311
- : null;
3312
- if (!next || (next.name != part && !next.isInGroup(part)))
3313
- return false;
3314
- depth--;
3315
- }
3316
- }
3317
- return true;
3318
- };
3319
- return match(parts.length - 1, this.open);
3320
- }
3321
- textblockFromContext() {
3322
- let $context = this.options.context;
3323
- if ($context)
3324
- for (let d = $context.depth; d >= 0; d--) {
3325
- let deflt = $context.node(d).contentMatchAt($context.indexAfter(d)).defaultType;
3326
- if (deflt && deflt.isTextblock && deflt.defaultAttrs)
3327
- return deflt;
3328
- }
3329
- for (let name in this.parser.schema.nodes) {
3330
- let type = this.parser.schema.nodes[name];
3331
- if (type.isTextblock && type.defaultAttrs)
3332
- return type;
3333
- }
3334
- }
3335
- }
3336
- // Kludge to work around directly nested list nodes produced by some
3337
- // tools and allowed by browsers to mean that the nested list is
3338
- // actually part of the list item above it.
3339
- function normalizeList(dom) {
3340
- for (let child = dom.firstChild, prevItem = null; child; child = child.nextSibling) {
3341
- let name = child.nodeType == 1 ? child.nodeName.toLowerCase() : null;
3342
- if (name && listTags.hasOwnProperty(name) && prevItem) {
3343
- prevItem.appendChild(child);
3344
- child = prevItem;
3345
- }
3346
- else if (name == "li") {
3347
- prevItem = child;
3348
- }
3349
- else if (name) {
3350
- prevItem = null;
3351
- }
3352
- }
3353
- }
3354
- // Apply a CSS selector.
3355
- function matches(dom, selector) {
3356
- return (dom.matches || dom.msMatchesSelector || dom.webkitMatchesSelector || dom.mozMatchesSelector).call(dom, selector);
3357
- }
3358
- function copy$1(obj) {
3359
- let copy = {};
3360
- for (let prop in obj)
3361
- copy[prop] = obj[prop];
3362
- return copy;
3363
- }
3364
- // Used when finding a mark at the top level of a fragment parse.
3365
- // Checks whether it would be reasonable to apply a given mark type to
3366
- // a given node, by looking at the way the mark occurs in the schema.
3367
- function markMayApply(markType, nodeType) {
3368
- let nodes = nodeType.schema.nodes;
3369
- for (let name in nodes) {
3370
- let parent = nodes[name];
3371
- if (!parent.allowsMarkType(markType))
3372
- continue;
3373
- let seen = [], scan = (match) => {
3374
- seen.push(match);
3375
- for (let i = 0; i < match.edgeCount; i++) {
3376
- let { type, next } = match.edge(i);
3377
- if (type == nodeType)
3378
- return true;
3379
- if (seen.indexOf(next) < 0 && scan(next))
3380
- return true;
3381
- }
3382
- };
3383
- if (scan(parent.contentMatch))
3384
- return true;
3385
- }
3386
- }
3387
-
3388
- /**
3389
- A DOM serializer knows how to convert ProseMirror nodes and
3390
- marks of various types to DOM nodes.
3391
- */
3392
- class DOMSerializer {
3393
- /**
3394
- Create a serializer. `nodes` should map node names to functions
3395
- that take a node and return a description of the corresponding
3396
- DOM. `marks` does the same for mark names, but also gets an
3397
- argument that tells it whether the mark's content is block or
3398
- inline content (for typical use, it'll always be inline). A mark
3399
- serializer may be `null` to indicate that marks of that type
3400
- should not be serialized.
3401
- */
3402
- constructor(
3403
- /**
3404
- The node serialization functions.
3405
- */
3406
- nodes,
3407
- /**
3408
- The mark serialization functions.
3409
- */
3410
- marks) {
3411
- this.nodes = nodes;
3412
- this.marks = marks;
3413
- }
3414
- /**
3415
- Serialize the content of this fragment to a DOM fragment. When
3416
- not in the browser, the `document` option, containing a DOM
3417
- document, should be passed so that the serializer can create
3418
- nodes.
3419
- */
3420
- serializeFragment(fragment, options = {}, target) {
3421
- if (!target)
3422
- target = doc$1(options).createDocumentFragment();
3423
- let top = target, active = [];
3424
- fragment.forEach(node => {
3425
- if (active.length || node.marks.length) {
3426
- let keep = 0, rendered = 0;
3427
- while (keep < active.length && rendered < node.marks.length) {
3428
- let next = node.marks[rendered];
3429
- if (!this.marks[next.type.name]) {
3430
- rendered++;
3431
- continue;
3432
- }
3433
- if (!next.eq(active[keep][0]) || next.type.spec.spanning === false)
3434
- break;
3435
- keep++;
3436
- rendered++;
3437
- }
3438
- while (keep < active.length)
3439
- top = active.pop()[1];
3440
- while (rendered < node.marks.length) {
3441
- let add = node.marks[rendered++];
3442
- let markDOM = this.serializeMark(add, node.isInline, options);
3443
- if (markDOM) {
3444
- active.push([add, top]);
3445
- top.appendChild(markDOM.dom);
3446
- top = markDOM.contentDOM || markDOM.dom;
3447
- }
3448
- }
3449
- }
3450
- top.appendChild(this.serializeNodeInner(node, options));
3451
- });
3452
- return target;
3453
- }
3454
- /**
3455
- @internal
3456
- */
3457
- serializeNodeInner(node, options) {
3458
- let { dom, contentDOM } = renderSpec(doc$1(options), this.nodes[node.type.name](node), null, node.attrs);
3459
- if (contentDOM) {
3460
- if (node.isLeaf)
3461
- throw new RangeError("Content hole not allowed in a leaf node spec");
3462
- this.serializeFragment(node.content, options, contentDOM);
3463
- }
3464
- return dom;
3465
- }
3466
- /**
3467
- Serialize this node to a DOM node. This can be useful when you
3468
- need to serialize a part of a document, as opposed to the whole
3469
- document. To serialize a whole document, use
3470
- [`serializeFragment`](https://prosemirror.net/docs/ref/#model.DOMSerializer.serializeFragment) on
3471
- its [content](https://prosemirror.net/docs/ref/#model.Node.content).
3472
- */
3473
- serializeNode(node, options = {}) {
3474
- let dom = this.serializeNodeInner(node, options);
3475
- for (let i = node.marks.length - 1; i >= 0; i--) {
3476
- let wrap = this.serializeMark(node.marks[i], node.isInline, options);
3477
- if (wrap) {
3478
- (wrap.contentDOM || wrap.dom).appendChild(dom);
3479
- dom = wrap.dom;
3480
- }
3481
- }
3482
- return dom;
3483
- }
3484
- /**
3485
- @internal
3486
- */
3487
- serializeMark(mark, inline, options = {}) {
3488
- let toDOM = this.marks[mark.type.name];
3489
- return toDOM && renderSpec(doc$1(options), toDOM(mark, inline), null, mark.attrs);
3490
- }
3491
- static renderSpec(doc, structure, xmlNS = null, blockArraysIn) {
3492
- return renderSpec(doc, structure, xmlNS, blockArraysIn);
3493
- }
3494
- /**
3495
- Build a serializer using the [`toDOM`](https://prosemirror.net/docs/ref/#model.NodeSpec.toDOM)
3496
- properties in a schema's node and mark specs.
3497
- */
3498
- static fromSchema(schema) {
3499
- return schema.cached.domSerializer ||
3500
- (schema.cached.domSerializer = new DOMSerializer(this.nodesFromSchema(schema), this.marksFromSchema(schema)));
3501
- }
3502
- /**
3503
- Gather the serializers in a schema's node specs into an object.
3504
- This can be useful as a base to build a custom serializer from.
3505
- */
3506
- static nodesFromSchema(schema) {
3507
- let result = gatherToDOM(schema.nodes);
3508
- if (!result.text)
3509
- result.text = node => node.text;
3510
- return result;
3511
- }
3512
- /**
3513
- Gather the serializers in a schema's mark specs into an object.
3514
- */
3515
- static marksFromSchema(schema) {
3516
- return gatherToDOM(schema.marks);
3517
- }
3518
- }
3519
- function gatherToDOM(obj) {
3520
- let result = {};
3521
- for (let name in obj) {
3522
- let toDOM = obj[name].spec.toDOM;
3523
- if (toDOM)
3524
- result[name] = toDOM;
3525
- }
3526
- return result;
3527
- }
3528
- function doc$1(options) {
3529
- return options.document || window.document;
3530
- }
3531
- const suspiciousAttributeCache = new WeakMap();
3532
- function suspiciousAttributes(attrs) {
3533
- let value = suspiciousAttributeCache.get(attrs);
3534
- if (value === undefined)
3535
- suspiciousAttributeCache.set(attrs, value = suspiciousAttributesInner(attrs));
3536
- return value;
3537
- }
3538
- function suspiciousAttributesInner(attrs) {
3539
- let result = null;
3540
- function scan(value) {
3541
- if (value && typeof value == "object") {
3542
- if (Array.isArray(value)) {
3543
- if (typeof value[0] == "string") {
3544
- if (!result)
3545
- result = [];
3546
- result.push(value);
3547
- }
3548
- else {
3549
- for (let i = 0; i < value.length; i++)
3550
- scan(value[i]);
3551
- }
3552
- }
3553
- else {
3554
- for (let prop in value)
3555
- scan(value[prop]);
3556
- }
3557
- }
3558
- }
3559
- scan(attrs);
3560
- return result;
3561
- }
3562
- function renderSpec(doc, structure, xmlNS, blockArraysIn) {
3563
- if (typeof structure == "string")
3564
- return { dom: doc.createTextNode(structure) };
3565
- if (structure.nodeType != null)
3566
- return { dom: structure };
3567
- if (structure.dom && structure.dom.nodeType != null)
3568
- return structure;
3569
- let tagName = structure[0], suspicious;
3570
- if (typeof tagName != "string")
3571
- throw new RangeError("Invalid array passed to renderSpec");
3572
- if (blockArraysIn && (suspicious = suspiciousAttributes(blockArraysIn)) &&
3573
- suspicious.indexOf(structure) > -1)
3574
- throw new RangeError("Using an array from an attribute object as a DOM spec. This may be an attempted cross site scripting attack.");
3575
- let space = tagName.indexOf(" ");
3576
- if (space > 0) {
3577
- xmlNS = tagName.slice(0, space);
3578
- tagName = tagName.slice(space + 1);
3579
- }
3580
- let contentDOM;
3581
- let dom = (xmlNS ? doc.createElementNS(xmlNS, tagName) : doc.createElement(tagName));
3582
- let attrs = structure[1], start = 1;
3583
- if (attrs && typeof attrs == "object" && attrs.nodeType == null && !Array.isArray(attrs)) {
3584
- start = 2;
3585
- for (let name in attrs)
3586
- if (attrs[name] != null) {
3587
- let space = name.indexOf(" ");
3588
- if (space > 0)
3589
- dom.setAttributeNS(name.slice(0, space), name.slice(space + 1), attrs[name]);
3590
- else if (name == "style" && dom.style)
3591
- dom.style.cssText = attrs[name];
3592
- else
3593
- dom.setAttribute(name, attrs[name]);
3594
- }
3595
- }
3596
- for (let i = start; i < structure.length; i++) {
3597
- let child = structure[i];
3598
- if (child === 0) {
3599
- if (i < structure.length - 1 || i > start)
3600
- throw new RangeError("Content hole must be the only child of its parent node");
3601
- return { dom, contentDOM: dom };
3602
- }
3603
- else {
3604
- let { dom: inner, contentDOM: innerContent } = renderSpec(doc, child, xmlNS, blockArraysIn);
3605
- dom.appendChild(inner);
3606
- if (innerContent) {
3607
- if (contentDOM)
3608
- throw new RangeError("Multiple content holes");
3609
- contentDOM = innerContent;
3610
- }
3611
- }
3612
- }
3613
- return { dom, contentDOM };
3614
- }
3615
-
3616
20
  // Recovery values encode a range index and an offset. They are
3617
21
  // represented as numbers, because tons of them will be created when
3618
22
  // mapping, for example, a large number of decorations. The number's
@@ -12544,8 +8948,6 @@ class TextblockAttrs {
12544
8948
  }
12545
8949
  }
12546
8950
 
12547
- const impl = Symbol("rte-internal-impl");
12548
-
12549
8951
  class TextblockMarks {
12550
8952
  constructor(specs) {
12551
8953
  this.specs = specs;
@@ -12560,68 +8962,6 @@ class TextblockMarks {
12560
8962
  }
12561
8963
  }
12562
8964
 
12563
- const sortedContributions = (contributions) => {
12564
- return contributions.sort((a, b) => {
12565
- const priorityDelta = a.priority - b.priority;
12566
- if (priorityDelta !== 0) {
12567
- return priorityDelta;
12568
- }
12569
- if (a.featureName === b.featureName) {
12570
- return 0;
12571
- }
12572
- return a.featureName > b.featureName ? 1 : -1;
12573
- }).map((c) => c.value);
12574
- };
12575
- const contributionPriority = {
12576
- high: -10,
12577
- default: 0};
12578
- class RteFeature {
12579
- }
12580
- class RteFeatureImpl {
12581
- /**
12582
- * Creates a contribution of this feature.
12583
- */
12584
- contribution(value, order) {
12585
- return {
12586
- featureName: this.name,
12587
- priority: order ?? contributionPriority.default,
12588
- value
12589
- };
12590
- }
12591
- getStyles() {
12592
- return [];
12593
- }
12594
- getSchema(textblockAttrs, textblockMarks) {
12595
- return [];
12596
- }
12597
- getTextblockAttrs() {
12598
- return [];
12599
- }
12600
- getTextblockMarks() {
12601
- return [];
12602
- }
12603
- getPlugins(rte) {
12604
- return [];
12605
- }
12606
- getToolbarItems(rte) {
12607
- return [];
12608
- }
12609
- // A feature may include additional features for better organization.
12610
- getFeatures() {
12611
- return [this];
12612
- }
12613
- }
12614
- const featureFacade = (FeatureImpl) => {
12615
- var _a;
12616
- class Facade extends (_a = RteFeature, _a) {
12617
- constructor(...args) {
12618
- super();
12619
- this[impl] = new FeatureImpl(...args);
12620
- }
12621
- }
12622
- return Facade;
12623
- };
12624
-
12625
8965
  var base = {
12626
8966
  8: "Backspace",
12627
8967
  9: "Tab",
@@ -15234,7 +11574,7 @@ const popoverDefinition = defineVividComponent(
15234
11574
  {}
15235
11575
  );
15236
11576
 
15237
- const linkCss = ".link-popover{display:flex;flex-direction:column;padding:16px;gap:8px;inline-size:450px}.link-popover-header{display:flex;align-items:center;justify-content:space-between}.link-popover-label{color:var(--vvd-color-neutral-600);font:var(--vvd-typography-base-condensed)}.link-popover-content{display:flex;flex-direction:column;gap:16px}.link,.editor a[href],.link:visited,.editor a[href]:visited{color:var(--vvd-color-cta-600)}.link:hover,.editor a[href]:hover{color:var(--vvd-color-cta-500)}.link:active,.editor a[href]:active{color:var(--vvd-color-cta-400)}.link-action-bar{display:flex;justify-content:end;gap:8px}.link-toolbar-menu{display:flex;flex-direction:column;padding:8px;gap:16px;inline-size:368px}";
11577
+ const linkCss = ".link-popover{display:flex;flex-direction:column;padding:16px;gap:8px;inline-size:450px}.link-popover-header{display:flex;align-items:center;justify-content:space-between}.link-popover-label{color:var(--vvd-color-neutral-600);font:var(--vvd-typography-base-condensed)}.link-popover-content{display:flex;flex-direction:column;gap:16px}.link,.rich-text a[href],.link:visited,.rich-text a[href]:visited{color:var(--vvd-color-cta-600)}.link:hover,.rich-text a[href]:hover{color:var(--vvd-color-cta-500)}.link:active,.rich-text a[href]:active{color:var(--vvd-color-cta-400)}.link-action-bar{display:flex;justify-content:end;gap:8px}.link-toolbar-menu{display:flex;flex-direction:column;padding:8px;gap:16px;inline-size:368px}";
15238
11578
 
15239
11579
  class RteLinkFeatureImpl extends RteFeatureImpl {
15240
11580
  constructor() {
@@ -15257,36 +11597,45 @@ class RteLinkFeatureImpl extends RteFeatureImpl {
15257
11597
  }
15258
11598
  getSchema() {
15259
11599
  return [
15260
- this.contribution({
15261
- marks: {
15262
- link: {
15263
- attrs: {
15264
- href: { validate: "string" }
15265
- },
15266
- inclusive: false,
15267
- parseDOM: [
15268
- {
15269
- tag: "a[href]",
15270
- getAttrs(dom) {
15271
- return {
15272
- href: dom.getAttribute("href")
15273
- };
11600
+ this.contribution(
11601
+ {
11602
+ marks: {
11603
+ link: {
11604
+ attrs: {
11605
+ href: { validate: "string" }
11606
+ },
11607
+ inclusive: false,
11608
+ parseDOM: [
11609
+ {
11610
+ tag: "a[href]",
11611
+ getAttrs(dom) {
11612
+ return {
11613
+ href: dom.getAttribute("href")
11614
+ };
11615
+ }
15274
11616
  }
11617
+ ],
11618
+ toDOM(node) {
11619
+ const { href } = node.attrs;
11620
+ return ["a", { href }, 0];
15275
11621
  }
15276
- ],
15277
- toDOM(node) {
15278
- const { href } = node.attrs;
15279
- return ["a", { href }, 0];
15280
11622
  }
15281
11623
  }
15282
- }
15283
- })
11624
+ },
11625
+ // A higher priority will ensure links are serialized to a single <a> element over other marks
11626
+ // E.g. <a>my <b>bold</b> link</a> instead of <a>my </a><b><a>bold</a></b><a> link</a>
11627
+ contributionPriority.high
11628
+ )
15284
11629
  ];
15285
11630
  }
15286
11631
  getTextblockMarks() {
15287
11632
  return [this.contribution({ markName: "link" })];
15288
11633
  }
15289
11634
  getPlugins(rte) {
11635
+ const insertLinkCommand = () => {
11636
+ this.toolbarMenu.open = true;
11637
+ return true;
11638
+ };
15290
11639
  return [
15291
11640
  this.contribution(
15292
11641
  new Plugin({
@@ -15318,7 +11667,7 @@ class RteLinkFeatureImpl extends RteFeatureImpl {
15318
11667
  className: "link-popover-label",
15319
11668
  children: [
15320
11669
  createText(ctx, {
15321
- text: () => ctx.rte.getLocale().richTextEditor.clickHere
11670
+ text: () => this.getCurrentLink(ctx.view.state)?.text || ""
15322
11671
  })
15323
11672
  ]
15324
11673
  }),
@@ -15392,6 +11741,11 @@ class RteLinkFeatureImpl extends RteFeatureImpl {
15392
11741
  };
15393
11742
  }
15394
11743
  })
11744
+ ),
11745
+ this.contribution(
11746
+ keymap({
11747
+ "Mod-k": insertLinkCommand
11748
+ })
15395
11749
  )
15396
11750
  ];
15397
11751
  }
@@ -15498,23 +11852,44 @@ class RteLinkFeatureImpl extends RteFeatureImpl {
15498
11852
  }
15499
11853
  getCurrentLink(state) {
15500
11854
  const selection = state.selection;
15501
- if (!(selection instanceof TextSelection && selection.empty)) {
11855
+ if (!(selection instanceof TextSelection)) {
15502
11856
  return null;
15503
11857
  }
15504
- const { $from } = selection;
15505
11858
  const linkMark = state.schema.marks.link;
15506
- const link = linkMark.isInSet($from.marks());
15507
- if (!link) {
11859
+ const { $from, $to } = selection;
11860
+ const selectionStartMarks = state.selection.empty ? $from.marks() : $from.nodeAfter?.marks ?? [];
11861
+ const targetHref = linkMark.isInSet(selectionStartMarks)?.attrs.href;
11862
+ if (!targetHref) {
11863
+ return null;
11864
+ }
11865
+ const isTargetLink = (node) => linkMark.isInSet(node.marks)?.attrs.href === targetHref;
11866
+ let linkStart = -1;
11867
+ let linkEnd = -1;
11868
+ let selectionValid = true;
11869
+ state.doc.nodesBetween($from.start(), $to.end(), (node, pos) => {
11870
+ if (!node.isText) {
11871
+ return true;
11872
+ }
11873
+ const nodeEnd = pos + node.nodeSize;
11874
+ if (isTargetLink(node)) {
11875
+ if (linkStart === -1) {
11876
+ linkStart = pos;
11877
+ }
11878
+ linkEnd = nodeEnd;
11879
+ } else if (nodeEnd > selection.from && pos < selection.to) {
11880
+ selectionValid = false;
11881
+ }
11882
+ return false;
11883
+ });
11884
+ if (!selectionValid || linkStart === -1) {
15508
11885
  return null;
15509
11886
  }
15510
- const textNode = $from.parent.childAfter($from.parentOffset);
15511
- const start = $from.pos - $from.parentOffset + textNode.offset;
15512
- const end = start + textNode.node.nodeSize;
11887
+ const linkText = state.doc.textBetween(linkStart, linkEnd, " ");
15513
11888
  return {
15514
- text: textNode.node.text,
15515
- href: link.attrs.href,
15516
- start,
15517
- end
11889
+ text: linkText,
11890
+ href: targetHref,
11891
+ start: linkStart,
11892
+ end: linkEnd
15518
11893
  };
15519
11894
  }
15520
11895
  insertLink(rte, href, text) {
@@ -15602,6 +11977,74 @@ class RteToolbarFeatureImpl extends RteFeatureImpl {
15602
11977
  }
15603
11978
  const RteToolbarFeature = featureFacade(RteToolbarFeatureImpl);
15604
11979
 
11980
+ const convertToView = (doc, ctx) => {
11981
+ const schema = ctx.config[impl].schema;
11982
+ const getMarkRank = (mark) => schema.marks[mark.type].rank;
11983
+ const marksEqual = (a, b) => {
11984
+ if (a.type !== b.type) return false;
11985
+ const attrsA = a.attrs;
11986
+ const attrsB = b.attrs;
11987
+ if (attrsA === attrsB) return true;
11988
+ /* v8 ignore next -- not currently reachable since all attrs are required @preserve */
11989
+ if (!attrsA || !attrsB) return false;
11990
+ const keysA = Object.keys(attrsA);
11991
+ const keysB = Object.keys(attrsB);
11992
+ /* v8 ignore next -- not currently reachable since all attrs are required @preserve */
11993
+ if (keysA.length !== keysB.length) return false;
11994
+ for (const key of keysA) {
11995
+ if (attrsA[key] !== attrsB[key]) return false;
11996
+ }
11997
+ return true;
11998
+ };
11999
+ const convertFragment = (nodes) => {
12000
+ const result = [];
12001
+ const active = [];
12002
+ const activeContents = [result];
12003
+ for (const node of nodes) {
12004
+ const nodeMarks = [...node.marks ?? []].sort(
12005
+ (a, b) => getMarkRank(a) - getMarkRank(b)
12006
+ );
12007
+ let keep = 0;
12008
+ let rendered = 0;
12009
+ while (keep < active.length && rendered < nodeMarks.length) {
12010
+ if (!marksEqual(active[keep], nodeMarks[rendered])) break;
12011
+ keep++;
12012
+ rendered++;
12013
+ }
12014
+ while (active.length > keep) {
12015
+ active.pop();
12016
+ activeContents.pop();
12017
+ }
12018
+ while (rendered < nodeMarks.length) {
12019
+ const mark = nodeMarks[rendered];
12020
+ const markContent = [];
12021
+ activeContents[activeContents.length - 1].push({
12022
+ type: "mark",
12023
+ mark,
12024
+ children: { type: "fragment", content: markContent, [impl]: ctx },
12025
+ [impl]: ctx
12026
+ });
12027
+ active.push(mark);
12028
+ activeContents.push(markContent);
12029
+ rendered++;
12030
+ }
12031
+ activeContents[activeContents.length - 1].push({
12032
+ type: "node",
12033
+ node,
12034
+ children: convertFragment(node.content ?? []),
12035
+ [impl]: ctx
12036
+ });
12037
+ }
12038
+ return { type: "fragment", content: result, [impl]: ctx };
12039
+ };
12040
+ return {
12041
+ type: "node",
12042
+ node: doc,
12043
+ children: convertFragment(doc.content),
12044
+ [impl]: ctx
12045
+ };
12046
+ };
12047
+
15605
12048
  class RteConfig {
15606
12049
  constructor(features) {
15607
12050
  this[impl] = new RteConfigImpl(features);
@@ -15609,6 +12052,9 @@ class RteConfig {
15609
12052
  instantiateEditor(options) {
15610
12053
  return new RteInstance(this, options);
15611
12054
  }
12055
+ instantiateView(document, options) {
12056
+ return convertToView(document, { config: this, options: { ...options } });
12057
+ }
15612
12058
  }
15613
12059
  class RteConfigImpl {
15614
12060
  constructor(featuresFacades) {
@@ -16016,7 +12462,7 @@ const RichTextEditorTemplate = (ctx) => {
16016
12462
  <div class="editor-viewport" ${ref("editorViewportElement")}>
16017
12463
  <div class="editor-scroll-area">
16018
12464
  <slot name="editor-start"></slot>
16019
- <div class="editor" ${ref("_editorEl")}></div>
12465
+ <div class="editor rich-text" ${ref("_editorEl")}></div>
16020
12466
  <div class="popovers"></div>
16021
12467
  <slot name="editor-end"></slot>
16022
12468
  </div>
@@ -16989,7 +13435,7 @@ const RteTextColorPickerFeature = featureFacade(
16989
13435
  RteTextColorPickerFeatureImpl
16990
13436
  );
16991
13437
 
16992
- const listCss = ".editor ul,.editor ol{padding-inline-start:1.5em}.editor li{list-style-position:inside}";
13438
+ const listCss = ".rich-text ul,.rich-text ol{padding-inline-start:1.5em}.rich-text li{list-style-position:inside}";
16993
13439
 
16994
13440
  const atStartOfNode = ($cursor) => $cursor.parentOffset === 0;
16995
13441
  const isEmpty = (node) => node.nodeSize === 2;
@@ -17639,7 +14085,7 @@ class RteInlineImageFeatureImpl extends RteFeatureImpl {
17639
14085
  return document.createTextNode("");
17640
14086
  }
17641
14087
  const attrs = { src: resolvedUrl, alt };
17642
- if (size) attrs.style = `max-width: ${size};`;
14088
+ if (size) attrs.style = `max-width: ${size}; height: auto;`;
17643
14089
  if (naturalWidth) attrs.width = naturalWidth;
17644
14090
  if (naturalHeight) attrs.height = naturalHeight;
17645
14091
  return ["img", attrs];