From 76be137283a497c88b7da445cd9f4b8533a04f35 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Mon, 29 Jan 2024 00:56:12 -0800 Subject: [PATCH] fix: attempt to merge cached folder state between builds (closes #691) --- quartz/components/Explorer.tsx | 3 +- quartz/components/scripts/explorer.inline.ts | 156 ++++++++----------- quartz/components/scripts/toc.inline.ts | 6 +- 3 files changed, 68 insertions(+), 97 deletions(-) diff --git a/quartz/components/Explorer.tsx b/quartz/components/Explorer.tsx index e3ed9b1..fdfff23 100644 --- a/quartz/components/Explorer.tsx +++ b/quartz/components/Explorer.tsx @@ -69,9 +69,8 @@ export default ((userOpts?: Partial) => { } // Get all folders of tree. Initialize with collapsed state - const folders = fileTree.getFolderPaths(opts.folderDefaultState === "collapsed") - // Stringify to pass json tree as data attribute ([data-tree]) + const folders = fileTree.getFolderPaths(opts.folderDefaultState === "collapsed") jsonTree = JSON.stringify(folders) } diff --git a/quartz/components/scripts/explorer.inline.ts b/quartz/components/scripts/explorer.inline.ts index 8e79d20..12546bb 100644 --- a/quartz/components/scripts/explorer.inline.ts +++ b/quartz/components/scripts/explorer.inline.ts @@ -1,132 +1,106 @@ import { FolderState } from "../ExplorerNode" -// Current state of folders -let explorerState: FolderState[] - +type MaybeHTMLElement = HTMLElement | undefined +let currentExplorerState: FolderState[] const observer = new IntersectionObserver((entries) => { // If last element is observed, remove gradient of "overflow" class so element is visible - const explorer = document.getElementById("explorer-ul") + const explorerUl = document.getElementById("explorer-ul") + if (!explorerUl) return for (const entry of entries) { if (entry.isIntersecting) { - explorer?.classList.add("no-background") + explorerUl.classList.add("no-background") } else { - explorer?.classList.remove("no-background") + explorerUl.classList.remove("no-background") } } }) function toggleExplorer(this: HTMLElement) { - // Toggle collapsed state of entire explorer this.classList.toggle("collapsed") - const content = this.nextElementSibling as HTMLElement + const content = this.nextElementSibling as MaybeHTMLElement + if (!content) return + content.classList.toggle("collapsed") content.style.maxHeight = content.style.maxHeight === "0px" ? content.scrollHeight + "px" : "0px" } function toggleFolder(evt: MouseEvent) { evt.stopPropagation() + const target = evt.target as MaybeHTMLElement + if (!target) return - // Element that was clicked - const target = evt.target as HTMLElement - - // Check if target was svg icon or button const isSvg = target.nodeName === "svg" - - // corresponding