From d123ce33b87955bc90d817dd422659be4af55964 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 3 Mar 2026 19:09:33 +0200 Subject: [PATCH] feat(spreadsheet): restore from JSON --- .../src/widgets/type_widgets/Spreadsheet.tsx | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/apps/client/src/widgets/type_widgets/Spreadsheet.tsx b/apps/client/src/widgets/type_widgets/Spreadsheet.tsx index 40de98b109..8965c6c94d 100644 --- a/apps/client/src/widgets/type_widgets/Spreadsheet.tsx +++ b/apps/client/src/widgets/type_widgets/Spreadsheet.tsx @@ -3,7 +3,7 @@ import "./Spreadsheet.css"; import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'; import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'; -import { CommandType, createUniver, FUniver, LocaleType, mergeLocales } from '@univerjs/presets'; +import { CommandType, createUniver, FUniver, IWorkbookData, LocaleType, mergeLocales } from '@univerjs/presets'; import { MutableRef, useEffect, useRef } from "preact/hooks"; import NoteContext from "../../components/note_context"; @@ -45,7 +45,6 @@ function useInitializeSpreadsheet(containerRef: MutableRef univerAPI.dispose(); }, [ apiRef, containerRef ]); } @@ -83,15 +82,20 @@ function usePersistence(note: FNote, noteContext: NoteContext | null | undefined const univerAPI = apiRef.current; if (!univerAPI) return undefined; - try { - const parsedContent = JSON.parse(newContent) as unknown; - if (parsedContent && typeof parsedContent === "object" && "workbook" in parsedContent) { - const persistedData = parsedContent as PersistedData; - univerAPI.createWorkbook(persistedData.workbook); + let workbook: Partial = {}; + if (newContent) { + try { + const parsedContent = JSON.parse(newContent) as unknown; + if (parsedContent && typeof parsedContent === "object" && "workbook" in parsedContent) { + const persistedData = parsedContent as PersistedData; + workbook = persistedData.workbook; + } + } catch (e) { + console.error("Failed to parse spreadsheet content", e); } - } catch (e) { - console.error("Failed to parse spreadsheet content", e); } + + univerAPI.createWorkbook(workbook); }, });