Skip to content

Build your personal knowledge base with Trilium Notes

client: fix some issues

Adorian Doran31344557d87b
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.ts
index 29e1d95cb2..bcb3305127 100644
--- a/apps/client/src/services/content_renderer.spec.ts
+++ b/apps/client/src/services/content_renderer.spec.ts
@@ -139,7 +139,8 @@ import {
getEmbedBoxSize,
getRenderedContent as rawGetRenderedContent,
getUploadBoxSize,
- hasRenderedPreview
+ hasRenderedPreview,
+ snapshotAndDispose
} from "./content_renderer.js";
import froca from "./froca.js";
import server from "./server.js";
@@ -1183,6 +1184,18 @@ describe("interactive content disposal", () => {
expect(dispose).toHaveBeenCalledTimes(1);
}
});
+
+ it("snapshots the markup without the markers before disposing of the content", async () => {
+ const { $renderedContent } = await getRenderedContent(buildChatNote([ "Hi" ]));
+ const dispose = vi.fn();
+ addContentDisposer($renderedContent[0], dispose);
+
+ const chat = "<span class=\"mock-chat-marker\">messages:1</span>";
+ expect(snapshotAndDispose($renderedContent))
+ .toBe(`<div class="note-detail-llm-chat-preview">${chat}</div>`);
+ expect(dispose).toHaveBeenCalledTimes(1);
+ expect($renderedContent.find(".mock-chat-marker").length).toBe(0);
+ });
});
describe("defensive guards for entities that are neither FNote nor FAttachment", () => {
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index ce482ceefa..e430a13980 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -909,6 +909,20 @@ export function disposeInteractiveContent($renderedContent: JQuery<HTMLElement>)
}
}
+/**
+ * Returns the HTML of the rendered content and then disposes of it, for a caller that keeps only
+ * the markup. The HTML leaves out the markers that {@link disposeInteractiveContent} reads.
+ */
+export function snapshotAndDispose($renderedContent: JQuery<HTMLElement>) {
+ const $snapshot = $renderedContent.clone();
+ disposeInteractiveContent($renderedContent);
+
+ const markers = [ INTERACTIVE_MOUNT_ATTR, DISPOSER_ATTR ];
+ const selector = markers.map((marker) => `[${marker}]`).join(", ");
+ $snapshot.find(selector).addBack(selector).removeAttr(markers.join(" "));
+ return $snapshot.html();
+}
+
/**
* Registers `dispose` to run when {@link disposeInteractiveContent} discards the rendered content
* that holds `element`, such as a pass that keeps observing the content.
diff --git a/apps/client/src/widgets/collections/presentation/model.spec.ts b/apps/client/src/widgets/collections/presentation/model.spec.ts
index c57e8bba9c..161f770534 100644
--- a/apps/client/src/widgets/collections/presentation/model.spec.ts
+++ b/apps/client/src/widgets/collections/presentation/model.spec.ts
@@ -2,12 +2,18 @@
// DOMPurify relies on browser-faithful DOM traversal (NodeIterator); happy-dom
// mishandles it and strips valid markup (surfaced by dompurify 3.4.8). Run the
// sanitization-dependent specs under jsdom, which matches real-browser behavior.
-import { beforeAll, describe, expect, it } from "vitest";
+import { h } from "preact";
+import { beforeAll, describe, expect, it, vi } from "vitest";
import FNote from "../../../entities/fnote";
import { buildNote } from "../../../test/easy-froca";
import { buildPresentationModel, PresentationModel } from "./model";
+vi.mock("../../type_widgets/llm_chat/ChatPreview", () => ({
+ default: ({ messages }: { messages: { content: string }[] }) =>
+ h("p", { class: "chat-preview" }, messages.map((message) => message.content).join(", "))
+}));
+
let presentationNote!: FNote;
let data!: PresentationModel;
@@ -85,4 +91,23 @@ describe("Presentation model", () => {
it("rewrites links even if they are not part of the slideshow", () => {
expect(data.slides[0].verticalSlides![0].content.__html).toStrictEqual(`<div class="ck-content"><p>Go to&nbsp;<a class="reference-link" href="#/slide-other"><span><span class="tn-icon bx bx-note"></span>Other note</span></a>.</p></div>`);
});
+
+ it("keeps the content of a slide that mounts a component", async () => {
+ const messages = [ { id: "m0", role: "user", content: "Hello", createdAt: "2026-01-01" } ];
+ const chatPresentation = buildNote({
+ title: "Chat presentation",
+ "#viewType": "presentation",
+ children: [ {
+ title: "Chat",
+ type: "llmChat",
+ content: JSON.stringify({ version: 1, messages })
+ } ]
+ });
+
+ const { slides } = await buildPresentationModel(chatPresentation);
+
+ expect(slides[0].content.__html).toStrictEqual(
+ `<div class="note-detail-llm-chat-preview"><p class="chat-preview">Hello</p></div>`
+ );
+ });
});
diff --git a/apps/client/src/widgets/collections/presentation/model.ts b/apps/client/src/widgets/collections/presentation/model.ts
index be564c60f8..c16aeb5d91 100644
--- a/apps/client/src/widgets/collections/presentation/model.ts
+++ b/apps/client/src/widgets/collections/presentation/model.ts
@@ -1,6 +1,6 @@
import { NoteType } from "@triliumnext/commons";
import FNote from "../../../entities/fnote";
-import contentRenderer, { disposeInteractiveContent } from "../../../services/content_renderer";
+import contentRenderer, { snapshotAndDispose } from "../../../services/content_renderer";
import { sanitizeNoteContentHtml } from "../../../services/sanitize_content";
import { ProgressChangedFn } from "../interface";
@@ -74,9 +74,7 @@ async function processContent(note: FNote): Promise<DangerouslySetInnerHTML> {
noChildrenList: true,
mediaEnvironment: "native"
});
- // Disposing before the read also drops the `data-content-disposer` markers from the HTML.
- disposeInteractiveContent($renderedContent);
- return { __html: sanitizeNoteContentHtml($renderedContent.html()) };
+ return { __html: sanitizeNoteContentHtml(snapshotAndDispose($renderedContent)) };
}
async function postProcessSlides(slides: (PresentationSlideModel | PresentationSlideBaseModel)[]) {
diff --git a/apps/client/src/widgets/react/ScrollableLabel.spec.tsx b/apps/client/src/widgets/react/ScrollableLabel.spec.tsx
index 84112876f5..1cf032273a 100644
--- a/apps/client/src/widgets/react/ScrollableLabel.spec.tsx
+++ b/apps/client/src/widgets/react/ScrollableLabel.spec.tsx
@@ -2,7 +2,7 @@ import { type ComponentChildren, render } from "preact";
import { act } from "preact/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
-import ScrollableLabel from "./ScrollableLabel";
+import ScrollableLabel, { mountScrollableLabel } from "./ScrollableLabel";
let container: HTMLDivElement | undefined;
@@ -267,3 +267,25 @@ describe("ScrollableLabel", () => {
expect(label.classList.contains("scroll-fade-end")).toBe(false);
});
});
+
+describe("mountScrollableLabel", () => {
+ it("moves the content of an element into a label, and back once disposed of", () => {
+ const element = document.body.appendChild(document.createElement("p"));
+ element.innerHTML = "Long <b>title</b>";
+ const [ text, bold ] = element.childNodes;
+
+ let dispose = () => {};
+ act(() => {
+ dispose = mountScrollableLabel(element);
+ });
+ const content = element.querySelector(":scope > .scrollable-label > span");
+ expect(content?.childNodes[0]).toBe(text);
+ expect(content?.childNodes[1]).toBe(bold);
+
+ const replacement = document.createElement("i");
+ bold.replaceWith(replacement);
+ act(() => dispose());
+ expect([ ...element.childNodes ]).toEqual([ text, replacement ]);
+ element.remove();
+ });
+});
diff --git a/apps/client/src/widgets/react/ScrollableLabel.tsx b/apps/client/src/widgets/react/ScrollableLabel.tsx
index 90805bd8d0..a6ba2c5b03 100644
--- a/apps/client/src/widgets/react/ScrollableLabel.tsx
+++ b/apps/client/src/widgets/react/ScrollableLabel.tsx
@@ -1,7 +1,7 @@
import "./ScrollableLabel.css";
import clsx from "clsx";
-import type { ComponentChildren, RefObject } from "preact";
+import { type ComponentChildren, createRef, type RefObject, render } from "preact";
import { useCallback, useEffect, useRef } from "preact/hooks";
import { useScrollFade } from "./scroll_fade";
@@ -121,6 +121,27 @@ export default function ScrollableLabel({ children, className, autoScroll }: Scr
);
}
+/**
+ * Moves the content of `element`, which Preact does not render, into a {@link ScrollableLabel}
+ * mounted inside it. Returns a function that unmounts the label and puts the content back.
+ */
+export function mountScrollableLabel(element: HTMLElement) {
+ // `render()` reuses or removes the children it finds in `element`.
+ const nodes = [ ...element.childNodes ];
+ element.replaceChildren();
+
+ const contentRef = createRef<HTMLSpanElement>();
+ render(<ScrollableLabel><span ref={contentRef} /></ScrollableLabel>, element);
+ const content = contentRef.current;
+ content?.append(...nodes);
+
+ return () => {
+ const current = content ? [ ...content.childNodes ] : nodes;
+ render(null, element);
+ element.replaceChildren(...current);
+ };
+}
+
/**
* Scrolls the label in `ref` with a mouse dragged along it, as a swipe does on a touch screen.
* While the label overflows, a mouse press on it starts no native drag, such as that of a link, and
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
index be1a3b9155..9a3d07697e 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
@@ -462,3 +462,33 @@ describe("ReadOnlyText tables", () => {
harness.container.remove();
});
});
+
+describe("ReadOnlyText tabs", () => {
+ it("shows each tab title with text as a scrollable label", async () => {
+ const harness = setupHarness({ isVisible: true });
+ harness.note.getBlob = async () => ({
+ content: "<div class=\"trilium-tabs\"><section class=\"trilium-tab\">"
+ + "<p class=\"trilium-tab-title\">First <b>tab</b></p>"
+ + "<div class=\"trilium-tab-panel\"><p>One</p></div></section>"
+ + "<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">&nbsp;</p>"
+ + "<div class=\"trilium-tab-panel\"><p>Two</p></div></section>"
+ + "</div>"
+ }) as never;
+
+ await harness.mount();
+
+ const titles = harness.container.querySelectorAll<HTMLElement>(".trilium-tab-title");
+ const [ first, second ] = titles;
+ const label = first.querySelector(":scope > .scrollable-label");
+ expect(label?.innerHTML).toBe("<span>First <b>tab</b></span>");
+ expect(second.childNodes.length).toBe(0);
+
+ second.click();
+ label?.querySelector("b")?.click();
+ expect(first.parentElement?.classList.contains("trilium-tab--active")).toBe(true);
+ expect(second.parentElement?.classList.contains("trilium-tab--active")).toBe(false);
+
+ await act(async () => render(null, harness.container));
+ harness.container.remove();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index 88055e5db7..5eb24e7b8f 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -8,6 +8,7 @@ import { applyTableScroll } from "@triliumnext/ckeditor5/src/plugins/table_scrol
import {
applyStickyTableHeaders
} from "@triliumnext/ckeditor5/src/plugins/table_sticky_header_read_only.js";
+import { CLASSES as TAB_CLASSES } from "@triliumnext/ckeditor5/src/plugins/tabs/constants.js";
import { applyTabs } from "@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js";
import clsx from "clsx";
import { Ref } from "preact";
@@ -31,6 +32,7 @@ import {
useTriliumEvent, useTriliumOption, useTriliumOptionBool
} from "../../react/hooks";
import { RawHtmlBlock } from "../../react/RawHtml";
+import { mountScrollableLabel } from "../../react/ScrollableLabel";
import { TypeWidgetProps } from "../type_widget";
import { applyReferenceLinks } from "./read_only_helper";
import {
@@ -155,11 +157,18 @@ export function ReadOnlyTextContent({
const disposeStickyTableHeaders = applyStickyTableHeaders(container);
applyMath(container);
applyTabs(container, { placeholder: t("text-editor.ck.tab-title") });
+ const tabTitles = container.querySelectorAll<HTMLElement>(`.${TAB_CLASSES.tabTitle}`);
+ const disposeTabTitles = [ ...tabTitles ]
+ .filter((title) => title.hasChildNodes())
+ .map((title) => mountScrollableLabel(title));
setupImageOpening(container, true);
return () => {
disposeTableScroll();
disposeStickyTableHeaders();
+ for (const dispose of disposeTabTitles) {
+ dispose();
+ }
};
}, [ html, noteId, ntxId, contentRef ]);
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html
index 1ba75c9d44..21b7a633be 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html
@@ -53,6 +53,9 @@
<li>To move between the titles of a block, use the arrow keys, <kbd>Home</kbd> and <kbd>End</kbd>.</li>
</ul>
<p>A tab without a title shows “Tab title” in place of one.</p>
+<p>In read-only notes, a title too long for its tab fades out where it is
+ cut off. To read the rest of it, swipe it sideways or drag it with the
+ mouse.</p>
<h2>Linking to a tab</h2>
<p>A link can point to a single tab, much like a link to a block (see&nbsp;<a
class="reference-link" href="#root/_help_klC3ilXa9kUE">Block references</a>):</p>
diff --git a/docs/User Guide/User Guide/Note Types/Text/Content tabs.md b/docs/User Guide/User Guide/Note Types/Text/Content tabs.md
index f2ce025bce..1e83b5d794 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Content tabs.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Content tabs.md
@@ -38,6 +38,8 @@ In <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/Note
A tab without a title shows “Tab title” in place of one.
+In read-only notes, a title too long for its tab fades out where it is cut off. To read the rest of it, swipe it sideways or drag it with the mouse.
+
## Linking to a tab
A link can point to a single tab, much like a link to a block (see <a class="reference-link" href="Block%20references.md">Block references</a>):
diff --git a/packages/ckeditor5/src/theme/tabs.css b/packages/ckeditor5/src/theme/tabs.css
index 7953e0f540..f996786eff 100644
--- a/packages/ckeditor5/src/theme/tabs.css
+++ b/packages/ckeditor5/src/theme/tabs.css
@@ -106,7 +106,6 @@
}
}
-/* Outlines only the innermost block that holds the focus. */
.ck-editor__editable .trilium-tabs:has(.ck-editor__nested-editable_focused) {
&:not(:has(.trilium-tabs .ck-editor__nested-editable_focused)) {
outline: var(--ck-widget-outline-thickness, 3px) solid var(--tabs-focus-border-color);