-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
liveposition.ts
159 lines (136 loc) · 5.24 KB
/
liveposition.ts
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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/model/liveposition
*/
import Position, { type PositionOffset, type PositionStickiness } from './position.js';
import type { ModelApplyOperationEvent } from './model.js';
import type DocumentFragment from './documentfragment.js';
import type Item from './item.js';
import type Operation from './operation/operation.js';
import type RootElement from './rootelement.js';
import { CKEditorError, EmitterMixin } from '@ckeditor/ckeditor5-utils';
/**
* `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.
*/
export default class LivePosition extends EmitterMixin( Position ) {
/**
* Root of the position path.
*/
declare public readonly root: RootElement;
/**
* Creates a live position.
*
* @see module:engine/model/position~Position
*/
constructor( root: RootElement, path: Array<number>, stickiness: PositionStickiness = '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).
*/
public detach(): void {
this.stopListening();
}
/**
* Creates a {@link module:engine/model/position~Position position instance}, which is equal to this live position.
*/
public toPosition(): Position {
return new Position( this.root, this.path.slice(), this.stickiness );
}
/**
* Creates a `LivePosition` instance that is equal to position.
*/
public static fromPosition( position: Position, stickiness?: PositionStickiness ): LivePosition {
return new this( position.root as RootElement, position.path.slice(), stickiness ? stickiness : position.stickiness );
}
/**
* @internal
* @see module:engine/model/position~Position._createAfter
*/
declare public static readonly _createAfter: ( item: Item | DocumentFragment, stickiness?: PositionStickiness ) => LivePosition;
/**
* @internal
* @see module:engine/model/position~Position._createBefore
*/
declare public static readonly _createBefore: ( item: Item | DocumentFragment, stickiness?: PositionStickiness ) => LivePosition;
/**
* @internal
* @see module:engine/model/position~Position._createAt
*/
declare public static readonly _createAt: (
itemOrPosition: Item | Position | DocumentFragment,
offset?: PositionOffset,
stickiness?: PositionStickiness
) => LivePosition;
}
// The magic of type inference using `is` method is centralized in `TypeCheckable` class.
// Proper overload would interfere with that.
LivePosition.prototype.is = function( type: string ): boolean {
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';
};
/**
* 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}.
*/
function bindWithDocument( this: LivePosition ) {
this.listenTo<ModelApplyOperationEvent>(
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.
*/
function transform( this: LivePosition, operation: Operation ) {
const result = this.getTransformedByOperation( operation );
if ( !this.isEqual( result ) ) {
const oldPosition = this.toPosition();
( this as any ).path = result.path;
( this as any ).root = result.root;
this.fire<LivePositionChangeEvent>( 'change', oldPosition );
}
}
/**
* Fired when `LivePosition` instance is changed due to changes on {@link module:engine/model/document~Document}.
*
* @eventName ~LivePosition#change
* @param oldPosition Position equal to this live position before it got changed.
*/
export type LivePositionChangeEvent = {
name: 'change';
args: [ oldPosition: Position ];
};