This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
detachoperation.js
103 lines (88 loc) · 2.67 KB
/
detachoperation.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
/**
* @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/operation/detachoperation
*/
import Operation from './operation';
import Range from '../range';
import { _remove } from './utils';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
// @if CK_DEBUG_ENGINE // const ModelRange = require( '../range' ).default;
/**
* Operation to permanently remove node from detached root.
* Note this operation is only a local operation and won't be send to the other clients.
*
* @extends module:engine/model/operation/operation~Operation
*/
export default class DetachOperation extends Operation {
/**
* Creates an insert operation.
*
* @param {module:engine/model/position~Position} sourcePosition
* Position before the first {@link module:engine/model/item~Item model item} to move.
* @param {Number} howMany Offset size of moved range. Moved range will start from `sourcePosition` and end at
* `sourcePosition` with offset shifted by `howMany`.
*/
constructor( sourcePosition, howMany ) {
super( null );
/**
* Position before the first {@link module:engine/model/item~Item model item} to detach.
*
* @member {module:engine/model/position~Position} #sourcePosition
*/
this.sourcePosition = sourcePosition.clone();
/**
* Offset size of moved range.
*
* @member {Number} #howMany
*/
this.howMany = howMany;
}
/**
* @inheritDoc
*/
get type() {
return 'detach';
}
/**
* @inheritDoc
*/
toJSON() {
const json = super.toJSON();
json.sourcePosition = this.sourcePosition.toJSON();
return json;
}
/**
* @inheritDoc
*/
_validate() {
if ( this.sourcePosition.root.document ) {
/**
* Cannot detach document node.
*
* @error detach-operation-on-document-node
*/
throw new CKEditorError( 'detach-operation-on-document-node: Cannot detach document node.', this );
}
}
/**
* @inheritDoc
*/
_execute() {
_remove( Range._createFromPositionAndShift( this.sourcePosition, this.howMany ) );
}
/**
* @inheritDoc
*/
static get className() {
return 'DetachOperation';
}
// @if CK_DEBUG_ENGINE // toString() {
// @if CK_DEBUG_ENGINE // const range = ModelRange._createFromPositionAndShift( this.sourcePosition, this.howMany );
// @if CK_DEBUG_ENGINE // const nodes = Array.from( range.getItems() );
// @if CK_DEBUG_ENGINE // const nodeString = nodes.length > 1 ? `[ ${ nodes.length } ]` : nodes[ 0 ];
// @if CK_DEBUG_ENGINE // return `DetachOperation( ${ this.baseVersion } ): ${ nodeString } -> ${ range }`;
// @if CK_DEBUG_ENGINE // }
}