Skip to content

Commit

Permalink
[FIX] paint format: properly paint borders and tables
Browse files Browse the repository at this point in the history
Since the paint format tool was moved from the clipboard plugin to
its own store, it didn't handle the borders and tables properly. This
commit fixes that by adding the two ClipboardHandlers to the
PaintFormatStore.

closes #5051

Task: 4213894
X-original-commit: d29a874
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Adrien Minne (adrm) <[email protected]>
  • Loading branch information
hokolomopo committed Oct 2, 2024
1 parent ae9ec2a commit a84cae5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/components/paint_format_button/paint_format_store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ClipboardHandler } from "../../clipboard_handlers/abstract_clipboard_handler";
import { BorderClipboardHandler } from "../../clipboard_handlers/borders_clipboard";
import { CellClipboardHandler } from "../../clipboard_handlers/cell_clipboard";
import { TableClipboardHandler } from "../../clipboard_handlers/tables_clipboard";
import { SELECTION_BORDER_COLOR } from "../../constants";
import { getClipboardDataPositions } from "../../helpers/clipboard/clipboard_helpers";
import { Get } from "../../store_engine";
Expand All @@ -16,7 +19,11 @@ export class PaintFormatStore extends SpreadsheetStore {
mutators = ["activate", "cancel", "pasteFormat"] as const;

protected highlightStore = this.get(HighlightStore);
private cellClipboardHandler = new CellClipboardHandler(this.getters, this.model.dispatch);
private clipboardHandlers: ClipboardHandler<any>[] = [
new CellClipboardHandler(this.getters, this.model.dispatch),
new BorderClipboardHandler(this.getters, this.model.dispatch),
new TableClipboardHandler(this.getters, this.model.dispatch),
];

private status: "inactive" | "oneOff" | "persistent" = "inactive";
private copiedData?: ClipboardContent;
Expand All @@ -42,10 +49,12 @@ export class PaintFormatStore extends SpreadsheetStore {
pasteFormat(target: Zone[]) {
if (this.copiedData) {
const sheetId = this.getters.getActiveSheetId();
this.cellClipboardHandler.paste({ zones: target, sheetId }, this.copiedData, {
isCutOperation: false,
pasteOption: "onlyFormat",
});
for (const handler of this.clipboardHandlers) {
handler.paste({ zones: target, sheetId }, this.copiedData, {
isCutOperation: false,
pasteOption: "onlyFormat",
});
}
}
if (this.status === "oneOff") {
this.cancel();
Expand All @@ -60,7 +69,12 @@ export class PaintFormatStore extends SpreadsheetStore {
const sheetId = this.getters.getActiveSheetId();
const zones = this.getters.getSelectedZones();

return this.cellClipboardHandler.copy(getClipboardDataPositions(sheetId, zones));
const copiedData = {};
for (const handler of this.clipboardHandlers) {
Object.assign(copiedData, handler.copy(getClipboardDataPositions(sheetId, zones)));
}

return copiedData as ClipboardContent;
}

get highlights(): Highlight[] {
Expand Down
17 changes: 16 additions & 1 deletion tests/grid/grid_component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PaintFormatStore } from "../../src/components/paint_format_button/paint
import { CellPopoverStore } from "../../src/components/popover";
import {
BACKGROUND_GRAY_COLOR,
DEFAULT_BORDER_DESC,
DEFAULT_CELL_HEIGHT,
DEFAULT_CELL_WIDTH,
GRID_ICON_EDGE_LENGTH,
Expand Down Expand Up @@ -40,6 +41,7 @@ import {
selectColumn,
selectHeader,
selectRow,
setBorders,
setCellContent,
setCellFormat,
setSelection,
Expand All @@ -64,6 +66,7 @@ import {
} from "../test_helpers/dom_helper";
import {
getActiveSheetFullScrollInfo,
getBorder,
getCell,
getCellContent,
getCellText,
Expand Down Expand Up @@ -849,22 +852,34 @@ describe("Grid component", () => {
highlightStore = env.getStore(HighlightStore);
});

test("can paste format with mouse once", async () => {
test("can paste format and borders with mouse once", async () => {
setCellContent(model, "B2", "b2");
selectCell(model, "B2");
setStyle(model, "B2", { bold: true });
setBorders(model, "B2", { top: DEFAULT_BORDER_DESC });
paintFormatStore.activate({ persistent: false });
gridMouseEvent(model, "pointerdown", "C8");
expect(getCell(model, "C8")).toBeUndefined();
gridMouseEvent(model, "pointerup", "C8");
expect(getCell(model, "C8")!.style).toEqual({ bold: true });
expect(getBorder(model, "C8")).toEqual({ top: DEFAULT_BORDER_DESC });

gridMouseEvent(model, "pointerdown", "D8");
expect(getCell(model, "D8")).toBeUndefined();
gridMouseEvent(model, "pointerup", "D8");
expect(getCell(model, "D8")).toBeUndefined();
});

test("Paste format works with table style", () => {
createTable(model, "A1:B2", { styleId: "TableStyleLight11" });
selectCell(model, "A1");
paintFormatStore.activate({ persistent: false });
gridMouseEvent(model, "pointerdown", "C8");
gridMouseEvent(model, "pointerup", "C8");

expect(getCell(model, "C8")?.style).toMatchObject({ fillColor: "#748747" });
});

test("can keep the paint format mode persistently", async () => {
setCellContent(model, "B2", "b2");
selectCell(model, "B2");
Expand Down

0 comments on commit a84cae5

Please sign in to comment.