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
fix(add_link): focus resets to start of note (closes #7115)
Elian Dorande7c1329f849
diff --git a/apps/client/src/widgets/dialogs/add_link.tsx b/apps/client/src/widgets/dialogs/add_link.tsxindex 97440491da..43cfa4e4e3 100644--- a/apps/client/src/widgets/dialogs/add_link.tsx+++ b/apps/client/src/widgets/dialogs/add_link.tsx@@ -22,6 +22,7 @@ export default function AddLinkDialog() {const [ linkType, setLinkType ] = useState<LinkType>(hasSelection ? "hyper-link" : "reference-link");const [ suggestion, setSuggestion ] = useState<Suggestion | null>(null);const [ shown, setShown ] = useState(false);+ const hasSubmittedRef = useRef(false);useTriliumEvent("showAddLinkDialog", ( { textTypeWidget, text }) => {setTextTypeWidget(textTypeWidget);@@ -83,14 +84,11 @@ export default function AddLinkDialog() {}function onSubmit() {- if (suggestion?.notePath) {- // Handle note link- setShown(false);- textTypeWidget?.addLink(suggestion.notePath, linkType === "reference-link" ? null : linkTitle);- } else if (suggestion?.externalLink) {- // Handle external link+ hasSubmittedRef.current = true;++ if (suggestion) {+ // Insertion logic in onHidden because it needs focus.setShown(false);- textTypeWidget?.addLink(suggestion.externalLink, linkTitle, true);} else {logError("No link to add.");}@@ -109,6 +107,19 @@ export default function AddLinkDialog() {onSubmit={onSubmit}onShown={onShown}onHidden={() => {+ // Insert the link.+ if (hasSubmittedRef.current && suggestion && textTypeWidget) {+ hasSubmittedRef.current = false;++ if (suggestion.notePath) {+ // Handle note link+ textTypeWidget.addLink(suggestion.notePath, linkType === "reference-link" ? null : linkTitle);+ } else if (suggestion.externalLink) {+ // Handle external link+ textTypeWidget.addLink(suggestion.externalLink, linkTitle, true);+ }+ }+setSuggestion(null);setShown(false);}}