-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
optimization.js
144 lines (121 loc) · 3.86 KB
/
optimization.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
( function() {
var preventListener = true,
preventOptimization = false;
/**
* Sets editor listeners up to optimize the selection.
*
* **Note**: This method is called automatically during the editor initialization and should not be called manually.
*
* @since 4.13.0
* @static
* @see CKEDITOR.dom.selection.optimizeInElementEnds
* @param {CKEDITOR.editor} editor
* @member CKEDITOR.dom.selection
*/
CKEDITOR.dom.selection.setupEditorOptimization = function( editor ) {
editor.on( 'selectionCheck', function( evt ) {
if ( evt.data && !preventOptimization ) {
evt.data.optimizeInElementEnds();
}
preventOptimization = false;
} );
editor.on( 'contentDom', function() {
var editable = editor.editable();
if ( !editable ) {
return;
}
editable.attachListener( editable, 'keydown', function( evt ) {
this._.shiftPressed = evt.data.$.shiftKey;
}, this );
editable.attachListener( editable, 'keyup', function( evt ) {
this._.shiftPressed = evt.data.$.shiftKey;
}, this );
} );
};
/**
* Performs an optimization on the current selection if necessary.
*
* The general idea is to shrink the range to text when:
*
* * The range starts at the end of an element.
* * The range ends at the start of an element.
* * One of the range ends is anchored in a text node and another in an element.
*
* For example:
*
* ```html
* <p>{foo</p>
* <p>]bar</p>
* ```
*
* is optimized too:
*
* ```html
* <p>{foo}</p>
* <p>bar</p>
* ```
*
* @since 4.13.0
* @member CKEDITOR.dom.selection
*/
CKEDITOR.dom.selection.prototype.optimizeInElementEnds = function() {
var range = this.getRanges()[ 0 ],
editor = this.root.editor;
if ( !shouldOptimize( range, this ) ) {
return;
}
var oldRange = range.clone();
range.shrink( CKEDITOR.SHRINK_TEXT, false, { skipBogus: !CKEDITOR.env.webkit } );
preventListener = false;
preventRecurrency( editor, range, oldRange );
range.select();
preventListener = true;
};
function isText( node ) {
return node.type === CKEDITOR.NODE_TEXT;
}
// Returns `true` if any condition is met:
// * The range starts at the end of an element.
// * The range ends at the start of an element.
// * One end of the range is in text and another one is not.
//
// Always returns `false` when:
// * The Shift key is pressed.
// * The selection is fake.
// * The range is collapsed.
// * The range start and end container is the same element.
function shouldOptimize( range, selection ) {
if ( selection.root.editor._.shiftPressed ) {
return false;
}
if ( selection.isFake || range.isCollapsed || range.startContainer.equals( range.endContainer ) ) {
return false;
}
if ( range.endOffset === 0 ) {
return true;
}
var startsInText = isText( range.startContainer ),
endsInText = isText( range.endContainer ),
limit = startsInText ? range.startContainer.getLength() : range.startContainer.getChildCount();
return range.startOffset === limit || startsInText ^ endsInText;
}
// Prevent infinite recurrency when the browser does not allow the expected selection.
// There are two cases to handle:
// - When the browser modified the range in a way that it is the same as before the optimization.
// The second event is canceled, we do not need to fire listeners two times with the exact same selection.
// - When the browser does not modify the range.
// The event is not canceled, as the selection changed, however, the next optimization is prevented.
function preventRecurrency( editor, targetRange, initialRange ) {
editor.once( 'selectionCheck', function( evt ) {
if ( preventListener ) {
return;
}
var newRange = evt.data.getRanges()[ 0 ];
if ( initialRange.equals( newRange ) ) {
evt.cancel();
} else if ( targetRange.equals( newRange ) ) {
preventOptimization = true;
}
}, null, null, -1 );
}
} )();