Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(search): hold the quick search query to one line
Elian Doran9439a85cbd30
diff --git a/apps/client/src/widgets/quick_search.ts b/apps/client/src/widgets/quick_search.tsindex ed7df520d3..7119ff3b0a 100644--- a/apps/client/src/widgets/quick_search.ts+++ b/apps/client/src/widgets/quick_search.ts@@ -30,10 +30,14 @@ const TPL = /*html*/`min-width: 0;}- /* The results list sits right below the field, so extra lines scroll inside it. */.quick-search .search-string .cm-editor {width: 100%;- max-height: 100%;+ }++ /* Outweighs the pre-wrap the shared stylesheet sets, which would wrap a long query onto a+ second visual line and grow the field. */+ .quick-search .search-string .cm-line {+ white-space: pre;}.quick-search .dropdown-menu {@@ -236,6 +240,7 @@ export default class QuickSearchWidget extends BasicWidget {this.editor = createSearchFieldEditor({parent: this.$searchField[0],placeholder: t("quick-search.placeholder"),+ singleLine: true,onEnter: () => this.runSearch(),onArrowDown: () => this.focusFirstResult(),onEscape: () => this.closeDropdown()diff --git a/packages/codemirror/src/field_editor.spec.ts b/packages/codemirror/src/field_editor.spec.tsindex 658fe7ccda..cd99b440e9 100644--- a/packages/codemirror/src/field_editor.spec.ts+++ b/packages/codemirror/src/field_editor.spec.ts@@ -106,6 +106,22 @@ describe("createFieldEditor", () => {expect(onEscape).toHaveBeenCalledOnce();});+ 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 });++ editor.dispatch({ changes: { from: 0, insert: "#book\n #year = 1954" } });++ expect(editor.state.doc.lines).toBe(1);+ expect(editor.state.doc.toString()).toBe("#book #year = 1954");+ expect(onChange).toHaveBeenLastCalledWith("#book #year = 1954");+ expect(editor.state.selection.main.head).toBe(editor.state.doc.length);++ // Shift-Enter is swallowed, rather than reaching the field as the flattened indentation.+ expect(pressKey(editor, "Enter", { shiftKey: true })).toBe(true);+ expect(editor.state.doc.toString()).toBe("#book #year = 1954");+ });+it("keeps the line breaks in inserted text", () => {const onChange = vi.fn();editor = build({ onChange });diff --git a/packages/codemirror/src/field_editor.ts b/packages/codemirror/src/field_editor.tsindex 1bcd5f91ed..1f1b800837 100644--- a/packages/codemirror/src/field_editor.ts+++ b/packages/codemirror/src/field_editor.ts@@ -1,6 +1,6 @@import { autocompletion, type Completion, completionStatus, type CompletionSource } from "@codemirror/autocomplete";import { history, historyKeymap, standardKeymap } from "@codemirror/commands";-import type { Extension } from "@codemirror/state";+import { ChangeSet, type ChangeSpec, EditorState, type Extension } from "@codemirror/state";import { EditorView, keymap, placeholder } from "@codemirror/view";/**@@ -28,9 +28,15 @@ export interface FieldEditorConfig {* opening rather than a finished value.*/activateOnCompletion?(completion: Completion): boolean;+ /**+ * Whether the value is confined to one line, the way an input is: Shift-Enter does nothing and+ * a line break in inserted text becomes a space. Long values scroll sideways instead of+ * wrapping, since `lineWrapping` would grow the field.+ */+ singleLine?: boolean;/** Runs after every document change, with the whole text. */onChange?(value: string): void;- /** Runs when Enter is pressed; Shift-Enter inserts a line break instead. */+ /** Runs when Enter is pressed; Shift-Enter inserts a line break, unless {@link singleLine}. */onEnter?(): void;/*** Runs when ArrowDown is pressed and no completion is open or pending, for a field whose@@ -51,7 +57,8 @@ export type FieldEditor = EditorView;** 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.+ * line and keeps its indentation, for a value the user lays out over several of them; `singleLine`+ * holds the value to one line instead.** `onArrowDown` and `onEscape` run only while no completion is open or pending, so a key the popup* drops does not move focus out of the editor instead. An IME keeps Enter for as long as it is@@ -62,7 +69,6 @@ export type FieldEditor = EditorView;*/export function createFieldEditor(config: FieldEditorConfig): FieldEditor {const extensions: Extension[] = [- EditorView.lineWrapping,history(),keymap.of([{@@ -76,6 +82,9 @@ export function createFieldEditor(config: FieldEditorConfig): FieldEditor {return true;}},+ // Swallowed rather than left to `insertNewlineAndIndent`, whose line break and+ // indentation `flattenToOneLine` would otherwise turn into stray spaces.+ ...(config.singleLine ? [ { key: "Shift-Enter", run: () => true } ] : []),{ key: "ArrowDown", run: (view) => keyBelongsToField(view) && !!config.onArrowDown?.() },{ key: "Escape", run: (view) => keyBelongsToField(view) && !!config.onEscape?.() },...standardKeymap,@@ -86,6 +95,7 @@ export function createFieldEditor(config: FieldEditorConfig): FieldEditor {config.onChange?.(update.state.doc.toString());}}),+ ...(config.singleLine ? [ flattenToOneLine ] : [ EditorView.lineWrapping ]),...(config.extensions ?? [])];@@ -114,6 +124,30 @@ export function createFieldEditor(config: FieldEditorConfig): FieldEditor {});}+/**+ * Keeps the document on one line, for a field laid out as an input. A line break in inserted text+ * becomes a space, so pasting several lines still puts their text in the field.+ */+const flattenToOneLine = EditorState.transactionFilter.of((tr) => {+ if (!tr.docChanged || tr.newDoc.lines === 1) {+ return tr;+ }++ const flattened: ChangeSpec[] = [];+ tr.changes.iterChanges((fromA, toA, _fromB, _toB, inserted) => {+ flattened.push({ from: fromA, to: toA, insert: inserted.toString().replace(/\r\n?|\n/g, " ") });+ });++ const changes = ChangeSet.of(flattened, tr.startState.doc.length);++ return {+ changes,+ // Mapped with assoc 1, so the caret lands after the inserted text as it otherwise would.+ selection: tr.startState.selection.map(changes, 1),+ scrollIntoView: tr.scrollIntoView+ };+});+/*** Whether a navigation key the completion popup declined belongs to the field. The popup drops* ArrowDown and Escape for `interactionDelay` after it opens, and a key dropped that way must not