fix: history/undo/redo — re-init after project load + sync to Yjs CRDT
This commit is contained in:
+23
-2
@@ -238,18 +238,30 @@ const CADEditor: React.FC<CADEditorProps> = ({ projectId, token, onNavigateBack
|
||||
if (snap) {
|
||||
restoreSnapshot(snap);
|
||||
syncHistory();
|
||||
// Sync restored state to Yjs CRDT to prevent collab overwrite
|
||||
if (collab.status === 'connected') {
|
||||
for (const el of snap.elements) collab.setElement(el);
|
||||
for (const layer of snap.layers) collab.setLayer(layer);
|
||||
for (const block of snap.blocks) collab.setBlock(block);
|
||||
}
|
||||
setCommandHistory((prev) => [...prev, { prefix: '·', text: 'Rückgängig: letzte Aktion', type: 'info' }]);
|
||||
}
|
||||
}, [restoreSnapshot, syncHistory]);
|
||||
}, [restoreSnapshot, syncHistory, collab]);
|
||||
|
||||
const handleRedo = useCallback(() => {
|
||||
const snap = historyManagerRef.current.redo();
|
||||
if (snap) {
|
||||
restoreSnapshot(snap);
|
||||
syncHistory();
|
||||
// Sync restored state to Yjs CRDT to prevent collab overwrite
|
||||
if (collab.status === 'connected') {
|
||||
for (const el of snap.elements) collab.setElement(el);
|
||||
for (const layer of snap.layers) collab.setLayer(layer);
|
||||
for (const block of snap.blocks) collab.setBlock(block);
|
||||
}
|
||||
setCommandHistory((prev) => [...prev, { prefix: '·', text: 'Wiederherstellen: letzte Aktion', type: 'info' }]);
|
||||
}
|
||||
}, [restoreSnapshot, syncHistory]);
|
||||
}, [restoreSnapshot, syncHistory, collab]);
|
||||
|
||||
const pushHistorySnapshot = useCallback((label: string) => {
|
||||
historyManagerRef.current.pushSnapshot({
|
||||
@@ -318,6 +330,15 @@ const CADEditor: React.FC<CADEditorProps> = ({ projectId, token, onNavigateBack
|
||||
}
|
||||
setSavedStatus('gespeichert');
|
||||
setDataLoaded(true);
|
||||
// Re-initialize HistoryManager with loaded project state so undo does not jump back to empty initial state
|
||||
historyManagerRef.current.initialize({
|
||||
elements: data.elements,
|
||||
layers: data.layers.length > 0 ? data.layers : initialLayers,
|
||||
blocks: data.blocks,
|
||||
groups: [],
|
||||
bgConfig: null,
|
||||
});
|
||||
syncHistory();
|
||||
// Push initial data to Yjs CRDT after load
|
||||
// Issue #7: Only push to CRDT if backend data actually has content.
|
||||
// Pushing empty arrays would wipe existing CRDT data from other connected clients.
|
||||
|
||||
Reference in New Issue
Block a user