Skip to content

Build your personal knowledge base with Trilium Notes

fix(autocomplete): drop a key held for a query the host has since replaced, and leave a composing Tab

Elian Doran38ba5f57eafa
diff --git a/apps/client/src/widgets/react/FormAutocomplete.spec.tsx b/apps/client/src/widgets/react/FormAutocomplete.spec.tsx
index 3df298a889..29f8d76756 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.spec.tsx
@@ -100,6 +100,38 @@ describe("Enter pressed before the newer query's suggestions", () => {
});
});
+describe("Enter pressed between typing and the render it causes", () => {
+ it("is held for the text just typed, and picks among its suggestions", async () => {
+ vi.useFakeTimers();
+ const onPick = vi.fn();
+ function Field() {
+ const [ value, setValue ] = useState("");
+ return <FormAutocomplete currentValue={value} onChange={setValue} onPick={onPick} autoActivate
+ source={async (query) => [ `${query}!` ]} />;
+ }
+ const found = renderInto(<Field />).querySelector("input");
+ if (!found) throw new Error("no input rendered");
+ const input: HTMLInputElement = found;
+
+ await act(async () => {
+ input.value = "a";
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+
+ // Outside `act`, the render for "ab" comes after the Enter.
+ input.value = "ab";
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));
+ // The first flushes the render and its effects, the second the lookup they debounce.
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+
+ expect(onPick).toHaveBeenCalledExactlyOnceWith("ab!");
+ vi.useRealTimers();
+ });
+});
+
describe("an input method composing in the field", () => {
it("looks up what the input method commits, not what it is composing, and passes the events on", async () => {
vi.useFakeTimers();
diff --git a/apps/client/src/widgets/react/FormAutocomplete.tsx b/apps/client/src/widgets/react/FormAutocomplete.tsx
index f5edd17e01..3aacfc4ebd 100644
--- a/apps/client/src/widgets/react/FormAutocomplete.tsx
+++ b/apps/client/src/widgets/react/FormAutocomplete.tsx
@@ -226,7 +226,7 @@ export function useForwardedKeys<T>(autocomplete: ReturnType<typeof useAutocompl
handleRef.current = {
handleKeyDown(e) {
if (!autocomplete.isShown) return false;
- if (e.key === "Tab" && autocomplete.takeHighlighted(e)) return true;
+ if (e.key === "Tab" && !e.isComposing && autocomplete.takeHighlighted(e)) return true;
autocomplete.handleKeyDown(e);
return e.defaultPrevented;
}
@@ -360,12 +360,16 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,
const isStale = useRef(false);
const keyHeld = useRef(false);
// A host that forwards its keys changes `query` without `handleInput`, and a key can arrive
- // before the lookup effect runs.
+ // before the lookup effect runs. A key held before such a change confirmed the text it replaced.
+ // After `handleInput`, a key held before this render confirmed the text it renders.
const renderedQuery = useRef(query);
+ const inputSinceRender = useRef(false);
if (renderedQuery.current !== query) {
renderedQuery.current = query;
isStale.current = true;
+ if (!inputSinceRender.current) keyHeld.current = false;
}
+ inputSinceRender.current = false;
const pickLatest = useRef(pick);
pickLatest.current = pick;
// Set when the keyboard or a new list moves the highlight, which the effect below scrolls into view.
@@ -547,6 +551,7 @@ export function useAutocomplete<T>({ query, source, onPick, inputRef, disabled,
isStale.current = true;
// A held key confirms the text it was pressed on, not text typed after it.
keyHeld.current = false;
+ inputSinceRender.current = true;
open();
},
handleFocus() {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index f109d6847f..a499b8021c 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -1218,6 +1218,29 @@ describe("NoteMentionList", () => {
}
});
+ it("drops a held key once the host's query changes again, and leaves a composing Tab alone", async () => {
+ const handleRef = createRef<AutocompleteListHandle>() as { current: AutocompleteListHandle | null };
+ const host = document.createElement("div");
+ const onPick = vi.fn();
+ const draw = (text: string) => preactRender(<NoteMentionList query={text} anchor={anchor} onPick={onPick} handleRef={handleRef} />, host);
+ const press = (key: string, init: KeyboardEventInit = {}) =>
+ handleRef.current?.handleKeyDown(new KeyboardEvent("keydown", { key, cancelable: true, ...init })) ?? false;
+
+ await act(async () => draw("al"));
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+ // An input method's Tab, while it composes, is the editor's.
+ expect(press("Tab", { isComposing: true })).toBe(false);
+
+ draw("be");
+ expect(press("Tab")).toBe(true);
+ // Typed on before the notes for "be" arrive, so the Tab confirmed text that is gone.
+ draw("bet");
+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });
+ expect(rows().length).toBeGreaterThan(0);
+ expect(onPick).not.toHaveBeenCalled();
+ await act(async () => preactRender(null, host));
+ });
+
it("takes the keys while it shows notes, and reports the path of the one Enter or a click picks", async () => {
const { onPick, press, unmount } = await show("al");