9.7 KiB
Undo/Redo System Implementation
Overview
The Constellation Analyzer now features a comprehensive per-document undo/redo system that allows users to safely experiment with their graphs without fear of permanent mistakes.
Key Features:
- ✅ Per-Document History: Each document maintains its own independent undo/redo stack (max 50 actions)
- ✅ Keyboard Shortcuts: Ctrl+Z (undo), Ctrl+Y or Ctrl+Shift+Z (redo)
- ✅ Visual UI: Undo/Redo buttons in toolbar with disabled states and tooltips
- ✅ Action Descriptions: Hover tooltips show what action will be undone/redone
- ✅ Automatic Tracking: All graph operations are automatically tracked
- ✅ Debounced Moves: Node dragging is debounced to avoid cluttering history
- ✅ Document Switching: History is preserved when switching between documents
Architecture
1. History Store (src/stores/historyStore.ts)
The central store manages history for all documents:
{
histories: Map<documentId, DocumentHistory>
maxHistorySize: 50
}
Each DocumentHistory contains:
undoStack: Array of past actions (most recent at end)redoStack: Array of undone actions that can be redonecurrentState: The current document state snapshot
Key Methods:
pushAction(documentId, action): Records a new actionundo(documentId): Reverts to previous stateredo(documentId): Restores undone statecanUndo/canRedo(documentId): Check if actions availableinitializeHistory(documentId, initialState): Setup history for new documentremoveHistory(documentId): Clean up when document deleted
2. Document History Hook (src/hooks/useDocumentHistory.ts)
Provides high-level undo/redo functionality for the active document:
const { undo, redo, canUndo, canRedo, undoDescription, redoDescription, pushToHistory } = useDocumentHistory();
Responsibilities:
- Initializes history when document is first loaded
- Provides
pushToHistory(description)to record actions - Handles undo/redo by restoring document state
- Updates both graphStore and workspaceStore on undo/redo
- Marks documents as dirty after undo/redo
3. Graph Operations with History (src/hooks/useGraphWithHistory.ts)
OPTIONAL WRAPPER: This hook wraps all graph operations with automatic history tracking.
const { addNode, updateNode, deleteNode, addEdge, ... } = useGraphWithHistory();
Features:
- Debounces node position changes (500ms) to avoid cluttering history during dragging
- Immediate history push for add/delete operations
- Smart action descriptions (e.g., "Add Person Actor", "Delete Collaborates Relation")
- Prevents recursive history pushes during undo/redo restore
Note: This is an alternative to manually calling pushToHistory() after each operation.
4. Keyboard Shortcuts (src/hooks/useKeyboardShortcuts.ts)
Extended to support undo/redo:
useKeyboardShortcuts({
onUndo: undo,
onRedo: redo,
// ... other shortcuts
});
Handles:
Ctrl+Z/Cmd+Z: UndoCtrl+Y/Cmd+Y: RedoCtrl+Shift+Z/Cmd+Shift+Z: Alternative redo
5. Toolbar UI (src/components/Toolbar/Toolbar.tsx)
Displays undo/redo buttons with visual feedback:
- Undo Button: Shows "Undo: [action description] (Ctrl+Z)" on hover
- Redo Button: Shows "Redo: [action description] (Ctrl+Y)" on hover
- Buttons are disabled (grayed out) when no actions available
- Uses Material-UI icons (UndoIcon, RedoIcon)
6. App Integration (src/App.tsx)
Connects keyboard shortcuts to undo/redo functionality:
const { undo, redo } = useDocumentHistory();
useKeyboardShortcuts({
onUndo: undo,
onRedo: redo,
});
Usage
Option A: Manual History Tracking
Components can manually record actions:
import { useDocumentHistory } from '../hooks/useDocumentHistory';
function MyComponent() {
const { pushToHistory } = useDocumentHistory();
const graphStore = useGraphStore();
const handleAddNode = () => {
graphStore.addNode(newNode);
pushToHistory('Add Person Actor');
};
}
Option B: Automatic with useGraphWithHistory
Replace useGraphStore() with useGraphWithHistory():
import { useGraphWithHistory } from '../hooks/useGraphWithHistory';
function MyComponent() {
const { addNode } = useGraphWithHistory();
const handleAddNode = () => {
addNode(newNode); // Automatically tracked!
};
}
Current Implementation
The current codebase uses Option A (manual tracking). Components like GraphEditor and Toolbar use useGraphStore() directly.
To enable automatic tracking, update components to use useGraphWithHistory() instead of useGraphStore().
How It Works: Undo/Redo Flow
Recording an Action
- User performs action (e.g., adds a node)
pushToHistory('Add Person Actor')is called- Current document state is snapshotted
- Snapshot is pushed to
undoStack redoStackis cleared (since new action invalidates redo)
Performing Undo
- User presses Ctrl+Z or clicks Undo button
- Last action is popped from
undoStack - Current state is pushed to
redoStack - Previous state from action is restored
- GraphStore and WorkspaceStore are updated
- Document marked as dirty
Performing Redo
- User presses Ctrl+Y or clicks Redo button
- Last undone action is popped from
redoStack - Current state is pushed to
undoStack - Future state from undone action is restored
- GraphStore and WorkspaceStore are updated
- Document marked as dirty
Per-Document Independence
Critical Feature: Each document has completely separate history.
Example workflow:
- Document A: Add 3 nodes
- Switch to Document B: Add 2 edges
- Switch back to Document A: Can still undo those 3 node additions
- Switch back to Document B: Can still undo those 2 edge additions
History stacks are preserved across document switches and remain independent.
Performance Considerations
Memory Management
- Max 50 actions per document (configurable via
MAX_HISTORY_SIZE) - Old actions are automatically removed when limit exceeded
- History is removed when document is deleted
- Document states use deep cloning to prevent mutation issues
Debouncing
- Node position updates are debounced (500ms) to group multiple moves
- Add/delete operations are immediate (0ms delay)
- Prevents hundreds of history entries when dragging nodes
Testing Checklist
- Create history store
- Create useDocumentHistory hook
- Add keyboard shortcuts (Ctrl+Z, Ctrl+Y, Ctrl+Shift+Z)
- Add undo/redo buttons to toolbar
- Show action descriptions in tooltips
- Disable buttons when no actions available
- Test: Add node → Undo → Node disappears
- Test: Delete node → Undo → Node reappears with connections
- Test: Move node → Undo → Node returns to original position
- Test: Add edge → Undo → Edge disappears
- Test: Update node properties → Undo → Properties restored
- Test: Multiple operations → Undo multiple times → Redo multiple times
- Test: Document A changes → Switch to Document B → Changes independent
- Test: 51 actions → Oldest action removed from history
- Test: Undo then new action → Redo stack cleared
- Test: Keyboard shortcuts work (Ctrl+Z, Ctrl+Y)
Future Enhancements
- History Panel: Show list of all actions with ability to jump to specific point
- Persistent History: Save history to localStorage (survives page refresh)
- Collaborative Undo: Undo operations in multi-user scenarios
- Selective Undo: Undo specific actions, not just chronological
- History Branching: Tree-based history (like Git) instead of linear
- Action Grouping: Combine related actions (e.g., "Add 5 nodes" instead of 5 separate entries)
- Undo Metadata: Store viewport position, selection state with each action
- History Analytics: Track most common actions, undo patterns
Implementation Notes
Why Deep Cloning?
Document states are deep cloned using JSON.parse(JSON.stringify()) to prevent mutation:
const snapshot = JSON.parse(JSON.stringify(currentDoc));
This ensures that modifying the current state doesn't affect historical snapshots.
Why Separate undoStack and redoStack?
Standard undo/redo pattern:
- undoStack: Stores past states
- redoStack: Stores undone states that can be restored
When a new action occurs, redoStack is cleared because the "future" is no longer valid.
Why Per-Document History?
Users expect each document to maintain independent history, similar to:
- Text editors (each file has own undo stack)
- Image editors (each image has own history)
- IDEs (each file has own history)
This matches user mental model and prevents confusion.
File Structure
src/
├── stores/
│ └── historyStore.ts # Central history management
├── hooks/
│ ├── useDocumentHistory.ts # Per-document undo/redo
│ ├── useGraphWithHistory.ts # Automatic history tracking wrapper
│ └── useKeyboardShortcuts.ts # Keyboard shortcuts (extended)
├── components/
│ └── Toolbar/
│ └── Toolbar.tsx # UI buttons for undo/redo
└── App.tsx # Connects keyboard shortcuts
Conclusion
The undo/redo system provides a safety net for users, encouraging experimentation without fear of permanent mistakes. Each document maintains independent history, operations are automatically tracked, and the UI provides clear feedback about available undo/redo actions.
Status: ✅ Implementation Complete (Ready for Testing)
Implemented: 2025-10-09 Based on: UX_ANALYSIS.md recommendations (Priority: CRITICAL)