UNPKG

1.91 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module table/commands/splitcellcommand
7 */
8import { Command } from 'ckeditor5/src/core';
9/**
10 * The split cell command.
11 *
12 * The command is registered by {@link module:table/tableediting~TableEditing} as the `'splitTableCellVertically'`
13 * and `'splitTableCellHorizontally'` editor commands.
14 *
15 * You can split any cell vertically or horizontally by executing this command. For example, to split the selected table cell vertically:
16 *
17 * ```ts
18 * editor.execute( 'splitTableCellVertically' );
19 * ```
20 */
21export default class SplitCellCommand extends Command {
22 /**
23 * Creates a new `SplitCellCommand` instance.
24 *
25 * @param editor The editor on which this command will be used.
26 * @param options.direction Indicates whether the command should split cells `'horizontally'` or `'vertically'`.
27 */
28 constructor(editor, options = {}) {
29 super(editor);
30 this.direction = options.direction || 'horizontally';
31 }
32 /**
33 * @inheritDoc
34 */
35 refresh() {
36 const tableUtils = this.editor.plugins.get('TableUtils');
37 const selectedCells = tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection);
38 this.isEnabled = selectedCells.length === 1;
39 }
40 /**
41 * @inheritDoc
42 */
43 execute() {
44 const tableUtils = this.editor.plugins.get('TableUtils');
45 const tableCell = tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection)[0];
46 const isHorizontal = this.direction === 'horizontally';
47 if (isHorizontal) {
48 tableUtils.splitCellHorizontally(tableCell, 2);
49 }
50 else {
51 tableUtils.splitCellVertically(tableCell, 2);
52 }
53 }
54}