-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
listpropertiesediting.ts
422 lines (346 loc) · 11.2 KB
/
listpropertiesediting.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* @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/listproperties/listpropertiesediting
*/
import { Plugin, type Editor } from 'ckeditor5/src/core.js';
import type {
Consumables,
DowncastWriter,
Element,
Item,
UpcastElementEvent,
ViewElement
} from 'ckeditor5/src/engine.js';
import ListEditing, {
type ListType,
type ListEditingCheckAttributesEvent,
type ListEditingPostFixerEvent,
type ListItemAttributesMap
} from '../list/listediting.js';
import ListStartCommand from './liststartcommand.js';
import ListStyleCommand from './liststylecommand.js';
import ListReversedCommand from './listreversedcommand.js';
import { listPropertiesUpcastConverter } from './converters.js';
import {
getAllSupportedStyleTypes,
getListTypeFromListStyleType,
getListStyleTypeFromTypeAttribute,
getTypeAttributeFromListStyleType
} from './utils/style.js';
import ListPropertiesUtils from './listpropertiesutils.js';
import {
isNumberedListType
} from '../list/utils/model.js';
import type { ListIndentCommandAfterExecuteEvent } from '../list/listindentcommand.js';
import type { ListPropertiesConfig } from '../listconfig.js';
const DEFAULT_LIST_TYPE = 'default';
/**
* The document list properties engine feature.
*
* It registers the `'listStyle'`, `'listReversed'` and `'listStart'` commands if they are enabled in the configuration.
* Read more in {@link module:list/listconfig~ListPropertiesConfig}.
*/
export default class ListPropertiesEditing extends Plugin {
/**
* @inheritDoc
*/
public static get requires() {
return [ ListEditing, ListPropertiesUtils ] as const;
}
/**
* @inheritDoc
*/
public static get pluginName() {
return 'ListPropertiesEditing' as const;
}
/**
* @inheritDoc
*/
constructor( editor: Editor ) {
super( editor );
editor.config.define( 'list.properties', {
styles: true,
startIndex: false,
reversed: false
} );
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const model = editor.model;
const listEditing = editor.plugins.get( ListEditing );
const enabledProperties = editor.config.get( 'list.properties' )!;
const strategies = createAttributeStrategies( enabledProperties );
for ( const strategy of strategies ) {
strategy.addCommand( editor );
model.schema.extend( '$listItem', { allowAttributes: strategy.attributeName } );
// Register downcast strategy.
listEditing.registerDowncastStrategy( {
scope: 'list',
attributeName: strategy.attributeName,
setAttributeOnDowncast( writer, attributeValue, viewElement ) {
strategy.setAttributeOnDowncast( writer, attributeValue, viewElement );
}
} );
}
// Set up conversion.
editor.conversion.for( 'upcast' ).add( dispatcher => {
for ( const strategy of strategies ) {
dispatcher.on<UpcastElementEvent>( 'element:ol', listPropertiesUpcastConverter( strategy ) );
dispatcher.on<UpcastElementEvent>( 'element:ul', listPropertiesUpcastConverter( strategy ) );
}
} );
// Verify if the list view element (ul or ol) requires refreshing.
listEditing.on<ListEditingCheckAttributesEvent>(
'checkAttributes:list',
( evt, { viewElement, modelAttributes } ) => {
for ( const strategy of strategies ) {
if ( strategy.getAttributeOnUpcast( viewElement ) != modelAttributes[ strategy.attributeName ] ) {
evt.return = true;
evt.stop();
}
}
}
);
// Reset list properties after indenting list items.
this.listenTo<ListIndentCommandAfterExecuteEvent>(
editor.commands.get( 'indentList' )!,
'afterExecute',
( evt, changedBlocks ) => {
model.change( writer => {
for ( const node of changedBlocks ) {
for ( const strategy of strategies ) {
if ( strategy.appliesToListItem( node ) ) {
// Just reset the attribute.
// If there is a previous indented list that this node should be merged into,
// the postfixer will unify all the attributes of both sub-lists.
writer.setAttribute( strategy.attributeName, strategy.defaultValue, node );
}
}
}
} );
}
);
// Add or remove list properties attributes depending on the list type.
listEditing.on<ListEditingPostFixerEvent>( 'postFixer', ( evt, { listNodes, writer } ) => {
for ( const { node } of listNodes ) {
for ( const strategy of strategies ) {
// Check if attribute is valid.
if ( strategy.hasValidAttribute( node ) ) {
continue;
}
// Add missing default property attributes...
if ( strategy.appliesToListItem( node ) ) {
writer.setAttribute( strategy.attributeName, strategy.defaultValue, node );
}
// ...or remove invalid property attributes.
else {
writer.removeAttribute( strategy.attributeName, node );
}
evt.return = true;
}
}
} );
// Make sure that all items in a single list (items at the same level & listType) have the same properties.
listEditing.on<ListEditingPostFixerEvent>( 'postFixer', ( evt, { listNodes, writer } ) => {
for ( const { node, previousNodeInList } of listNodes ) {
// This is a first item of a nested list.
if ( !previousNodeInList ) {
continue;
}
// This is a first block of a list of a different type.
if ( previousNodeInList.getAttribute( 'listType' ) != node.getAttribute( 'listType' ) ) {
continue;
}
// Copy properties from the previous one.
for ( const strategy of strategies ) {
const { attributeName } = strategy;
if ( !strategy.appliesToListItem( node ) ) {
continue;
}
const value = previousNodeInList.getAttribute( attributeName );
if ( node.getAttribute( attributeName ) != value ) {
writer.setAttribute( attributeName, value, node );
evt.return = true;
}
}
}
} );
}
}
/**
* Strategy for dealing with `listItem` attributes supported by this plugin.
*
* @internal
*/
export interface AttributeStrategy {
/**
* The model attribute name.
*/
attributeName: keyof ListItemAttributesMap;
/**
* The model attribute default value.
*/
defaultValue: unknown;
/**
* The view consumable as expected by {@link module:engine/conversion/viewconsumable~ViewConsumable#consume `ViewConsumable`}.
*/
viewConsumables: Consumables;
/**
* Registers an editor command.
*/
addCommand( editor: Editor ): void;
/**
* Verifies whether the strategy is applicable for the specified model element.
*/
appliesToListItem( element: Item ): boolean;
/**
* Verifies whether the model attribute value is valid.
*/
hasValidAttribute( element: Element ): boolean;
/**
* Sets the property on the view element.
*/
setAttributeOnDowncast( writer: DowncastWriter, value: unknown, element: ViewElement ): void;
/**
* Retrieves the property value from the view element.
*/
getAttributeOnUpcast( element: ViewElement ): unknown;
}
/**
* Creates an array of strategies for dealing with enabled listItem attributes.
*/
function createAttributeStrategies( enabledProperties: ListPropertiesConfig ) {
const strategies: Array<AttributeStrategy> = [];
if ( enabledProperties.styles ) {
const useAttribute = typeof enabledProperties.styles == 'object' && enabledProperties.styles.useAttribute;
strategies.push( {
attributeName: 'listStyle',
defaultValue: DEFAULT_LIST_TYPE,
viewConsumables: { styles: 'list-style-type' },
addCommand( editor ) {
let supportedTypes = getAllSupportedStyleTypes();
if ( useAttribute ) {
supportedTypes = supportedTypes.filter( styleType => !!getTypeAttributeFromListStyleType( styleType ) );
}
editor.commands.add( 'listStyle', new ListStyleCommand( editor, DEFAULT_LIST_TYPE, supportedTypes ) );
},
appliesToListItem( item ) {
return item.getAttribute( 'listType' ) == 'numbered' || item.getAttribute( 'listType' ) == 'bulleted';
},
hasValidAttribute( item ) {
if ( !this.appliesToListItem( item ) ) {
return !item.hasAttribute( 'listStyle' );
}
if ( !item.hasAttribute( 'listStyle' ) ) {
return false;
}
const value = item.getAttribute( 'listStyle' );
if ( value == DEFAULT_LIST_TYPE ) {
return true;
}
return getListTypeFromListStyleType( value as string ) == item.getAttribute( 'listType' );
},
setAttributeOnDowncast( writer, listStyle, element ) {
if ( listStyle && listStyle !== DEFAULT_LIST_TYPE ) {
if ( useAttribute ) {
const value = getTypeAttributeFromListStyleType( listStyle as string );
if ( value ) {
writer.setAttribute( 'type', value, element );
return;
}
} else {
writer.setStyle( 'list-style-type', listStyle as string, element );
return;
}
}
writer.removeStyle( 'list-style-type', element );
writer.removeAttribute( 'type', element );
},
getAttributeOnUpcast( listParent ) {
const style = listParent.getStyle( 'list-style-type' );
if ( style ) {
return style;
}
const attribute = listParent.getAttribute( 'type' );
if ( attribute ) {
return getListStyleTypeFromTypeAttribute( attribute );
}
return DEFAULT_LIST_TYPE;
}
} );
}
if ( enabledProperties.reversed ) {
strategies.push( {
attributeName: 'listReversed',
defaultValue: false,
viewConsumables: { attributes: 'reversed' },
addCommand( editor ) {
editor.commands.add( 'listReversed', new ListReversedCommand( editor ) );
},
appliesToListItem( item ) {
return item.getAttribute( 'listType' ) == 'numbered';
},
hasValidAttribute( item ) {
return this.appliesToListItem( item ) == item.hasAttribute( 'listReversed' );
},
setAttributeOnDowncast( writer, listReversed, element ) {
if ( listReversed ) {
writer.setAttribute( 'reversed', 'reversed', element );
} else {
writer.removeAttribute( 'reversed', element );
}
},
getAttributeOnUpcast( listParent ) {
return listParent.hasAttribute( 'reversed' );
}
} );
}
if ( enabledProperties.startIndex ) {
strategies.push( {
attributeName: 'listStart',
defaultValue: 1,
viewConsumables: { attributes: 'start' },
addCommand( editor ) {
editor.commands.add( 'listStart', new ListStartCommand( editor ) );
},
appliesToListItem( item ) {
return isNumberedListType( item.getAttribute( 'listType' ) as ListType );
},
hasValidAttribute( item ) {
return this.appliesToListItem( item ) == item.hasAttribute( 'listStart' );
},
setAttributeOnDowncast( writer, listStart, element ) {
if ( listStart == 0 || ( listStart as number ) > 1 ) {
writer.setAttribute( 'start', listStart, element );
} else {
writer.removeAttribute( 'start', element );
}
},
getAttributeOnUpcast( listParent ) {
const startAttributeValue: any = listParent.getAttribute( 'start' );
return startAttributeValue >= 0 ? startAttributeValue : 1;
}
} );
}
return strategies;
}
declare module '../list/listediting' {
interface ListItemAttributesMap {
listStyle?: string;
listStart?: number;
listReversed?: boolean;
}
}
declare module '../list/utils/model' {
interface ListElement {
getAttribute( key: 'listStyle' ): string;
getAttribute( key: 'listStart' ): number;
getAttribute( key: 'listReversed' ): boolean;
}
}