Skip to content

Add undo/redo with input change batching#10

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/add-undo-redo-functionality
Draft

Add undo/redo with input change batching#10
Copilot wants to merge 4 commits intomainfrom
copilot/add-undo-redo-functionality

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 17, 2025

Implements Cmd/Ctrl+Z undo and Shift+Cmd/Ctrl+Z redo. Input changes are batched while focused—multiple arrow key presses become a single undo stop on blur or Enter.

Implementation

History management (use-history.ts)

  • Generic hook managing state stack with JSON-based change detection
  • Tracks history index for bidirectional navigation

Focus detection (use-input-focus.ts)

  • Disables global shortcuts when INPUT/TEXTAREA/contentEditable has focus
  • Prevents conflict with native browser undo/redo

State integration (use-matrices.ts)

  • Maintains separate display state for immediate feedback during skipHistory updates
  • Syncs with history state on undo/redo via useEffect
  • Commits pending input changes to history on blur

Keyboard handling (app.tsx)

useEffect(() => {
  const handleKeyDown = (e: KeyboardEvent) => {
    if (hasInputFocus) return
    
    const isModifierPressed = e.metaKey || e.ctrlKey
    if (isModifierPressed && e.key === 'z') {
      e.preventDefault()
      e.shiftKey ? values.redo() : values.undo()
    }
  }
  // ...
}, [hasInputFocus, values])

All operations are undoable: matrix updates, add/delete/move/invert/clone/toggle visibility.

Demo

Initial state with one matrix:
Initial

After adding second matrix:
Added

After Cmd+Z (undo) removes second matrix:
Undone

Original prompt

This section details on the original issue you should resolve

<issue_title>Undo/redo functionality</issue_title>
<issue_description>When an input doesn't have focus (those have their own notion of undo/redo), cmd z/shift cmd z (and ctrl variants on windows) should undo the last change made. I think all input changes should be batched while an input has focus. For example, if you are using arrowup/down to change the value of an input, each onChange event should not could as a single undo stop. Rather, the final value before the input is blurred should be considered the value. All operations should be undoable, including moving matrices around, inverting, etc. Probably easiest to store the entire state of all the matrices on every undo stop, guaranteeing we will always be able to go back to where we were previously.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits November 17, 2025 05:14
Co-authored-by: bschlenk <1390303+bschlenk@users.noreply.github.com>
Co-authored-by: bschlenk <1390303+bschlenk@users.noreply.github.com>
…atching

Co-authored-by: bschlenk <1390303+bschlenk@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement undo/redo functionality for input changes Add undo/redo with input change batching Nov 17, 2025
Copilot AI requested a review from bschlenk November 17, 2025 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undo/redo functionality

2 participants