Spaces:
Sleeping
Sleeping
Download web/src/navigator.js from brianbellx/codehygiene: direct link, hf CLI and curl.
- Browser
- Download file 7.29 kB
-
https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/navigator.js
- Command line
-
hf download hf://spaces/brianbellx/codehygiene/web/src/navigator.js
-
curl -L -o navigator.js https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/navigator.js
7.29 kB
| import { byId, el } from "./dom.js"; | |
| import { plural } from "./labels.js"; | |
| const ROW_INDENT = 12; | |
| const NARROW = "(max-width: 640px)"; | |
| function insertPath(root, file) { | |
| const parts = file.path.split("/"); | |
| let folder = root; | |
| parts.slice(0, -1).forEach((name, depth) => { | |
| const path = parts.slice(0, depth + 1).join("/"); | |
| if (!folder.folders.has(name)) { | |
| folder.folders.set(name, { name, path, folders: new Map(), files: [] }); | |
| } | |
| folder = folder.folders.get(name); | |
| }); | |
| folder.files.push(file); | |
| } | |
| function buildTree(files) { | |
| const root = { name: "", path: "", folders: new Map(), files: [] }; | |
| files.forEach((file) => insertPath(root, file)); | |
| return root; | |
| } | |
| const byName = (left, right) => left.name.localeCompare(right.name); | |
| const byPath = (left, right) => left.path.localeCompare(right.path); | |
| function sortedFolders(folder) { | |
| return [...folder.folders.values()].sort(byName); | |
| } | |
| function sortedFiles(folder) { | |
| return [...folder.files].sort(byPath); | |
| } | |
| function flatten(folder) { | |
| return [...sortedFolders(folder).flatMap(flatten), ...sortedFiles(folder)]; | |
| } | |
| export function fileState(file) { | |
| return file.result.squiggles.length > 0 ? "has-issues" : ""; | |
| } | |
| function folderState(folder) { | |
| return flatten(folder).some((file) => fileState(file) === "has-issues") ? "has-issues" : ""; | |
| } | |
| function ancestors(path) { | |
| const parts = path.split("/").slice(0, -1); | |
| return parts.map((_, index) => parts.slice(0, index + 1).join("/")); | |
| } | |
| export function baseName(path) { | |
| return path.slice(path.lastIndexOf("/") + 1); | |
| } | |
| function fileTitle(file) { | |
| const count = file.result.squiggles.length; | |
| return count > 0 ? `${file.path} · ${plural(count, "issue")}` : file.path; | |
| } | |
| function icon(href, className = "icon") { | |
| const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | |
| svg.setAttribute("class", className); | |
| svg.setAttribute("aria-hidden", "true"); | |
| const use = document.createElementNS("http://www.w3.org/2000/svg", "use"); | |
| use.setAttribute("href", href); | |
| svg.append(use); | |
| return svg; | |
| } | |
| function fileBadge(file) { | |
| const count = file.result.squiggles.length; | |
| return count > 0 ? el("span", { class: "tree-count", text: String(count) }) : ""; | |
| } | |
| function rowClass(state, selected) { | |
| return ["tree-row", state, selected ? "is-current" : ""].filter(Boolean).join(" "); | |
| } | |
| function keepRowVisible(list, row) { | |
| const rowBox = row.getBoundingClientRect(); | |
| const listBox = list.getBoundingClientRect(); | |
| if (rowBox.top < listBox.top) { | |
| list.scrollTop -= listBox.top - rowBox.top + 8; | |
| } else if (rowBox.bottom > listBox.bottom) { | |
| list.scrollTop += rowBox.bottom - listBox.bottom + 8; | |
| } | |
| } | |
| export function createNavigator(onSelect) { | |
| const stage = byId("stage"); | |
| const explorer = byId("explorer"); | |
| const toggle = byId("explorer-toggle"); | |
| const filter = byId("tree-filter"); | |
| const list = byId("tree-list"); | |
| const narrow = window.matchMedia(NARROW); | |
| const open = new Set(); | |
| let files = []; | |
| let order = []; | |
| let current = null; | |
| function setExplorer(visible) { | |
| const label = visible ? "Hide files" : "Show files"; | |
| stage.classList.toggle("is-collapsed", !visible); | |
| toggle.setAttribute("aria-expanded", String(visible)); | |
| toggle.setAttribute("aria-label", label); | |
| toggle.title = label; | |
| } | |
| function explorerVisible() { | |
| return !stage.classList.contains("is-collapsed"); | |
| } | |
| function choose(file) { | |
| if (narrow.matches) { | |
| setExplorer(false); | |
| } | |
| onSelect(file); | |
| } | |
| function folderRow(folder, depth, expanded) { | |
| return el("button", { | |
| class: rowClass(folderState(folder), false), | |
| type: "button", | |
| style: `--depth: ${depth * ROW_INDENT}px`, | |
| "aria-expanded": String(expanded), | |
| onclick: () => { | |
| if (open.has(folder.path)) { | |
| open.delete(folder.path); | |
| } else { | |
| open.add(folder.path); | |
| } | |
| paintList(); | |
| }, | |
| }, [ | |
| icon("#i-chevron", "icon tree-chevron"), | |
| icon("#i-folder"), | |
| el("span", { class: "tree-name", text: folder.name }), | |
| folderState(folder) ? el("span", { class: "tree-mark" }) : "", | |
| ]); | |
| } | |
| function fileRow(file, depth, label) { | |
| const selected = file === current; | |
| return el("button", { | |
| class: rowClass(fileState(file), selected), | |
| type: "button", | |
| style: `--depth: ${depth * ROW_INDENT}px`, | |
| title: fileTitle(file), | |
| "aria-current": selected ? "true" : null, | |
| onclick: () => choose(file), | |
| }, [ | |
| el("span", { class: "tree-spacer" }), | |
| icon("#i-code"), | |
| el("span", { class: "tree-name", text: label }), | |
| fileBadge(file), | |
| ]); | |
| } | |
| function folderRows(folder, depth) { | |
| const rows = []; | |
| for (const child of sortedFolders(folder)) { | |
| const expanded = open.has(child.path); | |
| rows.push(folderRow(child, depth, expanded)); | |
| if (expanded) { | |
| rows.push(...folderRows(child, depth + 1)); | |
| } | |
| } | |
| rows.push(...sortedFiles(folder).map((file) => fileRow(file, depth, baseName(file.path)))); | |
| return rows; | |
| } | |
| function matchingRows(query) { | |
| const matches = order.filter((file) => file.path.toLowerCase().includes(query)); | |
| if (matches.length === 0) { | |
| return [el("p", { class: "tree-empty", text: "No matches" })]; | |
| } | |
| return matches.map((file) => fileRow(file, 0, file.path)); | |
| } | |
| function paintList() { | |
| const query = filter.value.trim().toLowerCase(); | |
| list.replaceChildren(...(query ? matchingRows(query) : folderRows(buildTree(files), 0))); | |
| } | |
| function revealCurrent() { | |
| if (current) { | |
| ancestors(current.path).forEach((path) => open.add(path)); | |
| } | |
| } | |
| function showCurrentRow() { | |
| const row = list.querySelector(".is-current"); | |
| if (row && explorerVisible()) { | |
| keepRowVisible(list, row); | |
| } | |
| } | |
| function step(offset) { | |
| if (order.length === 0) { | |
| return; | |
| } | |
| const index = Math.max(0, order.indexOf(current)); | |
| onSelect(order[(index + offset + order.length) % order.length]); | |
| } | |
| setExplorer(!narrow.matches); | |
| toggle.addEventListener("click", () => setExplorer(!explorerVisible())); | |
| byId("nav-prev").addEventListener("click", () => step(-1)); | |
| byId("nav-next").addEventListener("click", () => step(1)); | |
| filter.addEventListener("input", paintList); | |
| filter.addEventListener("keydown", (event) => { | |
| if (event.key === "Enter") { | |
| list.querySelector(".tree-row:not([aria-expanded])")?.click(); | |
| } | |
| }); | |
| document.addEventListener("pointerdown", (event) => { | |
| const outside = !explorer.contains(event.target) && !toggle.contains(event.target); | |
| if (narrow.matches && explorerVisible() && outside) { | |
| setExplorer(false); | |
| } | |
| }); | |
| return { | |
| render(nextFiles, nextCurrent) { | |
| files = nextFiles; | |
| order = flatten(buildTree(files)); | |
| open.clear(); | |
| current = nextCurrent; | |
| filter.value = ""; | |
| revealCurrent(); | |
| paintList(); | |
| list.scrollTop = 0; | |
| showCurrentRow(); | |
| }, | |
| update(nextFiles, nextCurrent) { | |
| const moved = nextCurrent !== current; | |
| files = nextFiles; | |
| current = nextCurrent; | |
| if (moved) { | |
| revealCurrent(); | |
| } | |
| paintList(); | |
| if (moved) { | |
| showCurrentRow(); | |
| } | |
| }, | |
| }; | |
| } | |