/
liveposition.js
182 lines (161 loc) · 6.11 KB
/
liveposition.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/model/liveposition
*/
import Position from './position';
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
/**
* `LivePosition` is a type of {@link module:engine/model/position~Position Position}
* that updates itself as {@link module:engine/model/document~Document document}
* is changed through operations. It may be used as a bookmark.
*
* **Note:** Contrary to {@link module:engine/model/position~Position}, `LivePosition` works only in roots that are
* {@link module:engine/model/rootelement~RootElement}.
* If {@link module:engine/model/documentfragment~DocumentFragment} is passed, error will be thrown.
*
* **Note:** Be very careful when dealing with `LivePosition`. Each `LivePosition` instance bind events that might
* have to be unbound.
* Use {@link module:engine/model/liveposition~LivePosition#detach} whenever you don't need `LivePosition` anymore.
*
* @extends module:engine/model/position~Position
*/
export default class LivePosition extends Position {
/**
* Creates a live position.
*
* @see module:engine/model/position~Position
* @param {module:engine/model/rootelement~RootElement} root
* @param {Array.<Number>} path
* @param {module:engine/model/position~PositionStickiness} [stickiness]
*/
constructor( root, path, stickiness = 'toNone' ) {
super( root, path, stickiness );
if ( !this.root.is( 'rootElement' ) ) {
/**
* LivePosition's root has to be an instance of RootElement.
*
* @error model-liveposition-root-not-rootelement
*/
throw new CKEditorError( 'model-liveposition-root-not-rootelement', root );
}
bindWithDocument.call( this );
}
/**
* Unbinds all events previously bound by `LivePosition`. Use it whenever you don't need `LivePosition` instance
* anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
* referring to it).
*/
detach() {
this.stopListening();
}
/**
* Checks whether this object is of the given.
*
* livePosition.is( 'position' ); // -> true
* livePosition.is( 'model:position' ); // -> true
* livePosition.is( 'liveposition' ); // -> true
* livePosition.is( 'model:livePosition' ); // -> true
*
* livePosition.is( 'view:position' ); // -> false
* livePosition.is( 'documentSelection' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type === 'livePosition' || type === 'model:livePosition' ||
// From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
type == 'position' || type === 'model:position';
}
/**
* Creates a {@link module:engine/model/position~Position position instance}, which is equal to this live position.
*
* @returns {module:engine/model/position~Position}
*/
toPosition() {
return new Position( this.root, this.path.slice(), this.stickiness );
}
/**
* Creates a `LivePosition` instance that is equal to position.
*
* @param {module:engine/model/position~Position} position
* @param {module:engine/model/position~PositionStickiness} [stickiness]
* @returns {module:engine/model/position~Position}
*/
static fromPosition( position, stickiness ) {
return new this( position.root, position.path.slice(), stickiness ? stickiness : position.stickiness );
}
/**
* @static
* @protected
* @method module:engine/model/liveposition~LivePosition._createAfter
* @see module:engine/model/position~Position._createAfter
* @param {module:engine/model/node~Node} node
* @param {module:engine/model/position~PositionStickiness} [stickiness='toNone']
* @returns {module:engine/model/liveposition~LivePosition}
*/
/**
* @static
* @protected
* @method module:engine/model/liveposition~LivePosition._createBefore
* @see module:engine/model/position~Position._createBefore
* @param {module:engine/model/node~Node} node
* @param {module:engine/model/position~PositionStickiness} [stickiness='toNone']
* @returns {module:engine/model/liveposition~LivePosition}
*/
/**
* @static
* @protected
* @method module:engine/model/liveposition~LivePosition._createAt
* @see module:engine/model/position~Position._createAt
* @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
* @param {Number|'end'|'before'|'after'} [offset]
* @param {module:engine/model/position~PositionStickiness} [stickiness='toNone']
* @returns {module:engine/model/liveposition~LivePosition}
*/
/**
* Fired when `LivePosition` instance is changed due to changes on {@link module:engine/model/document~Document}.
*
* @event module:engine/model/liveposition~LivePosition#change
* @param {module:engine/model/position~Position} oldPosition Position equal to this live position before it got changed.
*/
}
// Binds this `LivePosition` to the {@link module:engine/model/document~Document document} that owns
// this position's {@link module:engine/model/position~Position#root root}.
//
// @private
function bindWithDocument() {
this.listenTo(
this.root.document.model,
'applyOperation',
( event, args ) => {
const operation = args[ 0 ];
if ( !operation.isDocumentOperation ) {
return;
}
transform.call( this, operation );
},
{ priority: 'low' }
);
}
// Updates this position accordingly to the updates applied to the model. Bases on change events.
//
// @private
// @param {module:engine/model/operation/operation~Operation} operation Executed operation.
function transform( operation ) {
const result = this.getTransformedByOperation( operation );
if ( !this.isEqual( result ) ) {
const oldPosition = this.toPosition();
this.path = result.path;
this.root = result.root;
this.fire( 'change', oldPosition );
}
}
mix( LivePosition, EmitterMixin );