/
insertoperation.ts
192 lines (161 loc) · 5.36 KB
/
insertoperation.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
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
/**
* @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/operation/insertoperation
*/
import Operation from './operation.js';
import Position from '../position.js';
import NodeList from '../nodelist.js';
import MoveOperation from './moveoperation.js';
import { _insert, _normalizeNodes, type NodeSet } from './utils.js';
import Text from '../text.js';
import Element from '../element.js';
import type { Selectable } from '../selection.js';
import type Document from '../document.js';
import { CKEditorError } from '@ckeditor/ckeditor5-utils';
/**
* Operation to insert one or more nodes at given position in the model.
*/
export default class InsertOperation extends Operation {
/**
* Position of insertion.
*
* @readonly
*/
public position: Position;
/**
* List of nodes to insert.
*
* @readonly
*/
public nodes: NodeList;
/**
* Flag deciding how the operation should be transformed. If set to `true`, nodes might get additional attributes
* during operational transformation. This happens when the operation insertion position is inside of a range
* where attributes have changed.
*/
public shouldReceiveAttributes: boolean;
/**
* Creates an insert operation.
*
* @param position Position of insertion.
* @param nodes The list of nodes to be inserted.
* @param baseVersion Document {@link module:engine/model/document~Document#version} on which operation
* can be applied or `null` if the operation operates on detached (non-document) tree.
*/
constructor( position: Position, nodes: NodeSet, baseVersion: number | null ) {
super( baseVersion );
this.position = position.clone();
this.position.stickiness = 'toNone';
this.nodes = new NodeList( _normalizeNodes( nodes ) );
this.shouldReceiveAttributes = false;
}
/**
* @inheritDoc
*/
public get type(): 'insert' {
return 'insert';
}
/**
* Total offset size of inserted nodes.
*/
public get howMany(): number {
return this.nodes.maxOffset;
}
/**
* @inheritDoc
*/
public get affectedSelectable(): Selectable {
return this.position.clone();
}
/**
* Creates and returns an operation that has the same parameters as this operation.
*/
public clone(): InsertOperation {
const nodes = new NodeList( [ ...this.nodes ].map( node => node._clone( true ) ) );
const insert = new InsertOperation( this.position, nodes, this.baseVersion );
insert.shouldReceiveAttributes = this.shouldReceiveAttributes;
return insert;
}
/**
* See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
*/
public getReversed(): Operation {
const graveyard = this.position.root.document!.graveyard;
const gyPosition = new Position( graveyard, [ 0 ] );
return new MoveOperation( this.position, this.nodes.maxOffset, gyPosition, this.baseVersion! + 1 );
}
/**
* @inheritDoc
* @internal
*/
public override _validate(): void {
const targetElement = this.position.parent;
if ( !targetElement || targetElement.maxOffset < this.position.offset ) {
/**
* Insertion position is invalid.
*
* @error insert-operation-position-invalid
*/
throw new CKEditorError(
'insert-operation-position-invalid',
this
);
}
}
/**
* @inheritDoc
* @internal
*/
public _execute(): void {
// What happens here is that we want original nodes be passed to writer because we want original nodes
// to be inserted to the model. But in InsertOperation, we want to keep those nodes as they were added
// to the operation, not modified. For example, text nodes can get merged or cropped while Elements can
// get children. It is important that InsertOperation has the copy of original nodes in intact state.
const originalNodes = this.nodes;
this.nodes = new NodeList( [ ...originalNodes ].map( node => node._clone( true ) ) );
_insert( this.position, originalNodes );
}
/**
* @inheritDoc
*/
public override toJSON(): unknown {
const json: any = super.toJSON();
json.position = this.position.toJSON();
json.nodes = this.nodes.toJSON();
return json;
}
/**
* @inheritDoc
*/
public static override get className(): string {
return 'InsertOperation';
}
/**
* Creates `InsertOperation` object from deserialized object, i.e. from parsed JSON string.
*
* @param json Deserialized JSON object.
* @param document Document on which this operation will be applied.
*/
public static override fromJSON( json: any, document: Document ): InsertOperation {
const children = [];
for ( const child of json.nodes ) {
if ( child.name ) {
// If child has name property, it is an Element.
children.push( Element.fromJSON( child ) );
} else {
// Otherwise, it is a Text node.
children.push( Text.fromJSON( child ) );
}
}
const insert = new InsertOperation( Position.fromJSON( json.position, document ), children, json.baseVersion );
insert.shouldReceiveAttributes = json.shouldReceiveAttributes;
return insert;
}
// @if CK_DEBUG_ENGINE // public override toString(): string {
// @if CK_DEBUG_ENGINE // const nodeString = this.nodes.length > 1 ? `[ ${ this.nodes.length } ]` : this.nodes.getNode( 0 );
// @if CK_DEBUG_ENGINE // return `InsertOperation( ${ this.baseVersion } ): ${ nodeString } -> ${ this.position }`;
// @if CK_DEBUG_ENGINE // }
}