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

48 lines
1.4 KiB
TypeScript
Raw Normal View History

import { useEffect, useRef, useState } from "preact/hooks";
2025-08-22 17:31:06 +03:00
import link from "../../services/link";
import RawHtml from "./RawHtml";
import { useSearchHighlighlighting } 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;
2025-08-22 17:31:06 +03:00
}
export default function NoteLink({ className, notePath, showNotePath, showNoteIcon, style, noPreview, noTnLink, highlightedTokens }: NoteLinkOpts) {
2025-08-22 17:31:06 +03:00
const stringifiedNotePath = Array.isArray(notePath) ? notePath.join("/") : notePath;
const [ jqueryEl, setJqueryEl ] = useState<JQuery<HTMLElement>>();
const containerRef = useRef<HTMLDivElement>(null);
useSearchHighlighlighting(containerRef, highlightedTokens);
2025-08-22 17:31:06 +03:00
useEffect(() => {
link.createLink(stringifiedNotePath, { showNotePath, showNoteIcon })
2025-08-22 17:31:06 +03:00
.then(setJqueryEl);
2025-08-22 19:27:58 +03:00
}, [ stringifiedNotePath, showNotePath ]);
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 (className) {
$linkEl?.addClass(className);
}
return <RawHtml containerRef={containerRef} html={jqueryEl} />
}