-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
colorgridsfragmentview.ts
464 lines (393 loc) · 12.8 KB
/
colorgridsfragmentview.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
/**
* @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 ui/colorselector/colorgridsfragmentview
*/
import View from '../view.js';
import ButtonView from '../button/buttonview.js';
import ColorGridView, { type ColorDefinition } from '../colorgrid/colorgridview.js';
import ColorTileView from '../colorgrid/colortileview.js';
import LabelView from '../label/labelview.js';
import Template from '../template.js';
import DocumentColorCollection from './documentcolorcollection.js';
import type { Model } from '@ckeditor/ckeditor5-engine';
import type { FocusTracker, Locale } from '@ckeditor/ckeditor5-utils';
import type ViewCollection from '../viewcollection.js';
import type { FocusableView } from '../focuscycler.js';
import type { ColorSelectorExecuteEvent, ColorSelectorColorPickerShowEvent } from './colorselectorview.js';
import { icons } from '@ckeditor/ckeditor5-core';
const { eraser: removeButtonIcon, colorPalette: colorPaletteIcon } = icons;
/**
* One of the fragments of {@link module:ui/colorselector/colorselectorview~ColorSelectorView}.
*
* It provides a UI that allows users to select colors from the a predefined set and from existing document colors.
*
* It consists of the following sub–components:
*
* * A "Remove color" button,
* * A static {@link module:ui/colorgrid/colorgridview~ColorGridView} of colors defined in the configuration,
* * A dynamic {@link module:ui/colorgrid/colorgridview~ColorGridView} of colors used in the document.
* * If color picker is configured, the "Color Picker" button is visible too.
*/
export default class ColorGridsFragmentView extends View {
/**
* A collection of the children of the table.
*/
public readonly items: ViewCollection;
/**
* An array with objects representing colors to be displayed in the grid.
*/
public colorDefinitions: Array<ColorDefinition>;
/**
* Tracks information about the DOM focus in the list.
*/
public readonly focusTracker: FocusTracker;
/**
* The number of columns in the color grid.
*/
public columns: number;
/**
* Preserves the reference to {@link module:ui/colorselector/documentcolorcollection~DocumentColorCollection} used to collect
* definitions that store the document colors.
*
* @readonly
*/
public documentColors: DocumentColorCollection;
/**
* The maximum number of colors in the document colors section.
* If it equals 0, the document colors section is not added.
*
* @readonly
*/
public documentColorsCount?: number;
/**
* Keeps the value of the command associated with the table for the current selection.
*/
declare public selectedColor: string;
/**
* Preserves the reference to {@link module:ui/colorgrid/colorgridview~ColorGridView} used to create
* the default (static) color set.
*
* The property is loaded once the the parent dropdown is opened the first time.
*
* @readonly
*/
public staticColorsGrid: ColorGridView | undefined;
/**
* Preserves the reference to {@link module:ui/colorgrid/colorgridview~ColorGridView} used to create
* the document colors. It remains undefined if the document colors feature is disabled.
*
* The property is loaded once the the parent dropdown is opened the first time.
*
* @readonly
*/
public documentColorsGrid: ColorGridView | undefined;
/**
* The "Color picker" button view.
*/
public colorPickerButtonView?: ButtonView;
/**
* The "Remove color" button view.
*/
public removeColorButtonView: ButtonView;
/**
* The property which is responsible for is component visible or not.
*/
declare public isVisible: boolean;
/**
* A collection of views that can be focused in the view.
*
* @readonly
*/
protected _focusables: ViewCollection<FocusableView>;
/**
* Document color section's label.
*
* @readonly
*/
private _documentColorsLabel?: string;
/**
* The label of the button responsible for removing color attributes.
*/
private _removeButtonLabel: string;
/**
* The label of the button responsible for switching to the color picker component.
*/
private _colorPickerLabel: string;
/**
* Creates an instance of the view.
*
* @param locale The localization services instance.
* @param colors An array with definitions of colors to be displayed in the table.
* @param columns The number of columns in the color grid.
* @param removeButtonLabel The label of the button responsible for removing the color.
* @param colorPickerLabel The label of the button responsible for color picker appearing.
* @param documentColorsLabel The label for the section with the document colors.
* @param documentColorsCount The number of colors in the document colors section inside the color dropdown.
* @param focusTracker Tracks information about the DOM focus in the list.
* @param focusables A collection of views that can be focused in the view.
*/
constructor(
locale: Locale,
{
colors, columns, removeButtonLabel, documentColorsLabel, documentColorsCount,
colorPickerLabel, focusTracker, focusables
}: {
colors: Array<ColorDefinition>;
columns: number;
removeButtonLabel: string;
colorPickerLabel: string;
documentColorsLabel?: string;
documentColorsCount?: number;
focusTracker: FocusTracker;
focusables: ViewCollection<FocusableView>;
}
) {
super( locale );
const bind = this.bindTemplate;
this.set( 'isVisible', true );
this.focusTracker = focusTracker;
this.items = this.createCollection();
this.colorDefinitions = colors;
this.columns = columns;
this.documentColors = new DocumentColorCollection();
this.documentColorsCount = documentColorsCount;
this._focusables = focusables;
this._removeButtonLabel = removeButtonLabel;
this._colorPickerLabel = colorPickerLabel;
this._documentColorsLabel = documentColorsLabel;
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck-color-grids-fragment',
bind.if( 'isVisible', 'ck-hidden', value => !value )
]
},
children: this.items
} );
this.removeColorButtonView = this._createRemoveColorButton();
this.items.add( this.removeColorButtonView );
}
/**
* Scans through the editor model and searches for text node attributes with the given attribute name.
* Found entries are set as document colors.
*
* All the previously stored document colors will be lost in the process.
*
* @param model The model used as a source to obtain the document colors.
* @param attributeName Determines the name of the related model's attribute for a given dropdown.
*/
public updateDocumentColors( model: Model, attributeName: string ): void {
const document = model.document;
const maxCount = this.documentColorsCount;
this.documentColors.clear();
for ( const root of document.getRoots() ) {
const range = model.createRangeIn( root );
for ( const node of range.getItems() ) {
if ( node.is( '$textProxy' ) && node.hasAttribute( attributeName ) ) {
this._addColorToDocumentColors( node.getAttribute( attributeName ) as string );
if ( this.documentColors.length >= maxCount! ) {
return;
}
}
}
}
}
/**
* Refreshes the state of the selected color in one or both {@link module:ui/colorgrid/colorgridview~ColorGridView}s
* available in the {@link module:ui/colorselector/colorselectorview~ColorSelectorView}. It guarantees that the selection will
* occur only in one of them.
*/
public updateSelectedColors(): void {
const documentColorsGrid = this.documentColorsGrid;
const staticColorsGrid = this.staticColorsGrid!;
const selectedColor = this.selectedColor;
staticColorsGrid.selectedColor = selectedColor;
if ( documentColorsGrid ) {
documentColorsGrid.selectedColor = selectedColor;
}
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this.staticColorsGrid = this._createStaticColorsGrid();
this.items.add( this.staticColorsGrid );
if ( this.documentColorsCount ) {
// Create a label for document colors.
const bind = Template.bind( this.documentColors, this.documentColors );
const label = new LabelView( this.locale );
label.text = this._documentColorsLabel;
label.extendTemplate( {
attributes: {
class: [
'ck',
'ck-color-grid__label',
bind.if( 'isEmpty', 'ck-hidden' )
]
}
} );
this.items.add( label );
this.documentColorsGrid = this._createDocumentColorsGrid();
this.items.add( this.documentColorsGrid );
}
this._createColorPickerButton();
this._addColorSelectorElementsToFocusTracker();
}
/**
* Focuses the component.
*/
public focus(): void {
this.removeColorButtonView.focus();
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
}
/**
* Handles displaying the color picker button (if it was previously created) and making it focusable.
*/
public addColorPickerButton(): void {
if ( this.colorPickerButtonView ) {
this.items.add( this.colorPickerButtonView );
this.focusTracker.add( this.colorPickerButtonView.element! );
this._focusables.add( this.colorPickerButtonView );
}
}
/**
* Adds color selector elements to focus tracker.
*/
private _addColorSelectorElementsToFocusTracker(): void {
this.focusTracker.add( this.removeColorButtonView.element! );
this._focusables.add( this.removeColorButtonView );
if ( this.staticColorsGrid ) {
this.focusTracker.add( this.staticColorsGrid.element! );
this._focusables.add( this.staticColorsGrid );
}
if ( this.documentColorsGrid ) {
this.focusTracker.add( this.documentColorsGrid.element! );
this._focusables.add( this.documentColorsGrid );
}
}
/**
* Creates the button responsible for displaying the color picker component.
*/
private _createColorPickerButton(): void {
this.colorPickerButtonView = new ButtonView();
this.colorPickerButtonView.set( {
label: this._colorPickerLabel,
withText: true,
icon: colorPaletteIcon,
class: 'ck-color-selector__color-picker'
} );
this.colorPickerButtonView.on( 'execute', () => {
this.fire<ColorSelectorColorPickerShowEvent>( 'colorPicker:show' );
} );
}
/**
* Adds the remove color button as a child of the current view.
*/
private _createRemoveColorButton(): ButtonView {
const buttonView = new ButtonView();
buttonView.set( {
withText: true,
icon: removeButtonIcon,
label: this._removeButtonLabel
} );
buttonView.class = 'ck-color-selector__remove-color';
buttonView.on( 'execute', () => {
this.fire( 'execute', {
value: null,
source: 'removeColorButton'
} );
} );
buttonView.render();
return buttonView;
}
/**
* Creates a static color grid based on the editor configuration.
*/
private _createStaticColorsGrid(): ColorGridView {
const colorGrid = new ColorGridView( this.locale, {
colorDefinitions: this.colorDefinitions,
columns: this.columns
} );
colorGrid.on( 'execute', ( evt, data ) => {
this.fire<ColorSelectorExecuteEvent>( 'execute', {
value: data.value,
source: 'staticColorsGrid'
} );
} );
return colorGrid;
}
/**
* Creates the document colors section view and binds it to {@link #documentColors}.
*/
private _createDocumentColorsGrid(): ColorGridView {
const bind = Template.bind( this.documentColors, this.documentColors );
const documentColorsGrid = new ColorGridView( this.locale, {
columns: this.columns
} );
documentColorsGrid.extendTemplate( {
attributes: {
class: bind.if( 'isEmpty', 'ck-hidden' )
}
} );
documentColorsGrid.items.bindTo( this.documentColors ).using(
colorObj => {
const colorTile = new ColorTileView();
colorTile.set( {
color: colorObj.color,
hasBorder: colorObj.options && colorObj.options.hasBorder
} );
if ( colorObj.label ) {
colorTile.set( {
label: colorObj.label,
tooltip: true
} );
}
colorTile.on( 'execute', () => {
this.fire<ColorSelectorExecuteEvent>( 'execute', {
value: colorObj.color,
source: 'documentColorsGrid'
} );
} );
return colorTile;
}
);
// Selected color should be cleared when document colors became empty.
this.documentColors.on( 'change:isEmpty', ( evt, name, val ) => {
if ( val ) {
documentColorsGrid.selectedColor = null;
}
} );
return documentColorsGrid;
}
/**
* Adds a given color to the document colors list. If possible, the method will attempt to use
* data from the {@link #colorDefinitions} (label, color options).
*
* @param color A string that stores the value of the recently applied color.
*/
private _addColorToDocumentColors( color: string ): void {
const predefinedColor = this.colorDefinitions
.find( definition => definition.color === color );
if ( !predefinedColor ) {
this.documentColors.add( {
color,
label: color,
options: {
hasBorder: false
}
} );
} else {
this.documentColors.add( Object.assign( {}, predefinedColor ) );
}
}
}