-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
tableui.js
354 lines (310 loc) · 9.68 KB
/
tableui.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/tableui
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import { addListToDropdown, createDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import SplitButtonView from '@ckeditor/ckeditor5-ui/src/dropdown/button/splitbuttonview';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import InsertTableView from './ui/inserttableview';
import tableIcon from './../theme/icons/table.svg';
import tableColumnIcon from './../theme/icons/table-column.svg';
import tableRowIcon from './../theme/icons/table-row.svg';
import tableMergeCellIcon from './../theme/icons/table-merge-cell.svg';
/**
* The table UI plugin. It introduces:
*
* * The `'insertTable'` dropdown,
* * The `'tableColumn'` dropdown,
* * The `'tableRow'` dropdown,
* * The `'mergeTableCells'` split button.
*
* The `'tableColumn'`, `'tableRow'` and `'mergeTableCells'` dropdowns work best with {@link module:table/tabletoolbar~TableToolbar}.
*
* @extends module:core/plugin~Plugin
*/
export default class TableUI extends Plugin {
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const t = this.editor.t;
const contentLanguageDirection = editor.locale.contentLanguageDirection;
const isContentLtr = contentLanguageDirection === 'ltr';
editor.ui.componentFactory.add( 'insertTable', locale => {
const command = editor.commands.get( 'insertTable' );
const dropdownView = createDropdown( locale );
dropdownView.bind( 'isEnabled' ).to( command );
// Decorate dropdown's button.
dropdownView.buttonView.set( {
icon: tableIcon,
label: t( 'Insert table' ),
tooltip: true
} );
let insertTableView;
dropdownView.on( 'change:isOpen', () => {
if ( insertTableView ) {
return;
}
// Prepare custom view for dropdown's panel.
insertTableView = new InsertTableView( locale );
dropdownView.panelView.children.add( insertTableView );
insertTableView.delegate( 'execute' ).to( dropdownView );
dropdownView.buttonView.on( 'open', () => {
// Reset the chooser before showing it to the user.
insertTableView.rows = 0;
insertTableView.columns = 0;
} );
dropdownView.on( 'execute', () => {
editor.execute( 'insertTable', { rows: insertTableView.rows, columns: insertTableView.columns } );
editor.editing.view.focus();
} );
} );
return dropdownView;
} );
editor.ui.componentFactory.add( 'tableColumn', locale => {
const options = [
{
type: 'switchbutton',
model: {
commandName: 'setTableColumnHeader',
label: t( 'Header column' ),
bindIsOn: true
}
},
{ type: 'separator' },
{
type: 'button',
model: {
commandName: isContentLtr ? 'insertTableColumnLeft' : 'insertTableColumnRight',
label: t( 'Insert column left' )
}
},
{
type: 'button',
model: {
commandName: isContentLtr ? 'insertTableColumnRight' : 'insertTableColumnLeft',
label: t( 'Insert column right' )
}
},
{
type: 'button',
model: {
commandName: 'removeTableColumn',
label: t( 'Delete column' )
}
},
{
type: 'button',
model: {
commandName: 'selectTableColumn',
label: t( 'Select column' )
}
}
];
return this._prepareDropdown( t( 'Column' ), tableColumnIcon, options, locale );
} );
editor.ui.componentFactory.add( 'tableRow', locale => {
const options = [
{
type: 'switchbutton',
model: {
commandName: 'setTableRowHeader',
label: t( 'Header row' ),
bindIsOn: true
}
},
{ type: 'separator' },
{
type: 'button',
model: {
commandName: 'insertTableRowBelow',
label: t( 'Insert row below' )
}
},
{
type: 'button',
model: {
commandName: 'insertTableRowAbove',
label: t( 'Insert row above' )
}
},
{
type: 'button',
model: {
commandName: 'removeTableRow',
label: t( 'Delete row' )
}
},
{
type: 'button',
model: {
commandName: 'selectTableRow',
label: t( 'Select row' )
}
}
];
return this._prepareDropdown( t( 'Row' ), tableRowIcon, options, locale );
} );
editor.ui.componentFactory.add( 'mergeTableCells', locale => {
const options = [
{
type: 'button',
model: {
commandName: 'mergeTableCellUp',
label: t( 'Merge cell up' )
}
},
{
type: 'button',
model: {
commandName: isContentLtr ? 'mergeTableCellRight' : 'mergeTableCellLeft',
label: t( 'Merge cell right' )
}
},
{
type: 'button',
model: {
commandName: 'mergeTableCellDown',
label: t( 'Merge cell down' )
}
},
{
type: 'button',
model: {
commandName: isContentLtr ? 'mergeTableCellLeft' : 'mergeTableCellRight',
label: t( 'Merge cell left' )
}
},
{ type: 'separator' },
{
type: 'button',
model: {
commandName: 'splitTableCellVertically',
label: t( 'Split cell vertically' )
}
},
{
type: 'button',
model: {
commandName: 'splitTableCellHorizontally',
label: t( 'Split cell horizontally' )
}
}
];
return this._prepareMergeSplitButtonDropdown( t( 'Merge cells' ), tableMergeCellIcon, options, locale );
} );
}
/**
* Creates a dropdown view from a set of options.
*
* @private
* @param {String} label The dropdown button label.
* @param {String} icon An icon for the dropdown button.
* @param {Array.<module:ui/dropdown/utils~ListDropdownItemDefinition>} options The list of options for the dropdown.
* @param {module:utils/locale~Locale} locale
* @returns {module:ui/dropdown/dropdownview~DropdownView}
*/
_prepareDropdown( label, icon, options, locale ) {
const editor = this.editor;
const dropdownView = createDropdown( locale );
const commands = this._fillDropdownWithListOptions( dropdownView, options );
// Decorate dropdown's button.
dropdownView.buttonView.set( {
label,
icon,
tooltip: true
} );
// Make dropdown button disabled when all options are disabled.
dropdownView.bind( 'isEnabled' ).toMany( commands, 'isEnabled', ( ...areEnabled ) => {
return areEnabled.some( isEnabled => isEnabled );
} );
this.listenTo( dropdownView, 'execute', evt => {
editor.execute( evt.source.commandName );
editor.editing.view.focus();
} );
return dropdownView;
}
/**
* Creates a dropdown view with a {@link module:ui/dropdown/button/splitbuttonview~SplitButtonView} for
* merge (and split)–related commands.
*
* @private
* @param {String} label The dropdown button label.
* @param {String} icon An icon for the dropdown button.
* @param {Array.<module:ui/dropdown/utils~ListDropdownItemDefinition>} options The list of options for the dropdown.
* @param {module:utils/locale~Locale} locale
* @returns {module:ui/dropdown/dropdownview~DropdownView}
*/
_prepareMergeSplitButtonDropdown( label, icon, options, locale ) {
const editor = this.editor;
const dropdownView = createDropdown( locale, SplitButtonView );
const mergeCommandName = 'mergeTableCells';
this._fillDropdownWithListOptions( dropdownView, options );
dropdownView.buttonView.set( {
label,
icon,
tooltip: true,
isEnabled: true
} );
// Merge selected table cells when the main part of the split button is clicked.
this.listenTo( dropdownView.buttonView, 'execute', () => {
editor.execute( mergeCommandName );
editor.editing.view.focus();
} );
// Execute commands for events coming from the list in the dropdown panel.
this.listenTo( dropdownView, 'execute', evt => {
editor.execute( evt.source.commandName );
editor.editing.view.focus();
} );
return dropdownView;
}
/**
* Injects a {@link module:ui/list/listview~ListView} into the passed dropdown with buttons
* which execute editor commands as configured in passed options.
*
* @private
* @param {module:ui/dropdown/dropdownview~DropdownView} dropdownView
* @param {Array.<module:ui/dropdown/utils~ListDropdownItemDefinition>} options The list of options for the dropdown.
* @returns {Array.<module:core/command~Command>} Commands the list options are interacting with.
*/
_fillDropdownWithListOptions( dropdownView, options ) {
const editor = this.editor;
const commands = [];
const itemDefinitions = new Collection();
for ( const option of options ) {
addListOption( option, editor, commands, itemDefinitions );
}
addListToDropdown( dropdownView, itemDefinitions, editor.ui.componentFactory );
return commands;
}
}
// Adds an option to a list view.
//
// @param {module:table/tableui~DropdownOption} option A configuration option.
// @param {module:core/editor/editor~Editor} editor
// @param {Array.<module:core/command~Command>} commands The list of commands to update.
// @param {Iterable.<module:ui/dropdown/utils~ListDropdownItemDefinition>} itemDefinitions
// A collection of dropdown items to update with the given option.
function addListOption( option, editor, commands, itemDefinitions ) {
const model = option.model = new Model( option.model );
const { commandName, bindIsOn } = option.model;
if ( option.type === 'button' || option.type === 'switchbutton' ) {
const command = editor.commands.get( commandName );
commands.push( command );
model.set( { commandName } );
model.bind( 'isEnabled' ).to( command );
if ( bindIsOn ) {
model.bind( 'isOn' ).to( command, 'value' );
}
}
model.set( {
withText: true
} );
itemDefinitions.add( option );
}