-
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
import "@fontsource/inter-tight/600.css";
import "@fontsource/jetbrains-mono/400.css";
import "@wooorm/starry-night/style/both";
import {
derived,
effect,
el,
fragment,
signal,
PromiseSnapshot,
} from "./ef.js";
import { app, route, title } from "./app.js";
import { type LibBundleEntry, type Route } from "./route.js";
declare global {
const __VERSION__: string;
const __BUNDLE_INFO__: readonly LibBundleEntry[];
}
function rafp(): Promise<void> {
return new Promise((resolve) => {
requestAnimationFrame(() => {
resolve();
});
});
}
const $route = signal<PromiseSnapshot<Route, unknown>>({
isSettled: false,
isRejected: false,
isResolved: false,
});
route(location.pathname)
.then(async (r) => {
$route.set({
isSettled: true,
isRejected: false,
isResolved: true,
data: r,
});
const url = new URL(location.href);
if (!url.hash) {
return;
}
// Wait for the new element to be attached to document tree
await rafp();
const match = document.querySelector(url.hash);
if (match) {
match.scrollIntoView();
}
})
.catch((error) => {
$route.set({
isSettled: true,
isRejected: true,
isResolved: false,
error,
});
});
if (window.navigation) {
window.navigation.addEventListener("navigate", (ev) => {
if (
!ev.canIntercept ||
ev.downloadRequest ||
ev.hashChange ||
ev.navigationType === "reload" ||
!ev.userInitiated
) {
return;
}
ev.intercept({
async handler() {
const r = await route(new URL(ev.destination.url).pathname);
// NOTE: Chrome seems to have a bug that it can't scroll at all on Navigation API.
// I don't know what causes this.
if (ev.navigationType === "push") {
document.body.scroll({
top: 0,
behavior: "instant",
});
}
$route.set({
isSettled: true,
isRejected: false,
isResolved: true,
data: r,
});
// Wait for the new element to be attached to document tree
await rafp();
},
});
});
}
const $title = derived(() => {
const route = $route.get();
if (!route.isResolved) {
return null;
}
return route.data.title;
});
effect(() => {
if ($title.value) {
document.title = title($title.value);
}
});
const $head = derived(() => {
if (!$route.value.isResolved) {
return null;
}
return fragment($route.value.data.head || []);
});
const headMarker = document.head.querySelector(
`script[type="application/json"][data-ef-head-mk]`,
);
while (headMarker && headMarker.nextSibling) {
document.head.removeChild(headMarker.nextSibling);
}
document.head.appendChild(fragment([$head]));
const ctx = {
version: __VERSION__,
bundle: __BUNDLE_INFO__,
};
const $main = derived(() => {
if (!$route.value.isSettled) {
return el("p", [], ["Loading"]);
}
if ($route.value.isRejected) {
return el(
"p",
[],
["Failed to render the route: ", String($route.value.error)],
);
}
return $route.value.data.content(ctx);
});
const $outline = derived(() => {
if (!$route.value.isResolved) {
return null;
}
return $route.value.data.outline?.(ctx);
});
document.body.replaceChildren(app($main, $outline));