-
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
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
/** @jsx h */
import { h } from "../../../../deps/npm/hastscript/mod.ts";
import type { Document, DocumentDirectory } from "../../../types.ts";
import { buildClasses, css, join } from "../css.ts";
import type { BuildContext, DocumentBuildContext } from "../context.ts";
import { javascript } from "../script.ts";
import * as icons from "../icons/lucide.tsx";
const c = buildClasses("w-dt", [
"root",
"list",
"directoryHeader",
"directory",
"chevron",
"link",
"document",
]);
export const documentTreeStyles = join(
icons.lucideIconStyles,
css`
.${c.root} {
padding: calc(var(--baseline) * 0.25rem) 0.75em;
font-size: 0.85rem;
}
.${c.root}, .${c.list} {
margin: 0;
list-style: none;
}
.${c.list} {
padding: 0;
padding-inline-start: calc(0.5em + 4px);
margin-inline-start: calc(0.5em - 1px);
border-inline-start: 2px solid var(--color-subtle-overlay);
line-height: 2;
}
.${c.directoryHeader} {
display: flex;
justify-content: flex-start;
align-items: center;
gap: 4px;
cursor: pointer;
}
.${c.directoryHeader}::marker,
.${c.directoryHeader}::-webkit-details-marker {
display: none;
}
.${c.link} {
color: var(--color-fg-sub);
text-decoration: none;
}
.${c.link}:hover {
text-decoration: underline;
}
.${c.link}[aria-current] {
font-weight: bold;
}
.${c.directory} {
display: flex;
flex-direction: column;
}
.${c.document} {
margin-inline-start: 1.25em;
}
.${c.chevron} {
color: var(--color-fg-light);
transition: transform 0.1s ease;
}
.${c.directory}:not([open]) > .${c.directoryHeader} > .${c.chevron} {
transform: rotate(-90deg);
}
`,
);
const enum StorageKey {
OpenedPaths = "__macana_doctree_0",
}
export const documentTreeScript = javascript`
function enchanceDocumentTree() {
function loadSavedTreeState() {
const data = window.sessionStorage.getItem("${StorageKey.OpenedPaths}");
if (!data) {
return []
}
const parsed = JSON.parse(data)
if (!Array.isArray(parsed)) {
return []
}
return parsed.filter(path => typeof path === "string");
}
let saved = new Set();
try {
saved = new Set(loadSavedTreeState());
} catch (error) {
console.warn("Failed to restore tree state", { error });
}
for (const dir of Array.from(document.getElementsByClassName("${c.directory}"))) {
const path = dir.dataset.macanaPath;
if (typeof path !== "string") {
continue;
}
if (saved.has(path)) {
dir.open = true;
}
dir.addEventListener("toggle", () => {
if (dir.open) {
saved.add(path);
} else {
saved.delete(path);
}
window.sessionStorage.setItem(
"${StorageKey.OpenedPaths}",
JSON.stringify(Array.from(saved.values()))
);
});
}
}
enchanceDocumentTree();
`;
export interface DocumentTreeProps {
context: Readonly<BuildContext | DocumentBuildContext>;
}
export function documentTree({ context }: DocumentTreeProps) {
return (
<ul className={c.root} lang={context.documentTree.defaultLanguage}>
{context.documentTree.nodes.map((entry) => (
node({
value: entry,
currentPath: "document" in context
? context.document.path
: undefined,
context,
})
))}
</ul>
);
}
interface NodeProps {
value: Document | DocumentDirectory;
currentPath?: readonly string[];
context: Readonly<BuildContext | DocumentBuildContext>;
}
function node({ currentPath, value, context }: NodeProps) {
if ("file" in value) {
const url = context.resolveURL([
...value.path.map((segment) => encodeURIComponent(segment)),
// For trailing slash
"",
]);
const isCurrent = "document" in context &&
value.path.length === context.document.path.length &&
value.path.every((x, i) => context.document.path[i] === x);
return (
<li lang={value.metadata.language ?? undefined} class={c.document}>
<a
className={c.link}
href={url}
aria-current={isCurrent ? "page" : undefined}
>
{value.metadata.title}
</a>
</li>
);
}
const defaultOpened = currentPath && currentPath[0] === value.metadata.name;
return (
<li lang={value.metadata.language ?? undefined}>
<details
className={c.directory}
open={defaultOpened ? "" : undefined}
data-macana-path={value.path.join("/")}
>
<summary className={c.directoryHeader}>
{icons.chevronDown({ className: c.chevron })}
<span>{value.metadata.title}</span>
</summary>
<ul className={c.list}>
{value.entries.map((entry) => (
node({
value: entry,
currentPath: currentPath?.slice(1),
context,
})
))}
</ul>
</details>
</li>
);
}