Skip to content

Commit

Permalink
[FIX] clipboard: cannot paste whole table style
Browse files Browse the repository at this point in the history
Trying to copy a whole table, and then paste only the formatting
didn't work and nothing was pasted. This commit fixes the issue.

closes #5046

Task: 4218988
X-original-commit: 3fd1ef9
Signed-off-by: Rémi Rahir (rar) <[email protected]>
Signed-off-by: Adrien Minne (adrm) <[email protected]>
  • Loading branch information
hokolomopo committed Oct 1, 2024
1 parent d17293e commit 6fe34dc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
45 changes: 28 additions & 17 deletions src/clipboard_handlers/tables_clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface CopiedTable {
interface TableCell {
style?: TableStyle;
table?: CopiedTable;
isWholeTableCopied?: boolean;
}

interface ClipboardContent {
Expand All @@ -53,31 +54,36 @@ export class TableClipboardHandler extends AbstractCellClipboardHandler<
for (let col of columnsIndexes) {
const position = { col, row, sheetId };
const table = this.getters.getTable(position);
if (!table || copiedTablesIds.has(table.id)) {
if (!table) {
tableCellsInRow.push({});
continue;
}
const coreTable = this.getters.getCoreTable(position);
const tableZone = coreTable?.range.zone;
let copiedTable: CopiedTable | undefined = undefined;
// Copy whole table
if (coreTable && tableZone && zones.some((z) => isZoneInside(tableZone, z))) {
copiedTablesIds.add(coreTable.id);
if (
!copiedTablesIds.has(table.id) &&
coreTable &&
tableZone &&
zones.some((z) => isZoneInside(tableZone, z))
) {
copiedTablesIds.add(table.id);
const values: Array<string[]> = [];
for (const col of range(tableZone.left, tableZone.right + 1)) {
values.push(this.getters.getFilterHiddenValues({ sheetId, col, row: tableZone.top }));
}
tableCellsInRow.push({
table: {
range: coreTable.range.rangeData,
config: coreTable.config,
type: coreTable.type,
},
});
}
// Copy only style of cell
else if (table) {
tableCellsInRow.push({ style: this.getTableStyleToCopy(position) });
copiedTable = {
range: coreTable.range.rangeData,
config: coreTable.config,
type: coreTable.type,
};
}
tableCellsInRow.push({
table: copiedTable,
style: this.getTableStyleToCopy(position),
isWholeTableCopied: copiedTablesIds.has(table.id),
});
}
}

Expand Down Expand Up @@ -183,11 +189,16 @@ export class TableClipboardHandler extends AbstractCellClipboardHandler<
});
}

// Do not paste table style if we're inside another table
// We cannot check for dynamic tables, because at this point the paste can have changed the evaluation, and the
// dynamic tables are not yet computed
if (!this.getters.getCoreTable(position)) {
if (tableCell.style?.style && options?.pasteOption !== "asValue") {
if (this.getters.getCoreTable(position) || options?.pasteOption === "asValue") {
return;
}
if (
(!options?.pasteOption && !tableCell.isWholeTableCopied) ||
options?.pasteOption === "onlyFormat"
) {
if (tableCell.style?.style) {
this.dispatch("UPDATE_CELL", { ...position, style: tableCell.style.style });
}
if (tableCell.style?.border) {
Expand Down
30 changes: 19 additions & 11 deletions tests/table/tables_plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandResult, Model } from "../../src";
import { toUnboundedZone, toZone, zoneToXc } from "../../src/helpers";
import { ClipboardPasteOptions, UID } from "../../src/types";
import { UID } from "../../src/types";
import {
addColumns,
addRows,
Expand Down Expand Up @@ -815,17 +815,25 @@ describe("Table plugin", () => {
expect(getCell(model, "B1")?.style?.fillColor).not.toEqual("#FF0000");
});

test.each(["onlyFormat", "asValue"] as ClipboardPasteOptions[])(
"Special paste %s don't paste whole tables",
(pasteOption: ClipboardPasteOptions) => {
createTable(model, "A1:B4");
updateFilter(model, "A1", ["thisIsAValue"]);
test("Paste as value do not copy the table", () => {
createTable(model, "A1:B4", { styleId: "TestStyleAllRed" });

copy(model, "A1:B4");
paste(model, "A5", pasteOption);
expect(getTable(model, "A5")).toBeFalsy();
}
);
copy(model, "A1:B4");
paste(model, "A5", "asValue");
expect(getTable(model, "A5")).toBeFalsy();
expect(getCell(model, "A5")?.style).toBeUndefined();
});

test("Can copy/paste the whole table formatting", () => {
createTable(model, "A1:A2", { styleId: "TestStyleAllRed" });

copy(model, "A1:A2");
paste(model, "A5", "onlyFormat");
expect(getTable(model, "A5")).toBeFalsy();
expect(getCell(model, "A5")?.style).toEqual({ fillColor: "#FF0000", bold: true });
expect(getBorder(model, "A5")).toEqual({ top: DEFAULT_BORDER_DESC });
expect(getCell(model, "A6")?.style).toEqual({ fillColor: "#FF0000" });
});

test("Pasting onlyFormat with a partial table copied paste the table style, not asValue", () => {
createTable(model, "A1:B4");
Expand Down

0 comments on commit 6fe34dc

Please sign in to comment.