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
fix(presentation): refit slides whenever their size changes
Elian Doranbf2a55b308a2
diff --git a/apps/client/src/widgets/collections/presentation/index.spec.ts b/apps/client/src/widgets/collections/presentation/index.spec.tsindex 81944ae003..e05ce55200 100644--- a/apps/client/src/widgets/collections/presentation/index.spec.ts+++ b/apps/client/src/widgets/collections/presentation/index.spec.ts@@ -1,6 +1,6 @@-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";-import { fitSlideContent } from "./index";+import { createFitObserver, fitSlideContent } from "./index";describe("fitSlideContent", () => {it("zooms out only the content that is larger than the slide", () => {@@ -35,6 +35,38 @@ describe("fitSlideContent", () => {});});+describe("createFitObserver", () => {+ it("fits again when the size changes, but not for the size its own fit leaves", () => {+ let notify: ResizeObserverCallback = () => {};+ vi.stubGlobal("ResizeObserver", class {+ constructor(callback: ResizeObserverCallback) { notify = callback; }+ disconnect() {}+ });++ let naturalHeight = 2;+ const content = buildContent({ height: zoom => naturalHeight * zoom });+ const onFitted = vi.fn();+ const observer = createFitObserver(onFitted);+ const resize = () => notify([ { target: content } as unknown as ResizeObserverEntry ], observer);++ resize();+ expect(Number(content.style.zoom)).toBeCloseTo(0.5);+ expect(onFitted).toHaveBeenCalledTimes(1);++ // The notification for the zoom the fit applied.+ resize();+ expect(onFitted).toHaveBeenCalledTimes(1);++ // A stylesheet makes the content taller.+ naturalHeight = 4;+ resize();+ expect(Number(content.style.zoom)).toBeCloseTo(0.25);+ expect(onFitted).toHaveBeenCalledTimes(2);++ vi.unstubAllGlobals();+ });+});+interface ContentSize {/** The height relative to the slide's, for a given zoom. */height?: (zoom: number) => number;@@ -56,5 +88,7 @@ function buildContent({ height = () => 1, width = () => 1, hidden }: ContentSizecontent.getBoundingClientRect = () => DOMRect.fromRect({ width: 640, height: 360 * height(zoom()) });Object.defineProperty(content, "clientWidth", { get: () => clientWidth });Object.defineProperty(content, "scrollWidth", { get: () => clientWidth * width(zoom()) });+ Object.defineProperty(content, "offsetWidth", { get: () => clientWidth });+ Object.defineProperty(content, "offsetHeight", { get: () => 720 * height(zoom()) });return content;}diff --git a/apps/client/src/widgets/collections/presentation/index.tsx b/apps/client/src/widgets/collections/presentation/index.tsxindex 811c28687d..128def1f9a 100644--- a/apps/client/src/widgets/collections/presentation/index.tsx+++ b/apps/client/src/widgets/collections/presentation/index.tsx@@ -159,6 +159,7 @@ function ButtonOverlay({ containerRef, api, isReadOnly }: { containerRef: RefObjfunction Presentation({ presentation, setApi } : { presentation: PresentationModel, setApi: (api: RevealApi | undefined) => void }) {const containerRef = useRef<HTMLDivElement>(null);const [revealApi, setRevealApi] = useState<RevealApi>();+ const fitObserverRef = useRef<ResizeObserver>(null);useEffect(() => {if (!containerRef.current) return;@@ -182,23 +183,10 @@ function Presentation({ presentation, setApi } : { presentation: PresentationMod},});const container = containerRef.current;- const fit = () => {- fitSlides(container);+ const fitObserver = createFitObserver(() => {if (api.isReady()) api.layout();- };- const fitLoadedImage = (e: Event) => {- const content = (e.target as HTMLElement).closest<HTMLElement>(".slide-content");- if (!content) return;- fitSlideContent(content);- if (api.isReady()) api.layout();- };-- // Reveal.js shows only the slides near the current one, so the others are fitted once they appear.- api.on("ready", fit);- api.on("slidechanged", fit);- api.on("overviewshown", fit);- container.addEventListener("load", fitLoadedImage, true);- document.fonts.ready.then(fit);+ });+ fitObserverRef.current = fitObserver;api.initialize().then(() => {setRevealApi(api);@@ -213,7 +201,8 @@ function Presentation({ presentation, setApi } : { presentation: PresentationModresizeObserver.observe(container);return () => {- container.removeEventListener("load", fitLoadedImage, true);+ fitObserver.disconnect();+ fitObserverRef.current = null;resizeObserver.disconnect();api.destroy();setRevealApi(undefined);@@ -222,10 +211,14 @@ function Presentation({ presentation, setApi } : { presentation: PresentationMod}, []);useEffect(() => {- if (!revealApi || !containerRef.current) return;+ const fitObserver = fitObserverRef.current;+ if (!revealApi || !containerRef.current || !fitObserver) return;revealApi.sync();- fitSlides(containerRef.current);- revealApi.layout();++ fitObserver.disconnect();+ for (const content of containerRef.current.querySelectorAll(".slide-content")) {+ fitObserver.observe(content);+ }}, [ presentation, revealApi ]);return (@@ -262,10 +255,27 @@ function Slide({ slide }: { slide: PresentationSlideBaseModel }) {);}-function fitSlides(container: HTMLElement) {- for (const content of container.querySelectorAll<HTMLElement>(".slide-content")) {- fitSlideContent(content);- }+/**+ * Fits each observed slide content again whenever its size changes: an image or a font loads, the theme+ * changes, or Reveal.js shows a slide it had hidden. The size each fit leaves behind is remembered, so the+ * fit's own zoom does not trigger another fit.+ */+export function createFitObserver(onFitted: () => void) {+ const fittedSizes = new WeakMap<Element, string>();+ return new ResizeObserver((entries) => {+ let fitted = false;+ for (const { target } of entries) {+ if (!(target instanceof HTMLElement) || fittedSizes.get(target) === measureSize(target)) continue;+ fitSlideContent(target);+ fittedSizes.set(target, measureSize(target));+ fitted = true;+ }+ if (fitted) onFitted();+ });+}++function measureSize(element: HTMLElement) {+ return `${element.offsetWidth}x${element.offsetHeight}`;}/**