1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { Command } from 'ckeditor5/src/core';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export default class RemoveRowCommand extends Command {
|
21 | |
22 |
|
23 |
|
24 | refresh() {
|
25 | const tableUtils = this.editor.plugins.get('TableUtils');
|
26 | const selectedCells = tableUtils.getSelectionAffectedTableCells(this.editor.model.document.selection);
|
27 | const firstCell = selectedCells[0];
|
28 | if (firstCell) {
|
29 | const table = firstCell.findAncestor('table');
|
30 | const tableRowCount = tableUtils.getRows(table);
|
31 | const lastRowIndex = tableRowCount - 1;
|
32 | const selectedRowIndexes = tableUtils.getRowIndexes(selectedCells);
|
33 | const areAllRowsSelected = selectedRowIndexes.first === 0 && selectedRowIndexes.last === lastRowIndex;
|
34 |
|
35 | this.isEnabled = !areAllRowsSelected;
|
36 | }
|
37 | else {
|
38 | this.isEnabled = false;
|
39 | }
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | execute() {
|
45 | const model = this.editor.model;
|
46 | const tableUtils = this.editor.plugins.get('TableUtils');
|
47 | const referenceCells = tableUtils.getSelectionAffectedTableCells(model.document.selection);
|
48 | const removedRowIndexes = tableUtils.getRowIndexes(referenceCells);
|
49 | const firstCell = referenceCells[0];
|
50 | const table = firstCell.findAncestor('table');
|
51 | const columnIndexToFocus = tableUtils.getCellLocation(firstCell).column;
|
52 | model.change(writer => {
|
53 | const rowsToRemove = removedRowIndexes.last - removedRowIndexes.first + 1;
|
54 | tableUtils.removeRows(table, {
|
55 | at: removedRowIndexes.first,
|
56 | rows: rowsToRemove
|
57 | });
|
58 | const cellToFocus = getCellToFocus(table, removedRowIndexes.first, columnIndexToFocus, tableUtils.getRows(table));
|
59 | writer.setSelection(writer.createPositionAt(cellToFocus, 0));
|
60 | });
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function getCellToFocus(table, removedRowIndex, columnToFocus, tableRowCount) {
|
69 |
|
70 | const row = table.getChild(Math.min(removedRowIndex, tableRowCount - 1));
|
71 |
|
72 | let cellToFocus = row.getChild(0);
|
73 | let column = 0;
|
74 | for (const tableCell of row.getChildren()) {
|
75 | if (column > columnToFocus) {
|
76 | return cellToFocus;
|
77 | }
|
78 | cellToFocus = tableCell;
|
79 | column += parseInt(tableCell.getAttribute('colspan') || '1');
|
80 | }
|
81 | return cellToFocus;
|
82 | }
|