diff --git a/apps/client/src/widgets/dialogs/add_link.tsx b/apps/client/src/widgets/dialogs/add_link.tsx index d62a24304..c0d344cd2 100644 --- a/apps/client/src/widgets/dialogs/add_link.tsx +++ b/apps/client/src/widgets/dialogs/add_link.tsx @@ -6,10 +6,10 @@ import ReactBasicWidget from "../react/ReactBasicWidget"; import Button from "../react/Button"; import FormRadioGroup from "../react/FormRadioGroup"; import NoteAutocomplete from "../react/NoteAutocomplete"; -import { useState } from "preact/hooks"; +import { useRef, useState } from "preact/hooks"; import tree from "../../services/tree"; import { useEffect } from "react"; -import { Suggestion } from "../../services/note_autocomplete"; +import note_autocomplete, { Suggestion } from "../../services/note_autocomplete"; import type { default as TextTypeWidget } from "../type_widgets/editable_text.js"; import { logError } from "../../services/ws"; @@ -58,6 +58,20 @@ function AddLinkDialogComponent({ text: _text, textTypeWidget }: AddLinkDialogPr } }, [suggestion]); + function onShown() { + const $autocompleteEl = $(autocompleteRef.current); + if (!text) { + note_autocomplete.showRecentNotes($autocompleteEl); + } else { + note_autocomplete.setText($autocompleteEl, text); + } + + // to be able to quickly remove entered text + $autocompleteEl + .trigger("focus") + .trigger("select"); + } + function onSubmit() { if (suggestion.notePath) { // Handle note link @@ -72,6 +86,8 @@ function AddLinkDialogComponent({ text: _text, textTypeWidget }: AddLinkDialogPr } } + const autocompleteRef = useRef(null); + return ( } onSubmit={onSubmit} - onHidden={() => setSuggestion(null)} + onShown={onShown} + onHidden={() => setSuggestion(null)} >
; text?: string; allowExternalLinks?: boolean; allowCreatingNotes?: boolean; onChange?: (suggestion: Suggestion) => void; } -export default function NoteAutocomplete({ text, allowCreatingNotes, allowExternalLinks, onChange }: NoteAutocompleteProps) { - const ref = useRef(null); +export default function NoteAutocomplete({ inputRef: _ref, text, allowCreatingNotes, allowExternalLinks, onChange }: NoteAutocompleteProps) { + const ref = _ref ?? useRef(null); useEffect(() => { if (!ref.current) return;