@bigbinary/neeto-editor-frontend 2.0.10 → 2.1.0-beta1

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