Skip to content

Commit e2dfe15

Browse files
committed
fix(user-input): fall back to setRangeText when execCommand delete is unsupported (Firefox)
1 parent c2624ad commit e2dfe15

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/user-input/hooks

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/user-input/hooks/use-mention-tokens.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,19 @@ export function useMentionTokens({
134134
? range.end + (after.length - after.replace(/^ +/, '').length)
135135
: range.end
136136

137-
// Delete via `execCommand` (not `setMessage`/`setRangeText`) so the
138-
// removal lands on the browser's native undo stack and Cmd+Z restores the
139-
// chip — empirically the only primitive that preserves undo in Chromium.
140-
// `execCommand` is deprecated but remains the sole API wired to undo.
137+
// Prefer `execCommand` (deprecated, but the only primitive that lands the
138+
// removal on the native undo stack, so Cmd+Z restores the chip). It's a
139+
// no-op on Firefox textareas and returns false there, so fall back to a
140+
// direct edit — correct deletion, just no native undo on that browser.
141141
textarea.focus()
142142
textarea.setSelectionRange(range.start, deleteEnd)
143-
document.execCommand('delete')
143+
const valueBeforeDelete = textarea.value
144+
if (!document.execCommand('delete') || textarea.value === valueBeforeDelete) {
145+
textarea.setRangeText('', range.start, deleteEnd, 'start')
146+
}
144147

145-
// `execCommand` fires `input`, but sync state explicitly so this hook
146-
// doesn't depend on the consumer's onChange wiring.
148+
// The edit fires `input`, but sync state explicitly so this hook doesn't
149+
// depend on the consumer's onChange wiring.
147150
setMessage(textarea.value)
148151

149152
setTimeout(() => {

0 commit comments

Comments
 (0)