This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
markeroperation.js
149 lines (129 loc) · 3.91 KB
/
markeroperation.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
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/model/operation/markeroperation
*/
import Operation from './operation';
import Range from '../range';
/**
* @extends module:engine/model/operation/operation~Operation
*/
export default class MarkerOperation extends Operation {
/**
* @param {String} name Marker name.
* @param {module:engine/model/range~Range} oldRange Marker range before the change.
* @param {module:engine/model/range~Range} newRange Marker range after the change.
* @param {module:engine/model/markercollection~MarkerCollection} markers Marker collection on which change should be executed.
* @param {Boolean} affectsData Specifies whether the marker operation affects the data produced by the data pipeline
* (is persisted in the editor's data).
* @param {Number|null} 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( name, oldRange, newRange, markers, affectsData, baseVersion ) {
super( baseVersion );
/**
* Marker name.
*
* @readonly
* @member {String}
*/
this.name = name;
/**
* Marker range before the change.
*
* @readonly
* @member {module:engine/model/range~Range}
*/
this.oldRange = oldRange ? oldRange.clone() : null;
/**
* Marker range after the change.
*
* @readonly
* @member {module:engine/model/range~Range}
*/
this.newRange = newRange ? newRange.clone() : null;
/**
* Specifies whether the marker operation affects the data produced by the data pipeline
* (is persisted in the editor's data).
*
* @readonly
* @member {Boolean}
*/
this.affectsData = affectsData;
/**
* Marker collection on which change should be executed.
*
* @private
* @member {module:engine/model/markercollection~MarkerCollection}
*/
this._markers = markers;
}
/**
* @inheritDoc
*/
get type() {
return 'marker';
}
/**
* Creates and returns an operation that has the same parameters as this operation.
*
* @returns {module:engine/model/operation/markeroperation~MarkerOperation} Clone of this operation.
*/
clone() {
return new MarkerOperation( this.name, this.oldRange, this.newRange, this._markers, this.affectsData, this.baseVersion );
}
/**
* See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
*
* @returns {module:engine/model/operation/markeroperation~MarkerOperation}
*/
getReversed() {
return new MarkerOperation( this.name, this.newRange, this.oldRange, this._markers, this.affectsData, this.baseVersion + 1 );
}
/**
* @inheritDoc
*/
_execute() {
const type = this.newRange ? '_set' : '_remove';
this._markers[ type ]( this.name, this.newRange, true, this.affectsData );
}
/**
* @inheritDoc
*/
toJSON() {
const json = super.toJSON();
if ( this.oldRange ) {
json.oldRange = this.oldRange.toJSON();
}
if ( this.newRange ) {
json.newRange = this.newRange.toJSON();
}
delete json._markers;
return json;
}
/**
* @inheritDoc
*/
static get className() {
return 'MarkerOperation';
}
/**
* Creates `MarkerOperation` object from deserialized object, i.e. from parsed JSON string.
*
* @param {Object} json Deserialized JSON object.
* @param {module:engine/model/document~Document} document Document on which this operation will be applied.
* @returns {module:engine/model/operation/markeroperation~MarkerOperation}
*/
static fromJSON( json, document ) {
return new MarkerOperation(
json.name,
json.oldRange ? Range.fromJSON( json.oldRange, document ) : null,
json.newRange ? Range.fromJSON( json.newRange, document ) : null,
document.model.markers,
json.affectsData,
json.baseVersion
);
}
}