A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
editor: fix TOC active heading detection (#9413)
Copilot26a9086d966c
diff --git a/packages/editor/src/utils/toc.ts b/packages/editor/src/utils/toc.tsindex 980394e2d..4e01d927f 100644--- a/packages/editor/src/utils/toc.ts+++ b/packages/editor/src/utils/toc.ts@@ -33,6 +33,23 @@ const levelsMap: Record<string, number> = {H6: 6};+function getOffsetTopRelativeTo(+ element: HTMLElement,+ ancestor: HTMLElement+): number {+ let top = 0;+ let current: HTMLElement | null = element;+ // Walk up the offsetParent chain until we reach ancestor (the editor root).+ // This correctly handles elements nested inside positioned containers such as+ // callout blocks, where `offsetTop` alone would only be relative to the+ // nearest positioned parent instead of the editor root.+ while (current && current !== ancestor) {+ top += current.offsetTop;+ current = current.offsetParent as HTMLElement | null;+ }+ return top;+}+export function getTableOfContents(content: HTMLElement) {const tableOfContents: TOCItem[] = [];let level = -1;@@ -46,6 +63,9 @@ export function getTableOfContents(content: HTMLElement) {const nodeName = heading.nodeName;const currentHeading = levelsMap[nodeName];+ const isInsideCallout = !!closestWithin(heading, ".callout", content);+ if (isInsideCallout) continue;+level =prevHeading < currentHeading? level + 1@@ -59,7 +79,7 @@ export function getTableOfContents(content: HTMLElement) {level,title,id,- top: (heading as HTMLElement).offsetTop+ top: getOffsetTopRelativeTo(heading, content)});}return tableOfContents;@@ -93,3 +113,16 @@ export function scrollIntoViewById(blockId: string, optionalStyles = "") {);}}++function closestWithin(+ element: Element,+ selector: string,+ boundary: Element+): Element | null {+ let current: Element | null = element;+ while (current && current !== boundary) {+ if (current.matches(selector)) return current;+ current = current.parentElement;+ }+ return null;+}