Skip to content

Build your personal knowledge base with Trilium Notes

client/AI chat: fix some issues

Adorian Doran3cdb261fbe04
diff --git a/apps/client/src/print.css b/apps/client/src/print.css
index d6541ebc23..447c359ebd 100644
--- a/apps/client/src/print.css
+++ b/apps/client/src/print.css
@@ -255,11 +255,11 @@ body[data-note-type="llmChat"] {
}
body[data-note-type="llmChat"] .llm-chat-message-wrapper {
- contain: none !important;
+ contain: none !important;
}
body[data-note-type="llmChat"] .llm-chat-message-user {
- background: #f0f0f0;
+ background: #f0f0f0;
border: 0.5pt solid #ccc;
print-color-adjust: exact;
-webkit-print-color-adjust: exact;
diff --git a/apps/client/src/print.tsx b/apps/client/src/print.tsx
index fb91f8990f..567d57aa61 100644
--- a/apps/client/src/print.tsx
+++ b/apps/client/src/print.tsx
@@ -119,7 +119,7 @@ export function SingleNoteRenderer({ note, onReady }: RendererProps) {
// passes — mermaid, syntax highlighting, included notes — in a layout effect that
// getRenderedContent does not await. Printing snapshots the page, so wait for those
// to settle first; otherwise they land after the capture and are missing from the PDF.
- await waitForPendingRenders();
+ await waitForPendingRenders(container);
// Wait for all images to load.
const images = Array.from(container.querySelectorAll("img"));
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index d390c9c43a..80047a238f 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -460,8 +460,9 @@ async function renderLlmChat(note: FNote, $renderedContent: JQuery<HTMLElement>,
if (options.tooltip) {
// The chat's markdown renders through the read-only text pipeline, whose passes (mermaid,
// math, syntax highlighting) land after the mount — snapshotting before they settle would
- // freeze half-rendered content into the tooltip.
- await waitForPendingRenders();
+ // freeze half-rendered content into the tooltip. Scoped to this preview, so a hover never
+ // waits on a note rendering in another pane.
+ await waitForPendingRenders(container);
const html = container.innerHTML;
render(null, container);
diff --git a/apps/client/src/services/pending_renders.spec.ts b/apps/client/src/services/pending_renders.spec.ts
index 9ae3a9c189..829bbda827 100644
--- a/apps/client/src/services/pending_renders.spec.ts
+++ b/apps/client/src/services/pending_renders.spec.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
import { trackPendingRender, waitForPendingRenders } from "./pending_renders.js";
@@ -17,34 +17,47 @@ function flushMicrotasks() {
return new Promise<void>((resolve) => setTimeout(resolve, 0));
}
-/** Whether `waitForPendingRenders()` has resolved, without blocking on it. */
-function watchCompletion() {
+/** Whether `waitForPendingRenders(root)` has resolved, without blocking on it. */
+function watchCompletion(root: Node) {
const state = { done: false };
- void waitForPendingRenders().then(() => {
+ void waitForPendingRenders(root).then(() => {
state.done = true;
});
return state;
}
+let root: HTMLElement;
+/** A container rendering inside `root` — what a real transform pass registers itself against. */
+let child: HTMLElement;
+/** A container outside `root`, standing in for a note rendering in another pane. */
+let elsewhere: HTMLElement;
+
describe("pending renders", () => {
+ beforeEach(() => {
+ root = document.createElement("div");
+ child = document.createElement("div");
+ root.appendChild(child);
+ elsewhere = document.createElement("div");
+ });
+
it("resolves immediately when nothing is tracked", async () => {
- await expect(waitForPendingRenders()).resolves.toBeUndefined();
+ await expect(waitForPendingRenders(root)).resolves.toBeUndefined();
});
- it("waits for tracked work, including work scheduled by tracked work", async () => {
+ it("waits for work inside the root, including work scheduled by that work", async () => {
const first = deferred();
const second = deferred();
- trackPendingRender(first.promise);
+ trackPendingRender(child, first.promise);
- const completion = watchCompletion();
- await Promise.resolve();
+ const completion = watchCompletion(root);
+ await flushMicrotasks();
expect(completion.done).toBe(false);
// An included note renders its own content: work that only gets tracked once the first
// pass has finished must still be waited for.
first.resolve();
- trackPendingRender(second.promise);
- await Promise.resolve();
+ trackPendingRender(child, second.promise);
+ await flushMicrotasks();
expect(completion.done).toBe(false);
second.resolve();
@@ -52,12 +65,22 @@ describe("pending renders", () => {
expect(completion.done).toBe(true);
});
+ it("ignores work rendering outside the root", async () => {
+ const unrelated = deferred();
+ trackPendingRender(elsewhere, unrelated.promise);
+
+ // Never resolved: a hover preview must not stall on another pane's render.
+ await expect(waitForPendingRenders(root)).resolves.toBeUndefined();
+
+ unrelated.resolve();
+ });
+
it("does not stall or reject when tracked work fails", async () => {
const failing = deferred();
- trackPendingRender(failing.promise);
+ trackPendingRender(child, failing.promise);
failing.reject(new Error("mermaid blew up"));
- await expect(waitForPendingRenders()).resolves.toBeUndefined();
+ await expect(waitForPendingRenders(root)).resolves.toBeUndefined();
});
});
diff --git a/apps/client/src/services/pending_renders.ts b/apps/client/src/services/pending_renders.ts
index 6a840e60d7..5ac9ecedd2 100644
--- a/apps/client/src/services/pending_renders.ts
+++ b/apps/client/src/services/pending_renders.ts
@@ -8,26 +8,44 @@
* are ready.
*
* That breaks any caller that snapshots the finished DOM instead of watching it: printing / PDF
- * export captures the page once, so a transform that lands after the capture is silently missing from
- * the output. Such a caller awaits {@link waitForPendingRenders} to get a settled DOM.
+ * export captures the page once, and the note tooltip serializes its preview to an HTML string, so a
+ * transform that lands after the capture is silently missing from the output. Such a caller awaits
+ * {@link waitForPendingRenders} on the container it is about to capture.
+ *
+ * Work is tracked against the container it renders into, so waiting is scoped to that subtree: a
+ * tooltip capturing a hovered note never blocks on a heavy note rendering in another pane.
*/
-const pending = new Set<Promise<unknown>>();
+interface PendingRender {
+ /** The element the work renders into — what scopes a wait to the subtree being captured. */
+ container: Node;
+ /** Never rejects: see {@link trackPendingRender}. */
+ work: Promise<unknown>;
+}
+
+const pending = new Set<PendingRender>();
-/** Registers async render work, so that {@link waitForPendingRenders} waits for it. */
-export function trackPendingRender(work: Promise<unknown>) {
+/** Registers async render work against its container, for {@link waitForPendingRenders} to await. */
+export function trackPendingRender(container: Node, work: Promise<unknown>) {
// Failures are the tracked work's own business: a transform that throws still finishes, and a
// caller waiting for a settled DOM should proceed rather than inherit the rejection.
- const tracked = work.catch(() => undefined);
- pending.add(tracked);
- void tracked.then(() => pending.delete(tracked));
+ const entry: PendingRender = { container, work: work.catch(() => undefined) };
+ pending.add(entry);
+ void entry.work.then(() => pending.delete(entry));
}
/**
- * Resolves once all tracked render work has settled, including work that other work scheduled (an
- * included note renders its own content, which highlights its own code blocks, …).
+ * Resolves once the render work within `root` has settled — including work that other work scheduled
+ * (an included note renders its own content, which highlights its own code blocks, …). Work rendering
+ * outside `root` is left alone.
*/
-export async function waitForPendingRenders() {
- while (pending.size > 0) {
- await Promise.all([...pending]);
+export async function waitForPendingRenders(root: Node) {
+ for (;;) {
+ const within = [...pending]
+ .filter(({ container }) => container === root || root.contains(container))
+ .map(({ work }) => work);
+
+ if (!within.length) return;
+
+ await Promise.all(within);
}
}
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index 3871b0334a..7f040d2f82 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -92,7 +92,7 @@ export function ReadOnlyTextContent({ html, ntxId, dir, className, contentRef: e
// which render a whole note of their own — finish after this effect returns. On screen they
// simply paint when ready; a caller that snapshots the DOM instead (printing) has to wait for
// them, so the work is registered rather than dropped on the floor.
- trackPendingRender(Promise.all([
+ trackPendingRender(container, Promise.all([
rewriteMermaidDiagramsInContainer(container),
applyInlineMermaid(container),
applyIncludedNotes(container),