-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
listmergecommand.ts
242 lines (205 loc) · 7.86 KB
/
listmergecommand.ts
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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module list/list/listmergecommand
*/
import { Command, type Editor } from 'ckeditor5/src/core.js';
import type { DocumentSelection, Element, Node, Selection } from 'ckeditor5/src/engine.js';
import {
getNestedListBlocks,
indentBlocks,
sortBlocks,
isFirstBlockOfListItem,
mergeListItemBefore,
isSingleListItem,
getSelectedBlockObject,
isListItemBlock,
type ListElement
} from './utils/model.js';
import ListWalker from './utils/listwalker.js';
/**
* The document list merge command. It is used by the {@link module:list/list~List list feature}.
*/
export default class ListMergeCommand extends Command {
/**
* Whether list item should be merged before or after the selected block.
*/
private readonly _direction: 'forward' | 'backward';
/**
* Creates an instance of the command.
*
* @param editor The editor instance.
* @param direction Whether list item should be merged before or after the selected block.
*/
constructor( editor: Editor, direction: 'forward' | 'backward' ) {
super( editor );
this._direction = direction;
}
/**
* @inheritDoc
*/
public override refresh(): void {
this.isEnabled = this._checkEnabled();
}
/**
* Merges list blocks together (depending on the {@link #constructor}'s `direction` parameter).
*
* @fires execute
* @fires afterExecute
* @param options Command options.
* @param options.shouldMergeOnBlocksContentLevel When set `true`, merging will be performed together
* with {@link module:engine/model/model~Model#deleteContent} to get rid of the inline content in the selection or take advantage
* of the heuristics in `deleteContent()` that helps convert lists into paragraphs in certain cases.
*/
public override execute(
{ shouldMergeOnBlocksContentLevel = false }: { shouldMergeOnBlocksContentLevel?: boolean } = {}
): void {
const model = this.editor.model;
const selection = model.document.selection;
const changedBlocks: Array<Element> = [];
model.change( writer => {
const { firstElement, lastElement } = this._getMergeSubjectElements( selection, shouldMergeOnBlocksContentLevel );
const firstIndent = firstElement.getAttribute( 'listIndent' ) || 0;
const lastIndent = lastElement.getAttribute( 'listIndent' );
const lastElementId = lastElement.getAttribute( 'listItemId' );
if ( firstIndent != lastIndent ) {
const nestedLastElementBlocks = getNestedListBlocks( lastElement );
changedBlocks.push( ...indentBlocks( [ lastElement, ...nestedLastElementBlocks ], writer, {
indentBy: firstIndent - lastIndent,
// If outdenting, the entire sub-tree that follows must be included.
expand: firstIndent < lastIndent
} ) );
}
if ( shouldMergeOnBlocksContentLevel ) {
let sel: Selection | DocumentSelection = selection;
if ( selection.isCollapsed ) {
sel = writer.createSelection( writer.createRange(
writer.createPositionAt( firstElement, 'end' ),
writer.createPositionAt( lastElement, 0 )
) );
}
// Delete selected content. Replace entire content only for non-collapsed selection.
model.deleteContent( sel, { doNotResetEntireContent: selection.isCollapsed } );
// Get the last "touched" element after deleteContent call (can't use the lastElement because
// it could get merged into the firstElement while deleting content).
const lastElementAfterDelete = sel.getLastPosition()!.parent;
// Check if the element after it was in the same list item and adjust it if needed.
const nextSibling = lastElementAfterDelete.nextSibling;
changedBlocks.push( lastElementAfterDelete as Element );
if ( nextSibling && nextSibling !== lastElement && nextSibling.getAttribute( 'listItemId' ) == lastElementId ) {
changedBlocks.push( ...mergeListItemBefore( nextSibling, lastElementAfterDelete, writer ) );
}
} else {
changedBlocks.push( ...mergeListItemBefore( lastElement, firstElement, writer ) );
}
this._fireAfterExecute( changedBlocks );
} );
}
/**
* Fires the `afterExecute` event.
*
* @param changedBlocks The changed list elements.
*/
private _fireAfterExecute( changedBlocks: Array<Element> ) {
this.fire<ListMergeCommandAfterExecuteEvent>( 'afterExecute', sortBlocks( new Set( changedBlocks ) ) );
}
/**
* Checks whether the command can be enabled in the current context.
*
* @returns Whether the command should be enabled.
*/
private _checkEnabled(): boolean {
const model = this.editor.model;
const selection = model.document.selection;
const selectedBlockObject = getSelectedBlockObject( model );
if ( selection.isCollapsed || selectedBlockObject ) {
const positionParent = selectedBlockObject || selection.getFirstPosition()!.parent;
if ( !isListItemBlock( positionParent ) ) {
return false;
}
const siblingNode = this._direction == 'backward' ?
positionParent.previousSibling :
positionParent.nextSibling;
if ( !siblingNode ) {
return false;
}
if ( isSingleListItem( [ positionParent, siblingNode ] ) ) {
return false;
}
} else {
const lastPosition = selection.getLastPosition()!;
const firstPosition = selection.getFirstPosition()!;
// If deleting within a single block of a list item, there's no need to merge anything.
// The default delete should be executed instead.
if ( lastPosition.parent === firstPosition.parent ) {
return false;
}
if ( !isListItemBlock( lastPosition.parent ) ) {
return false;
}
}
return true;
}
/**
* Returns the boundary elements the merge should be executed for. These are not necessarily selection's first
* and last position parents but sometimes sibling or even further blocks depending on the context.
*
* @param selection The selection the merge is executed for.
* @param shouldMergeOnBlocksContentLevel When `true`, merge is performed together with
* {@link module:engine/model/model~Model#deleteContent} to remove the inline content within the selection.
*/
private _getMergeSubjectElements(
selection: Selection | DocumentSelection,
shouldMergeOnBlocksContentLevel: boolean
) {
const model = this.editor.model;
const selectedBlockObject = getSelectedBlockObject( model );
let firstElement, lastElement;
if ( selection.isCollapsed || selectedBlockObject ) {
const positionParent = selectedBlockObject || selection.getFirstPosition()!.parent as Node;
const isFirstBlock = isFirstBlockOfListItem( positionParent );
if ( this._direction == 'backward' ) {
lastElement = positionParent;
if ( isFirstBlock && !shouldMergeOnBlocksContentLevel ) {
// For the "c" as an anchorElement:
// * a
// * b
// * [c] <-- this block should be merged with "a"
// It should find "a" element to merge with:
// * a
// * b
// c
firstElement = ListWalker.first( positionParent, { sameIndent: true, lowerIndent: true } );
} else {
firstElement = positionParent.previousSibling;
}
} else {
// In case of the forward merge there is no case as above, just merge with next sibling.
firstElement = positionParent;
lastElement = positionParent.nextSibling;
}
} else {
firstElement = selection.getFirstPosition()!.parent;
lastElement = selection.getLastPosition()!.parent;
}
return {
firstElement: firstElement as ListElement,
lastElement: lastElement as ListElement
};
}
}
/**
* Event fired by the {@link ~ListMergeCommand#execute} method.
*
* It allows to execute an action after executing the {@link module:list/list/listcommand~ListCommand#execute}
* method, for example adjusting attributes of changed list items.
*
* @internal
* @eventName ~ListMergeCommand#afterExecute
*/
export type ListMergeCommandAfterExecuteEvent = {
name: 'afterExecute';
args: [ changedBlocks: Array<Element> ];
};