selectrowcommand.js 1.6 KB

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