Skip to content

Build your personal knowledge base with Trilium Notes

feat(spreadsheet): trim ID from saved data

Elian Doran13cd06155019
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
index c6b559e368..584718b075 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
@@ -1,37 +1,50 @@
import { IWorkbookData } from "@univerjs/presets";
import { describe, expect, it } from "vitest";
-import { stripEmptyResources } from "./persistence";
+import { slimWorkbookData } from "./persistence";
-function makeWorkbook(resources: IWorkbookData["resources"]): IWorkbookData {
- return { resources } as IWorkbookData;
+function makeWorkbook(workbook: Partial<IWorkbookData>): IWorkbookData {
+ return workbook as IWorkbookData;
}
-describe("stripEmptyResources", () => {
- it("removes resources whose data is empty (\"\" or \"{}\") and keeps the rest", () => {
- const workbook = makeWorkbook([
- { name: "SHEET_RANGE_PROTECTION_PLUGIN", data: "" },
- { name: "SHEET_NOTE_PLUGIN", data: "{}" },
- { name: "SHEET_FILTER_PLUGIN", data: "{\"col\":1}" },
- { name: "SHEET_DEFINED_NAME_PLUGIN", data: "[]" }
- ]);
+describe("slimWorkbookData", () => {
+ it("drops the top-level workbook id (regenerated on load)", () => {
+ const result = slimWorkbookData(makeWorkbook({ id: "abc123", name: "Sheet" }));
- const result = stripEmptyResources(workbook);
+ expect(result.id).toBeUndefined();
+ expect(result.name).toBe("Sheet");
+ });
+
+ it("removes resources whose data is empty (\"\" or \"{}\") and keeps the rest", () => {
+ const result = slimWorkbookData(makeWorkbook({
+ resources: [
+ { name: "SHEET_RANGE_PROTECTION_PLUGIN", data: "" },
+ { name: "SHEET_NOTE_PLUGIN", data: "{}" },
+ { name: "SHEET_FILTER_PLUGIN", data: "{\"col\":1}" },
+ // "[]" is not one of the stripped sentinels, so it is preserved.
+ { name: "SHEET_DEFINED_NAME_PLUGIN", data: "[]" }
+ ]
+ }));
expect(result.resources).toEqual([
{ name: "SHEET_FILTER_PLUGIN", data: "{\"col\":1}" },
- // "[]" is not one of the stripped sentinels, so it is preserved.
{ name: "SHEET_DEFINED_NAME_PLUGIN", data: "[]" }
]);
});
- it("leaves an all-empty resource list as an empty array and is a no-op when resources is absent", () => {
- expect(stripEmptyResources(makeWorkbook([
- { name: "A", data: "" },
- { name: "B", data: "{}" }
- ])).resources).toEqual([]);
+ it("leaves an all-empty resource list as an empty array, tolerates absent resources, and does not mutate the input", () => {
+ expect(slimWorkbookData(makeWorkbook({
+ resources: [
+ { name: "A", data: "" },
+ { name: "B", data: "{}" }
+ ]
+ })).resources).toEqual([]);
+
+ expect(slimWorkbookData(makeWorkbook({})).resources).toBeUndefined();
- const noResources = {} as IWorkbookData;
- expect(stripEmptyResources(noResources).resources).toBeUndefined();
+ const input = makeWorkbook({ id: "keep", resources: [{ name: "A", data: "" }] });
+ slimWorkbookData(input);
+ expect(input.id).toBe("keep");
+ expect(input.resources).toHaveLength(1);
});
});
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
index 4ccb20b1a7..bf438eaf76 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
@@ -128,7 +128,7 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | n
if (!workbook) return undefined;
const content = {
version: 1,
- workbook: stripEmptyResources(workbook.save())
+ workbook: slimWorkbookData(workbook.save())
};
const attachments: SavedData["attachments"] = [];
@@ -197,16 +197,24 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | n
}
/**
- * Drops plugin resources that carry no information (empty string or "{}") from the
- * workbook data before persisting. Univer leaves an absent resource's plugin at its
- * default (empty) state on load, which is identical to restoring these, so omitting
- * them is lossless and removes a large dead-weight block from the saved payload.
+ * Trims fields from the workbook data that are dead weight in the saved payload:
+ *
+ * - The top-level workbook `id` is regenerated on every load (see `applyContent`),
+ * so the persisted value is never read — dropping it is lossless.
+ * - Plugin resources whose `data` carries no information (empty string or "{}") are
+ * removed. Univer leaves an absent resource's plugin at its default (empty) state
+ * on load, which is identical to restoring these.
+ *
+ * Operates on a shallow copy so the live workbook object is left untouched.
*/
-export function stripEmptyResources(workbookData: IWorkbookData): IWorkbookData {
- if (workbookData.resources) {
- workbookData.resources = workbookData.resources.filter(
+export function slimWorkbookData(workbookData: IWorkbookData): Partial<IWorkbookData> {
+ const slimmed: Partial<IWorkbookData> = { ...workbookData };
+ delete slimmed.id;
+
+ if (slimmed.resources) {
+ slimmed.resources = slimmed.resources.filter(
resource => resource.data !== "" && resource.data !== "{}"
);
}
- return workbookData;
+ return slimmed;
}