selectrowcommand.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/selectrowcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
  10. /**
  11. * The select row command.
  12. *
  13. * The command is registered by {@link module:table/tableediting~TableEditing} as the `'selectTableRow'` editor command.
  14. *
  15. * To select the rows containing the selected cells, execute the command:
  16. *
  17. * editor.execute( 'selectTableRow' );
  18. *
  19. * @extends module:core/command~Command
  20. */
  21. export default class SelectRowCommand extends Command {
  22. /**
  23. * @inheritDoc
  24. */
  25. refresh() {
  26. const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
  27. this.isEnabled = selectedCells.length > 0;
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. execute() {
  33. const model = this.editor.model;
  34. const referenceCells = getSelectionAffectedTableCells( model.document.selection );
  35. const rowIndexes = getRowIndexes( referenceCells );
  36. const table = referenceCells[ 0 ].findAncestor( 'table' );
  37. const rangesToSelect = [];
  38. for ( let rowIndex = rowIndexes.first; rowIndex <= rowIndexes.last; rowIndex++ ) {
  39. for ( const cell of table.getChild( rowIndex ).getChildren() ) {
  40. rangesToSelect.push( model.createRangeOn( cell ) );
  41. }
  42. }
  43. model.change( writer => {
  44. writer.setSelection( rangesToSelect );
  45. } );
  46. }
  47. }