-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.ts
More file actions
77 lines (62 loc) · 2.18 KB
/
main.ts
File metadata and controls
77 lines (62 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/// <reference types="vite/client" />
export {};
type SketchLoader = () => Promise<unknown>;
const allModules = import.meta.glob<SketchLoader>(["./sketches/**/*.ts", "./doodles/**/*.ts"]);
const EXCLUDE = new Set(["utils"]);
const sketches = Object.fromEntries(
Object.entries(allModules)
.filter(([path]) => {
const name = path.split("/").pop()!.replace(".ts", "");
return !EXCLUDE.has(name);
})
.map(([path, mod]) => [path.replace(/^\.\//, "").replace(/\.ts$/, ""), mod]),
);
const params = new URLSearchParams(location.search);
const sketchParam = params.get("s");
if (sketchParam && sketches[sketchParam]) {
document.body.classList.add("sketch");
await sketches[sketchParam]();
const back = document.createElement("a");
back.href = "/";
back.textContent = "← back";
back.className = "back-link";
document.body.appendChild(back);
} else {
renderPicker();
}
function renderPicker() {
const groups = new Map<string, string[]>();
for (const path of Object.keys(sketches)) {
const parts = path.split("/");
const name = parts.pop()!;
const dir = parts.join("/");
if (!groups.has(dir)) groups.set(dir, []);
groups.get(dir)!.push(name);
}
const sorted = [...groups.entries()].sort(([a], [b]) => a.localeCompare(b));
const groups_html = sorted
.map(([dir, names]) => {
const label = formatGroupLabel(dir);
const links = names
.slice()
.sort()
.map((n) => `<a href="/?s=${dir}/${n}">${n}</a>`)
.join("");
return `<div class="group">
<div class="group-label">${label}</div>
<div class="items">${links}</div>
</div>`;
})
.join("");
document.body.innerHTML = `<h1>generative processing</h1>${groups_html}`;
}
function formatGroupLabel(dir: string): string {
// sketches/v1/circleseed → v1 / <span>circleseed</span>
// doodles → <span>doodles</span>
// archive/article → archive / <span>article</span>
const parts = dir.replace(/^sketches\//, "").split("/");
if (parts.length === 1) return `<span>${parts[0]}</span>`;
const prefix = parts.slice(0, -1).join(" / ");
const last = parts[parts.length - 1];
return `${prefix} / <span>${last}</span>`;
}