-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
charactergridview.ts
223 lines (192 loc) · 5.28 KB
/
charactergridview.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
/**
* @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 special-characters/ui/charactergridview
*/
import {
View,
ButtonView,
addKeyboardHandlingForGrid,
type ViewCollection
} from 'ckeditor5/src/ui.js';
import {
KeystrokeHandler,
FocusTracker,
global,
type Locale
} from 'ckeditor5/src/utils.js';
import '../../theme/charactergrid.css';
/**
* A grid of character tiles. It allows browsing special characters and selecting the character to
* be inserted into the content.
*/
export default class CharacterGridView extends View<HTMLDivElement> {
/**
* A collection of the child tile views. Each tile represents a particular character.
*/
public readonly tiles: ViewCollection<ButtonView>;
/**
* Tracks information about the DOM focus in the grid.
*/
public readonly focusTracker: FocusTracker;
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* Creates an instance of a character grid containing tiles representing special characters.
*
* @param locale The localization services instance.
*/
constructor( locale: Locale ) {
super( locale );
this.tiles = this.createCollection() as ViewCollection<ButtonView>;
this.setTemplate( {
tag: 'div',
children: [
{
tag: 'div',
attributes: {
class: [
'ck',
'ck-character-grid__tiles'
]
},
children: this.tiles
}
],
attributes: {
class: [
'ck',
'ck-character-grid'
]
}
} );
this.focusTracker = new FocusTracker();
this.keystrokes = new KeystrokeHandler();
addKeyboardHandlingForGrid( {
keystrokeHandler: this.keystrokes,
focusTracker: this.focusTracker,
gridItems: this.tiles,
numberOfColumns: () => global.window
.getComputedStyle( this.element!.firstChild as Element ) // Responsive .ck-character-grid__tiles
.getPropertyValue( 'grid-template-columns' )
.split( ' ' )
.length,
uiLanguageDirection: this.locale && this.locale.uiLanguageDirection
} );
}
/**
* Creates a new tile for the grid.
*
* @param character A human-readable character displayed as the label (e.g. "ε").
* @param name The name of the character (e.g. "greek small letter epsilon").
*/
public createTile( character: string, name: string ): ButtonView {
const tile = new ButtonView( this.locale );
tile.set( {
label: character,
withText: true,
class: 'ck-character-grid__tile'
} );
// Labels are vital for the users to understand what character they're looking at.
// For now we're using native title attribute for that, see #5817.
tile.extendTemplate( {
attributes: {
title: name
},
on: {
mouseover: tile.bindTemplate.to( 'mouseover' ),
focus: tile.bindTemplate.to( 'focus' )
}
} );
tile.on( 'mouseover', () => {
this.fire<CharacterGridViewTileHoverEvent>( 'tileHover', { name, character } );
} );
tile.on( 'focus', () => {
this.fire<CharacterGridViewTileFocusEvent>( 'tileFocus', { name, character } );
} );
tile.on( 'execute', () => {
this.fire<CharacterGridViewExecuteEvent>( 'execute', { name, character } );
} );
return tile;
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
for ( const item of this.tiles ) {
this.focusTracker.add( item.element! );
}
this.tiles.on( 'change', ( eventInfo, { added, removed } ) => {
if ( added.length > 0 ) {
for ( const item of added ) {
this.focusTracker.add( item.element );
}
}
if ( removed.length > 0 ) {
for ( const item of removed ) {
this.focusTracker.remove( item.element );
}
}
} );
this.keystrokes.listenTo( this.element! );
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this.keystrokes.destroy();
}
/**
* Focuses the first focusable in {@link ~CharacterGridView#tiles}.
*/
public focus(): void {
this.tiles.first!.focus();
}
}
/**
* Fired when any of {@link ~CharacterGridView#tiles grid tiles} is clicked.
*
* @eventName ~CharacterGridView#execute
* @param data Additional information about the event.
*/
export type CharacterGridViewExecuteEvent = {
name: 'execute';
args: [ data: CharacterGridViewEventData ];
};
/**
* Fired when a mouse or another pointing device caused the cursor to move onto any {@link ~CharacterGridView#tiles grid tile}
* (similar to the native `mouseover` DOM event).
*
* @eventName ~CharacterGridView#tileHover
* @param data Additional information about the event.
*/
export type CharacterGridViewTileHoverEvent = {
name: 'tileHover';
args: [ data: CharacterGridViewEventData ];
};
/**
* Fired when {@link ~CharacterGridView#tiles grid tile} is focused (e.g. by navigating with arrow keys).
*
* @eventName ~CharacterGridView#tileFocus
* @param data Additional information about the event.
*/
export type CharacterGridViewTileFocusEvent = {
name: 'tileFocus';
args: [ data: CharacterGridViewEventData ];
};
export interface CharacterGridViewEventData {
/**
* The name of the tile that caused the event (e.g. "greek small letter epsilon").
*/
name: string;
/**
* A human-readable character displayed as the label (e.g. "ε").
*/
character: string;
}