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
refactor(autocomplete): share the autocomplete row type with core
Elian Doran50f1279bf1d5
diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.tsindex 5bab519bb4..4dc6b41df6 100644--- a/apps/client/src/services/note_autocomplete.ts+++ b/apps/client/src/services/note_autocomplete.ts@@ -1,20 +1,17 @@import type { MentionFeedObjectItem } from "@triliumnext/ckeditor5";+import type { AutocompleteResult } from "@triliumnext/commons";-// TODO: Deduplicate with server.-export interface Suggestion {- noteTitle?: string;- externalLink?: string;- notePathTitle?: string;- notePath?: string;- highlightedNotePathTitle?: string;+/**+ * One row of the dropdown: a note from `GET /api/autocomplete`, or a row added by the client.+ * A client row sets `action` to its kind and fills in only the fields that kind uses.+ */+export interface Suggestion extends Partial<AutocompleteResult> {action?: string | "create-note" | "create-child-note" | "search-notes" | "external-link" | "command";+ externalLink?: string;parentNoteId?: string;- icon?: string;commandId?: string;commandDescription?: string;commandShortcut?: string;- attributeSnippet?: string;- highlightedAttributeSnippet?: string;}export interface Options {diff --git a/apps/client/src/services/note_autocomplete_bak.ts b/apps/client/src/services/note_autocomplete_bak.tsindex 3766f158d1..73ec7c0f90 100644--- a/apps/client/src/services/note_autocomplete_bak.ts+++ b/apps/client/src/services/note_autocomplete_bak.ts@@ -6,6 +6,7 @@ import commandRegistry from "./command_registry.js";import dateNoteService from "./date_notes.js";import froca from "./froca.js";import { t } from "./i18n.js";+import type { Options, Suggestion } from "./note_autocomplete.js";import noteCreateService from "./note_create.js";import server from "./server.js";import { escapeHtml } from "./utils.js";@@ -116,37 +117,6 @@ function buildCreateNoteTitle(term: string, target: InboxTargetResponse | null)return t("note_autocomplete.create-note-into", { term: escapeHtml(term), parentTitle: escapeHtml(target.title) });}-// TODO: Deduplicate with server.-export interface Suggestion {- noteTitle?: string;- externalLink?: string;- notePathTitle?: string;- notePath?: string;- highlightedNotePathTitle?: string;- action?: string | "create-note" | "create-child-note" | "search-notes" | "external-link" | "command";- parentNoteId?: string;- icon?: string;- commandId?: string;- commandDescription?: string;- commandShortcut?: string;- attributeSnippet?: string;- highlightedAttributeSnippet?: string;-}--export interface Options {- container?: HTMLElement | null;- fastSearch?: boolean;- allowCreatingNotes?: boolean;- allowJumpToSearchNotes?: boolean;- allowExternalLinks?: boolean;- /** If set, hides the right-side button corresponding to go to selected note. */- hideGoToSelectedNoteButton?: boolean;- /** If set, hides all right-side buttons in the autocomplete dropdown */- hideAllButtons?: boolean;- /** If set, enables command palette mode */- isCommandPalette?: boolean;-}-/*** Feeds a CKEditor mention. Creation entries are offered only where the editor's host component* implements `createNoteForReferenceLink`, which is what `MentionCustomization` calls to act ondiff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 6864ece661..3dbd2a0486 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -3,7 +3,7 @@ import type { CSSProperties } from "preact/compat";import type { Options, Suggestion } from "../../services/note_autocomplete";-interface NoteAutocompleteProps {+export interface NoteAutocompleteProps {id?: string;inputRef?: RefObject<HTMLInputElement>;text?: string;diff --git a/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx b/apps/client/src/widgets/react/NoteAutocomplete_bak.tsxindex d1afe15462..cded1ef9d0 100644--- a/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete_bak.tsx@@ -1,29 +1,8 @@import { t } from "../../services/i18n";import { useEffect } from "preact/hooks";-import note_autocomplete, { Options, type Suggestion } from "../../services/note_autocomplete";-import type { RefObject } from "preact";-import type { CSSProperties } from "preact/compat";+import note_autocomplete from "../../services/note_autocomplete";import { useSyncedRef } from "./hooks";--interface NoteAutocompleteProps {- id?: string;- inputRef?: RefObject<HTMLInputElement>;- text?: string;- placeholder?: string;- container?: RefObject<HTMLElement | null | undefined>;- containerStyle?: CSSProperties;- opts?: Omit<Options, "container">;- onChange?: (suggestion: Suggestion | null) => void;- onTextChange?: (text: string) => void;- onKeyDown?: (e: KeyboardEvent) => void;- onBlur?: (newValue: string) => void;- noteIdChanged?: (noteId: string) => void;- noteId?: string;- /** Shows the selected note without allowing a different one to be picked. */- readOnly?: boolean;- /** Places the input in the tab order of a host that orders its fields with `tabIndex`. */- tabIndex?: number;-}+import type { NoteAutocompleteProps } from "./NoteAutocomplete";export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, onChange, onTextChange, container, containerStyle, opts, noteId, noteIdChanged, onKeyDown, onBlur, readOnly, tabIndex }: NoteAutocompleteProps) {const ref = useSyncedRef<HTMLInputElement>(externalInputRef);diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.tsindex 0b77df0377..195b116a4e 100644--- a/apps/client/src/widgets/ribbon/search_completions.ts+++ b/apps/client/src/widgets/ribbon/search_completions.ts@@ -1,8 +1,7 @@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 { ALLOWED_NOTE_TYPES, allowedSearchOperators, type AutocompleteResult, MIME_TYPES_DICT, SEARCH_NOTE_PATH, SEARCH_NOTE_PATH_SEGMENTS } from "@triliumnext/commons";import { isBuiltinAttribute } from "../../services/attributes";-import type { Suggestion } from "../../services/note_autocomplete";import { t } from "../../services/i18n";import server from "../../services/server";import { fetchAttributeNames } from "../attribute_widgets/attribute_detail";@@ -144,9 +143,9 @@ interface NoteCompletion extends Completion {* note after a rename; with nothing typed yet the call answers with the recently visited notes.*/async function noteCompletions(term: string, atPos: number): Promise<CompletionResult | null> {- let suggestions: Suggestion[];+ let suggestions: AutocompleteResult[];try {- suggestions = await server.get<Suggestion[]>(+ suggestions = await server.get<AutocompleteResult[]>(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=none&fastSearch=true`);} catch {diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.tsindex 535b234dae..3376c942ae 100644--- a/packages/commons/src/lib/server_api.ts+++ b/packages/commons/src/lib/server_api.ts@@ -389,6 +389,13 @@ export interface SearchResultDetails {icon: string;}+/**+ * One row of `GET /api/autocomplete`: a recently visited note when the query is empty, a search+ * result otherwise. Only search results carry the attribute snippet.+ */+export type AutocompleteResult = Pick<SearchResultDetails, "notePath" | "noteTitle" | "notePathTitle"+ | "highlightedNotePathTitle" | "attributeSnippet" | "highlightedAttributeSnippet" | "icon">;+/** Response for `GET /api/quick-search/:searchString`. */export interface QuickSearchResponse {searchResultNoteIds: string[];diff --git a/packages/trilium-core/src/routes/api/autocomplete.ts b/packages/trilium-core/src/routes/api/autocomplete.tsindex bcc7eb4bb0..2d533e501b 100644--- a/packages/trilium-core/src/routes/api/autocomplete.ts+++ b/packages/trilium-core/src/routes/api/autocomplete.ts@@ -1,3 +1,5 @@+import type { AutocompleteResult } from "@triliumnext/commons";+import type { Request } from "../../http_interface";import becca from "../../becca/becca.js";@@ -8,7 +10,7 @@ import { escapeHtml } from "../../services/utils/index.js";import { ValidationError } from "../../errors.js";import becca_service from "../../becca/becca_service.js";-function getAutocomplete(req: Request) {+function getAutocomplete(req: Request): AutocompleteResult[] {if (typeof req.query.query !== "string") {throw new ValidationError("Invalid query data type.");}@@ -17,7 +19,7 @@ function getAutocomplete(req: Request) {const activeNoteId = req.query.activeNoteId || "none";- let results;+ let results: AutocompleteResult[];const timestampStarted = Date.now();@@ -36,7 +38,7 @@ function getAutocomplete(req: Request) {return results;}-function getRecentNotes(activeNoteId: string) {+function getRecentNotes(activeNoteId: string): AutocompleteResult[] {let extraCondition = "";const params = [activeNoteId];