splitcellcommand.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/commands/splitcellcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import TableUtils from '../tableutils';
  10. /**
  11. * The split cell command.
  12. *
  13. * The command is registered by {@link module:table/tableediting~TableEditing} as `'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 model = this.editor.model;
  45. const doc = model.document;
  46. const element = doc.selection.getFirstPosition().parent;
  47. this.isEnabled = element.is( 'tableCell' );
  48. }
  49. /**
  50. * @inheritDoc
  51. */
  52. execute() {
  53. const model = this.editor.model;
  54. const document = model.document;
  55. const selection = document.selection;
  56. const firstPosition = selection.getFirstPosition();
  57. const tableCell = firstPosition.parent;
  58. const isHorizontally = this.direction === 'horizontally';
  59. const tableUtils = this.editor.plugins.get( TableUtils );
  60. if ( isHorizontally ) {
  61. tableUtils.splitCellHorizontally( tableCell, 2 );
  62. } else {
  63. tableUtils.splitCellVertically( tableCell, 2 );
  64. }
  65. }
  66. }