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/AI chat: improve the scroll position and ToC current item sync
Adorian Doranc6d02f3a2069
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.tsindex 20c5585a18..4499ae21f9 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts@@ -18,6 +18,13 @@ export interface ChatHeading extends RawHeading {/** How far down the viewport (as a fraction of its height) the "you are here" line sits. */const ACTIVATION_LINE_FRACTION = 0.3;+/**+ * Pixels a clicked heading is scrolled *past* the activation line, so the scroll-spy counts it as crossed+ * despite sub-pixel scroll snapping (device-pixel rounding can leave it a hair short of the line, lighting+ * up the previous entry). A few pixels beats the snapping while staying well within the heading's section.+ */+const ACTIVATION_LINE_MARGIN_PX = 4;+/*** Publishes a table of contents for an AI chat into the note context, so the shared* {@link TableOfContents} widget renders it exactly like the text-note and PDF tables of@@ -42,15 +49,16 @@ export function useChatToc(chat: UseLlmChatReturn, noteContext: NoteContext | unconst container = scrollContainerRef.current;const element = heading.element;if (!container || !element?.isConnected) return;- // Scroll the heading to the very activation line the scroll-spy uses to pick the active entry, so+ // Scroll the heading to the activation line the scroll-spy uses to pick the active entry, so// clicking a heading leaves *that* heading highlighted. Scrolling it to the top instead would drop// the activation line (30% down) into the following section, highlighting the next entry. Same- // content-space math as `measure()` below; round up so the heading lands at or just below the line- // despite sub-pixel scroll rounding (never one entry short).+ // content-space math as `measure()` below; land the heading a few pixels *past* the line so it+ // registers as crossed despite sub-pixel scroll snapping (otherwise it can settle a hair short and+ // the previous entry lights up).const contentTop = container.getBoundingClientRect().top - container.scrollTop;const headingOffset = element.getBoundingClientRect().top - contentTop;const activationOffset = container.clientHeight * ACTIVATION_LINE_FRACTION;- container.scrollTo({ top: Math.ceil(headingOffset - activationOffset), behavior: "smooth" });+ container.scrollTo({ top: headingOffset - activationOffset + ACTIVATION_LINE_MARGIN_PX, behavior: "smooth" });}, [scrollContainerRef]);// Scroll-spy: pick the active heading whenever the timeline scrolls or resizes. Heading