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
Merge remote-tracking branch 'origin/main' into toc_scroll_active
Elian Doran7184cf49302b
diff --cc apps/client/src/widgets/sidebar/TableOfContents.tsxindex 089e549b36,bc15368bf7..abc6add389--- a/apps/client/src/widgets/sidebar/TableOfContents.tsx+++ b/apps/client/src/widgets/sidebar/TableOfContents.tsx@@@ -209,59 -144,58 +210,81 @@@ interface CKHeading extends RawHeading}function EditableTextTableOfContents() {- const { note, noteContext } = useActiveNoteContext();+ const { noteContext } = useActiveNoteContext();const textEditor = useTextEditor(noteContext);const [ headings, setHeadings ] = useState<CKHeading[]>([]);+ const [ scrollingContainer, setScrollingContainer ] = useState<HTMLElement | null>(null);+ // Subscribe to editor changes once per editor instance — crucially NOT keyed on the+ // active note. The CKEditor instance is reused across note switches within a tab (the+ // content is swapped in via `editor.setData()`, which emits `change:data`), so keying+ // this on the note would tear the listener down and re-attach it on every navigation.+ // Because re-attaching is deferred behind an async `import()`, the `setData()` for the+ // freshly-navigated note — and the `change:data` it emits — can fire during that gap+ // with no listener attached, leaving the sidebar stuck on the previous note's headings+ // (especially for large notes, whose content lands well after the switch). A stable+ // per-editor subscription closes that window: the initial extract handles the first+ // note, and every subsequent note's `setData()` re-extracts through the same listener.useEffect(() => {if (!textEditor) return;- const headings = extractTocFromTextEditor(textEditor);- setHeadings(headings);-- // React to changes.- const changeCallback = () => {- const changes = textEditor.model.document.differ.getChanges();-- const affectsHeadings = changes.some( change => {- return (- change.type === 'insert' || change.type === 'remove' ||- (change.type === 'attribute' && attributeChangeAffectsHeading(change, textEditor))- );- });- if (affectsHeadings) {- requestAnimationFrame(() => {- setHeadings(extractTocFromTextEditor(textEditor));+ setHeadings(extractTocFromTextEditor(textEditor));++ // The helper lives in the CKEditor bundle, which is statically heavy but guaranteed+ // to be loaded by now (a text editor instance exists), so resolving it via a dynamic+ // import keeps it out of this component's startup graph.+ let disposed = false;+ let removeListener: (() => void) | undefined;+ void import("@triliumnext/ckeditor5").then(({ attributeChangeAffectsHeading }) => {+ if (disposed) return;++ const changeCallback = () => {+ const changes = textEditor.model.document.differ.getChanges();++ const affectsHeadings = changes.some( change => {+ return (+ change.type === 'insert' || change.type === 'remove' ||+ (change.type === 'attribute' && attributeChangeAffectsHeading(change, textEditor))+ );});- }- };+ if (affectsHeadings) {+ requestAnimationFrame(() => {+ setHeadings(extractTocFromTextEditor(textEditor));+ });+ }+ };- textEditor.model.document.on("change:data", changeCallback);- return () => textEditor.model.document.off("change:data", changeCallback);- }, [ textEditor, note ]);+ textEditor.model.document.on("change:data", changeCallback);+ removeListener = () => textEditor.model.document.off("change:data", changeCallback);+ });++ return () => {+ disposed = true;+ removeListener?.();+ };+ }, [ textEditor ]);+ useEffect(() => {+ if (!textEditor) {+ setScrollingContainer(null);+ return;+ }++ const container = textEditor.editing.view.getDomRoot()?.closest(".scrolling-container") as HTMLElement | null;+ setScrollingContainer(container);+ }, [textEditor]);++ const getHeadingElement = useCallback((heading: CKHeading) => {+ if (!textEditor) return null;++ const viewEl = textEditor.editing.mapper.toViewElement(heading.element);+ if (!viewEl) return null;++ const domEl = textEditor.editing.view.domConverter.mapViewToDom(viewEl);+ return domEl ?? null;+ }, [textEditor]);++ const activeHeadingId = useActiveHeading({ headings, scrollingContainer, getHeadingElement });+const scrollToHeading = useCallback((heading: CKHeading) => {if (!textEditor) return;