/
tablepropertiesview.ts
748 lines (627 loc) · 18.7 KB
/
tablepropertiesview.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/**
* @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 table/tableproperties/ui/tablepropertiesview
*/
import {
addListToDropdown,
ButtonView,
createLabeledDropdown,
createLabeledInputText,
FocusCycler,
FormHeaderView,
LabeledFieldView,
LabelView,
submitHandler,
ToolbarView,
View,
ViewCollection,
type DropdownView,
type InputTextView,
type NormalizedColorOption,
type ColorPickerConfig,
type FocusCyclerForwardCycleEvent,
type FocusCyclerBackwardCycleEvent,
type FocusableView
} from 'ckeditor5/src/ui.js';
import { FocusTracker, KeystrokeHandler, type ObservableChangeEvent, type Locale } from 'ckeditor5/src/utils.js';
import { icons } from 'ckeditor5/src/core.js';
import {
fillToolbar,
getBorderStyleDefinitions,
getBorderStyleLabels,
getLabeledColorInputCreator
} from '../../utils/ui/table-properties.js';
import FormRowView from '../../ui/formrowview.js';
import '../../../theme/form.css';
import '../../../theme/tableform.css';
import '../../../theme/tableproperties.css';
import type ColorInputView from '../../ui/colorinputview.js';
import type { TablePropertiesOptions } from '../../tableconfig.js';
const ALIGNMENT_ICONS = {
left: icons.objectLeft,
center: icons.objectCenter,
right: icons.objectRight
};
/**
* Additional configuration of the view.
*/
export interface TablePropertiesViewOptions {
/**
* A configuration of the border color palette used by the
* {@link module:table/tableproperties/ui/tablepropertiesview~TablePropertiesView#borderColorInput}.
*/
borderColors: Array<NormalizedColorOption>;
/**
* A configuration of the background color palette used by the
* {@link module:table/tableproperties/ui/tablepropertiesview~TablePropertiesView#backgroundInput}.
*/
backgroundColors: Array<NormalizedColorOption>;
/**
* The default table properties.
*/
defaultTableProperties: TablePropertiesOptions;
/**
* The default color picker config.
*/
colorPickerConfig: false | ColorPickerConfig;
}
/**
* The class representing a table properties form, allowing users to customize
* certain style aspects of a table, for instance, border, background color, alignment, etc..
*/
export default class TablePropertiesView extends View {
/**
* The value of the border style.
*
* @observable
* @default ''
*/
declare public borderStyle: string;
/**
* The value of the border width style.
*
* @observable
* @default ''
*/
declare public borderWidth: string;
/**
* The value of the border color style.
*
* @observable
* @default ''
*/
declare public borderColor: string;
/**
* The value of the background color style.
*
* @observable
* @default ''
*/
declare public backgroundColor: string;
/**
* The value of the table width style.
*
* @observable
* @default ''
*/
declare public width: string;
/**
* The value of the table height style.
*
* @observable
* @default ''
*/
declare public height: string;
/**
* The value of the table alignment style.
*
* @observable
* @default ''
*/
declare public alignment: string;
/**
* Options passed to the view. See {@link #constructor} to learn more.
*/
public readonly options: TablePropertiesViewOptions;
/**
* Tracks information about the DOM focus in the form.
*/
public readonly focusTracker: FocusTracker;
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* A collection of child views in the form.
*/
public readonly children: ViewCollection;
/**
* A dropdown that allows selecting the style of the table border.
*/
public readonly borderStyleDropdown: LabeledFieldView<DropdownView>;
/**
* An input that allows specifying the width of the table border.
*/
public readonly borderWidthInput: LabeledFieldView<InputTextView>;
/**
* An input that allows specifying the color of the table border.
*/
public readonly borderColorInput: LabeledFieldView<ColorInputView>;
/**
* An input that allows specifying the table background color.
*/
public readonly backgroundInput: LabeledFieldView<ColorInputView>;
/**
* An input that allows specifying the table width.
*/
public readonly widthInput: LabeledFieldView<InputTextView>;
/**
* An input that allows specifying the table height.
*/
public readonly heightInput: LabeledFieldView<InputTextView>;
/**
* A toolbar with buttons that allow changing the alignment of an entire table.
*/
public readonly alignmentToolbar: ToolbarView;
/**
* The "Save" button view.
*/
public saveButtonView: ButtonView;
/**
* The "Cancel" button view.
*/
public cancelButtonView: ButtonView;
/**
* A collection of views that can be focused in the form.
*/
protected readonly _focusables: ViewCollection<FocusableView>;
/**
* Helps cycling over {@link #_focusables} in the form.
*/
protected readonly _focusCycler: FocusCycler;
/**
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
* @param options Additional configuration of the view.
*/
constructor( locale: Locale, options: TablePropertiesViewOptions ) {
super( locale );
this.set( {
borderStyle: '',
borderWidth: '',
borderColor: '',
backgroundColor: '',
width: '',
height: '',
alignment: ''
} );
this.options = options;
const { borderStyleDropdown, borderWidthInput, borderColorInput, borderRowLabel } = this._createBorderFields();
const { backgroundRowLabel, backgroundInput } = this._createBackgroundFields();
const { widthInput, operatorLabel, heightInput, dimensionsLabel } = this._createDimensionFields();
const { alignmentToolbar, alignmentLabel } = this._createAlignmentFields();
this.focusTracker = new FocusTracker();
this.keystrokes = new KeystrokeHandler();
this.children = this.createCollection();
this.borderStyleDropdown = borderStyleDropdown;
this.borderWidthInput = borderWidthInput;
this.borderColorInput = borderColorInput;
this.backgroundInput = backgroundInput;
this.widthInput = widthInput;
this.heightInput = heightInput;
this.alignmentToolbar = alignmentToolbar;
// Defer creating to make sure other fields are present and the Save button can
// bind its #isEnabled to their error messages so there's no way to save unless all
// fields are valid.
const { saveButtonView, cancelButtonView } = this._createActionButtons();
this.saveButtonView = saveButtonView;
this.cancelButtonView = cancelButtonView;
this._focusables = new ViewCollection();
this._focusCycler = new FocusCycler( {
focusables: this._focusables,
focusTracker: this.focusTracker,
keystrokeHandler: this.keystrokes,
actions: {
// Navigate form fields backwards using the Shift + Tab keystroke.
focusPrevious: 'shift + tab',
// Navigate form fields forwards using the Tab key.
focusNext: 'tab'
}
} );
// Form header.
this.children.add( new FormHeaderView( locale, {
label: this.t!( 'Table properties' )
} ) );
// Border row.
this.children.add( new FormRowView( locale, {
labelView: borderRowLabel,
children: [
borderRowLabel,
borderStyleDropdown,
borderColorInput,
borderWidthInput
],
class: 'ck-table-form__border-row'
} ) );
// Background row.
this.children.add( new FormRowView( locale, {
labelView: backgroundRowLabel,
children: [
backgroundRowLabel,
backgroundInput
],
class: 'ck-table-form__background-row'
} ) );
this.children.add( new FormRowView( locale, {
children: [
// Dimensions row.
new FormRowView( locale, {
labelView: dimensionsLabel,
children: [
dimensionsLabel,
widthInput,
operatorLabel,
heightInput
],
class: 'ck-table-form__dimensions-row'
} ),
// Alignment row.
new FormRowView( locale, {
labelView: alignmentLabel,
children: [
alignmentLabel,
alignmentToolbar
],
class: 'ck-table-properties-form__alignment-row'
} )
]
} ) );
// Action row.
this.children.add( new FormRowView( locale, {
children: [
this.saveButtonView,
this.cancelButtonView
],
class: 'ck-table-form__action-row'
} ) );
this.setTemplate( {
tag: 'form',
attributes: {
class: [
'ck',
'ck-form',
'ck-table-form',
'ck-table-properties-form'
],
// https://github.com/ckeditor/ckeditor5-link/issues/90
tabindex: '-1'
},
children: this.children
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
// Enable the "submit" event for this view. It can be triggered by the #saveButtonView
// which is of the "submit" DOM "type".
submitHandler( {
view: this
} );
// Maintain continuous focus cycling over views that have focusable children and focus cyclers themselves.
[ this.borderColorInput, this.backgroundInput ].forEach( view => {
view.fieldView.focusCycler.on<FocusCyclerForwardCycleEvent>( 'forwardCycle', evt => {
this._focusCycler.focusNext();
evt.stop();
} );
view.fieldView.focusCycler.on<FocusCyclerBackwardCycleEvent>( 'backwardCycle', evt => {
this._focusCycler.focusPrevious();
evt.stop();
} );
} );
[
this.borderStyleDropdown,
this.borderColorInput,
this.borderWidthInput,
this.backgroundInput,
this.widthInput,
this.heightInput,
this.alignmentToolbar,
this.saveButtonView,
this.cancelButtonView
].forEach( view => {
// Register the view as focusable.
this._focusables.add( view! );
// Register the view in the focus tracker.
this.focusTracker.add( view!.element! );
} );
// Mainly for closing using "Esc" and navigation using "Tab".
this.keystrokes.listenTo( this.element! );
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this.focusTracker.destroy();
this.keystrokes.destroy();
}
/**
* Focuses the fist focusable field in the form.
*/
public focus(): void {
this._focusCycler.focusFirst();
}
/**
* Creates the following form fields:
*
* * {@link #borderStyleDropdown},
* * {@link #borderWidthInput},
* * {@link #borderColorInput}.
*/
private _createBorderFields() {
const defaultTableProperties = this.options.defaultTableProperties;
const defaultBorder = {
style: defaultTableProperties.borderStyle,
width: defaultTableProperties.borderWidth,
color: defaultTableProperties.borderColor
};
const colorInputCreator = getLabeledColorInputCreator( {
colorConfig: this.options.borderColors,
columns: 5,
defaultColorValue: defaultBorder.color,
colorPickerConfig: this.options.colorPickerConfig
} );
const locale = this.locale;
const t = this.t!;
const accessibleLabel = t( 'Style' );
// -- Group label ---------------------------------------------
const borderRowLabel = new LabelView( locale );
borderRowLabel.text = t( 'Border' );
// -- Style ---------------------------------------------------
const styleLabels = getBorderStyleLabels( t );
const borderStyleDropdown = new LabeledFieldView( locale, createLabeledDropdown );
borderStyleDropdown.set( {
label: accessibleLabel,
class: 'ck-table-form__border-style'
} );
borderStyleDropdown.fieldView.buttonView.set( {
ariaLabel: accessibleLabel,
ariaLabelledBy: undefined,
isOn: false,
withText: true,
tooltip: accessibleLabel
} );
borderStyleDropdown.fieldView.buttonView.bind( 'label' ).to( this, 'borderStyle', value => {
return styleLabels[ value ? value : 'none' ];
} );
borderStyleDropdown.fieldView.on( 'execute', evt => {
this.borderStyle = ( evt.source as any )._borderStyleValue;
} );
borderStyleDropdown.bind( 'isEmpty' ).to( this, 'borderStyle', value => !value );
addListToDropdown( borderStyleDropdown.fieldView, getBorderStyleDefinitions( this, defaultBorder.style! ), {
role: 'menu',
ariaLabel: accessibleLabel
} );
// -- Width ---------------------------------------------------
const borderWidthInput = new LabeledFieldView( locale, createLabeledInputText );
borderWidthInput.set( {
label: t( 'Width' ),
class: 'ck-table-form__border-width'
} );
borderWidthInput.fieldView.bind( 'value' ).to( this, 'borderWidth' );
borderWidthInput.bind( 'isEnabled' ).to( this, 'borderStyle', isBorderStyleSet );
borderWidthInput.fieldView.on( 'input', () => {
this.borderWidth = borderWidthInput.fieldView.element!.value;
} );
// -- Color ---------------------------------------------------
const borderColorInput = new LabeledFieldView( locale, colorInputCreator );
borderColorInput.set( {
label: t( 'Color' ),
class: 'ck-table-form__border-color'
} );
borderColorInput.fieldView.bind( 'value' ).to( this, 'borderColor' );
borderColorInput.bind( 'isEnabled' ).to( this, 'borderStyle', isBorderStyleSet );
borderColorInput.fieldView.on( 'input', () => {
this.borderColor = borderColorInput.fieldView.value;
} );
// Reset the border color and width fields depending on the `border-style` value.
this.on<ObservableChangeEvent<string>>( 'change:borderStyle', ( evt, name, newValue, oldValue ) => {
// When removing the border (`border-style:none`), clear the remaining `border-*` properties.
// See: https://github.com/ckeditor/ckeditor5/issues/6227.
if ( !isBorderStyleSet( newValue ) ) {
this.borderColor = '';
this.borderWidth = '';
}
// When setting the `border-style` from `none`, set the default `border-color` and `border-width` properties.
if ( !isBorderStyleSet( oldValue ) ) {
this.borderColor = defaultBorder.color!;
this.borderWidth = defaultBorder.width!;
}
} );
return {
borderRowLabel,
borderStyleDropdown,
borderColorInput,
borderWidthInput
};
}
/**
* Creates the following form fields:
*
* * {@link #backgroundInput}.
*/
private _createBackgroundFields() {
const locale = this.locale;
const t = this.t!;
// -- Group label ---------------------------------------------
const backgroundRowLabel = new LabelView( locale );
backgroundRowLabel.text = t( 'Background' );
// -- Background color input -----------------------------------
const backgroundInputCreator = getLabeledColorInputCreator( {
colorConfig: this.options.backgroundColors,
columns: 5,
defaultColorValue: this.options.defaultTableProperties.backgroundColor,
colorPickerConfig: this.options.colorPickerConfig
} );
const backgroundInput = new LabeledFieldView( locale, backgroundInputCreator );
backgroundInput.set( {
label: t( 'Color' ),
class: 'ck-table-properties-form__background'
} );
backgroundInput.fieldView.bind( 'value' ).to( this, 'backgroundColor' );
backgroundInput.fieldView.on( 'input', () => {
this.backgroundColor = backgroundInput.fieldView.value;
} );
return {
backgroundRowLabel,
backgroundInput
};
}
/**
* Creates the following form fields:
*
* * {@link #widthInput},
* * {@link #heightInput}.
*/
private _createDimensionFields() {
const locale = this.locale;
const t = this.t!;
// -- Label ---------------------------------------------------
const dimensionsLabel = new LabelView( locale );
dimensionsLabel.text = t( 'Dimensions' );
// -- Width ---------------------------------------------------
const widthInput = new LabeledFieldView( locale, createLabeledInputText );
widthInput.set( {
label: t( 'Width' ),
class: 'ck-table-form__dimensions-row__width'
} );
widthInput.fieldView.bind( 'value' ).to( this, 'width' );
widthInput.fieldView.on( 'input', () => {
this.width = widthInput.fieldView.element!.value;
} );
// -- Operator ---------------------------------------------------
const operatorLabel = new View( locale );
operatorLabel.setTemplate( {
tag: 'span',
attributes: {
class: [
'ck-table-form__dimension-operator'
]
},
children: [
{ text: '×' }
]
} );
// -- Height ---------------------------------------------------
const heightInput = new LabeledFieldView( locale, createLabeledInputText );
heightInput.set( {
label: t( 'Height' ),
class: 'ck-table-form__dimensions-row__height'
} );
heightInput.fieldView.bind( 'value' ).to( this, 'height' );
heightInput.fieldView.on( 'input', () => {
this.height = heightInput.fieldView.element!.value;
} );
return {
dimensionsLabel,
widthInput,
operatorLabel,
heightInput
};
}
/**
* Creates the following form fields:
*
* * {@link #alignmentToolbar}.
*/
private _createAlignmentFields() {
const locale = this.locale;
const t = this.t!;
// -- Label ---------------------------------------------------
const alignmentLabel = new LabelView( locale );
alignmentLabel.text = t( 'Alignment' );
// -- Toolbar ---------------------------------------------------
const alignmentToolbar = new ToolbarView( locale! );
alignmentToolbar.set( {
isCompact: true,
ariaLabel: t( 'Table alignment toolbar' )
} );
fillToolbar( {
view: this,
icons: ALIGNMENT_ICONS,
toolbar: alignmentToolbar,
labels: this._alignmentLabels,
propertyName: 'alignment',
defaultValue: this.options.defaultTableProperties.alignment!
} );
return {
alignmentLabel,
alignmentToolbar
};
}
/**
* Creates the following form controls:
*
* * {@link #saveButtonView},
* * {@link #cancelButtonView}.
*/
private _createActionButtons(): {
saveButtonView: ButtonView;
cancelButtonView: ButtonView;
} {
const locale = this.locale;
const t = this.t!;
const saveButtonView = new ButtonView( locale );
const cancelButtonView = new ButtonView( locale );
const fieldsThatShouldValidateToSave = [
this.borderWidthInput!,
this.borderColorInput!,
this.backgroundInput!,
this.widthInput!,
this.heightInput!
];
saveButtonView.set( {
label: t( 'Save' ),
icon: icons.check,
class: 'ck-button-save',
type: 'submit',
withText: true
} );
saveButtonView.bind( 'isEnabled' ).toMany( fieldsThatShouldValidateToSave, 'errorText', ( ...errorTexts ) => {
return errorTexts.every( errorText => !errorText );
} );
cancelButtonView.set( {
label: t( 'Cancel' ),
icon: icons.cancel,
class: 'ck-button-cancel',
withText: true
} );
cancelButtonView.delegate( 'execute' ).to( this, 'cancel' );
return {
saveButtonView, cancelButtonView
};
}
/**
* Provides localized labels for {@link #alignmentToolbar} buttons.
*/
private get _alignmentLabels() {
const locale = this.locale!;
const t = this.t!;
const left = t( 'Align table to the left' );
const center = t( 'Center table' );
const right = t( 'Align table to the right' );
// Returns object with a proper order of labels.
if ( locale.uiLanguageDirection === 'rtl' ) {
return { right, center, left };
} else {
return { left, center, right };
}
}
}
function isBorderStyleSet( value: string ) {
return value !== 'none';
}