@ckeditor/ckeditor5-list 32.0.0 → 34.0.1
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/README.md +2 -1
- package/build/list.js +2 -2
- package/build/list.js.map +1 -0
- package/build/translations/cs.js +1 -1
- package/build/translations/de.js +1 -1
- package/build/translations/el.js +1 -1
- package/build/translations/en-au.js +1 -1
- package/build/translations/es.js +1 -1
- package/build/translations/gl.js +1 -1
- package/build/translations/it.js +1 -1
- package/build/translations/jv.js +1 -0
- package/build/translations/pt-br.js +1 -1
- package/build/translations/sk.js +1 -1
- package/build/translations/sr-latn.js +1 -1
- package/build/translations/sr.js +1 -1
- package/ckeditor5-metadata.json +1 -1
- package/lang/translations/cs.po +4 -4
- package/lang/translations/de.po +4 -4
- package/lang/translations/el.po +27 -27
- package/lang/translations/en-au.po +4 -4
- package/lang/translations/es.po +22 -22
- package/lang/translations/gl.po +4 -4
- package/lang/translations/it.po +4 -4
- package/lang/translations/jv.po +125 -0
- package/lang/translations/pt-br.po +4 -4
- package/lang/translations/sk.po +4 -4
- package/lang/translations/sr-latn.po +4 -4
- package/lang/translations/sr.po +4 -4
- package/package.json +36 -24
- package/src/documentlist/converters.js +470 -0
- package/src/documentlist/documentlistcommand.js +216 -0
- package/src/documentlist/documentlistediting.js +676 -0
- package/src/documentlist/documentlistindentcommand.js +182 -0
- package/src/documentlist/documentlistmergecommand.js +235 -0
- package/src/documentlist/documentlistsplitcommand.js +114 -0
- package/src/documentlist/utils/listwalker.js +260 -0
- package/src/documentlist/utils/model.js +534 -0
- package/src/documentlist/utils/postfixers.js +138 -0
- package/src/documentlist/utils/view.js +148 -0
- package/src/documentlist.js +36 -0
- package/src/documentlistproperties/converters.js +57 -0
- package/src/documentlistproperties/documentlistpropertiesediting.js +338 -0
- package/src/documentlistproperties/documentlistreversedcommand.js +76 -0
- package/src/documentlistproperties/documentliststartcommand.js +76 -0
- package/src/documentlistproperties/documentliststylecommand.js +140 -0
- package/src/documentlistproperties/utils/style.js +41 -0
- package/src/documentlistproperties.js +37 -0
- package/src/index.js +10 -6
- package/src/{converters.js → list/converters.js} +11 -10
- package/src/{indentcommand.js → list/indentcommand.js} +1 -1
- package/src/{listcommand.js → list/listcommand.js} +1 -1
- package/src/{listediting.js → list/listediting.js} +11 -12
- package/src/{listui.js → list/listui.js} +3 -3
- package/src/{utils.js → list/utils.js} +35 -4
- package/src/list.js +7 -6
- package/src/{listpropertiesediting.js → listproperties/listpropertiesediting.js} +6 -5
- package/src/{listpropertiesui.js → listproperties/listpropertiesui.js} +15 -16
- package/src/{listreversedcommand.js → listproperties/listreversedcommand.js} +3 -3
- package/src/{liststartcommand.js → listproperties/liststartcommand.js} +3 -3
- package/src/{liststylecommand.js → listproperties/liststylecommand.js} +36 -4
- package/src/{ui → listproperties/ui}/collapsibleview.js +1 -1
- package/src/{ui → listproperties/ui}/listpropertiesview.js +1 -1
- package/src/listproperties.js +14 -9
- package/src/{checktodolistcommand.js → todolist/checktodolistcommand.js} +2 -2
- package/src/{todolistconverters.js → todolist/todolistconverters.js} +8 -4
- package/src/{todolistediting.js → todolist/todolistediting.js} +4 -4
- package/src/{todolistui.js → todolist/todolistui.js} +3 -3
- package/src/todolist.js +4 -4
- package/theme/documentlist.css +8 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, 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
|
+
/**
|
|
7
|
+
* @module list/documentlist/utils/model
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { uid, toArray } from 'ckeditor5/src/utils';
|
|
11
|
+
import ListWalker, { iterateSiblingListBlocks } from './listwalker';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The list item ID generator.
|
|
15
|
+
*
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
export class ListItemUid {
|
|
19
|
+
/**
|
|
20
|
+
* Returns the next ID.
|
|
21
|
+
*
|
|
22
|
+
* @protected
|
|
23
|
+
* @returns {String}
|
|
24
|
+
*/
|
|
25
|
+
/* istanbul ignore next: static function definition */
|
|
26
|
+
static next() {
|
|
27
|
+
return uid();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns true if the given model node is a list item block.
|
|
33
|
+
*
|
|
34
|
+
* @protected
|
|
35
|
+
* @param {module:engine/model/node~Node} node A model node.
|
|
36
|
+
* @returns {Boolean}
|
|
37
|
+
*/
|
|
38
|
+
export function isListItemBlock( node ) {
|
|
39
|
+
return !!node && node.is( 'element' ) && node.hasAttribute( 'listItemId' );
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Returns an array with all elements that represents the same list item.
|
|
44
|
+
*
|
|
45
|
+
* It means that values for `listIndent`, and `listItemId` for all items are equal.
|
|
46
|
+
*
|
|
47
|
+
* @protected
|
|
48
|
+
* @param {module:engine/model/element~Element} listItem Starting list item element.
|
|
49
|
+
* @param {Object} [options]
|
|
50
|
+
* @param {Boolean} [options.higherIndent=false] Whether blocks with a higher indent level than the start block should be included
|
|
51
|
+
* in the result.
|
|
52
|
+
* @return {Array.<module:engine/model/element~Element>}
|
|
53
|
+
*/
|
|
54
|
+
export function getAllListItemBlocks( listItem, options = {} ) {
|
|
55
|
+
return [
|
|
56
|
+
...getListItemBlocks( listItem, { ...options, direction: 'backward' } ),
|
|
57
|
+
...getListItemBlocks( listItem, { ...options, direction: 'forward' } )
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns an array with elements that represents the same list item in the specified direction.
|
|
63
|
+
*
|
|
64
|
+
* It means that values for `listIndent` and `listItemId` for all items are equal.
|
|
65
|
+
*
|
|
66
|
+
* **Note**: For backward search the provided item is not included, but for forward search it is included in the result.
|
|
67
|
+
*
|
|
68
|
+
* @protected
|
|
69
|
+
* @param {module:engine/model/element~Element} listItem Starting list item element.
|
|
70
|
+
* @param {Object} [options]
|
|
71
|
+
* @param {'forward'|'backward'} [options.direction='backward'] Walking direction.
|
|
72
|
+
* @param {Boolean} [options.higherIndent=false] Whether blocks with a higher indent level than the start block should be included
|
|
73
|
+
* in the result.
|
|
74
|
+
* @returns {Array.<module:engine/model/element~Element>}
|
|
75
|
+
*/
|
|
76
|
+
export function getListItemBlocks( listItem, options = {} ) {
|
|
77
|
+
const isForward = options.direction == 'forward';
|
|
78
|
+
|
|
79
|
+
const items = Array.from( new ListWalker( listItem, {
|
|
80
|
+
...options,
|
|
81
|
+
includeSelf: isForward,
|
|
82
|
+
sameIndent: true,
|
|
83
|
+
sameAttributes: 'listItemId'
|
|
84
|
+
} ) );
|
|
85
|
+
|
|
86
|
+
return isForward ? items : items.reverse();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns a list items nested inside the given list item.
|
|
91
|
+
*
|
|
92
|
+
* @protected
|
|
93
|
+
* @param {module:engine/model/element~Element} listItem Starting list item element.
|
|
94
|
+
* @returns {Array.<module:engine/model/element~Element>}
|
|
95
|
+
*/
|
|
96
|
+
export function getNestedListBlocks( listItem ) {
|
|
97
|
+
return Array.from( new ListWalker( listItem, {
|
|
98
|
+
direction: 'forward',
|
|
99
|
+
higherIndent: true
|
|
100
|
+
} ) );
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns array of all blocks/items of the same list as given block (same indent, same type and properties).
|
|
105
|
+
*
|
|
106
|
+
* @protected
|
|
107
|
+
* @param {module:engine/model/element~Element} listItem Starting list item element.
|
|
108
|
+
* @returns {Array.<module:engine/model/element~Element>}
|
|
109
|
+
*/
|
|
110
|
+
export function getListItems( listItem ) {
|
|
111
|
+
const backwardBlocks = new ListWalker( listItem, {
|
|
112
|
+
sameIndent: true,
|
|
113
|
+
sameAttributes: 'listType'
|
|
114
|
+
} );
|
|
115
|
+
|
|
116
|
+
const forwardBlocks = new ListWalker( listItem, {
|
|
117
|
+
sameIndent: true,
|
|
118
|
+
sameAttributes: 'listType',
|
|
119
|
+
includeSelf: true,
|
|
120
|
+
direction: 'forward'
|
|
121
|
+
} );
|
|
122
|
+
|
|
123
|
+
return [
|
|
124
|
+
...Array.from( backwardBlocks ).reverse(),
|
|
125
|
+
...forwardBlocks
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check if the given block is the first in the list item.
|
|
131
|
+
*
|
|
132
|
+
* @protected
|
|
133
|
+
* @param {module:engine/model/element~Element} listBlock The list block element.
|
|
134
|
+
* @returns {Boolean}
|
|
135
|
+
*/
|
|
136
|
+
export function isFirstBlockOfListItem( listBlock ) {
|
|
137
|
+
const previousSibling = ListWalker.first( listBlock, {
|
|
138
|
+
sameIndent: true,
|
|
139
|
+
sameAttributes: 'listItemId'
|
|
140
|
+
} );
|
|
141
|
+
|
|
142
|
+
if ( !previousSibling ) {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Check if the given block is the last in the list item.
|
|
151
|
+
*
|
|
152
|
+
* @protected
|
|
153
|
+
* @param {module:engine/model/element~Element} listBlock The list block element.
|
|
154
|
+
* @returns {Boolean}
|
|
155
|
+
*/
|
|
156
|
+
export function isLastBlockOfListItem( listBlock ) {
|
|
157
|
+
const nextSibling = ListWalker.first( listBlock, {
|
|
158
|
+
direction: 'forward',
|
|
159
|
+
sameIndent: true,
|
|
160
|
+
sameAttributes: 'listItemId'
|
|
161
|
+
} );
|
|
162
|
+
|
|
163
|
+
if ( !nextSibling ) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Expands the given list of selected blocks to include the leading and tailing blocks of partially selected list items.
|
|
172
|
+
*
|
|
173
|
+
* @protected
|
|
174
|
+
* @param {module:engine/model/element~Element|Array.<module:engine/model/element~Element>} blocks The list of selected blocks.
|
|
175
|
+
* @param {Object} [options]
|
|
176
|
+
* @param {Boolean} [options.withNested=true] Whether should include nested list items.
|
|
177
|
+
* @returns {Array.<module:engine/model/element~Element>}
|
|
178
|
+
*/
|
|
179
|
+
export function expandListBlocksToCompleteItems( blocks, options = {} ) {
|
|
180
|
+
blocks = toArray( blocks );
|
|
181
|
+
|
|
182
|
+
const higherIndent = options.withNested !== false;
|
|
183
|
+
const allBlocks = new Set();
|
|
184
|
+
|
|
185
|
+
for ( const block of blocks ) {
|
|
186
|
+
for ( const itemBlock of getAllListItemBlocks( block, { higherIndent } ) ) {
|
|
187
|
+
allBlocks.add( itemBlock );
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return sortBlocks( allBlocks );
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Expands the given list of selected blocks to include all the items of the lists they're in.
|
|
196
|
+
*
|
|
197
|
+
* @protected
|
|
198
|
+
* @param {module:engine/model/element~Element|Array.<module:engine/model/element~Element>} blocks The list of selected blocks.
|
|
199
|
+
* @returns {Array.<module:engine/model/element~Element>}
|
|
200
|
+
*/
|
|
201
|
+
export function expandListBlocksToCompleteList( blocks ) {
|
|
202
|
+
blocks = toArray( blocks );
|
|
203
|
+
|
|
204
|
+
const allBlocks = new Set();
|
|
205
|
+
|
|
206
|
+
for ( const block of blocks ) {
|
|
207
|
+
for ( const itemBlock of getListItems( block ) ) {
|
|
208
|
+
allBlocks.add( itemBlock );
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return sortBlocks( allBlocks );
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Splits the list item just before the provided list block.
|
|
217
|
+
*
|
|
218
|
+
* @protected
|
|
219
|
+
* @param {module:engine/model/element~Element} listBlock The list block element.
|
|
220
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
221
|
+
* @returns {Array.<module:engine/model/element~Element>} The array of updated blocks.
|
|
222
|
+
*/
|
|
223
|
+
export function splitListItemBefore( listBlock, writer ) {
|
|
224
|
+
const blocks = getListItemBlocks( listBlock, { direction: 'forward' } );
|
|
225
|
+
const id = ListItemUid.next();
|
|
226
|
+
|
|
227
|
+
for ( const block of blocks ) {
|
|
228
|
+
writer.setAttribute( 'listItemId', id, block );
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return blocks;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Merges the list item with the parent list item.
|
|
236
|
+
*
|
|
237
|
+
* @protected
|
|
238
|
+
* @param {module:engine/model/element~Element} listBlock The list block element.
|
|
239
|
+
* @param {module:engine/model/element~Element} parentBlock The list block element to merge with.
|
|
240
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
241
|
+
* @returns {Array.<module:engine/model/element~Element>} The array of updated blocks.
|
|
242
|
+
*/
|
|
243
|
+
export function mergeListItemBefore( listBlock, parentBlock, writer ) {
|
|
244
|
+
const attributes = {};
|
|
245
|
+
|
|
246
|
+
for ( const [ key, value ] of parentBlock.getAttributes() ) {
|
|
247
|
+
if ( key.startsWith( 'list' ) ) {
|
|
248
|
+
attributes[ key ] = value;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const blocks = getListItemBlocks( listBlock, { direction: 'forward' } );
|
|
253
|
+
|
|
254
|
+
for ( const block of blocks ) {
|
|
255
|
+
writer.setAttributes( attributes, block );
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return blocks;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Increases indentation of given list blocks.
|
|
263
|
+
*
|
|
264
|
+
* @protected
|
|
265
|
+
* @param {module:engine/model/element~Element|Iterable.<module:engine/model/element~Element>} blocks The block or iterable of blocks.
|
|
266
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
267
|
+
* @param {Object} [options]
|
|
268
|
+
* @param {Boolean} [options.expand=false] Whether should expand the list of blocks to include complete list items.
|
|
269
|
+
* @param {Number} [options.indentBy=1] The number of levels the indentation should change (could be negative).
|
|
270
|
+
*/
|
|
271
|
+
export function indentBlocks( blocks, writer, { expand, indentBy = 1 } = {} ) {
|
|
272
|
+
blocks = toArray( blocks );
|
|
273
|
+
|
|
274
|
+
// Expand the selected blocks to contain the whole list items.
|
|
275
|
+
const allBlocks = expand ? expandListBlocksToCompleteItems( blocks ) : blocks;
|
|
276
|
+
|
|
277
|
+
for ( const block of allBlocks ) {
|
|
278
|
+
const blockIndent = block.getAttribute( 'listIndent' ) + indentBy;
|
|
279
|
+
|
|
280
|
+
if ( blockIndent < 0 ) {
|
|
281
|
+
removeListAttributes( block, writer );
|
|
282
|
+
} else {
|
|
283
|
+
writer.setAttribute( 'listIndent', blockIndent, block );
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return allBlocks;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Decreases indentation of given list of blocks. If the indentation of some blocks matches the indentation
|
|
292
|
+
* of surrounding blocks, they get merged together.
|
|
293
|
+
*
|
|
294
|
+
* @protected
|
|
295
|
+
* @param {module:engine/model/element~Element|Iterable.<module:engine/model/element~Element>} blocks The block or iterable of blocks.
|
|
296
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
297
|
+
*/
|
|
298
|
+
export function outdentBlocksWithMerge( blocks, writer ) {
|
|
299
|
+
blocks = toArray( blocks );
|
|
300
|
+
|
|
301
|
+
// Expand the selected blocks to contain the whole list items.
|
|
302
|
+
const allBlocks = expandListBlocksToCompleteItems( blocks );
|
|
303
|
+
const visited = new Set();
|
|
304
|
+
|
|
305
|
+
const referenceIndent = Math.min( ...allBlocks.map( block => block.getAttribute( 'listIndent' ) ) );
|
|
306
|
+
const parentBlocks = new Map();
|
|
307
|
+
|
|
308
|
+
// Collect parent blocks before the list structure gets altered.
|
|
309
|
+
for ( const block of allBlocks ) {
|
|
310
|
+
parentBlocks.set( block, ListWalker.first( block, { lowerIndent: true } ) );
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
for ( const block of allBlocks ) {
|
|
314
|
+
if ( visited.has( block ) ) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
visited.add( block );
|
|
319
|
+
|
|
320
|
+
const blockIndent = block.getAttribute( 'listIndent' ) - 1;
|
|
321
|
+
|
|
322
|
+
if ( blockIndent < 0 ) {
|
|
323
|
+
removeListAttributes( block, writer );
|
|
324
|
+
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Merge with parent list item while outdenting and indent matches reference indent.
|
|
329
|
+
if ( block.getAttribute( 'listIndent' ) == referenceIndent ) {
|
|
330
|
+
const mergedBlocks = mergeListItemIfNotLast( block, parentBlocks.get( block ), writer );
|
|
331
|
+
|
|
332
|
+
// All list item blocks are updated while merging so add those to visited set.
|
|
333
|
+
for ( const mergedBlock of mergedBlocks ) {
|
|
334
|
+
visited.add( mergedBlock );
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// The indent level was updated while merging so continue to next block.
|
|
338
|
+
if ( mergedBlocks.length ) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
writer.setAttribute( 'listIndent', blockIndent, block );
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return sortBlocks( visited );
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Removes all list attributes from the given blocks.
|
|
351
|
+
*
|
|
352
|
+
* @protected
|
|
353
|
+
* @param {module:engine/model/element~Element|Iterable.<module:engine/model/element~Element>} blocks The block or iterable of blocks.
|
|
354
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
355
|
+
* @returns {Array.<module:engine/model/element~Element>} Array of altered blocks.
|
|
356
|
+
*/
|
|
357
|
+
export function removeListAttributes( blocks, writer ) {
|
|
358
|
+
blocks = toArray( blocks );
|
|
359
|
+
|
|
360
|
+
for ( const block of blocks ) {
|
|
361
|
+
for ( const attributeKey of block.getAttributeKeys() ) {
|
|
362
|
+
if ( attributeKey.startsWith( 'list' ) ) {
|
|
363
|
+
writer.removeAttribute( attributeKey, block );
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return blocks;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Checks whether the given blocks are related to a single list item.
|
|
373
|
+
*
|
|
374
|
+
* @protected
|
|
375
|
+
* @param {Array.<module:engine/model/element~Element>} blocks The list block elements.
|
|
376
|
+
* @returns {Boolean}
|
|
377
|
+
*/
|
|
378
|
+
export function isSingleListItem( blocks ) {
|
|
379
|
+
if ( !blocks.length ) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const firstItemId = blocks[ 0 ].getAttribute( 'listItemId' );
|
|
384
|
+
|
|
385
|
+
if ( !firstItemId ) {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return !blocks.some( item => item.getAttribute( 'listItemId' ) != firstItemId );
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Modifies the indents of list blocks following the given list block so the indentation is valid after
|
|
394
|
+
* the given block is no longer a list item.
|
|
395
|
+
*
|
|
396
|
+
* @protected
|
|
397
|
+
* @param {module:engine/model/element~Element} lastBlock The last list block that has become a non-list element.
|
|
398
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
399
|
+
* @returns {Array.<module:engine/model/element~Element>} Array of altered blocks.
|
|
400
|
+
*/
|
|
401
|
+
export function outdentFollowingItems( lastBlock, writer ) {
|
|
402
|
+
const changedBlocks = [];
|
|
403
|
+
|
|
404
|
+
// Start from the model item that is just after the last turned-off item.
|
|
405
|
+
let currentIndent = Number.POSITIVE_INFINITY;
|
|
406
|
+
|
|
407
|
+
// Correct indent of all items after the last turned off item.
|
|
408
|
+
// Rules that should be followed:
|
|
409
|
+
// 1. All direct sub-items of turned-off item should become indent 0, because the first item after it
|
|
410
|
+
// will be the first item of a new list. Other items are at the same level, so should have same 0 index.
|
|
411
|
+
// 2. All items with indent lower than indent of turned-off item should become indent 0, because they
|
|
412
|
+
// should not end up as a child of any of list items that they were not children of before.
|
|
413
|
+
// 3. All other items should have their indent changed relatively to it's parent.
|
|
414
|
+
//
|
|
415
|
+
// For example:
|
|
416
|
+
// 1 * --------
|
|
417
|
+
// 2 * --------
|
|
418
|
+
// 3 * -------- <-- this is turned off.
|
|
419
|
+
// 4 * -------- <-- this has to become indent = 0, because it will be first item on a new list.
|
|
420
|
+
// 5 * -------- <-- this should be still be a child of item above, so indent = 1.
|
|
421
|
+
// 6 * -------- <-- this has to become indent = 0, because it should not be a child of any of items above.
|
|
422
|
+
// 7 * -------- <-- this should be still be a child of item above, so indent = 1.
|
|
423
|
+
// 8 * -------- <-- this has to become indent = 0.
|
|
424
|
+
// 9 * -------- <-- this should still be a child of item above, so indent = 1.
|
|
425
|
+
// 10 * -------- <-- this should still be a child of item above, so indent = 2.
|
|
426
|
+
// 11 * -------- <-- this should still be at the same level as item above, so indent = 2.
|
|
427
|
+
// 12 * -------- <-- this and all below are left unchanged.
|
|
428
|
+
// 13 * --------
|
|
429
|
+
// 14 * --------
|
|
430
|
+
//
|
|
431
|
+
// After turning off 3 the list becomes:
|
|
432
|
+
//
|
|
433
|
+
// 1 * --------
|
|
434
|
+
// 2 * --------
|
|
435
|
+
//
|
|
436
|
+
// 3 --------
|
|
437
|
+
//
|
|
438
|
+
// 4 * --------
|
|
439
|
+
// 5 * --------
|
|
440
|
+
// 6 * --------
|
|
441
|
+
// 7 * --------
|
|
442
|
+
// 8 * --------
|
|
443
|
+
// 9 * --------
|
|
444
|
+
// 10 * --------
|
|
445
|
+
// 11 * --------
|
|
446
|
+
// 12 * --------
|
|
447
|
+
// 13 * --------
|
|
448
|
+
// 14 * --------
|
|
449
|
+
//
|
|
450
|
+
// Thanks to this algorithm no lists are mismatched and no items get unexpected children/parent, while
|
|
451
|
+
// those parent-child connection which are possible to maintain are still maintained. It's worth noting
|
|
452
|
+
// that this is the same effect that we would be get by multiple use of outdent command. However doing
|
|
453
|
+
// it like this is much more efficient because it's less operation (less memory usage, easier OT) and
|
|
454
|
+
// less conversion (faster).
|
|
455
|
+
for ( const { node } of iterateSiblingListBlocks( lastBlock.nextSibling, 'forward' ) ) {
|
|
456
|
+
// Check each next list item, as long as its indent is higher than 0.
|
|
457
|
+
const indent = node.getAttribute( 'listIndent' );
|
|
458
|
+
|
|
459
|
+
// If the indent is 0 we are not going to change anything anyway.
|
|
460
|
+
if ( indent == 0 ) {
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// We check if that's item indent is lower than current relative indent.
|
|
465
|
+
if ( indent < currentIndent ) {
|
|
466
|
+
// If it is, current relative indent becomes that indent.
|
|
467
|
+
currentIndent = indent;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Fix indent relatively to current relative indent.
|
|
471
|
+
// Note, that if we just changed the current relative indent, the newIndent will be equal to 0.
|
|
472
|
+
const newIndent = indent - currentIndent;
|
|
473
|
+
|
|
474
|
+
writer.setAttribute( 'listIndent', newIndent, node );
|
|
475
|
+
changedBlocks.push( node );
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return changedBlocks;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Returns the array of given blocks sorted by model indexes (document order).
|
|
483
|
+
*
|
|
484
|
+
* @protected
|
|
485
|
+
* @param {Iterable.<module:engine/model/element~Element>} blocks The array of blocks.
|
|
486
|
+
* @returns {Array.<module:engine/model/element~Element>} The sorted array of blocks.
|
|
487
|
+
*/
|
|
488
|
+
export function sortBlocks( blocks ) {
|
|
489
|
+
return Array.from( blocks )
|
|
490
|
+
.filter( block => block.root.rootName !== '$graveyard' )
|
|
491
|
+
.sort( ( a, b ) => a.index - b.index );
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Returns a selected block object. If a selected object is inline or when there is no selected
|
|
496
|
+
* object, `null` is returned.
|
|
497
|
+
*
|
|
498
|
+
* @protected
|
|
499
|
+
* @param {module:engine/model/model~Model} model The instance of editor model.
|
|
500
|
+
* @returns {module:engine/model/element~Element|null} Selected block object or `null`.
|
|
501
|
+
*/
|
|
502
|
+
export function getSelectedBlockObject( model ) {
|
|
503
|
+
const selectedElement = model.document.selection.getSelectedElement();
|
|
504
|
+
|
|
505
|
+
if ( !selectedElement ) {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if ( model.schema.isObject( selectedElement ) && model.schema.isBlock( selectedElement ) ) {
|
|
510
|
+
return selectedElement;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Merges a given block to the given parent block if parent is a list item and there is no more blocks in the same item.
|
|
517
|
+
function mergeListItemIfNotLast( block, parentBlock, writer ) {
|
|
518
|
+
const parentItemBlocks = getListItemBlocks( parentBlock, { direction: 'forward' } );
|
|
519
|
+
|
|
520
|
+
// Merge with parent only if outdented item wasn't the last one in its parent.
|
|
521
|
+
// Merge:
|
|
522
|
+
// * a -> * a
|
|
523
|
+
// * [b] -> b
|
|
524
|
+
// c -> c
|
|
525
|
+
// Don't merge:
|
|
526
|
+
// * a -> * a
|
|
527
|
+
// * [b] -> * b
|
|
528
|
+
// * c -> * c
|
|
529
|
+
if ( parentItemBlocks.pop().index > block.index ) {
|
|
530
|
+
return mergeListItemBefore( block, parentBlock, writer );
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
return [];
|
|
534
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, 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
|
+
/**
|
|
7
|
+
* @module list/documentlist/utils/postfixers
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { iterateSiblingListBlocks } from './listwalker';
|
|
11
|
+
import { getListItemBlocks, isListItemBlock, ListItemUid } from './model';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Based on the provided positions looks for the list head and stores it in the provided map.
|
|
15
|
+
*
|
|
16
|
+
* @protected
|
|
17
|
+
* @param {module:engine/model/position~Position} position The search starting position.
|
|
18
|
+
* @param {Map.<module:engine/model/element~Element,module:engine/model/element~Element>} itemToListHead The map from list item element
|
|
19
|
+
* to the list head element.
|
|
20
|
+
*/
|
|
21
|
+
export function findAndAddListHeadToMap( position, itemToListHead ) {
|
|
22
|
+
const previousNode = position.nodeBefore;
|
|
23
|
+
|
|
24
|
+
if ( !isListItemBlock( previousNode ) ) {
|
|
25
|
+
const item = position.nodeAfter;
|
|
26
|
+
|
|
27
|
+
if ( isListItemBlock( item ) ) {
|
|
28
|
+
itemToListHead.set( item, item );
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
let listHead = previousNode;
|
|
32
|
+
|
|
33
|
+
for ( { node: listHead } of iterateSiblingListBlocks( listHead, 'backward' ) ) {
|
|
34
|
+
if ( itemToListHead.has( listHead ) ) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
itemToListHead.set( previousNode, listHead );
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Scans the list starting from the given list head element and fixes items' indentation.
|
|
45
|
+
*
|
|
46
|
+
* @protected
|
|
47
|
+
* @param {Iterable.<module:list/documentlist/utils/listwalker~ListIteratorValue>} listNodes The iterable of list nodes.
|
|
48
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
49
|
+
* @returns {Boolean} Whether the model was modified.
|
|
50
|
+
*/
|
|
51
|
+
export function fixListIndents( listNodes, writer ) {
|
|
52
|
+
let maxIndent = 0; // Guards local sublist max indents that need fixing.
|
|
53
|
+
let prevIndent = -1; // Previous item indent.
|
|
54
|
+
let fixBy = null;
|
|
55
|
+
let applied = false;
|
|
56
|
+
|
|
57
|
+
for ( const { node } of listNodes ) {
|
|
58
|
+
const itemIndent = node.getAttribute( 'listIndent' );
|
|
59
|
+
|
|
60
|
+
if ( itemIndent > maxIndent ) {
|
|
61
|
+
let newIndent;
|
|
62
|
+
|
|
63
|
+
if ( fixBy === null ) {
|
|
64
|
+
fixBy = itemIndent - maxIndent;
|
|
65
|
+
newIndent = maxIndent;
|
|
66
|
+
} else {
|
|
67
|
+
if ( fixBy > itemIndent ) {
|
|
68
|
+
fixBy = itemIndent;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
newIndent = itemIndent - fixBy;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if ( newIndent > prevIndent + 1 ) {
|
|
75
|
+
newIndent = prevIndent + 1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
writer.setAttribute( 'listIndent', newIndent, node );
|
|
79
|
+
|
|
80
|
+
applied = true;
|
|
81
|
+
prevIndent = newIndent;
|
|
82
|
+
} else {
|
|
83
|
+
fixBy = null;
|
|
84
|
+
maxIndent = itemIndent + 1;
|
|
85
|
+
prevIndent = itemIndent;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return applied;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Scans the list starting from the given list head element and fixes items' types.
|
|
94
|
+
*
|
|
95
|
+
* @protected
|
|
96
|
+
* @param {Iterable.<module:list/documentlist/utils/listwalker~ListIteratorValue>} listNodes The iterable of list nodes.
|
|
97
|
+
* @param {Set.<String>} seenIds The set of already known IDs.
|
|
98
|
+
* @param {module:engine/model/writer~Writer} writer The model writer.
|
|
99
|
+
* @returns {Boolean} Whether the model was modified.
|
|
100
|
+
*/
|
|
101
|
+
export function fixListItemIds( listNodes, seenIds, writer ) {
|
|
102
|
+
const visited = new Set();
|
|
103
|
+
let applied = false;
|
|
104
|
+
|
|
105
|
+
for ( const { node } of listNodes ) {
|
|
106
|
+
if ( visited.has( node ) ) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let listType = node.getAttribute( 'listType' );
|
|
111
|
+
let listItemId = node.getAttribute( 'listItemId' );
|
|
112
|
+
|
|
113
|
+
// Use a new ID if this one was spot earlier (even in other list).
|
|
114
|
+
if ( seenIds.has( listItemId ) ) {
|
|
115
|
+
listItemId = ListItemUid.next();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
seenIds.add( listItemId );
|
|
119
|
+
|
|
120
|
+
for ( const block of getListItemBlocks( node, { direction: 'forward' } ) ) {
|
|
121
|
+
visited.add( block );
|
|
122
|
+
|
|
123
|
+
// Use a new ID if a block of a bigger list item has different type.
|
|
124
|
+
if ( block.getAttribute( 'listType' ) != listType ) {
|
|
125
|
+
listItemId = ListItemUid.next();
|
|
126
|
+
listType = block.getAttribute( 'listType' );
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if ( block.getAttribute( 'listItemId' ) != listItemId ) {
|
|
130
|
+
writer.setAttribute( 'listItemId', listItemId, block );
|
|
131
|
+
|
|
132
|
+
applied = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return applied;
|
|
138
|
+
}
|