fix: history/undo/redo — re-init after project load + sync to Yjs CRDT

This commit is contained in:
A0 Orchestrator
2026-07-03 21:18:13 +02:00
parent c8858430ad
commit 510004cd8f
+23 -2
View File
@@ -238,18 +238,30 @@ const CADEditor: React.FC<CADEditorProps> = ({ projectId, token, onNavigateBack
if (snap) { if (snap) {
restoreSnapshot(snap); restoreSnapshot(snap);
syncHistory(); 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' }]); setCommandHistory((prev) => [...prev, { prefix: '·', text: 'Rückgängig: letzte Aktion', type: 'info' }]);
} }
}, [restoreSnapshot, syncHistory]); }, [restoreSnapshot, syncHistory, collab]);
const handleRedo = useCallback(() => { const handleRedo = useCallback(() => {
const snap = historyManagerRef.current.redo(); const snap = historyManagerRef.current.redo();
if (snap) { if (snap) {
restoreSnapshot(snap); restoreSnapshot(snap);
syncHistory(); 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' }]); setCommandHistory((prev) => [...prev, { prefix: '·', text: 'Wiederherstellen: letzte Aktion', type: 'info' }]);
} }
}, [restoreSnapshot, syncHistory]); }, [restoreSnapshot, syncHistory, collab]);
const pushHistorySnapshot = useCallback((label: string) => { const pushHistorySnapshot = useCallback((label: string) => {
historyManagerRef.current.pushSnapshot({ historyManagerRef.current.pushSnapshot({
@@ -318,6 +330,15 @@ const CADEditor: React.FC<CADEditorProps> = ({ projectId, token, onNavigateBack
} }
setSavedStatus('gespeichert'); setSavedStatus('gespeichert');
setDataLoaded(true); 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 // Push initial data to Yjs CRDT after load
// Issue #7: Only push to CRDT if backend data actually has content. // Issue #7: Only push to CRDT if backend data actually has content.
// Pushing empty arrays would wipe existing CRDT data from other connected clients. // Pushing empty arrays would wipe existing CRDT data from other connected clients.