Skip to content

fix(app): preserve undo history when inserting tag pills in prompt input#22237

Open
callmeYe wants to merge 1 commit intoanomalyco:devfrom
callmeYe:fix/undo-tag-pill
Open

fix(app): preserve undo history when inserting tag pills in prompt input#22237
callmeYe wants to merge 1 commit intoanomalyco:devfrom
callmeYe:fix/undo-tag-pill

Conversation

@callmeYe
Copy link
Copy Markdown

@callmeYe callmeYe commented Apr 13, 2026

Issue for this PR

Closes #22234

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When inserting file/agent tag pills into the contenteditable prompt editor, the current code uses range.insertNode() — a direct DOM operation that bypasses the browser's native undo stack. This means pressing Ctrl+Z after inserting a tag doesn't undo the tag insertion; instead it undoes whatever happened before the tag (typically deleting preceding text).

This PR introduces a small helper (editor-insert.ts) that uses document.execCommand("insertHTML") to insert the pill HTML. Although execCommand is deprecated, it remains the only reliable way to push operations onto the browser's built-in undo/redo stack in a contenteditable element. The call site in prompt-input.tsx tries insertAtomicPartAtSelection() first, and falls back to the original range.insertNode path if execCommand is unavailable.

Files changed:

  • packages/app/src/components/prompt-input/editor-insert.ts (new) — serializeAtomicPartHtml() + insertAtomicPartAtSelection() helper
  • packages/app/src/components/prompt-input/editor-insert.test.ts (new) — unit tests for HTML serialization, XSS escaping, and execCommand usage
  • packages/app/src/components/prompt-input.tsx — import the helper, replace the direct DOM insertion with the execCommand approach + fallback

How did you verify your code works?

  • Unit tests pass (bun test editor-insert.test.ts — 5 tests, 13 assertions)
  • Manual test: type text → insert tag via @ → Ctrl+Z correctly removes the tag pill (not the preceding text)
  • Verified tag pills still render and function correctly after insertion
  • Verified existing prompt-input behavior is unaffected

Screenshots / recordings

N/A — this is an interaction behavior fix, best verified by testing Ctrl+Z after inserting a tag pill.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@callmeYe callmeYe requested a review from adamdotdevin as a code owner April 13, 2026 03:59
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 13, 2026
@callmeYe callmeYe changed the title fix(app): use execCommand for tag pill insertion to preserve undo history fix(app): preserve undo history when inserting tag pills in prompt input Apr 13, 2026
@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

…tory

When inserting file/agent tag pills into the contenteditable prompt input,
direct DOM manipulation (range.insertNode) bypasses the browser's native
undo stack. Pressing Ctrl+Z after inserting a tag deletes the text before
the tag instead of removing the tag itself.

Switch to document.execCommand("insertHTML") which registers the operation
on the undo stack. Falls back to the previous range.insertNode approach
when execCommand is unavailable.

Closes anomalyco#22234
@callmeYe callmeYe force-pushed the fix/undo-tag-pill branch from a554c4b to a139127 Compare April 13, 2026 04:45
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 (Ctrl+Z) deletes text before tag pill instead of removing the tag

1 participant