From 5f0a950b24e542776a081992a6b0d7c54ce18a69 Mon Sep 17 00:00:00 2001 From: "Adrien Minne (adrm)" Date: Fri, 13 Dec 2024 09:23:13 +0100 Subject: [PATCH] [FIX] sheet view: DELETE_SHEET in initial revisions If a revision contains a `UPDATE_CELL`, the sheet is added to the `sheetsWithDirtyViewports` set. But the sheet isn't when deleting the sheet. So if the initial revisions a sheet is updated then deleted, we'll try to update its viewport in the `finalize` and crash. closes odoo/o-spreadsheet#5343 Task: 4405573 Signed-off-by: Pierre Rousseau (pro) --- src/plugins/ui/sheetview.ts | 4 ++++ tests/plugins/sheetview.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/plugins/ui/sheetview.ts b/src/plugins/ui/sheetview.ts index 19107edff1..d196e840d4 100644 --- a/src/plugins/ui/sheetview.ts +++ b/src/plugins/ui/sheetview.ts @@ -202,6 +202,7 @@ export class SheetViewPlugin extends UIPlugin { break; case "DELETE_SHEET": this.cleanViewports(); + this.sheetsWithDirtyViewports.delete(cmd.sheetId); break; case "ACTIVATE_SHEET": this.setViewports(); @@ -611,6 +612,9 @@ export class SheetViewPlugin extends UIPlugin { } private resetViewports(sheetId: UID) { + if (!this.getters.tryGetSheet(sheetId)) { + return; + } const { xSplit, ySplit } = this.getters.getPaneDivisions(sheetId); const nCols = this.getters.getNumberCols(sheetId); const nRows = this.getters.getNumberRows(sheetId); diff --git a/tests/plugins/sheetview.test.ts b/tests/plugins/sheetview.test.ts index 021a954494..2b294f48fa 100644 --- a/tests/plugins/sheetview.test.ts +++ b/tests/plugins/sheetview.test.ts @@ -2,12 +2,15 @@ import { CommandResult } from "../../src"; import { DEFAULT_CELL_HEIGHT, DEFAULT_CELL_WIDTH, + DEFAULT_REVISION_ID, DEFAULT_SHEETVIEW_SIZE, + MESSAGE_VERSION, } from "../../src/constants"; import { isDefined, numberToLetters, range, toXC, toZone, zoneToXc } from "../../src/helpers"; import { Model } from "../../src/model"; import { SheetViewPlugin } from "../../src/plugins/ui/sheetview"; import { Zone } from "../../src/types"; +import { StateUpdateMessage } from "../../src/types/collaborative/transport_service"; import { activateSheet, addColumns, @@ -909,6 +912,25 @@ describe("Viewport of Simple sheet", () => { height: 4.5 * DEFAULT_CELL_HEIGHT, }); }); + + test("Loading a model with initial revisions in sheet that is deleted doesn't crash", () => { + const initialMessages: StateUpdateMessage[] = [ + { + type: "REMOTE_REVISION", + serverRevisionId: DEFAULT_REVISION_ID, + nextRevisionId: "1", + version: MESSAGE_VERSION, + clientId: "bob", + commands: [ + { type: "CREATE_SHEET", position: 1, sheetId: "newSheetId" }, + { type: "UPDATE_CELL", sheetId: "newSheetId", col: 0, row: 0, content: "1" }, + { type: "DELETE_SHEET", sheetId: "newSheetId" }, + ], + }, + ]; + + expect(() => new Model({}, {}, initialMessages)).not.toThrow(); + }); }); describe("Multi Panes viewport", () => {