mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-26 21:36:23 +00:00
91f9ae2d71
* feat: add basic explorer structure„ * feat: integrate new component/plugin * feat: add basic explorer structure * feat: add sort to FileNodes * style: improve style for explorer * refactor: remove unused explorer plugin * refactor: clean explorer structure, fix base (toc) * refactor: clean css, respect displayClass * style: add styling to chevron * refactor: clean up debug statements * refactor: remove unused import * fix: clicking folder icon sometimes turns invisible * refactor: clean css * feat(explorer): add config for title * feat: add config for folder click behavior * fix: `no-pointer` not being set for all elements new approach, have one `no-pointer` class, that removes pointer events and one `clickable` class on the svg and button (everything that can normally be clicked). then, find all children with `clickable` and toggle `no-pointer` * fix: bug where nested folders got incorrect height this fixes the bug where nested folders weren't calculating their total height correctly. done by adding class to main container of all children and calculating total * feat: introduce `folderDefaultState` config * feat: store depth for explorer nodes * feat: implement option for collapsed state + bug fixes folderBehavior: "link" still has bad styling, but major bugs with pointers fixed (not clean yet, but working) * fix: default folder icon rotation * fix: hitbox problem with folder links, fix style * fix: redirect url for nested folders * fix: inconsistent behavior with 'collapseFolders' opt * chore: add comments to `ExplorerNode` * feat: save explorer state to local storage (not clean) * feat: rework `getFolders()`, fix localstorage read + write * feat: set folder state from localStorage needs serious refactoring but functional (except folder icon orientation) * fix: folder icon orientation after local storage * feat: add config for `useSavedState` * refactor: clean `explorer.inline.ts` remove unused functions, comments, unused code, add types to EventHandler * refactor: clean explorer merge `isSvg` paths, remove console logs * refactor: add documentation, remove unused funcs * feat: rework folder collapse logic use grids instead of jank scuffed solution with calculating total heights * refactor: remove depth arg from insert * feat: restore collapse functionality to clicks allow folder icon + folder label to collapse folders again * refactor: remove `pointer-event` jank * feat: improve svg viewbox + remove unused props * feat: use css selector to toggle icon rework folder icon to work purely with css instead of JS manipulation * refactor: remove unused cfg * feat: move TOC to right sidebar * refactor: clean css * style: fix overflow + overflow margin * fix: use `resolveRelative` to resolve file paths * fix: `defaultFolderState` config option * refactor: rename import, rename `folderLi` + ul * fix: use `QuartzPluginData` type * docs: add explorer documentation
141 lines
4.7 KiB
TypeScript
141 lines
4.7 KiB
TypeScript
import { FolderState } from "../ExplorerNode"
|
|
|
|
// Current state of folders
|
|
let explorerState: FolderState[]
|
|
|
|
function toggleExplorer(this: HTMLElement) {
|
|
// Toggle collapsed state of entire explorer
|
|
this.classList.toggle("collapsed")
|
|
const content = this.nextElementSibling as HTMLElement
|
|
content.classList.toggle("collapsed")
|
|
content.style.maxHeight = content.style.maxHeight === "0px" ? content.scrollHeight + "px" : "0px"
|
|
}
|
|
|
|
function toggleFolder(evt: MouseEvent) {
|
|
evt.stopPropagation()
|
|
|
|
// Element that was clicked
|
|
const target = evt.target as HTMLElement
|
|
|
|
// Check if target was svg icon or button
|
|
const isSvg = target.nodeName === "svg"
|
|
|
|
// corresponding <ul> element relative to clicked button/folder
|
|
let childFolderContainer: HTMLElement
|
|
|
|
// <li> element of folder (stores folder-path dataset)
|
|
let currentFolderParent: HTMLElement
|
|
|
|
// Get correct relative container and toggle collapsed class
|
|
if (isSvg) {
|
|
childFolderContainer = target.parentElement?.nextSibling as HTMLElement
|
|
currentFolderParent = target.nextElementSibling as HTMLElement
|
|
|
|
childFolderContainer.classList.toggle("open")
|
|
} else {
|
|
childFolderContainer = target.parentElement?.parentElement?.nextElementSibling as HTMLElement
|
|
currentFolderParent = target.parentElement as HTMLElement
|
|
|
|
childFolderContainer.classList.toggle("open")
|
|
}
|
|
if (!childFolderContainer) return
|
|
|
|
// Collapse folder container
|
|
const isCollapsed = childFolderContainer.classList.contains("open")
|
|
setFolderState(childFolderContainer, !isCollapsed)
|
|
|
|
// Save folder state to localStorage
|
|
const clickFolderPath = currentFolderParent.dataset.folderpath as string
|
|
|
|
// Remove leading "/"
|
|
const fullFolderPath = clickFolderPath.substring(1)
|
|
toggleCollapsedByPath(explorerState, fullFolderPath)
|
|
|
|
const stringifiedFileTree = JSON.stringify(explorerState)
|
|
localStorage.setItem("fileTree", stringifiedFileTree)
|
|
}
|
|
|
|
function setupExplorer() {
|
|
// Set click handler for collapsing entire explorer
|
|
const explorer = document.getElementById("explorer")
|
|
|
|
// Get folder state from local storage
|
|
const storageTree = localStorage.getItem("fileTree")
|
|
|
|
// Convert to bool
|
|
const useSavedFolderState = explorer?.dataset.savestate === "true"
|
|
|
|
if (explorer) {
|
|
// Get config
|
|
const collapseBehavior = explorer.dataset.behavior
|
|
|
|
// Add click handlers for all folders (click handler on folder "label")
|
|
if (collapseBehavior === "collapse") {
|
|
Array.prototype.forEach.call(
|
|
document.getElementsByClassName("folder-button"),
|
|
function (item) {
|
|
item.removeEventListener("click", toggleFolder)
|
|
item.addEventListener("click", toggleFolder)
|
|
},
|
|
)
|
|
}
|
|
|
|
// Add click handler to main explorer
|
|
explorer.removeEventListener("click", toggleExplorer)
|
|
explorer.addEventListener("click", toggleExplorer)
|
|
}
|
|
|
|
// Set up click handlers for each folder (click handler on folder "icon")
|
|
Array.prototype.forEach.call(document.getElementsByClassName("folder-icon"), function (item) {
|
|
item.removeEventListener("click", toggleFolder)
|
|
item.addEventListener("click", toggleFolder)
|
|
})
|
|
|
|
if (storageTree && useSavedFolderState) {
|
|
// Get state from localStorage and set folder state
|
|
explorerState = JSON.parse(storageTree)
|
|
explorerState.map((folderUl) => {
|
|
// grab <li> element for matching folder path
|
|
const folderLi = document.querySelector(
|
|
`[data-folderpath='/${folderUl.path}']`,
|
|
) as HTMLElement
|
|
|
|
// Get corresponding content <ul> tag and set state
|
|
const folderUL = folderLi.parentElement?.nextElementSibling as HTMLElement
|
|
setFolderState(folderUL, folderUl.collapsed)
|
|
})
|
|
} else {
|
|
// If tree is not in localStorage or config is disabled, use tree passed from Explorer as dataset
|
|
explorerState = JSON.parse(explorer?.dataset.tree as string)
|
|
}
|
|
}
|
|
|
|
window.addEventListener("resize", setupExplorer)
|
|
document.addEventListener("nav", () => {
|
|
setupExplorer()
|
|
})
|
|
|
|
/**
|
|
* Toggles the state of a given folder
|
|
* @param folderElement <div class="folder-outer"> Element of folder (parent)
|
|
* @param collapsed if folder should be set to collapsed or not
|
|
*/
|
|
function setFolderState(folderElement: HTMLElement, collapsed: boolean) {
|
|
if (collapsed) {
|
|
folderElement?.classList.remove("open")
|
|
} else {
|
|
folderElement?.classList.add("open")
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Toggles visibility of a folder
|
|
* @param array array of FolderState (`fileTree`, either get from local storage or data attribute)
|
|
* @param path path to folder (e.g. 'advanced/more/more2')
|
|
*/
|
|
function toggleCollapsedByPath(array: FolderState[], path: string) {
|
|
const entry = array.find((item) => item.path === path)
|
|
if (entry) {
|
|
entry.collapsed = !entry.collapsed
|
|
}
|
|
}
|