From e99cacb040f7e536234819a2936b5d7547bf4cf9 Mon Sep 17 00:00:00 2001 From: CrossPr0duct Date: Thu, 5 Jun 2025 19:12:31 -0400 Subject: [PATCH] Scene State saving works. --- .../src/pages/PageDraw/stores/SceneState.ts | 4 ++ .../editor3d/app/src/pages/Stores/TabState.ts | 40 +++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/frontend/apps/editor3d/app/src/pages/PageDraw/stores/SceneState.ts b/frontend/apps/editor3d/app/src/pages/PageDraw/stores/SceneState.ts index 3cb055089d..9a21d2bdf6 100644 --- a/frontend/apps/editor3d/app/src/pages/PageDraw/stores/SceneState.ts +++ b/frontend/apps/editor3d/app/src/pages/PageDraw/stores/SceneState.ts @@ -96,6 +96,10 @@ interface SceneState { importSceneFromJson: (jsonString: string) => boolean; saveSceneToFile: () => Promise; loadSceneFromFile: (file: File) => Promise; + + // Add these two properties + serializeSceneToString: () => Promise; + loadSceneFromString: (jsonString: string) => boolean; } const generateId = (): string => { diff --git a/frontend/apps/editor3d/app/src/pages/Stores/TabState.ts b/frontend/apps/editor3d/app/src/pages/Stores/TabState.ts index c82a5f8203..3267c4c7ba 100644 --- a/frontend/apps/editor3d/app/src/pages/Stores/TabState.ts +++ b/frontend/apps/editor3d/app/src/pages/Stores/TabState.ts @@ -1,4 +1,5 @@ import { create } from 'zustand'; +import { useSceneStore } from '../PageDraw/stores/SceneState'; export type TabId = '2D' | '3D' | 'VIDEO' | 'IMAGE'; @@ -10,7 +11,7 @@ interface TabState { [K in TabId]?: string; }; // Actions - setActiveTab: (tabId: TabId) => void; + setActiveTab: (tabId: TabId) => Promise; updateTabData: (tabId: TabId, data: unknown) => void; getTabData: (tabId: TabId) => T | null; clearTabData: (tabId: TabId) => void; @@ -20,8 +21,41 @@ export const useTabStore = create((set, get) => ({ activeTabId: '2D', tabData: {}, - setActiveTab: (tabId) => { - set({ activeTabId: tabId }); + setActiveTab: async (newTabId) => { + const currentTabId = get().activeTabId; + + // Don't do anything if we're already on this tab + if (currentTabId === newTabId) return true; + + try { + // Save current 2D state if we're leaving 2D tab + if (currentTabId === '2D') { + const sceneStore = useSceneStore.getState(); + const sceneState = await sceneStore.serializeSceneToString(); + set(state => ({ + tabData: { + ...state.tabData, + '2D': sceneState + } + })); + } + + // Load 2D state if we're entering 2D tab + if (newTabId === '2D') { + const savedState = get().tabData['2D']; + if (savedState) { + const sceneStore = useSceneStore.getState(); + sceneStore.loadSceneFromString(savedState); + } + } + + // Update active tab + set({ activeTabId: newTabId }); + return true; + } catch (error) { + console.error('Error during tab change:', error); + return false; + } }, updateTabData: (tabId, data) => {