-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
colorgridview.ts
267 lines (224 loc) · 6.55 KB
/
colorgridview.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
/**
* @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/colorgrid/colorgridview
*/
import View from '../view.js';
import ColorTileView from './colortileview.js';
import addKeyboardHandlingForGrid from '../bindings/addkeyboardhandlingforgrid.js';
import type { ButtonExecuteEvent } from '../button/button.js';
import type DropdownPanelFocusable from '../dropdown/dropdownpanelfocusable.js';
import type ViewCollection from '../viewcollection.js';
import {
FocusTracker,
KeystrokeHandler,
type CollectionAddEvent,
type CollectionRemoveEvent,
type Locale,
type ObservableChangeEvent
} from '@ckeditor/ckeditor5-utils';
import '../../theme/components/colorgrid/colorgrid.css';
/**
* A grid of {@link module:ui/colorgrid/colortileview~ColorTileView color tiles}.
*/
export default class ColorGridView extends View implements DropdownPanelFocusable {
/**
* A number of columns for the tiles grid.
*/
public readonly columns: number;
/**
* Collection of the child tile views.
*/
public readonly items: ViewCollection<ColorTileView>;
/**
* Tracks information about DOM focus in the grid.
*/
public readonly focusTracker: FocusTracker;
/**
* Instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* The color of the currently selected color tile in {@link #items}.
*
* @observable
*/
declare public selectedColor: string | undefined | null;
/**
* Creates an instance of a color grid containing {@link module:ui/colorgrid/colortileview~ColorTileView tiles}.
*
* @fires execute
* @param locale The localization services instance.
* @param options Component configuration
* @param options.colorDefinitions Array with definitions
* required to create the {@link module:ui/colorgrid/colortileview~ColorTileView tiles}.
* @param options.columns A number of columns to display the tiles.
*/
constructor(
locale?: Locale,
options?: {
colorDefinitions?: Array<ColorDefinition>;
columns?: number;
}
) {
super( locale );
const colorDefinitions = options && options.colorDefinitions ? options.colorDefinitions : [];
this.columns = options && options.columns ? options.columns : 5;
const viewStyleAttribute = {
gridTemplateColumns: `repeat( ${ this.columns }, 1fr)`
};
this.set( 'selectedColor', undefined );
this.items = this.createCollection();
this.focusTracker = new FocusTracker();
this.keystrokes = new KeystrokeHandler();
this.items.on<CollectionAddEvent<ColorTileView>>( 'add', ( evt, colorTile ) => {
colorTile.isOn = colorTile.color === this.selectedColor;
} );
colorDefinitions.forEach( color => {
const colorTile = new ColorTileView();
colorTile.set( {
color: color.color,
label: color.label,
tooltip: true,
hasBorder: color.options.hasBorder
} );
colorTile.on<ButtonExecuteEvent>( 'execute', () => {
this.fire<ColorGridViewExecuteEvent>( 'execute', {
value: color.color,
hasBorder: color.options.hasBorder,
label: color.label
} );
} );
this.items.add( colorTile );
} );
this.setTemplate( {
tag: 'div',
children: this.items,
attributes: {
class: [
'ck',
'ck-color-grid'
],
style: viewStyleAttribute
}
} );
this.on<ObservableChangeEvent<string | undefined>>( 'change:selectedColor', ( evt, name, selectedColor ) => {
for ( const item of this.items ) {
item.isOn = item.color === selectedColor;
}
} );
}
/**
* Focuses the first focusable in {@link #items}.
*/
public focus(): void {
if ( this.items.length ) {
this.items.first!.focus();
}
}
/**
* Focuses the last focusable in {@link #items}.
*/
public focusLast(): void {
if ( this.items.length ) {
this.items.last!.focus();
}
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
// Items added before rendering should be known to the #focusTracker.
for ( const item of this.items ) {
this.focusTracker.add( item.element! );
}
this.items.on<CollectionAddEvent<ColorTileView>>( 'add', ( evt, item ) => {
this.focusTracker.add( item.element! );
} );
this.items.on<CollectionRemoveEvent<ColorTileView>>( 'remove', ( evt, item ) => {
this.focusTracker.remove( item.element! );
} );
// Start listening for the keystrokes coming from #element.
this.keystrokes.listenTo( this.element! );
addKeyboardHandlingForGrid( {
keystrokeHandler: this.keystrokes,
focusTracker: this.focusTracker,
gridItems: this.items,
numberOfColumns: this.columns,
uiLanguageDirection: this.locale && this.locale.uiLanguageDirection
} );
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this.focusTracker.destroy();
this.keystrokes.destroy();
}
}
/**
* A color definition used to create a {@link module:ui/colorgrid/colortileview~ColorTileView}.
*
* ```json
* {
* color: 'hsl(0, 0%, 75%)',
* label: 'Light Grey',
* options: {
* hasBorder: true
* }
* }
* ```
*/
export interface ColorDefinition {
/**
* String representing a color.
* It is used as value of background-color style in {@link module:ui/colorgrid/colortileview~ColorTileView}.
*/
color: string;
/**
* String used as label for {@link module:ui/colorgrid/colortileview~ColorTileView}.
*/
label: string;
/**
* Additional options passed to create a {@link module:ui/colorgrid/colortileview~ColorTileView}.
*/
options: {
/**
* A flag that indicates if special a CSS class should be added
* to {@link module:ui/colorgrid/colortileview~ColorTileView}, which renders a border around it.
*/
hasBorder: boolean;
};
}
/**
* Fired when the `ColorTileView` for the picked item is executed.
*
* @eventName ~ColorGridView#execute
* @param data Additional information about the event.
*/
export type ColorGridViewExecuteEvent = {
name: 'execute';
args: [ data: ColorGridViewExecuteEventData ];
};
/**
* The data of {@link ~ColorGridViewExecuteEvent execute event}.
*/
export interface ColorGridViewExecuteEventData {
/**
* The value of the selected color ({@link module:ui/colorgrid/colorgridview~ColorDefinition#color `color.color`}).
*/
value: string;
/**
* The `hasBorder` property of the selected color
* ({@link module:ui/colorgrid/colorgridview~ColorDefinition#options `color.options.hasBorder`}).
*/
hasBorder: boolean;
/**
* The label of the selected color ({@link module:ui/colorgrid/colorgridview~ColorDefinition#label `color.label`})
*/
label: string;
}