1 |
|
2 |
|
3 |
|
4 |
|
5 | import { Command } from 'ckeditor5/src/core';
|
6 | import TableUtils from '../tableutils';
|
7 | import { updateNumericAttribute } from '../utils/common';
|
8 | import { removeEmptyRowsColumns } from '../utils/structure';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export default class MergeCellsCommand extends Command {
|
21 | |
22 |
|
23 |
|
24 | refresh() {
|
25 | const tableUtils = this.editor.plugins.get(TableUtils);
|
26 | const selectedTableCells = tableUtils.getSelectedTableCells(this.editor.model.document.selection);
|
27 | this.isEnabled = tableUtils.isSelectionRectangular(selectedTableCells);
|
28 | }
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | execute() {
|
35 | const model = this.editor.model;
|
36 | const tableUtils = this.editor.plugins.get(TableUtils);
|
37 | model.change(writer => {
|
38 | const selectedTableCells = tableUtils.getSelectedTableCells(model.document.selection);
|
39 |
|
40 | const firstTableCell = selectedTableCells.shift();
|
41 |
|
42 | const { mergeWidth, mergeHeight } = getMergeDimensions(firstTableCell, selectedTableCells, tableUtils);
|
43 | updateNumericAttribute('colspan', mergeWidth, firstTableCell, writer);
|
44 | updateNumericAttribute('rowspan', mergeHeight, firstTableCell, writer);
|
45 | for (const tableCell of selectedTableCells) {
|
46 | mergeTableCells(tableCell, firstTableCell, writer);
|
47 | }
|
48 | const table = firstTableCell.findAncestor('table');
|
49 |
|
50 | removeEmptyRowsColumns(table, tableUtils);
|
51 | writer.setSelection(firstTableCell, 'in');
|
52 | });
|
53 | }
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function mergeTableCells(cellBeingMerged, targetCell, writer) {
|
61 | if (!isEmpty(cellBeingMerged)) {
|
62 | if (isEmpty(targetCell)) {
|
63 | writer.remove(writer.createRangeIn(targetCell));
|
64 | }
|
65 | writer.move(writer.createRangeIn(cellBeingMerged), writer.createPositionAt(targetCell, 'end'));
|
66 | }
|
67 |
|
68 | writer.remove(cellBeingMerged);
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | function isEmpty(tableCell) {
|
74 | const firstTableChild = tableCell.getChild(0);
|
75 | return tableCell.childCount == 1 && firstTableChild.is('element', 'paragraph') && firstTableChild.isEmpty;
|
76 | }
|
77 | function getMergeDimensions(firstTableCell, selectedTableCells, tableUtils) {
|
78 | let maxWidthOffset = 0;
|
79 | let maxHeightOffset = 0;
|
80 | for (const tableCell of selectedTableCells) {
|
81 | const { row, column } = tableUtils.getCellLocation(tableCell);
|
82 | maxWidthOffset = getMaxOffset(tableCell, column, maxWidthOffset, 'colspan');
|
83 | maxHeightOffset = getMaxOffset(tableCell, row, maxHeightOffset, 'rowspan');
|
84 | }
|
85 |
|
86 | const { row: firstCellRow, column: firstCellColumn } = tableUtils.getCellLocation(firstTableCell);
|
87 | const mergeWidth = maxWidthOffset - firstCellColumn;
|
88 | const mergeHeight = maxHeightOffset - firstCellRow;
|
89 | return { mergeWidth, mergeHeight };
|
90 | }
|
91 | function getMaxOffset(tableCell, start, currentMaxOffset, which) {
|
92 | const dimensionValue = parseInt(tableCell.getAttribute(which) || '1');
|
93 | return Math.max(currentMaxOffset, start + dimensionValue);
|
94 | }
|