Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): lay a search query out over several lines

Elian Doran695045e59517
diff --git a/apps/client/src/widgets/ribbon/SearchStringEditor.css b/apps/client/src/widgets/ribbon/SearchStringEditor.css
index bbe97dbc0c..46cc3e789a 100644
--- a/apps/client/src/widgets/ribbon/SearchStringEditor.css
+++ b/apps/client/src/widgets/ribbon/SearchStringEditor.css
@@ -2,6 +2,8 @@
.search-string-editor {
.cm-editor {
background: transparent;
+ /* Past this the query scrolls, rather than growing the ribbon without bound. */
+ max-height: 12em;
}
.cm-editor.cm-focused {
@@ -12,6 +14,8 @@
font-family: inherit;
font-size: inherit;
line-height: inherit;
+ /* Pairs with the max-height above, which CodeMirror only scrolls against an overflow here. */
+ overflow: auto;
}
.cm-content {
diff --git a/apps/client/src/widgets/ribbon/SearchStringEditor.tsx b/apps/client/src/widgets/ribbon/SearchStringEditor.tsx
index 1173c3891d..aefaad1f8f 100644
--- a/apps/client/src/widgets/ribbon/SearchStringEditor.tsx
+++ b/apps/client/src/widgets/ribbon/SearchStringEditor.tsx
@@ -1,8 +1,8 @@
import "./SearchStringEditor.css";
import type { SearchLintMessages } from "@triliumnext/codemirror/src/extensions/trilium_search_lint";
+import type { FieldEditor } from "@triliumnext/codemirror/src/field_editor";
import type { SearchLintResponse } from "@triliumnext/commons";
-import type { SingleLineEditor } from "@triliumnext/codemirror/src/single_line";
import clsx from "clsx";
import { useEffect, useRef } from "preact/hooks";
@@ -23,14 +23,15 @@ interface SearchStringEditorProps {
}
/**
- * Edits the `#searchString` of a saved search in a one-line CodeMirror editor.
+ * Edits the `#searchString` of a saved search in a CodeMirror editor. Enter runs the search and
+ * Shift-Enter starts a new line, so a long query can be laid out over several of them.
*
* CodeMirror loads on demand: `RibbonDefinition` imports the tab holding this component
* statically, so a static import here would put the editor in the initial bundle.
*/
export default function SearchStringEditor({ currentValue, noteId, placeholder, className, autoFocus, onChange, onEnter }: SearchStringEditorProps) {
const parentRef = useRef<HTMLDivElement>(null);
- const editorRef = useRef<SingleLineEditor>();
+ const editorRef = useRef<FieldEditor>();
// The editor is built once, so it reaches the current props through a ref rather than
// through the closure of the render that created it.
const propsRef = useRef({ currentValue, onChange, onEnter });
@@ -40,19 +41,19 @@ export default function SearchStringEditor({ currentValue, noteId, placeholder,
const isAdopting = useRef(false);
useEffect(() => {
- let editor: SingleLineEditor | undefined;
+ let editor: FieldEditor | undefined;
let cancelled = false;
void Promise.all([
- import("@triliumnext/codemirror/src/single_line"),
+ import("@triliumnext/codemirror/src/field_editor"),
import("@triliumnext/codemirror/src/extensions/trilium_search_highlighter"),
import("@triliumnext/codemirror/src/extensions/trilium_search_lint")
- ]).then(([ { createSingleLineEditor }, { triliumSearchHighlighter }, { triliumSearchLinter } ]) => {
+ ]).then(([ { createFieldEditor }, { triliumSearchHighlighter }, { triliumSearchLinter } ]) => {
if (cancelled || !parentRef.current) {
return;
}
- editor = createSingleLineEditor({
+ editor = createFieldEditor({
parent: parentRef.current,
doc: propsRef.current.currentValue,
placeholder,
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index 48a3b9310d..dc11fa5e33 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -1,4 +1,4 @@
-import type { CompletionContext, CompletionResult } from "@triliumnext/codemirror/src/single_line";
+import type { CompletionContext, CompletionResult } from "@triliumnext/codemirror/src/field_editor";
import { beforeEach, describe, expect, it, vi } from "vitest";
import server from "../../services/server";
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index 1e01315826..b6be5d0373 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -1,4 +1,4 @@
-import type { Completion, CompletionContext, CompletionResult } from "@triliumnext/codemirror/src/single_line";
+import type { Completion, CompletionContext, CompletionResult } from "@triliumnext/codemirror/src/field_editor";
import { ALLOWED_NOTE_TYPES, allowedSearchOperators, MIME_TYPES_DICT, SEARCH_NOTE_PATH, SEARCH_NOTE_PATH_SEGMENTS } from "@triliumnext/commons";
import { isBuiltinAttribute } from "../../services/attributes";
diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts
index bfbe074428..60b4e5c499 100644
--- a/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts
+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { createSingleLineEditor } from "../single_line.js";
+import { createFieldEditor } from "../field_editor.js";
import { tokenizeSearchQuery, triliumSearchHighlighter } from "./trilium_search_highlighter.js";
describe("tokenizeSearchQuery", () => {
@@ -89,7 +89,7 @@ describe("triliumSearchHighlighter", () => {
it("marks the tokens in the rendered editor", () => {
const parent = document.createElement("div");
document.body.appendChild(parent);
- const editor = createSingleLineEditor({
+ const editor = createFieldEditor({
parent,
doc: "#book or ~author.title = 'Tolkien'",
extensions: [ triliumSearchHighlighter ]
diff --git a/packages/codemirror/src/field_editor.spec.ts b/packages/codemirror/src/field_editor.spec.ts
new file mode 100644
index 0000000000..238a85cbb4
--- /dev/null
+++ b/packages/codemirror/src/field_editor.spec.ts
@@ -0,0 +1,77 @@
+import { EditorSelection } from "@codemirror/state";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { createFieldEditor, type FieldEditor, type FieldEditorConfig } from "./field_editor.js";
+
+let editor: FieldEditor | undefined;
+
+afterEach(() => {
+ editor?.destroy();
+ editor = undefined;
+});
+
+describe("createFieldEditor", () => {
+ it("mounts with the given document and reports every change", () => {
+ const onChange = vi.fn();
+ const parent = document.createElement("div");
+ document.body.appendChild(parent);
+ editor = createFieldEditor({ parent, doc: "#book", onChange });
+
+ expect(parent.contains(editor.dom)).toBe(true);
+ expect(editor.state.doc.toString()).toBe("#book");
+ // The initial document is not a change.
+ expect(onChange).not.toHaveBeenCalled();
+
+ editor.dispatch({ changes: { from: 5, insert: " #year = 1954" } });
+
+ expect(onChange).toHaveBeenCalledWith("#book #year = 1954");
+ });
+
+ it("treats Enter as a command rather than a line break", () => {
+ const onEnter = vi.fn();
+ editor = build({ doc: "#book", onEnter });
+ editor.dispatch({ selection: EditorSelection.cursor(5) });
+
+ expect(pressEnter(editor)).toBe(true);
+ expect(onEnter).toHaveBeenCalledOnce();
+ expect(editor.state.doc.toString()).toBe("#book");
+ });
+
+ it("breaks the line on Shift-Enter, keeping the indentation", () => {
+ const onEnter = vi.fn();
+ const onChange = vi.fn();
+ editor = build({ doc: "#book\n and #author", onEnter, onChange });
+ editor.dispatch({ selection: EditorSelection.cursor(editor.state.doc.length) });
+
+ expect(pressEnter(editor, { shiftKey: true })).toBe(true);
+ expect(onEnter).not.toHaveBeenCalled();
+ expect(editor.state.doc.toString()).toBe("#book\n and #author\n ");
+ expect(onChange).toHaveBeenLastCalledWith("#book\n and #author\n ");
+ });
+
+ it("keeps the line breaks in inserted text", () => {
+ const onChange = vi.fn();
+ editor = build({ onChange });
+
+ editor.dispatch({ changes: { from: 0, insert: "#book\n #year = 1954" } });
+
+ expect(editor.state.doc.lines).toBe(2);
+ expect(onChange).toHaveBeenLastCalledWith("#book\n #year = 1954");
+ });
+});
+
+function build(config: Partial<FieldEditorConfig> = {}): FieldEditor {
+ const parent = document.createElement("div");
+ document.body.appendChild(parent);
+ return createFieldEditor({ parent, ...config });
+}
+
+/** Presses Enter on the editor and answers whether a binding handled it. */
+function pressEnter(editor: FieldEditor, init: KeyboardEventInit = {}) {
+ return !editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
+ key: "Enter",
+ bubbles: true,
+ cancelable: true,
+ ...init
+ }));
+}
diff --git a/packages/codemirror/src/single_line.ts b/packages/codemirror/src/field_editor.ts
similarity index 72%
rename from packages/codemirror/src/single_line.ts
rename to packages/codemirror/src/field_editor.ts
index 7277f3ad5b..635aadbbab 100644
--- a/packages/codemirror/src/single_line.ts
+++ b/packages/codemirror/src/field_editor.ts
@@ -1,15 +1,15 @@
import { autocompletion, type Completion, type CompletionSource } from "@codemirror/autocomplete";
import { history, historyKeymap, standardKeymap } from "@codemirror/commands";
-import { EditorState, type Extension } from "@codemirror/state";
+import type { Extension } from "@codemirror/state";
import { EditorView, keymap, placeholder } from "@codemirror/view";
/**
- * The types a consumer needs to write a {@link SingleLineEditorConfig.completionSource}, re-exported
+ * The types a consumer needs to write a {@link FieldEditorConfig.completionSource}, re-exported
* so it does not have to depend on `@codemirror/autocomplete` itself.
*/
export type { Completion, CompletionContext, CompletionResult, CompletionSource } from "@codemirror/autocomplete";
-export interface SingleLineEditorConfig {
+export interface FieldEditorConfig {
parent: HTMLElement;
/** Text the editor starts with. */
doc?: string;
@@ -30,25 +30,28 @@ export interface SingleLineEditorConfig {
activateOnCompletion?(completion: Completion): boolean;
/** Runs after every document change, with the whole text. */
onChange?(value: string): void;
- /** Runs when Enter is pressed; the key never inserts a line break. */
+ /** Runs when Enter is pressed; Shift-Enter inserts a line break instead. */
onEnter?(): void;
}
-/** The editor {@link createSingleLineEditor} returns. */
-export type SingleLineEditor = EditorView;
+/** The editor {@link createFieldEditor} returns. */
+export type FieldEditor = EditorView;
/**
- * Builds a CodeMirror editor for a one-line value: text editing, undo history and wrapped
+ * Builds a CodeMirror editor for a form field's value: text editing, undo history and wrapped
* display, and nothing else — no gutter and no language. Highlighting and completion are the
* consumer's to supply, through `extensions` and `completionSource`.
*
+ * Enter runs `onEnter` instead of inserting a line break, so the field answers the key the way an
+ * input does. Shift-Enter, which `standardKeymap` binds to `insertNewlineAndIndent`, breaks the
+ * line and keeps its indentation, for a value the user lays out over several of them.
+ *
* It sits apart from the editor in `index.ts` so a consumer that wants a plain input does not
* load the language, theme and completion machinery a code note needs.
*/
-export function createSingleLineEditor(config: SingleLineEditorConfig): SingleLineEditor {
+export function createFieldEditor(config: FieldEditorConfig): FieldEditor {
const extensions: Extension[] = [
EditorView.lineWrapping,
- collapseLineBreaks,
history(),
keymap.of([
{
@@ -95,7 +98,7 @@ export function createSingleLineEditor(config: SingleLineEditorConfig): SingleLi
});
}
-/** Where CodeMirror draws its own icons, which {@link SingleLineEditorConfig.completionIcon} takes over. */
+/** Where CodeMirror draws its own icons, which {@link FieldEditorConfig.completionIcon} takes over. */
const ICON_POSITION = 20;
function renderIcon(classes: string | undefined) {
@@ -110,21 +113,3 @@ function renderIcon(classes: string | undefined) {
return element;
}
-
-/**
- * Keeps the document on one line. Typing a line break is already impossible because Enter is
- * bound, so this covers text arriving by paste or drop: every run of whitespace holding a line
- * break collapses to a single space.
- */
-const collapseLineBreaks = EditorState.transactionFilter.of((tr) => {
- if (!tr.docChanged || tr.newDoc.lines === 1) {
- return tr;
- }
-
- const flattened = tr.newDoc.toString().replace(/\s*\n\s*/g, " ");
-
- return {
- changes: { from: 0, to: tr.startState.doc.length, insert: flattened },
- selection: { anchor: Math.min(tr.newSelection.main.head, flattened.length) }
- };
-});
diff --git a/packages/codemirror/src/single_line.spec.ts b/packages/codemirror/src/single_line.spec.ts
deleted file mode 100644
index f3ebe14b1f..0000000000
--- a/packages/codemirror/src/single_line.spec.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-import { EditorSelection } from "@codemirror/state";
-import { afterEach, describe, expect, it, vi } from "vitest";
-
-import { createSingleLineEditor, type SingleLineEditor, type SingleLineEditorConfig } from "./single_line.js";
-
-let editor: SingleLineEditor | undefined;
-
-afterEach(() => {
- editor?.destroy();
- editor = undefined;
-});
-
-describe("createSingleLineEditor", () => {
- it("mounts with the given document and reports every change", () => {
- const onChange = vi.fn();
- const parent = document.createElement("div");
- document.body.appendChild(parent);
- editor = createSingleLineEditor({ parent, doc: "#book", onChange });
-
- expect(parent.contains(editor.dom)).toBe(true);
- expect(editor.state.doc.toString()).toBe("#book");
- // The initial document is not a change.
- expect(onChange).not.toHaveBeenCalled();
-
- editor.dispatch({ changes: { from: 5, insert: " #year = 1954" } });
-
- expect(onChange).toHaveBeenCalledWith("#book #year = 1954");
- });
-
- it("treats Enter as a command rather than a line break", () => {
- const onEnter = vi.fn();
- editor = build({ doc: "#book", onEnter });
- editor.dispatch({ selection: EditorSelection.cursor(5) });
-
- const handled = !editor.contentDOM.dispatchEvent(new KeyboardEvent("keydown", {
- key: "Enter",
- bubbles: true,
- cancelable: true
- }));
-
- expect(handled).toBe(true);
- expect(onEnter).toHaveBeenCalledOnce();
- expect(editor.state.doc.toString()).toBe("#book");
- });
-
- it("collapses line breaks in inserted text, keeping the document on one line", () => {
- const onChange = vi.fn();
- editor = build({ onChange });
-
- editor.dispatch({ changes: { from: 0, insert: "#book\r\n #year = 1954\nnote.title *=* a" } });
-
- expect(editor.state.doc.lines).toBe(1);
- expect(editor.state.doc.toString()).toBe("#book #year = 1954 note.title *=* a");
- expect(onChange).toHaveBeenLastCalledWith("#book #year = 1954 note.title *=* a");
- // The caret stays within the shortened document.
- expect(editor.state.selection.main.head).toBeLessThanOrEqual(editor.state.doc.length);
- });
-});
-
-function build(config: Partial<SingleLineEditorConfig> = {}): SingleLineEditor {
- const parent = document.createElement("div");
- document.body.appendChild(parent);
- return createSingleLineEditor({ parent, ...config });
-}