markdotserver 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- markdotserver/__init__.py +7 -0
- markdotserver/__main__.py +252 -0
- markdotserver/app.py +191 -0
- markdotserver/config.py +118 -0
- markdotserver/filesystem.py +547 -0
- markdotserver/graphviz_renderer.py +333 -0
- markdotserver/markdown_renderer.py +516 -0
- markdotserver/py.typed +1 -0
- markdotserver/routes.py +741 -0
- markdotserver/static/css/app.css +888 -0
- markdotserver/static/js/app.js +741 -0
- markdotserver/static/js/theme.js +24 -0
- markdotserver/templates/base.html +19 -0
- markdotserver/templates/index.html +41 -0
- markdotserver/templates/partials/directory_tree.html +44 -0
- markdotserver/templates/partials/document.html +71 -0
- markdotserver/templates/partials/error.html +7 -0
- markdotserver/templates/partials/headings.html +24 -0
- markdotserver/templates/partials/toolbar.html +46 -0
- markdotserver-0.1.0.dist-info/METADATA +243 -0
- markdotserver-0.1.0.dist-info/RECORD +25 -0
- markdotserver-0.1.0.dist-info/WHEEL +5 -0
- markdotserver-0.1.0.dist-info/entry_points.txt +2 -0
- markdotserver-0.1.0.dist-info/licenses/LICENSE +21 -0
- markdotserver-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,741 @@
|
|
|
1
|
+
/* SPDX-FileCopyrightText: 2026 Alexey Agafonov */
|
|
2
|
+
/* SPDX-License-Identifier: MIT */
|
|
3
|
+
/* markdotserver — theme, sidebar, tabs, tree, headings, navigation and diagram controls. */
|
|
4
|
+
(() => {
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const KEYS = {
|
|
8
|
+
theme: "markdotserver.theme",
|
|
9
|
+
sidebarWidth: "markdotserver.sidebarWidth",
|
|
10
|
+
sidebarCollapsed: "markdotserver.sidebarCollapsed",
|
|
11
|
+
activeTab: "markdotserver.activeTab",
|
|
12
|
+
expandedDirs: "markdotserver.expandedDirs",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const root = document.documentElement;
|
|
16
|
+
const darkQuery = window.matchMedia
|
|
17
|
+
? window.matchMedia("(prefers-color-scheme: dark)")
|
|
18
|
+
: null;
|
|
19
|
+
|
|
20
|
+
let headingObserver = null;
|
|
21
|
+
let sidebarWidth = 288;
|
|
22
|
+
|
|
23
|
+
function read(key, fallback) {
|
|
24
|
+
try {
|
|
25
|
+
const value = window.localStorage.getItem(key);
|
|
26
|
+
return value === null ? fallback : value;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
return fallback;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function write(key, value) {
|
|
33
|
+
try {
|
|
34
|
+
window.localStorage.setItem(key, value);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
/* storage may be unavailable or full */
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function decodePath(value) {
|
|
41
|
+
try {
|
|
42
|
+
return decodeURIComponent(value);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function layout() {
|
|
49
|
+
return document.getElementById("app-layout");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isCollapsed() {
|
|
53
|
+
return Boolean(layout() && layout().classList.contains("sidebar-collapsed"));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* Theme ----------------------------------------------------------------- */
|
|
57
|
+
|
|
58
|
+
function applyTheme(theme) {
|
|
59
|
+
const value = theme === "dark" ? "dark" : "light";
|
|
60
|
+
root.setAttribute("data-theme", value);
|
|
61
|
+
document.querySelectorAll("#theme-toggle").forEach((button) => {
|
|
62
|
+
const dark = value === "dark";
|
|
63
|
+
button.setAttribute("aria-pressed", String(dark));
|
|
64
|
+
button.textContent = dark ? "Light mode" : "Dark mode";
|
|
65
|
+
button.title = dark ? "Switch to light theme" : "Switch to dark theme";
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function toggleTheme() {
|
|
70
|
+
const next = root.getAttribute("data-theme") === "dark" ? "light" : "dark";
|
|
71
|
+
applyTheme(next);
|
|
72
|
+
write(KEYS.theme, next);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* Sidebar --------------------------------------------------------------- */
|
|
76
|
+
|
|
77
|
+
function applySidebarWidth(width) {
|
|
78
|
+
const available = Math.min(720, Math.max(240, window.innerWidth * 0.7));
|
|
79
|
+
const clamped = Math.max(180, Math.min(Number(width) || 288, available));
|
|
80
|
+
sidebarWidth = Math.round(clamped);
|
|
81
|
+
root.style.setProperty("--sidebar-width", `${sidebarWidth}px`);
|
|
82
|
+
return sidebarWidth;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function updateSidebarToggle() {
|
|
86
|
+
const collapsed = isCollapsed();
|
|
87
|
+
document.querySelectorAll("#sidebar-toggle").forEach((button) => {
|
|
88
|
+
button.setAttribute("aria-expanded", String(!collapsed));
|
|
89
|
+
button.textContent = collapsed ? "Show sidebar" : "Hide sidebar";
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function setCollapsed(collapsed, persist = true) {
|
|
94
|
+
if (layout()) {
|
|
95
|
+
layout().classList.toggle("sidebar-collapsed", collapsed);
|
|
96
|
+
}
|
|
97
|
+
updateSidebarToggle();
|
|
98
|
+
if (persist) write(KEYS.sidebarCollapsed, String(collapsed));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function toggleSidebar() {
|
|
102
|
+
setCollapsed(!isCollapsed());
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function initResizer() {
|
|
106
|
+
const handle = document.getElementById("sidebar-resizer");
|
|
107
|
+
if (!handle || !layout()) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let dragging = false;
|
|
112
|
+
|
|
113
|
+
handle.addEventListener("pointerdown", (event) => {
|
|
114
|
+
if (event.button !== 0) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
dragging = true;
|
|
118
|
+
document.body.classList.add("resizing");
|
|
119
|
+
if (handle.setPointerCapture) {
|
|
120
|
+
handle.setPointerCapture(event.pointerId);
|
|
121
|
+
}
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
handle.addEventListener("pointermove", (event) => {
|
|
126
|
+
if (!dragging) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const left = layout().getBoundingClientRect().left;
|
|
130
|
+
applySidebarWidth(event.clientX - left);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
function stop(event) {
|
|
134
|
+
if (!dragging) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
dragging = false;
|
|
138
|
+
document.body.classList.remove("resizing");
|
|
139
|
+
if (handle.releasePointerCapture && event.pointerId !== undefined) {
|
|
140
|
+
try {
|
|
141
|
+
handle.releasePointerCapture(event.pointerId);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
/* capture already released */
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
write(KEYS.sidebarWidth, String(sidebarWidth));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
handle.addEventListener("pointerup", stop);
|
|
150
|
+
handle.addEventListener("pointercancel", stop);
|
|
151
|
+
|
|
152
|
+
handle.addEventListener("keydown", (event) => {
|
|
153
|
+
const step = event.shiftKey ? 48 : 16;
|
|
154
|
+
if (event.key === "ArrowLeft") {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
write(KEYS.sidebarWidth, String(applySidebarWidth(sidebarWidth - step)));
|
|
157
|
+
} else if (event.key === "ArrowRight") {
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
write(KEYS.sidebarWidth, String(applySidebarWidth(sidebarWidth + step)));
|
|
160
|
+
} else if (event.key === "Home") {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
write(KEYS.sidebarWidth, String(applySidebarWidth(180)));
|
|
163
|
+
} else if (event.key === "End") {
|
|
164
|
+
event.preventDefault();
|
|
165
|
+
write(KEYS.sidebarWidth, String(applySidebarWidth(720)));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function initViewportWatcher() {
|
|
171
|
+
/* applySidebarWidth clamps against the viewport at call time only, so
|
|
172
|
+
shrinking the window (or rotating a device) can leave the sidebar wider
|
|
173
|
+
than the space available. Re-clamp after the resize settles. */
|
|
174
|
+
if (!window.addEventListener) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
let timer = 0;
|
|
178
|
+
window.addEventListener("resize", () => {
|
|
179
|
+
window.clearTimeout(timer);
|
|
180
|
+
timer = window.setTimeout(() => applySidebarWidth(sidebarWidth), 120);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/* Tabs ------------------------------------------------------------------ */
|
|
185
|
+
|
|
186
|
+
function activateTab(name, persist = true) {
|
|
187
|
+
const value = name === "headings" ? "headings" : "files";
|
|
188
|
+
document.querySelectorAll("[data-tab]").forEach((button) => {
|
|
189
|
+
const active = button.dataset.tab === value;
|
|
190
|
+
button.setAttribute("aria-selected", String(active));
|
|
191
|
+
button.classList.toggle("active", active);
|
|
192
|
+
button.tabIndex = active ? 0 : -1;
|
|
193
|
+
});
|
|
194
|
+
document.querySelectorAll("[data-panel]").forEach((panel) => {
|
|
195
|
+
panel.hidden = panel.dataset.panel !== value;
|
|
196
|
+
});
|
|
197
|
+
if (persist) {
|
|
198
|
+
write(KEYS.activeTab, value);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* Directory tree -------------------------------------------------------- */
|
|
203
|
+
|
|
204
|
+
function storedExpanded() {
|
|
205
|
+
const raw = read(KEYS.expandedDirs, "[]");
|
|
206
|
+
try {
|
|
207
|
+
const parsed = JSON.parse(raw);
|
|
208
|
+
if (Array.isArray(parsed)) {
|
|
209
|
+
return new Set(parsed.filter((item) => typeof item === "string"));
|
|
210
|
+
}
|
|
211
|
+
} catch (error) {
|
|
212
|
+
/* ignore malformed storage */
|
|
213
|
+
}
|
|
214
|
+
return new Set();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* localStorage is finite and shared; a user browsing many trees should not
|
|
218
|
+
grow the entry list without bound. Keep the most recently saved entries. */
|
|
219
|
+
const EXPANDED_DIRS_LIMIT = 500;
|
|
220
|
+
|
|
221
|
+
function saveExpanded(expanded) {
|
|
222
|
+
let items = [...expanded];
|
|
223
|
+
if (items.length > EXPANDED_DIRS_LIMIT) {
|
|
224
|
+
items = items.slice(items.length - EXPANDED_DIRS_LIMIT);
|
|
225
|
+
}
|
|
226
|
+
write(KEYS.expandedDirs, JSON.stringify(items));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function selectedPath() {
|
|
230
|
+
const match = window.location.pathname.match(/^\/view\/(.+)$/);
|
|
231
|
+
return match ? decodePath(match[1]) : "";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function ancestorPaths(path) {
|
|
235
|
+
const parts = path.split("/");
|
|
236
|
+
const ancestors = [];
|
|
237
|
+
for (let index = 1; index < parts.length; index += 1) {
|
|
238
|
+
ancestors.push(parts.slice(0, index).join("/"));
|
|
239
|
+
}
|
|
240
|
+
return ancestors;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function syncSelectedTree() {
|
|
244
|
+
const current = selectedPath();
|
|
245
|
+
document.querySelectorAll(".tree-file").forEach((item) => {
|
|
246
|
+
const link = item.querySelector("a");
|
|
247
|
+
if (!link) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const href = link.getAttribute("href") || "";
|
|
251
|
+
const path = href.startsWith("/view/")
|
|
252
|
+
? decodePath(href.slice(6).split("?")[0].split("#")[0])
|
|
253
|
+
: "";
|
|
254
|
+
const selected = path !== "" && path === current;
|
|
255
|
+
item.classList.toggle("selected", selected);
|
|
256
|
+
if (selected) {
|
|
257
|
+
link.setAttribute("aria-current", "page");
|
|
258
|
+
} else {
|
|
259
|
+
link.removeAttribute("aria-current");
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function applyTreeState() {
|
|
265
|
+
const expanded = storedExpanded();
|
|
266
|
+
ancestorPaths(selectedPath()).forEach((path) => expanded.add(path));
|
|
267
|
+
document.querySelectorAll("details[data-dir]").forEach((details) => {
|
|
268
|
+
if (expanded.has(details.dataset.dir)) {
|
|
269
|
+
details.open = true;
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
syncSelectedTree();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function initTreePersistence() {
|
|
276
|
+
document.addEventListener(
|
|
277
|
+
"toggle",
|
|
278
|
+
(event) => {
|
|
279
|
+
const details = event.target;
|
|
280
|
+
if (!(details instanceof HTMLDetailsElement) || !details.dataset.dir) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const expanded = storedExpanded();
|
|
284
|
+
if (details.open) {
|
|
285
|
+
expanded.add(details.dataset.dir);
|
|
286
|
+
} else {
|
|
287
|
+
expanded.delete(details.dataset.dir);
|
|
288
|
+
}
|
|
289
|
+
saveExpanded(expanded);
|
|
290
|
+
},
|
|
291
|
+
true,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/* Headings -------------------------------------------------------------- */
|
|
296
|
+
|
|
297
|
+
function initHeadings() {
|
|
298
|
+
if (headingObserver) {
|
|
299
|
+
headingObserver.disconnect();
|
|
300
|
+
headingObserver = null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const content = document.getElementById("content");
|
|
304
|
+
const panel = document.getElementById("headings");
|
|
305
|
+
if (!content || !panel) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const links = [...panel.querySelectorAll('a[href^="#"]')];
|
|
310
|
+
const targets = new Map();
|
|
311
|
+
links.forEach((link) => {
|
|
312
|
+
const id = link.getAttribute("href").slice(1);
|
|
313
|
+
const target = document.getElementById(id);
|
|
314
|
+
if (target) {
|
|
315
|
+
targets.set(target, link);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
links.forEach((link) => {
|
|
320
|
+
link.classList.remove("active");
|
|
321
|
+
link.removeAttribute("aria-current");
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
if (!targets.size || !("IntersectionObserver" in window)) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
headingObserver = new IntersectionObserver(
|
|
329
|
+
(entries) => {
|
|
330
|
+
entries.forEach((entry) => {
|
|
331
|
+
if (!entry.isIntersecting) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const link = targets.get(entry.target);
|
|
335
|
+
if (!link) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
links.forEach((item) => {
|
|
339
|
+
item.classList.remove("active");
|
|
340
|
+
item.removeAttribute("aria-current");
|
|
341
|
+
});
|
|
342
|
+
link.classList.add("active");
|
|
343
|
+
link.setAttribute("aria-current", "location");
|
|
344
|
+
});
|
|
345
|
+
},
|
|
346
|
+
{ root: content, rootMargin: "-10% 0px -70% 0px", threshold: 0 },
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
targets.forEach((_link, target) => headingObserver.observe(target));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function scrollToHash() {
|
|
353
|
+
const hash = effectiveHash();
|
|
354
|
+
if (!hash) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
let id;
|
|
358
|
+
try {
|
|
359
|
+
id = decodeURIComponent(hash.slice(1));
|
|
360
|
+
} catch (error) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const target = document.getElementById(id);
|
|
364
|
+
if (!target) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
window.requestAnimationFrame(() => {
|
|
368
|
+
target.scrollIntoView({ block: "start" });
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/* Diagrams -------------------------------------------------------------- */
|
|
373
|
+
|
|
374
|
+
let pendingDiagramAction = null;
|
|
375
|
+
|
|
376
|
+
function diagramImage() {
|
|
377
|
+
return document.getElementById("diagram-image");
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function currentScale() {
|
|
381
|
+
const image = diagramImage();
|
|
382
|
+
const value = image ? Number.parseFloat(image.dataset.scale || "1") : 1;
|
|
383
|
+
return Number.isFinite(value) && value > 0 ? value : 1;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function applyDiagramScale(scale) {
|
|
387
|
+
const image = diagramImage();
|
|
388
|
+
if (!image) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const naturalWidth = image.naturalWidth || image.width || 0;
|
|
392
|
+
if (!naturalWidth) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
const value = Math.max(0.1, Math.min(scale, 8));
|
|
396
|
+
image.dataset.scale = String(value);
|
|
397
|
+
image.style.maxWidth = "none";
|
|
398
|
+
image.style.width = `${Math.round(naturalWidth * value)}px`;
|
|
399
|
+
image.style.height = "auto";
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function fitDiagram() {
|
|
403
|
+
const image = diagramImage();
|
|
404
|
+
const viewport = document.getElementById("diagram-viewport");
|
|
405
|
+
if (!image || !viewport || !image.naturalWidth) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const available = Math.max(120, viewport.clientWidth - 32);
|
|
409
|
+
applyDiagramScale(Math.min(1, available / image.naturalWidth));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function initDiagram() {
|
|
413
|
+
const image = diagramImage();
|
|
414
|
+
pendingDiagramAction = null;
|
|
415
|
+
if (!image) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (!image.dataset.scale) {
|
|
419
|
+
image.dataset.scale = "1";
|
|
420
|
+
}
|
|
421
|
+
// A control clicked before the SVG has decoded would be a silent no-op,
|
|
422
|
+
// because naturalWidth is still 0. Replay the last request once it loads.
|
|
423
|
+
image.addEventListener(
|
|
424
|
+
"load",
|
|
425
|
+
() => {
|
|
426
|
+
const action = pendingDiagramAction;
|
|
427
|
+
pendingDiagramAction = null;
|
|
428
|
+
if (action) {
|
|
429
|
+
handleDiagramAction(action);
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
{ once: true },
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function handleDiagramAction(action) {
|
|
437
|
+
const image = diagramImage();
|
|
438
|
+
if (!image) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (!image.naturalWidth) {
|
|
442
|
+
pendingDiagramAction = action;
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
pendingDiagramAction = null;
|
|
446
|
+
if (action === "zoom-in") {
|
|
447
|
+
applyDiagramScale(currentScale() * 1.2);
|
|
448
|
+
} else if (action === "zoom-out") {
|
|
449
|
+
applyDiagramScale(currentScale() / 1.2);
|
|
450
|
+
} else if (action === "fit") {
|
|
451
|
+
fitDiagram();
|
|
452
|
+
} else if (action === "reset") {
|
|
453
|
+
applyDiagramScale(1);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/* Fragment navigation ---------------------------------------------------- */
|
|
458
|
+
|
|
459
|
+
let activeFragment = null;
|
|
460
|
+
let fragmentToken = 0;
|
|
461
|
+
let fragmentLocation = window.location.pathname + window.location.search;
|
|
462
|
+
|
|
463
|
+
/* The skip link targets the content pane itself, so its hash must not be
|
|
464
|
+
mistaken for a heading deep link. */
|
|
465
|
+
const CONTENT_ANCHOR = "#content";
|
|
466
|
+
|
|
467
|
+
function effectiveHash() {
|
|
468
|
+
const hash = window.location.hash;
|
|
469
|
+
return hash === CONTENT_ANCHOR ? "" : hash;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function refreshToolbarState() {
|
|
473
|
+
applyTheme(root.getAttribute("data-theme") || "light");
|
|
474
|
+
updateSidebarToggle();
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function afterFragmentSwap() {
|
|
478
|
+
refreshToolbarState();
|
|
479
|
+
applyTreeState();
|
|
480
|
+
initHeadings();
|
|
481
|
+
initDiagram();
|
|
482
|
+
scrollToHash();
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function swapFragment(html, targetSelector, swapMode) {
|
|
486
|
+
const template = document.createElement("template");
|
|
487
|
+
template.innerHTML = html;
|
|
488
|
+
const incoming = template.content;
|
|
489
|
+
|
|
490
|
+
incoming.querySelectorAll("[data-swap-oob]").forEach((element) => {
|
|
491
|
+
const current = element.id ? document.getElementById(element.id) : null;
|
|
492
|
+
if (current) {
|
|
493
|
+
current.replaceWith(element);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
const target = document.querySelector(targetSelector);
|
|
498
|
+
if (!target) {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
if (swapMode === "outerHTML") {
|
|
502
|
+
target.replaceWith(...incoming.childNodes);
|
|
503
|
+
} else {
|
|
504
|
+
target.replaceChildren(...incoming.childNodes);
|
|
505
|
+
}
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
async function fetchFragment(url) {
|
|
510
|
+
if (activeFragment) {
|
|
511
|
+
activeFragment.abort();
|
|
512
|
+
}
|
|
513
|
+
const controller = new AbortController();
|
|
514
|
+
activeFragment = controller;
|
|
515
|
+
fragmentToken += 1;
|
|
516
|
+
const token = fragmentToken;
|
|
517
|
+
try {
|
|
518
|
+
const response = await fetch(url, {
|
|
519
|
+
headers: { "X-Fragment": "true" },
|
|
520
|
+
credentials: "same-origin",
|
|
521
|
+
signal: controller.signal,
|
|
522
|
+
});
|
|
523
|
+
const html = await response.text();
|
|
524
|
+
/* Re-check only after the body has been read. A newer navigation can
|
|
525
|
+
start while this one is still streaming, and swapping the older body
|
|
526
|
+
would leave the pane and the address bar out of order. */
|
|
527
|
+
if (token !== fragmentToken) {
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
const contentType = response.headers.get("content-type") || "";
|
|
531
|
+
const unrendered = response.status >= 500 && !contentType.includes("text/html");
|
|
532
|
+
if (!html.trim() || unrendered) {
|
|
533
|
+
/* Nothing usable to swap: fall back to a full navigation instead of
|
|
534
|
+
blanking the content pane. */
|
|
535
|
+
return { failed: true };
|
|
536
|
+
}
|
|
537
|
+
return { html, finalUrl: response.url || url };
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (token !== fragmentToken) {
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
if (error && error.name === "AbortError") {
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
return { failed: true };
|
|
546
|
+
} finally {
|
|
547
|
+
if (controller === activeFragment) {
|
|
548
|
+
activeFragment = null;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function settledUrl(result, requestedUrl) {
|
|
554
|
+
try {
|
|
555
|
+
return new URL(result.finalUrl, window.location.href);
|
|
556
|
+
} catch (error) {
|
|
557
|
+
return new URL(requestedUrl, window.location.href);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function requestedHash(url) {
|
|
562
|
+
try {
|
|
563
|
+
return new URL(url, window.location.href).hash;
|
|
564
|
+
} catch (error) {
|
|
565
|
+
return "";
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
async function navigateToFragment(url, options) {
|
|
570
|
+
const targetSelector = options.target || "#content";
|
|
571
|
+
const result = await fetchFragment(url);
|
|
572
|
+
if (result === null) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
if (
|
|
576
|
+
result.failed ||
|
|
577
|
+
!swapFragment(result.html, targetSelector, options.swap || "innerHTML")
|
|
578
|
+
) {
|
|
579
|
+
/* location.assign() to the current URL with a different hash would only
|
|
580
|
+
move the anchor, leaving the stale pane in place; force a reload. */
|
|
581
|
+
const target = new URL(url, window.location.href);
|
|
582
|
+
if (
|
|
583
|
+
target.pathname === window.location.pathname &&
|
|
584
|
+
target.search === window.location.search
|
|
585
|
+
) {
|
|
586
|
+
window.location.reload();
|
|
587
|
+
} else {
|
|
588
|
+
window.location.assign(url);
|
|
589
|
+
}
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const settled = settledUrl(result, url);
|
|
594
|
+
if (options.history === "push") {
|
|
595
|
+
window.history.pushState(
|
|
596
|
+
null,
|
|
597
|
+
"",
|
|
598
|
+
settled.pathname + settled.search + requestedHash(url),
|
|
599
|
+
);
|
|
600
|
+
} else if (options.history === "sync") {
|
|
601
|
+
const next = settled.pathname + settled.search + window.location.hash;
|
|
602
|
+
if (
|
|
603
|
+
next !==
|
|
604
|
+
window.location.pathname + window.location.search + window.location.hash
|
|
605
|
+
) {
|
|
606
|
+
window.history.replaceState(null, "", next);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
fragmentLocation = window.location.pathname + window.location.search;
|
|
610
|
+
|
|
611
|
+
if (targetSelector === "#content" && !effectiveHash()) {
|
|
612
|
+
const content = document.getElementById("content");
|
|
613
|
+
if (content) {
|
|
614
|
+
content.scrollTop = 0;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
afterFragmentSwap();
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function onFragmentClick(event) {
|
|
621
|
+
if (
|
|
622
|
+
event.defaultPrevented ||
|
|
623
|
+
event.button !== 0 ||
|
|
624
|
+
event.metaKey ||
|
|
625
|
+
event.ctrlKey ||
|
|
626
|
+
event.shiftKey ||
|
|
627
|
+
event.altKey
|
|
628
|
+
) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
const element =
|
|
632
|
+
event.target instanceof Element ? event.target.closest("[data-get]") : null;
|
|
633
|
+
if (!element || !element.dataset.get) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
event.preventDefault();
|
|
637
|
+
navigateToFragment(element.dataset.get, {
|
|
638
|
+
target: element.dataset.target || "#content",
|
|
639
|
+
swap: element.dataset.swap || "innerHTML",
|
|
640
|
+
history: element.dataset.pushUrl === "true" ? "push" : "none",
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function onFragmentPopState() {
|
|
645
|
+
const current = window.location.pathname + window.location.search;
|
|
646
|
+
if (current === fragmentLocation) {
|
|
647
|
+
scrollToHash();
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
navigateToFragment(window.location.href, {
|
|
651
|
+
target: "#content",
|
|
652
|
+
swap: "innerHTML",
|
|
653
|
+
history: "sync",
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function initFragmentNavigation() {
|
|
658
|
+
document.addEventListener("click", onFragmentClick);
|
|
659
|
+
window.addEventListener("popstate", onFragmentPopState);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/* Global delegated controls --------------------------------------------- */
|
|
663
|
+
|
|
664
|
+
function initControls() {
|
|
665
|
+
document.addEventListener("click", (event) => {
|
|
666
|
+
if (!(event.target instanceof Element)) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
const control = event.target.closest(
|
|
670
|
+
"#theme-toggle, #sidebar-toggle, [data-tab], [data-diagram]",
|
|
671
|
+
);
|
|
672
|
+
if (!control) {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (control.id === "theme-toggle") {
|
|
676
|
+
toggleTheme();
|
|
677
|
+
} else if (control.id === "sidebar-toggle") {
|
|
678
|
+
toggleSidebar();
|
|
679
|
+
} else if (control.dataset.tab) {
|
|
680
|
+
activateTab(control.dataset.tab);
|
|
681
|
+
} else if (control.dataset.diagram) {
|
|
682
|
+
handleDiagramAction(control.dataset.diagram);
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
document.addEventListener("keydown", (event) => {
|
|
687
|
+
const tab = event.target instanceof Element ? event.target.closest("[data-tab]") : null;
|
|
688
|
+
if (!tab || (event.key !== "ArrowLeft" && event.key !== "ArrowRight")) {
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
event.preventDefault();
|
|
692
|
+
const tabs = [...document.querySelectorAll("[data-tab]")];
|
|
693
|
+
const index = tabs.indexOf(tab);
|
|
694
|
+
const next = tabs[(index + (event.key === "ArrowRight" ? 1 : -1) + tabs.length) % tabs.length];
|
|
695
|
+
if (next) {
|
|
696
|
+
activateTab(next.dataset.tab);
|
|
697
|
+
next.focus();
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/* Initialization -------------------------------------------------------- */
|
|
703
|
+
|
|
704
|
+
function init() {
|
|
705
|
+
const theme =
|
|
706
|
+
root.getAttribute("data-theme") ||
|
|
707
|
+
(darkQuery && darkQuery.matches ? "dark" : "light");
|
|
708
|
+
applyTheme(theme);
|
|
709
|
+
|
|
710
|
+
const storedWidth = Number.parseInt(read(KEYS.sidebarWidth, ""), 10);
|
|
711
|
+
if (Number.isFinite(storedWidth) && storedWidth > 0) {
|
|
712
|
+
applySidebarWidth(storedWidth);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
setCollapsed(read(KEYS.sidebarCollapsed, "false") === "true", false);
|
|
716
|
+
activateTab(read(KEYS.activeTab, "files"), false);
|
|
717
|
+
initResizer();
|
|
718
|
+
initViewportWatcher();
|
|
719
|
+
initTreePersistence();
|
|
720
|
+
applyTreeState();
|
|
721
|
+
initHeadings();
|
|
722
|
+
initDiagram();
|
|
723
|
+
initControls();
|
|
724
|
+
initFragmentNavigation();
|
|
725
|
+
scrollToHash();
|
|
726
|
+
|
|
727
|
+
if (darkQuery && darkQuery.addEventListener) {
|
|
728
|
+
darkQuery.addEventListener("change", (event) => {
|
|
729
|
+
if (!read(KEYS.theme, "")) {
|
|
730
|
+
applyTheme(event.matches ? "dark" : "light");
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
if (document.readyState === "loading") {
|
|
737
|
+
document.addEventListener("DOMContentLoaded", init, { once: true });
|
|
738
|
+
} else {
|
|
739
|
+
init();
|
|
740
|
+
}
|
|
741
|
+
})();
|