@ckeditor/ckeditor5-list 33.0.0 → 34.0.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/README.md +2 -1
- package/build/list.js +1 -1
- package/build/list.js.map +1 -1
- package/build/translations/de.js +1 -1
- package/build/translations/en-au.js +1 -1
- package/build/translations/gl.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/lang/translations/de.po +4 -4
- package/lang/translations/en-au.po +4 -4
- package/lang/translations/gl.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/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/list/converters.js +4 -0
- package/src/list/listediting.js +12 -13
- package/src/list.js +3 -2
- package/src/listproperties/listpropertiesediting.js +1 -1
- package/src/listproperties/listreversedcommand.js +1 -1
- package/src/listproperties/liststartcommand.js +1 -1
- package/src/listproperties/liststylecommand.js +1 -1
- package/src/listproperties.js +11 -6
- package/theme/documentlist.css +8 -0
- package/src/listproperties/ui/inputnumberview.js +0 -0
|
@@ -0,0 +1,470 @@
|
|
|
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/converters
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
getAllListItemBlocks,
|
|
12
|
+
getListItemBlocks,
|
|
13
|
+
isListItemBlock,
|
|
14
|
+
ListItemUid
|
|
15
|
+
} from './utils/model';
|
|
16
|
+
import {
|
|
17
|
+
createListElement,
|
|
18
|
+
createListItemElement,
|
|
19
|
+
getIndent,
|
|
20
|
+
isListView,
|
|
21
|
+
isListItemView
|
|
22
|
+
} from './utils/view';
|
|
23
|
+
import ListWalker, { iterateSiblingListBlocks } from './utils/listwalker';
|
|
24
|
+
import { findAndAddListHeadToMap } from './utils/postfixers';
|
|
25
|
+
|
|
26
|
+
import { UpcastWriter } from 'ckeditor5/src/engine';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns the upcast converter for list items. It's supposed to work after the block converters (content inside list items) is converted.
|
|
30
|
+
*
|
|
31
|
+
* @protected
|
|
32
|
+
* @returns {Function}
|
|
33
|
+
*/
|
|
34
|
+
export function listItemUpcastConverter() {
|
|
35
|
+
return ( evt, data, conversionApi ) => {
|
|
36
|
+
const { writer, schema } = conversionApi;
|
|
37
|
+
|
|
38
|
+
if ( !data.modelRange ) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const items = Array.from( data.modelRange.getItems( { shallow: true } ) )
|
|
43
|
+
.filter( item => schema.checkAttribute( item, 'listItemId' ) );
|
|
44
|
+
|
|
45
|
+
if ( !items.length ) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const attributes = {
|
|
50
|
+
listItemId: ListItemUid.next(),
|
|
51
|
+
listIndent: getIndent( data.viewItem ),
|
|
52
|
+
listType: data.viewItem.parent && data.viewItem.parent.name == 'ol' ? 'numbered' : 'bulleted'
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
for ( const item of items ) {
|
|
56
|
+
// Set list attributes only on same level items, those nested deeper are already handled by the recursive conversion.
|
|
57
|
+
if ( !isListItemBlock( item ) ) {
|
|
58
|
+
writer.setAttributes( attributes, item );
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if ( items.length > 1 ) {
|
|
63
|
+
// Make sure that list item that contain only nested list will preserve paragraph for itself:
|
|
64
|
+
// <ul>
|
|
65
|
+
// <li>
|
|
66
|
+
// <p></p> <-- this one must be kept
|
|
67
|
+
// <ul>
|
|
68
|
+
// <li></li>
|
|
69
|
+
// </ul>
|
|
70
|
+
// </li>
|
|
71
|
+
// </ul>
|
|
72
|
+
if ( items[ 1 ].getAttribute( 'listItemId' ) != attributes.listItemId ) {
|
|
73
|
+
conversionApi.keepEmptyElement( items[ 0 ] );
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Returns the upcast converter for the `<ul>` and `<ol>` view elements that cleans the input view of garbage.
|
|
81
|
+
* This is mostly to clean whitespaces from between the `<li>` view elements inside the view list element, however, also
|
|
82
|
+
* incorrect data can be cleared if the view was incorrect.
|
|
83
|
+
*
|
|
84
|
+
* @protected
|
|
85
|
+
* @returns {Function}
|
|
86
|
+
*/
|
|
87
|
+
export function listUpcastCleanList() {
|
|
88
|
+
return ( evt, data, conversionApi ) => {
|
|
89
|
+
if ( !conversionApi.consumable.test( data.viewItem, { name: true } ) ) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const viewWriter = new UpcastWriter( data.viewItem.document );
|
|
94
|
+
|
|
95
|
+
for ( const child of Array.from( data.viewItem.getChildren() ) ) {
|
|
96
|
+
if ( !isListItemView( child ) && !isListView( child ) ) {
|
|
97
|
+
viewWriter.remove( child );
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns a model document change:data event listener that triggers conversion of related items if needed.
|
|
105
|
+
*
|
|
106
|
+
* @protected
|
|
107
|
+
* @param {module:engine/model/model~Model} model The editor model.
|
|
108
|
+
* @param {module:engine/controller/editingcontroller~EditingController} editing The editing controller.
|
|
109
|
+
* @param {Array.<String>} attributeNames The list of all model list attributes (including registered strategies).
|
|
110
|
+
* @param {module:list/documentlist/documentlistediting~DocumentListEditing} documentListEditing The document list editing plugin.
|
|
111
|
+
* @return {Function}
|
|
112
|
+
*/
|
|
113
|
+
export function reconvertItemsOnDataChange( model, editing, attributeNames, documentListEditing ) {
|
|
114
|
+
return () => {
|
|
115
|
+
const changes = model.document.differ.getChanges();
|
|
116
|
+
const itemsToRefresh = [];
|
|
117
|
+
const itemToListHead = new Map();
|
|
118
|
+
const changedItems = new Set();
|
|
119
|
+
|
|
120
|
+
for ( const entry of changes ) {
|
|
121
|
+
if ( entry.type == 'insert' && entry.name != '$text' ) {
|
|
122
|
+
findAndAddListHeadToMap( entry.position, itemToListHead );
|
|
123
|
+
|
|
124
|
+
// Insert of a non-list item.
|
|
125
|
+
if ( !entry.attributes.has( 'listItemId' ) ) {
|
|
126
|
+
findAndAddListHeadToMap( entry.position.getShiftedBy( entry.length ), itemToListHead );
|
|
127
|
+
} else {
|
|
128
|
+
changedItems.add( entry.position.nodeAfter );
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Removed list item.
|
|
132
|
+
else if ( entry.type == 'remove' && entry.attributes.has( 'listItemId' ) ) {
|
|
133
|
+
findAndAddListHeadToMap( entry.position, itemToListHead );
|
|
134
|
+
}
|
|
135
|
+
// Changed list attribute.
|
|
136
|
+
else if ( entry.type == 'attribute' ) {
|
|
137
|
+
const item = entry.range.start.nodeAfter;
|
|
138
|
+
|
|
139
|
+
if ( attributeNames.includes( entry.attributeKey ) ) {
|
|
140
|
+
findAndAddListHeadToMap( entry.range.start, itemToListHead );
|
|
141
|
+
|
|
142
|
+
if ( entry.attributeNewValue === null ) {
|
|
143
|
+
findAndAddListHeadToMap( entry.range.start.getShiftedBy( 1 ), itemToListHead );
|
|
144
|
+
|
|
145
|
+
// Check if paragraph should be converted from bogus to plain paragraph.
|
|
146
|
+
if ( doesItemParagraphRequiresRefresh( item ) ) {
|
|
147
|
+
itemsToRefresh.push( item );
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
changedItems.add( item );
|
|
151
|
+
}
|
|
152
|
+
} else if ( isListItemBlock( item ) ) {
|
|
153
|
+
// Some other attribute was changed on the list item,
|
|
154
|
+
// check if paragraph does not need to be converted to bogus or back.
|
|
155
|
+
if ( doesItemParagraphRequiresRefresh( item ) ) {
|
|
156
|
+
itemsToRefresh.push( item );
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for ( const listHead of itemToListHead.values() ) {
|
|
163
|
+
itemsToRefresh.push( ...collectListItemsToRefresh( listHead, changedItems ) );
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
for ( const item of new Set( itemsToRefresh ) ) {
|
|
167
|
+
editing.reconvertItem( item );
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
function collectListItemsToRefresh( listHead, changedItems ) {
|
|
172
|
+
const itemsToRefresh = [];
|
|
173
|
+
const visited = new Set();
|
|
174
|
+
const stack = [];
|
|
175
|
+
|
|
176
|
+
for ( const { node, previous } of iterateSiblingListBlocks( listHead, 'forward' ) ) {
|
|
177
|
+
if ( visited.has( node ) ) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const itemIndent = node.getAttribute( 'listIndent' );
|
|
182
|
+
|
|
183
|
+
// Current node is at the lower indent so trim the stack.
|
|
184
|
+
if ( previous && itemIndent < previous.getAttribute( 'listIndent' ) ) {
|
|
185
|
+
stack.length = itemIndent + 1;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Update the stack for the current indent level.
|
|
189
|
+
stack[ itemIndent ] = Object.fromEntries(
|
|
190
|
+
Array.from( node.getAttributes() )
|
|
191
|
+
.filter( ( [ key ] ) => attributeNames.includes( key ) )
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// Find all blocks of the current node.
|
|
195
|
+
const blocks = getListItemBlocks( node, { direction: 'forward' } );
|
|
196
|
+
|
|
197
|
+
for ( const block of blocks ) {
|
|
198
|
+
visited.add( block );
|
|
199
|
+
|
|
200
|
+
// Check if bogus vs plain paragraph needs refresh.
|
|
201
|
+
if ( doesItemParagraphRequiresRefresh( block, blocks ) ) {
|
|
202
|
+
itemsToRefresh.push( block );
|
|
203
|
+
}
|
|
204
|
+
// Check if wrapping with UL, OL, LIs needs refresh.
|
|
205
|
+
else if ( doesItemWrappingRequiresRefresh( block, stack, changedItems ) ) {
|
|
206
|
+
itemsToRefresh.push( block );
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return itemsToRefresh;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function doesItemParagraphRequiresRefresh( item, blocks ) {
|
|
215
|
+
if ( !item.is( 'element', 'paragraph' ) ) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const viewElement = editing.mapper.toViewElement( item );
|
|
220
|
+
|
|
221
|
+
if ( !viewElement ) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const useBogus = shouldUseBogusParagraph( item, attributeNames, blocks );
|
|
226
|
+
|
|
227
|
+
if ( useBogus && viewElement.is( 'element', 'p' ) ) {
|
|
228
|
+
return true;
|
|
229
|
+
} else if ( !useBogus && viewElement.is( 'element', 'span' ) ) {
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function doesItemWrappingRequiresRefresh( item, stack, changedItems ) {
|
|
237
|
+
// Items directly affected by some "change" don't need a refresh, they will be converted by their own changes.
|
|
238
|
+
if ( changedItems.has( item ) ) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const viewElement = editing.mapper.toViewElement( item );
|
|
243
|
+
let indent = stack.length - 1;
|
|
244
|
+
|
|
245
|
+
// Traverse down the stack to the root to verify if all ULs, OLs, and LIs are as expected.
|
|
246
|
+
for (
|
|
247
|
+
let element = viewElement.parent;
|
|
248
|
+
!element.is( 'editableElement' );
|
|
249
|
+
element = element.parent
|
|
250
|
+
) {
|
|
251
|
+
const isListItemElement = isListItemView( element );
|
|
252
|
+
const isListElement = isListView( element );
|
|
253
|
+
|
|
254
|
+
if ( !isListElement && !isListItemElement ) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Event fired on changes detected on the model list element to verify if the view representation of a list element
|
|
260
|
+
* is representing those attributes.
|
|
261
|
+
*
|
|
262
|
+
* It allows triggering a re-wrapping of a list item.
|
|
263
|
+
*
|
|
264
|
+
* **Note**: For convenience this event is namespaced and could be captured as `checkAttributes:list` or `checkAttributes:item`.
|
|
265
|
+
*
|
|
266
|
+
* @protected
|
|
267
|
+
* @event module:list/documentlist/documentlistediting~DocumentListEditing#event:checkAttributes
|
|
268
|
+
* @param {module:engine/view/element~Element} viewElement
|
|
269
|
+
* @param {Object} modelAttributes
|
|
270
|
+
*/
|
|
271
|
+
const eventName = `checkAttributes:${ isListItemElement ? 'item' : 'list' }`;
|
|
272
|
+
const needsRefresh = documentListEditing.fire( eventName, {
|
|
273
|
+
viewElement: element,
|
|
274
|
+
modelAttributes: stack[ indent ]
|
|
275
|
+
} );
|
|
276
|
+
|
|
277
|
+
if ( needsRefresh ) {
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if ( isListElement ) {
|
|
282
|
+
indent--;
|
|
283
|
+
|
|
284
|
+
// Don't need to iterate further if we already know that the item is wrapped appropriately.
|
|
285
|
+
if ( indent < 0 ) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Returns the list item downcast converter.
|
|
297
|
+
*
|
|
298
|
+
* @protected
|
|
299
|
+
* @param {Array.<String>} attributeNames A list of attribute names that should be converted if are set.
|
|
300
|
+
* @param {Array.<module:list/documentlistproperties/documentlistpropertiesediting~AttributeStrategy>} strategies The strategies.
|
|
301
|
+
* @param {module:engine/model/model~Model} model The model.
|
|
302
|
+
* @returns {Function}
|
|
303
|
+
*/
|
|
304
|
+
export function listItemDowncastConverter( attributeNames, strategies, model ) {
|
|
305
|
+
const consumer = createAttributesConsumer( attributeNames );
|
|
306
|
+
|
|
307
|
+
return ( evt, data, conversionApi ) => {
|
|
308
|
+
const { writer, mapper, consumable } = conversionApi;
|
|
309
|
+
|
|
310
|
+
const listItem = data.item;
|
|
311
|
+
|
|
312
|
+
if ( !attributeNames.includes( data.attributeKey ) ) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Test if attributes on the converted items are not consumed.
|
|
317
|
+
if ( !consumer( listItem, consumable ) ) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Use positions mapping instead of mapper.toViewElement( listItem ) to find outermost view element.
|
|
322
|
+
// This is for cases when mapping is using inner view element like in the code blocks (pre > code).
|
|
323
|
+
const viewElement = findMappedViewElement( listItem, mapper, model );
|
|
324
|
+
|
|
325
|
+
// Unwrap element from current list wrappers.
|
|
326
|
+
unwrapListItemBlock( viewElement, writer );
|
|
327
|
+
|
|
328
|
+
// Then wrap them with the new list wrappers.
|
|
329
|
+
wrapListItemBlock( listItem, writer.createRangeOn( viewElement ), strategies, writer );
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Returns the bogus paragraph view element creator. A bogus paragraph is used if a list item contains only a single block or nested list.
|
|
335
|
+
*
|
|
336
|
+
* @protected
|
|
337
|
+
* @param {Array.<String>} attributeNames The list of all model list attributes (including registered strategies).
|
|
338
|
+
* @param {Object} [options]
|
|
339
|
+
* @param {Boolean} [options.dataPipeline=false]
|
|
340
|
+
* @returns {Function}
|
|
341
|
+
*/
|
|
342
|
+
export function bogusParagraphCreator( attributeNames, { dataPipeline } = {} ) {
|
|
343
|
+
return ( modelElement, { writer } ) => {
|
|
344
|
+
// Convert only if a bogus paragraph should be used.
|
|
345
|
+
if ( !shouldUseBogusParagraph( modelElement, attributeNames ) ) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const viewElement = writer.createContainerElement( 'span', { class: 'ck-list-bogus-paragraph' } );
|
|
350
|
+
|
|
351
|
+
if ( dataPipeline ) {
|
|
352
|
+
writer.setCustomProperty( 'dataPipeline:transparentRendering', true, viewElement );
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return viewElement;
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Helper for mapping mode to view elements. It's using positions mapping instead of mapper.toViewElement( element )
|
|
361
|
+
* to find outermost view element. This is for cases when mapping is using inner view element like in the code blocks (pre > code).
|
|
362
|
+
*
|
|
363
|
+
* @protected
|
|
364
|
+
* @param {module:engine/model/element~Element} element The model element.
|
|
365
|
+
* @param {module:engine/conversion/mapper~Mapper} mapper The mapper instance.
|
|
366
|
+
* @param {module:engine/model/model~Model} model The model.
|
|
367
|
+
* @returns {module:engine/view/element~Element|null}
|
|
368
|
+
*/
|
|
369
|
+
export function findMappedViewElement( element, mapper, model ) {
|
|
370
|
+
const modelRange = model.createRangeOn( element );
|
|
371
|
+
const viewRange = mapper.toViewRange( modelRange ).getTrimmed();
|
|
372
|
+
|
|
373
|
+
return viewRange.getContainedElement();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Unwraps all ol, ul, and li attribute elements that are wrapping the provided view element.
|
|
377
|
+
function unwrapListItemBlock( viewElement, viewWriter ) {
|
|
378
|
+
let attributeElement = viewElement.parent;
|
|
379
|
+
|
|
380
|
+
while ( attributeElement.is( 'attributeElement' ) && [ 'ul', 'ol', 'li' ].includes( attributeElement.name ) ) {
|
|
381
|
+
const parentElement = attributeElement.parent;
|
|
382
|
+
|
|
383
|
+
viewWriter.unwrap( viewWriter.createRangeOn( viewElement ), attributeElement );
|
|
384
|
+
|
|
385
|
+
attributeElement = parentElement;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Wraps the given list item with appropriate attribute elements for ul, ol, and li.
|
|
390
|
+
function wrapListItemBlock( listItem, viewRange, strategies, writer ) {
|
|
391
|
+
if ( !listItem.hasAttribute( 'listIndent' ) ) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const listItemIndent = listItem.getAttribute( 'listIndent' );
|
|
396
|
+
let currentListItem = listItem;
|
|
397
|
+
|
|
398
|
+
for ( let indent = listItemIndent; indent >= 0; indent-- ) {
|
|
399
|
+
const listItemViewElement = createListItemElement( writer, indent, currentListItem.getAttribute( 'listItemId' ) );
|
|
400
|
+
const listViewElement = createListElement( writer, indent, currentListItem.getAttribute( 'listType' ) );
|
|
401
|
+
|
|
402
|
+
for ( const strategy of strategies ) {
|
|
403
|
+
if ( currentListItem.hasAttribute( strategy.attributeName ) ) {
|
|
404
|
+
strategy.setAttributeOnDowncast(
|
|
405
|
+
writer,
|
|
406
|
+
currentListItem.getAttribute( strategy.attributeName ),
|
|
407
|
+
strategy.scope == 'list' ? listViewElement : listItemViewElement
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
viewRange = writer.wrap( viewRange, listItemViewElement );
|
|
413
|
+
viewRange = writer.wrap( viewRange, listViewElement );
|
|
414
|
+
|
|
415
|
+
if ( indent == 0 ) {
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
currentListItem = ListWalker.first( currentListItem, { lowerIndent: true } );
|
|
420
|
+
|
|
421
|
+
// There is no list item with lower indent, this means this is a document fragment containing
|
|
422
|
+
// only a part of nested list (like copy to clipboard) so we don't need to try to wrap it further.
|
|
423
|
+
if ( !currentListItem ) {
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Returns the function that is responsible for consuming attributes that are set on the model node.
|
|
430
|
+
function createAttributesConsumer( attributeNames ) {
|
|
431
|
+
return ( node, consumable ) => {
|
|
432
|
+
const events = [];
|
|
433
|
+
|
|
434
|
+
// Collect all set attributes that are triggering conversion.
|
|
435
|
+
for ( const attributeName of attributeNames ) {
|
|
436
|
+
if ( node.hasAttribute( attributeName ) ) {
|
|
437
|
+
events.push( `attribute:${ attributeName }` );
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if ( !events.every( event => consumable.test( node, event ) !== false ) ) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
events.forEach( event => consumable.consume( node, event ) );
|
|
446
|
+
|
|
447
|
+
return true;
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Whether the given item should be rendered as a bogus paragraph.
|
|
452
|
+
function shouldUseBogusParagraph( item, attributeNames, blocks = getAllListItemBlocks( item ) ) {
|
|
453
|
+
if ( !isListItemBlock( item ) ) {
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
for ( const attributeKey of item.getAttributeKeys() ) {
|
|
458
|
+
// Ignore selection attributes stored on block elements.
|
|
459
|
+
if ( attributeKey.startsWith( 'selection:' ) ) {
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Don't use bogus paragraph if there are attributes from other features.
|
|
464
|
+
if ( !attributeNames.includes( attributeKey ) ) {
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return blocks.length < 2;
|
|
470
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
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/documentlistcommand
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Command } from 'ckeditor5/src/core';
|
|
11
|
+
import {
|
|
12
|
+
splitListItemBefore,
|
|
13
|
+
expandListBlocksToCompleteItems,
|
|
14
|
+
getListItemBlocks,
|
|
15
|
+
getListItems,
|
|
16
|
+
removeListAttributes,
|
|
17
|
+
outdentFollowingItems,
|
|
18
|
+
ListItemUid,
|
|
19
|
+
sortBlocks,
|
|
20
|
+
getSelectedBlockObject,
|
|
21
|
+
isListItemBlock
|
|
22
|
+
} from './utils/model';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The list command. It is used by the {@link module:list/documentlist~DocumentList document list feature}.
|
|
26
|
+
*
|
|
27
|
+
* @extends module:core/command~Command
|
|
28
|
+
*/
|
|
29
|
+
export default class DocumentListCommand extends Command {
|
|
30
|
+
/**
|
|
31
|
+
* Creates an instance of the command.
|
|
32
|
+
*
|
|
33
|
+
* @param {module:core/editor/editor~Editor} editor The editor instance.
|
|
34
|
+
* @param {'numbered'|'bulleted'} type List type that will be handled by this command.
|
|
35
|
+
*/
|
|
36
|
+
constructor( editor, type ) {
|
|
37
|
+
super( editor );
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The type of the list created by the command.
|
|
41
|
+
*
|
|
42
|
+
* @readonly
|
|
43
|
+
* @member {'numbered'|'bulleted'}
|
|
44
|
+
*/
|
|
45
|
+
this.type = type;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A flag indicating whether the command is active, which means that the selection starts in a list of the same type.
|
|
49
|
+
*
|
|
50
|
+
* @observable
|
|
51
|
+
* @readonly
|
|
52
|
+
* @member {Boolean} #value
|
|
53
|
+
*/
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
refresh() {
|
|
60
|
+
this.value = this._getValue();
|
|
61
|
+
this.isEnabled = this._checkEnabled();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Executes the list command.
|
|
66
|
+
*
|
|
67
|
+
* @fires execute
|
|
68
|
+
* @fires afterExecute
|
|
69
|
+
* @param {Object} [options] Command options.
|
|
70
|
+
* @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will try to convert the
|
|
71
|
+
* selected items and potentially the neighbor elements to the proper list items. If set to `false` it will convert selected elements
|
|
72
|
+
* to paragraphs. If not set, the command will toggle selected elements to list items or paragraphs, depending on the selection.
|
|
73
|
+
*/
|
|
74
|
+
execute( options = {} ) {
|
|
75
|
+
const model = this.editor.model;
|
|
76
|
+
const document = model.document;
|
|
77
|
+
const selectedBlockObject = getSelectedBlockObject( model );
|
|
78
|
+
|
|
79
|
+
const blocks = Array.from( document.selection.getSelectedBlocks() )
|
|
80
|
+
.filter( block => model.schema.checkAttribute( block, 'listType' ) );
|
|
81
|
+
|
|
82
|
+
// Whether we are turning off some items.
|
|
83
|
+
const turnOff = options.forceValue !== undefined ? !options.forceValue : this.value;
|
|
84
|
+
|
|
85
|
+
model.change( writer => {
|
|
86
|
+
if ( turnOff ) {
|
|
87
|
+
const lastBlock = blocks[ blocks.length - 1 ];
|
|
88
|
+
|
|
89
|
+
// Split the first block from the list item.
|
|
90
|
+
const itemBlocks = getListItemBlocks( lastBlock, { direction: 'forward' } );
|
|
91
|
+
const changedBlocks = [];
|
|
92
|
+
|
|
93
|
+
if ( itemBlocks.length > 1 ) {
|
|
94
|
+
changedBlocks.push( ...splitListItemBefore( itemBlocks[ 1 ], writer ) );
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Convert list blocks to plain blocks.
|
|
98
|
+
changedBlocks.push( ...removeListAttributes( blocks, writer ) );
|
|
99
|
+
|
|
100
|
+
// Outdent items following the selected list item.
|
|
101
|
+
changedBlocks.push( ...outdentFollowingItems( lastBlock, writer ) );
|
|
102
|
+
|
|
103
|
+
this._fireAfterExecute( changedBlocks );
|
|
104
|
+
}
|
|
105
|
+
// Turning on the list items for a collapsed selection inside a list item.
|
|
106
|
+
else if ( ( selectedBlockObject || document.selection.isCollapsed ) && isListItemBlock( blocks[ 0 ] ) ) {
|
|
107
|
+
const changedBlocks = getListItems( selectedBlockObject || blocks[ 0 ] );
|
|
108
|
+
|
|
109
|
+
for ( const block of changedBlocks ) {
|
|
110
|
+
writer.setAttribute( 'listType', this.type, block );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this._fireAfterExecute( changedBlocks );
|
|
114
|
+
}
|
|
115
|
+
// Turning on the list items for a non-collapsed selection.
|
|
116
|
+
else {
|
|
117
|
+
const changedBlocks = [];
|
|
118
|
+
|
|
119
|
+
for ( const block of blocks ) {
|
|
120
|
+
// Promote the given block to the list item.
|
|
121
|
+
if ( !block.hasAttribute( 'listType' ) ) {
|
|
122
|
+
writer.setAttributes( {
|
|
123
|
+
listIndent: 0,
|
|
124
|
+
listItemId: ListItemUid.next(),
|
|
125
|
+
listType: this.type
|
|
126
|
+
}, block );
|
|
127
|
+
|
|
128
|
+
changedBlocks.push( block );
|
|
129
|
+
}
|
|
130
|
+
// Change the type of list item.
|
|
131
|
+
else {
|
|
132
|
+
for ( const node of expandListBlocksToCompleteItems( block, { withNested: false } ) ) {
|
|
133
|
+
if ( node.getAttribute( 'listType' ) != this.type ) {
|
|
134
|
+
writer.setAttribute( 'listType', this.type, node );
|
|
135
|
+
changedBlocks.push( node );
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this._fireAfterExecute( changedBlocks );
|
|
142
|
+
}
|
|
143
|
+
} );
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Fires the `afterExecute` event.
|
|
148
|
+
*
|
|
149
|
+
* @private
|
|
150
|
+
* @param {Array.<module:engine/model/element~Element>} changedBlocks The changed list elements.
|
|
151
|
+
*/
|
|
152
|
+
_fireAfterExecute( changedBlocks ) {
|
|
153
|
+
/**
|
|
154
|
+
* Event fired by the {@link #execute} method.
|
|
155
|
+
*
|
|
156
|
+
* It allows to execute an action after executing the {@link ~DocumentListCommand#execute} method,
|
|
157
|
+
* for example adjusting attributes of changed list items.
|
|
158
|
+
*
|
|
159
|
+
* @protected
|
|
160
|
+
* @event afterExecute
|
|
161
|
+
*/
|
|
162
|
+
this.fire( 'afterExecute', sortBlocks( new Set( changedBlocks ) ) );
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Checks the command's {@link #value}.
|
|
167
|
+
*
|
|
168
|
+
* @private
|
|
169
|
+
* @returns {Boolean} The current value.
|
|
170
|
+
*/
|
|
171
|
+
_getValue() {
|
|
172
|
+
const selection = this.editor.model.document.selection;
|
|
173
|
+
const blocks = Array.from( selection.getSelectedBlocks() );
|
|
174
|
+
|
|
175
|
+
if ( !blocks.length ) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
for ( const block of blocks ) {
|
|
180
|
+
if ( block.getAttribute( 'listType' ) != this.type ) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Checks whether the command can be enabled in the current context.
|
|
190
|
+
*
|
|
191
|
+
* @private
|
|
192
|
+
* @returns {Boolean} Whether the command should be enabled.
|
|
193
|
+
*/
|
|
194
|
+
_checkEnabled() {
|
|
195
|
+
const selection = this.editor.model.document.selection;
|
|
196
|
+
const schema = this.editor.model.schema;
|
|
197
|
+
const blocks = Array.from( selection.getSelectedBlocks() );
|
|
198
|
+
|
|
199
|
+
if ( !blocks.length ) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// If command value is true it means that we are in list item, so the command should be enabled.
|
|
204
|
+
if ( this.value ) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
for ( const block of blocks ) {
|
|
209
|
+
if ( schema.checkAttribute( block, 'listType' ) ) {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|