Skip to content

Build your personal knowledge base with Trilium Notes

refactor(autocomplete): list into the host's container for the empty tab and Jump to Note

Elian Doran3465249beadf
diff --git a/apps/client/src/services/note_autocomplete_bak.spec.ts b/apps/client/src/services/note_autocomplete_bak.spec.ts
index 2d995e37ff..0e1837f7a7 100644
--- a/apps/client/src/services/note_autocomplete_bak.spec.ts
+++ b/apps/client/src/services/note_autocomplete_bak.spec.ts
@@ -434,14 +434,6 @@ describe("initNoteAutocomplete wiring", () => {
expect($group.find(".go-to-selected-note-button").length).toBe(0);
});
- it("passes a container into the autocomplete config and enables debug", () => {
- const container = document.createElement("div");
- const $el = makeEl();
- noteAutocomplete.initNoteAutocomplete($el, { container });
- expect(lastConfig?.dropdownMenuContainer).toBe(container);
- expect(lastConfig?.debug).toBe(true);
- });
-
it("Ctrl+Enter triggers a search-notes selection when allowJumpToSearchNotes", () => {
const $el = makeEl();
const selected = vi.fn();
diff --git a/apps/client/src/services/note_autocomplete_bak.ts b/apps/client/src/services/note_autocomplete_bak.ts
index 638445efb3..37216c3586 100644
--- a/apps/client/src/services/note_autocomplete_bak.ts
+++ b/apps/client/src/services/note_autocomplete_bak.ts
@@ -164,12 +164,6 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
return false;
});
- const autocompleteOptions: AutoCompleteConfig = {};
- if (options.container) {
- autocompleteOptions.dropdownMenuContainer = options.container;
- autocompleteOptions.debug = true; // don't close on blur
- }
-
if (options.allowJumpToSearchNotes) {
$el.on("keydown", (event) => {
if (event.ctrlKey && event.key === "Enter") {
@@ -203,7 +197,6 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
$el.autocomplete(
{
- ...autocompleteOptions,
appendTo: document.querySelector("body"),
hint: false,
autoselect: true,
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.css
index fea5deae8c..548b7da904 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.css
+++ b/apps/client/src/widgets/react/NoteAutocomplete.css
@@ -41,7 +41,6 @@
}
.algolia-autocomplete .aa-dropdown-menu {
- display: block;
width: 100%;
background-color: var(--main-background-color);
border: 1px solid var(--main-border-color);
@@ -52,7 +51,9 @@
margin: 0;
}
-.algolia-autocomplete .aa-suggestions {
+/* In a popup and in a host's container alike, as `autocomplete.js` set them inline. */
+span.aa-dropdown-menu,
+span.aa-dropdown-menu .aa-suggestions {
display: block;
}
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index c3b6e00ff5..11a8cd24c1 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -291,6 +291,29 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(document.activeElement).toBe(input);
});
+ it("lists into the host's container, open past a blur until Escape", async () => {
+ const host = document.createElement("div");
+ document.body.append(host);
+ const input = await mount({ container: { current: host } });
+ await type(input, "a");
+
+ const menu = host.querySelector<HTMLElement>(":scope > span.aa-dropdown-menu");
+ expect(menu?.querySelectorAll(".aa-suggestion")).toHaveLength(2);
+ // In the host's flow, as `autocomplete.js` left it: its contained menu had only `display: block`.
+ expect(menu?.hasAttribute("style")).toBe(false);
+ expect(document.querySelector(".algolia-autocomplete")).toBeNull();
+
+ await act(async () => {
+ input.focus();
+ input.blur();
+ });
+ expect(host.querySelectorAll(".aa-suggestion")).toHaveLength(2);
+
+ await press(input, "Escape");
+ expect(host.querySelector(".aa-dropdown-menu")).toBeNull();
+ host.remove();
+ });
+
it("spans the whole field, the buttons included", async () => {
const input = await mount();
const group = input.closest(".input-group");
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 8cee381b5b..5db77c644a 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -2,7 +2,7 @@ import "./NoteAutocomplete.css";
import clsx from "clsx";
import type { RefObject } from "preact";
-import type { CSSProperties } from "preact/compat";
+import { createPortal, type CSSProperties } from "preact/compat";
import { type MutableRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
import froca from "../../services/froca";
@@ -41,7 +41,7 @@ export interface NoteAutocompleteHandle {
showRecentNotes(): void;
}
-export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, tabIndex, handleRef }: NoteAutocompleteProps) {
+export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, tabIndex, handleRef }: NoteAutocompleteProps) {
const inputRef = useSyncedRef<HTMLInputElement>(externalInputRef);
const groupRef = useRef<HTMLDivElement>(null);
const [ value, setValue ] = useState("");
@@ -134,7 +134,8 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
onKeyDown?.(e);
}}
onBlur={() => {
- autocomplete.handleBlur();
+ // A list in the host's container stays open until Escape or a pick.
+ if (!container) autocomplete.handleBlur();
onBlur?.(value.trim() ? lastSegment(notePath) : "");
}}
/>
@@ -164,48 +165,59 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
/>
</>}
- {autocomplete.isShown && groupRef.current && (
- <Popup
- anchor={groupRef.current}
- placement="bottom-start"
- capHeight={false}
- className="algolia-autocomplete"
- // The list spans the whole field, buttons included.
- style={{ width: `${groupRef.current.getBoundingClientRect().width}px` }}
- escapeDismisses={false}
- onDismiss={autocomplete.close}
- >
- <span
- className="aa-dropdown-menu"
- role="listbox"
- // Keeps the input focused, so its blur does not close the list before the
- // click lands on a suggestion.
- onMouseDown={(e) => e.preventDefault()}
+ {autocomplete.isShown && (container
+ ? container.current && createPortal(
+ <NoteSuggestionList autocomplete={autocomplete} />,
+ container.current)
+ : groupRef.current && (
+ <Popup
+ anchor={groupRef.current}
+ placement="bottom-start"
+ capHeight={false}
+ className="algolia-autocomplete"
+ // The list spans the whole field, buttons included.
+ style={{ width: `${groupRef.current.getBoundingClientRect().width}px` }}
+ escapeDismisses={false}
+ onDismiss={autocomplete.close}
>
- <div className="aa-dataset-0">
- <span className="aa-suggestions">
- {autocomplete.items.map((suggestion, index) => (
- <div
- key={`${suggestion.action ?? ""}:${suggestion.notePath ?? index}`}
- id={autocomplete.itemId(index)}
- className={clsx("aa-suggestion", index === autocomplete.activeIndex && "aa-cursor")}
- role="option"
- aria-selected={index === autocomplete.activeIndex}
- onMouseEnter={() => autocomplete.setActiveIndex(index)}
- onClick={() => autocomplete.pick(suggestion)}
- >
- <NoteSuggestion suggestion={suggestion} />
- </div>
- ))}
- </span>
- </div>
- </span>
- </Popup>
- )}
+ <NoteSuggestionList autocomplete={autocomplete} />
+ </Popup>
+ ))}
</div>
);
}
+/** The list of suggestions, in the markup of `autocomplete.js`. */
+function NoteSuggestionList({ autocomplete }: { autocomplete: ReturnType<typeof useAutocomplete<Suggestion>> }) {
+ return (
+ <span
+ className="aa-dropdown-menu"
+ role="listbox"
+ // Keeps the input focused, so its blur does not close the list before the click lands
+ // on a suggestion.
+ onMouseDown={(e) => e.preventDefault()}
+ >
+ <div className="aa-dataset-0">
+ <span className="aa-suggestions">
+ {autocomplete.items.map((suggestion, index) => (
+ <div
+ key={`${suggestion.action ?? ""}:${suggestion.notePath ?? index}`}
+ id={autocomplete.itemId(index)}
+ className={clsx("aa-suggestion", index === autocomplete.activeIndex && "aa-cursor")}
+ role="option"
+ aria-selected={index === autocomplete.activeIndex}
+ onMouseEnter={() => autocomplete.setActiveIndex(index)}
+ onClick={() => autocomplete.pick(suggestion)}
+ >
+ <NoteSuggestion suggestion={suggestion} />
+ </div>
+ ))}
+ </span>
+ </div>
+ </span>
+ );
+}
+
/** One row of the list, in the markup of the jQuery plugin's suggestion template. */
function NoteSuggestion({ suggestion }: { suggestion: Suggestion }) {
return (
diff --git a/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx b/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx
index 661a252f46..91f6febeab 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx
@@ -3,7 +3,7 @@ import note_autocomplete from "../../services/note_autocomplete";
import { useSyncedRef } from "./hooks";
import type { NoteAutocompleteProps } from "./NoteAutocomplete";
-export default function NoteAutocomplete({ inputRef: externalInputRef, text, onChange, container, opts, noteId, noteIdChanged }: NoteAutocompleteProps) {
+export default function NoteAutocomplete({ inputRef: externalInputRef, text, onChange, opts, noteId, noteIdChanged }: NoteAutocompleteProps) {
const ref = useSyncedRef<HTMLInputElement>(externalInputRef);
useEffect(() => {
@@ -15,11 +15,8 @@ export default function NoteAutocomplete({ inputRef: externalInputRef, text, onC
.off("autocomplete:noteselected")
.off("autocomplete:commandselected")
- note_autocomplete.initNoteAutocomplete($autoComplete, {
- ...opts,
- container: container?.current
- });
- }, [opts, container?.current]);
+ note_autocomplete.initNoteAutocomplete($autoComplete, opts);
+ }, [opts]);
// On change event handlers.
useEffect(() => {
@@ -44,7 +41,7 @@ export default function NoteAutocomplete({ inputRef: externalInputRef, text, onC
.off("autocomplete:commandselected", autoCompleteListener);
};
}
- }, [opts, container?.current, onChange, noteIdChanged])
+ }, [opts, onChange, noteIdChanged])
useEffect(() => {
if (!ref.current || noteId || !text) return;