Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 48e8d227de test(app): cover undo after clicking a file mention 2026-12-02 17:56:19 +00:00
paseo-bot[bot] 4fc5b823d3 fix(app): restore undo for composer file mentions
Assigning the textarea value when completing a file mention clears its undo history. Use the browser editing command for focused composer edits so Undo can recover the draft that preceded the completion.
2026-12-02 16:32:17 +00:00
6 changed files with 111 additions and 15 deletions
@@ -8,6 +8,8 @@ import {
exitComposerFullscreen,
resizeComposerViewport,
selectComposerDraftRange,
undoComposerEdit,
redoComposerEdit,
} from "../support/helpers/composer";
import { openAgentRoute, seedMockAgentWorkspace } from "../support/helpers/mock-agent";
import { expectVisibleAgentSurfacesIdle } from "../support/helpers/agent-stream";
@@ -25,6 +27,10 @@ async function selectReadmeMention(page: Page): Promise<void> {
await expectComposerDraft(page, '"README.md"');
}
async function clickReadmeMention(page: Page): Promise<void> {
await fileSuggestions(page).getByText("README.md", { exact: true }).click();
}
async function sendEmailIntact(page: Page, email: string): Promise<void> {
await fillComposerDraft(page, email);
await expect(fileSuggestions(page)).not.toBeVisible();
@@ -42,6 +48,17 @@ test("Enter sends email formats intact and still completes file mentions", async
try {
await openAgentRoute(page, agent);
await expectComposerVisible(page);
await test.step("undo and redo a file mention picked with the mouse", async () => {
await fillComposerDraft(page, "Review @README");
await clickReadmeMention(page);
await expectComposerDraft(page, 'Review "README.md"');
await undoComposerEdit(page);
await expectComposerDraft(page, "Review @README");
await redoComposerEdit(page);
await expectComposerDraft(page, 'Review "README.md"');
});
for (const email of [
"user <12345+user@noreply.example.com>",
"alice@example.com",
@@ -2,11 +2,32 @@ import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef
import { TextInput } from "react-native";
import type { EditingTextInputHandle, EditingTextInputProps } from "./types";
interface WebTextInputElement extends TextInput {
value?: string;
setSelectionRange?: (start: number, end: number) => void;
addEventListener(type: "compositionstart" | "compositionend", listener: EventListener): void;
removeEventListener(type: "compositionstart" | "compositionend", listener: EventListener): void;
type WebTextInputElement = HTMLInputElement | HTMLTextAreaElement;
function replaceWithUndo(input: WebTextInputElement, previous: string, next: string): boolean {
let start = 0;
let previousEnd = previous.length;
let nextEnd = next.length;
while (start < previousEnd && start < nextEnd && previous[start] === next[start]) {
start++;
}
while (
previousEnd > start &&
nextEnd > start &&
previous[previousEnd - 1] === next[nextEnd - 1]
) {
previousEnd--;
nextEnd--;
}
// A DOM value assignment clears undo history. Use the same native edit as a paste.
input.setSelectionRange(start, previousEnd);
input.ownerDocument.execCommand(
start === nextEnd ? "delete" : "insertText",
false,
next.slice(start, nextEnd),
);
return input.value === next;
}
export const EditingTextInput = forwardRef<EditingTextInputHandle, EditingTextInputProps>(
@@ -29,7 +50,7 @@ export const EditingTextInput = forwardRef<EditingTextInputHandle, EditingTextIn
onChangeTextRef.current = onChangeText;
useEffect(() => {
const input = inputRef.current as WebTextInputElement | null;
const input = inputRef.current as unknown as WebTextInputElement | null;
if (!input) return;
const startComposition = () => {
@@ -64,26 +85,36 @@ export const EditingTextInput = forwardRef<EditingTextInputHandle, EditingTextIn
blur: () => inputRef.current?.blur(),
isFocused: () => document.activeElement === inputRef.current,
getText: () => {
const input = inputRef.current as WebTextInputElement | null;
const input = inputRef.current as unknown as WebTextInputElement | null;
const nextText = input?.value ?? textRef.current;
textRef.current = nextText;
return nextText;
},
replaceText: (nextText, selection) => {
replaceText: (nextText, selection, options) => {
// The caller publishes this edit. Suppress the synchronous input event from the command.
textRef.current = nextText;
const input = inputRef.current as WebTextInputElement | null;
if (input && "value" in input) input.value = nextText;
const input = inputRef.current as unknown as WebTextInputElement | null;
if (input && typeof input.value === "string" && input.value !== nextText) {
if (
!options?.undoable ||
isComposingRef.current ||
document.activeElement !== input ||
!replaceWithUndo(input, input.value, nextText)
) {
input.value = nextText;
}
}
if (selection && typeof input?.setSelectionRange === "function") {
input.setSelectionRange(selection.start, selection.end);
}
},
setSelection: (selection) => {
const input = inputRef.current as WebTextInputElement | null;
const input = inputRef.current as unknown as WebTextInputElement | null;
input?.setSelectionRange?.(selection.start, selection.end);
},
reset: () => {
textRef.current = "";
const input = inputRef.current as WebTextInputElement | null;
const input = inputRef.current as unknown as WebTextInputElement | null;
if (input && "value" in input) input.value = "";
},
getNativeRef: () => inputRef.current,
@@ -7,7 +7,11 @@ export interface EditingTextInputHandle {
blur(): void;
isFocused(): boolean;
getText(): string;
replaceText(text: string, selection?: { start: number; end: number }): void;
replaceText(
text: string,
selection?: { start: number; end: number },
options?: { undoable?: boolean },
): void;
setSelection(selection: { start: number; end: number }): void;
/** Clear the editor and reset its intrinsic layout, preserving focus intent. */
reset(): void;
+7 -1
View File
@@ -1041,7 +1041,13 @@ function ComposerAutocompleteBinding({
]);
const selectOption = autocomplete.onSelectOption;
const onSelect = useCallback(
(option: AutocompleteOption) => selectOption(option, inputRef.current?.getInputSnapshot()),
(option: AutocompleteOption) => {
const input = inputRef.current;
const snapshot = input?.getInputSnapshot();
// The clicked suggestion takes focus. Restore it before applying the edit.
input?.focus();
selectOption(option, snapshot);
},
[selectOption, inputRef],
);
return (
+1 -1
View File
@@ -1506,7 +1506,7 @@ export const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
if (nextText === "") {
textInputRef.current?.reset();
} else {
textInputRef.current?.replaceText(nextText, selectionRef.current);
textInputRef.current?.replaceText(nextText, selectionRef.current, { undoable: true });
}
onChangeText(nextText);
// iOS does not emit a selection event for a programmatic text replacement.
@@ -81,6 +81,44 @@ afterEach(() => {
}
});
it("undoes and redoes a programmatic edit in the focused composer", () => {
const recorder = createTextRecorder();
const { inputRef, textarea } = mountInput(recorder.onChangeText);
const input = inputRef.current!;
const query = "Review @README";
const mention = 'Review "README.md" ';
act(() => {
input.focus();
document.execCommand("insertText", false, query);
});
expect(textarea.value).toBe(query);
act(() =>
input.replaceText(mention, { start: mention.length, end: mention.length }, { undoable: true }),
);
expect(textarea.value).toBe(mention);
expect(textarea.selectionStart).toBe(mention.length);
expect(recorder.changes).toEqual([query]);
act(() => {
document.execCommand("undo");
});
expect(textarea.value).toBe(query);
expect(recorder.changes).toEqual([query, query]);
act(() => {
document.execCommand("redo");
});
expect(textarea.value).toBe(mention);
expect(recorder.changes).toEqual([query, query, mention]);
act(() => {
document.execCommand("undo");
document.execCommand("undo");
});
expect(textarea.value).toBe("");
});
describe("ComposerTextInput web IME composition", () => {
it("resets the composer text while preserving focus", () => {
const mounted = mountInput(ignoreTextChange);