| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /**
- * @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/insertcolumncommand
- */
- import Command from '@ckeditor/ckeditor5-core/src/command';
- import { getRangeContainedElement, findAncestor } from './utils';
- /**
- * The insert column command.
- *
- * The command is registered by {@link module:table/tableediting~TableEditing} as the `'insertTableColumnLeft'` and
- * `'insertTableColumnRight'` editor commands.
- *
- * To insert a column to the left of the selected cell, execute the following command:
- *
- * editor.execute( 'insertTableColumnLeft' );
- *
- * To insert a column to the right of the selected cell, execute the following command:
- *
- * editor.execute( 'insertTableColumnRight' );
- *
- * @extends module:core/command~Command
- */
- export default class InsertColumnCommand extends Command {
- /**
- * Creates a new `InsertColumnCommand` instance.
- *
- * @param {module:core/editor/editor~Editor} editor An editor on which this command will be used.
- * @param {Object} options
- * @param {String} [options.order="right"] The order of insertion relative to the column in which the caret is located.
- * Possible values: `"left"` and `"right"`.
- */
- constructor( editor, options = {} ) {
- super( editor );
- /**
- * The order of insertion relative to the column in which the caret is located.
- *
- * @readonly
- * @member {String} module:table/commands/insertcolumncommand~InsertColumnCommand#order
- */
- this.order = options.order || 'right';
- }
- /**
- * @inheritDoc
- */
- refresh() {
- const selection = this.editor.model.document.selection;
- const tableParent = findAncestor( 'table', selection.getFirstPosition() );
- this.isEnabled = !!tableParent;
- }
- /**
- * Executes the command.
- *
- * Depending on the command's {@link #order} value, it inserts a column to the `'left'` or `'right'` of the column
- * in which the selection is set.
- *
- * @fires execute
- */
- execute() {
- const editor = this.editor;
- const selection = editor.model.document.selection;
- const tableUtils = editor.plugins.get( 'TableUtils' );
- const insertBefore = this.order === 'left';
- const referencePosition = insertBefore ? selection.getFirstPosition() : selection.getLastPosition();
- const referenceRange = insertBefore ? selection.getFirstRange() : selection.getLastRange();
- const tableCell = getRangeContainedElement( referenceRange ) || findAncestor( 'tableCell', referencePosition );
- const table = tableCell.parent.parent;
- const { column } = tableUtils.getCellLocation( tableCell );
- tableUtils.insertColumns( table, { columns: 1, at: insertBefore ? column : column + 1 } );
- }
- }
|