This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
document.js
521 lines (459 loc) · 17.7 KB
/
document.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module engine/model/document
*/
// Load all basic deltas and transformations, they register themselves, but they need to be imported somewhere.
/* eslint-disable no-unused-vars */
import deltas from './delta/basic-deltas';
import transformations from './delta/basic-transformations';
/* eslint-enable no-unused-vars */
import Range from './range';
import Position from './position';
import RootElement from './rootelement';
import Batch from './batch';
import History from './history';
import LiveSelection from './liveselection';
import Schema from './schema';
import TreeWalker from './treewalker';
import MarkerCollection from './markercollection';
import deltaTransform from './delta/transform';
import clone from '@ckeditor/ckeditor5-utils/src/lib/lodash/clone';
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import { isInsideSurrogatePair, isInsideCombinedSymbol } from '@ckeditor/ckeditor5-utils/src/unicode';
const graveyardName = '$graveyard';
/**
* Document tree model describes all editable data in the editor. It may contain multiple
* {@link module:engine/model/document~Document#roots root elements}, for example if the editor have multiple editable areas,
* each area will be represented by the separate root.
*
* All changes in the document are done by {@link module:engine/model/operation/operation~Operation operations}. To create operations in
* a simple way, use the {@link module:engine/model/batch~Batch} API, for example:
*
* doc.batch().insert( position, nodes ).split( otherPosition );
*
* @see module:engine/model/document~Document#batch
* @mixes module:utils/emittermixin~EmitterMixin
*/
export default class Document {
/**
* Creates an empty document instance with no {@link #roots} (other than
* the {@link #graveyard graveyard root}).
*/
constructor() {
/**
* Document version. It starts from `0` and every operation increases the version number. It is used to ensure that
* operations are applied on the proper document version.
* If the {@link module:engine/model/operation/operation~Operation#baseVersion} will not match document version the
* {@link module:utils/ckeditorerror~CKEditorError model-document-applyOperation-wrong-version} error is thrown.
*
* @readonly
* @member {Number}
*/
this.version = 0;
/**
* Schema for this document.
*
* @member {module:engine/model/schema~Schema}
*/
this.schema = new Schema();
/**
* Document's history.
*
* **Note:** Be aware that deltas applied to the document might get removed or changed.
*
* @readonly
* @member {module:engine/model/history~History}
*/
this.history = new History( this );
/**
* Document's markers' collection.
*
* @readonly
* @member {module:engine/model/markercollection~MarkerCollection}
*/
this.markers = new MarkerCollection();
/**
* Selection done on this document.
*
* @readonly
* @member {module:engine/model/liveselection~LiveSelection}
*/
this.selection = new LiveSelection( this );
/**
* Array of pending changes. See: {@link #enqueueChanges}.
*
* @private
* @member {Array.<Function>}
*/
this._pendingChanges = [];
/**
* List of roots that are owned and managed by this document. Use {@link #createRoot} and
* {@link #getRoot} to manipulate it.
*
* @readonly
* @member {Map}
*/
this.roots = new Map();
// Add events that will ensure selection correctness.
this.selection.on( 'change:range', () => {
for ( const range of this.selection.getRanges() ) {
if ( !this._validateSelectionRange( range ) ) {
/**
* Range from document selection starts or ends at incorrect position.
*
* @error document-selection-wrong-position
* @param {module:engine/model/range~Range} range
*/
throw new CKEditorError( 'document-selection-wrong-position: ' +
'Range from document selection starts or ends at incorrect position.', { range } );
}
}
} );
// Graveyard tree root. Document always have a graveyard root, which stores removed nodes.
this.createRoot( '$root', graveyardName );
}
/**
* Graveyard tree root. Document always have a graveyard root, which stores removed nodes.
*
* @readonly
* @member {module:engine/model/rootelement~RootElement}
*/
get graveyard() {
return this.getRoot( graveyardName );
}
/**
* This is the entry point for all document changes. All changes on the document are done using
* {@link module:engine/model/operation/operation~Operation operations}. To create operations in the simple way use the
* {@link module:engine/model/batch~Batch} API available via {@link #batch} method.
*
* @fires event:change
* @param {module:engine/model/operation/operation~Operation} operation Operation to be applied.
*/
applyOperation( operation ) {
if ( operation.baseVersion !== this.version ) {
/**
* Only operations with matching versions can be applied.
*
* @error document-applyOperation-wrong-version
* @param {module:engine/model/operation/operation~Operation} operation
*/
throw new CKEditorError(
'model-document-applyOperation-wrong-version: Only operations with matching versions can be applied.',
{ operation } );
}
const changes = operation._execute();
this.version++;
this.history.addDelta( operation.delta );
if ( changes ) {
// `NoOperation` returns no changes, do not fire event for it.
this.fire( 'change', operation.type, changes, operation.delta.batch, operation.delta.type );
}
}
/**
* Creates a {@link module:engine/model/batch~Batch} instance which allows to change the document.
*
* @param {String} [type] Batch type. See {@link module:engine/model/batch~Batch#type}.
* @returns {module:engine/model/batch~Batch} Batch instance.
*/
batch( type ) {
return new Batch( this, type );
}
/**
* Creates a new top-level root.
*
* @param {String} [elementName='$root'] Element name. Defaults to `'$root'` which also have
* some basic schema defined (`$block`s are allowed inside the `$root`). Make sure to define a proper
* schema if you use a different name.
* @param {String} [rootName='main'] Unique root name.
* @returns {module:engine/model/rootelement~RootElement} Created root.
*/
createRoot( elementName = '$root', rootName = 'main' ) {
if ( this.roots.has( rootName ) ) {
/**
* Root with specified name already exists.
*
* @error document-createRoot-name-exists
* @param {module:engine/model/document~Document} doc
* @param {String} name
*/
throw new CKEditorError(
'model-document-createRoot-name-exists: Root with specified name already exists.',
{ name: rootName }
);
}
const root = new RootElement( this, elementName, rootName );
this.roots.set( rootName, root );
return root;
}
/**
* Removes all events listeners set by document instance.
*/
destroy() {
this.selection.destroy();
this.stopListening();
}
/**
* Enqueues document changes. Any changes to be done on document (mostly using {@link #batch}
* should be placed in the queued callback. If no other plugin is changing document at the moment, the callback will be
* called immediately. Otherwise it will wait for all previously queued changes to finish happening. This way
* queued callback will not interrupt other callbacks.
*
* When all queued changes are done {@link #event:changesDone} event is fired.
*
* @fires changesDone
* @param {Function} callback Callback to enqueue.
*/
enqueueChanges( callback ) {
this._pendingChanges.push( callback );
if ( this._pendingChanges.length == 1 ) {
while ( this._pendingChanges.length ) {
this._pendingChanges[ 0 ]();
this._pendingChanges.shift();
}
this.fire( 'changesDone' );
}
}
/**
* Returns top-level root by its name.
*
* @param {String} [name='main'] Unique root name.
* @returns {module:engine/model/rootelement~RootElement} Root registered under given name.
*/
getRoot( name = 'main' ) {
if ( !this.roots.has( name ) ) {
/**
* Root with specified name does not exist.
*
* @error document-getRoot-root-not-exist
* @param {String} name
*/
throw new CKEditorError(
'model-document-getRoot-root-not-exist: Root with specified name does not exist.',
{ name }
);
}
return this.roots.get( name );
}
/**
* Checks if root with given name is defined.
*
* @param {String} name Name of root to check.
* @returns {Boolean}
*/
hasRoot( name ) {
return this.roots.has( name );
}
/**
* Returns array with names of all roots (without the {@link #graveyard}) added to the document.
*
* @returns {Array.<String>} Roots names.
*/
getRootNames() {
return Array.from( this.roots.keys() ).filter( name => name != graveyardName );
}
/**
* Basing on given `position`, finds and returns a {@link module:engine/model/range~Range Range} instance that is
* nearest to that `position` and is a correct range for selection.
*
* Correct selection range might be collapsed - when it's located in position where text node can be placed.
* Non-collapsed range is returned when selection can be placed around element marked as "object" in
* {@link module:engine/model/schema~Schema schema}.
*
* Direction of searching for nearest correct selection range can be specified as:
* * `both` - searching will be performed in both ways,
* * `forward` - searching will be performed only forward,
* * `backward` - searching will be performed only backward.
*
* When valid selection range cannot be found, `null` is returned.
*
* @param {module:engine/model/position~Position} position Reference position where new selection range should be looked for.
* @param {'both'|'forward'|'backward'} [direction='both'] Search direction.
* @returns {module:engine/model/range~Range|null} Nearest selection range or `null` if one cannot be found.
*/
getNearestSelectionRange( position, direction = 'both' ) {
// Return collapsed range if provided position is valid.
if ( this.schema.check( { name: '$text', inside: position } ) ) {
return new Range( position );
}
let backwardWalker, forwardWalker;
if ( direction == 'both' || direction == 'backward' ) {
backwardWalker = new TreeWalker( { startPosition: position, direction: 'backward' } );
}
if ( direction == 'both' || direction == 'forward' ) {
forwardWalker = new TreeWalker( { startPosition: position } );
}
for ( const data of combineWalkers( backwardWalker, forwardWalker ) ) {
const type = ( data.walker == backwardWalker ? 'elementEnd' : 'elementStart' );
const value = data.value;
if ( value.type == type && this.schema.objects.has( value.item.name ) ) {
return Range.createOn( value.item );
}
if ( this.schema.check( { name: '$text', inside: value.nextPosition } ) ) {
return new Range( value.nextPosition );
}
}
return null;
}
/**
* Transforms two sets of deltas by themselves. Returns both transformed sets.
*
* @param {Array.<module:engine/model/delta/delta~Delta>} deltasA Array with the first set of deltas to transform. These
* deltas are considered more important (than `deltasB`) when resolving conflicts.
* @param {Array.<module:engine/model/delta/delta~Delta>} deltasB Array with the second set of deltas to transform. These
* deltas are considered less important (than `deltasA`) when resolving conflicts.
* @param {Boolean} [useContext=false] When set to `true`, transformation will store and use additional context
* information to guarantee more expected results. Should be used whenever deltas related to already applied
* deltas are transformed (for example when undoing changes).
* @returns {Object}
* @returns {Array.<module:engine/model/delta/delta~Delta>} return.deltasA The first set of deltas transformed
* by the second set of deltas.
* @returns {Array.<module:engine/model/delta/delta~Delta>} return.deltasB The second set of deltas transformed
* by the first set of deltas.
*/
transformDeltas( deltasA, deltasB, useContext = false ) {
return deltaTransform.transformDeltaSets( deltasA, deltasB, useContext ? this : null );
}
/**
* Custom toJSON method to solve child-parent circular dependencies.
*
* @returns {Object} Clone of this object with the document property changed to string.
*/
toJSON() {
const json = clone( this );
// Due to circular references we need to remove parent reference.
json.selection = '[engine.model.LiveSelection]';
return json;
}
/**
* Returns default root for this document which is either the first root that was added to the the document using
* {@link #createRoot} or the {@link #graveyard graveyard root} if no other roots were created.
*
* @protected
* @returns {module:engine/model/rootelement~RootElement} The default root for this document.
*/
_getDefaultRoot() {
for ( const root of this.roots.values() ) {
if ( root !== this.graveyard ) {
return root;
}
}
return this.graveyard;
}
/**
* Returns a default range for this selection. The default range is a collapsed range that starts and ends
* at the beginning of this selection's document's {@link #_getDefaultRoot default root}.
*
* @protected
* @returns {module:engine/model/range~Range}
*/
_getDefaultRange() {
const defaultRoot = this._getDefaultRoot();
// Find the first position where the selection can be put.
const position = new Position( defaultRoot, [ 0 ] );
const nearestRange = this.getNearestSelectionRange( position );
// If valid selection range is not found - return range collapsed at the beginning of the root.
return nearestRange || new Range( position );
}
/**
* Checks whether given {@link module:engine/model/range~Range range} is a valid range for
* {@link #selection document's selection}.
*
* @private
* @param {module:engine/model/range~Range} range Range to check.
* @returns {Boolean} `true` if `range` is valid, `false` otherwise.
*/
_validateSelectionRange( range ) {
return validateTextNodePosition( range.start ) && validateTextNodePosition( range.end );
}
/**
* Fired when document changes by applying an operation.
*
* There are 5 types of change:
*
* * 'insert' when nodes are inserted,
* * 'remove' when nodes are removed,
* * 'reinsert' when remove is undone,
* * 'move' when nodes are moved,
* * 'addAttribute' when attributes are added,
* * 'removeAttribute' when attributes are removed,
* * 'changeAttribute' when attributes change,
* * 'addRootAttribute' when attribute for root is added,
* * 'removeRootAttribute' when attribute for root is removed,
* * 'changeRootAttribute' when attribute for root changes.
*
* @event change
* @param {String} type Change type, possible option: 'insert', 'remove', 'reinsert', 'move', 'attribute'.
* @param {Object} data Additional information about the change.
* @param {module:engine/model/range~Range} data.range Range in model containing changed nodes. Note that the range state is
* after changes has been done, i.e. for 'remove' the range will be in the {@link #graveyard graveyard root}.
* This is `undefined` for "...root..." types.
* @param {module:engine/model/position~Position} [data.sourcePosition] Change source position.
* Exists for 'remove', 'reinsert' and 'move'.
* Note that this position state is before changes has been done, i.e. for 'reinsert' the source position will be in the
* {@link #graveyard graveyard root}.
* @param {String} [data.key] Only for attribute types. Key of changed / inserted / removed attribute.
* @param {*} [data.oldValue] Only for 'removeAttribute', 'removeRootAttribute', 'changeAttribute' or
* 'changeRootAttribute' type.
* @param {*} [data.newValue] Only for 'addAttribute', 'addRootAttribute', 'changeAttribute' or
* 'changeRootAttribute' type.
* @param {module:engine/model/rootelement~RootElement} [changeInfo.root] Root element which attributes got changed. This is defined
* only for root types.
* @param {module:engine/model/batch~Batch} batch A {@link module:engine/model/batch~Batch batch}
* of changes which this change is a part of.
*/
/**
* Fired when all queued document changes are done. See {@link #enqueueChanges}.
*
* @event changesDone
*/
}
mix( Document, EmitterMixin );
// Checks whether given range boundary position is valid for document selection, meaning that is not between
// unicode surrogate pairs or base character and combining marks.
function validateTextNodePosition( rangeBoundary ) {
const textNode = rangeBoundary.textNode;
if ( textNode ) {
const data = textNode.data;
const offset = rangeBoundary.offset - textNode.startOffset;
return !isInsideSurrogatePair( data, offset ) && !isInsideCombinedSymbol( data, offset );
}
return true;
}
// Generator function returning values from provided walkers, switching between them at each iteration. If only one walker
// is provided it will return data only from that walker.
//
// @param {module:engine/module/treewalker~TreeWalker} [backward] Walker iterating in backward direction.
// @param {module:engine/module/treewalker~TreeWalker} [forward] Walker iterating in forward direction.
// @returns {Iterable.<Object>} Object returned at each iteration contains `value` and `walker` (informing which walker returned
// given value) fields.
function* combineWalkers( backward, forward ) {
let done = false;
while ( !done ) {
done = true;
if ( backward ) {
const step = backward.next();
if ( !step.done ) {
done = false;
yield{
walker: backward,
value: step.value
};
}
}
if ( forward ) {
const step = forward.next();
if ( !step.done ) {
done = false;
yield{
walker: forward,
value: step.value
};
}
}
}
}