/
splitcellcommand.js
68 lines (60 loc) · 1.97 KB
/
splitcellcommand.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
/**
* @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/commands/splitcellcommand
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import { getSelectionAffectedTableCells } from '../utils/selection';
/**
* The split cell command.
*
* The command is registered by {@link module:table/tableediting~TableEditing} as the `'splitTableCellVertically'`
* and `'splitTableCellHorizontally'` editor commands.
*
* You can split any cell vertically or horizontally by executing this command. For example, to split the selected table cell vertically:
*
* editor.execute( 'splitTableCellVertically' );
*
* @extends module:core/command~Command
*/
export default class SplitCellCommand extends Command {
/**
* Creates a new `SplitCellCommand` instance.
*
* @param {module:core/editor/editor~Editor} editor The editor on which this command will be used.
* @param {Object} options
* @param {String} options.direction Indicates whether the command should split cells `'horizontally'` or `'vertically'`.
*/
constructor( editor, options = {} ) {
super( editor );
/**
* The direction that indicates which cell will be split.
*
* @readonly
* @member {String} #direction
*/
this.direction = options.direction || 'horizontally';
}
/**
* @inheritDoc
*/
refresh() {
const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
this.isEnabled = selectedCells.length === 1;
}
/**
* @inheritDoc
*/
execute() {
const tableCell = getSelectionAffectedTableCells( this.editor.model.document.selection )[ 0 ];
const isHorizontal = this.direction === 'horizontally';
const tableUtils = this.editor.plugins.get( 'TableUtils' );
if ( isHorizontal ) {
tableUtils.splitCellHorizontally( tableCell, 2 );
} else {
tableUtils.splitCellVertically( tableCell, 2 );
}
}
}