Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): keep Enter from completing a plain search term

Elian Doranc837e034d5ca
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
index 4df8c854b8..4f79044360 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
@@ -91,6 +91,9 @@
<h3>Autocomplete</h3>
<p>To help with the syntax, Trilium offers an autocomplete functionality
which can be triggered by pressing <kbd>Ctrl</kbd>+<kbd>Space</kbd>.</p>
+<p>No suggestion is selected when the list opens, so <kbd>Enter</kbd> runs
+ the search as typed. To insert a suggestion, select it with <kbd>Down</kbd> and
+ press <kbd>Enter</kbd>.</p>
<p>The autocomplete offers:</p>
<ul>
<li>
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
index 1e5cbe8fe2..1d9c6c8feb 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
@@ -50,6 +50,8 @@ To search for notes, click on the magnifying glass icon on the toolbar or press
To help with the syntax, Trilium offers an autocomplete functionality which can be triggered by pressing <kbd>Ctrl</kbd>+<kbd>Space</kbd>.
+No suggestion is selected when the list opens, so <kbd>Enter</kbd> runs the search as typed. To insert a suggestion, select it with <kbd>Down</kbd> and press <kbd>Enter</kbd>.
+
The autocomplete offers:
* Basic operators such as `*=` and keywords (`limit`, `not`).
diff --git a/packages/codemirror/src/field_editor.spec.ts b/packages/codemirror/src/field_editor.spec.ts
index e2158e4161..00fedf24fb 100644
--- a/packages/codemirror/src/field_editor.spec.ts
+++ b/packages/codemirror/src/field_editor.spec.ts
@@ -106,6 +106,30 @@ describe("createFieldEditor", () => {
expect(onEscape).toHaveBeenCalledOnce();
});
+ it("selects no completion on opening, so Enter runs the field's command until one is picked", async () => {
+ const onEnter = vi.fn();
+ const view = build({
+ doc: "a",
+ completionSource: () => ({ from: 0, options: [ { label: "and" }, { label: "asc" } ] }),
+ onEnter
+ });
+ editor = view;
+ view.dispatch({ selection: EditorSelection.cursor(1) });
+
+ await openCompletion(view);
+ await waitOutInteractionDelay();
+ pressKey(view, "Enter");
+ expect(onEnter).toHaveBeenCalledOnce();
+ expect(view.state.doc.toString()).toBe("a");
+
+ await openCompletion(view);
+ await waitOutInteractionDelay();
+ pressKey(view, "ArrowDown");
+ pressKey(view, "Enter");
+ expect(onEnter).toHaveBeenCalledOnce();
+ expect(view.state.doc.toString()).toBe("and");
+ });
+
it("holds a single-line field to one line, flattening the breaks in what is inserted", () => {
const onChange = vi.fn();
editor = build({ singleLine: true, onChange });
@@ -192,6 +216,11 @@ async function openCompletion(view: FieldEditor) {
await vi.waitFor(() => expect(completionStatus(view.state)).toBe("active"));
}
+/** The popup drops the keys pressed within `interactionDelay` (75ms by default) of opening. */
+function waitOutInteractionDelay() {
+ return new Promise((resolve) => setTimeout(resolve, 100));
+}
+
/** Presses a key on the editor and answers whether a binding handled it. */
function pressKey(editor: FieldEditor, key: string, init: KeyboardEventInit = {}) {
return !editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
diff --git a/packages/codemirror/src/field_editor.ts b/packages/codemirror/src/field_editor.ts
index 9f432cc429..fba57069f2 100644
--- a/packages/codemirror/src/field_editor.ts
+++ b/packages/codemirror/src/field_editor.ts
@@ -114,9 +114,12 @@ export function createFieldEditor(config: FieldEditorConfig): FieldEditor {
// The completion keymap is registered at the highest precedence, so Enter picks the
// selected option while the popup is open and reaches `onEnter` the rest of the time.
+ // Nothing is selected as the popup opens, so Enter on a plain search term runs the search
+ // instead of completing the term; ArrowDown selects an option first.
extensions.push(autocompletion({
override: [ config.completionSource ],
activateOnTyping: true,
+ selectOnOpen: false,
activateOnCompletion: config.activateOnCompletion,
icons: !icon,
addToOptions: icon ? [ { position: ICON_POSITION, render: (completion) => renderIcon(icon(completion)) } ] : []