From 87597b77012bc9edf2ef83607c840a5d081b7762 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Tue, 3 Jun 2025 17:06:26 +0200 Subject: [PATCH] save6 --- .../components/custom-blocks/DrawBlock.tsx | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DrawBlock.tsx b/src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DrawBlock.tsx index 55a5a463..95fdabc7 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DrawBlock.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/components/custom-blocks/DrawBlock.tsx @@ -54,11 +54,28 @@ export const DrawBlock = createReactBlockSpec( return; } + // Load saved drawing data if available + if (block.props.drawingData) { + try { + console.log('Attempting to load saved drawing data'); + const drawingData = JSON.parse(block.props.drawingData); + editor.store.loadSnapshot(drawingData); + console.log('Successfully loaded drawing data'); + } catch (error) { + console.error('Failed to load drawing data:', error); + } + } + function logChangeEvent(eventInfo: string, changeData: any = null) { console.log(eventInfo); + // Get current properties + const currentProps = { ...block.props }; + // Get current changeHistory or initialize empty array - const currentHistory = block.props.changeHistory || []; + const currentHistory = Array.isArray(currentProps.changeHistory) + ? currentProps.changeHistory + : []; // Create a change record with timestamp const changeRecord = { @@ -67,17 +84,21 @@ export const DrawBlock = createReactBlockSpec( data: changeData, }; - // Update the block with the new change information - editorBN.updateBlock(block, { - props: { - changeHistory: [...currentHistory, changeRecord], - lastChange: changeRecord, - // Store drawing data if available - ...(changeData?.drawingData && { - drawingData: changeData.drawingData, - }), - }, - }); + // Create new props object with all updated values + const updatedProps = { + ...currentProps, + changeHistory: [...currentHistory, changeRecord], + lastChange: changeRecord, + }; + + // Add drawingData if available + if (changeData?.drawingData) { + updatedProps.drawingData = changeData.drawingData; + } + + // Update the block with the new props + console.log('Updating block with props:', updatedProps); + editorBN.updateBlock(block, { props: updatedProps }); setStoreEvents((events) => [...events, eventInfo]); } @@ -170,10 +191,19 @@ export const DrawBlock = createReactBlockSpec( // Capture the current drawing state if available if (editor.store) { try { - const snapshot = editor.store.getSnapshot(); - logChangeEvent('drawing updated', { - drawingData: snapshot, + // Get a serializable snapshot of the drawing + const snapshot = JSON.stringify(editor.store.getSnapshot()); + + // Only update drawingData property to avoid multiple updates + const currentProps = { ...block.props }; + editorBN.updateBlock(block, { + props: { + ...currentProps, + drawingData: snapshot, + }, }); + + console.log('Drawing snapshot updated'); } catch (error) { console.error('Failed to capture drawing snapshot:', error); }