Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/main' into react/type_widgets

Elian Doran286d7c8228a1
diff --cc apps/client/src/widgets/dialogs/add_link.tsx
index 287a19379c,43cfa4e4e3..82ac9fced5
--- a/apps/client/src/widgets/dialogs/add_link.tsx
+++ b/apps/client/src/widgets/dialogs/add_link.tsx
@@@ -13,21 -14,19 +13,22 @@@ import { useTriliumEvent } from "../rea
type LinkType = "reference-link" | "external-link" | "hyper-link";
+export interface AddLinkOpts {
+ text: string;
+ hasSelection: boolean;
+ addLink(notePath: string, linkTitle: string | null, externalLink?: boolean): Promise<void>;
+}
+
export default function AddLinkDialog() {
- const [ textTypeWidget, setTextTypeWidget ] = useState<TextTypeWidget>();
- const initialText = useRef<string>();
+ const [ opts, setOpts ] = useState<AddLinkOpts>();
const [ linkTitle, setLinkTitle ] = useState("");
- const hasSelection = textTypeWidget?.hasSelection();
- const [ linkType, setLinkType ] = useState<LinkType>(hasSelection ? "hyper-link" : "reference-link");
+ const [ linkType, setLinkType ] = useState<LinkType>();
const [ suggestion, setSuggestion ] = useState<Suggestion | null>(null);
const [ shown, setShown ] = useState(false);
+ const hasSubmittedRef = useRef(false);
- useTriliumEvent("showAddLinkDialog", ( { textTypeWidget, text }) => {
- setTextTypeWidget(textTypeWidget);
- initialText.current = text;
+ useTriliumEvent("showAddLinkDialog", opts => {
+ setOpts(opts);
setShown(true);
});
@@@ -111,6 -107,19 +109,19 @@@
onSubmit={onSubmit}
onShown={onShown}
onHidden={() => {
+ // Insert the link.
- if (hasSubmittedRef.current && suggestion && textTypeWidget) {
++ if (hasSubmittedRef.current && suggestion && opts) {
+ hasSubmittedRef.current = false;
+
+ if (suggestion.notePath) {
+ // Handle note link
- textTypeWidget.addLink(suggestion.notePath, linkType === "reference-link" ? null : linkTitle);
++ opts.addLink(suggestion.notePath, linkType === "reference-link" ? null : linkTitle);
+ } else if (suggestion.externalLink) {
+ // Handle external link
- textTypeWidget.addLink(suggestion.externalLink, linkTitle, true);
++ opts.addLink(suggestion.externalLink, linkTitle, true);
+ }
+ }
+
setSuggestion(null);
setShown(false);
}}