This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
colortableview.js
395 lines (342 loc) · 10.9 KB
/
colortableview.js
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
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module font/ui/colortableview
*/
import View from '@ckeditor/ckeditor5-ui/src/view';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import ColorTileView from '@ckeditor/ckeditor5-ui/src/colorgrid/colortileview';
import ColorGridView from '@ckeditor/ckeditor5-ui/src/colorgrid/colorgridview';
import LabelView from '@ckeditor/ckeditor5-ui/src/label/labelview';
import DocumentColorCollection from '../documentcolorcollection';
import Template from '@ckeditor/ckeditor5-ui/src/template';
import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
import removeButtonIcon from '@ckeditor/ckeditor5-core/theme/icons/eraser.svg';
import '../../theme/fontcolor.css';
/**
* A class which represents a view with the following sub–components:
*
* * A remove color button,
* * A static {@link module:ui/colorgrid/colorgrid~ColorGridView} of colors defined in the configuration,
* * A dynamic {@link module:ui/colorgrid/colorgrid~ColorGridView} of colors used in the document.
*
* @extends module:ui/view~View
*/
export default class ColorTableView extends View {
/**
* Creates a view to be inserted as a child of {@link module:ui/dropdown/dropdownview~DropdownView}.
*
* @param {module:utils/locale~Locale} [locale] The localization services instance.
* @param {Object} config The configuration object.
* @param {Array.<module:ui/colorgrid/colorgrid~ColorDefinition>} config.colors An array with definitions of colors to
* be displayed in the table.
* @param {Number} config.columns The number of columns in the color grid.
* @param {String} config.removeButtonLabel The label of the button responsible for removing the color.
* @param {String} config.documentColorsLabel The label for the section with the document colors.
* @param {String} config.documentColorsCount The number of colors in the document colors section inside the color dropdown.
*/
constructor( locale, { colors, columns, removeButtonLabel, documentColorsLabel, documentColorsCount } ) {
super( locale );
/**
* A collection of the children of the table.
*
* @readonly
* @member {module:ui/viewcollection~ViewCollection}
*/
this.items = this.createCollection();
/**
* An array with objects representing colors to be displayed in the grid.
*
* @type {Arrray.<module:ui/colorgrid/colorgrid~ColorDefinition>}
*/
this.colorDefinitions = colors;
/**
* Tracks information about the DOM focus in the list.
*
* @readonly
* @member {module:utils/focustracker~FocusTracker}
*/
this.focusTracker = new FocusTracker();
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*
* @readonly
* @member {module:utils/keystrokehandler~KeystrokeHandler}
*/
this.keystrokes = new KeystrokeHandler();
/**
* Keeps the value of the command associated with the table for the current selection.
*
* @type {String}
*/
this.set( 'selectedColor' );
/**
* The label of the button responsible for removing color attributes.
*
* @type {String}
*/
this.removeButtonLabel = removeButtonLabel;
/**
* The number of columns in the color grid.
*
* @type {Number}
*/
this.columns = columns;
/**
* A collection of definitions that store the document colors.
*
* @readonly
* @member {module:font/documentcolorcollection~DocumentColorCollection}
*/
this.documentColors = new DocumentColorCollection();
/**
* The maximum number of colors in the document colors section.
* If it equals 0, the document colors section is not added.
*
* @readonly
* @type {Number}
*/
this.documentColorsCount = documentColorsCount;
/**
* Preserves the reference to {@link module:ui/colorgrid/colorgrid~ColorGridView} used to create
* the default (static) color set.
*
* @readonly
* @member {module:ui/colorgrid/colorgrid~ColorGridView}
*/
this.staticColorsGrid = this._createStaticColorsGrid();
/**
* Preserves the reference to {@link module:ui/colorgrid/colorgrid~ColorGridView} used to create
* the document colors. It remains undefined if the document colors feature is disabled.
*
* @readonly
* @member {module:ui/colorgrid/colorgrid~ColorGridView}
*/
this.documentColorGrid;
/**
* Helps cycling over focusable {@link #items} in the list.
*
* @readonly
* @protected
* @member {module:ui/focuscycler~FocusCycler}
*/
this._focusCycler = new FocusCycler( {
focusables: this.items,
focusTracker: this.focusTracker,
keystrokeHandler: this.keystrokes,
actions: {
// Navigate list items backwards using the Arrow Up key.
focusPrevious: 'arrowup',
// Navigate list items forwards using the Arrow Down key.
focusNext: 'arrowdown',
}
} );
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-color-table'
]
},
children: this.items
} );
this.items.add( this._removeColorButton() );
this.items.add( this.staticColorsGrid );
if ( documentColorsCount ) {
// Create a label for document colors.
const bind = Template.bind( this.documentColors, this.documentColors );
const label = new LabelView( this.locale );
label.text = 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 );
}
}
/**
* 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 {module:engine/model/model~Model} model The model used as a source to obtain the document colors.
* @param {String} attributeName Determines the name of the related model's attribute for a given dropdown.
*/
updateDocumentColors( model, attributeName ) {
const document = model.document;
const maxCount = this.documentColorsCount;
this.documentColors.clear();
for ( const rootName of document.getRootNames() ) {
const root = document.getRoot( rootName );
const range = model.createRangeIn( root );
for ( const node of range.getItems() ) {
if ( node.is( 'textProxy' ) && node.hasAttribute( attributeName ) ) {
this._addColorToDocumentColors( node.getAttribute( attributeName ) );
if ( this.documentColors.length >= maxCount ) {
return;
}
}
}
}
}
/**
* Refreshes the state of the selected color in one or both {@link module:ui/colorgrid/colorgrid~ColorGridView}s
* available in the {@link module:font/ui/colortableview~ColorTableView}. It guarantees that the selection will occur only in one
* of them.
*/
updateSelectedColors() {
const documentColorsGrid = this.documentColorsGrid;
const staticColorsGrid = this.staticColorsGrid;
const selectedColor = this.selectedColor;
if ( !this.documentColors.isEmpty ) {
if ( this.documentColors.hasColor( selectedColor ) ) {
staticColorsGrid.selectedColor = null;
documentColorsGrid.selectedColor = selectedColor;
} else {
staticColorsGrid.selectedColor = selectedColor;
documentColorsGrid.selectedColor = null;
}
} else {
staticColorsGrid.selectedColor = selectedColor;
}
}
/**
* @inheritDoc
*/
render() {
super.render();
// Items added before rendering should be known to the #focusTracker.
for ( const item of this.items ) {
this.focusTracker.add( item.element );
}
// Start listening for the keystrokes coming from #element.
this.keystrokes.listenTo( this.element );
}
/**
* Focuses the first focusable element in {@link #items}.
*/
focus() {
this._focusCycler.focusFirst();
}
/**
* Focuses the last focusable element in {@link #items}.
*/
focusLast() {
this._focusCycler.focusLast();
}
/**
* Adds the remove color button as a child of the current view.
*
* @private
* @returns {module:ui/button/buttonview~ButtonView}
*/
_removeColorButton() {
const buttonView = new ButtonView();
buttonView.set( {
withText: true,
icon: removeButtonIcon,
tooltip: true,
label: this.removeButtonLabel
} );
buttonView.class = 'ck-color-table__remove-color';
buttonView.on( 'execute', () => {
this.fire( 'execute', { value: null } );
} );
return buttonView;
}
/**
* Creates a static color table grid based on the editor configuration.
*
* @private
* @returns {module:ui/colorgrid/colorgrid~ColorGridView}
*/
_createStaticColorsGrid() {
const colorGrid = new ColorGridView( this.locale, {
colorDefinitions: this.colorDefinitions,
columns: this.columns
} );
colorGrid.delegate( 'execute' ).to( this );
return colorGrid;
}
/**
* Creates the document colors section view and binds it to {@link #documentColors}.
*
* @private
* @returns {module:ui/colorgrid/colorgrid~ColorGridView}
*/
_createDocumentColorsGrid() {
const bind = Template.bind( this.documentColors, this.documentColors );
const documentColorsGrid = new ColorGridView( this.locale, {
columns: this.columns
} );
documentColorsGrid.delegate( 'execute' ).to( this );
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( 'execute', {
value: colorObj.color
} );
} );
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).
*
* @private
* @param {String} color A string that stores the value of the recently applied color.
*/
_addColorToDocumentColors( color ) {
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 ) );
}
}
}