Skip to content

Commit

Permalink
[FIX] clipboard: Do not update viewport on paste
Browse files Browse the repository at this point in the history
This commit fixes the problem that when pasting, the viewport will move
if the end of pasted area is not in the current viewport. This problem
is especially obvious when pasting a full col/row into a cell.

After fixing, the viewport will not move, so it will stay at the
location before pasting.

task 3271366

closes #2369

Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
  • Loading branch information
Chenyun Yang authored and rrahir committed Jun 5, 2023
1 parent 1c4aa50 commit 8b9c988
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/helpers/clipboard/clipboard_cells_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class ClipboardCellsState extends ClipboardCellsAbstractState {
if (height > 1 || width > 1 || isCutOperation) {
const zones = this.pastedZones(target, width, height);
const newZone = isCutOperation ? zones[0] : union(...zones);
this.selection.selectZone({ cell: { col, row }, zone: newZone });
this.selection.selectZone({ cell: { col, row }, zone: newZone }, { scrollIntoView: false });
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/helpers/clipboard/clipboard_os_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export class ClipboardOsState extends ClipboardCellsAbstractState {
right: activeCol + numberOfCols - 1,
bottom: activeRow + numberOfRows - 1,
};
this.selection.selectZone({ cell: { col: activeCol, row: activeRow }, zone });
this.selection.selectZone(
{ cell: { col: activeCol, row: activeRow }, zone },
{ scrollIntoView: false }
);
}

getClipboardContent(): Record<string, string> {
Expand Down
20 changes: 20 additions & 0 deletions tests/plugins/clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
setCellFormat,
setSelection,
setStyle,
setViewportOffset,
setZoneBorders,
undo,
} from "../test_helpers/commands_helpers";
Expand Down Expand Up @@ -556,6 +557,13 @@ describe("clipboard", () => {
expect(model.getters.getMerges(sheetId).map(zoneToXc)).toEqual(["B2:C3"]);
});

test("pasting from OS will not change the viewport", () => {
const model = new Model();
const viewport = model.getters.getActiveMainViewport();
pasteFromOSClipboard(model, "C60", "a\t1\nb\t2");
expect(model.getters.getActiveMainViewport()).toEqual(viewport);
});

test("pasting numbers from windows clipboard => interpreted as number", () => {
const model = new Model();
pasteFromOSClipboard(model, "C1", "1\r\n2\r\n3");
Expand Down Expand Up @@ -600,6 +608,18 @@ describe("clipboard", () => {
expect(getCellContent(model, "F2")).toBe("c2");
});

test("Viewport won't move after pasting", () => {
const model = new Model();
copy(model, "A1:B2");

setSelection(model, ["C60:D70"]);
setViewportOffset(model, 0, 0);
const viewport = model.getters.getActiveMainViewport();

paste(model, "C60:D70");
expect(model.getters.getActiveMainViewport()).toEqual(viewport);
});

describe("copy/paste a zone in a larger selection will duplicate the zone on the selection as long as it does not exceed it", () => {
test("paste a value (zone with hight=1 and width=1)", () => {
const model = new Model();
Expand Down

0 comments on commit 8b9c988

Please sign in to comment.