@ckeditor/ckeditor5-typing 35.2.0 → 35.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +31 -18
- package/src/delete.js +68 -123
- package/src/deletecommand.js +205 -242
- package/src/deleteobserver.js +205 -112
- package/src/index.js +0 -6
- package/src/input.js +125 -28
- package/src/inserttextcommand.js +96 -0
- package/src/inserttextobserver.js +104 -0
- package/src/texttransformation.js +174 -384
- package/src/textwatcher.js +131 -171
- package/src/twostepcaretmovement.js +300 -341
- package/src/typing.js +9 -43
- package/src/typingconfig.js +5 -0
- package/src/utils/changebuffer.js +142 -151
- package/src/utils/findattributerange.js +12 -24
- package/src/utils/getlasttextline.js +11 -29
- package/src/utils/inlinehighlight.js +38 -52
- package/src/inputcommand.js +0 -100
- package/src/utils/injecttypingmutationshandling.js +0 -331
- package/src/utils/injectunsafekeystrokeshandling.js +0 -189
- package/src/utils/utils.js +0 -104
package/src/deletecommand.js
CHANGED
|
@@ -2,16 +2,14 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module typing/deletecommand
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import Command from '@ckeditor/ckeditor5-core/src/command';
|
|
11
9
|
import count from '@ckeditor/ckeditor5-utils/src/count';
|
|
12
|
-
|
|
13
10
|
import ChangeBuffer from './utils/changebuffer';
|
|
14
|
-
|
|
11
|
+
// Import config.typing declaration.
|
|
12
|
+
import './typingconfig';
|
|
15
13
|
/**
|
|
16
14
|
* The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
|
|
17
15
|
* <kbd>Backspace</kbd> keys.
|
|
@@ -19,242 +17,207 @@ import ChangeBuffer from './utils/changebuffer';
|
|
|
19
17
|
* @extends module:core/command~Command
|
|
20
18
|
*/
|
|
21
19
|
export default class DeleteCommand extends Command {
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if ( sequence > 1 || this.direction != 'backward' ) {
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if ( !selection.isCollapsed ) {
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const position = selection.getFirstPosition();
|
|
234
|
-
const limitElement = model.schema.getLimitElement( position );
|
|
235
|
-
const limitElementFirstChild = limitElement.getChild( 0 );
|
|
236
|
-
|
|
237
|
-
// Only elements that are direct children of the limit element can be replaced.
|
|
238
|
-
// Unwrapping from a block quote should be handled in a dedicated feature.
|
|
239
|
-
if ( position.parent != limitElementFirstChild ) {
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// A block should be replaced only if it was empty.
|
|
244
|
-
if ( !selection.containsEntireContent( limitElementFirstChild ) ) {
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Replace with a paragraph only if it's allowed there.
|
|
249
|
-
if ( !model.schema.checkChild( limitElement, 'paragraph' ) ) {
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// Does nothing if the limit element already contains only a paragraph.
|
|
254
|
-
if ( limitElementFirstChild.name == 'paragraph' ) {
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return true;
|
|
259
|
-
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates an instance of the command.
|
|
22
|
+
*
|
|
23
|
+
* @param {module:core/editor/editor~Editor} editor
|
|
24
|
+
* @param {'forward'|'backward'} direction The directionality of the delete describing in what direction it
|
|
25
|
+
* should consume the content when the selection is collapsed.
|
|
26
|
+
*/
|
|
27
|
+
constructor(editor, direction) {
|
|
28
|
+
super(editor);
|
|
29
|
+
/**
|
|
30
|
+
* The directionality of the delete describing in what direction it should
|
|
31
|
+
* consume the content when the selection is collapsed.
|
|
32
|
+
*
|
|
33
|
+
* @readonly
|
|
34
|
+
* @member {'forward'|'backward'} #direction
|
|
35
|
+
*/
|
|
36
|
+
this.direction = direction;
|
|
37
|
+
/**
|
|
38
|
+
* Delete's change buffer used to group subsequent changes into batches.
|
|
39
|
+
*
|
|
40
|
+
* @readonly
|
|
41
|
+
* @private
|
|
42
|
+
* @type {module:typing/utils/changebuffer~ChangeBuffer}
|
|
43
|
+
*/
|
|
44
|
+
this._buffer = new ChangeBuffer(editor.model, editor.config.get('typing.undoStep'));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The current change buffer.
|
|
48
|
+
*
|
|
49
|
+
* @type {module:typing/utils/changebuffer~ChangeBuffer}
|
|
50
|
+
*/
|
|
51
|
+
get buffer() {
|
|
52
|
+
return this._buffer;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
|
|
56
|
+
* or a piece of content in the {@link #direction defined direction}.
|
|
57
|
+
*
|
|
58
|
+
* @fires execute
|
|
59
|
+
* @param {Object} [options] The command options.
|
|
60
|
+
* @param {'character'|'codePoint'|'word'} [options.unit='character']
|
|
61
|
+
* See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
|
|
62
|
+
* @param {Number} [options.sequence=1] A number describing which subsequent delete event it is without the key being released.
|
|
63
|
+
* See the {@link module:engine/view/document~Document#event:delete} event data.
|
|
64
|
+
* @param {module:engine/model/selection~Selection} [options.selection] Selection to remove. If not set, current model selection
|
|
65
|
+
* will be used.
|
|
66
|
+
*/
|
|
67
|
+
execute(options = {}) {
|
|
68
|
+
const model = this.editor.model;
|
|
69
|
+
const doc = model.document;
|
|
70
|
+
model.enqueueChange(this._buffer.batch, writer => {
|
|
71
|
+
this._buffer.lock();
|
|
72
|
+
const selection = writer.createSelection(options.selection || doc.selection);
|
|
73
|
+
const sequence = options.sequence || 1;
|
|
74
|
+
// Do not replace the whole selected content if selection was collapsed.
|
|
75
|
+
// This prevents such situation:
|
|
76
|
+
//
|
|
77
|
+
// <h1></h1><p>[]</p> --> <h1>[</h1><p>]</p> --> <p></p>
|
|
78
|
+
// starting content --> after `modifySelection` --> after `deleteContent`.
|
|
79
|
+
const doNotResetEntireContent = selection.isCollapsed;
|
|
80
|
+
// Try to extend the selection in the specified direction.
|
|
81
|
+
if (selection.isCollapsed) {
|
|
82
|
+
model.modifySelection(selection, {
|
|
83
|
+
direction: this.direction,
|
|
84
|
+
unit: options.unit,
|
|
85
|
+
treatEmojiAsSingleUnit: true
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// Check if deleting in an empty editor. See #61.
|
|
89
|
+
if (this._shouldEntireContentBeReplacedWithParagraph(sequence)) {
|
|
90
|
+
this._replaceEntireContentWithParagraph(writer);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// Check if deleting in the first empty block.
|
|
94
|
+
// See https://github.com/ckeditor/ckeditor5/issues/8137.
|
|
95
|
+
if (this._shouldReplaceFirstBlockWithParagraph(selection, sequence)) {
|
|
96
|
+
this.editor.execute('paragraph', { selection });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// If selection is still collapsed, then there's nothing to delete.
|
|
100
|
+
if (selection.isCollapsed) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
let changeCount = 0;
|
|
104
|
+
selection.getFirstRange().getMinimalFlatRanges().forEach(range => {
|
|
105
|
+
changeCount += count(range.getWalker({ singleCharacters: true, ignoreElementEnd: true, shallow: true }));
|
|
106
|
+
});
|
|
107
|
+
// @if CK_DEBUG_TYPING // if ( window.logCKETyping ) {
|
|
108
|
+
// @if CK_DEBUG_TYPING // console.log( '%c[DeleteCommand]%c Delete content',
|
|
109
|
+
// @if CK_DEBUG_TYPING // 'font-weight: bold; color: green;', '',
|
|
110
|
+
// @if CK_DEBUG_TYPING // `[${ selection.getFirstPosition().path }]-[${ selection.getLastPosition().path }]`, options
|
|
111
|
+
// @if CK_DEBUG_TYPING // );
|
|
112
|
+
// @if CK_DEBUG_TYPING // }
|
|
113
|
+
model.deleteContent(selection, {
|
|
114
|
+
doNotResetEntireContent,
|
|
115
|
+
direction: this.direction
|
|
116
|
+
});
|
|
117
|
+
this._buffer.input(changeCount);
|
|
118
|
+
writer.setSelection(selection);
|
|
119
|
+
this._buffer.unlock();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
|
|
124
|
+
* editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
|
|
125
|
+
* (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
|
|
126
|
+
*
|
|
127
|
+
* But, if the user pressed the key in an empty editable for the first time,
|
|
128
|
+
* we want to replace the entire content with a paragraph if:
|
|
129
|
+
*
|
|
130
|
+
* * the current limit element is empty,
|
|
131
|
+
* * the paragraph is allowed in the limit element,
|
|
132
|
+
* * the limit doesn't already have a paragraph inside.
|
|
133
|
+
*
|
|
134
|
+
* See https://github.com/ckeditor/ckeditor5-typing/issues/61.
|
|
135
|
+
*
|
|
136
|
+
* @private
|
|
137
|
+
* @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
|
|
138
|
+
* @returns {Boolean}
|
|
139
|
+
*/
|
|
140
|
+
_shouldEntireContentBeReplacedWithParagraph(sequence) {
|
|
141
|
+
// Does nothing if user pressed and held the "Backspace" or "Delete" key.
|
|
142
|
+
if (sequence > 1) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
const model = this.editor.model;
|
|
146
|
+
const doc = model.document;
|
|
147
|
+
const selection = doc.selection;
|
|
148
|
+
const limitElement = model.schema.getLimitElement(selection);
|
|
149
|
+
// If a collapsed selection contains the whole content it means that the content is empty
|
|
150
|
+
// (from the user perspective).
|
|
151
|
+
const limitElementIsEmpty = selection.isCollapsed && selection.containsEntireContent(limitElement);
|
|
152
|
+
if (!limitElementIsEmpty) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (!model.schema.checkChild(limitElement, 'paragraph')) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const limitElementFirstChild = limitElement.getChild(0);
|
|
159
|
+
// Does nothing if the limit element already contains only a paragraph.
|
|
160
|
+
// We ignore the case when paragraph might have some inline elements (<p><inlineWidget>[]</inlineWidget></p>)
|
|
161
|
+
// because we don't support such cases yet and it's unclear whether inlineWidget shouldn't be a limit itself.
|
|
162
|
+
if (limitElementFirstChild && limitElementFirstChild.is('element', 'paragraph')) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
|
|
169
|
+
*
|
|
170
|
+
* @private
|
|
171
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
172
|
+
*/
|
|
173
|
+
_replaceEntireContentWithParagraph(writer) {
|
|
174
|
+
const model = this.editor.model;
|
|
175
|
+
const doc = model.document;
|
|
176
|
+
const selection = doc.selection;
|
|
177
|
+
const limitElement = model.schema.getLimitElement(selection);
|
|
178
|
+
const paragraph = writer.createElement('paragraph');
|
|
179
|
+
writer.remove(writer.createRangeIn(limitElement));
|
|
180
|
+
writer.insert(paragraph, limitElement);
|
|
181
|
+
writer.setSelection(paragraph, 0);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Checks if the selection is inside an empty element that is the first child of the limit element
|
|
185
|
+
* and should be replaced with a paragraph.
|
|
186
|
+
*
|
|
187
|
+
* @private
|
|
188
|
+
* @param {module:engine/model/selection~Selection} selection The selection.
|
|
189
|
+
* @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
|
|
190
|
+
* @returns {Boolean}
|
|
191
|
+
*/
|
|
192
|
+
_shouldReplaceFirstBlockWithParagraph(selection, sequence) {
|
|
193
|
+
const model = this.editor.model;
|
|
194
|
+
// Does nothing if user pressed and held the "Backspace" key or it was a "Delete" button.
|
|
195
|
+
if (sequence > 1 || this.direction != 'backward') {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
if (!selection.isCollapsed) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
const position = selection.getFirstPosition();
|
|
202
|
+
const limitElement = model.schema.getLimitElement(position);
|
|
203
|
+
const limitElementFirstChild = limitElement.getChild(0);
|
|
204
|
+
// Only elements that are direct children of the limit element can be replaced.
|
|
205
|
+
// Unwrapping from a block quote should be handled in a dedicated feature.
|
|
206
|
+
if (position.parent != limitElementFirstChild) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
// A block should be replaced only if it was empty.
|
|
210
|
+
if (!selection.containsEntireContent(limitElementFirstChild)) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
// Replace with a paragraph only if it's allowed there.
|
|
214
|
+
if (!model.schema.checkChild(limitElement, 'paragraph')) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
// Does nothing if the limit element already contains only a paragraph.
|
|
218
|
+
if (limitElementFirstChild.name == 'paragraph') {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
260
223
|
}
|