splitcellcommand.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/splitcellcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { getSelectionAffectedTableCells } from '../utils/selection';
  10. /**
  11. * The split cell command.
  12. *
  13. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'splitTableCellVertically'`
  14. * and `'splitTableCellHorizontally'` editor commands.
  15. *
  16. * You can split any cell vertically or horizontally by executing this command. For example, to split the selected table cell vertically:
  17. *
  18. * editor.execute( 'splitTableCellVertically' );
  19. *
  20. * @extends module:core/command~Command
  21. */
  22. export default class SplitCellCommand extends Command {
  23. /**
  24. * Creates a new `SplitCellCommand` instance.
  25. *
  26. * @param {module:core/editor/editor~Editor} editor The editor on which this command will be used.
  27. * @param {Object} options
  28. * @param {String} options.direction Indicates whether the command should split cells `'horizontally'` or `'vertically'`.
  29. */
  30. constructor( editor, options = {} ) {
  31. super( editor );
  32. /**
  33. * The direction that indicates which cell will be split.
  34. *
  35. * @readonly
  36. * @member {String} #direction
  37. */
  38. this.direction = options.direction || 'horizontally';
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. refresh() {
  44. const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
  45. this.isEnabled = selectedCells.length === 1;
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. execute() {
  51. const tableCell = getSelectionAffectedTableCells( this.editor.model.document.selection )[ 0 ];
  52. const isHorizontal = this.direction === 'horizontally';
  53. const tableUtils = this.editor.plugins.get( 'TableUtils' );
  54. if ( isHorizontal ) {
  55. tableUtils.splitCellHorizontally( tableCell, 2 );
  56. } else {
  57. tableUtils.splitCellVertically( tableCell, 2 );
  58. }
  59. }
  60. }