-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathinserttableview.js
289 lines (245 loc) · 6.15 KB
/
inserttableview.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
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/ui/inserttableview
*/
import { View, addKeyboardHandlingForGrid } from 'ckeditor5/src/ui';
import { KeystrokeHandler, FocusTracker, uid } from 'ckeditor5/src/utils';
import './../../theme/inserttable.css';
/**
* The table size view.
*
* It renders a 10x10 grid to choose the inserted table size.
*
* @extends module:ui/view~View
* @implements module:ui/dropdown/dropdownpanelfocusable~DropdownPanelFocusable
*/
export default class InsertTableView extends View {
/**
* @inheritDoc
*/
constructor( locale ) {
super( locale );
const bind = this.bindTemplate;
/**
* A unique id of a label element displaying the current geometry of the table.
* Used by every {@link #items item} of the view as a pointer to an accessible label.
*
* @private
* @readonly
* @member {String}
*/
this._geometryLabelId = `ck-editor__label_${ uid() }`;
/**
* A collection of table size box items.
*
* @readonly
* @member {module:ui/viewcollection~ViewCollection}
*/
this.items = this._createGridCollection();
this.keystrokes = new KeystrokeHandler();
/**
* Tracks information about the DOM focus in the grid.
*
* @readonly
* @member {module:utils/focustracker~FocusTracker}
*/
this.focusTracker = new FocusTracker();
/**
* The currently selected number of rows of the new table.
*
* @observable
* @member {Number} #rows
*/
this.set( 'rows', 0 );
/**
* The currently selected number of columns of the new table.
*
* @observable
* @member {Number} #columns
*/
this.set( 'columns', 0 );
/**
* The label text displayed under the boxes.
*
* @observable
* @member {String} #label
*/
this.bind( 'label' )
.to( this, 'columns', this, 'rows', ( columns, rows ) => `${ rows } × ${ columns }` );
this.setTemplate( {
tag: 'div',
attributes: {
class: [ 'ck' ]
},
children: [
{
tag: 'div',
attributes: {
class: [ 'ck-insert-table-dropdown__grid' ]
},
on: {
'[email protected]': bind.to( 'boxover' )
},
children: this.items
},
{
tag: 'div',
attributes: {
id: this._geometryLabelId,
class: [
'ck',
'ck-insert-table-dropdown__label'
]
},
children: [
{
text: bind.to( 'label' )
}
]
}
],
on: {
mousedown: bind.to( evt => {
evt.preventDefault();
} ),
click: bind.to( () => {
this.fire( 'execute' );
} ),
keydown: bind.to( evt => {
if ( evt.key === 'Enter' ) {
this.fire( 'execute' );
evt.preventDefault();
}
} )
}
} );
// #rows and #columns are set via changes to #focusTracker on mouse over.
this.on( 'boxover', ( evt, domEvt ) => {
const { row, column } = domEvt.target.dataset;
this.items.get( ( row - 1 ) * 10 + ( column - 1 ) ).focus();
} );
// This allows the #rows and #columns to be updated when:
// * the user navigates the grid using the keyboard,
// * the user moves the mouse over grid items.
this.focusTracker.on( 'change:focusedElement', ( evt, name, focusedElement ) => {
if ( !focusedElement ) {
return;
}
const { row, column } = focusedElement.dataset;
// As row & column indexes are zero-based transform it to number of selected rows & columns.
this.set( {
rows: parseInt( row ),
columns: parseInt( column )
} );
} );
this.on( 'change:columns', () => this._highlightGridBoxes() );
this.on( 'change:rows', () => this._highlightGridBoxes() );
}
render() {
super.render();
addKeyboardHandlingForGrid( {
keystrokeHandler: this.keystrokes,
focusTracker: this.focusTracker,
gridItems: this.items,
numberOfColumns: 10
} );
for ( const item of this.items ) {
this.focusTracker.add( item.element );
}
this.keystrokes.listenTo( this.element );
}
/**
* @inheritDoc
*/
focus() {
this.items.get( 0 ).focus();
}
/**
* @inheritDoc
*/
focusLast() {
this.items.get( 0 ).focus();
}
/**
* Highlights grid boxes depending on rows and columns selected.
*
* @private
*/
_highlightGridBoxes() {
const rows = this.rows;
const columns = this.columns;
this.items.map( ( boxView, index ) => {
// Translate box index to the row & column index.
const itemRow = Math.floor( index / 10 );
const itemColumn = index % 10;
// Grid box is highlighted when its row & column index belongs to selected number of rows & columns.
const isOn = itemRow < rows && itemColumn < columns;
boxView.set( 'isOn', isOn );
} );
}
/**
* @private
* @returns {module:ui/viewcollection~ViewCollection} A view collection containing boxes to be placed in a table grid.
*/
_createGridCollection() {
const boxes = [];
// Add grid boxes to table selection view.
for ( let index = 0; index < 100; index++ ) {
const row = Math.floor( index / 10 );
const column = index % 10;
boxes.push( new TableSizeGridBoxView( this.locale, row + 1, column + 1, this._geometryLabelId ) );
}
return this.createCollection( boxes );
}
/**
* Fired when the mouse hover over one of the {@link #items child grid boxes}.
*
* @event boxover
*/
}
/**
* A single grid box view element.
*
* This class is used to render the table size selection grid in {@link module:table/ui/inserttableview~InsertTableView}.
*
* @private
*/
class TableSizeGridBoxView extends View {
/**
* @inheritDoc
*/
constructor( locale, row, column, ariaLabelledById ) {
super( locale );
const bind = this.bindTemplate;
/**
* Controls whether the grid box view is "on".
*
* @observable
* @member {Boolean} #isOn
*/
this.set( 'isOn', false );
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-insert-table-dropdown-grid-box',
bind.if( 'isOn', 'ck-on' )
],
'data-row': row,
'data-column': column,
'tabindex': -1,
'aria-labelledby': ariaLabelledById
}
} );
}
/**
* @inheritDoc
*/
focus() {
this.element.focus();
}
}