prosemirror-transform 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/index.cjs +254 -25
- package/dist/index.d.ts +105 -1
- package/dist/index.js +194 -2
- package/package.json +1 -1
- package/src/README.md +3 -0
- package/src/attr_step.ts +53 -0
- package/src/index.ts +2 -1
- package/src/mark_step.ts +96 -0
- package/src/transform.ts +27 -0
- package/dist/index.es.js +0 -1702
- package/dist/index.es.js.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/index.es.js
DELETED
|
@@ -1,1702 +0,0 @@
|
|
|
1
|
-
import { ReplaceError, Slice, Fragment, MarkType } from 'prosemirror-model';
|
|
2
|
-
|
|
3
|
-
// Mappable:: interface
|
|
4
|
-
// There are several things that positions can be mapped through.
|
|
5
|
-
// Such objects conform to this interface.
|
|
6
|
-
//
|
|
7
|
-
// map:: (pos: number, assoc: ?number) → number
|
|
8
|
-
// Map a position through this object. When given, `assoc` (should
|
|
9
|
-
// be -1 or 1, defaults to 1) determines with which side the
|
|
10
|
-
// position is associated, which determines in which direction to
|
|
11
|
-
// move when a chunk of content is inserted at the mapped position.
|
|
12
|
-
//
|
|
13
|
-
// mapResult:: (pos: number, assoc: ?number) → MapResult
|
|
14
|
-
// Map a position, and return an object containing additional
|
|
15
|
-
// information about the mapping. The result's `deleted` field tells
|
|
16
|
-
// you whether the position was deleted (completely enclosed in a
|
|
17
|
-
// replaced range) during the mapping. When content on only one side
|
|
18
|
-
// is deleted, the position itself is only considered deleted when
|
|
19
|
-
// `assoc` points in the direction of the deleted content.
|
|
20
|
-
|
|
21
|
-
// Recovery values encode a range index and an offset. They are
|
|
22
|
-
// represented as numbers, because tons of them will be created when
|
|
23
|
-
// mapping, for example, a large number of decorations. The number's
|
|
24
|
-
// lower 16 bits provide the index, the remaining bits the offset.
|
|
25
|
-
//
|
|
26
|
-
// Note: We intentionally don't use bit shift operators to en- and
|
|
27
|
-
// decode these, since those clip to 32 bits, which we might in rare
|
|
28
|
-
// cases want to overflow. A 64-bit float can represent 48-bit
|
|
29
|
-
// integers precisely.
|
|
30
|
-
|
|
31
|
-
var lower16 = 0xffff;
|
|
32
|
-
var factor16 = Math.pow(2, 16);
|
|
33
|
-
|
|
34
|
-
function makeRecover(index, offset) { return index + offset * factor16 }
|
|
35
|
-
function recoverIndex(value) { return value & lower16 }
|
|
36
|
-
function recoverOffset(value) { return (value - (value & lower16)) / factor16 }
|
|
37
|
-
|
|
38
|
-
// ::- An object representing a mapped position with extra
|
|
39
|
-
// information.
|
|
40
|
-
var MapResult = function MapResult(pos, deleted, recover) {
|
|
41
|
-
if ( deleted === void 0 ) deleted = false;
|
|
42
|
-
if ( recover === void 0 ) recover = null;
|
|
43
|
-
|
|
44
|
-
// :: number The mapped version of the position.
|
|
45
|
-
this.pos = pos;
|
|
46
|
-
// :: bool Tells you whether the position was deleted, that is,
|
|
47
|
-
// whether the step removed its surroundings from the document.
|
|
48
|
-
this.deleted = deleted;
|
|
49
|
-
this.recover = recover;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// :: class extends Mappable
|
|
53
|
-
// A map describing the deletions and insertions made by a step, which
|
|
54
|
-
// can be used to find the correspondence between positions in the
|
|
55
|
-
// pre-step version of a document and the same position in the
|
|
56
|
-
// post-step version.
|
|
57
|
-
var StepMap = function StepMap(ranges, inverted) {
|
|
58
|
-
if ( inverted === void 0 ) inverted = false;
|
|
59
|
-
|
|
60
|
-
if (!ranges.length && StepMap.empty) { return StepMap.empty }
|
|
61
|
-
this.ranges = ranges;
|
|
62
|
-
this.inverted = inverted;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
StepMap.prototype.recover = function recover (value) {
|
|
66
|
-
var diff = 0, index = recoverIndex(value);
|
|
67
|
-
if (!this.inverted) { for (var i = 0; i < index; i++)
|
|
68
|
-
{ diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1]; } }
|
|
69
|
-
return this.ranges[index * 3] + diff + recoverOffset(value)
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
// : (number, ?number) → MapResult
|
|
73
|
-
StepMap.prototype.mapResult = function mapResult (pos, assoc) {
|
|
74
|
-
if ( assoc === void 0 ) assoc = 1;
|
|
75
|
-
return this._map(pos, assoc, false) };
|
|
76
|
-
|
|
77
|
-
// : (number, ?number) → number
|
|
78
|
-
StepMap.prototype.map = function map (pos, assoc) {
|
|
79
|
-
if ( assoc === void 0 ) assoc = 1;
|
|
80
|
-
return this._map(pos, assoc, true) };
|
|
81
|
-
|
|
82
|
-
StepMap.prototype._map = function _map (pos, assoc, simple) {
|
|
83
|
-
var diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
84
|
-
for (var i = 0; i < this.ranges.length; i += 3) {
|
|
85
|
-
var start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
86
|
-
if (start > pos) { break }
|
|
87
|
-
var oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
|
|
88
|
-
if (pos <= end) {
|
|
89
|
-
var side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
|
|
90
|
-
var result = start + diff + (side < 0 ? 0 : newSize);
|
|
91
|
-
if (simple) { return result }
|
|
92
|
-
var recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
|
|
93
|
-
return new MapResult(result, assoc < 0 ? pos != start : pos != end, recover)
|
|
94
|
-
}
|
|
95
|
-
diff += newSize - oldSize;
|
|
96
|
-
}
|
|
97
|
-
return simple ? pos + diff : new MapResult(pos + diff)
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
StepMap.prototype.touches = function touches (pos, recover) {
|
|
101
|
-
var diff = 0, index = recoverIndex(recover);
|
|
102
|
-
var oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
103
|
-
for (var i = 0; i < this.ranges.length; i += 3) {
|
|
104
|
-
var start = this.ranges[i] - (this.inverted ? diff : 0);
|
|
105
|
-
if (start > pos) { break }
|
|
106
|
-
var oldSize = this.ranges[i + oldIndex], end = start + oldSize;
|
|
107
|
-
if (pos <= end && i == index * 3) { return true }
|
|
108
|
-
diff += this.ranges[i + newIndex] - oldSize;
|
|
109
|
-
}
|
|
110
|
-
return false
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
// :: ((oldStart: number, oldEnd: number, newStart: number, newEnd: number))
|
|
114
|
-
// Calls the given function on each of the changed ranges included in
|
|
115
|
-
// this map.
|
|
116
|
-
StepMap.prototype.forEach = function forEach (f) {
|
|
117
|
-
var oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
|
|
118
|
-
for (var i = 0, diff = 0; i < this.ranges.length; i += 3) {
|
|
119
|
-
var start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
|
|
120
|
-
var oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
|
|
121
|
-
f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
|
|
122
|
-
diff += newSize - oldSize;
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// :: () → StepMap
|
|
127
|
-
// Create an inverted version of this map. The result can be used to
|
|
128
|
-
// map positions in the post-step document to the pre-step document.
|
|
129
|
-
StepMap.prototype.invert = function invert () {
|
|
130
|
-
return new StepMap(this.ranges, !this.inverted)
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
StepMap.prototype.toString = function toString () {
|
|
134
|
-
return (this.inverted ? "-" : "") + JSON.stringify(this.ranges)
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
// :: (n: number) → StepMap
|
|
138
|
-
// Create a map that moves all positions by offset `n` (which may be
|
|
139
|
-
// negative). This can be useful when applying steps meant for a
|
|
140
|
-
// sub-document to a larger document, or vice-versa.
|
|
141
|
-
StepMap.offset = function offset (n) {
|
|
142
|
-
return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n])
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
// :: StepMap
|
|
146
|
-
// A StepMap that contains no changed ranges.
|
|
147
|
-
StepMap.empty = new StepMap([]);
|
|
148
|
-
|
|
149
|
-
// :: class extends Mappable
|
|
150
|
-
// A mapping represents a pipeline of zero or more [step
|
|
151
|
-
// maps](#transform.StepMap). It has special provisions for losslessly
|
|
152
|
-
// handling mapping positions through a series of steps in which some
|
|
153
|
-
// steps are inverted versions of earlier steps. (This comes up when
|
|
154
|
-
// ‘[rebasing](/docs/guide/#transform.rebasing)’ steps for
|
|
155
|
-
// collaboration or history management.)
|
|
156
|
-
var Mapping = function Mapping(maps, mirror, from, to) {
|
|
157
|
-
// :: [StepMap]
|
|
158
|
-
// The step maps in this mapping.
|
|
159
|
-
this.maps = maps || [];
|
|
160
|
-
// :: number
|
|
161
|
-
// The starting position in the `maps` array, used when `map` or
|
|
162
|
-
// `mapResult` is called.
|
|
163
|
-
this.from = from || 0;
|
|
164
|
-
// :: number
|
|
165
|
-
// The end position in the `maps` array.
|
|
166
|
-
this.to = to == null ? this.maps.length : to;
|
|
167
|
-
this.mirror = mirror;
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// :: (?number, ?number) → Mapping
|
|
171
|
-
// Create a mapping that maps only through a part of this one.
|
|
172
|
-
Mapping.prototype.slice = function slice (from, to) {
|
|
173
|
-
if ( from === void 0 ) from = 0;
|
|
174
|
-
if ( to === void 0 ) to = this.maps.length;
|
|
175
|
-
|
|
176
|
-
return new Mapping(this.maps, this.mirror, from, to)
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
Mapping.prototype.copy = function copy () {
|
|
180
|
-
return new Mapping(this.maps.slice(), this.mirror && this.mirror.slice(), this.from, this.to)
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
// :: (StepMap, ?number)
|
|
184
|
-
// Add a step map to the end of this mapping. If `mirrors` is
|
|
185
|
-
// given, it should be the index of the step map that is the mirror
|
|
186
|
-
// image of this one.
|
|
187
|
-
Mapping.prototype.appendMap = function appendMap (map, mirrors) {
|
|
188
|
-
this.to = this.maps.push(map);
|
|
189
|
-
if (mirrors != null) { this.setMirror(this.maps.length - 1, mirrors); }
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
// :: (Mapping)
|
|
193
|
-
// Add all the step maps in a given mapping to this one (preserving
|
|
194
|
-
// mirroring information).
|
|
195
|
-
Mapping.prototype.appendMapping = function appendMapping (mapping) {
|
|
196
|
-
for (var i = 0, startSize = this.maps.length; i < mapping.maps.length; i++) {
|
|
197
|
-
var mirr = mapping.getMirror(i);
|
|
198
|
-
this.appendMap(mapping.maps[i], mirr != null && mirr < i ? startSize + mirr : null);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
// :: (number) → ?number
|
|
203
|
-
// Finds the offset of the step map that mirrors the map at the
|
|
204
|
-
// given offset, in this mapping (as per the second argument to
|
|
205
|
-
// `appendMap`).
|
|
206
|
-
Mapping.prototype.getMirror = function getMirror (n) {
|
|
207
|
-
if (this.mirror) { for (var i = 0; i < this.mirror.length; i++)
|
|
208
|
-
{ if (this.mirror[i] == n) { return this.mirror[i + (i % 2 ? -1 : 1)] } } }
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
Mapping.prototype.setMirror = function setMirror (n, m) {
|
|
212
|
-
if (!this.mirror) { this.mirror = []; }
|
|
213
|
-
this.mirror.push(n, m);
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// :: (Mapping)
|
|
217
|
-
// Append the inverse of the given mapping to this one.
|
|
218
|
-
Mapping.prototype.appendMappingInverted = function appendMappingInverted (mapping) {
|
|
219
|
-
for (var i = mapping.maps.length - 1, totalSize = this.maps.length + mapping.maps.length; i >= 0; i--) {
|
|
220
|
-
var mirr = mapping.getMirror(i);
|
|
221
|
-
this.appendMap(mapping.maps[i].invert(), mirr != null && mirr > i ? totalSize - mirr - 1 : null);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
// :: () → Mapping
|
|
226
|
-
// Create an inverted version of this mapping.
|
|
227
|
-
Mapping.prototype.invert = function invert () {
|
|
228
|
-
var inverse = new Mapping;
|
|
229
|
-
inverse.appendMappingInverted(this);
|
|
230
|
-
return inverse
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
// : (number, ?number) → number
|
|
234
|
-
// Map a position through this mapping.
|
|
235
|
-
Mapping.prototype.map = function map (pos, assoc) {
|
|
236
|
-
if ( assoc === void 0 ) assoc = 1;
|
|
237
|
-
|
|
238
|
-
if (this.mirror) { return this._map(pos, assoc, true) }
|
|
239
|
-
for (var i = this.from; i < this.to; i++)
|
|
240
|
-
{ pos = this.maps[i].map(pos, assoc); }
|
|
241
|
-
return pos
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
// : (number, ?number) → MapResult
|
|
245
|
-
// Map a position through this mapping, returning a mapping
|
|
246
|
-
// result.
|
|
247
|
-
Mapping.prototype.mapResult = function mapResult (pos, assoc) {
|
|
248
|
-
if ( assoc === void 0 ) assoc = 1;
|
|
249
|
-
return this._map(pos, assoc, false) };
|
|
250
|
-
|
|
251
|
-
Mapping.prototype._map = function _map (pos, assoc, simple) {
|
|
252
|
-
var deleted = false;
|
|
253
|
-
|
|
254
|
-
for (var i = this.from; i < this.to; i++) {
|
|
255
|
-
var map = this.maps[i], result = map.mapResult(pos, assoc);
|
|
256
|
-
if (result.recover != null) {
|
|
257
|
-
var corr = this.getMirror(i);
|
|
258
|
-
if (corr != null && corr > i && corr < this.to) {
|
|
259
|
-
i = corr;
|
|
260
|
-
pos = this.maps[corr].recover(result.recover);
|
|
261
|
-
continue
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (result.deleted) { deleted = true; }
|
|
266
|
-
pos = result.pos;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return simple ? pos : new MapResult(pos, deleted)
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
function TransformError(message) {
|
|
273
|
-
var err = Error.call(this, message);
|
|
274
|
-
err.__proto__ = TransformError.prototype;
|
|
275
|
-
return err
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
TransformError.prototype = Object.create(Error.prototype);
|
|
279
|
-
TransformError.prototype.constructor = TransformError;
|
|
280
|
-
TransformError.prototype.name = "TransformError";
|
|
281
|
-
|
|
282
|
-
// ::- Abstraction to build up and track an array of
|
|
283
|
-
// [steps](#transform.Step) representing a document transformation.
|
|
284
|
-
//
|
|
285
|
-
// Most transforming methods return the `Transform` object itself, so
|
|
286
|
-
// that they can be chained.
|
|
287
|
-
var Transform = function Transform(doc) {
|
|
288
|
-
// :: Node
|
|
289
|
-
// The current document (the result of applying the steps in the
|
|
290
|
-
// transform).
|
|
291
|
-
this.doc = doc;
|
|
292
|
-
// :: [Step]
|
|
293
|
-
// The steps in this transform.
|
|
294
|
-
this.steps = [];
|
|
295
|
-
// :: [Node]
|
|
296
|
-
// The documents before each of the steps.
|
|
297
|
-
this.docs = [];
|
|
298
|
-
// :: Mapping
|
|
299
|
-
// A mapping with the maps for each of the steps in this transform.
|
|
300
|
-
this.mapping = new Mapping;
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
var prototypeAccessors$1 = { before: { configurable: true },docChanged: { configurable: true } };
|
|
304
|
-
|
|
305
|
-
// :: Node The starting document.
|
|
306
|
-
prototypeAccessors$1.before.get = function () { return this.docs.length ? this.docs[0] : this.doc };
|
|
307
|
-
|
|
308
|
-
// :: (step: Step) → this
|
|
309
|
-
// Apply a new step in this transform, saving the result. Throws an
|
|
310
|
-
// error when the step fails.
|
|
311
|
-
Transform.prototype.step = function step (object) {
|
|
312
|
-
var result = this.maybeStep(object);
|
|
313
|
-
if (result.failed) { throw new TransformError(result.failed) }
|
|
314
|
-
return this
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
// :: (Step) → StepResult
|
|
318
|
-
// Try to apply a step in this transformation, ignoring it if it
|
|
319
|
-
// fails. Returns the step result.
|
|
320
|
-
Transform.prototype.maybeStep = function maybeStep (step) {
|
|
321
|
-
var result = step.apply(this.doc);
|
|
322
|
-
if (!result.failed) { this.addStep(step, result.doc); }
|
|
323
|
-
return result
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
// :: bool
|
|
327
|
-
// True when the document has been changed (when there are any
|
|
328
|
-
// steps).
|
|
329
|
-
prototypeAccessors$1.docChanged.get = function () {
|
|
330
|
-
return this.steps.length > 0
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
Transform.prototype.addStep = function addStep (step, doc) {
|
|
334
|
-
this.docs.push(this.doc);
|
|
335
|
-
this.steps.push(step);
|
|
336
|
-
this.mapping.appendMap(step.getMap());
|
|
337
|
-
this.doc = doc;
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
Object.defineProperties( Transform.prototype, prototypeAccessors$1 );
|
|
341
|
-
|
|
342
|
-
function mustOverride() { throw new Error("Override me") }
|
|
343
|
-
|
|
344
|
-
var stepsByID = Object.create(null);
|
|
345
|
-
|
|
346
|
-
// ::- A step object represents an atomic change. It generally applies
|
|
347
|
-
// only to the document it was created for, since the positions
|
|
348
|
-
// stored in it will only make sense for that document.
|
|
349
|
-
//
|
|
350
|
-
// New steps are defined by creating classes that extend `Step`,
|
|
351
|
-
// overriding the `apply`, `invert`, `map`, `getMap` and `fromJSON`
|
|
352
|
-
// methods, and registering your class with a unique
|
|
353
|
-
// JSON-serialization identifier using
|
|
354
|
-
// [`Step.jsonID`](#transform.Step^jsonID).
|
|
355
|
-
var Step = function Step () {};
|
|
356
|
-
|
|
357
|
-
Step.prototype.apply = function apply (_doc) { return mustOverride() };
|
|
358
|
-
|
|
359
|
-
// :: () → StepMap
|
|
360
|
-
// Get the step map that represents the changes made by this step,
|
|
361
|
-
// and which can be used to transform between positions in the old
|
|
362
|
-
// and the new document.
|
|
363
|
-
Step.prototype.getMap = function getMap () { return StepMap.empty };
|
|
364
|
-
|
|
365
|
-
// :: (doc: Node) → Step
|
|
366
|
-
// Create an inverted version of this step. Needs the document as it
|
|
367
|
-
// was before the step as argument.
|
|
368
|
-
Step.prototype.invert = function invert (_doc) { return mustOverride() };
|
|
369
|
-
|
|
370
|
-
// :: (mapping: Mappable) → ?Step
|
|
371
|
-
// Map this step through a mappable thing, returning either a
|
|
372
|
-
// version of that step with its positions adjusted, or `null` if
|
|
373
|
-
// the step was entirely deleted by the mapping.
|
|
374
|
-
Step.prototype.map = function map (_mapping) { return mustOverride() };
|
|
375
|
-
|
|
376
|
-
// :: (other: Step) → ?Step
|
|
377
|
-
// Try to merge this step with another one, to be applied directly
|
|
378
|
-
// after it. Returns the merged step when possible, null if the
|
|
379
|
-
// steps can't be merged.
|
|
380
|
-
Step.prototype.merge = function merge (_other) { return null };
|
|
381
|
-
|
|
382
|
-
// :: () → Object
|
|
383
|
-
// Create a JSON-serializeable representation of this step. When
|
|
384
|
-
// defining this for a custom subclass, make sure the result object
|
|
385
|
-
// includes the step type's [JSON id](#transform.Step^jsonID) under
|
|
386
|
-
// the `stepType` property.
|
|
387
|
-
Step.prototype.toJSON = function toJSON () { return mustOverride() };
|
|
388
|
-
|
|
389
|
-
// :: (Schema, Object) → Step
|
|
390
|
-
// Deserialize a step from its JSON representation. Will call
|
|
391
|
-
// through to the step class' own implementation of this method.
|
|
392
|
-
Step.fromJSON = function fromJSON (schema, json) {
|
|
393
|
-
if (!json || !json.stepType) { throw new RangeError("Invalid input for Step.fromJSON") }
|
|
394
|
-
var type = stepsByID[json.stepType];
|
|
395
|
-
if (!type) { throw new RangeError(("No step type " + (json.stepType) + " defined")) }
|
|
396
|
-
return type.fromJSON(schema, json)
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
// :: (string, constructor<Step>)
|
|
400
|
-
// To be able to serialize steps to JSON, each step needs a string
|
|
401
|
-
// ID to attach to its JSON representation. Use this method to
|
|
402
|
-
// register an ID for your step classes. Try to pick something
|
|
403
|
-
// that's unlikely to clash with steps from other modules.
|
|
404
|
-
Step.jsonID = function jsonID (id, stepClass) {
|
|
405
|
-
if (id in stepsByID) { throw new RangeError("Duplicate use of step JSON ID " + id) }
|
|
406
|
-
stepsByID[id] = stepClass;
|
|
407
|
-
stepClass.prototype.jsonID = id;
|
|
408
|
-
return stepClass
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
// ::- The result of [applying](#transform.Step.apply) a step. Contains either a
|
|
412
|
-
// new document or a failure value.
|
|
413
|
-
var StepResult = function StepResult(doc, failed) {
|
|
414
|
-
// :: ?Node The transformed document.
|
|
415
|
-
this.doc = doc;
|
|
416
|
-
// :: ?string Text providing information about a failed step.
|
|
417
|
-
this.failed = failed;
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
// :: (Node) → StepResult
|
|
421
|
-
// Create a successful step result.
|
|
422
|
-
StepResult.ok = function ok (doc) { return new StepResult(doc, null) };
|
|
423
|
-
|
|
424
|
-
// :: (string) → StepResult
|
|
425
|
-
// Create a failed step result.
|
|
426
|
-
StepResult.fail = function fail (message) { return new StepResult(null, message) };
|
|
427
|
-
|
|
428
|
-
// :: (Node, number, number, Slice) → StepResult
|
|
429
|
-
// Call [`Node.replace`](#model.Node.replace) with the given
|
|
430
|
-
// arguments. Create a successful result if it succeeds, and a
|
|
431
|
-
// failed one if it throws a `ReplaceError`.
|
|
432
|
-
StepResult.fromReplace = function fromReplace (doc, from, to, slice) {
|
|
433
|
-
try {
|
|
434
|
-
return StepResult.ok(doc.replace(from, to, slice))
|
|
435
|
-
} catch (e) {
|
|
436
|
-
if (e instanceof ReplaceError) { return StepResult.fail(e.message) }
|
|
437
|
-
throw e
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
// ::- Replace a part of the document with a slice of new content.
|
|
442
|
-
var ReplaceStep = /*@__PURE__*/(function (Step) {
|
|
443
|
-
function ReplaceStep(from, to, slice, structure) {
|
|
444
|
-
Step.call(this);
|
|
445
|
-
// :: number
|
|
446
|
-
// The start position of the replaced range.
|
|
447
|
-
this.from = from;
|
|
448
|
-
// :: number
|
|
449
|
-
// The end position of the replaced range.
|
|
450
|
-
this.to = to;
|
|
451
|
-
// :: Slice
|
|
452
|
-
// The slice to insert.
|
|
453
|
-
this.slice = slice;
|
|
454
|
-
this.structure = !!structure;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
if ( Step ) ReplaceStep.__proto__ = Step;
|
|
458
|
-
ReplaceStep.prototype = Object.create( Step && Step.prototype );
|
|
459
|
-
ReplaceStep.prototype.constructor = ReplaceStep;
|
|
460
|
-
|
|
461
|
-
ReplaceStep.prototype.apply = function apply (doc) {
|
|
462
|
-
if (this.structure && contentBetween(doc, this.from, this.to))
|
|
463
|
-
{ return StepResult.fail("Structure replace would overwrite content") }
|
|
464
|
-
return StepResult.fromReplace(doc, this.from, this.to, this.slice)
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
ReplaceStep.prototype.getMap = function getMap () {
|
|
468
|
-
return new StepMap([this.from, this.to - this.from, this.slice.size])
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
ReplaceStep.prototype.invert = function invert (doc) {
|
|
472
|
-
return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to))
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
ReplaceStep.prototype.map = function map (mapping) {
|
|
476
|
-
var from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
477
|
-
if (from.deleted && to.deleted) { return null }
|
|
478
|
-
return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice)
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
ReplaceStep.prototype.merge = function merge (other) {
|
|
482
|
-
if (!(other instanceof ReplaceStep) || other.structure || this.structure) { return null }
|
|
483
|
-
|
|
484
|
-
if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
|
|
485
|
-
var slice = this.slice.size + other.slice.size == 0 ? Slice.empty
|
|
486
|
-
: new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
|
|
487
|
-
return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure)
|
|
488
|
-
} else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
|
|
489
|
-
var slice$1 = this.slice.size + other.slice.size == 0 ? Slice.empty
|
|
490
|
-
: new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
|
|
491
|
-
return new ReplaceStep(other.from, this.to, slice$1, this.structure)
|
|
492
|
-
} else {
|
|
493
|
-
return null
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
ReplaceStep.prototype.toJSON = function toJSON () {
|
|
498
|
-
var json = {stepType: "replace", from: this.from, to: this.to};
|
|
499
|
-
if (this.slice.size) { json.slice = this.slice.toJSON(); }
|
|
500
|
-
if (this.structure) { json.structure = true; }
|
|
501
|
-
return json
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
ReplaceStep.fromJSON = function fromJSON (schema, json) {
|
|
505
|
-
if (typeof json.from != "number" || typeof json.to != "number")
|
|
506
|
-
{ throw new RangeError("Invalid input for ReplaceStep.fromJSON") }
|
|
507
|
-
return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure)
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
return ReplaceStep;
|
|
511
|
-
}(Step));
|
|
512
|
-
|
|
513
|
-
Step.jsonID("replace", ReplaceStep);
|
|
514
|
-
|
|
515
|
-
// ::- Replace a part of the document with a slice of content, but
|
|
516
|
-
// preserve a range of the replaced content by moving it into the
|
|
517
|
-
// slice.
|
|
518
|
-
var ReplaceAroundStep = /*@__PURE__*/(function (Step) {
|
|
519
|
-
function ReplaceAroundStep(from, to, gapFrom, gapTo, slice, insert, structure) {
|
|
520
|
-
Step.call(this);
|
|
521
|
-
// :: number
|
|
522
|
-
// The start position of the replaced range.
|
|
523
|
-
this.from = from;
|
|
524
|
-
// :: number
|
|
525
|
-
// The end position of the replaced range.
|
|
526
|
-
this.to = to;
|
|
527
|
-
// :: number
|
|
528
|
-
// The start of preserved range.
|
|
529
|
-
this.gapFrom = gapFrom;
|
|
530
|
-
// :: number
|
|
531
|
-
// The end of preserved range.
|
|
532
|
-
this.gapTo = gapTo;
|
|
533
|
-
// :: Slice
|
|
534
|
-
// The slice to insert.
|
|
535
|
-
this.slice = slice;
|
|
536
|
-
// :: number
|
|
537
|
-
// The position in the slice where the preserved range should be
|
|
538
|
-
// inserted.
|
|
539
|
-
this.insert = insert;
|
|
540
|
-
this.structure = !!structure;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
if ( Step ) ReplaceAroundStep.__proto__ = Step;
|
|
544
|
-
ReplaceAroundStep.prototype = Object.create( Step && Step.prototype );
|
|
545
|
-
ReplaceAroundStep.prototype.constructor = ReplaceAroundStep;
|
|
546
|
-
|
|
547
|
-
ReplaceAroundStep.prototype.apply = function apply (doc) {
|
|
548
|
-
if (this.structure && (contentBetween(doc, this.from, this.gapFrom) ||
|
|
549
|
-
contentBetween(doc, this.gapTo, this.to)))
|
|
550
|
-
{ return StepResult.fail("Structure gap-replace would overwrite content") }
|
|
551
|
-
|
|
552
|
-
var gap = doc.slice(this.gapFrom, this.gapTo);
|
|
553
|
-
if (gap.openStart || gap.openEnd)
|
|
554
|
-
{ return StepResult.fail("Gap is not a flat range") }
|
|
555
|
-
var inserted = this.slice.insertAt(this.insert, gap.content);
|
|
556
|
-
if (!inserted) { return StepResult.fail("Content does not fit in gap") }
|
|
557
|
-
return StepResult.fromReplace(doc, this.from, this.to, inserted)
|
|
558
|
-
};
|
|
559
|
-
|
|
560
|
-
ReplaceAroundStep.prototype.getMap = function getMap () {
|
|
561
|
-
return new StepMap([this.from, this.gapFrom - this.from, this.insert,
|
|
562
|
-
this.gapTo, this.to - this.gapTo, this.slice.size - this.insert])
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
ReplaceAroundStep.prototype.invert = function invert (doc) {
|
|
566
|
-
var gap = this.gapTo - this.gapFrom;
|
|
567
|
-
return new ReplaceAroundStep(this.from, this.from + this.slice.size + gap,
|
|
568
|
-
this.from + this.insert, this.from + this.insert + gap,
|
|
569
|
-
doc.slice(this.from, this.to).removeBetween(this.gapFrom - this.from, this.gapTo - this.from),
|
|
570
|
-
this.gapFrom - this.from, this.structure)
|
|
571
|
-
};
|
|
572
|
-
|
|
573
|
-
ReplaceAroundStep.prototype.map = function map (mapping) {
|
|
574
|
-
var from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
575
|
-
var gapFrom = mapping.map(this.gapFrom, -1), gapTo = mapping.map(this.gapTo, 1);
|
|
576
|
-
if ((from.deleted && to.deleted) || gapFrom < from.pos || gapTo > to.pos) { return null }
|
|
577
|
-
return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure)
|
|
578
|
-
};
|
|
579
|
-
|
|
580
|
-
ReplaceAroundStep.prototype.toJSON = function toJSON () {
|
|
581
|
-
var json = {stepType: "replaceAround", from: this.from, to: this.to,
|
|
582
|
-
gapFrom: this.gapFrom, gapTo: this.gapTo, insert: this.insert};
|
|
583
|
-
if (this.slice.size) { json.slice = this.slice.toJSON(); }
|
|
584
|
-
if (this.structure) { json.structure = true; }
|
|
585
|
-
return json
|
|
586
|
-
};
|
|
587
|
-
|
|
588
|
-
ReplaceAroundStep.fromJSON = function fromJSON (schema, json) {
|
|
589
|
-
if (typeof json.from != "number" || typeof json.to != "number" ||
|
|
590
|
-
typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number")
|
|
591
|
-
{ throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON") }
|
|
592
|
-
return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo,
|
|
593
|
-
Slice.fromJSON(schema, json.slice), json.insert, !!json.structure)
|
|
594
|
-
};
|
|
595
|
-
|
|
596
|
-
return ReplaceAroundStep;
|
|
597
|
-
}(Step));
|
|
598
|
-
|
|
599
|
-
Step.jsonID("replaceAround", ReplaceAroundStep);
|
|
600
|
-
|
|
601
|
-
function contentBetween(doc, from, to) {
|
|
602
|
-
var $from = doc.resolve(from), dist = to - from, depth = $from.depth;
|
|
603
|
-
while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
|
|
604
|
-
depth--;
|
|
605
|
-
dist--;
|
|
606
|
-
}
|
|
607
|
-
if (dist > 0) {
|
|
608
|
-
var next = $from.node(depth).maybeChild($from.indexAfter(depth));
|
|
609
|
-
while (dist > 0) {
|
|
610
|
-
if (!next || next.isLeaf) { return true }
|
|
611
|
-
next = next.firstChild;
|
|
612
|
-
dist--;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
return false
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
function canCut(node, start, end) {
|
|
619
|
-
return (start == 0 || node.canReplace(start, node.childCount)) &&
|
|
620
|
-
(end == node.childCount || node.canReplace(0, end))
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
// :: (NodeRange) → ?number
|
|
624
|
-
// Try to find a target depth to which the content in the given range
|
|
625
|
-
// can be lifted. Will not go across
|
|
626
|
-
// [isolating](#model.NodeSpec.isolating) parent nodes.
|
|
627
|
-
function liftTarget(range) {
|
|
628
|
-
var parent = range.parent;
|
|
629
|
-
var content = parent.content.cutByIndex(range.startIndex, range.endIndex);
|
|
630
|
-
for (var depth = range.depth;; --depth) {
|
|
631
|
-
var node = range.$from.node(depth);
|
|
632
|
-
var index = range.$from.index(depth), endIndex = range.$to.indexAfter(depth);
|
|
633
|
-
if (depth < range.depth && node.canReplace(index, endIndex, content))
|
|
634
|
-
{ return depth }
|
|
635
|
-
if (depth == 0 || node.type.spec.isolating || !canCut(node, index, endIndex)) { break }
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
// :: (NodeRange, number) → this
|
|
640
|
-
// Split the content in the given range off from its parent, if there
|
|
641
|
-
// is sibling content before or after it, and move it up the tree to
|
|
642
|
-
// the depth specified by `target`. You'll probably want to use
|
|
643
|
-
// [`liftTarget`](#transform.liftTarget) to compute `target`, to make
|
|
644
|
-
// sure the lift is valid.
|
|
645
|
-
Transform.prototype.lift = function(range, target) {
|
|
646
|
-
var $from = range.$from;
|
|
647
|
-
var $to = range.$to;
|
|
648
|
-
var depth = range.depth;
|
|
649
|
-
|
|
650
|
-
var gapStart = $from.before(depth + 1), gapEnd = $to.after(depth + 1);
|
|
651
|
-
var start = gapStart, end = gapEnd;
|
|
652
|
-
|
|
653
|
-
var before = Fragment.empty, openStart = 0;
|
|
654
|
-
for (var d = depth, splitting = false; d > target; d--)
|
|
655
|
-
{ if (splitting || $from.index(d) > 0) {
|
|
656
|
-
splitting = true;
|
|
657
|
-
before = Fragment.from($from.node(d).copy(before));
|
|
658
|
-
openStart++;
|
|
659
|
-
} else {
|
|
660
|
-
start--;
|
|
661
|
-
} }
|
|
662
|
-
var after = Fragment.empty, openEnd = 0;
|
|
663
|
-
for (var d$1 = depth, splitting$1 = false; d$1 > target; d$1--)
|
|
664
|
-
{ if (splitting$1 || $to.after(d$1 + 1) < $to.end(d$1)) {
|
|
665
|
-
splitting$1 = true;
|
|
666
|
-
after = Fragment.from($to.node(d$1).copy(after));
|
|
667
|
-
openEnd++;
|
|
668
|
-
} else {
|
|
669
|
-
end++;
|
|
670
|
-
} }
|
|
671
|
-
|
|
672
|
-
return this.step(new ReplaceAroundStep(start, end, gapStart, gapEnd,
|
|
673
|
-
new Slice(before.append(after), openStart, openEnd),
|
|
674
|
-
before.size - openStart, true))
|
|
675
|
-
};
|
|
676
|
-
|
|
677
|
-
// :: (NodeRange, NodeType, ?Object, ?NodeRange) → ?[{type: NodeType, attrs: ?Object}]
|
|
678
|
-
// Try to find a valid way to wrap the content in the given range in a
|
|
679
|
-
// node of the given type. May introduce extra nodes around and inside
|
|
680
|
-
// the wrapper node, if necessary. Returns null if no valid wrapping
|
|
681
|
-
// could be found. When `innerRange` is given, that range's content is
|
|
682
|
-
// used as the content to fit into the wrapping, instead of the
|
|
683
|
-
// content of `range`.
|
|
684
|
-
function findWrapping(range, nodeType, attrs, innerRange) {
|
|
685
|
-
if ( innerRange === void 0 ) innerRange = range;
|
|
686
|
-
|
|
687
|
-
var around = findWrappingOutside(range, nodeType);
|
|
688
|
-
var inner = around && findWrappingInside(innerRange, nodeType);
|
|
689
|
-
if (!inner) { return null }
|
|
690
|
-
return around.map(withAttrs).concat({type: nodeType, attrs: attrs}).concat(inner.map(withAttrs))
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
function withAttrs(type) { return {type: type, attrs: null} }
|
|
694
|
-
|
|
695
|
-
function findWrappingOutside(range, type) {
|
|
696
|
-
var parent = range.parent;
|
|
697
|
-
var startIndex = range.startIndex;
|
|
698
|
-
var endIndex = range.endIndex;
|
|
699
|
-
var around = parent.contentMatchAt(startIndex).findWrapping(type);
|
|
700
|
-
if (!around) { return null }
|
|
701
|
-
var outer = around.length ? around[0] : type;
|
|
702
|
-
return parent.canReplaceWith(startIndex, endIndex, outer) ? around : null
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
function findWrappingInside(range, type) {
|
|
706
|
-
var parent = range.parent;
|
|
707
|
-
var startIndex = range.startIndex;
|
|
708
|
-
var endIndex = range.endIndex;
|
|
709
|
-
var inner = parent.child(startIndex);
|
|
710
|
-
var inside = type.contentMatch.findWrapping(inner.type);
|
|
711
|
-
if (!inside) { return null }
|
|
712
|
-
var lastType = inside.length ? inside[inside.length - 1] : type;
|
|
713
|
-
var innerMatch = lastType.contentMatch;
|
|
714
|
-
for (var i = startIndex; innerMatch && i < endIndex; i++)
|
|
715
|
-
{ innerMatch = innerMatch.matchType(parent.child(i).type); }
|
|
716
|
-
if (!innerMatch || !innerMatch.validEnd) { return null }
|
|
717
|
-
return inside
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
// :: (NodeRange, [{type: NodeType, attrs: ?Object}]) → this
|
|
721
|
-
// Wrap the given [range](#model.NodeRange) in the given set of wrappers.
|
|
722
|
-
// The wrappers are assumed to be valid in this position, and should
|
|
723
|
-
// probably be computed with [`findWrapping`](#transform.findWrapping).
|
|
724
|
-
Transform.prototype.wrap = function(range, wrappers) {
|
|
725
|
-
var content = Fragment.empty;
|
|
726
|
-
for (var i = wrappers.length - 1; i >= 0; i--) {
|
|
727
|
-
if (content.size) {
|
|
728
|
-
var match = wrappers[i].type.contentMatch.matchFragment(content);
|
|
729
|
-
if (!match || !match.validEnd)
|
|
730
|
-
{ throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper") }
|
|
731
|
-
}
|
|
732
|
-
content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
var start = range.start, end = range.end;
|
|
736
|
-
return this.step(new ReplaceAroundStep(start, end, start, end, new Slice(content, 0, 0), wrappers.length, true))
|
|
737
|
-
};
|
|
738
|
-
|
|
739
|
-
// :: (number, ?number, NodeType, ?Object) → this
|
|
740
|
-
// Set the type of all textblocks (partly) between `from` and `to` to
|
|
741
|
-
// the given node type with the given attributes.
|
|
742
|
-
Transform.prototype.setBlockType = function(from, to, type, attrs) {
|
|
743
|
-
var this$1$1 = this;
|
|
744
|
-
if ( to === void 0 ) to = from;
|
|
745
|
-
|
|
746
|
-
if (!type.isTextblock) { throw new RangeError("Type given to setBlockType should be a textblock") }
|
|
747
|
-
var mapFrom = this.steps.length;
|
|
748
|
-
this.doc.nodesBetween(from, to, function (node, pos) {
|
|
749
|
-
if (node.isTextblock && !node.hasMarkup(type, attrs) && canChangeType(this$1$1.doc, this$1$1.mapping.slice(mapFrom).map(pos), type)) {
|
|
750
|
-
// Ensure all markup that isn't allowed in the new node type is cleared
|
|
751
|
-
this$1$1.clearIncompatible(this$1$1.mapping.slice(mapFrom).map(pos, 1), type);
|
|
752
|
-
var mapping = this$1$1.mapping.slice(mapFrom);
|
|
753
|
-
var startM = mapping.map(pos, 1), endM = mapping.map(pos + node.nodeSize, 1);
|
|
754
|
-
this$1$1.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1,
|
|
755
|
-
new Slice(Fragment.from(type.create(attrs, null, node.marks)), 0, 0), 1, true));
|
|
756
|
-
return false
|
|
757
|
-
}
|
|
758
|
-
});
|
|
759
|
-
return this
|
|
760
|
-
};
|
|
761
|
-
|
|
762
|
-
function canChangeType(doc, pos, type) {
|
|
763
|
-
var $pos = doc.resolve(pos), index = $pos.index();
|
|
764
|
-
return $pos.parent.canReplaceWith(index, index + 1, type)
|
|
765
|
-
}
|
|
766
|
-
|
|
767
|
-
// :: (number, ?NodeType, ?Object, ?[Mark]) → this
|
|
768
|
-
// Change the type, attributes, and/or marks of the node at `pos`.
|
|
769
|
-
// When `type` isn't given, the existing node type is preserved,
|
|
770
|
-
Transform.prototype.setNodeMarkup = function(pos, type, attrs, marks) {
|
|
771
|
-
var node = this.doc.nodeAt(pos);
|
|
772
|
-
if (!node) { throw new RangeError("No node at given position") }
|
|
773
|
-
if (!type) { type = node.type; }
|
|
774
|
-
var newNode = type.create(attrs, null, marks || node.marks);
|
|
775
|
-
if (node.isLeaf)
|
|
776
|
-
{ return this.replaceWith(pos, pos + node.nodeSize, newNode) }
|
|
777
|
-
|
|
778
|
-
if (!type.validContent(node.content))
|
|
779
|
-
{ throw new RangeError("Invalid content for node type " + type.name) }
|
|
780
|
-
|
|
781
|
-
return this.step(new ReplaceAroundStep(pos, pos + node.nodeSize, pos + 1, pos + node.nodeSize - 1,
|
|
782
|
-
new Slice(Fragment.from(newNode), 0, 0), 1, true))
|
|
783
|
-
};
|
|
784
|
-
|
|
785
|
-
// :: (Node, number, number, ?[?{type: NodeType, attrs: ?Object}]) → bool
|
|
786
|
-
// Check whether splitting at the given position is allowed.
|
|
787
|
-
function canSplit(doc, pos, depth, typesAfter) {
|
|
788
|
-
if ( depth === void 0 ) depth = 1;
|
|
789
|
-
|
|
790
|
-
var $pos = doc.resolve(pos), base = $pos.depth - depth;
|
|
791
|
-
var innerType = (typesAfter && typesAfter[typesAfter.length - 1]) || $pos.parent;
|
|
792
|
-
if (base < 0 || $pos.parent.type.spec.isolating ||
|
|
793
|
-
!$pos.parent.canReplace($pos.index(), $pos.parent.childCount) ||
|
|
794
|
-
!innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount)))
|
|
795
|
-
{ return false }
|
|
796
|
-
for (var d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) {
|
|
797
|
-
var node = $pos.node(d), index$1 = $pos.index(d);
|
|
798
|
-
if (node.type.spec.isolating) { return false }
|
|
799
|
-
var rest = node.content.cutByIndex(index$1, node.childCount);
|
|
800
|
-
var after = (typesAfter && typesAfter[i]) || node;
|
|
801
|
-
if (after != node) { rest = rest.replaceChild(0, after.type.create(after.attrs)); }
|
|
802
|
-
if (!node.canReplace(index$1 + 1, node.childCount) || !after.type.validContent(rest))
|
|
803
|
-
{ return false }
|
|
804
|
-
}
|
|
805
|
-
var index = $pos.indexAfter(base);
|
|
806
|
-
var baseType = typesAfter && typesAfter[0];
|
|
807
|
-
return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type)
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
// :: (number, ?number, ?[?{type: NodeType, attrs: ?Object}]) → this
|
|
811
|
-
// Split the node at the given position, and optionally, if `depth` is
|
|
812
|
-
// greater than one, any number of nodes above that. By default, the
|
|
813
|
-
// parts split off will inherit the node type of the original node.
|
|
814
|
-
// This can be changed by passing an array of types and attributes to
|
|
815
|
-
// use after the split.
|
|
816
|
-
Transform.prototype.split = function(pos, depth, typesAfter) {
|
|
817
|
-
if ( depth === void 0 ) depth = 1;
|
|
818
|
-
|
|
819
|
-
var $pos = this.doc.resolve(pos), before = Fragment.empty, after = Fragment.empty;
|
|
820
|
-
for (var d = $pos.depth, e = $pos.depth - depth, i = depth - 1; d > e; d--, i--) {
|
|
821
|
-
before = Fragment.from($pos.node(d).copy(before));
|
|
822
|
-
var typeAfter = typesAfter && typesAfter[i];
|
|
823
|
-
after = Fragment.from(typeAfter ? typeAfter.type.create(typeAfter.attrs, after) : $pos.node(d).copy(after));
|
|
824
|
-
}
|
|
825
|
-
return this.step(new ReplaceStep(pos, pos, new Slice(before.append(after), depth, depth), true))
|
|
826
|
-
};
|
|
827
|
-
|
|
828
|
-
// :: (Node, number) → bool
|
|
829
|
-
// Test whether the blocks before and after a given position can be
|
|
830
|
-
// joined.
|
|
831
|
-
function canJoin(doc, pos) {
|
|
832
|
-
var $pos = doc.resolve(pos), index = $pos.index();
|
|
833
|
-
return joinable($pos.nodeBefore, $pos.nodeAfter) &&
|
|
834
|
-
$pos.parent.canReplace(index, index + 1)
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
function joinable(a, b) {
|
|
838
|
-
return a && b && !a.isLeaf && a.canAppend(b)
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
// :: (Node, number, ?number) → ?number
|
|
842
|
-
// Find an ancestor of the given position that can be joined to the
|
|
843
|
-
// block before (or after if `dir` is positive). Returns the joinable
|
|
844
|
-
// point, if any.
|
|
845
|
-
function joinPoint(doc, pos, dir) {
|
|
846
|
-
if ( dir === void 0 ) dir = -1;
|
|
847
|
-
|
|
848
|
-
var $pos = doc.resolve(pos);
|
|
849
|
-
for (var d = $pos.depth;; d--) {
|
|
850
|
-
var before = (void 0), after = (void 0), index = $pos.index(d);
|
|
851
|
-
if (d == $pos.depth) {
|
|
852
|
-
before = $pos.nodeBefore;
|
|
853
|
-
after = $pos.nodeAfter;
|
|
854
|
-
} else if (dir > 0) {
|
|
855
|
-
before = $pos.node(d + 1);
|
|
856
|
-
index++;
|
|
857
|
-
after = $pos.node(d).maybeChild(index);
|
|
858
|
-
} else {
|
|
859
|
-
before = $pos.node(d).maybeChild(index - 1);
|
|
860
|
-
after = $pos.node(d + 1);
|
|
861
|
-
}
|
|
862
|
-
if (before && !before.isTextblock && joinable(before, after) &&
|
|
863
|
-
$pos.node(d).canReplace(index, index + 1)) { return pos }
|
|
864
|
-
if (d == 0) { break }
|
|
865
|
-
pos = dir < 0 ? $pos.before(d) : $pos.after(d);
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
// :: (number, ?number) → this
|
|
870
|
-
// Join the blocks around the given position. If depth is 2, their
|
|
871
|
-
// last and first siblings are also joined, and so on.
|
|
872
|
-
Transform.prototype.join = function(pos, depth) {
|
|
873
|
-
if ( depth === void 0 ) depth = 1;
|
|
874
|
-
|
|
875
|
-
var step = new ReplaceStep(pos - depth, pos + depth, Slice.empty, true);
|
|
876
|
-
return this.step(step)
|
|
877
|
-
};
|
|
878
|
-
|
|
879
|
-
// :: (Node, number, NodeType) → ?number
|
|
880
|
-
// Try to find a point where a node of the given type can be inserted
|
|
881
|
-
// near `pos`, by searching up the node hierarchy when `pos` itself
|
|
882
|
-
// isn't a valid place but is at the start or end of a node. Return
|
|
883
|
-
// null if no position was found.
|
|
884
|
-
function insertPoint(doc, pos, nodeType) {
|
|
885
|
-
var $pos = doc.resolve(pos);
|
|
886
|
-
if ($pos.parent.canReplaceWith($pos.index(), $pos.index(), nodeType)) { return pos }
|
|
887
|
-
|
|
888
|
-
if ($pos.parentOffset == 0)
|
|
889
|
-
{ for (var d = $pos.depth - 1; d >= 0; d--) {
|
|
890
|
-
var index = $pos.index(d);
|
|
891
|
-
if ($pos.node(d).canReplaceWith(index, index, nodeType)) { return $pos.before(d + 1) }
|
|
892
|
-
if (index > 0) { return null }
|
|
893
|
-
} }
|
|
894
|
-
if ($pos.parentOffset == $pos.parent.content.size)
|
|
895
|
-
{ for (var d$1 = $pos.depth - 1; d$1 >= 0; d$1--) {
|
|
896
|
-
var index$1 = $pos.indexAfter(d$1);
|
|
897
|
-
if ($pos.node(d$1).canReplaceWith(index$1, index$1, nodeType)) { return $pos.after(d$1 + 1) }
|
|
898
|
-
if (index$1 < $pos.node(d$1).childCount) { return null }
|
|
899
|
-
} }
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
// :: (Node, number, Slice) → ?number
|
|
903
|
-
// Finds a position at or around the given position where the given
|
|
904
|
-
// slice can be inserted. Will look at parent nodes' nearest boundary
|
|
905
|
-
// and try there, even if the original position wasn't directly at the
|
|
906
|
-
// start or end of that node. Returns null when no position was found.
|
|
907
|
-
function dropPoint(doc, pos, slice) {
|
|
908
|
-
var $pos = doc.resolve(pos);
|
|
909
|
-
if (!slice.content.size) { return pos }
|
|
910
|
-
var content = slice.content;
|
|
911
|
-
for (var i = 0; i < slice.openStart; i++) { content = content.firstChild.content; }
|
|
912
|
-
for (var pass = 1; pass <= (slice.openStart == 0 && slice.size ? 2 : 1); pass++) {
|
|
913
|
-
for (var d = $pos.depth; d >= 0; d--) {
|
|
914
|
-
var bias = d == $pos.depth ? 0 : $pos.pos <= ($pos.start(d + 1) + $pos.end(d + 1)) / 2 ? -1 : 1;
|
|
915
|
-
var insertPos = $pos.index(d) + (bias > 0 ? 1 : 0);
|
|
916
|
-
var parent = $pos.node(d), fits = false;
|
|
917
|
-
if (pass == 1) {
|
|
918
|
-
fits = parent.canReplace(insertPos, insertPos, content);
|
|
919
|
-
} else {
|
|
920
|
-
var wrapping = parent.contentMatchAt(insertPos).findWrapping(content.firstChild.type);
|
|
921
|
-
fits = wrapping && parent.canReplaceWith(insertPos, insertPos, wrapping[0]);
|
|
922
|
-
}
|
|
923
|
-
if (fits)
|
|
924
|
-
{ return bias == 0 ? $pos.pos : bias < 0 ? $pos.before(d + 1) : $pos.after(d + 1) }
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
return null
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
function mapFragment(fragment, f, parent) {
|
|
931
|
-
var mapped = [];
|
|
932
|
-
for (var i = 0; i < fragment.childCount; i++) {
|
|
933
|
-
var child = fragment.child(i);
|
|
934
|
-
if (child.content.size) { child = child.copy(mapFragment(child.content, f, child)); }
|
|
935
|
-
if (child.isInline) { child = f(child, parent, i); }
|
|
936
|
-
mapped.push(child);
|
|
937
|
-
}
|
|
938
|
-
return Fragment.fromArray(mapped)
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
// ::- Add a mark to all inline content between two positions.
|
|
942
|
-
var AddMarkStep = /*@__PURE__*/(function (Step) {
|
|
943
|
-
function AddMarkStep(from, to, mark) {
|
|
944
|
-
Step.call(this);
|
|
945
|
-
// :: number
|
|
946
|
-
// The start of the marked range.
|
|
947
|
-
this.from = from;
|
|
948
|
-
// :: number
|
|
949
|
-
// The end of the marked range.
|
|
950
|
-
this.to = to;
|
|
951
|
-
// :: Mark
|
|
952
|
-
// The mark to add.
|
|
953
|
-
this.mark = mark;
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
if ( Step ) AddMarkStep.__proto__ = Step;
|
|
957
|
-
AddMarkStep.prototype = Object.create( Step && Step.prototype );
|
|
958
|
-
AddMarkStep.prototype.constructor = AddMarkStep;
|
|
959
|
-
|
|
960
|
-
AddMarkStep.prototype.apply = function apply (doc) {
|
|
961
|
-
var this$1$1 = this;
|
|
962
|
-
|
|
963
|
-
var oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from);
|
|
964
|
-
var parent = $from.node($from.sharedDepth(this.to));
|
|
965
|
-
var slice = new Slice(mapFragment(oldSlice.content, function (node, parent) {
|
|
966
|
-
if (!node.isAtom || !parent.type.allowsMarkType(this$1$1.mark.type)) { return node }
|
|
967
|
-
return node.mark(this$1$1.mark.addToSet(node.marks))
|
|
968
|
-
}, parent), oldSlice.openStart, oldSlice.openEnd);
|
|
969
|
-
return StepResult.fromReplace(doc, this.from, this.to, slice)
|
|
970
|
-
};
|
|
971
|
-
|
|
972
|
-
AddMarkStep.prototype.invert = function invert () {
|
|
973
|
-
return new RemoveMarkStep(this.from, this.to, this.mark)
|
|
974
|
-
};
|
|
975
|
-
|
|
976
|
-
AddMarkStep.prototype.map = function map (mapping) {
|
|
977
|
-
var from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
978
|
-
if (from.deleted && to.deleted || from.pos >= to.pos) { return null }
|
|
979
|
-
return new AddMarkStep(from.pos, to.pos, this.mark)
|
|
980
|
-
};
|
|
981
|
-
|
|
982
|
-
AddMarkStep.prototype.merge = function merge (other) {
|
|
983
|
-
if (other instanceof AddMarkStep &&
|
|
984
|
-
other.mark.eq(this.mark) &&
|
|
985
|
-
this.from <= other.to && this.to >= other.from)
|
|
986
|
-
{ return new AddMarkStep(Math.min(this.from, other.from),
|
|
987
|
-
Math.max(this.to, other.to), this.mark) }
|
|
988
|
-
};
|
|
989
|
-
|
|
990
|
-
AddMarkStep.prototype.toJSON = function toJSON () {
|
|
991
|
-
return {stepType: "addMark", mark: this.mark.toJSON(),
|
|
992
|
-
from: this.from, to: this.to}
|
|
993
|
-
};
|
|
994
|
-
|
|
995
|
-
AddMarkStep.fromJSON = function fromJSON (schema, json) {
|
|
996
|
-
if (typeof json.from != "number" || typeof json.to != "number")
|
|
997
|
-
{ throw new RangeError("Invalid input for AddMarkStep.fromJSON") }
|
|
998
|
-
return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark))
|
|
999
|
-
};
|
|
1000
|
-
|
|
1001
|
-
return AddMarkStep;
|
|
1002
|
-
}(Step));
|
|
1003
|
-
|
|
1004
|
-
Step.jsonID("addMark", AddMarkStep);
|
|
1005
|
-
|
|
1006
|
-
// ::- Remove a mark from all inline content between two positions.
|
|
1007
|
-
var RemoveMarkStep = /*@__PURE__*/(function (Step) {
|
|
1008
|
-
function RemoveMarkStep(from, to, mark) {
|
|
1009
|
-
Step.call(this);
|
|
1010
|
-
// :: number
|
|
1011
|
-
// The start of the unmarked range.
|
|
1012
|
-
this.from = from;
|
|
1013
|
-
// :: number
|
|
1014
|
-
// The end of the unmarked range.
|
|
1015
|
-
this.to = to;
|
|
1016
|
-
// :: Mark
|
|
1017
|
-
// The mark to remove.
|
|
1018
|
-
this.mark = mark;
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
if ( Step ) RemoveMarkStep.__proto__ = Step;
|
|
1022
|
-
RemoveMarkStep.prototype = Object.create( Step && Step.prototype );
|
|
1023
|
-
RemoveMarkStep.prototype.constructor = RemoveMarkStep;
|
|
1024
|
-
|
|
1025
|
-
RemoveMarkStep.prototype.apply = function apply (doc) {
|
|
1026
|
-
var this$1$1 = this;
|
|
1027
|
-
|
|
1028
|
-
var oldSlice = doc.slice(this.from, this.to);
|
|
1029
|
-
var slice = new Slice(mapFragment(oldSlice.content, function (node) {
|
|
1030
|
-
return node.mark(this$1$1.mark.removeFromSet(node.marks))
|
|
1031
|
-
}), oldSlice.openStart, oldSlice.openEnd);
|
|
1032
|
-
return StepResult.fromReplace(doc, this.from, this.to, slice)
|
|
1033
|
-
};
|
|
1034
|
-
|
|
1035
|
-
RemoveMarkStep.prototype.invert = function invert () {
|
|
1036
|
-
return new AddMarkStep(this.from, this.to, this.mark)
|
|
1037
|
-
};
|
|
1038
|
-
|
|
1039
|
-
RemoveMarkStep.prototype.map = function map (mapping) {
|
|
1040
|
-
var from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
|
|
1041
|
-
if (from.deleted && to.deleted || from.pos >= to.pos) { return null }
|
|
1042
|
-
return new RemoveMarkStep(from.pos, to.pos, this.mark)
|
|
1043
|
-
};
|
|
1044
|
-
|
|
1045
|
-
RemoveMarkStep.prototype.merge = function merge (other) {
|
|
1046
|
-
if (other instanceof RemoveMarkStep &&
|
|
1047
|
-
other.mark.eq(this.mark) &&
|
|
1048
|
-
this.from <= other.to && this.to >= other.from)
|
|
1049
|
-
{ return new RemoveMarkStep(Math.min(this.from, other.from),
|
|
1050
|
-
Math.max(this.to, other.to), this.mark) }
|
|
1051
|
-
};
|
|
1052
|
-
|
|
1053
|
-
RemoveMarkStep.prototype.toJSON = function toJSON () {
|
|
1054
|
-
return {stepType: "removeMark", mark: this.mark.toJSON(),
|
|
1055
|
-
from: this.from, to: this.to}
|
|
1056
|
-
};
|
|
1057
|
-
|
|
1058
|
-
RemoveMarkStep.fromJSON = function fromJSON (schema, json) {
|
|
1059
|
-
if (typeof json.from != "number" || typeof json.to != "number")
|
|
1060
|
-
{ throw new RangeError("Invalid input for RemoveMarkStep.fromJSON") }
|
|
1061
|
-
return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark))
|
|
1062
|
-
};
|
|
1063
|
-
|
|
1064
|
-
return RemoveMarkStep;
|
|
1065
|
-
}(Step));
|
|
1066
|
-
|
|
1067
|
-
Step.jsonID("removeMark", RemoveMarkStep);
|
|
1068
|
-
|
|
1069
|
-
// :: (number, number, Mark) → this
|
|
1070
|
-
// Add the given mark to the inline content between `from` and `to`.
|
|
1071
|
-
Transform.prototype.addMark = function(from, to, mark) {
|
|
1072
|
-
var this$1$1 = this;
|
|
1073
|
-
|
|
1074
|
-
var removed = [], added = [], removing = null, adding = null;
|
|
1075
|
-
this.doc.nodesBetween(from, to, function (node, pos, parent) {
|
|
1076
|
-
if (!node.isInline) { return }
|
|
1077
|
-
var marks = node.marks;
|
|
1078
|
-
if (!mark.isInSet(marks) && parent.type.allowsMarkType(mark.type)) {
|
|
1079
|
-
var start = Math.max(pos, from), end = Math.min(pos + node.nodeSize, to);
|
|
1080
|
-
var newSet = mark.addToSet(marks);
|
|
1081
|
-
|
|
1082
|
-
for (var i = 0; i < marks.length; i++) {
|
|
1083
|
-
if (!marks[i].isInSet(newSet)) {
|
|
1084
|
-
if (removing && removing.to == start && removing.mark.eq(marks[i]))
|
|
1085
|
-
{ removing.to = end; }
|
|
1086
|
-
else
|
|
1087
|
-
{ removed.push(removing = new RemoveMarkStep(start, end, marks[i])); }
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
if (adding && adding.to == start)
|
|
1092
|
-
{ adding.to = end; }
|
|
1093
|
-
else
|
|
1094
|
-
{ added.push(adding = new AddMarkStep(start, end, mark)); }
|
|
1095
|
-
}
|
|
1096
|
-
});
|
|
1097
|
-
|
|
1098
|
-
removed.forEach(function (s) { return this$1$1.step(s); });
|
|
1099
|
-
added.forEach(function (s) { return this$1$1.step(s); });
|
|
1100
|
-
return this
|
|
1101
|
-
};
|
|
1102
|
-
|
|
1103
|
-
// :: (number, number, ?union<Mark, MarkType>) → this
|
|
1104
|
-
// Remove marks from inline nodes between `from` and `to`. When `mark`
|
|
1105
|
-
// is a single mark, remove precisely that mark. When it is a mark type,
|
|
1106
|
-
// remove all marks of that type. When it is null, remove all marks of
|
|
1107
|
-
// any type.
|
|
1108
|
-
Transform.prototype.removeMark = function(from, to, mark) {
|
|
1109
|
-
var this$1$1 = this;
|
|
1110
|
-
if ( mark === void 0 ) mark = null;
|
|
1111
|
-
|
|
1112
|
-
var matched = [], step = 0;
|
|
1113
|
-
this.doc.nodesBetween(from, to, function (node, pos) {
|
|
1114
|
-
if (!node.isInline) { return }
|
|
1115
|
-
step++;
|
|
1116
|
-
var toRemove = null;
|
|
1117
|
-
if (mark instanceof MarkType) {
|
|
1118
|
-
var set = node.marks, found;
|
|
1119
|
-
while (found = mark.isInSet(set)) {
|
|
1120
|
-
(toRemove || (toRemove = [])).push(found);
|
|
1121
|
-
set = found.removeFromSet(set);
|
|
1122
|
-
}
|
|
1123
|
-
} else if (mark) {
|
|
1124
|
-
if (mark.isInSet(node.marks)) { toRemove = [mark]; }
|
|
1125
|
-
} else {
|
|
1126
|
-
toRemove = node.marks;
|
|
1127
|
-
}
|
|
1128
|
-
if (toRemove && toRemove.length) {
|
|
1129
|
-
var end = Math.min(pos + node.nodeSize, to);
|
|
1130
|
-
for (var i = 0; i < toRemove.length; i++) {
|
|
1131
|
-
var style = toRemove[i], found$1 = (void 0);
|
|
1132
|
-
for (var j = 0; j < matched.length; j++) {
|
|
1133
|
-
var m = matched[j];
|
|
1134
|
-
if (m.step == step - 1 && style.eq(matched[j].style)) { found$1 = m; }
|
|
1135
|
-
}
|
|
1136
|
-
if (found$1) {
|
|
1137
|
-
found$1.to = end;
|
|
1138
|
-
found$1.step = step;
|
|
1139
|
-
} else {
|
|
1140
|
-
matched.push({style: style, from: Math.max(pos, from), to: end, step: step});
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
});
|
|
1145
|
-
matched.forEach(function (m) { return this$1$1.step(new RemoveMarkStep(m.from, m.to, m.style)); });
|
|
1146
|
-
return this
|
|
1147
|
-
};
|
|
1148
|
-
|
|
1149
|
-
// :: (number, NodeType, ?ContentMatch) → this
|
|
1150
|
-
// Removes all marks and nodes from the content of the node at `pos`
|
|
1151
|
-
// that don't match the given new parent node type. Accepts an
|
|
1152
|
-
// optional starting [content match](#model.ContentMatch) as third
|
|
1153
|
-
// argument.
|
|
1154
|
-
Transform.prototype.clearIncompatible = function(pos, parentType, match) {
|
|
1155
|
-
if ( match === void 0 ) match = parentType.contentMatch;
|
|
1156
|
-
|
|
1157
|
-
var node = this.doc.nodeAt(pos);
|
|
1158
|
-
var delSteps = [], cur = pos + 1;
|
|
1159
|
-
for (var i = 0; i < node.childCount; i++) {
|
|
1160
|
-
var child = node.child(i), end = cur + child.nodeSize;
|
|
1161
|
-
var allowed = match.matchType(child.type, child.attrs);
|
|
1162
|
-
if (!allowed) {
|
|
1163
|
-
delSteps.push(new ReplaceStep(cur, end, Slice.empty));
|
|
1164
|
-
} else {
|
|
1165
|
-
match = allowed;
|
|
1166
|
-
for (var j = 0; j < child.marks.length; j++) { if (!parentType.allowsMarkType(child.marks[j].type))
|
|
1167
|
-
{ this.step(new RemoveMarkStep(cur, end, child.marks[j])); } }
|
|
1168
|
-
}
|
|
1169
|
-
cur = end;
|
|
1170
|
-
}
|
|
1171
|
-
if (!match.validEnd) {
|
|
1172
|
-
var fill = match.fillBefore(Fragment.empty, true);
|
|
1173
|
-
this.replace(cur, cur, new Slice(fill, 0, 0));
|
|
1174
|
-
}
|
|
1175
|
-
for (var i$1 = delSteps.length - 1; i$1 >= 0; i$1--) { this.step(delSteps[i$1]); }
|
|
1176
|
-
return this
|
|
1177
|
-
};
|
|
1178
|
-
|
|
1179
|
-
// :: (Node, number, ?number, ?Slice) → ?Step
|
|
1180
|
-
// ‘Fit’ a slice into a given position in the document, producing a
|
|
1181
|
-
// [step](#transform.Step) that inserts it. Will return null if
|
|
1182
|
-
// there's no meaningful way to insert the slice here, or inserting it
|
|
1183
|
-
// would be a no-op (an empty slice over an empty range).
|
|
1184
|
-
function replaceStep(doc, from, to, slice) {
|
|
1185
|
-
if ( to === void 0 ) to = from;
|
|
1186
|
-
if ( slice === void 0 ) slice = Slice.empty;
|
|
1187
|
-
|
|
1188
|
-
if (from == to && !slice.size) { return null }
|
|
1189
|
-
|
|
1190
|
-
var $from = doc.resolve(from), $to = doc.resolve(to);
|
|
1191
|
-
// Optimization -- avoid work if it's obvious that it's not needed.
|
|
1192
|
-
if (fitsTrivially($from, $to, slice)) { return new ReplaceStep(from, to, slice) }
|
|
1193
|
-
return new Fitter($from, $to, slice).fit()
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
// :: (number, ?number, ?Slice) → this
|
|
1197
|
-
// Replace the part of the document between `from` and `to` with the
|
|
1198
|
-
// given `slice`.
|
|
1199
|
-
Transform.prototype.replace = function(from, to, slice) {
|
|
1200
|
-
if ( to === void 0 ) to = from;
|
|
1201
|
-
if ( slice === void 0 ) slice = Slice.empty;
|
|
1202
|
-
|
|
1203
|
-
var step = replaceStep(this.doc, from, to, slice);
|
|
1204
|
-
if (step) { this.step(step); }
|
|
1205
|
-
return this
|
|
1206
|
-
};
|
|
1207
|
-
|
|
1208
|
-
// :: (number, number, union<Fragment, Node, [Node]>) → this
|
|
1209
|
-
// Replace the given range with the given content, which may be a
|
|
1210
|
-
// fragment, node, or array of nodes.
|
|
1211
|
-
Transform.prototype.replaceWith = function(from, to, content) {
|
|
1212
|
-
return this.replace(from, to, new Slice(Fragment.from(content), 0, 0))
|
|
1213
|
-
};
|
|
1214
|
-
|
|
1215
|
-
// :: (number, number) → this
|
|
1216
|
-
// Delete the content between the given positions.
|
|
1217
|
-
Transform.prototype.delete = function(from, to) {
|
|
1218
|
-
return this.replace(from, to, Slice.empty)
|
|
1219
|
-
};
|
|
1220
|
-
|
|
1221
|
-
// :: (number, union<Fragment, Node, [Node]>) → this
|
|
1222
|
-
// Insert the given content at the given position.
|
|
1223
|
-
Transform.prototype.insert = function(pos, content) {
|
|
1224
|
-
return this.replaceWith(pos, pos, content)
|
|
1225
|
-
};
|
|
1226
|
-
|
|
1227
|
-
function fitsTrivially($from, $to, slice) {
|
|
1228
|
-
return !slice.openStart && !slice.openEnd && $from.start() == $to.start() &&
|
|
1229
|
-
$from.parent.canReplace($from.index(), $to.index(), slice.content)
|
|
1230
|
-
}
|
|
1231
|
-
|
|
1232
|
-
// Algorithm for 'placing' the elements of a slice into a gap:
|
|
1233
|
-
//
|
|
1234
|
-
// We consider the content of each node that is open to the left to be
|
|
1235
|
-
// independently placeable. I.e. in <p("foo"), p("bar")>, when the
|
|
1236
|
-
// paragraph on the left is open, "foo" can be placed (somewhere on
|
|
1237
|
-
// the left side of the replacement gap) independently from p("bar").
|
|
1238
|
-
//
|
|
1239
|
-
// This class tracks the state of the placement progress in the
|
|
1240
|
-
// following properties:
|
|
1241
|
-
//
|
|
1242
|
-
// - `frontier` holds a stack of `{type, match}` objects that
|
|
1243
|
-
// represent the open side of the replacement. It starts at
|
|
1244
|
-
// `$from`, then moves forward as content is placed, and is finally
|
|
1245
|
-
// reconciled with `$to`.
|
|
1246
|
-
//
|
|
1247
|
-
// - `unplaced` is a slice that represents the content that hasn't
|
|
1248
|
-
// been placed yet.
|
|
1249
|
-
//
|
|
1250
|
-
// - `placed` is a fragment of placed content. Its open-start value
|
|
1251
|
-
// is implicit in `$from`, and its open-end value in `frontier`.
|
|
1252
|
-
var Fitter = function Fitter($from, $to, slice) {
|
|
1253
|
-
this.$to = $to;
|
|
1254
|
-
this.$from = $from;
|
|
1255
|
-
this.unplaced = slice;
|
|
1256
|
-
|
|
1257
|
-
this.frontier = [];
|
|
1258
|
-
for (var i = 0; i <= $from.depth; i++) {
|
|
1259
|
-
var node = $from.node(i);
|
|
1260
|
-
this.frontier.push({
|
|
1261
|
-
type: node.type,
|
|
1262
|
-
match: node.contentMatchAt($from.indexAfter(i))
|
|
1263
|
-
});
|
|
1264
|
-
}
|
|
1265
|
-
|
|
1266
|
-
this.placed = Fragment.empty;
|
|
1267
|
-
for (var i$1 = $from.depth; i$1 > 0; i$1--)
|
|
1268
|
-
{ this.placed = Fragment.from($from.node(i$1).copy(this.placed)); }
|
|
1269
|
-
};
|
|
1270
|
-
|
|
1271
|
-
var prototypeAccessors = { depth: { configurable: true } };
|
|
1272
|
-
|
|
1273
|
-
prototypeAccessors.depth.get = function () { return this.frontier.length - 1 };
|
|
1274
|
-
|
|
1275
|
-
Fitter.prototype.fit = function fit () {
|
|
1276
|
-
// As long as there's unplaced content, try to place some of it.
|
|
1277
|
-
// If that fails, either increase the open score of the unplaced
|
|
1278
|
-
// slice, or drop nodes from it, and then try again.
|
|
1279
|
-
while (this.unplaced.size) {
|
|
1280
|
-
var fit = this.findFittable();
|
|
1281
|
-
if (fit) { this.placeNodes(fit); }
|
|
1282
|
-
else { this.openMore() || this.dropNode(); }
|
|
1283
|
-
}
|
|
1284
|
-
// When there's inline content directly after the frontier _and_
|
|
1285
|
-
// directly after `this.$to`, we must generate a `ReplaceAround`
|
|
1286
|
-
// step that pulls that content into the node after the frontier.
|
|
1287
|
-
// That means the fitting must be done to the end of the textblock
|
|
1288
|
-
// node after `this.$to`, not `this.$to` itself.
|
|
1289
|
-
var moveInline = this.mustMoveInline(), placedSize = this.placed.size - this.depth - this.$from.depth;
|
|
1290
|
-
var $from = this.$from, $to = this.close(moveInline < 0 ? this.$to : $from.doc.resolve(moveInline));
|
|
1291
|
-
if (!$to) { return null }
|
|
1292
|
-
|
|
1293
|
-
// If closing to `$to` succeeded, create a step
|
|
1294
|
-
var content = this.placed, openStart = $from.depth, openEnd = $to.depth;
|
|
1295
|
-
while (openStart && openEnd && content.childCount == 1) { // Normalize by dropping open parent nodes
|
|
1296
|
-
content = content.firstChild.content;
|
|
1297
|
-
openStart--; openEnd--;
|
|
1298
|
-
}
|
|
1299
|
-
var slice = new Slice(content, openStart, openEnd);
|
|
1300
|
-
if (moveInline > -1)
|
|
1301
|
-
{ return new ReplaceAroundStep($from.pos, moveInline, this.$to.pos, this.$to.end(), slice, placedSize) }
|
|
1302
|
-
if (slice.size || $from.pos != this.$to.pos) // Don't generate no-op steps
|
|
1303
|
-
{ return new ReplaceStep($from.pos, $to.pos, slice) }
|
|
1304
|
-
};
|
|
1305
|
-
|
|
1306
|
-
// Find a position on the start spine of `this.unplaced` that has
|
|
1307
|
-
// content that can be moved somewhere on the frontier. Returns two
|
|
1308
|
-
// depths, one for the slice and one for the frontier.
|
|
1309
|
-
Fitter.prototype.findFittable = function findFittable () {
|
|
1310
|
-
// Only try wrapping nodes (pass 2) after finding a place without
|
|
1311
|
-
// wrapping failed.
|
|
1312
|
-
for (var pass = 1; pass <= 2; pass++) {
|
|
1313
|
-
for (var sliceDepth = this.unplaced.openStart; sliceDepth >= 0; sliceDepth--) {
|
|
1314
|
-
var fragment = (void 0), parent = (void 0);
|
|
1315
|
-
if (sliceDepth) {
|
|
1316
|
-
parent = contentAt(this.unplaced.content, sliceDepth - 1).firstChild;
|
|
1317
|
-
fragment = parent.content;
|
|
1318
|
-
} else {
|
|
1319
|
-
fragment = this.unplaced.content;
|
|
1320
|
-
}
|
|
1321
|
-
var first = fragment.firstChild;
|
|
1322
|
-
for (var frontierDepth = this.depth; frontierDepth >= 0; frontierDepth--) {
|
|
1323
|
-
var ref = this.frontier[frontierDepth];
|
|
1324
|
-
var type = ref.type;
|
|
1325
|
-
var match = ref.match;
|
|
1326
|
-
var wrap = (void 0), inject = (void 0);
|
|
1327
|
-
// In pass 1, if the next node matches, or there is no next
|
|
1328
|
-
// node but the parents look compatible, we've found a
|
|
1329
|
-
// place.
|
|
1330
|
-
if (pass == 1 && (first ? match.matchType(first.type) || (inject = match.fillBefore(Fragment.from(first), false))
|
|
1331
|
-
: type.compatibleContent(parent.type)))
|
|
1332
|
-
{ return {sliceDepth: sliceDepth, frontierDepth: frontierDepth, parent: parent, inject: inject} }
|
|
1333
|
-
// In pass 2, look for a set of wrapping nodes that make
|
|
1334
|
-
// `first` fit here.
|
|
1335
|
-
else if (pass == 2 && first && (wrap = match.findWrapping(first.type)))
|
|
1336
|
-
{ return {sliceDepth: sliceDepth, frontierDepth: frontierDepth, parent: parent, wrap: wrap} }
|
|
1337
|
-
// Don't continue looking further up if the parent node
|
|
1338
|
-
// would fit here.
|
|
1339
|
-
if (parent && match.matchType(parent.type)) { break }
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
};
|
|
1344
|
-
|
|
1345
|
-
Fitter.prototype.openMore = function openMore () {
|
|
1346
|
-
var ref = this.unplaced;
|
|
1347
|
-
var content = ref.content;
|
|
1348
|
-
var openStart = ref.openStart;
|
|
1349
|
-
var openEnd = ref.openEnd;
|
|
1350
|
-
var inner = contentAt(content, openStart);
|
|
1351
|
-
if (!inner.childCount || inner.firstChild.isLeaf) { return false }
|
|
1352
|
-
this.unplaced = new Slice(content, openStart + 1,
|
|
1353
|
-
Math.max(openEnd, inner.size + openStart >= content.size - openEnd ? openStart + 1 : 0));
|
|
1354
|
-
return true
|
|
1355
|
-
};
|
|
1356
|
-
|
|
1357
|
-
Fitter.prototype.dropNode = function dropNode () {
|
|
1358
|
-
var ref = this.unplaced;
|
|
1359
|
-
var content = ref.content;
|
|
1360
|
-
var openStart = ref.openStart;
|
|
1361
|
-
var openEnd = ref.openEnd;
|
|
1362
|
-
var inner = contentAt(content, openStart);
|
|
1363
|
-
if (inner.childCount <= 1 && openStart > 0) {
|
|
1364
|
-
var openAtEnd = content.size - openStart <= openStart + inner.size;
|
|
1365
|
-
this.unplaced = new Slice(dropFromFragment(content, openStart - 1, 1), openStart - 1,
|
|
1366
|
-
openAtEnd ? openStart - 1 : openEnd);
|
|
1367
|
-
} else {
|
|
1368
|
-
this.unplaced = new Slice(dropFromFragment(content, openStart, 1), openStart, openEnd);
|
|
1369
|
-
}
|
|
1370
|
-
};
|
|
1371
|
-
|
|
1372
|
-
// : ({sliceDepth: number, frontierDepth: number, parent: ?Node, wrap: ?[NodeType], inject: ?Fragment})
|
|
1373
|
-
// Move content from the unplaced slice at `sliceDepth` to the
|
|
1374
|
-
// frontier node at `frontierDepth`. Close that frontier node when
|
|
1375
|
-
// applicable.
|
|
1376
|
-
Fitter.prototype.placeNodes = function placeNodes (ref) {
|
|
1377
|
-
var sliceDepth = ref.sliceDepth;
|
|
1378
|
-
var frontierDepth = ref.frontierDepth;
|
|
1379
|
-
var parent = ref.parent;
|
|
1380
|
-
var inject = ref.inject;
|
|
1381
|
-
var wrap = ref.wrap;
|
|
1382
|
-
|
|
1383
|
-
while (this.depth > frontierDepth) { this.closeFrontierNode(); }
|
|
1384
|
-
if (wrap) { for (var i = 0; i < wrap.length; i++) { this.openFrontierNode(wrap[i]); } }
|
|
1385
|
-
|
|
1386
|
-
var slice = this.unplaced, fragment = parent ? parent.content : slice.content;
|
|
1387
|
-
var openStart = slice.openStart - sliceDepth;
|
|
1388
|
-
var taken = 0, add = [];
|
|
1389
|
-
var ref$1 = this.frontier[frontierDepth];
|
|
1390
|
-
var match = ref$1.match;
|
|
1391
|
-
var type = ref$1.type;
|
|
1392
|
-
if (inject) {
|
|
1393
|
-
for (var i$1 = 0; i$1 < inject.childCount; i$1++) { add.push(inject.child(i$1)); }
|
|
1394
|
-
match = match.matchFragment(inject);
|
|
1395
|
-
}
|
|
1396
|
-
// Computes the amount of (end) open nodes at the end of the
|
|
1397
|
-
// fragment. When 0, the parent is open, but no more. When
|
|
1398
|
-
// negative, nothing is open.
|
|
1399
|
-
var openEndCount = (fragment.size + sliceDepth) - (slice.content.size - slice.openEnd);
|
|
1400
|
-
// Scan over the fragment, fitting as many child nodes as
|
|
1401
|
-
// possible.
|
|
1402
|
-
while (taken < fragment.childCount) {
|
|
1403
|
-
var next = fragment.child(taken), matches = match.matchType(next.type);
|
|
1404
|
-
if (!matches) { break }
|
|
1405
|
-
taken++;
|
|
1406
|
-
if (taken > 1 || openStart == 0 || next.content.size) { // Drop empty open nodes
|
|
1407
|
-
match = matches;
|
|
1408
|
-
add.push(closeNodeStart(next.mark(type.allowedMarks(next.marks)), taken == 1 ? openStart : 0,
|
|
1409
|
-
taken == fragment.childCount ? openEndCount : -1));
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
var toEnd = taken == fragment.childCount;
|
|
1413
|
-
if (!toEnd) { openEndCount = -1; }
|
|
1414
|
-
|
|
1415
|
-
this.placed = addToFragment(this.placed, frontierDepth, Fragment.from(add));
|
|
1416
|
-
this.frontier[frontierDepth].match = match;
|
|
1417
|
-
|
|
1418
|
-
// If the parent types match, and the entire node was moved, and
|
|
1419
|
-
// it's not open, close this frontier node right away.
|
|
1420
|
-
if (toEnd && openEndCount < 0 && parent && parent.type == this.frontier[this.depth].type && this.frontier.length > 1)
|
|
1421
|
-
{ this.closeFrontierNode(); }
|
|
1422
|
-
|
|
1423
|
-
// Add new frontier nodes for any open nodes at the end.
|
|
1424
|
-
for (var i$2 = 0, cur = fragment; i$2 < openEndCount; i$2++) {
|
|
1425
|
-
var node = cur.lastChild;
|
|
1426
|
-
this.frontier.push({type: node.type, match: node.contentMatchAt(node.childCount)});
|
|
1427
|
-
cur = node.content;
|
|
1428
|
-
}
|
|
1429
|
-
|
|
1430
|
-
// Update `this.unplaced`. Drop the entire node from which we
|
|
1431
|
-
// placed it we got to its end, otherwise just drop the placed
|
|
1432
|
-
// nodes.
|
|
1433
|
-
this.unplaced = !toEnd ? new Slice(dropFromFragment(slice.content, sliceDepth, taken), slice.openStart, slice.openEnd)
|
|
1434
|
-
: sliceDepth == 0 ? Slice.empty
|
|
1435
|
-
: new Slice(dropFromFragment(slice.content, sliceDepth - 1, 1),
|
|
1436
|
-
sliceDepth - 1, openEndCount < 0 ? slice.openEnd : sliceDepth - 1);
|
|
1437
|
-
};
|
|
1438
|
-
|
|
1439
|
-
Fitter.prototype.mustMoveInline = function mustMoveInline () {
|
|
1440
|
-
if (!this.$to.parent.isTextblock) { return -1 }
|
|
1441
|
-
var top = this.frontier[this.depth], level;
|
|
1442
|
-
if (!top.type.isTextblock || !contentAfterFits(this.$to, this.$to.depth, top.type, top.match, false) ||
|
|
1443
|
-
(this.$to.depth == this.depth && (level = this.findCloseLevel(this.$to)) && level.depth == this.depth)) { return -1 }
|
|
1444
|
-
|
|
1445
|
-
var ref = this.$to;
|
|
1446
|
-
var depth = ref.depth;
|
|
1447
|
-
var after = this.$to.after(depth);
|
|
1448
|
-
while (depth > 1 && after == this.$to.end(--depth)) { ++after; }
|
|
1449
|
-
return after
|
|
1450
|
-
};
|
|
1451
|
-
|
|
1452
|
-
Fitter.prototype.findCloseLevel = function findCloseLevel ($to) {
|
|
1453
|
-
scan: for (var i = Math.min(this.depth, $to.depth); i >= 0; i--) {
|
|
1454
|
-
var ref = this.frontier[i];
|
|
1455
|
-
var match = ref.match;
|
|
1456
|
-
var type = ref.type;
|
|
1457
|
-
var dropInner = i < $to.depth && $to.end(i + 1) == $to.pos + ($to.depth - (i + 1));
|
|
1458
|
-
var fit = contentAfterFits($to, i, type, match, dropInner);
|
|
1459
|
-
if (!fit) { continue }
|
|
1460
|
-
for (var d = i - 1; d >= 0; d--) {
|
|
1461
|
-
var ref$1 = this.frontier[d];
|
|
1462
|
-
var match$1 = ref$1.match;
|
|
1463
|
-
var type$1 = ref$1.type;
|
|
1464
|
-
var matches = contentAfterFits($to, d, type$1, match$1, true);
|
|
1465
|
-
if (!matches || matches.childCount) { continue scan }
|
|
1466
|
-
}
|
|
1467
|
-
return {depth: i, fit: fit, move: dropInner ? $to.doc.resolve($to.after(i + 1)) : $to}
|
|
1468
|
-
}
|
|
1469
|
-
};
|
|
1470
|
-
|
|
1471
|
-
Fitter.prototype.close = function close ($to) {
|
|
1472
|
-
var close = this.findCloseLevel($to);
|
|
1473
|
-
if (!close) { return null }
|
|
1474
|
-
|
|
1475
|
-
while (this.depth > close.depth) { this.closeFrontierNode(); }
|
|
1476
|
-
if (close.fit.childCount) { this.placed = addToFragment(this.placed, close.depth, close.fit); }
|
|
1477
|
-
$to = close.move;
|
|
1478
|
-
for (var d = close.depth + 1; d <= $to.depth; d++) {
|
|
1479
|
-
var node = $to.node(d), add = node.type.contentMatch.fillBefore(node.content, true, $to.index(d));
|
|
1480
|
-
this.openFrontierNode(node.type, node.attrs, add);
|
|
1481
|
-
}
|
|
1482
|
-
return $to
|
|
1483
|
-
};
|
|
1484
|
-
|
|
1485
|
-
Fitter.prototype.openFrontierNode = function openFrontierNode (type, attrs, content) {
|
|
1486
|
-
var top = this.frontier[this.depth];
|
|
1487
|
-
top.match = top.match.matchType(type);
|
|
1488
|
-
this.placed = addToFragment(this.placed, this.depth, Fragment.from(type.create(attrs, content)));
|
|
1489
|
-
this.frontier.push({type: type, match: type.contentMatch});
|
|
1490
|
-
};
|
|
1491
|
-
|
|
1492
|
-
Fitter.prototype.closeFrontierNode = function closeFrontierNode () {
|
|
1493
|
-
var open = this.frontier.pop();
|
|
1494
|
-
var add = open.match.fillBefore(Fragment.empty, true);
|
|
1495
|
-
if (add.childCount) { this.placed = addToFragment(this.placed, this.frontier.length, add); }
|
|
1496
|
-
};
|
|
1497
|
-
|
|
1498
|
-
Object.defineProperties( Fitter.prototype, prototypeAccessors );
|
|
1499
|
-
|
|
1500
|
-
function dropFromFragment(fragment, depth, count) {
|
|
1501
|
-
if (depth == 0) { return fragment.cutByIndex(count) }
|
|
1502
|
-
return fragment.replaceChild(0, fragment.firstChild.copy(dropFromFragment(fragment.firstChild.content, depth - 1, count)))
|
|
1503
|
-
}
|
|
1504
|
-
|
|
1505
|
-
function addToFragment(fragment, depth, content) {
|
|
1506
|
-
if (depth == 0) { return fragment.append(content) }
|
|
1507
|
-
return fragment.replaceChild(fragment.childCount - 1,
|
|
1508
|
-
fragment.lastChild.copy(addToFragment(fragment.lastChild.content, depth - 1, content)))
|
|
1509
|
-
}
|
|
1510
|
-
|
|
1511
|
-
function contentAt(fragment, depth) {
|
|
1512
|
-
for (var i = 0; i < depth; i++) { fragment = fragment.firstChild.content; }
|
|
1513
|
-
return fragment
|
|
1514
|
-
}
|
|
1515
|
-
|
|
1516
|
-
function closeNodeStart(node, openStart, openEnd) {
|
|
1517
|
-
if (openStart <= 0) { return node }
|
|
1518
|
-
var frag = node.content;
|
|
1519
|
-
if (openStart > 1)
|
|
1520
|
-
{ frag = frag.replaceChild(0, closeNodeStart(frag.firstChild, openStart - 1, frag.childCount == 1 ? openEnd - 1 : 0)); }
|
|
1521
|
-
if (openStart > 0) {
|
|
1522
|
-
frag = node.type.contentMatch.fillBefore(frag).append(frag);
|
|
1523
|
-
if (openEnd <= 0) { frag = frag.append(node.type.contentMatch.matchFragment(frag).fillBefore(Fragment.empty, true)); }
|
|
1524
|
-
}
|
|
1525
|
-
return node.copy(frag)
|
|
1526
|
-
}
|
|
1527
|
-
|
|
1528
|
-
function contentAfterFits($to, depth, type, match, open) {
|
|
1529
|
-
var node = $to.node(depth), index = open ? $to.indexAfter(depth) : $to.index(depth);
|
|
1530
|
-
if (index == node.childCount && !type.compatibleContent(node.type)) { return null }
|
|
1531
|
-
var fit = match.fillBefore(node.content, true, index);
|
|
1532
|
-
return fit && !invalidMarks(type, node.content, index) ? fit : null
|
|
1533
|
-
}
|
|
1534
|
-
|
|
1535
|
-
function invalidMarks(type, fragment, start) {
|
|
1536
|
-
for (var i = start; i < fragment.childCount; i++)
|
|
1537
|
-
{ if (!type.allowsMarks(fragment.child(i).marks)) { return true } }
|
|
1538
|
-
return false
|
|
1539
|
-
}
|
|
1540
|
-
|
|
1541
|
-
function definesContent(type) {
|
|
1542
|
-
return type.spec.defining || type.spec.definingForContent
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
// :: (number, number, Slice) → this
|
|
1546
|
-
// Replace a range of the document with a given slice, using `from`,
|
|
1547
|
-
// `to`, and the slice's [`openStart`](#model.Slice.openStart) property
|
|
1548
|
-
// as hints, rather than fixed start and end points. This method may
|
|
1549
|
-
// grow the replaced area or close open nodes in the slice in order to
|
|
1550
|
-
// get a fit that is more in line with WYSIWYG expectations, by
|
|
1551
|
-
// dropping fully covered parent nodes of the replaced region when
|
|
1552
|
-
// they are marked [non-defining as context](#model.NodeSpec.definingAsContext), or
|
|
1553
|
-
// including an open parent node from the slice that _is_ marked as
|
|
1554
|
-
// [defining its content](#model.NodeSpec.definingForContent).
|
|
1555
|
-
//
|
|
1556
|
-
// This is the method, for example, to handle paste. The similar
|
|
1557
|
-
// [`replace`](#transform.Transform.replace) method is a more
|
|
1558
|
-
// primitive tool which will _not_ move the start and end of its given
|
|
1559
|
-
// range, and is useful in situations where you need more precise
|
|
1560
|
-
// control over what happens.
|
|
1561
|
-
Transform.prototype.replaceRange = function(from, to, slice) {
|
|
1562
|
-
if (!slice.size) { return this.deleteRange(from, to) }
|
|
1563
|
-
|
|
1564
|
-
var $from = this.doc.resolve(from), $to = this.doc.resolve(to);
|
|
1565
|
-
if (fitsTrivially($from, $to, slice))
|
|
1566
|
-
{ return this.step(new ReplaceStep(from, to, slice)) }
|
|
1567
|
-
|
|
1568
|
-
var targetDepths = coveredDepths($from, this.doc.resolve(to));
|
|
1569
|
-
// Can't replace the whole document, so remove 0 if it's present
|
|
1570
|
-
if (targetDepths[targetDepths.length - 1] == 0) { targetDepths.pop(); }
|
|
1571
|
-
// Negative numbers represent not expansion over the whole node at
|
|
1572
|
-
// that depth, but replacing from $from.before(-D) to $to.pos.
|
|
1573
|
-
var preferredTarget = -($from.depth + 1);
|
|
1574
|
-
targetDepths.unshift(preferredTarget);
|
|
1575
|
-
// This loop picks a preferred target depth, if one of the covering
|
|
1576
|
-
// depths is not outside of a defining node, and adds negative
|
|
1577
|
-
// depths for any depth that has $from at its start and does not
|
|
1578
|
-
// cross a defining node.
|
|
1579
|
-
for (var d = $from.depth, pos = $from.pos - 1; d > 0; d--, pos--) {
|
|
1580
|
-
var spec = $from.node(d).type.spec;
|
|
1581
|
-
if (spec.defining || spec.definingAsContext || spec.isolating) { break }
|
|
1582
|
-
if (targetDepths.indexOf(d) > -1) { preferredTarget = d; }
|
|
1583
|
-
else if ($from.before(d) == pos) { targetDepths.splice(1, 0, -d); }
|
|
1584
|
-
}
|
|
1585
|
-
// Try to fit each possible depth of the slice into each possible
|
|
1586
|
-
// target depth, starting with the preferred depths.
|
|
1587
|
-
var preferredTargetIndex = targetDepths.indexOf(preferredTarget);
|
|
1588
|
-
|
|
1589
|
-
var leftNodes = [], preferredDepth = slice.openStart;
|
|
1590
|
-
for (var content = slice.content, i = 0;; i++) {
|
|
1591
|
-
var node = content.firstChild;
|
|
1592
|
-
leftNodes.push(node);
|
|
1593
|
-
if (i == slice.openStart) { break }
|
|
1594
|
-
content = node.content;
|
|
1595
|
-
}
|
|
1596
|
-
|
|
1597
|
-
// Back up preferredDepth to cover defining textblocks directly
|
|
1598
|
-
// above it, possibly skipping a non-defining textblock.
|
|
1599
|
-
for (var d$1 = preferredDepth - 1; d$1 >= 0; d$1--) {
|
|
1600
|
-
var type = leftNodes[d$1].type, def = definesContent(type);
|
|
1601
|
-
if (def && $from.node(preferredTargetIndex).type != type) { preferredDepth = d$1; }
|
|
1602
|
-
else if (def || !type.isTextblock) { break }
|
|
1603
|
-
}
|
|
1604
|
-
|
|
1605
|
-
for (var j = slice.openStart; j >= 0; j--) {
|
|
1606
|
-
var openDepth = (j + preferredDepth + 1) % (slice.openStart + 1);
|
|
1607
|
-
var insert = leftNodes[openDepth];
|
|
1608
|
-
if (!insert) { continue }
|
|
1609
|
-
for (var i$1 = 0; i$1 < targetDepths.length; i$1++) {
|
|
1610
|
-
// Loop over possible expansion levels, starting with the
|
|
1611
|
-
// preferred one
|
|
1612
|
-
var targetDepth = targetDepths[(i$1 + preferredTargetIndex) % targetDepths.length], expand = true;
|
|
1613
|
-
if (targetDepth < 0) { expand = false; targetDepth = -targetDepth; }
|
|
1614
|
-
var parent = $from.node(targetDepth - 1), index = $from.index(targetDepth - 1);
|
|
1615
|
-
if (parent.canReplaceWith(index, index, insert.type, insert.marks))
|
|
1616
|
-
{ return this.replace($from.before(targetDepth), expand ? $to.after(targetDepth) : to,
|
|
1617
|
-
new Slice(closeFragment(slice.content, 0, slice.openStart, openDepth),
|
|
1618
|
-
openDepth, slice.openEnd)) }
|
|
1619
|
-
}
|
|
1620
|
-
}
|
|
1621
|
-
|
|
1622
|
-
var startSteps = this.steps.length;
|
|
1623
|
-
for (var i$2 = targetDepths.length - 1; i$2 >= 0; i$2--) {
|
|
1624
|
-
this.replace(from, to, slice);
|
|
1625
|
-
if (this.steps.length > startSteps) { break }
|
|
1626
|
-
var depth = targetDepths[i$2];
|
|
1627
|
-
if (depth < 0) { continue }
|
|
1628
|
-
from = $from.before(depth); to = $to.after(depth);
|
|
1629
|
-
}
|
|
1630
|
-
return this
|
|
1631
|
-
};
|
|
1632
|
-
|
|
1633
|
-
function closeFragment(fragment, depth, oldOpen, newOpen, parent) {
|
|
1634
|
-
if (depth < oldOpen) {
|
|
1635
|
-
var first = fragment.firstChild;
|
|
1636
|
-
fragment = fragment.replaceChild(0, first.copy(closeFragment(first.content, depth + 1, oldOpen, newOpen, first)));
|
|
1637
|
-
}
|
|
1638
|
-
if (depth > newOpen) {
|
|
1639
|
-
var match = parent.contentMatchAt(0);
|
|
1640
|
-
var start = match.fillBefore(fragment).append(fragment);
|
|
1641
|
-
fragment = start.append(match.matchFragment(start).fillBefore(Fragment.empty, true));
|
|
1642
|
-
}
|
|
1643
|
-
return fragment
|
|
1644
|
-
}
|
|
1645
|
-
|
|
1646
|
-
// :: (number, number, Node) → this
|
|
1647
|
-
// Replace the given range with a node, but use `from` and `to` as
|
|
1648
|
-
// hints, rather than precise positions. When from and to are the same
|
|
1649
|
-
// and are at the start or end of a parent node in which the given
|
|
1650
|
-
// node doesn't fit, this method may _move_ them out towards a parent
|
|
1651
|
-
// that does allow the given node to be placed. When the given range
|
|
1652
|
-
// completely covers a parent node, this method may completely replace
|
|
1653
|
-
// that parent node.
|
|
1654
|
-
Transform.prototype.replaceRangeWith = function(from, to, node) {
|
|
1655
|
-
if (!node.isInline && from == to && this.doc.resolve(from).parent.content.size) {
|
|
1656
|
-
var point = insertPoint(this.doc, from, node.type);
|
|
1657
|
-
if (point != null) { from = to = point; }
|
|
1658
|
-
}
|
|
1659
|
-
return this.replaceRange(from, to, new Slice(Fragment.from(node), 0, 0))
|
|
1660
|
-
};
|
|
1661
|
-
|
|
1662
|
-
// :: (number, number) → this
|
|
1663
|
-
// Delete the given range, expanding it to cover fully covered
|
|
1664
|
-
// parent nodes until a valid replace is found.
|
|
1665
|
-
Transform.prototype.deleteRange = function(from, to) {
|
|
1666
|
-
var $from = this.doc.resolve(from), $to = this.doc.resolve(to);
|
|
1667
|
-
var covered = coveredDepths($from, $to);
|
|
1668
|
-
for (var i = 0; i < covered.length; i++) {
|
|
1669
|
-
var depth = covered[i], last = i == covered.length - 1;
|
|
1670
|
-
if ((last && depth == 0) || $from.node(depth).type.contentMatch.validEnd)
|
|
1671
|
-
{ return this.delete($from.start(depth), $to.end(depth)) }
|
|
1672
|
-
if (depth > 0 && (last || $from.node(depth - 1).canReplace($from.index(depth - 1), $to.indexAfter(depth - 1))))
|
|
1673
|
-
{ return this.delete($from.before(depth), $to.after(depth)) }
|
|
1674
|
-
}
|
|
1675
|
-
for (var d = 1; d <= $from.depth && d <= $to.depth; d++) {
|
|
1676
|
-
if (from - $from.start(d) == $from.depth - d && to > $from.end(d) && $to.end(d) - to != $to.depth - d)
|
|
1677
|
-
{ return this.delete($from.before(d), to) }
|
|
1678
|
-
}
|
|
1679
|
-
return this.delete(from, to)
|
|
1680
|
-
};
|
|
1681
|
-
|
|
1682
|
-
// : (ResolvedPos, ResolvedPos) → [number]
|
|
1683
|
-
// Returns an array of all depths for which $from - $to spans the
|
|
1684
|
-
// whole content of the nodes at that depth.
|
|
1685
|
-
function coveredDepths($from, $to) {
|
|
1686
|
-
var result = [], minDepth = Math.min($from.depth, $to.depth);
|
|
1687
|
-
for (var d = minDepth; d >= 0; d--) {
|
|
1688
|
-
var start = $from.start(d);
|
|
1689
|
-
if (start < $from.pos - ($from.depth - d) ||
|
|
1690
|
-
$to.end(d) > $to.pos + ($to.depth - d) ||
|
|
1691
|
-
$from.node(d).type.spec.isolating ||
|
|
1692
|
-
$to.node(d).type.spec.isolating) { break }
|
|
1693
|
-
if (start == $to.start(d) ||
|
|
1694
|
-
(d == $from.depth && d == $to.depth && $from.parent.inlineContent && $to.parent.inlineContent &&
|
|
1695
|
-
d && $to.start(d - 1) == start - 1))
|
|
1696
|
-
{ result.push(d); }
|
|
1697
|
-
}
|
|
1698
|
-
return result
|
|
1699
|
-
}
|
|
1700
|
-
|
|
1701
|
-
export { AddMarkStep, MapResult, Mapping, RemoveMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, TransformError, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
|
|
1702
|
-
//# sourceMappingURL=index.es.js.map
|