fix(add_link): focus resets to start of note (closes #7115)

This commit is contained in:
Elian Doran 2025-09-28 10:55:48 +03:00
parent 6fc5aa0090
commit de7c1329f8
No known key found for this signature in database

View File

@ -22,6 +22,7 @@ export default function AddLinkDialog() {
const [ linkType, setLinkType ] = useState<LinkType>(hasSelection ? "hyper-link" : "reference-link"); const [ linkType, setLinkType ] = useState<LinkType>(hasSelection ? "hyper-link" : "reference-link");
const [ suggestion, setSuggestion ] = useState<Suggestion | null>(null); const [ suggestion, setSuggestion ] = useState<Suggestion | null>(null);
const [ shown, setShown ] = useState(false); const [ shown, setShown ] = useState(false);
const hasSubmittedRef = useRef(false);
useTriliumEvent("showAddLinkDialog", ( { textTypeWidget, text }) => { useTriliumEvent("showAddLinkDialog", ( { textTypeWidget, text }) => {
setTextTypeWidget(textTypeWidget); setTextTypeWidget(textTypeWidget);
@ -83,14 +84,11 @@ export default function AddLinkDialog() {
} }
function onSubmit() { function onSubmit() {
if (suggestion?.notePath) { hasSubmittedRef.current = true;
// Handle note link
if (suggestion) {
// Insertion logic in onHidden because it needs focus.
setShown(false); setShown(false);
textTypeWidget?.addLink(suggestion.notePath, linkType === "reference-link" ? null : linkTitle);
} else if (suggestion?.externalLink) {
// Handle external link
setShown(false);
textTypeWidget?.addLink(suggestion.externalLink, linkTitle, true);
} else { } else {
logError("No link to add."); logError("No link to add.");
} }
@ -109,6 +107,19 @@ export default function AddLinkDialog() {
onSubmit={onSubmit} onSubmit={onSubmit}
onShown={onShown} onShown={onShown}
onHidden={() => { 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); setSuggestion(null);
setShown(false); setShown(false);
}} }}