Files
Trilium/apps/client/src/widgets/react/NoteLink.tsx

65 lines
1.9 KiB
TypeScript
Raw Normal View History

import { useEffect, useRef, useState } from "preact/hooks";
import link, { ViewScope } from "../../services/link";
import { useImperativeSearchHighlighlighting } from "./hooks";
2025-08-22 17:31:06 +03:00
interface NoteLinkOpts {
className?: string;
2025-08-22 17:31:06 +03:00
notePath: string | string[];
showNotePath?: boolean;
showNoteIcon?: boolean;
2025-08-22 19:27:58 +03:00
style?: Record<string, string | number>;
noPreview?: boolean;
noTnLink?: boolean;
highlightedTokens?: string[] | null | undefined;
// Override the text of the link, otherwise the note title is used.
title?: string;
viewScope?: ViewScope;
noContextMenu?: boolean;
2025-08-22 17:31:06 +03:00
}
export default function NoteLink({ className, notePath, showNotePath, showNoteIcon, style, noPreview, noTnLink, highlightedTokens, title, viewScope, noContextMenu }: NoteLinkOpts) {
2025-08-22 17:31:06 +03:00
const stringifiedNotePath = Array.isArray(notePath) ? notePath.join("/") : notePath;
const ref = useRef<HTMLSpanElement>(null);
2025-08-22 17:31:06 +03:00
const [ jqueryEl, setJqueryEl ] = useState<JQuery<HTMLElement>>();
const highlightSearch = useImperativeSearchHighlighlighting(highlightedTokens);
2025-08-22 17:31:06 +03:00
useEffect(() => {
link.createLink(stringifiedNotePath, {
title,
showNotePath,
showNoteIcon,
viewScope
}).then(setJqueryEl);
}, [ stringifiedNotePath, showNotePath, title, viewScope ]);
2025-08-22 19:27:58 +03:00
useEffect(() => {
if (!ref.current || !jqueryEl) return;
ref.current.replaceChildren(jqueryEl[0]);
highlightSearch(ref.current);
}, [ jqueryEl, highlightedTokens ]);
2025-08-22 19:27:58 +03:00
if (style) {
jqueryEl?.css(style);
}
2025-08-22 17:31:06 +03:00
const $linkEl = jqueryEl?.find("a");
if (noPreview) {
$linkEl?.addClass("no-tooltip-preview");
}
if (!noTnLink) {
$linkEl?.addClass("tn-link");
}
if (noContextMenu) {
$linkEl?.attr("data-no-context-menu", "true");
}
if (className) {
$linkEl?.addClass(className);
}
return <span ref={ref} />
}