UNPKG

22.1 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 { Plugin } from 'ckeditor5/src/core';
6import TableSelection from './tableselection';
7import TableWalker from './tablewalker';
8import TableUtils from './tableutils';
9import { cropTableToDimensions, getHorizontallyOverlappingCells, getVerticallyOverlappingCells, removeEmptyRowsColumns, splitHorizontally, splitVertically, trimTableCellIfNeeded, adjustLastRowIndex, adjustLastColumnIndex } from './utils/structure';
10/**
11 * This plugin adds support for copying/cutting/pasting fragments of tables.
12 * It is loaded automatically by the {@link module:table/table~Table} plugin.
13 */
14export default class TableClipboard extends Plugin {
15 /**
16 * @inheritDoc
17 */
18 static get pluginName() {
19 return 'TableClipboard';
20 }
21 /**
22 * @inheritDoc
23 */
24 static get requires() {
25 return [TableSelection, TableUtils];
26 }
27 /**
28 * @inheritDoc
29 */
30 init() {
31 const editor = this.editor;
32 const viewDocument = editor.editing.view.document;
33 this.listenTo(viewDocument, 'copy', (evt, data) => this._onCopyCut(evt, data));
34 this.listenTo(viewDocument, 'cut', (evt, data) => this._onCopyCut(evt, data));
35 this.listenTo(editor.model, 'insertContent', (evt, [content, selectable]) => this._onInsertContent(evt, content, selectable), { priority: 'high' });
36 this.decorate('_replaceTableSlotCell');
37 }
38 /**
39 * Copies table content to a clipboard on "copy" & "cut" events.
40 *
41 * @param evt An object containing information about the handled event.
42 * @param data Clipboard event data.
43 */
44 _onCopyCut(evt, data) {
45 const tableSelection = this.editor.plugins.get(TableSelection);
46 if (!tableSelection.getSelectedTableCells()) {
47 return;
48 }
49 if (evt.name == 'cut' && !this.editor.model.canEditAt(this.editor.model.document.selection)) {
50 return;
51 }
52 data.preventDefault();
53 evt.stop();
54 const dataController = this.editor.data;
55 const viewDocument = this.editor.editing.view.document;
56 const content = dataController.toView(tableSelection.getSelectionAsFragment());
57 viewDocument.fire('clipboardOutput', {
58 dataTransfer: data.dataTransfer,
59 content,
60 method: evt.name
61 });
62 }
63 /**
64 * Overrides default {@link module:engine/model/model~Model#insertContent `model.insertContent()`} method to handle pasting table inside
65 * selected table fragment.
66 *
67 * Depending on selected table fragment:
68 * - If a selected table fragment is smaller than paste table it will crop pasted table to match dimensions.
69 * - If dimensions are equal it will replace selected table fragment with a pasted table contents.
70 *
71 * @param content The content to insert.
72 * @param selectable The selection into which the content should be inserted.
73 * If not provided the current model document selection will be used.
74 */
75 _onInsertContent(evt, content, selectable) {
76 if (selectable && !selectable.is('documentSelection')) {
77 return;
78 }
79 const model = this.editor.model;
80 const tableUtils = this.editor.plugins.get(TableUtils);
81 // We might need to crop table before inserting so reference might change.
82 let pastedTable = this.getTableIfOnlyTableInContent(content, model);
83 if (!pastedTable) {
84 return;
85 }
86 const selectedTableCells = tableUtils.getSelectionAffectedTableCells(model.document.selection);
87 if (!selectedTableCells.length) {
88 removeEmptyRowsColumns(pastedTable, tableUtils);
89 return;
90 }
91 // Override default model.insertContent() handling at this point.
92 evt.stop();
93 model.change(writer => {
94 const pastedDimensions = {
95 width: tableUtils.getColumns(pastedTable),
96 height: tableUtils.getRows(pastedTable)
97 };
98 // Prepare the table for pasting.
99 const selection = prepareTableForPasting(selectedTableCells, pastedDimensions, writer, tableUtils);
100 // Beyond this point we operate on a fixed content table with rectangular selection and proper last row/column values.
101 const selectionHeight = selection.lastRow - selection.firstRow + 1;
102 const selectionWidth = selection.lastColumn - selection.firstColumn + 1;
103 // Crop pasted table if:
104 // - Pasted table dimensions exceeds selection area.
105 // - Pasted table has broken layout (ie some cells sticks out by the table dimensions established by the first and last row).
106 //
107 // Note: The table dimensions are established by the width of the first row and the total number of rows.
108 // It is possible to programmatically create a table that has rows which would have cells anchored beyond first row width but
109 // such table will not be created by other editing solutions.
110 const cropDimensions = {
111 startRow: 0,
112 startColumn: 0,
113 endRow: Math.min(selectionHeight, pastedDimensions.height) - 1,
114 endColumn: Math.min(selectionWidth, pastedDimensions.width) - 1
115 };
116 pastedTable = cropTableToDimensions(pastedTable, cropDimensions, writer);
117 // Content table to which we insert a pasted table.
118 const selectedTable = selectedTableCells[0].findAncestor('table');
119 const cellsToSelect = this._replaceSelectedCellsWithPasted(pastedTable, pastedDimensions, selectedTable, selection, writer);
120 if (this.editor.plugins.get('TableSelection').isEnabled) {
121 // Selection ranges must be sorted because the first and last selection ranges are considered
122 // as anchor/focus cell ranges for multi-cell selection.
123 const selectionRanges = tableUtils.sortRanges(cellsToSelect.map(cell => writer.createRangeOn(cell)));
124 writer.setSelection(selectionRanges);
125 }
126 else {
127 // Set selection inside first cell if multi-cell selection is disabled.
128 writer.setSelection(cellsToSelect[0], 0);
129 }
130 });
131 }
132 /**
133 * Replaces the part of selectedTable with pastedTable.
134 */
135 _replaceSelectedCellsWithPasted(pastedTable, pastedDimensions, selectedTable, selection, writer) {
136 const { width: pastedWidth, height: pastedHeight } = pastedDimensions;
137 // Holds two-dimensional array that is addressed by [ row ][ column ] that stores cells anchored at given location.
138 const pastedTableLocationMap = createLocationMap(pastedTable, pastedWidth, pastedHeight);
139 const selectedTableMap = [...new TableWalker(selectedTable, {
140 startRow: selection.firstRow,
141 endRow: selection.lastRow,
142 startColumn: selection.firstColumn,
143 endColumn: selection.lastColumn,
144 includeAllSlots: true
145 })];
146 // Selection must be set to pasted cells (some might be removed or new created).
147 const cellsToSelect = [];
148 // Store next cell insert position.
149 let insertPosition;
150 // Content table replace cells algorithm iterates over a selected table fragment and:
151 //
152 // - Removes existing table cells at current slot (location).
153 // - Inserts cell from a pasted table for a matched slots.
154 //
155 // This ensures proper table geometry after the paste
156 for (const tableSlot of selectedTableMap) {
157 const { row, column } = tableSlot;
158 // Save the insert position for current row start.
159 if (column === selection.firstColumn) {
160 insertPosition = tableSlot.getPositionBefore();
161 }
162 // Map current table slot location to an pasted table slot location.
163 const pastedRow = row - selection.firstRow;
164 const pastedColumn = column - selection.firstColumn;
165 const pastedCell = pastedTableLocationMap[pastedRow % pastedHeight][pastedColumn % pastedWidth];
166 // Clone cell to insert (to duplicate its attributes and children).
167 // Cloning is required to support repeating pasted table content when inserting to a bigger selection.
168 const cellToInsert = pastedCell ? writer.cloneElement(pastedCell) : null;
169 // Replace the cell from the current slot with new table cell.
170 const newTableCell = this._replaceTableSlotCell(tableSlot, cellToInsert, insertPosition, writer);
171 // The cell was only removed.
172 if (!newTableCell) {
173 continue;
174 }
175 // Trim the cell if it's row/col-spans would exceed selection area.
176 trimTableCellIfNeeded(newTableCell, row, column, selection.lastRow, selection.lastColumn, writer);
177 cellsToSelect.push(newTableCell);
178 insertPosition = writer.createPositionAfter(newTableCell);
179 }
180 // If there are any headings, all the cells that overlap from heading must be splitted.
181 const headingRows = parseInt(selectedTable.getAttribute('headingRows') || '0');
182 const headingColumns = parseInt(selectedTable.getAttribute('headingColumns') || '0');
183 const areHeadingRowsIntersectingSelection = selection.firstRow < headingRows && headingRows <= selection.lastRow;
184 const areHeadingColumnsIntersectingSelection = selection.firstColumn < headingColumns && headingColumns <= selection.lastColumn;
185 if (areHeadingRowsIntersectingSelection) {
186 const columnsLimit = { first: selection.firstColumn, last: selection.lastColumn };
187 const newCells = doHorizontalSplit(selectedTable, headingRows, columnsLimit, writer, selection.firstRow);
188 cellsToSelect.push(...newCells);
189 }
190 if (areHeadingColumnsIntersectingSelection) {
191 const rowsLimit = { first: selection.firstRow, last: selection.lastRow };
192 const newCells = doVerticalSplit(selectedTable, headingColumns, rowsLimit, writer);
193 cellsToSelect.push(...newCells);
194 }
195 return cellsToSelect;
196 }
197 /**
198 * Replaces a single table slot.
199 *
200 * @returns Inserted table cell or null if slot should remain empty.
201 * @private
202 */
203 _replaceTableSlotCell(tableSlot, cellToInsert, insertPosition, writer) {
204 const { cell, isAnchor } = tableSlot;
205 // If the slot is occupied by a cell in a selected table - remove it.
206 // The slot of this cell will be either:
207 // - Replaced by a pasted table cell.
208 // - Spanned by a previously pasted table cell.
209 if (isAnchor) {
210 writer.remove(cell);
211 }
212 // There is no cell to insert (might be spanned by other cell in a pasted table) - advance to the next content table slot.
213 if (!cellToInsert) {
214 return null;
215 }
216 writer.insert(cellToInsert, insertPosition);
217 return cellToInsert;
218 }
219 /**
220 * Extracts the table for pasting into a table.
221 *
222 * @param content The content to insert.
223 * @param model The editor model.
224 */
225 getTableIfOnlyTableInContent(content, model) {
226 if (!content.is('documentFragment') && !content.is('element')) {
227 return null;
228 }
229 // Table passed directly.
230 if (content.is('element', 'table')) {
231 return content;
232 }
233 // We do not support mixed content when pasting table into table.
234 // See: https://github.com/ckeditor/ckeditor5/issues/6817.
235 if (content.childCount == 1 && content.getChild(0).is('element', 'table')) {
236 return content.getChild(0);
237 }
238 // If there are only whitespaces around a table then use that table for pasting.
239 const contentRange = model.createRangeIn(content);
240 for (const element of contentRange.getItems()) {
241 if (element.is('element', 'table')) {
242 // Stop checking if there is some content before table.
243 const rangeBefore = model.createRange(contentRange.start, model.createPositionBefore(element));
244 if (model.hasContent(rangeBefore, { ignoreWhitespaces: true })) {
245 return null;
246 }
247 // Stop checking if there is some content after table.
248 const rangeAfter = model.createRange(model.createPositionAfter(element), contentRange.end);
249 if (model.hasContent(rangeAfter, { ignoreWhitespaces: true })) {
250 return null;
251 }
252 // There wasn't any content neither before nor after.
253 return element;
254 }
255 }
256 return null;
257 }
258}
259/**
260 * Prepares a table for pasting and returns adjusted selection dimensions.
261 */
262function prepareTableForPasting(selectedTableCells, pastedDimensions, writer, tableUtils) {
263 const selectedTable = selectedTableCells[0].findAncestor('table');
264 const columnIndexes = tableUtils.getColumnIndexes(selectedTableCells);
265 const rowIndexes = tableUtils.getRowIndexes(selectedTableCells);
266 const selection = {
267 firstColumn: columnIndexes.first,
268 lastColumn: columnIndexes.last,
269 firstRow: rowIndexes.first,
270 lastRow: rowIndexes.last
271 };
272 // Single cell selected - expand selection to pasted table dimensions.
273 const shouldExpandSelection = selectedTableCells.length === 1;
274 if (shouldExpandSelection) {
275 selection.lastRow += pastedDimensions.height - 1;
276 selection.lastColumn += pastedDimensions.width - 1;
277 expandTableSize(selectedTable, selection.lastRow + 1, selection.lastColumn + 1, tableUtils);
278 }
279 // In case of expanding selection we do not reset the selection so in this case we will always try to fix selection
280 // like in the case of a non-rectangular area. This might be fixed by re-setting selected cells array but this shortcut is safe.
281 if (shouldExpandSelection || !tableUtils.isSelectionRectangular(selectedTableCells)) {
282 // For a non-rectangular selection (ie in which some cells sticks out from a virtual selection rectangle) we need to create
283 // a table layout that has a rectangular selection. This will split cells so the selection become rectangular.
284 // Beyond this point we will operate on fixed content table.
285 splitCellsToRectangularSelection(selectedTable, selection, writer);
286 }
287 // However a selected table fragment might be invalid if examined alone. Ie such table fragment:
288 //
289 // +---+---+---+---+
290 // 0 | a | b | c | d |
291 // + + +---+---+
292 // 1 | | e | f | g |
293 // + +---+ +---+
294 // 2 | | h | | i | <- last row, each cell has rowspan = 2,
295 // + + + + + so we need to return 3, not 2
296 // 3 | | | | |
297 // +---+---+---+---+
298 //
299 // is invalid as the cells "h" and "i" have rowspans.
300 // This case needs only adjusting the selection dimension as the rest of the algorithm operates on empty slots also.
301 else {
302 selection.lastRow = adjustLastRowIndex(selectedTable, selection);
303 selection.lastColumn = adjustLastColumnIndex(selectedTable, selection);
304 }
305 return selection;
306}
307/**
308 * Expand table (in place) to expected size.
309 */
310function expandTableSize(table, expectedHeight, expectedWidth, tableUtils) {
311 const tableWidth = tableUtils.getColumns(table);
312 const tableHeight = tableUtils.getRows(table);
313 if (expectedWidth > tableWidth) {
314 tableUtils.insertColumns(table, {
315 at: tableWidth,
316 columns: expectedWidth - tableWidth
317 });
318 }
319 if (expectedHeight > tableHeight) {
320 tableUtils.insertRows(table, {
321 at: tableHeight,
322 rows: expectedHeight - tableHeight
323 });
324 }
325}
326/**
327 * Returns two-dimensional array that is addressed by [ row ][ column ] that stores cells anchored at given location.
328 *
329 * At given row & column location it might be one of:
330 *
331 * * cell - cell from pasted table anchored at this location.
332 * * null - if no cell is anchored at this location.
333 *
334 * For instance, from a table below:
335 *
336 * +----+----+----+----+
337 * | 00 | 01 | 02 | 03 |
338 * + +----+----+----+
339 * | | 11 | 13 |
340 * +----+ +----+
341 * | 20 | | 23 |
342 * +----+----+----+----+
343 *
344 * The method will return an array (numbers represents cell element):
345 *
346 * ```ts
347 * const map = [
348 * [ '00', '01', '02', '03' ],
349 * [ null, '11', null, '13' ],
350 * [ '20', null, null, '23' ]
351 * ]
352 * ```
353 *
354 * This allows for a quick access to table at give row & column. For instance to access table cell "13" from pasted table call:
355 *
356 * ```ts
357 * const cell = map[ 1 ][ 3 ]
358 * ```
359 */
360function createLocationMap(table, width, height) {
361 // Create height x width (row x column) two-dimensional table to store cells.
362 const map = new Array(height).fill(null)
363 .map(() => new Array(width).fill(null));
364 for (const { column, row, cell } of new TableWalker(table)) {
365 map[row][column] = cell;
366 }
367 return map;
368}
369/**
370 * Make selected cells rectangular by splitting the cells that stand out from a rectangular selection.
371 *
372 * In the table below a selection is shown with "::" and slots with anchor cells are named.
373 *
374 * +----+----+----+----+----+ +----+----+----+----+----+
375 * | 00 | 01 | 02 | 03 | | 00 | 01 | 02 | 03 |
376 * + +----+ +----+----+ | ::::::::::::::::----+
377 * | | 11 | | 13 | 14 | | ::11 | | 13:: 14 | <- first row
378 * +----+----+ + +----+ +----::---| | ::----+
379 * | 20 | 21 | | | 24 | select cells: | 20 ::21 | | :: 24 |
380 * +----+----+ +----+----+ 11 -> 33 +----::---| |---::----+
381 * | 30 | | 33 | 34 | | 30 :: | | 33:: 34 | <- last row
382 * + + +----+ + | :::::::::::::::: +
383 * | | | 43 | | | | | 43 | |
384 * +----+----+----+----+----+ +----+----+----+----+----+
385 * ^ ^
386 * first & last columns
387 *
388 * Will update table to:
389 *
390 * +----+----+----+----+----+
391 * | 00 | 01 | 02 | 03 |
392 * + +----+----+----+----+
393 * | | 11 | | 13 | 14 |
394 * +----+----+ + +----+
395 * | 20 | 21 | | | 24 |
396 * +----+----+ +----+----+
397 * | 30 | | | 33 | 34 |
398 * + +----+----+----+ +
399 * | | | | 43 | |
400 * +----+----+----+----+----+
401 *
402 * In th example above:
403 * - Cell "02" which have `rowspan = 4` must be trimmed at first and at after last row.
404 * - Cell "03" which have `rowspan = 2` and `colspan = 2` must be trimmed at first column and after last row.
405 * - Cells "00", "03" & "30" which cannot be cut by this algorithm as they are outside the trimmed area.
406 * - Cell "13" cannot be cut as it is inside the trimmed area.
407 */
408function splitCellsToRectangularSelection(table, dimensions, writer) {
409 const { firstRow, lastRow, firstColumn, lastColumn } = dimensions;
410 const rowIndexes = { first: firstRow, last: lastRow };
411 const columnIndexes = { first: firstColumn, last: lastColumn };
412 // 1. Split cells vertically in two steps as first step might create cells that needs to split again.
413 doVerticalSplit(table, firstColumn, rowIndexes, writer);
414 doVerticalSplit(table, lastColumn + 1, rowIndexes, writer);
415 // 2. Split cells horizontally in two steps as first step might create cells that needs to split again.
416 doHorizontalSplit(table, firstRow, columnIndexes, writer);
417 doHorizontalSplit(table, lastRow + 1, columnIndexes, writer, firstRow);
418}
419function doHorizontalSplit(table, splitRow, limitColumns, writer, startRow = 0) {
420 // If selection starts at first row then no split is needed.
421 if (splitRow < 1) {
422 return;
423 }
424 const overlappingCells = getVerticallyOverlappingCells(table, splitRow, startRow);
425 // Filter out cells that are not touching insides of the rectangular selection.
426 const cellsToSplit = overlappingCells.filter(({ column, cellWidth }) => isAffectedBySelection(column, cellWidth, limitColumns));
427 return cellsToSplit.map(({ cell }) => splitHorizontally(cell, splitRow, writer));
428}
429function doVerticalSplit(table, splitColumn, limitRows, writer) {
430 // If selection starts at first column then no split is needed.
431 if (splitColumn < 1) {
432 return;
433 }
434 const overlappingCells = getHorizontallyOverlappingCells(table, splitColumn);
435 // Filter out cells that are not touching insides of the rectangular selection.
436 const cellsToSplit = overlappingCells.filter(({ row, cellHeight }) => isAffectedBySelection(row, cellHeight, limitRows));
437 return cellsToSplit.map(({ cell, column }) => splitVertically(cell, column, splitColumn, writer));
438}
439/**
440 * Checks if cell at given row (column) is affected by a rectangular selection defined by first/last column (row).
441 *
442 * The same check is used for row as for column.
443 */
444function isAffectedBySelection(index, span, limit) {
445 const endIndex = index + span - 1;
446 const { first, last } = limit;
447 const isInsideSelection = index >= first && index <= last;
448 const overlapsSelectionFromOutside = index < first && endIndex >= first;
449 return isInsideSelection || overlapsSelectionFromOutside;
450}