/
listpropertiesview.ts
430 lines (360 loc) · 12.7 KB
/
listpropertiesview.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
423
424
425
426
427
428
429
430
/**
* @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/ui/listpropertiesview
*/
import {
View,
ViewCollection,
FocusCycler,
SwitchButtonView,
LabeledFieldView,
createLabeledInputNumber,
addKeyboardHandlingForGrid,
CollapsibleView,
type ButtonView,
type InputNumberView,
type FocusableView
} from 'ckeditor5/src/ui.js';
import {
FocusTracker,
KeystrokeHandler,
global,
type Locale
} from 'ckeditor5/src/utils.js';
import type { ListPropertiesConfig } from '../../listconfig.js';
import '../../../theme/listproperties.css';
/**
* The list properties view to be displayed in the list dropdown.
*
* Contains a grid of available list styles and, for numbered list, also the list start index and reversed fields.
*
* @internal
*/
export default class ListPropertiesView extends View {
/**
* @inheritDoc
*/
declare public locale: Locale;
/**
* A collection of the child views.
*/
public readonly children: ViewCollection;
/**
* A view that renders the grid of list styles.
*/
public readonly stylesView: StylesView | null = null;
/**
* A collapsible view that hosts additional list property fields ({@link #startIndexFieldView} and
* {@link #reversedSwitchButtonView}) to visually separate them from the {@link #stylesView grid of styles}.
*
* **Note**: Only present when:
* * the view represents **numbered** list properties,
* * and the {@link #stylesView} is rendered,
* * and either {@link #startIndexFieldView} or {@link #reversedSwitchButtonView} is rendered.
*
* @readonly
*/
public additionalPropertiesCollapsibleView: CollapsibleView | null = null;
/**
* A labeled number field allowing the user to set the start index of the list.
*
* **Note**: Only present when the view represents **numbered** list properties.
*
* @readonly
*/
public startIndexFieldView: LabeledFieldView<InputNumberView> | null = null;
/**
* A switch button allowing the user to make the edited list reversed.
*
* **Note**: Only present when the view represents **numbered** list properties.
*
* @readonly
*/
public reversedSwitchButtonView: SwitchButtonView | null = null;
/**
* Tracks information about the DOM focus in the view.
*/
public readonly focusTracker: FocusTracker = new FocusTracker();
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*/
public readonly keystrokes: KeystrokeHandler = new KeystrokeHandler();
/**
* A collection of views that can be focused in the properties view.
*/
public readonly focusables = new ViewCollection<FocusableView>();
/**
* Helps cycling over {@link #focusables} in the view.
*/
public readonly focusCycler: FocusCycler;
/**
* Creates an instance of the list properties view.
*
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
* @param options Options of the view.
* @param options.enabledProperties An object containing the configuration of enabled list property names.
* Allows conditional rendering the sub-components of the properties view.
* @param options.styleButtonViews A list of style buttons to be rendered
* inside the styles grid. The grid will not be rendered when `enabledProperties` does not include the `'styles'` key.
* @param options.styleGridAriaLabel An assistive technologies label set on the grid of styles (if the grid is rendered).
*/
constructor(
locale: Locale,
{ enabledProperties, styleButtonViews, styleGridAriaLabel }: {
enabledProperties: ListPropertiesConfig;
styleButtonViews: Array<ButtonView> | null;
styleGridAriaLabel: string;
}
) {
super( locale );
const elementCssClasses = [
'ck',
'ck-list-properties'
];
this.children = this.createCollection();
this.focusCycler = new FocusCycler( {
focusables: this.focusables,
focusTracker: this.focusTracker,
keystrokeHandler: this.keystrokes,
actions: {
// Navigate #children backwards using the <kbd>Shift</kbd> + <kbd>Tab</kbd> keystroke.
focusPrevious: 'shift + tab',
// Navigate #children forwards using the <kbd>Tab</kbd> key.
focusNext: 'tab'
}
} );
// The rendering of the styles grid is conditional. When there is no styles grid, the view will render without collapsible
// for numbered list properties, hence simplifying the layout.
if ( enabledProperties.styles ) {
this.stylesView = this._createStylesView( styleButtonViews!, styleGridAriaLabel );
this.children.add( this.stylesView );
} else {
elementCssClasses.push( 'ck-list-properties_without-styles' );
}
// The rendering of the numbered list property views is also conditional. It only makes sense for the numbered list
// dropdown. The unordered list does not have such properties.
if ( enabledProperties.startIndex || enabledProperties.reversed ) {
this._addNumberedListPropertyViews( enabledProperties );
elementCssClasses.push( 'ck-list-properties_with-numbered-properties' );
}
this.setTemplate( {
tag: 'div',
attributes: {
class: elementCssClasses
},
children: this.children
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
if ( this.stylesView ) {
this.focusables.add( this.stylesView );
this.focusTracker.add( this.stylesView.element! );
// Register the collapsible toggle button to the focus system.
if ( this.startIndexFieldView || this.reversedSwitchButtonView ) {
this.focusables.add( ( this.children.last as any ).buttonView );
this.focusTracker.add( ( this.children.last as any ).buttonView.element );
}
for ( const item of this.stylesView.children ) {
this.stylesView.focusTracker.add( item.element! );
}
addKeyboardHandlingForGrid( {
keystrokeHandler: this.stylesView.keystrokes,
focusTracker: this.stylesView.focusTracker,
gridItems: this.stylesView.children,
// Note: The styles view has a different number of columns depending on whether the other properties
// are enabled in the dropdown or not (https://github.com/ckeditor/ckeditor5/issues/12340)
numberOfColumns: () => global.window
.getComputedStyle( this.stylesView!.element! )
.getPropertyValue( 'grid-template-columns' )
.split( ' ' )
.length,
uiLanguageDirection: this.locale && this.locale.uiLanguageDirection
} );
}
if ( this.startIndexFieldView ) {
this.focusables.add( this.startIndexFieldView );
this.focusTracker.add( this.startIndexFieldView.element! );
const stopPropagation = ( data: Event ) => data.stopPropagation();
// Since the form is in the dropdown panel which is a child of the toolbar, the toolbar's
// keystroke handler would take over the key management in the input. We need to prevent
// this ASAP. Otherwise, the basic caret movement using the arrow keys will be impossible.
this.keystrokes.set( 'arrowright', stopPropagation );
this.keystrokes.set( 'arrowleft', stopPropagation );
this.keystrokes.set( 'arrowup', stopPropagation );
this.keystrokes.set( 'arrowdown', stopPropagation );
}
if ( this.reversedSwitchButtonView ) {
this.focusables.add( this.reversedSwitchButtonView );
this.focusTracker.add( this.reversedSwitchButtonView.element! );
}
// Start listening for the keystrokes coming from #element.
this.keystrokes.listenTo( this.element! );
}
/**
* @inheritDoc
*/
public focus(): void {
this.focusCycler.focusFirst();
}
/**
* @inheritDoc
*/
public focusLast(): void {
this.focusCycler.focusLast();
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this.focusTracker.destroy();
this.keystrokes.destroy();
}
/**
* Creates the list styles grid.
*
* @param styleButtons Buttons to be placed in the grid.
* @param styleGridAriaLabel The assistive technology label of the grid.
*/
private _createStylesView( styleButtons: Array<ButtonView>, styleGridAriaLabel: string ) {
const stylesView = new View( this.locale ) as StylesView;
stylesView.children = stylesView.createCollection();
stylesView.children.addMany( styleButtons );
stylesView.setTemplate( {
tag: 'div',
attributes: {
'aria-label': styleGridAriaLabel,
class: [
'ck',
'ck-list-styles-list'
]
},
children: stylesView.children
} );
stylesView.children.delegate( 'execute' ).to( this );
stylesView.focus = function( this: any ) {
this.children.first.focus();
};
stylesView.focusTracker = new FocusTracker();
stylesView.keystrokes = new KeystrokeHandler();
stylesView.render();
stylesView.keystrokes.listenTo( stylesView.element! );
return stylesView;
}
/**
* Renders {@link #startIndexFieldView} and/or {@link #reversedSwitchButtonView} depending on the configuration of the properties view.
*
* @param enabledProperties An object containing the configuration of enabled list property names
* (see {@link #constructor}).
*/
private _addNumberedListPropertyViews( enabledProperties: ListPropertiesConfig ) {
const t = this.locale.t;
const numberedPropertyViews = [];
if ( enabledProperties.startIndex ) {
this.startIndexFieldView = this._createStartIndexField();
numberedPropertyViews.push( this.startIndexFieldView );
}
if ( enabledProperties.reversed ) {
this.reversedSwitchButtonView = this._createReversedSwitchButton();
numberedPropertyViews.push( this.reversedSwitchButtonView );
}
// When there are some style buttons, pack the numbered list properties into a collapsible to separate them.
if ( enabledProperties.styles ) {
this.additionalPropertiesCollapsibleView = new CollapsibleView( this.locale, numberedPropertyViews );
this.additionalPropertiesCollapsibleView.set( {
label: t( 'List properties' ),
isCollapsed: true
} );
// Don't enable the collapsible view unless either start index or reversed field is enabled (e.g. when no list is selected).
this.additionalPropertiesCollapsibleView.buttonView.bind( 'isEnabled' ).toMany(
numberedPropertyViews, 'isEnabled', ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) );
// Automatically collapse the additional properties collapsible when either start index or reversed field gets disabled.
this.additionalPropertiesCollapsibleView.buttonView.on( 'change:isEnabled', ( evt, data, isEnabled ) => {
if ( !isEnabled ) {
this.additionalPropertiesCollapsibleView!.isCollapsed = true;
}
} );
this.children.add( this.additionalPropertiesCollapsibleView );
} else {
this.children.addMany( numberedPropertyViews );
}
}
/**
* Creates the list start index labeled field.
*/
private _createStartIndexField() {
const t = this.locale.t;
const startIndexFieldView = new LabeledFieldView( this.locale, createLabeledInputNumber );
startIndexFieldView.set( {
label: t( 'Start at' ),
class: 'ck-numbered-list-properties__start-index'
} );
startIndexFieldView.fieldView.set( {
min: 0,
step: 1,
value: 1,
inputMode: 'numeric'
} );
startIndexFieldView.fieldView.on( 'input', () => {
const inputElement = startIndexFieldView.fieldView.element!;
const startIndex = inputElement.valueAsNumber;
if ( Number.isNaN( startIndex ) ) {
// Number inputs allow for the entry of characters that may result in NaN,
// such as 'e', '+', '123e', '2-'.
startIndexFieldView.errorText = t( 'Invalid start index value.' );
return;
}
if ( !inputElement.checkValidity() ) {
startIndexFieldView.errorText = t( 'Start index must be greater than 0.' );
} else {
this.fire<ListPropertiesViewListStartEvent>( 'listStart', { startIndex } );
}
} );
return startIndexFieldView;
}
/**
* Creates the reversed list switch button.
*/
private _createReversedSwitchButton() {
const t = this.locale.t;
const reversedButtonView = new SwitchButtonView( this.locale );
reversedButtonView.set( {
withText: true,
label: t( 'Reversed order' ),
class: 'ck-numbered-list-properties__reversed-order'
} );
reversedButtonView.delegate( 'execute' ).to( this, 'listReversed' );
return reversedButtonView;
}
}
export type StylesView = View & {
children: ViewCollection;
focusTracker: FocusTracker;
keystrokes: KeystrokeHandler;
focus(): void;
};
/**
* Fired when the list start index value has changed via {@link ~ListPropertiesView#startIndexFieldView}.
*
* @eventName ~ListPropertiesView#listStart
*/
export type ListPropertiesViewListStartEvent = {
name: 'listStart';
args: [ data: { startIndex: number } ];
};
/**
* Fired when the list order has changed (reversed) via {@link ~ListPropertiesView#reversedSwitchButtonView}.
*
* @eventName ~ListPropertiesView#listReversed
*/
export type ListPropertiesViewListReversedEvent = {
name: 'listReversed';
args: [];
};