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
client/text editor/youtube video preview: add exit transition for the cover image
Adorian Doranbe7c462212ef
diff --git a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css b/apps/client/src/widgets/type_widgets/text/LinkEmbed.cssindex 2c3f7ee7d5..e290b76286 100644--- a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css+++ b/apps/client/src/widgets/type_widgets/text/LinkEmbed.css@@ -124,9 +124,16 @@ body.light-theme .link-embed-favicon-light {/* ---------- Video embed (YouTube iframe) ---------- */.link-embed-video {+ display: grid;+ grid-template-columns: minmax(0, 1fr);position: relative;width: 100%;overflow: hidden;++ & > iframe,+ & > .link-embed-video-facade {+ grid-area: 1 / 1;+ }}.link-embed-preview-wrapper > .link-embed-video {@@ -161,6 +168,10 @@ body.light-theme .link-embed-favicon-light {cursor: pointer;overflow: hidden;+ &.link-embed-video-facade-loading {+ cursor: progress;+ }+&:hover .link-embed-video-thumbnail-lowres {filter: blur(var(--link-preview-video-facade-blur-radius)) brightness(0.65);}diff --git a/packages/commons/src/lib/link_embed_dom.spec.ts b/packages/commons/src/lib/link_embed_dom.spec.tsindex 6110c634c7..447cc5f124 100644--- a/packages/commons/src/lib/link_embed_dom.spec.ts+++ b/packages/commons/src/lib/link_embed_dom.spec.ts@@ -71,23 +71,55 @@ beforeEach(() => {afterEach(() => {vi.unstubAllGlobals();+ vi.restoreAllMocks();document.body.innerHTML = "";});describe("enhanceLinkPreviews", () => {const VIDEO = { url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", embedType: "youtube" };- it("loads the player only once the facade is clicked, with the page's web origin", () => {+ it("loads the player under the facade once clicked, with the page's web origin", () => {+ const fade = new EventTarget() as Animation;+ const animate = vi.spyOn(Element.prototype, "animate").mockReturnValue(fade);const container = render(renderLinkEmbedHtml(VIDEO, OPTIONS));+ const facade = container.querySelector<HTMLButtonElement>(".link-embed-video-facade");expect(container.querySelector("iframe")).toBeNull();- container.querySelector<HTMLButtonElement>(".link-embed-video-facade")?.click();+ facade?.click();const iframe = container.querySelector("iframe");- expect(container.querySelector(".link-embed-video-facade")).toBeNull();expect(iframe?.getAttribute("src")).toBe("https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ"+ `?rel=0&autoplay=1&origin=${encodeURIComponent(location.origin)}`);expect(iframe?.parentElement?.className).toBe("link-embed-video");+ expect(iframe?.nextElementSibling).toBe(facade);+ expect(facade?.classList.contains("link-embed-video-facade-loading")).toBe(true);+ expect(animate).not.toHaveBeenCalled();++ // Fades out once the player has loaded, and is removed once faded.+ iframe?.dispatchEvent(new Event("load"));+ expect(animate).toHaveBeenCalledExactlyOnceWith(+ { opacity: [ 1, 0 ] }, expect.objectContaining({ duration: 300 })+ );+ expect(animate.mock.contexts[0]).toBe(facade);+ expect(facade?.isConnected).toBe(true);+ fade.dispatchEvent(new Event("finish"));+ expect(facade?.isConnected).toBe(false);+ expect(iframe?.isConnected).toBe(true);+ });++ it("removes the facade without fading it for a user who asks for reduced motion", () => {+ vi.spyOn(window, "matchMedia").mockReturnValue({ matches: true } as MediaQueryList);+ const animate = vi.spyOn(Element.prototype, "animate");+ const container = render(renderLinkEmbedHtml(VIDEO, OPTIONS));+ const facade = container.querySelector<HTMLButtonElement>(".link-embed-video-facade");++ facade?.click();+ expect(facade?.isConnected).toBe(true);+ container.querySelector("iframe")?.dispatchEvent(new Event("load"));++ expect(window.matchMedia).toHaveBeenCalledWith("(prefers-reduced-motion: reduce)");+ expect(animate).not.toHaveBeenCalled();+ expect(facade?.isConnected).toBe(false);});it("leaves the origin out on the desktop app's custom protocol", () => {diff --git a/packages/commons/src/lib/link_embed_dom.ts b/packages/commons/src/lib/link_embed_dom.tsindex 58e35f5efc..36e1c80f5d 100644--- a/packages/commons/src/lib/link_embed_dom.ts+++ b/packages/commons/src/lib/link_embed_dom.ts@@ -40,9 +40,9 @@ export function enhanceLinkPreviews(container: ParentNode) {}/**- * Replaces a video facade with the YouTube player. The click was the play command, hence- * `autoplay=1`. `origin` is valid only for a web origin: YouTube rejects the desktop app's- * `trilium-app://` one.+ * Loads the YouTube player under a video facade, which fades out once the player has loaded. The+ * click was the play command, hence `autoplay=1`. `origin` is valid only for a web origin: YouTube+ * rejects the desktop app's `trilium-app://` one.*/function playVideo(facade: HTMLButtonElement) {const videoId = encodeURIComponent(String(facade.dataset.videoId));@@ -57,7 +57,26 @@ function playVideo(facade: HTMLButtonElement) {iframe.referrerPolicy = "strict-origin-when-cross-origin";iframe.allowFullscreen = true;iframe.setAttribute("frameborder", "0");- facade.replaceWith(iframe);+ iframe.addEventListener("load", () => revealPlayer(facade), { once: true });+ facade.classList.add("link-embed-video-facade-loading");+ facade.before(iframe);+}++/** How long a video facade takes to fade out over the player, in milliseconds. */+const FACADE_FADE_DURATION = 300;++/** Fades out `facade` over the player loaded under it, then removes it. */+function revealPlayer(facade: HTMLButtonElement) {+ if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {+ facade.remove();+ return;+ }++ const fade = facade.animate(+ { opacity: [ 1, 0 ] },+ { duration: FACADE_FADE_DURATION, easing: "ease-out", fill: "forwards" }+ );+ fade.addEventListener("finish", () => facade.remove(), { once: true });}/**diff --git a/packages/share-theme/src/content/link_embed.css b/packages/share-theme/src/content/link_embed.cssindex ef09b26ac7..833d8adbda 100644--- a/packages/share-theme/src/content/link_embed.css+++ b/packages/share-theme/src/content/link_embed.css@@ -67,12 +67,19 @@ html.theme-light .link-embed-favicon-light {/* ---------- Video embed (YouTube iframe) ---------- */.link-embed-video {+ display: grid;+ grid-template-columns: minmax(0, 1fr);position: relative;max-width: 640px;margin: auto;border-radius: 8px;overflow: hidden;background: #000;++ & > iframe,+ & > .link-embed-video-facade {+ grid-area: 1 / 1;+ }}.link-embed-video iframe {@@ -97,6 +104,10 @@ html.theme-light .link-embed-favicon-light {cursor: pointer;}+.link-embed-video-facade-loading {+ cursor: progress;+}+.link-embed-video-thumbnail {width: 100%;height: 100%;