From 052392d7f0d09b3da76e9a22f8b3cb7f20f186d4 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Wed, 11 Jun 2025 00:36:17 -0500 Subject: [PATCH] Add action bindings on ToC component elements --- .../publications/TableOfContents.svelte | 74 +++++++++++-------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/lib/components/publications/TableOfContents.svelte b/src/lib/components/publications/TableOfContents.svelte index 8356509..9a1f252 100644 --- a/src/lib/components/publications/TableOfContents.svelte +++ b/src/lib/components/publications/TableOfContents.svelte @@ -23,50 +23,49 @@ let publicationTree = getContext('publicationTree') as SveltePublicationTree; let toc = new TableOfContents(rootAddress, publicationTree, page.url.pathname ?? ""); - let entries = $derived.by(() => { - console.debug("[ToC] Filtering entries for depth", depth); - const entries = Array + let entries = $derived( + Array .from(toc.addressMap.values()) - .filter((entry) => entry.depth === depth); - console.debug("[ToC] Filtered entries", entries.map((e) => e.title)); - return entries; - }); + .filter((entry) => entry.depth === depth) + ); - // Track the currently visible section for highlighting - let currentSection = $state(null); + function getEntryExpanded(address: string) { + return toc.getEntry(address)?.expanded; + } - // Handle section visibility changes from the IntersectionObserver - function handleSectionVisibility(address: string, isVisible: boolean) { - if (isVisible) { - currentSection = address; + function setEntryExpanded(address: string, expanded: boolean = false) { + const entry = toc.getEntry(address); + if (!entry) { + return; } - } - // Toggle expansion of a ToC entry - async function toggleExpansion(entry: TocEntry) { - // Update the current section in the ToC - const tocEntry = toc.getEntry(entry.address); - if (tocEntry) { - // Ensure the parent sections are expanded - let parent = tocEntry.parent; - while (parent) { - parent.expanded = true; - parent = parent.parent; - } + entry.expanded = expanded; + if (entry.childrenResolved) { + return; } - entry.expanded = !entry.expanded; - if (entry.expanded && !entry.childrenResolved) { - onSectionFocused?.(entry.address); - await entry.resolveChildren(); + if (expanded) { + entry.resolveChildren(); } } + + function handleEntryClick(address: string, expanded: boolean = false) { + setEntryExpanded(address, expanded); + onSectionFocused?.(address); + } + {#if displayMode === 'accordion'} {#each entries as entry} - + {@const address = entry.address} + getEntryExpanded(address), + (open) => setEntryExpanded(address, open) + } + > {#snippet header()} {entry.title} {/snippet} @@ -79,8 +78,15 @@ {:else} {#each entries as entry} + {@const address = entry.address} {#if entry.children.length > 0} - + getEntryExpanded(address), + (open) => setEntryExpanded(address, open) + } + > {:else} - + + handleEntryClick(address, !getEntryExpanded(address))} + /> {/if} {/each}