insertrowcommand.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/insertrowcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
  10. /**
  11. * The insert row command.
  12. *
  13. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'insertTableRowBelow'` and
  14. * `'insertTableRowAbove'` editor commands.
  15. *
  16. * To insert a row below the selected cell, execute the following command:
  17. *
  18. * editor.execute( 'insertTableRowBelow' );
  19. *
  20. * To insert a row above the selected cell, execute the following command:
  21. *
  22. * editor.execute( 'insertTableRowAbove' );
  23. *
  24. * @extends module:core/command~Command
  25. */
  26. export default class InsertRowCommand extends Command {
  27. /**
  28. * Creates a new `InsertRowCommand` instance.
  29. *
  30. * @param {module:core/editor/editor~Editor} editor The editor on which this command will be used.
  31. * @param {Object} options
  32. * @param {String} [options.order="below"] The order of insertion relative to the row in which the caret is located.
  33. * Possible values: `"above"` and `"below"`.
  34. */
  35. constructor( editor, options = {} ) {
  36. super( editor );
  37. /**
  38. * The order of insertion relative to the row in which the caret is located.
  39. *
  40. * @readonly
  41. * @member {String} module:table/commands/insertrowcommand~InsertRowCommand#order
  42. */
  43. this.order = options.order || 'below';
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. refresh() {
  49. const selection = this.editor.model.document.selection;
  50. const tableParent = selection.getFirstPosition().findAncestor( 'table' );
  51. this.isEnabled = !!tableParent;
  52. }
  53. /**
  54. * Executes the command.
  55. *
  56. * Depending on the command's {@link #order} value, it inserts a row `'below'` or `'above'` the row in which selection is set.
  57. *
  58. * @fires execute
  59. */
  60. execute() {
  61. const editor = this.editor;
  62. const selection = editor.model.document.selection;
  63. const tableUtils = editor.plugins.get( 'TableUtils' );
  64. const insertAbove = this.order === 'above';
  65. const affectedTableCells = getSelectionAffectedTableCells( selection );
  66. const rowIndexes = getRowIndexes( affectedTableCells );
  67. const row = insertAbove ? rowIndexes.first : rowIndexes.last;
  68. const table = affectedTableCells[ 0 ].findAncestor( 'table' );
  69. tableUtils.insertRows( table, { at: insertAbove ? row : row + 1, copyStructureFromAbove: !insertAbove } );
  70. }
  71. }