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/include notes: allow the embedded content to be viewed in fullscreen
Adorian Dorand33a9502b1e5
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 95743bd9c5..6db8faa379 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -576,7 +576,7 @@ const INTERACTIVE_MOUNT_ATTR = "data-interactive-mount";* `useTriliumEvent` subscriptions actually receive events — a bare standalone Preact root has no* parent component in context, so otherwise e.g. an embedded collection never reacts to new notes.*/-async function mountInteractiveWidget(vnode: JSX.Element, container: HTMLElement) {+export async function mountInteractiveWidget(vnode: JSX.Element, container: HTMLElement) {const [ { renderReactWidgetAtElement }, { default: appContext } ] = await Promise.all([import("../widgets/react/react_utils"),import("../components/app_context")@@ -711,5 +711,6 @@ function getRenderingType(entity: FNote | FAttachment) {export default {getRenderedContent,- disposeInteractiveContent+ disposeInteractiveContent,+ mountInteractiveWidget};diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex ee172dffd8..abf1db5f74 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1565,7 +1565,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t}/* Expandable include note styles */-.include-note-title-row {+.include-note[data-box-size="expandable"] .include-note-title-row {cursor: pointer;}@@ -1580,7 +1580,8 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-tflex-grow: 1;}-.include-note-toggle {+.include-note-toggle,+.include-note-fullscreen {background: none;border: none;padding: 2px;@@ -1594,7 +1595,8 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-tflex-shrink: 0;}-.include-note-toggle:hover {+.include-note-toggle:hover,+.include-note-fullscreen:hover {color: var(--main-link-color);}@@ -1626,6 +1628,35 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-tpadding-top: 0;}+.include-note-content:fullscreen {+ display: flex;+ flex-direction: column;+ background-color: var(--main-background-color);+}++/* Scrolls in place of the fullscreen box, so that the exit button stays put. */+.include-note-content:fullscreen > .rendered-content {+ flex: 1;+ min-height: 0;+ overflow: auto;+}++.include-note-content:not(:fullscreen) > .include-note-fullscreen-controls {+ display: none;+}++.include-note-exit-fullscreen {+ --overlay-group-inset-top:+ calc(var(--safe-area-inset-top, env(safe-area-inset-top)) + var(--overlay-group-inset));+ --overlay-group-inset-end:+ calc(var(--safe-area-inset-right, env(safe-area-inset-right)) + var(--overlay-group-inset));+ z-index: 1;+}++.include-note-exit-fullscreen > button {+ gap: 8px;+}+.alert {padding: 8px 14px;width: auto;diff --git a/apps/client/src/widgets/type_widgets/text/utils.spec.ts b/apps/client/src/widgets/type_widgets/text/utils.spec.tsindex 71a1a3da8d..51f34709f6 100644--- a/apps/client/src/widgets/type_widgets/text/utils.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/utils.spec.ts@@ -1,3 +1,4 @@+import type { VNode } from "preact";import { beforeEach, describe, expect, it, vi } from "vitest";import type FAttachment from "../../../entities/fattachment";@@ -9,9 +10,19 @@ vi.mock("../../../services/froca", () => ({vi.mock("../../../services/link", () => ({default: { createLink: vi.fn() }}));-vi.mock("../../../services/content_renderer", () => ({- default: { getRenderedContent: vi.fn(), disposeInteractiveContent: vi.fn() }-}));+vi.mock("../../../services/content_renderer", async () => {+ const { render } = await import("preact");+ return {+ default: {+ getRenderedContent: vi.fn(),+ disposeInteractiveContent: vi.fn(),+ mountInteractiveWidget: vi.fn(async (vnode: VNode, container: HTMLElement) => {+ render(vnode, container);+ })+ }+ };+});+vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));import content_renderer from "../../../services/content_renderer";import froca from "../../../services/froca";@@ -64,6 +75,15 @@ describe("loadIncludedNote", () => {expect(content_renderer.getRenderedContent).toHaveBeenCalledWith(note, { interactive: true, includesAsReferenceLinks: true, mediaEnvironment: "embedded" });});+ it("offers no fullscreen button at any box size", async () => {+ for (const boxSize of [ "small", "medium", "full", "expandable" ]) {+ const $el = $('<div class="include-note-wrapper">');+ await loadIncludedNote("noteY", $el, boxSize);+ expect($el.find(".include-note-fullscreen, .include-note-fullscreen-controls"))+ .toHaveLength(0);+ }+ });+it("disposes interactive content of a previous render before replacing it", async () => {const $el = $('<div class="include-note-wrapper">');@@ -104,6 +124,84 @@ describe("loadIncludedAttachment", () => {expect($el.find(".include-note-content.type-pdf").text()).toBe("body");});+ it("offers fullscreen from the end of a medium or full box's title row", async () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ // The read-only path reads the box size from the section rather than taking it.+ const boxes: [ JQuery<HTMLElement>, string | undefined ][] = [+ [ $('<div class="include-note-wrapper">'), "medium" ],+ [ $('<section class="include-note" data-box-size="full">'), undefined ]+ ];++ for (const [ $el, boxSize ] of boxes) {+ await loadIncludedAttachment("att1", $el, boxSize);++ const $row = $el.find(".include-note-title-row");+ const button = $row.children("button.include-note-fullscreen.bx-fullscreen")[0];+ expect($row.children().map((_, child) => child.className).get())+ .toEqual([ "include-note-title", "include-note-fullscreen bx bx-fullscreen" ]);+ expect(button.title).toBe("common.fullscreen");++ const content = $el.find(".include-note-content")[0];+ content.requestFullscreen = vi.fn(async () => {});+ const click = new MouseEvent("click", { bubbles: true });+ const stopPropagation = vi.spyOn(click, "stopPropagation");+ button.dispatchEvent(click);+ expect(content.requestFullscreen).toHaveBeenCalledOnce();+ expect(stopPropagation).toHaveBeenCalled();+ }++ // A refused request is logged rather than left unhandled.+ const content = boxes[1][0].find(".include-note-content")[0];+ content.requestFullscreen = vi.fn(async () => {+ throw new Error("Denied");+ });+ boxes[1][0].find("button.include-note-fullscreen")[0].click();+ await vi.waitFor(() => expect(warn).toHaveBeenCalled());+ warn.mockRestore();++ for (const boxSize of [ "small", "expandable" ]) {+ const $el = $('<div class="include-note-wrapper">');+ await loadIncludedAttachment("att1", $el, boxSize);+ expect($el.find(".include-note-fullscreen, .include-note-fullscreen-controls"))+ .toHaveLength(0);+ }+ });++ it("puts a labeled button leaving fullscreen over the top end of the content", async () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ const exitFullscreen = vi.fn<() => Promise<void>>(async () => {});+ Object.defineProperty(document, "exitFullscreen", {+ value: exitFullscreen,+ configurable: true+ });+ const $el = $('<div class="include-note-wrapper">');++ await loadIncludedAttachment("att1", $el, "full");++ const controls = $el.children(".include-note-content")[0].firstElementChild;+ const group = controls?.querySelector<HTMLElement>(".tn-overlay-control-group");+ const button = group?.querySelector("button");+ expect(controls?.className).toBe("include-note-fullscreen-controls");+ expect(group?.className).toContain("include-note-exit-fullscreen");+ expect(group?.dataset.placement).toBe("top-end");+ expect(button?.textContent).toBe("common.exit_fullscreen");+ expect(button?.querySelector(".bx.bx-exit")).not.toBeNull();++ const click = new MouseEvent("click", { bubbles: true });+ const stopPropagation = vi.spyOn(click, "stopPropagation");+ button?.dispatchEvent(click);+ expect(exitFullscreen).toHaveBeenCalledOnce();+ expect(stopPropagation).toHaveBeenCalled();++ // A refused request is logged rather than left unhandled.+ exitFullscreen.mockRejectedValueOnce(new Error("Not in fullscreen"));+ button?.click();+ await vi.waitFor(() => expect(warn).toHaveBeenCalled());++ warn.mockRestore();+ Reflect.deleteProperty(document, "exitFullscreen");+ });+it("leaves the box alone for a deleted attachment", async () => {vi.mocked(froca.getAttachment).mockResolvedValue(null);vi.mocked(link.createLink).mockClear();diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.tsindex dce997e100..e146614274 100644--- a/apps/client/src/widgets/type_widgets/text/utils.ts+++ b/apps/client/src/widgets/type_widgets/text/utils.ts@@ -1,8 +1,12 @@+import { h } from "preact";+import appContext from "../../../components/app_context";import content_renderer from "../../../services/content_renderer";import froca from "../../../services/froca";+import { t } from "../../../services/i18n";import link, { ViewScope } from "../../../services/link";import utils from "../../../services/utils";+import OverlayControlGroup, { OverlayControlButton } from "../../react/OverlayControlGroup";export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>, boxSize?: string) {const note = await froca.getNote(noteId);@@ -40,7 +44,7 @@ export async function loadIncludedAttachment(await fillIncludeBox($el, boxSize, $link, () => content_renderer.getRenderedContent(attachment,{ interactive: true, mediaEnvironment: "embedded" }- ));+ ), { offersFullscreen: true });}/** The href of a reference link to the attachment, or `null` once it is deleted. */@@ -53,11 +57,17 @@ export async function getAttachmentHref(attachmentId: string) {type RenderedContent = Awaited<ReturnType<typeof content_renderer.getRenderedContent>>;+interface IncludeBoxOptions {+ /** Whether a medium or full box ends its title row with a fullscreen button. */+ offersFullscreen?: boolean;+}+async function fillIncludeBox($el: JQuery<HTMLElement>,boxSize: string | undefined,$link: JQuery<HTMLElement>,- renderContent: () => Promise<RenderedContent>+ renderContent: () => Promise<RenderedContent>,+ { offersFullscreen = false }: IncludeBoxOptions = {}) {// The box size is supplied explicitly by the editing-view downcast; for the other// callers (read-only rendering, script API refresh) fall back to reading it from the DOM.@@ -94,11 +104,19 @@ async function fillIncludeBox($content.slideToggle(200);});} else {- // Standard display- $wrapper.append($('<h4 class="include-note-title">').append($link));-+ const $title = $('<h4 class="include-note-title">').append($link);const { $renderedContent, type } = await renderContent();- $wrapper.append($(`<div class="include-note-content type-${type}">`).append($renderedContent));+ const $content = $(`<div class="include-note-content type-${type}">`)+ .append($renderedContent);++ if (offersFullscreen && (effectiveBoxSize === "medium" || effectiveBoxSize === "full")) {+ const $titleRow = $('<div class="include-note-title-row">')+ .append($title, createFullscreenButton($content[0]));+ await mountExitFullscreenControls($content);+ $wrapper.append($titleRow, $content);+ } else {+ $wrapper.append($title, $content);+ }}// Unmount any interactive widgets from a previous render of this include (e.g. on a box-size@@ -108,6 +126,44 @@ async function fillIncludeBox($el.empty().append(isWrapper ? $wrapper.children() : $wrapper);}+/** A button giving `content` the whole screen. */+function createFullscreenButton(content: HTMLElement) {+ return $('<button type="button" class="include-note-fullscreen bx bx-fullscreen">')+ .attr({ title: t("common.fullscreen") })+ .on("click", (e) => {+ e.stopPropagation();+ content.requestFullscreen().catch((error: unknown) => {+ console.warn("Could not show the included content in fullscreen:", error);+ });+ });+}++/**+ * Mounts the button leaving fullscreen at the start of `$content`. The stylesheet shows it only+ * while `$content` has the screen.+ */+async function mountExitFullscreenControls($content: JQuery<HTMLElement>) {+ const container = document.createElement("div");+ container.className = "include-note-fullscreen-controls";+ $content.prepend(container);++ const exitButton = h(OverlayControlButton, {+ icon: "bx-exit",+ text: t("common.exit_fullscreen"),+ onClick: (e) => {+ e.stopPropagation();+ document.exitFullscreen().catch((error: unknown) => {+ console.warn("Could not leave fullscreen:", error);+ });+ }+ });+ await content_renderer.mountInteractiveWidget(h(OverlayControlGroup, {+ placement: "top-end",+ className: "include-note-exit-fullscreen",+ children: exitButton+ }), container);+}+export function refreshIncludedNote(container: HTMLDivElement, noteId: string) {const includedNotes = container.querySelectorAll(`section[data-note-id="${noteId}"]`);for (const includedNote of includedNotes) {diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.htmlindex e518f23fae..03d44612ff 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html@@ -77,6 +77,10 @@at the end of the menu.</li><li>An embed takes the default box size of included notes. To change its size,select the embed and use the box size menu in its toolbar.</li>+ <li>To show an embed of size <em>Medium</em> or <em>Full</em> on the whole screen,+ press the <span class="tn-icon bx bx-fullscreen"></span> <em>Fullscreen</em> button+ at the end of its title. To return, press <span class="tn-icon bx bx-exit"></span> <em>Exit fullscreen</em> in+ the top-right corner, or <kbd>Esc</kbd>.</li><li>To turn the embed back into a link, select it and press the <span class="tn-icon cke cke-link"></span> <em>Convert to link</em> buttonin its toolbar, or right-click its title and select <em>Convert to link</em>,at the end of the menu.</li>diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.mdindex d45f818528..9d61eabb0d 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md@@ -42,6 +42,7 @@ An attachment can be shown in the text instead of linked, the way <a class="ref* To attach files and embed them straight away, select _Attach and embed file_ from the arrow beside the <span class="tn-icon cke cke-paper-clip"></span> _Attach file_ button. Each file gets an embed of its own, which shows the file name while the file uploads.* To embed an attachment that is already linked, right-click a link to it in a text note being edited and select _Convert link to an embed_, at the end of the menu.* An embed takes the default box size of included notes. To change its size, select the embed and use the box size menu in its toolbar.+* To show an embed of size _Medium_ or _Full_ on the whole screen, press the <span class="tn-icon bx bx-fullscreen"></span> _Fullscreen_ button at the end of its title. To return, press <span class="tn-icon bx bx-exit"></span> _Exit fullscreen_ in the top-right corner, or <kbd>Esc</kbd>.* To turn the embed back into a link, select it and press the <span class="tn-icon cke cke-link"></span> _Convert to link_ button in its toolbar, or right-click its title and select _Convert to link_, at the end of the menu.An embed follows its attachment like a link does. Converting the attachment into a note turns the embed into an included note. In a [shared note](../../Advanced%20Usage/Sharing.md), an embedded picture is displayed, and any other attachment appears as a link to download it.