insertcolumncommand.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/commands/insertcolumncommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils/selection';
  10. import { findAncestor } from '../utils/common';
  11. /**
  12. * The insert column command.
  13. *
  14. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'insertTableColumnLeft'` and
  15. * `'insertTableColumnRight'` editor commands.
  16. *
  17. * To insert a column to the left of the selected cell, execute the following command:
  18. *
  19. * editor.execute( 'insertTableColumnLeft' );
  20. *
  21. * To insert a column to the right of the selected cell, execute the following command:
  22. *
  23. * editor.execute( 'insertTableColumnRight' );
  24. *
  25. * @extends module:core/command~Command
  26. */
  27. export default class InsertColumnCommand extends Command {
  28. /**
  29. * Creates a new `InsertColumnCommand` instance.
  30. *
  31. * @param {module:core/editor/editor~Editor} editor An editor on which this command will be used.
  32. * @param {Object} options
  33. * @param {String} [options.order="right"] The order of insertion relative to the column in which the caret is located.
  34. * Possible values: `"left"` and `"right"`.
  35. */
  36. constructor( editor, options = {} ) {
  37. super( editor );
  38. /**
  39. * The order of insertion relative to the column in which the caret is located.
  40. *
  41. * @readonly
  42. * @member {String} module:table/commands/insertcolumncommand~InsertColumnCommand#order
  43. */
  44. this.order = options.order || 'right';
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. refresh() {
  50. const selection = this.editor.model.document.selection;
  51. const tableParent = findAncestor( 'table', selection.getFirstPosition() );
  52. this.isEnabled = !!tableParent;
  53. }
  54. /**
  55. * Executes the command.
  56. *
  57. * Depending on the command's {@link #order} value, it inserts a column to the `'left'` or `'right'` of the column
  58. * in which the selection is set.
  59. *
  60. * @fires execute
  61. */
  62. execute() {
  63. const editor = this.editor;
  64. const selection = editor.model.document.selection;
  65. const tableUtils = editor.plugins.get( 'TableUtils' );
  66. const insertBefore = this.order === 'left';
  67. const affectedTableCells = getSelectionAffectedTableCells( selection );
  68. const columnIndexes = getColumnIndexes( affectedTableCells );
  69. const column = insertBefore ? columnIndexes.first : columnIndexes.last;
  70. const table = findAncestor( 'table', affectedTableCells[ 0 ] );
  71. tableUtils.insertColumns( table, { columns: 1, at: insertBefore ? column : column + 1 } );
  72. }
  73. }