@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.
@@ -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
- * Creates an instance of the command.
24
- *
25
- * @param {module:core/editor/editor~Editor} editor
26
- * @param {'forward'|'backward'} direction The directionality of the delete describing in what direction it
27
- * should consume the content when the selection is collapsed.
28
- */
29
- constructor( editor, direction ) {
30
- super( editor );
31
-
32
- /**
33
- * The directionality of the delete describing in what direction it should
34
- * consume the content when the selection is collapsed.
35
- *
36
- * @readonly
37
- * @member {'forward'|'backward'} #direction
38
- */
39
- this.direction = direction;
40
-
41
- /**
42
- * Delete's change buffer used to group subsequent changes into batches.
43
- *
44
- * @readonly
45
- * @private
46
- * @type {module:typing/utils/changebuffer~ChangeBuffer}
47
- */
48
- this._buffer = new ChangeBuffer( editor.model, editor.config.get( 'typing.undoStep' ) );
49
- }
50
-
51
- /**
52
- * The current change buffer.
53
- *
54
- * @type {module:typing/utils/changebuffer~ChangeBuffer}
55
- */
56
- get buffer() {
57
- return this._buffer;
58
- }
59
-
60
- /**
61
- * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
62
- * or a piece of content in the {@link #direction defined direction}.
63
- *
64
- * @fires execute
65
- * @param {Object} [options] The command options.
66
- * @param {'character'|'codePoint'|'word'} [options.unit='character']
67
- * See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
68
- * @param {Number} [options.sequence=1] A number describing which subsequent delete event it is without the key being released.
69
- * See the {@link module:engine/view/document~Document#event:delete} event data.
70
- * @param {module:engine/model/selection~Selection} [options.selection] Selection to remove. If not set, current model selection
71
- * will be used.
72
- */
73
- execute( options = {} ) {
74
- const model = this.editor.model;
75
- const doc = model.document;
76
-
77
- model.enqueueChange( this._buffer.batch, writer => {
78
- this._buffer.lock();
79
-
80
- const selection = writer.createSelection( options.selection || doc.selection );
81
- const sequence = options.sequence || 1;
82
-
83
- // Do not replace the whole selected content if selection was collapsed.
84
- // This prevents such situation:
85
- //
86
- // <h1></h1><p>[]</p> --> <h1>[</h1><p>]</p> --> <p></p>
87
- // starting content --> after `modifySelection` --> after `deleteContent`.
88
- const doNotResetEntireContent = selection.isCollapsed;
89
-
90
- // Try to extend the selection in the specified direction.
91
- if ( selection.isCollapsed ) {
92
- model.modifySelection( selection, {
93
- direction: this.direction,
94
- unit: options.unit,
95
- treatEmojiAsSingleUnit: true
96
- } );
97
- }
98
-
99
- // Check if deleting in an empty editor. See #61.
100
- if ( this._shouldEntireContentBeReplacedWithParagraph( sequence ) ) {
101
- this._replaceEntireContentWithParagraph( writer );
102
-
103
- return;
104
- }
105
-
106
- // Check if deleting in the first empty block.
107
- // See https://github.com/ckeditor/ckeditor5/issues/8137.
108
- if ( this._shouldReplaceFirstBlockWithParagraph( selection, sequence ) ) {
109
- this.editor.execute( 'paragraph', { selection } );
110
-
111
- return;
112
- }
113
-
114
- // If selection is still collapsed, then there's nothing to delete.
115
- if ( selection.isCollapsed ) {
116
- return;
117
- }
118
-
119
- let changeCount = 0;
120
-
121
- selection.getFirstRange().getMinimalFlatRanges().forEach( range => {
122
- changeCount += count(
123
- range.getWalker( { singleCharacters: true, ignoreElementEnd: true, shallow: true } )
124
- );
125
- } );
126
-
127
- model.deleteContent( selection, {
128
- doNotResetEntireContent,
129
- direction: this.direction
130
- } );
131
-
132
- this._buffer.input( changeCount );
133
-
134
- writer.setSelection( selection );
135
-
136
- this._buffer.unlock();
137
- } );
138
- }
139
-
140
- /**
141
- * If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
142
- * editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
143
- * (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
144
- *
145
- * But, if the user pressed the key in an empty editable for the first time,
146
- * we want to replace the entire content with a paragraph if:
147
- *
148
- * * the current limit element is empty,
149
- * * the paragraph is allowed in the limit element,
150
- * * the limit doesn't already have a paragraph inside.
151
- *
152
- * See https://github.com/ckeditor/ckeditor5-typing/issues/61.
153
- *
154
- * @private
155
- * @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
156
- * @returns {Boolean}
157
- */
158
- _shouldEntireContentBeReplacedWithParagraph( sequence ) {
159
- // Does nothing if user pressed and held the "Backspace" or "Delete" key.
160
- if ( sequence > 1 ) {
161
- return false;
162
- }
163
-
164
- const model = this.editor.model;
165
- const doc = model.document;
166
- const selection = doc.selection;
167
- const limitElement = model.schema.getLimitElement( selection );
168
-
169
- // If a collapsed selection contains the whole content it means that the content is empty
170
- // (from the user perspective).
171
- const limitElementIsEmpty = selection.isCollapsed && selection.containsEntireContent( limitElement );
172
-
173
- if ( !limitElementIsEmpty ) {
174
- return false;
175
- }
176
-
177
- if ( !model.schema.checkChild( limitElement, 'paragraph' ) ) {
178
- return false;
179
- }
180
-
181
- const limitElementFirstChild = limitElement.getChild( 0 );
182
-
183
- // Does nothing if the limit element already contains only a paragraph.
184
- // We ignore the case when paragraph might have some inline elements (<p><inlineWidget>[]</inlineWidget></p>)
185
- // because we don't support such cases yet and it's unclear whether inlineWidget shouldn't be a limit itself.
186
- if ( limitElementFirstChild && limitElementFirstChild.name === 'paragraph' ) {
187
- return false;
188
- }
189
-
190
- return true;
191
- }
192
-
193
- /**
194
- * The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
195
- *
196
- * @private
197
- * @param {module:engine/model/writer~Writer} writer The model writer.
198
- */
199
- _replaceEntireContentWithParagraph( writer ) {
200
- const model = this.editor.model;
201
- const doc = model.document;
202
- const selection = doc.selection;
203
- const limitElement = model.schema.getLimitElement( selection );
204
- const paragraph = writer.createElement( 'paragraph' );
205
-
206
- writer.remove( writer.createRangeIn( limitElement ) );
207
- writer.insert( paragraph, limitElement );
208
-
209
- writer.setSelection( paragraph, 0 );
210
- }
211
-
212
- /**
213
- * Checks if the selection is inside an empty element that is the first child of the limit element
214
- * and should be replaced with a paragraph.
215
- *
216
- * @private
217
- * @param {module:engine/model/selection~Selection} selection The selection.
218
- * @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
219
- * @returns {Boolean}
220
- */
221
- _shouldReplaceFirstBlockWithParagraph( selection, sequence ) {
222
- const model = this.editor.model;
223
-
224
- // Does nothing if user pressed and held the "Backspace" key or it was a "Delete" button.
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
  }