From 40bc22f6e81757994ad336a62cfd66e9ec521e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B1=BC=E6=98=9F?= <1772580802@qq.com> Date: Tue, 2 Jun 2026 11:05:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E5=90=8E=E5=A4=9A=E7=94=BB=E5=B8=83=E7=A9=BA=E7=99=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useFlowCallbacks.ts | 24 ++++++++++++++---- src/utils/flow/canvasCache.js | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/utils/flow/canvasCache.js diff --git a/src/hooks/useFlowCallbacks.ts b/src/hooks/useFlowCallbacks.ts index d99d4ab..d5a037f 100644 --- a/src/hooks/useFlowCallbacks.ts +++ b/src/hooks/useFlowCallbacks.ts @@ -63,6 +63,10 @@ import { buildRuntimeNode, resolveNodeDefinition, } from '@/utils/flow/nodeOnboarding'; +import { + shouldPersistCanvas, + shouldUseCachedCanvas, +} from '@/utils/flow/canvasCache'; import { Dispatch } from 'redux'; import { @@ -519,8 +523,9 @@ export const useFlowCallbacks = ( // 初始化画布数据 const initializeCanvasData = useCallback(() => { const appKey = getCurrentFlowAppKey(); - if (appKey && canvasDataMap[appKey]) { - const { edges, nodes } = canvasDataMap[appKey]; + const cachedCanvas = appKey ? canvasDataMap[appKey] : null; + if (shouldUseCachedCanvas({ cachedCanvas, initialData, useDefault })) { + const { edges, nodes } = cachedCanvas; setNodes(nodes); setEdges(edges); } else { @@ -539,11 +544,14 @@ export const useFlowCallbacks = ( // 标记历史记录已初始化 setHistoryInitialized(true); - }, [initialData, canvasDataMap, getCurrentFlowAppKey]); + }, [initialData, useDefault, canvasDataMap, getCurrentFlowAppKey]); // 实时更新 canvasDataMap const updateCanvasDataMapEffect = useCallback(() => { const appKey = getCurrentFlowAppKey(); - if (appKey) { + if ( + appKey && + shouldPersistCanvas({ nodes, edges }) + ) { updateCanvasDataMapDebounced( dispatch, canvasDataMap, @@ -557,7 +565,13 @@ export const useFlowCallbacks = ( return () => { // 取消防抖函数 }; - }, [nodes, edges, dispatch, canvasDataMap, getCurrentFlowAppKey]); + }, [ + nodes, + edges, + dispatch, + canvasDataMap, + getCurrentFlowAppKey, + ]); // 关闭编辑弹窗 const closeEditModal = useCallback(() => { setIsEditModalOpen(false); diff --git a/src/utils/flow/canvasCache.js b/src/utils/flow/canvasCache.js new file mode 100644 index 0000000..017d630 --- /dev/null +++ b/src/utils/flow/canvasCache.js @@ -0,0 +1,48 @@ +function hasObjectValues(value) { + return Boolean(value && typeof value === 'object' && Object.keys(value).length > 0); +} + +function hasInitialCanvasData(initialData, useDefault) { + if (!initialData) { + return false; + } + + if (Array.isArray(initialData)) { + return initialData.length > 0; + } + + if (useDefault) { + return hasObjectValues(initialData?.main?.components) || hasObjectValues(initialData?.components); + } + + return hasObjectValues(initialData); +} + +function hasCanvasContent(canvas) { + return Boolean( + canvas && + ((Array.isArray(canvas.nodes) && canvas.nodes.length > 0) || + (Array.isArray(canvas.edges) && canvas.edges.length > 0)) + ); +} + +function shouldUseCachedCanvas({ cachedCanvas, initialData, useDefault }) { + if (!cachedCanvas) { + return false; + } + + if (hasCanvasContent(cachedCanvas)) { + return true; + } + + return !hasInitialCanvasData(initialData, useDefault); +} + +function shouldPersistCanvas({ nodes, edges }) { + return hasCanvasContent({ nodes, edges }); +} + +module.exports = { + shouldUseCachedCanvas, + shouldPersistCanvas, +};