From 0d3cf2922618774fc397dca8cb92fcf76fb0db02 Mon Sep 17 00:00:00 2001
From: Ben Schlegel <31989404+benschlegel@users.noreply.github.com>
Date: Mon, 18 Sep 2023 21:32:00 +0000
Subject: [PATCH] docs: fix explorer example (#483)
---
quartz/components/Explorer.tsx | 35 +++++++++++++++++++++++++++++------
1 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/quartz/components/Explorer.tsx b/quartz/components/Explorer.tsx
index efc9f6a..0bdb5a6 100644
--- a/quartz/components/Explorer.tsx
+++ b/quartz/components/Explorer.tsx
@@ -22,6 +22,7 @@
return -1
}
},
+ order: ["filter", "map", "sort"],
})
export default ((userOpts?: Partial<Options>) => {
function Explorer({ allFiles, displayClass, fileData }: QuartzComponentProps) {
@@ -32,12 +33,33 @@
const fileTree = new FileNode("")
allFiles.forEach((file) => fileTree.add(file, 1))
- // Sort tree (folders first, then files (alphabetic))
- fileTree.sort(opts.sortFn!)
+ /**
+ * Keys of this object must match corresponding function name of `FileNode`,
+ * while values must be the argument that will be passed to the function.
+ *
+ * e.g. entry for FileNode.sort: `sort: opts.sortFn` (value is sort function from options)
+ */
+ const functions = {
+ map: opts.mapFn,
+ sort: opts.sortFn,
+ filter: opts.filterFn,
+ }
- // If provided, apply filter function to fileTree
- if (opts.filterFn) {
- fileTree.filter(opts.filterFn)
+ // Execute all functions (sort, filter, map) that were provided (if none were provided, only default "sort" is applied)
+ if (opts.order) {
+ // Order is important, use loop with index instead of order.map()
+ for (let i = 0; i < opts.order.length; i++) {
+ const functionName = opts.order[i]
+ if (functions[functionName]) {
+ // for every entry in order, call matching function in FileNode and pass matching argument
+ // e.g. i = 0; functionName = "filter"
+ // converted to: (if opts.filterFn) => fileTree.filter(opts.filterFn)
+
+ // @ts-ignore
+ // typescript cant statically check these dynamic references, so manually make sure reference is valid and ignore warning
+ fileTree[functionName].call(fileTree, functions[functionName])
+ }
+ }
}
// Get all folders of tree. Initialize with collapsed state
@@ -73,8 +95,9 @@
</svg>
</button>
<div id="explorer-content">
- <ul class="overflow">
+ <ul class="overflow" id="explorer-ul">
<ExplorerNode node={fileTree} opts={opts} fileData={fileData} />
+ <div id="explorer-end" />
</ul>
</div>
</div>
--
Gitblit v1.10.0