mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 07:08:55 +02:00
fix(add_link): focus resets to start of note (closes #7115)
This commit is contained in:
parent
6fc5aa0090
commit
de7c1329f8
@ -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);
|
||||||
}}
|
}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user