| ---
|
| import type { MarkdownHeading } from "astro";
|
| import "@/styles/toc.css";
|
| import WidgetLayout from "@/components/common/WidgetLayout.astro";
|
| import I18nKey from "@/i18n/i18nKey";
|
| import { i18n } from "@/i18n/translation";
|
|
|
| interface Props {
|
| headings: MarkdownHeading[];
|
| class?: string;
|
| style?: string;
|
| }
|
|
|
| const { headings: _headings = [], class: className, style } = Astro.props;
|
| ---
|
|
|
| <WidgetLayout
|
| name={i18n(I18nKey.tableOfContents)}
|
| id="sidebar-toc"
|
| class={className}
|
| style={style}
|
| >
|
| <div class="toc-scroll-container custom-scrollbar">
|
| <div
|
| class="toc-content"
|
| id="sidebar-toc-content"
|
| >
|
|
|
| </div>
|
| </div>
|
| </WidgetLayout>
|
|
|
| <style lang="stylus">
|
|
|
| .toc-scroll-container
|
| max-height: calc(100vh - 20rem)
|
| </style>
|
|
|
| <script>
|
| import { TOCManager } from "@/utils/tocUtils";
|
|
|
| if (typeof window.SidebarTOC === "undefined") {
|
| window.SidebarTOC = {
|
| manager: null
|
| };
|
| }
|
|
|
| async function initSidebarTOC() {
|
| const tocContent = document.getElementById("sidebar-toc-content");
|
| if (!tocContent) return;
|
|
|
|
|
| try {
|
|
|
| if (window.SidebarTOC.manager) {
|
| window.SidebarTOC.manager.cleanup();
|
| }
|
|
|
|
|
| window.SidebarTOC.manager = new TOCManager({
|
| contentId: "sidebar-toc-content",
|
| indicatorId: "sidebar-active-indicator",
|
| maxLevel: 3,
|
| scrollOffset: 80,
|
| });
|
|
|
|
|
| window.SidebarTOC.manager.init();
|
| } catch (error) {
|
| console.error("Failed to load TOCManager for SidebarTOC:", error);
|
| }
|
| }
|
|
|
|
|
|
|
| if (document.readyState === "loading") {
|
| document.addEventListener("DOMContentLoaded", initSidebarTOC);
|
| } else {
|
| initSidebarTOC();
|
| }
|
|
|
|
|
| document.addEventListener("swup:contentReplaced", () => {
|
| setTimeout(initSidebarTOC, 100);
|
| });
|
|
|
|
|
| document.addEventListener("astro:page-load", () => {
|
| setTimeout(initSidebarTOC, 100);
|
| });
|
|
|
|
|
| window.addEventListener("popstate", () => {
|
| setTimeout(initSidebarTOC, 200);
|
| });
|
|
|
|
|
| window.addEventListener("hashchange", () => {
|
| if (!window.tocInternalNavigation) {
|
| setTimeout(initSidebarTOC, 100);
|
| }
|
| window.tocInternalNavigation = false;
|
| });
|
| </script>
|
|
|