UNPKG

9.49 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 */
5import { Command } from 'ckeditor5/src/core';
6import TableWalker from '../tablewalker';
7import { isHeadingColumnCell } from '../utils/common';
8import { removeEmptyRowsColumns } from '../utils/structure';
9/**
10 * The merge cell command.
11 *
12 * The command is registered by {@link module:table/tableediting~TableEditing} as the `'mergeTableCellRight'`, `'mergeTableCellLeft'`,
13 * `'mergeTableCellUp'` and `'mergeTableCellDown'` editor commands.
14 *
15 * To merge a table cell at the current selection with another cell, execute the command corresponding with the preferred direction.
16 *
17 * For example, to merge with a cell to the right:
18 *
19 * ```ts
20 * editor.execute( 'mergeTableCellRight' );
21 * ```
22 *
23 * **Note**: If a table cell has a different [`rowspan`](https://www.w3.org/TR/html50/tabular-data.html#attr-tdth-rowspan)
24 * (for `'mergeTableCellRight'` and `'mergeTableCellLeft'`) or [`colspan`](https://www.w3.org/TR/html50/tabular-data.html#attr-tdth-colspan)
25 * (for `'mergeTableCellUp'` and `'mergeTableCellDown'`), the command will be disabled.
26 */
27export default class MergeCellCommand extends Command {
28 /**
29 * Creates a new `MergeCellCommand` instance.
30 *
31 * @param editor The editor on which this command will be used.
32 * @param options.direction Indicates which cell to merge with the currently selected one.
33 * Possible values are: `'left'`, `'right'`, `'up'` and `'down'`.
34 */
35 constructor(editor, options) {
36 super(editor);
37 this.direction = options.direction;
38 this.isHorizontal = this.direction == 'right' || this.direction == 'left';
39 }
40 /**
41 * @inheritDoc
42 */
43 refresh() {
44 const cellToMerge = this._getMergeableCell();
45 this.value = cellToMerge;
46 this.isEnabled = !!cellToMerge;
47 }
48 /**
49 * Executes the command.
50 *
51 * Depending on the command's {@link #direction} value, it will merge the cell that is to the `'left'`, `'right'`, `'up'` or `'down'`.
52 *
53 * @fires execute
54 */
55 execute() {
56 const model = this.editor.model;
57 const doc = model.document;
58 const tableUtils = this.editor.plugins.get('TableUtils');
59 const tableCell = tableUtils.getTableCellsContainingSelection(doc.selection)[0];
60 const cellToMerge = this.value;
61 const direction = this.direction;
62 model.change(writer => {
63 const isMergeNext = direction == 'right' || direction == 'down';
64 // The merge mechanism is always the same so sort cells to be merged.
65 const cellToExpand = (isMergeNext ? tableCell : cellToMerge);
66 const cellToRemove = (isMergeNext ? cellToMerge : tableCell);
67 // Cache the parent of cell to remove for later check.
68 const removedTableCellRow = cellToRemove.parent;
69 mergeTableCells(cellToRemove, cellToExpand, writer);
70 const spanAttribute = this.isHorizontal ? 'colspan' : 'rowspan';
71 const cellSpan = parseInt(tableCell.getAttribute(spanAttribute) || '1');
72 const cellToMergeSpan = parseInt(cellToMerge.getAttribute(spanAttribute) || '1');
73 // Update table cell span attribute and merge set selection on merged contents.
74 writer.setAttribute(spanAttribute, cellSpan + cellToMergeSpan, cellToExpand);
75 writer.setSelection(writer.createRangeIn(cellToExpand));
76 const tableUtils = this.editor.plugins.get('TableUtils');
77 const table = removedTableCellRow.findAncestor('table');
78 // Remove empty rows and columns after merging.
79 removeEmptyRowsColumns(table, tableUtils);
80 });
81 }
82 /**
83 * Returns a cell that can be merged with the current cell depending on the command's direction.
84 */
85 _getMergeableCell() {
86 const model = this.editor.model;
87 const doc = model.document;
88 const tableUtils = this.editor.plugins.get('TableUtils');
89 const tableCell = tableUtils.getTableCellsContainingSelection(doc.selection)[0];
90 if (!tableCell) {
91 return;
92 }
93 // First get the cell on proper direction.
94 const cellToMerge = this.isHorizontal ?
95 getHorizontalCell(tableCell, this.direction, tableUtils) :
96 getVerticalCell(tableCell, this.direction, tableUtils);
97 if (!cellToMerge) {
98 return;
99 }
100 // If found check if the span perpendicular to merge direction is equal on both cells.
101 const spanAttribute = this.isHorizontal ? 'rowspan' : 'colspan';
102 const span = parseInt(tableCell.getAttribute(spanAttribute) || '1');
103 const cellToMergeSpan = parseInt(cellToMerge.getAttribute(spanAttribute) || '1');
104 if (cellToMergeSpan === span) {
105 return cellToMerge;
106 }
107 }
108}
109/**
110 * Returns the cell that can be merged horizontally.
111 */
112function getHorizontalCell(tableCell, direction, tableUtils) {
113 const tableRow = tableCell.parent;
114 const table = tableRow.parent;
115 const horizontalCell = direction == 'right' ? tableCell.nextSibling : tableCell.previousSibling;
116 const hasHeadingColumns = (table.getAttribute('headingColumns') || 0) > 0;
117 if (!horizontalCell) {
118 return;
119 }
120 // Sort cells:
121 const cellOnLeft = (direction == 'right' ? tableCell : horizontalCell);
122 const cellOnRight = (direction == 'right' ? horizontalCell : tableCell);
123 // Get their column indexes:
124 const { column: leftCellColumn } = tableUtils.getCellLocation(cellOnLeft);
125 const { column: rightCellColumn } = tableUtils.getCellLocation(cellOnRight);
126 const leftCellSpan = parseInt(cellOnLeft.getAttribute('colspan') || '1');
127 const isCellOnLeftInHeadingColumn = isHeadingColumnCell(tableUtils, cellOnLeft);
128 const isCellOnRightInHeadingColumn = isHeadingColumnCell(tableUtils, cellOnRight);
129 // We cannot merge heading columns cells with regular cells.
130 if (hasHeadingColumns && isCellOnLeftInHeadingColumn != isCellOnRightInHeadingColumn) {
131 return;
132 }
133 // The cell on the right must have index that is distant to the cell on the left by the left cell's width (colspan).
134 const cellsAreTouching = leftCellColumn + leftCellSpan === rightCellColumn;
135 // If the right cell's column index is different it means that there are rowspanned cells between them.
136 return cellsAreTouching ? horizontalCell : undefined;
137}
138/**
139 * Returns the cell that can be merged vertically.
140 */
141function getVerticalCell(tableCell, direction, tableUtils) {
142 const tableRow = tableCell.parent;
143 const table = tableRow.parent;
144 const rowIndex = table.getChildIndex(tableRow);
145 // Don't search for mergeable cell if direction points out of the table.
146 if ((direction == 'down' && rowIndex === tableUtils.getRows(table) - 1) || (direction == 'up' && rowIndex === 0)) {
147 return null;
148 }
149 const rowspan = parseInt(tableCell.getAttribute('rowspan') || '1');
150 const headingRows = table.getAttribute('headingRows') || 0;
151 const isMergeWithBodyCell = direction == 'down' && (rowIndex + rowspan) === headingRows;
152 const isMergeWithHeadCell = direction == 'up' && rowIndex === headingRows;
153 // Don't search for mergeable cell if direction points out of the current table section.
154 if (headingRows && (isMergeWithBodyCell || isMergeWithHeadCell)) {
155 return null;
156 }
157 const currentCellRowSpan = parseInt(tableCell.getAttribute('rowspan') || '1');
158 const rowOfCellToMerge = direction == 'down' ? rowIndex + currentCellRowSpan : rowIndex;
159 const tableMap = [...new TableWalker(table, { endRow: rowOfCellToMerge })];
160 const currentCellData = tableMap.find(value => value.cell === tableCell);
161 const mergeColumn = currentCellData.column;
162 const cellToMergeData = tableMap.find(({ row, cellHeight, column }) => {
163 if (column !== mergeColumn) {
164 return false;
165 }
166 if (direction == 'down') {
167 // If merging a cell below the mergeRow is already calculated.
168 return row === rowOfCellToMerge;
169 }
170 else {
171 // If merging a cell above calculate if it spans to mergeRow.
172 return rowOfCellToMerge === row + cellHeight;
173 }
174 });
175 return cellToMergeData && cellToMergeData.cell ? cellToMergeData.cell : null;
176}
177/**
178 * Merges two table cells. It will ensure that after merging cells with an empty paragraph, the resulting table cell will only have one
179 * paragraph. If one of the merged table cells is empty, the merged table cell will have the contents of the non-empty table cell.
180 * If both are empty, the merged table cell will have only one empty paragraph.
181 */
182function mergeTableCells(cellToRemove, cellToExpand, writer) {
183 if (!isEmpty(cellToRemove)) {
184 if (isEmpty(cellToExpand)) {
185 writer.remove(writer.createRangeIn(cellToExpand));
186 }
187 writer.move(writer.createRangeIn(cellToRemove), writer.createPositionAt(cellToExpand, 'end'));
188 }
189 // Remove merged table cell.
190 writer.remove(cellToRemove);
191}
192/**
193 * Checks if the passed table cell contains an empty paragraph.
194 */
195function isEmpty(tableCell) {
196 const firstTableChild = tableCell.getChild(0);
197 return tableCell.childCount == 1 && firstTableChild.is('element', 'paragraph') && firstTableChild.isEmpty;
198}