diff --git a/quartz/components/Explorer.tsx b/quartz/components/Explorer.tsx index e3ed9b1cc..fdfff23b1 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 8e79d200e..12546bbb0 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" + const childFolderContainer = ( + isSvg + ? target.parentElement?.nextSibling + : target.parentElement?.parentElement?.nextElementSibling + ) as MaybeHTMLElement + const currentFolderParent = ( + isSvg ? target.nextElementSibling : target.parentElement + ) as MaybeHTMLElement + if (!(childFolderContainer && currentFolderParent)) return - // corresponding