insertrowcommand.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/insertrowcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { getParentTable } from './utils';
  10. import TableUtils from '../tableutils';
  11. /**
  12. * The insert row command.
  13. *
  14. * @extends module:core/command~Command
  15. */
  16. export default class InsertRowCommand extends Command {
  17. /**
  18. * Creates a new `InsertRowCommand` instance.
  19. *
  20. * @param {module:core/editor/editor~Editor} editor Editor on which this command will be used.
  21. * @param {Object} options
  22. * @param {String} [options.order="below"] The order of insertion relative to a row in which caret is located.
  23. * Possible values: "above" and "below".
  24. */
  25. constructor( editor, options = {} ) {
  26. super( editor );
  27. /**
  28. * The order of insertion relative to a row in which caret is located.
  29. *
  30. * @readonly
  31. * @member {String} module:table/commands/insertrowcommand~InsertRowCommand#order
  32. */
  33. this.order = options.order || 'below';
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. refresh() {
  39. const selection = this.editor.model.document.selection;
  40. const tableParent = getParentTable( selection.getFirstPosition() );
  41. this.isEnabled = !!tableParent;
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. execute() {
  47. const editor = this.editor;
  48. const selection = editor.model.document.selection;
  49. const tableUtils = editor.plugins.get( TableUtils );
  50. const tableCell = selection.getFirstPosition().parent;
  51. const table = getParentTable( selection.getFirstPosition() );
  52. const row = table.getChildIndex( tableCell.parent );
  53. const insertAt = this.order === 'below' ? row + 1 : row;
  54. tableUtils.insertRows( table, { rows: 1, at: insertAt } );
  55. }
  56. }