UNPKG

1.89 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. 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 */
8import { Command } from 'ckeditor5/src/core';
9/**
10 * The select row command.
11 *
12 * The command is registered by {@link module:table/tableediting~TableEditing} as the `'selectTableRow'` editor command.
13 *
14 * To select the rows containing the selected cells, execute the command:
15 *
16 * ```ts
17 * editor.execute( 'selectTableRow' );
18 * ```
19 */
20export default class SelectRowCommand extends Command {
21 /**
22 * @inheritDoc
23 */
24 constructor(editor) {
25 super(editor);
26 // It does not affect data so should be enabled in read-only mode.
27 this.affectsData = false;
28 }
29 /**
30 * @inheritDoc
31 */
32 refresh() {
33 const tableUtils = this.editor.plugins.get('TableUtils');
34 const selectedCells = tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection);
35 this.isEnabled = selectedCells.length > 0;
36 }
37 /**
38 * @inheritDoc
39 */
40 execute() {
41 const model = this.editor.model;
42 const tableUtils = this.editor.plugins.get('TableUtils');
43 const referenceCells = tableUtils.getSelectionAffectedTableCells(model.document.selection);
44 const rowIndexes = tableUtils.getRowIndexes(referenceCells);
45 const table = referenceCells[0].findAncestor('table');
46 const rangesToSelect = [];
47 for (let rowIndex = rowIndexes.first; rowIndex <= rowIndexes.last; rowIndex++) {
48 for (const cell of table.getChild(rowIndex).getChildren()) {
49 rangesToSelect.push(model.createRangeOn(cell));
50 }
51 }
52 model.change(writer => {
53 writer.setSelection(rangesToSelect);
54 });
55 }
56}