@ckeditor/ckeditor5-typing 40.0.0 → 40.2.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.
@@ -1,83 +1,83 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module typing/deletecommand
7
- */
8
- import { Command, type Editor } from '@ckeditor/ckeditor5-core';
9
- import type { DocumentSelection, Selection } from '@ckeditor/ckeditor5-engine';
10
- import ChangeBuffer from './utils/changebuffer';
11
- /**
12
- * The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
13
- * <kbd>Backspace</kbd> keys.
14
- */
15
- export default class DeleteCommand extends Command {
16
- /**
17
- * The directionality of the delete describing in what direction it should
18
- * consume the content when the selection is collapsed.
19
- */
20
- readonly direction: 'forward' | 'backward';
21
- /**
22
- * Delete's change buffer used to group subsequent changes into batches.
23
- */
24
- private readonly _buffer;
25
- /**
26
- * Creates an instance of the command.
27
- *
28
- * @param direction The directionality of the delete describing in what direction it
29
- * should consume the content when the selection is collapsed.
30
- */
31
- constructor(editor: Editor, direction: 'forward' | 'backward');
32
- /**
33
- * The current change buffer.
34
- */
35
- get buffer(): ChangeBuffer;
36
- /**
37
- * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
38
- * or a piece of content in the {@link #direction defined direction}.
39
- *
40
- * @fires execute
41
- * @param options The command options.
42
- * @param options.unit See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
43
- * @param options.sequence A number describing which subsequent delete event it is without the key being released.
44
- * See the {@link module:engine/view/document~Document#event:delete} event data.
45
- * @param options.selection Selection to remove. If not set, current model selection will be used.
46
- */
47
- execute(options?: {
48
- unit?: 'character' | 'codePoint' | 'word';
49
- sequence?: number;
50
- selection?: Selection | DocumentSelection;
51
- }): void;
52
- /**
53
- * If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
54
- * editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
55
- * (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
56
- *
57
- * But, if the user pressed the key in an empty editable for the first time,
58
- * we want to replace the entire content with a paragraph if:
59
- *
60
- * * the current limit element is empty,
61
- * * the paragraph is allowed in the limit element,
62
- * * the limit doesn't already have a paragraph inside.
63
- *
64
- * See https://github.com/ckeditor/ckeditor5-typing/issues/61.
65
- *
66
- * @param sequence A number describing which subsequent delete event it is without the key being released.
67
- */
68
- private _shouldEntireContentBeReplacedWithParagraph;
69
- /**
70
- * The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
71
- *
72
- * @param writer The model writer.
73
- */
74
- private _replaceEntireContentWithParagraph;
75
- /**
76
- * Checks if the selection is inside an empty element that is the first child of the limit element
77
- * and should be replaced with a paragraph.
78
- *
79
- * @param selection The selection.
80
- * @param sequence A number describing which subsequent delete event it is without the key being released.
81
- */
82
- private _shouldReplaceFirstBlockWithParagraph;
83
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module typing/deletecommand
7
+ */
8
+ import { Command, type Editor } from '@ckeditor/ckeditor5-core';
9
+ import type { DocumentSelection, Selection } from '@ckeditor/ckeditor5-engine';
10
+ import ChangeBuffer from './utils/changebuffer';
11
+ /**
12
+ * The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
13
+ * <kbd>Backspace</kbd> keys.
14
+ */
15
+ export default class DeleteCommand extends Command {
16
+ /**
17
+ * The directionality of the delete describing in what direction it should
18
+ * consume the content when the selection is collapsed.
19
+ */
20
+ readonly direction: 'forward' | 'backward';
21
+ /**
22
+ * Delete's change buffer used to group subsequent changes into batches.
23
+ */
24
+ private readonly _buffer;
25
+ /**
26
+ * Creates an instance of the command.
27
+ *
28
+ * @param direction The directionality of the delete describing in what direction it
29
+ * should consume the content when the selection is collapsed.
30
+ */
31
+ constructor(editor: Editor, direction: 'forward' | 'backward');
32
+ /**
33
+ * The current change buffer.
34
+ */
35
+ get buffer(): ChangeBuffer;
36
+ /**
37
+ * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
38
+ * or a piece of content in the {@link #direction defined direction}.
39
+ *
40
+ * @fires execute
41
+ * @param options The command options.
42
+ * @param options.unit See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
43
+ * @param options.sequence A number describing which subsequent delete event it is without the key being released.
44
+ * See the {@link module:engine/view/document~Document#event:delete} event data.
45
+ * @param options.selection Selection to remove. If not set, current model selection will be used.
46
+ */
47
+ execute(options?: {
48
+ unit?: 'character' | 'codePoint' | 'word';
49
+ sequence?: number;
50
+ selection?: Selection | DocumentSelection;
51
+ }): void;
52
+ /**
53
+ * If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
54
+ * editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
55
+ * (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
56
+ *
57
+ * But, if the user pressed the key in an empty editable for the first time,
58
+ * we want to replace the entire content with a paragraph if:
59
+ *
60
+ * * the current limit element is empty,
61
+ * * the paragraph is allowed in the limit element,
62
+ * * the limit doesn't already have a paragraph inside.
63
+ *
64
+ * See https://github.com/ckeditor/ckeditor5-typing/issues/61.
65
+ *
66
+ * @param sequence A number describing which subsequent delete event it is without the key being released.
67
+ */
68
+ private _shouldEntireContentBeReplacedWithParagraph;
69
+ /**
70
+ * The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
71
+ *
72
+ * @param writer The model writer.
73
+ */
74
+ private _replaceEntireContentWithParagraph;
75
+ /**
76
+ * Checks if the selection is inside an empty element that is the first child of the limit element
77
+ * and should be replaced with a paragraph.
78
+ *
79
+ * @param selection The selection.
80
+ * @param sequence A number describing which subsequent delete event it is without the key being released.
81
+ */
82
+ private _shouldReplaceFirstBlockWithParagraph;
83
+ }
@@ -1,201 +1,201 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module typing/deletecommand
7
- */
8
- import { Command } from '@ckeditor/ckeditor5-core';
9
- import { count } from '@ckeditor/ckeditor5-utils';
10
- import ChangeBuffer from './utils/changebuffer';
11
- /**
12
- * The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
13
- * <kbd>Backspace</kbd> keys.
14
- */
15
- export default class DeleteCommand extends Command {
16
- /**
17
- * Creates an instance of the command.
18
- *
19
- * @param direction The directionality of the delete describing in what direction it
20
- * should consume the content when the selection is collapsed.
21
- */
22
- constructor(editor, direction) {
23
- super(editor);
24
- this.direction = direction;
25
- this._buffer = new ChangeBuffer(editor.model, editor.config.get('typing.undoStep'));
26
- // Since this command may execute on different selectable than selection, it should be checked directly in execute block.
27
- this._isEnabledBasedOnSelection = false;
28
- }
29
- /**
30
- * The current change buffer.
31
- */
32
- get buffer() {
33
- return this._buffer;
34
- }
35
- /**
36
- * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
37
- * or a piece of content in the {@link #direction defined direction}.
38
- *
39
- * @fires execute
40
- * @param options The command options.
41
- * @param options.unit See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
42
- * @param options.sequence A number describing which subsequent delete event it is without the key being released.
43
- * See the {@link module:engine/view/document~Document#event:delete} event data.
44
- * @param options.selection Selection to remove. If not set, current model selection will be used.
45
- */
46
- execute(options = {}) {
47
- const model = this.editor.model;
48
- const doc = model.document;
49
- model.enqueueChange(this._buffer.batch, writer => {
50
- this._buffer.lock();
51
- const selection = writer.createSelection(options.selection || doc.selection);
52
- // Don't execute command when selection is in non-editable place.
53
- if (!model.canEditAt(selection)) {
54
- return;
55
- }
56
- const sequence = options.sequence || 1;
57
- // Do not replace the whole selected content if selection was collapsed.
58
- // This prevents such situation:
59
- //
60
- // <h1></h1><p>[]</p> --> <h1>[</h1><p>]</p> --> <p></p>
61
- // starting content --> after `modifySelection` --> after `deleteContent`.
62
- const doNotResetEntireContent = selection.isCollapsed;
63
- // Try to extend the selection in the specified direction.
64
- if (selection.isCollapsed) {
65
- model.modifySelection(selection, {
66
- direction: this.direction,
67
- unit: options.unit,
68
- treatEmojiAsSingleUnit: true
69
- });
70
- }
71
- // Check if deleting in an empty editor. See #61.
72
- if (this._shouldEntireContentBeReplacedWithParagraph(sequence)) {
73
- this._replaceEntireContentWithParagraph(writer);
74
- return;
75
- }
76
- // Check if deleting in the first empty block.
77
- // See https://github.com/ckeditor/ckeditor5/issues/8137.
78
- if (this._shouldReplaceFirstBlockWithParagraph(selection, sequence)) {
79
- this.editor.execute('paragraph', { selection });
80
- return;
81
- }
82
- // If selection is still collapsed, then there's nothing to delete.
83
- if (selection.isCollapsed) {
84
- return;
85
- }
86
- let changeCount = 0;
87
- selection.getFirstRange().getMinimalFlatRanges().forEach(range => {
88
- changeCount += count(range.getWalker({ singleCharacters: true, ignoreElementEnd: true, shallow: true }));
89
- });
90
- // @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
91
- // @if CK_DEBUG_TYPING // console.log( '%c[DeleteCommand]%c Delete content',
92
- // @if CK_DEBUG_TYPING // 'font-weight: bold; color: green;', '',
93
- // @if CK_DEBUG_TYPING // `[${ selection.getFirstPosition()!.path }]-[${ selection.getLastPosition()!.path }]`, options
94
- // @if CK_DEBUG_TYPING // );
95
- // @if CK_DEBUG_TYPING // }
96
- model.deleteContent(selection, {
97
- doNotResetEntireContent,
98
- direction: this.direction
99
- });
100
- this._buffer.input(changeCount);
101
- writer.setSelection(selection);
102
- this._buffer.unlock();
103
- });
104
- }
105
- /**
106
- * If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
107
- * editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
108
- * (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
109
- *
110
- * But, if the user pressed the key in an empty editable for the first time,
111
- * we want to replace the entire content with a paragraph if:
112
- *
113
- * * the current limit element is empty,
114
- * * the paragraph is allowed in the limit element,
115
- * * the limit doesn't already have a paragraph inside.
116
- *
117
- * See https://github.com/ckeditor/ckeditor5-typing/issues/61.
118
- *
119
- * @param sequence A number describing which subsequent delete event it is without the key being released.
120
- */
121
- _shouldEntireContentBeReplacedWithParagraph(sequence) {
122
- // Does nothing if user pressed and held the "Backspace" or "Delete" key.
123
- if (sequence > 1) {
124
- return false;
125
- }
126
- const model = this.editor.model;
127
- const doc = model.document;
128
- const selection = doc.selection;
129
- const limitElement = model.schema.getLimitElement(selection);
130
- // If a collapsed selection contains the whole content it means that the content is empty
131
- // (from the user perspective).
132
- const limitElementIsEmpty = selection.isCollapsed && selection.containsEntireContent(limitElement);
133
- if (!limitElementIsEmpty) {
134
- return false;
135
- }
136
- if (!model.schema.checkChild(limitElement, 'paragraph')) {
137
- return false;
138
- }
139
- const limitElementFirstChild = limitElement.getChild(0);
140
- // Does nothing if the limit element already contains only a paragraph.
141
- // We ignore the case when paragraph might have some inline elements (<p><inlineWidget>[]</inlineWidget></p>)
142
- // because we don't support such cases yet and it's unclear whether inlineWidget shouldn't be a limit itself.
143
- if (limitElementFirstChild && limitElementFirstChild.is('element', 'paragraph')) {
144
- return false;
145
- }
146
- return true;
147
- }
148
- /**
149
- * The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
150
- *
151
- * @param writer The model writer.
152
- */
153
- _replaceEntireContentWithParagraph(writer) {
154
- const model = this.editor.model;
155
- const doc = model.document;
156
- const selection = doc.selection;
157
- const limitElement = model.schema.getLimitElement(selection);
158
- const paragraph = writer.createElement('paragraph');
159
- writer.remove(writer.createRangeIn(limitElement));
160
- writer.insert(paragraph, limitElement);
161
- writer.setSelection(paragraph, 0);
162
- }
163
- /**
164
- * Checks if the selection is inside an empty element that is the first child of the limit element
165
- * and should be replaced with a paragraph.
166
- *
167
- * @param selection The selection.
168
- * @param sequence A number describing which subsequent delete event it is without the key being released.
169
- */
170
- _shouldReplaceFirstBlockWithParagraph(selection, sequence) {
171
- const model = this.editor.model;
172
- // Does nothing if user pressed and held the "Backspace" key or it was a "Delete" button.
173
- if (sequence > 1 || this.direction != 'backward') {
174
- return false;
175
- }
176
- if (!selection.isCollapsed) {
177
- return false;
178
- }
179
- const position = selection.getFirstPosition();
180
- const limitElement = model.schema.getLimitElement(position);
181
- const limitElementFirstChild = limitElement.getChild(0);
182
- // Only elements that are direct children of the limit element can be replaced.
183
- // Unwrapping from a block quote should be handled in a dedicated feature.
184
- if (position.parent != limitElementFirstChild) {
185
- return false;
186
- }
187
- // A block should be replaced only if it was empty.
188
- if (!selection.containsEntireContent(limitElementFirstChild)) {
189
- return false;
190
- }
191
- // Replace with a paragraph only if it's allowed there.
192
- if (!model.schema.checkChild(limitElement, 'paragraph')) {
193
- return false;
194
- }
195
- // Does nothing if the limit element already contains only a paragraph.
196
- if (limitElementFirstChild.name == 'paragraph') {
197
- return false;
198
- }
199
- return true;
200
- }
201
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module typing/deletecommand
7
+ */
8
+ import { Command } from '@ckeditor/ckeditor5-core';
9
+ import { count } from '@ckeditor/ckeditor5-utils';
10
+ import ChangeBuffer from './utils/changebuffer';
11
+ /**
12
+ * The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
13
+ * <kbd>Backspace</kbd> keys.
14
+ */
15
+ export default class DeleteCommand extends Command {
16
+ /**
17
+ * Creates an instance of the command.
18
+ *
19
+ * @param direction The directionality of the delete describing in what direction it
20
+ * should consume the content when the selection is collapsed.
21
+ */
22
+ constructor(editor, direction) {
23
+ super(editor);
24
+ this.direction = direction;
25
+ this._buffer = new ChangeBuffer(editor.model, editor.config.get('typing.undoStep'));
26
+ // Since this command may execute on different selectable than selection, it should be checked directly in execute block.
27
+ this._isEnabledBasedOnSelection = false;
28
+ }
29
+ /**
30
+ * The current change buffer.
31
+ */
32
+ get buffer() {
33
+ return this._buffer;
34
+ }
35
+ /**
36
+ * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
37
+ * or a piece of content in the {@link #direction defined direction}.
38
+ *
39
+ * @fires execute
40
+ * @param options The command options.
41
+ * @param options.unit See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
42
+ * @param options.sequence A number describing which subsequent delete event it is without the key being released.
43
+ * See the {@link module:engine/view/document~Document#event:delete} event data.
44
+ * @param options.selection Selection to remove. If not set, current model selection will be used.
45
+ */
46
+ execute(options = {}) {
47
+ const model = this.editor.model;
48
+ const doc = model.document;
49
+ model.enqueueChange(this._buffer.batch, writer => {
50
+ this._buffer.lock();
51
+ const selection = writer.createSelection(options.selection || doc.selection);
52
+ // Don't execute command when selection is in non-editable place.
53
+ if (!model.canEditAt(selection)) {
54
+ return;
55
+ }
56
+ const sequence = options.sequence || 1;
57
+ // Do not replace the whole selected content if selection was collapsed.
58
+ // This prevents such situation:
59
+ //
60
+ // <h1></h1><p>[]</p> --> <h1>[</h1><p>]</p> --> <p></p>
61
+ // starting content --> after `modifySelection` --> after `deleteContent`.
62
+ const doNotResetEntireContent = selection.isCollapsed;
63
+ // Try to extend the selection in the specified direction.
64
+ if (selection.isCollapsed) {
65
+ model.modifySelection(selection, {
66
+ direction: this.direction,
67
+ unit: options.unit,
68
+ treatEmojiAsSingleUnit: true
69
+ });
70
+ }
71
+ // Check if deleting in an empty editor. See #61.
72
+ if (this._shouldEntireContentBeReplacedWithParagraph(sequence)) {
73
+ this._replaceEntireContentWithParagraph(writer);
74
+ return;
75
+ }
76
+ // Check if deleting in the first empty block.
77
+ // See https://github.com/ckeditor/ckeditor5/issues/8137.
78
+ if (this._shouldReplaceFirstBlockWithParagraph(selection, sequence)) {
79
+ this.editor.execute('paragraph', { selection });
80
+ return;
81
+ }
82
+ // If selection is still collapsed, then there's nothing to delete.
83
+ if (selection.isCollapsed) {
84
+ return;
85
+ }
86
+ let changeCount = 0;
87
+ selection.getFirstRange().getMinimalFlatRanges().forEach(range => {
88
+ changeCount += count(range.getWalker({ singleCharacters: true, ignoreElementEnd: true, shallow: true }));
89
+ });
90
+ // @if CK_DEBUG_TYPING // if ( ( window as any ).logCKETyping ) {
91
+ // @if CK_DEBUG_TYPING // console.log( '%c[DeleteCommand]%c Delete content',
92
+ // @if CK_DEBUG_TYPING // 'font-weight: bold; color: green;', '',
93
+ // @if CK_DEBUG_TYPING // `[${ selection.getFirstPosition()!.path }]-[${ selection.getLastPosition()!.path }]`, options
94
+ // @if CK_DEBUG_TYPING // );
95
+ // @if CK_DEBUG_TYPING // }
96
+ model.deleteContent(selection, {
97
+ doNotResetEntireContent,
98
+ direction: this.direction
99
+ });
100
+ this._buffer.input(changeCount);
101
+ writer.setSelection(selection);
102
+ this._buffer.unlock();
103
+ });
104
+ }
105
+ /**
106
+ * If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
107
+ * editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
108
+ * (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
109
+ *
110
+ * But, if the user pressed the key in an empty editable for the first time,
111
+ * we want to replace the entire content with a paragraph if:
112
+ *
113
+ * * the current limit element is empty,
114
+ * * the paragraph is allowed in the limit element,
115
+ * * the limit doesn't already have a paragraph inside.
116
+ *
117
+ * See https://github.com/ckeditor/ckeditor5-typing/issues/61.
118
+ *
119
+ * @param sequence A number describing which subsequent delete event it is without the key being released.
120
+ */
121
+ _shouldEntireContentBeReplacedWithParagraph(sequence) {
122
+ // Does nothing if user pressed and held the "Backspace" or "Delete" key.
123
+ if (sequence > 1) {
124
+ return false;
125
+ }
126
+ const model = this.editor.model;
127
+ const doc = model.document;
128
+ const selection = doc.selection;
129
+ const limitElement = model.schema.getLimitElement(selection);
130
+ // If a collapsed selection contains the whole content it means that the content is empty
131
+ // (from the user perspective).
132
+ const limitElementIsEmpty = selection.isCollapsed && selection.containsEntireContent(limitElement);
133
+ if (!limitElementIsEmpty) {
134
+ return false;
135
+ }
136
+ if (!model.schema.checkChild(limitElement, 'paragraph')) {
137
+ return false;
138
+ }
139
+ const limitElementFirstChild = limitElement.getChild(0);
140
+ // Does nothing if the limit element already contains only a paragraph.
141
+ // We ignore the case when paragraph might have some inline elements (<p><inlineWidget>[]</inlineWidget></p>)
142
+ // because we don't support such cases yet and it's unclear whether inlineWidget shouldn't be a limit itself.
143
+ if (limitElementFirstChild && limitElementFirstChild.is('element', 'paragraph')) {
144
+ return false;
145
+ }
146
+ return true;
147
+ }
148
+ /**
149
+ * The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
150
+ *
151
+ * @param writer The model writer.
152
+ */
153
+ _replaceEntireContentWithParagraph(writer) {
154
+ const model = this.editor.model;
155
+ const doc = model.document;
156
+ const selection = doc.selection;
157
+ const limitElement = model.schema.getLimitElement(selection);
158
+ const paragraph = writer.createElement('paragraph');
159
+ writer.remove(writer.createRangeIn(limitElement));
160
+ writer.insert(paragraph, limitElement);
161
+ writer.setSelection(paragraph, 0);
162
+ }
163
+ /**
164
+ * Checks if the selection is inside an empty element that is the first child of the limit element
165
+ * and should be replaced with a paragraph.
166
+ *
167
+ * @param selection The selection.
168
+ * @param sequence A number describing which subsequent delete event it is without the key being released.
169
+ */
170
+ _shouldReplaceFirstBlockWithParagraph(selection, sequence) {
171
+ const model = this.editor.model;
172
+ // Does nothing if user pressed and held the "Backspace" key or it was a "Delete" button.
173
+ if (sequence > 1 || this.direction != 'backward') {
174
+ return false;
175
+ }
176
+ if (!selection.isCollapsed) {
177
+ return false;
178
+ }
179
+ const position = selection.getFirstPosition();
180
+ const limitElement = model.schema.getLimitElement(position);
181
+ const limitElementFirstChild = limitElement.getChild(0);
182
+ // Only elements that are direct children of the limit element can be replaced.
183
+ // Unwrapping from a block quote should be handled in a dedicated feature.
184
+ if (position.parent != limitElementFirstChild) {
185
+ return false;
186
+ }
187
+ // A block should be replaced only if it was empty.
188
+ if (!selection.containsEntireContent(limitElementFirstChild)) {
189
+ return false;
190
+ }
191
+ // Replace with a paragraph only if it's allowed there.
192
+ if (!model.schema.checkChild(limitElement, 'paragraph')) {
193
+ return false;
194
+ }
195
+ // Does nothing if the limit element already contains only a paragraph.
196
+ if (limitElementFirstChild.name == 'paragraph') {
197
+ return false;
198
+ }
199
+ return true;
200
+ }
201
+ }