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(react/bulk_actions): improve note auto complete handling
Elian Doran5c8e4fd6fd5e
diff --git a/apps/client/src/widgets/bulk_actions/note/move_note.tsx b/apps/client/src/widgets/bulk_actions/note/move_note.tsxindex 677680bbc9..5e9945bbb2 100644--- a/apps/client/src/widgets/bulk_actions/note/move_note.tsx+++ b/apps/client/src/widgets/bulk_actions/note/move_note.tsx@@ -4,15 +4,13 @@ import BulkAction, { BulkActionText } from "../BulkAction.jsx";import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";import { useEffect, useState } from "preact/hooks";import { useSpacedUpdate } from "../../react/hooks.jsx";-import { Suggestion } from "../../../services/note_autocomplete.js";function MoveNoteBulkActionComponent({ bulkAction, actionDef }: { bulkAction: AbstractBulkAction, actionDef: ActionDefinition }) {- const [ suggestion, setSuggestion ] = useState<Suggestion>();+ const [ targetParentNoteId, setTargetParentNoteId ] = useState<string>();const spacedUpdate = useSpacedUpdate(() => {- const noteId = suggestion?.notePath?.split("/")?.at(-1);- return bulkAction.saveAction({ targetParentNoteId: noteId })+ return bulkAction.saveAction({ targetParentNoteId: targetParentNoteId })});- useEffect(() => spacedUpdate.scheduleUpdate(), [ suggestion ]);+ useEffect(() => spacedUpdate.scheduleUpdate(), [ targetParentNoteId ]);return (<BulkAction@@ -32,7 +30,7 @@ function MoveNoteBulkActionComponent({ bulkAction, actionDef }: { bulkAction: Ab<NoteAutocompleteplaceholder={t("move_note.target_parent_note")}- onChange={setSuggestion}+ noteId={targetParentNoteId} noteIdChanged={setTargetParentNoteId}/></BulkAction>)diff --git a/apps/client/src/widgets/bulk_actions/relation/add_relation.tsx b/apps/client/src/widgets/bulk_actions/relation/add_relation.tsxindex 927c10ace5..5e10933a8b 100644--- a/apps/client/src/widgets/bulk_actions/relation/add_relation.tsx+++ b/apps/client/src/widgets/bulk_actions/relation/add_relation.tsx@@ -32,6 +32,7 @@ function AddRelationBulkActionComponent({ bulkAction, actionDef }: { bulkAction:<NoteAutocompleteplaceholder={t("add_relation.target_note")}+ noteId={targetNoteId} noteIdChanged={setTargetNoteId}/></BulkAction>)diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 1a7e9ad55f..926a9e3282 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -12,9 +12,11 @@ interface NoteAutocompleteProps {opts?: Omit<Options, "container">;onChange?: (suggestion: Suggestion | null) => void;onTextChange?: (text: string) => void;+ noteIdChanged?: (noteId: string) => void;+ noteId?: string;}-export default function NoteAutocomplete({ inputRef: _ref, text, placeholder, onChange, onTextChange, container, opts }: NoteAutocompleteProps) {+export default function NoteAutocomplete({ inputRef: _ref, text, placeholder, onChange, onTextChange, container, opts, noteId, noteIdChanged }: NoteAutocompleteProps) {const ref = _ref ?? useRef<HTMLInputElement>(null);useEffect(() => {@@ -30,8 +32,15 @@ export default function NoteAutocomplete({ inputRef: _ref, text, placeholder, on...opts,container: container?.current});- if (onChange) {- const listener = (_e, suggestion) => onChange(suggestion);+ if (onChange || noteIdChanged) {+ const listener = (_e, suggestion) => {+ onChange?.(suggestion);++ if (noteIdChanged) {+ const noteId = suggestion?.notePath?.split("/")?.at(-1);+ noteIdChanged(noteId);+ }+ };$autoComplete.on("autocomplete:noteselected", listener).on("autocomplete:externallinkselected", listener)