Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(link_embed): render using Preact
Elian Doranc7de37a43d37
diff --git a/apps/client/src/services/link_embed.ts b/apps/client/src/services/link_embed.tsxsimilarity index 51%rename from apps/client/src/services/link_embed.tsrename to apps/client/src/services/link_embed.tsxindex 666e40c02c..011cba5480 100644--- a/apps/client/src/services/link_embed.ts+++ b/apps/client/src/services/link_embed.tsx@@ -1,6 +1,9 @@import "../widgets/type_widgets/text/LinkEmbed.css";import type { LinkEmbedMetadata } from "@triliumnext/commons";+import { render } from "preact";+import { useState } from "preact/hooks";+import server from "./server.js";/** Paste mode chosen by user from the floating popup. */@@ -52,111 +55,105 @@ export async function fetchMetadata(url: string): Promise<EmbedMetadata> {}// ----------------------------------------------------------------------------// DOM renderers — render previews from stored metadata, no network requests.+// Preact components — render previews from stored metadata, no network requests.// Used by the CKEditor editing downcast (via component interface), the// read-only text renderer, and postProcessRichContent (tooltips, included// notes, markdown preview).// ----------------------------------------------------------------------------export function renderEmbedPreview(container: HTMLElement, meta: EmbedMetadata) {+function Favicon({ src }: { src?: string }) {+ const [failed, setFailed] = useState(false);++ if (!src || failed) {+ return <span className="link-embed-mention-dot" />;+ }++ return (+ <img+ className="link-embed-mention-favicon"+ src={src}+ width={16}+ height={16}+ onError={() => setFailed(true)}+ />+ );+}++function ImagePlaceholder() {+ return <div className="link-embed-card-image-placeholder">🔗</div>;+}++function CardImage({ src }: { src?: string }) {+ const [failed, setFailed] = useState(false);++ if (!src || failed) {+ return <ImagePlaceholder />;+ }++ return (+ <img+ className="link-embed-card-image"+ src={src}+ alt=""+ loading="lazy"+ onError={() => setFailed(true)}+ />+ );+}++function EmbedPreview({ meta }: { meta: EmbedMetadata }) {const videoId = YOUTUBE_REGEX.test(meta.url)? meta.url.match(YOUTUBE_REGEX)?.[1]: null;if (videoId) {- const origin = window.location.origin;- const wrapper = document.createElement("div");- wrapper.className = "link-embed-video";- const iframe = document.createElement("iframe");- iframe.src = `https://www.youtube-nocookie.com/embed/${videoId}?origin=${encodeURIComponent(origin)}&rel=0`;- iframe.setAttribute("frameborder", "0");- iframe.setAttribute("allowfullscreen", "true");- iframe.setAttribute("allow", "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share");- iframe.setAttribute("referrerpolicy", "strict-origin-when-cross-origin");- iframe.loading = "lazy";- wrapper.appendChild(iframe);- container.appendChild(wrapper);- return;+ return (+ <div className="link-embed-video">+ <iframe+ src={`https://www.youtube-nocookie.com/embed/${videoId}?origin=${encodeURIComponent(window.location.origin)}&rel=0`}+ frameBorder="0"+ allowFullScreen+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"+ referrerPolicy="strict-origin-when-cross-origin"+ loading="lazy"+ />+ </div>+ );}- // Card preview- const card = document.createElement("a");- card.className = "link-embed-card";- card.href = meta.url;- card.target = "_blank";- card.rel = "noopener noreferrer";-- const imgWrap = document.createElement("div");- imgWrap.className = "link-embed-card-image-wrapper";- if (meta.image) {- const img = document.createElement("img");- img.className = "link-embed-card-image";- img.src = meta.image;- img.alt = "";- img.loading = "lazy";- img.onerror = () => {- imgWrap.innerHTML = '<div class="link-embed-card-image-placeholder">🔗</div>';- };- imgWrap.appendChild(img);- } else {- imgWrap.innerHTML = '<div class="link-embed-card-image-placeholder">🔗</div>';- }- card.appendChild(imgWrap);-- const content = document.createElement("div");- content.className = "link-embed-card-content";- if (meta.title) {- const titleEl = document.createElement("div");- titleEl.className = "link-embed-card-title";- titleEl.textContent = meta.title;- content.appendChild(titleEl);- }- if (meta.description) {- const descEl = document.createElement("div");- descEl.className = "link-embed-card-description";- descEl.textContent = meta.description;- content.appendChild(descEl);- }- const urlEl = document.createElement("div");- urlEl.className = "link-embed-card-url";- urlEl.textContent = meta.siteName || safeHostname(meta.url);- content.appendChild(urlEl);+ return (+ <a className="link-embed-card" href={meta.url} target="_blank" rel="noopener noreferrer">+ <div className="link-embed-card-image-wrapper">+ <CardImage src={meta.image} />+ </div>+ <div className="link-embed-card-content">+ {meta.title && <div className="link-embed-card-title">{meta.title}</div>}+ {meta.description && <div className="link-embed-card-description">{meta.description}</div>}+ <div className="link-embed-card-url">{meta.siteName || safeHostname(meta.url)}</div>+ </div>+ </a>+ );+}- card.appendChild(content);- container.appendChild(card);+function MentionPreview({ meta }: { meta: { url: string; title?: string; favicon?: string } }) {+ return (+ <a className="link-embed-mention" href={meta.url} target="_blank" rel="noopener noreferrer">+ <Favicon src={meta.favicon} />+ <span className="link-embed-mention-title">{meta.title || safeHostname(meta.url)}</span>+ </a>+ );}-export function renderMentionPreview(container: HTMLElement, meta: { url: string; title?: string; favicon?: string }) {- const anchor = document.createElement("a");- anchor.className = "link-embed-mention";- anchor.href = meta.url;- anchor.target = "_blank";- anchor.rel = "noopener noreferrer";-- if (meta.favicon) {- const img = document.createElement("img");- img.className = "link-embed-mention-favicon";- img.src = meta.favicon;- img.width = 16;- img.height = 16;- img.onerror = () => {- const dot = document.createElement("span");- dot.className = "link-embed-mention-dot";- img.replaceWith(dot);- };- anchor.appendChild(img);- } else {- const dot = document.createElement("span");- dot.className = "link-embed-mention-dot";- anchor.appendChild(dot);- }+// ---------------------------------------------------------------------------+// Imperative API — renders Preact components into DOM containers.+// ---------------------------------------------------------------------------- const titleSpan = document.createElement("span");- titleSpan.className = "link-embed-mention-title";- titleSpan.textContent = meta.title || safeHostname(meta.url);- anchor.appendChild(titleSpan);+export function renderEmbedPreview(container: HTMLElement, meta: EmbedMetadata) {+ render(<EmbedPreview meta={meta} />, container);+}- container.appendChild(anchor);+export function renderMentionPreview(container: HTMLElement, meta: { url: string; title?: string; favicon?: string }) {+ render(<MentionPreview meta={meta} />, container);}/**