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): fit slides whose images do not shrink with zoom
Elian Doran0a26f84ed831
diff --git a/apps/client/src/widgets/collections/presentation/index.spec.ts b/apps/client/src/widgets/collections/presentation/index.spec.tsindex 6779fa1866..81944ae003 100644--- a/apps/client/src/widgets/collections/presentation/index.spec.ts+++ b/apps/client/src/widgets/collections/presentation/index.spec.ts@@ -4,34 +4,57 @@ import { fitSlideContent } from "./index";describe("fitSlideContent", () => {it("zooms out only the content that is larger than the slide", () => {- const content = buildContent(1280, 1440);- fitSlideContent(content);- expect(content.style.zoom).toBe("0.5");+ const tall = buildContent({ height: zoom => 2 * zoom });+ fitSlideContent(tall);+ expect(Number(tall.style.zoom)).toBeCloseTo(0.5);// A second fit measures the content without the zoom it already has.- fitSlideContent(content);- expect(content.style.zoom).toBe("0.5");+ fitSlideContent(tall);+ expect(Number(tall.style.zoom)).toBeCloseTo(0.5);- const wide = buildContent(2560, 360);+ const wide = buildContent({ width: zoom => 2 * zoom });fitSlideContent(wide);- expect(wide.style.zoom).toBe("0.5");+ expect(Number(wide.style.zoom)).toBeCloseTo(0.5);- const fitting = buildContent(1280, 720);+ const fitting = buildContent({ height: () => 1 });fitting.style.zoom = "0.5";fitSlideContent(fitting);expect(fitting.style.zoom).toBe("");- const hidden = buildContent(0, 0);+ const hidden = buildContent({ height: () => 0, hidden: true });fitSlideContent(hidden);expect(hidden.style.zoom).toBe("");});++ it("fits content with parts that do not scale with the zoom", () => {+ // Text as tall as the slide plus an image half as tall, which keeps its height.+ const content = buildContent({ height: zoom => zoom + 0.5 });+ fitSlideContent(content);+ expect(Number(content.style.zoom)).toBeCloseTo(0.5);+ expect(Number(content.style.zoom) + 0.5).toBeLessThanOrEqual(1);+ });});-/** Builds an element whose measured size shrinks with its zoom, as it does in a browser. */-function buildContent(width: number, height: number) {+interface ContentSize {+ /** The height relative to the slide's, for a given zoom. */+ height?: (zoom: number) => number;+ /** The scrolled width relative to the slide's, for a given zoom. */+ width?: (zoom: number) => number;+ hidden?: boolean;+}++/** Builds slide content whose measured size follows its zoom, as it does in a browser. */+function buildContent({ height = () => 1, width = () => 1, hidden }: ContentSize) {+ const slides = document.createElement("div");+ slides.className = "slides";const content = document.createElement("div");+ slides.appendChild(content);+const zoom = () => Number(content.style.zoom || 1);- Object.defineProperty(content, "scrollWidth", { get: () => width * zoom() });- Object.defineProperty(content, "scrollHeight", { get: () => height * zoom() });+ const clientWidth = hidden ? 0 : 1280;+ slides.getBoundingClientRect = () => DOMRect.fromRect({ width: 640, height: 360 });+ content.getBoundingClientRect = () => DOMRect.fromRect({ width: 640, height: 360 * height(zoom()) });+ Object.defineProperty(content, "clientWidth", { get: () => clientWidth });+ Object.defineProperty(content, "scrollWidth", { get: () => clientWidth * width(zoom()) });return content;}diff --git a/apps/client/src/widgets/collections/presentation/index.tsx b/apps/client/src/widgets/collections/presentation/index.tsxindex 7df16550db..811c28687d 100644--- a/apps/client/src/widgets/collections/presentation/index.tsx+++ b/apps/client/src/widgets/collections/presentation/index.tsx@@ -21,6 +21,7 @@ import { DEFAULT_THEME, loadPresentationTheme } from "./themes";const SLIDE_WIDTH = 1280;const SLIDE_HEIGHT = 720;+const MAX_FIT_ATTEMPTS = 8;export default function PresentationView({ note, noteIds, media, onReady, onProgressChanged }: ViewModeProps<{}>) {const [ presentation, setPresentation ] = useState<PresentationModel>();@@ -270,15 +271,39 @@ function fitSlides(container: HTMLElement) {/*** Zooms out the content of a slide that is larger than the slide, so that it fits. A hidden slide measures* as empty and keeps no zoom until it is fitted again.+ *+ * Some lengths (percentages, container query units) do not scale with `zoom`, so the content is measured+ * again after each zoom. The overflow is close to linear in the zoom, so each next zoom is a secant step.*/export function fitSlideContent(content: HTMLElement) {content.style.zoom = "";- const scale = Math.min(1, SLIDE_WIDTH / content.scrollWidth, SLIDE_HEIGHT / content.scrollHeight);- if (scale < 1) {+ let previous = { scale: 1, overflow: measureOverflow(content) };+ if (previous.overflow <= 1) return;++ let scale = 1 / previous.overflow;+ for (let attempt = 0; attempt < MAX_FIT_ATTEMPTS; attempt++) {content.style.zoom = String(scale);+ const overflow = measureOverflow(content);+ if (overflow <= 1) break;++ const slope = (previous.overflow - overflow) / (previous.scale - scale);+ const next = scale - (overflow - 1) / slope;+ previous = { scale, overflow };+ scale = slope > 0 && next > 0 ? next : scale / overflow;}}+/** Returns how many times larger the content is than the slide, as measured on screen. */+function measureOverflow(content: HTMLElement) {+ const slides = content.closest<HTMLElement>(".slides");+ const slidesHeight = slides?.getBoundingClientRect().height;+ if (!slidesHeight || !content.clientWidth) return 0;++ const heightRatio = content.getBoundingClientRect().height / slidesHeight;+ const widthRatio = content.scrollWidth / content.clientWidth;+ return Math.max(heightRatio, widthRatio);+}+function getNoteIdFromSlide(slide: HTMLElement | undefined) {if (!slide) return;return slide.dataset.noteId;