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
feat(print): print a render note's scroll containers at full height
Elian Doranb05fca3cf3ec
diff --git a/apps/client/src/services/live_print.css b/apps/client/src/services/live_print.cssindex 1c31890226..1f475eb3a1 100644--- a/apps/client/src/services/live_print.css+++ b/apps/client/src/services/live_print.css@@ -30,4 +30,10 @@body.print-live :not(:has(.print-live-target), .print-live-target, .print-live-target *) {display: none !important;}++ body.print-live .print-live-unscroll {+ overflow: visible !important;+ height: auto !important;+ max-height: none !important;+ }}diff --git a/apps/client/src/services/live_print.spec.ts b/apps/client/src/services/live_print.spec.tsindex 3540ede324..fe642deddd 100644--- a/apps/client/src/services/live_print.spec.ts+++ b/apps/client/src/services/live_print.spec.ts@@ -72,6 +72,24 @@ describe("live print", () => {expect(theme.media).toBe("screen");});+ it("prints scroll containers at full height without a scrollbar", async () => {+ const note = buildNote({ id: "live-scroll", title: "Scroll", type: "render" });+ const target = document.createElement("div");+ target.innerHTML = `+ <div class="scroller" style="overflow-y: auto; max-height: 100px"><table></table></div>+ <div class="clipped" style="overflow: hidden"></div>`;+ document.body.appendChild(target);++ await startLivePrint(note, target);++ expect(target.querySelector(".scroller")?.classList.contains("print-live-unscroll")).toBe(true);+ expect(target.querySelector(".clipped")?.classList.contains("print-live-unscroll")).toBe(false);++ endLivePrint();++ expect(target.querySelector(".print-live-unscroll")).toBeNull();+ });+it("prints through the browser dialog and restores the window afterwards", async () => {const note = buildNote({ id: "live-browser", title: "Browser", type: "render" });const target = document.createElement("div");diff --git a/apps/client/src/services/live_print.ts b/apps/client/src/services/live_print.tsindex 4a08d00336..18ef9591b9 100644--- a/apps/client/src/services/live_print.ts+++ b/apps/client/src/services/live_print.ts@@ -13,6 +13,8 @@ const TARGET_CLASS = "print-live-target";/** Holds the `media` a theme stylesheet had before {@link startLivePrint} limited it to the screen. */const ORIGINAL_MEDIA_ATTR = "data-live-print-media";const PRINT_CSS_ATTR = "data-live-print-css";+/** Marks a scroll container that prints at its full height, without a scrollbar. */+const UNSCROLL_CLASS = "print-live-unscroll";let activeTarget: HTMLElement | undefined;@@ -23,6 +25,13 @@ export async function startLivePrint(note: FNote, target: HTMLElement) {document.body.classList.add(ACTIVE_CLASS);target.classList.add(TARGET_CLASS);+ for (const el of target.querySelectorAll<HTMLElement>("*")) {+ const { overflowX, overflowY } = getComputedStyle(el);+ if ([ overflowX, overflowY ].some((overflow) => overflow === "auto" || overflow === "scroll")) {+ el.classList.add(UNSCROLL_CLASS);+ }+ }+// Paper gets the light baseline (`theme-light.css`), as on the `?print` page, which loads no theme.for (const link of document.querySelectorAll<HTMLLinkElement>("link[data-theme-stylesheet]")) {link.setAttribute(ORIGINAL_MEDIA_ATTR, link.media);@@ -38,6 +47,9 @@ export function endLivePrint() {if (!activeTarget) return;activeTarget.classList.remove(TARGET_CLASS);+ for (const el of activeTarget.querySelectorAll(`.${UNSCROLL_CLASS}`)) {+ el.classList.remove(UNSCROLL_CLASS);+ }activeTarget = undefined;document.body.classList.remove(ACTIVE_CLASS);diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Render Note.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Render Note.htmlindex 93fc8014e1..789e443533 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Render Note.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Render Note.html@@ -86,8 +86,9 @@ $dateEl.text(new Date());</code></pre><ul><li>The content is laid out again at the width of the page, so elements sizedby the script for the screen (such as charts) might not fit.</li>- <li>Containers with a fixed height and scrolling are cut off on paper. Use- a <code>~printCss</code> relation to give them an automatic height.</li>+ <li>Scrolling areas, such as a table in a scrolling container, are printed+ at their full height, without a scrollbar.</li>+ <li>To adjust the layout for printing, use a <code>~printCss</code> relation.</li><li>The light colors are used for printing, whatever the current theme.</li></ul><h2>Examples</h2>diff --git a/docs/User Guide/User Guide/Note Types/Render Note.md b/docs/User Guide/User Guide/Note Types/Render Note.mdindex 135814ee23..c75f7a6a01 100644--- a/docs/User Guide/User Guide/Note Types/Render Note.md+++ b/docs/User Guide/User Guide/Note Types/Render Note.md@@ -72,7 +72,8 @@ It's possible to refresh the note via:A render note can be printed or exported as PDF from the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20menu.md">Note menu</a> once its `~renderNote` relation is set (see <a class="reference-link" href="../Basic%20Concepts%20and%20Features/Notes/Printing%20%26%20Exporting%20as%20PDF.md">Printing & Exporting as PDF</a>). Unlike other notes, it is printed exactly as it is displayed: the script does not run again, so what was typed, selected or loaded in the note is kept.* The content is laid out again at the width of the page, so elements sized by the script for the screen (such as charts) might not fit.-* Containers with a fixed height and scrolling are cut off on paper. Use a `~printCss` relation to give them an automatic height.+* Scrolling areas, such as a table in a scrolling container, are printed at their full height, without a scrollbar.+* To adjust the layout for printing, use a `~printCss` relation.* The light colors are used for printing, whatever the current theme.## Examples