Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(spreadsheet): trim locale from saved data
Elian Doran138528f8bcc3
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.tsindex 41f62cec79..a2903683d4 100644--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.spec.ts@@ -1,4 +1,4 @@-import { IWorkbookData } from "@univerjs/presets";+import { IWorkbookData, LocaleType } from "@univerjs/presets";import { describe, expect, it } from "vitest";import { slimWorkbookData } from "./persistence";@@ -8,10 +8,11 @@ function makeWorkbook(workbook: Partial<IWorkbookData>): IWorkbookData {}describe("slimWorkbookData", () => {- it("drops the top-level workbook id (regenerated on load)", () => {- const result = slimWorkbookData(makeWorkbook({ id: "abc123", name: "Sheet" }));+ it("drops the top-level workbook id and locale (both reassigned on load)", () => {+ const result = slimWorkbookData(makeWorkbook({ id: "abc123", locale: LocaleType.ZH_CN, name: "Sheet" }));expect(result.id).toBeUndefined();+ expect(result.locale).toBeUndefined();expect(result.name).toBe("Sheet");});diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsxindex 19cee4959f..82c9bd64d5 100644--- a/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx+++ b/apps/client/src/widgets/type_widgets/spreadsheet/persistence.tsx@@ -1,4 +1,4 @@-import { CommandType, FUniver, IDisposable, IWorkbookData } from "@univerjs/presets";+import { CommandType, FUniver, IDisposable, IWorkbookData, LocaleType } from "@univerjs/presets";import { MutableRef, useEffect, useRef } from "preact/hooks";import NoteContext from "../../../components/note_context";@@ -92,6 +92,12 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | n// ephemeral — view state keys off the sheet id, not the workbook id.workbookData.id = randomString();+ // Pin the workbook locale to the only locale bundle the editor registers (see+ // Spreadsheet.tsx). Univer's workbook model otherwise defaults a missing locale+ // to "zhCN", which mismatches the English UI; pinning it here keeps new and+ // loaded workbooks consistent and lets the persisted value be dropped on save.+ workbookData.locale = LocaleType.EN_US;+// Create the new workbook BEFORE disposing the old one so the formula// engine transitions cleanly without a gap where stale state could leak.const workbook = univerAPI.createWorkbook(workbookData);@@ -199,8 +205,8 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | n/*** 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.+ * - The top-level workbook `id` and `locale` are both reassigned on every load (see+ * `applyContent`), so the persisted values are never read — dropping them 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.@@ -211,6 +217,7 @@ export default function usePersistence(note: FNote, noteContext: NoteContext | nexport function slimWorkbookData(workbookData: IWorkbookData): Partial<IWorkbookData> {const slimmed = workbookData as Partial<IWorkbookData>;delete slimmed.id;+ delete slimmed.locale;if (slimmed.resources) {slimmed.resources = slimmed.resources.filter(