Skip to content

Build your personal knowledge base with Trilium Notes

feat(spreadsheet): trim empty resources from data file

Elian Doranedc81f9d4b5d
diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md
index 4bc5fdba4e..c2e993faa6 100644
--- a/.github/copilot-instructions.md
+++ b/.github/copilot-instructions.md
@@ -255,6 +255,7 @@ Tools are defined using `defineTools()` in `apps/server/src/services/llm/tools/`
- **Build system**: Uses Vite for fast development, ESBuild for production optimization
- **Patches**: Custom patches in `patches/` directory for CKEditor and other dependencies
- **No non-null assertions**: Never use the TypeScript non-null assertion operator (postfix `!`), including in tests. Narrow instead — optional chaining (`?.`), a `?? fallback`, an explicit null check, or an `*OrThrow` accessor (e.g. `becca.getNoteOrThrow(id)` rather than `becca.getNote(id)!`).
+- **Helper placement**: When extracting a standalone helper function from a component, widget, hook, or route, place it below the primary export it supports (or in a separate module), not wedged between the imports and the main definition. The file's primary export stays near the top; supporting helpers follow it.
## Key Files for Context
diff --git a/CLAUDE.md b/CLAUDE.md
index ce2b703bd7..c5d09af3ef 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -267,6 +267,7 @@ Use `note.getOwnedAttribute()` for direct, `note.getAttribute()` for inherited.
- Unix line endings
- Import sorting via `eslint-plugin-simple-import-sort`
- **Never use the TypeScript non-null assertion operator (postfix `!`)** — including in tests. Narrow instead: optional chaining (`?.`), a `?? fallback`, an explicit null check before use, or an `*OrThrow` accessor (e.g. `becca.getNoteOrThrow(id)` rather than `becca.getNote(id)!`).
+- **Helper placement** — when extracting a standalone helper function from a component, widget, hook, or route, place it **below** the primary export it supports (or in a separate module), not wedged between the imports and the main definition. Keep the file's primary export near the top so the entry point reads first; supporting helpers follow it.
## Testing
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
new file mode 100644
index 0000000000..c6b559e368
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts
@@ -0,0 +1,37 @@
+import { IWorkbookData } from "@univerjs/presets";
+import { describe, expect, it } from "vitest";
+
+import { stripEmptyResources } from "./persistence";
+
+function makeWorkbook(resources: IWorkbookData["resources"]): IWorkbookData {
+ return { resources } 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: "[]" }
+ ]);
+
+ const result = stripEmptyResources(workbook);
+
+ 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([]);
+
+ const noResources = {} as IWorkbookData;
+ expect(stripEmptyResources(noResources).resources).toBeUndefined();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx
index c9e5999770..4ccb20b1a7 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: workbook.save()
+ workbook: stripEmptyResources(workbook.save())
};
const attachments: SavedData["attachments"] = [];
@@ -195,3 +195,18 @@ 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.
+ */
+export function stripEmptyResources(workbookData: IWorkbookData): IWorkbookData {
+ if (workbookData.resources) {
+ workbookData.resources = workbookData.resources.filter(
+ resource => resource.data !== "" && resource.data !== "{}"
+ );
+ }
+ return workbookData;
+}