@xingwangzhe/stalux 1.41.5 → 1.43.0
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.
- package/README.md +19 -5
- package/package.json +12 -3
- package/src/components/stalux/archives/archivesList.astro +0 -2
- package/src/components/stalux/archives/toArchive.astro +1 -5
- package/src/components/stalux/common/Typewriter.astro +1 -7
- package/src/components/stalux/common/author.astro +1 -6
- package/src/components/stalux/common/date.astro +1 -11
- package/src/components/stalux/layout/PageHeading.astro +2 -11
- package/src/components/stalux/layout/footer.astro +1 -6
- package/src/components/stalux/layout/head.astro +9 -2
- package/src/components/stalux/links/socialMedia.astro +1 -10
- package/src/components/stalux/posts/ArticleSummary.astro +2 -8
- package/src/components/stalux/posts/PostTaxonomy.astro +3 -14
- package/src/components/stalux/posts/postCC.astro +2 -2
- package/src/components/stalux/posts/postCard.astro +1 -5
- package/src/components/stalux/posts/postContent.astro +4 -23
- package/src/components/stalux/posts/postLeft.astro +4 -22
- package/src/components/stalux/posts/postRight.astro +5 -27
- package/src/components/stalux/posts/postToc.astro +3 -10
- package/src/components/stalux/posts/randomPost.astro +0 -1
- package/src/components/stalux/tags/tagHeader.astro +1 -5
- package/src/env.d.ts +5 -0
- package/src/index.ts +41 -2
- package/src/internal/social-card-data.ts +34 -0
- package/src/internal/social-card-renderer.ts +307 -0
- package/src/internal/style-names.generated.ts +59 -69
- package/src/layouts/PostLayout.astro +1 -5
- package/src/layouts/Stalux.astro +6 -19
- package/src/pages/index.astro +1 -5
- package/src/pages/og/[image].[format].ts +47 -0
- package/src/pages/posts/[post].astro +8 -0
- package/src/pages/posts/[post].md.ts +1 -1
- package/src/schemas/collections.ts +9 -0
- package/src/scripts/navigation-client.ts +13 -0
- package/src/scripts/navigation-scripts.ts +191 -0
- package/src/scripts/route-motion.ts +78 -11
- package/src/scripts/swup-router.ts +366 -0
- package/src/scripts/tags-cloud.ts +1 -1
- package/src/scripts/view-transitions.ts +3 -233
- package/src/scripts/word-list.ts +9 -3
- package/src/styles/generated.css +1 -1
- package/src/styles/motion.css +2 -211
- package/src/utils/post-markdown.ts +2 -0
- package/src/utils/route-catalog.ts +1 -0
- package/src/utils/seo-document.ts +21 -4
- package/src/utils/social-image.ts +60 -0
- package/src/scripts/archive-reveal.ts +0 -76
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import SwupA11yPlugin from "@swup/a11y-plugin";
|
|
2
|
+
import SwupHeadPlugin from "@swup/head-plugin";
|
|
3
|
+
import SwupJsPlugin from "@swup/js-plugin";
|
|
4
|
+
import SwupPreloadPlugin from "@swup/preload-plugin";
|
|
5
|
+
import SwupScrollPlugin from "@swup/scroll-plugin";
|
|
6
|
+
import Swup, { type Visit } from "swup";
|
|
7
|
+
import { getRouteKind } from "../utils/public-routes";
|
|
8
|
+
import { getRouteMotionPreset, type RouteMotionPreset } from "../utils/route-motion-policy";
|
|
9
|
+
import { installBackToTop } from "./back-to-top";
|
|
10
|
+
import { prepareNavigationFont } from "./navigation-font";
|
|
11
|
+
import "./navigation-client";
|
|
12
|
+
import { prepareNavigationScripts } from "./navigation-scripts";
|
|
13
|
+
import { prepareRouteMotion, type RouteMotion } from "./route-motion";
|
|
14
|
+
|
|
15
|
+
interface NavigationContext {
|
|
16
|
+
controller: AbortController;
|
|
17
|
+
from: URL;
|
|
18
|
+
to: URL;
|
|
19
|
+
direction: string;
|
|
20
|
+
navigationType: "push" | "replace" | "traverse";
|
|
21
|
+
sourceElement?: Element;
|
|
22
|
+
preset: RouteMotionPreset;
|
|
23
|
+
browserMotion: boolean;
|
|
24
|
+
motion?: RouteMotion;
|
|
25
|
+
executeScripts?: (signal?: AbortSignal) => Promise<void>;
|
|
26
|
+
scriptsReady?: Promise<void>;
|
|
27
|
+
obsoleteStyles: Element[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type Position = { top: number; left: number; focus?: string };
|
|
31
|
+
const MAIN = "main[data-stalux-route]";
|
|
32
|
+
|
|
33
|
+
function abortObsoleteVisit(visit: Visit): void {
|
|
34
|
+
// Swup 4.10 ships abort() but strips @internal members from its .d.ts.
|
|
35
|
+
// Pin core to that audited version; recheck this adapter when upgrading it.
|
|
36
|
+
(visit as Visit & { abort: () => void }).abort();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function dispatchNavigation(name: string, context: NavigationContext, nextDocument?: Document) {
|
|
40
|
+
const event = Object.assign(new Event(name, { cancelable: true }), {
|
|
41
|
+
from: context.from,
|
|
42
|
+
to: context.to,
|
|
43
|
+
direction: context.direction,
|
|
44
|
+
navigationType: context.navigationType,
|
|
45
|
+
sourceElement: context.sourceElement,
|
|
46
|
+
signal: context.controller.signal,
|
|
47
|
+
...(nextDocument ? { newDocument: nextDocument } : {}),
|
|
48
|
+
});
|
|
49
|
+
document.dispatchEvent(event);
|
|
50
|
+
return event;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function clearMotionState(): void {
|
|
54
|
+
for (const key of ["staluxVtFrom", "staluxVtTo", "staluxVtDirection", "staluxVtPreset"])
|
|
55
|
+
delete document.documentElement.dataset[key];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Astro renders pages; Swup alone owns their client navigation and animation. */
|
|
59
|
+
export function installSwupRouter(): Swup {
|
|
60
|
+
if (window.swup) return window.swup;
|
|
61
|
+
const contexts = new WeakMap<Visit, NavigationContext>();
|
|
62
|
+
const historyPositions = new Map<number, Position>();
|
|
63
|
+
let authoredStyles = [...document.head.querySelectorAll("style,link[rel=stylesheet]")];
|
|
64
|
+
let activeContext: NavigationContext | undefined;
|
|
65
|
+
let visibleHistoryIndex = Number(window.history.state?.index ?? 1);
|
|
66
|
+
let browserMotion = false;
|
|
67
|
+
const scroll = new SwupScrollPlugin({
|
|
68
|
+
doScrollingRightAway: false,
|
|
69
|
+
animateScroll: { betweenPages: false, samePageWithHash: false, samePage: false },
|
|
70
|
+
// ScrollPlugin's default callback adds one-shot wheel/scrollend
|
|
71
|
+
// listeners even for instant/no-op scrolls. Those events may never fire
|
|
72
|
+
// while navigating at the top, retaining two listeners per page visit.
|
|
73
|
+
scrollFunction: (element, top, left, _animate, start, end) => {
|
|
74
|
+
start();
|
|
75
|
+
element.scrollTo({ top, left, behavior: "instant" });
|
|
76
|
+
end();
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
window.addEventListener(
|
|
81
|
+
"popstate",
|
|
82
|
+
(event) => {
|
|
83
|
+
browserMotion =
|
|
84
|
+
(event as PopStateEvent & { hasUAVisualTransition?: boolean })
|
|
85
|
+
.hasUAVisualTransition === true;
|
|
86
|
+
},
|
|
87
|
+
{ capture: true },
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const swup = new Swup({
|
|
91
|
+
native: false,
|
|
92
|
+
containers: [MAIN],
|
|
93
|
+
animationSelector: false,
|
|
94
|
+
animateHistoryBrowsing: true,
|
|
95
|
+
timeout: 15_000,
|
|
96
|
+
ignoreVisit: (href, { el }) => {
|
|
97
|
+
const url = new URL(href, window.location.href);
|
|
98
|
+
const target =
|
|
99
|
+
el?.getAttribute("target") ??
|
|
100
|
+
document.querySelector("base[target]")?.getAttribute("target");
|
|
101
|
+
return (
|
|
102
|
+
getRouteKind(url.pathname) === "other" ||
|
|
103
|
+
/\.(?:md|json|xml|txt|html?|pdf|png|webp|avif|svg|jpe?g)$/i.test(url.pathname) ||
|
|
104
|
+
Boolean(el?.closest("[data-no-swup],[data-astro-reload],[rel~=external]")) ||
|
|
105
|
+
Boolean(target && target !== "_self")
|
|
106
|
+
);
|
|
107
|
+
},
|
|
108
|
+
plugins: [
|
|
109
|
+
new SwupHeadPlugin({
|
|
110
|
+
awaitAssets: true,
|
|
111
|
+
// Keep old CSS readable until replacement, then prune authored
|
|
112
|
+
// orphans. Styles inserted by live SDKs remain available.
|
|
113
|
+
persistTags: (el) => el.matches("script[src],style,link[rel=stylesheet]"),
|
|
114
|
+
}),
|
|
115
|
+
scroll,
|
|
116
|
+
new SwupA11yPlugin({ headingSelector: "[data-stalux-motion-focus]" }),
|
|
117
|
+
new SwupPreloadPlugin({
|
|
118
|
+
preloadHoveredLinks: true,
|
|
119
|
+
preloadVisibleLinks: false,
|
|
120
|
+
throttle: 2,
|
|
121
|
+
}),
|
|
122
|
+
new SwupJsPlugin({
|
|
123
|
+
animations: [
|
|
124
|
+
{
|
|
125
|
+
from: "(.*)",
|
|
126
|
+
to: "(.*)",
|
|
127
|
+
out: async () => {},
|
|
128
|
+
in: async (_done, { visit }) => {
|
|
129
|
+
const context = contexts.get(visit);
|
|
130
|
+
if (!context?.motion) return;
|
|
131
|
+
context.motion.start(
|
|
132
|
+
document.querySelector<HTMLElement>("[data-stalux-motion-focus]"),
|
|
133
|
+
{
|
|
134
|
+
preset: context.preset,
|
|
135
|
+
browserMotion: context.browserMotion,
|
|
136
|
+
skip: context.controller.signal.aborted,
|
|
137
|
+
},
|
|
138
|
+
);
|
|
139
|
+
await context.motion.finished;
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
}),
|
|
144
|
+
],
|
|
145
|
+
});
|
|
146
|
+
window.swup = swup;
|
|
147
|
+
document.documentElement.dataset.staluxRouter = "swup";
|
|
148
|
+
document.documentElement.dataset.staluxRoute = getRouteKind(window.location.pathname);
|
|
149
|
+
installBackToTop();
|
|
150
|
+
|
|
151
|
+
// Set this in the first synchronous before hook, even when a superseding
|
|
152
|
+
// click skips the later context handler. Swup's page:load promise must settle
|
|
153
|
+
// before an obsolete visit is stopped at the transition boundary.
|
|
154
|
+
swup.hooks.before(
|
|
155
|
+
"visit:start",
|
|
156
|
+
(visit) => {
|
|
157
|
+
visit.animation.wait = true;
|
|
158
|
+
},
|
|
159
|
+
{ priority: -1000 },
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
swup.hooks.on("visit:start", (visit) => {
|
|
163
|
+
const focused = document.activeElement;
|
|
164
|
+
const focus =
|
|
165
|
+
focused instanceof HTMLAnchorElement
|
|
166
|
+
? (focused.getAttribute("href") ?? undefined)
|
|
167
|
+
: undefined;
|
|
168
|
+
historyPositions.set(visibleHistoryIndex, {
|
|
169
|
+
top: window.scrollY,
|
|
170
|
+
left: window.scrollX,
|
|
171
|
+
focus,
|
|
172
|
+
});
|
|
173
|
+
if (historyPositions.size > 64)
|
|
174
|
+
historyPositions.delete(historyPositions.keys().next().value as number);
|
|
175
|
+
const from = new URL(visit.from.url + (visit.from.hash ?? ""), window.location.origin);
|
|
176
|
+
const to = new URL(visit.to.url + (visit.to.hash ?? ""), window.location.origin);
|
|
177
|
+
const navigationType = visit.history.popstate ? "traverse" : visit.history.action;
|
|
178
|
+
const context: NavigationContext = {
|
|
179
|
+
controller: new AbortController(),
|
|
180
|
+
from,
|
|
181
|
+
to,
|
|
182
|
+
navigationType,
|
|
183
|
+
direction: visit.history.direction === "backwards" ? "back" : "forward",
|
|
184
|
+
sourceElement: visit.trigger.el,
|
|
185
|
+
browserMotion: browserMotion && visit.history.popstate,
|
|
186
|
+
preset: getRouteMotionPreset({
|
|
187
|
+
from,
|
|
188
|
+
to,
|
|
189
|
+
navigationType,
|
|
190
|
+
intent:
|
|
191
|
+
visit.trigger.el
|
|
192
|
+
?.closest("[data-stalux-motion]")
|
|
193
|
+
?.getAttribute("data-stalux-motion") ?? undefined,
|
|
194
|
+
reducedMotion: window.matchMedia("(prefers-reduced-motion: reduce)").matches,
|
|
195
|
+
browserMotion: browserMotion && visit.history.popstate,
|
|
196
|
+
hidden: document.hidden,
|
|
197
|
+
}),
|
|
198
|
+
obsoleteStyles: [],
|
|
199
|
+
};
|
|
200
|
+
browserMotion = false;
|
|
201
|
+
contexts.set(visit, context);
|
|
202
|
+
activeContext = context;
|
|
203
|
+
visit.animation.wait = true;
|
|
204
|
+
visit.a11y.focus = visit.history.popstate ? false : { selector: MAIN, wait: true };
|
|
205
|
+
if (dispatchNavigation("astro:before-preparation", context).defaultPrevented)
|
|
206
|
+
visit.ignore();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
swup.hooks.before("visit:transition", (visit) => {
|
|
210
|
+
const context = contexts.get(visit);
|
|
211
|
+
// renderPage updates the URL before content:replace. A revived stale
|
|
212
|
+
// visit must stop here as well as before the later head/DOM work.
|
|
213
|
+
if (!context || context.controller.signal.aborted || visit !== swup.visit)
|
|
214
|
+
abortObsoleteVisit(visit);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
swup.hooks.before(
|
|
218
|
+
"content:replace",
|
|
219
|
+
async (visit) => {
|
|
220
|
+
const context = contexts.get(visit);
|
|
221
|
+
// Swup 4 can resume a superseded async visit:start after abort. Stop
|
|
222
|
+
// its entire remaining hook chain before HeadPlugin touches metadata.
|
|
223
|
+
if (!context || context.controller.signal.aborted || visit !== swup.visit) {
|
|
224
|
+
abortObsoleteVisit(visit);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const next = visit.to.document;
|
|
228
|
+
if (!next) return;
|
|
229
|
+
context.executeScripts = prepareNavigationScripts(next, { baseUrl: context.to.href });
|
|
230
|
+
await prepareNavigationFont(next, context.controller.signal);
|
|
231
|
+
if (context.controller.signal.aborted || visit !== swup.visit) {
|
|
232
|
+
abortObsoleteVisit(visit);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const incoming = [...next.head.querySelectorAll("style,link[rel=stylesheet]")];
|
|
236
|
+
context.obsoleteStyles = authoredStyles.filter(
|
|
237
|
+
(el) => !incoming.some((other) => el.isEqualNode(other)),
|
|
238
|
+
);
|
|
239
|
+
dispatchNavigation("astro:after-preparation", context, next);
|
|
240
|
+
},
|
|
241
|
+
{ priority: -100 },
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
// Assets are ready before old interactions are disposed. Replacement errors
|
|
245
|
+
// propagate to Swup's full-document fallback instead of leaving a dead page.
|
|
246
|
+
swup.hooks.replace("content:replace", async (visit, args, replace) => {
|
|
247
|
+
const context = contexts.get(visit);
|
|
248
|
+
// Swup can release an obsolete visit's parsed document while its async
|
|
249
|
+
// hook chain is still unwinding. Cancellation precedes validation: a
|
|
250
|
+
// missing document on an aborted visit is not a failed navigation.
|
|
251
|
+
if (context?.controller.signal.aborted || visit.done || visit !== swup.visit) return;
|
|
252
|
+
const next = visit.to.document;
|
|
253
|
+
if (!context || !next || !replace) throw new Error("Missing Swup document");
|
|
254
|
+
if (context.controller.signal.aborted) return;
|
|
255
|
+
const html = document.documentElement;
|
|
256
|
+
html.dataset.staluxVtFrom = getRouteKind(context.from.pathname);
|
|
257
|
+
html.dataset.staluxVtTo = getRouteKind(context.to.pathname);
|
|
258
|
+
html.dataset.staluxVtDirection = context.direction;
|
|
259
|
+
html.dataset.staluxVtPreset = context.preset;
|
|
260
|
+
context.motion = prepareRouteMotion(dispatchNavigation("astro:before-swap", context, next));
|
|
261
|
+
if (context.controller.signal.aborted) return;
|
|
262
|
+
await replace(visit, args);
|
|
263
|
+
if (context.controller.signal.aborted) return;
|
|
264
|
+
for (const element of context.obsoleteStyles) element.remove();
|
|
265
|
+
const incomingStyles = [...next.head.querySelectorAll("style,link[rel=stylesheet]")];
|
|
266
|
+
authoredStyles = [...document.head.querySelectorAll("style,link[rel=stylesheet]")].filter(
|
|
267
|
+
(el) => incomingStyles.some((other) => el.isEqualNode(other)),
|
|
268
|
+
);
|
|
269
|
+
// Main alone is replaced: header/footer/sentinel and background layers
|
|
270
|
+
// keep their node identities. Never reset live background state to SSR 0.
|
|
271
|
+
for (const { name, value } of [...next.body.attributes]) {
|
|
272
|
+
if (
|
|
273
|
+
name.startsWith("data-stalux-") &&
|
|
274
|
+
name !== "data-stalux-bg-index" &&
|
|
275
|
+
name !== "data-stalux-bg-layer"
|
|
276
|
+
)
|
|
277
|
+
document.body.setAttribute(name, value);
|
|
278
|
+
}
|
|
279
|
+
html.dataset.staluxRoute = getRouteKind(context.to.pathname);
|
|
280
|
+
visibleHistoryIndex = Number(window.history.state?.index ?? visibleHistoryIndex);
|
|
281
|
+
dispatchNavigation("astro:after-swap", context, next);
|
|
282
|
+
context.scriptsReady = context.executeScripts?.(context.controller.signal);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
swup.hooks.replace(
|
|
286
|
+
"content:scroll",
|
|
287
|
+
async (visit, args, defaultScroll) => {
|
|
288
|
+
const saved = visit.history.popstate
|
|
289
|
+
? historyPositions.get(Number(window.history.state?.index))
|
|
290
|
+
: undefined;
|
|
291
|
+
if (visit.history.popstate) {
|
|
292
|
+
// ScrollPlugin defers unanimated popstate to the browser, but Swup
|
|
293
|
+
// uses manual restoration even for reduced motion and native swipes.
|
|
294
|
+
const position = saved ??
|
|
295
|
+
scroll.getCachedScrollPositions(visit.to.url)?.window ?? { top: 0, left: 0 };
|
|
296
|
+
window.scrollTo({ top: position.top, left: position.left, behavior: "instant" });
|
|
297
|
+
scroll.restoreScrollContainers(visit.to.url);
|
|
298
|
+
if (saved?.focus) {
|
|
299
|
+
const link = [...document.querySelectorAll<HTMLAnchorElement>("a[href]")].find(
|
|
300
|
+
(el) => el.getAttribute("href") === saved.focus,
|
|
301
|
+
);
|
|
302
|
+
link?.focus({ preventScroll: true });
|
|
303
|
+
}
|
|
304
|
+
startMotion(visit);
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
await defaultScroll?.(visit, args);
|
|
308
|
+
// ScrollPlugin scrolls in rAF; finish it before heading visibility checks.
|
|
309
|
+
await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
|
|
310
|
+
startMotion(visit);
|
|
311
|
+
return true;
|
|
312
|
+
},
|
|
313
|
+
{ priority: 10 },
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
function startMotion(visit: Visit): void {
|
|
317
|
+
const context = contexts.get(visit);
|
|
318
|
+
context?.motion?.start(document.querySelector<HTMLElement>("[data-stalux-motion-focus]"), {
|
|
319
|
+
preset: context.preset,
|
|
320
|
+
skip: !visit.animation.animate || context.controller.signal.aborted,
|
|
321
|
+
browserMotion: context.browserMotion,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
swup.hooks.on("page:view", async (visit) => {
|
|
326
|
+
const context = contexts.get(visit);
|
|
327
|
+
if (!context || context.controller.signal.aborted) return;
|
|
328
|
+
await context.scriptsReady;
|
|
329
|
+
if (context.controller.signal.aborted) return;
|
|
330
|
+
dispatchNavigation("astro:page-load", context);
|
|
331
|
+
if (!visit.animation.animate) context.motion?.start(null, { skip: true });
|
|
332
|
+
});
|
|
333
|
+
swup.hooks.replace("link:self", () => undefined);
|
|
334
|
+
|
|
335
|
+
const cancel = (visit: Visit) => {
|
|
336
|
+
const context = contexts.get(visit);
|
|
337
|
+
context?.controller.abort();
|
|
338
|
+
context?.motion?.cancel();
|
|
339
|
+
if (context === activeContext) {
|
|
340
|
+
clearMotionState();
|
|
341
|
+
document.documentElement.removeAttribute("aria-busy");
|
|
342
|
+
activeContext = undefined;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
swup.hooks.on("visit:abort", cancel);
|
|
346
|
+
swup.hooks.on("visit:fail", cancel);
|
|
347
|
+
swup.hooks.on("visit:end", (visit) => {
|
|
348
|
+
const context = contexts.get(visit);
|
|
349
|
+
context?.motion?.start(null, { skip: true });
|
|
350
|
+
if (activeContext === context) {
|
|
351
|
+
clearMotionState();
|
|
352
|
+
activeContext = undefined;
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
swup.hooks.on("cache:set", () => {
|
|
356
|
+
while (swup.cache.size > 32)
|
|
357
|
+
swup.cache.delete(swup.cache.all.keys().next().value as string);
|
|
358
|
+
});
|
|
359
|
+
// Parsed DOM, not window.load: slow third-party resources cannot hold the
|
|
360
|
+
// first page's interactions hostage. Page-runtime dedupes the early mount.
|
|
361
|
+
const initialLoad = () => document.dispatchEvent(new Event("astro:page-load"));
|
|
362
|
+
if (document.readyState === "loading")
|
|
363
|
+
document.addEventListener("DOMContentLoaded", initialLoad, { once: true });
|
|
364
|
+
else queueMicrotask(initialLoad);
|
|
365
|
+
return swup;
|
|
366
|
+
}
|
|
@@ -1,234 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { installBackToTop } from "./back-to-top";
|
|
4
|
-
import { createClientLogger } from "./logger";
|
|
5
|
-
import { prepareNavigationFont } from "./navigation-font";
|
|
6
|
-
import { prepareRouteMotion, type RouteMotion } from "./route-motion";
|
|
1
|
+
// Keep the layout entry stable while Swup owns navigation and animation.
|
|
2
|
+
import { installSwupRouter } from "./swup-router";
|
|
7
3
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
interface TransitionWindow extends Window {
|
|
11
|
-
__STALUX_VT_LISTENER__?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function setRouteState(pathname: string): void {
|
|
15
|
-
const html = document.documentElement;
|
|
16
|
-
const route = getRouteKind(pathname);
|
|
17
|
-
logger.debug(`route state=${route}`);
|
|
18
|
-
if (html.dataset.staluxRoute !== route) html.dataset.staluxRoute = route;
|
|
19
|
-
const main = document.querySelector("main[data-stalux-route]");
|
|
20
|
-
if (main && main.getAttribute("data-stalux-route") !== route) {
|
|
21
|
-
main.setAttribute("data-stalux-route", route);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface NavigationEvent {
|
|
26
|
-
from: URL;
|
|
27
|
-
to: URL;
|
|
28
|
-
direction: string;
|
|
29
|
-
navigationType?: string;
|
|
30
|
-
loader: () => Promise<void>;
|
|
31
|
-
newDocument: Document;
|
|
32
|
-
signal: AbortSignal;
|
|
33
|
-
viewTransition?: ViewTransition;
|
|
34
|
-
sourceElement?: Element;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function setNavigationState(
|
|
38
|
-
event: NavigationEvent,
|
|
39
|
-
preset: RouteMotionPreset,
|
|
40
|
-
html = document.documentElement,
|
|
41
|
-
): void {
|
|
42
|
-
const from = getRouteKind(new URL(event.from, window.location.href).pathname);
|
|
43
|
-
const to = getRouteKind(new URL(event.to, window.location.href).pathname);
|
|
44
|
-
html.dataset.staluxVtFrom = from;
|
|
45
|
-
html.dataset.staluxVtTo = to;
|
|
46
|
-
html.dataset.staluxVtDirection = event.direction;
|
|
47
|
-
html.dataset.staluxVtPreset = preset;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function clearNavigationState(): void {
|
|
51
|
-
const html = document.documentElement;
|
|
52
|
-
delete html.dataset.staluxVtFrom;
|
|
53
|
-
delete html.dataset.staluxVtTo;
|
|
54
|
-
delete html.dataset.staluxVtDirection;
|
|
55
|
-
delete html.dataset.staluxVtPreset;
|
|
56
|
-
delete html.dataset.staluxVtPhase;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function preserveHeaderMetrics(nextDocument: Document): void {
|
|
60
|
-
const selector = '[data-astro-transition-persist="stalux-site-header"]';
|
|
61
|
-
if (!document.querySelector(selector) || !nextDocument.querySelector(selector)) return;
|
|
62
|
-
// The header DOM persists. Keep its measured dimensions through Astro's root
|
|
63
|
-
// attribute swap instead of invalidating inherited layout variables twice.
|
|
64
|
-
for (const property of ["--stalux-nav-offset", "--stalux-site-header-height"]) {
|
|
65
|
-
const value = document.documentElement.style.getPropertyValue(property);
|
|
66
|
-
if (value && !nextDocument.documentElement.style.getPropertyValue(property)) {
|
|
67
|
-
nextDocument.documentElement.style.setProperty(property, value);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const transitionWindow = window as TransitionWindow;
|
|
73
|
-
if (!transitionWindow.__STALUX_VT_LISTENER__) {
|
|
74
|
-
transitionWindow.__STALUX_VT_LISTENER__ = true;
|
|
75
|
-
let pendingNavigation = 0;
|
|
76
|
-
let swappedNavigation = 0;
|
|
77
|
-
|
|
78
|
-
// A current-page link is not a page replacement. Intercept before Astro's
|
|
79
|
-
// delegated handler, but keep propagation for menu cleanup and click tracking.
|
|
80
|
-
// During preparation, Astro must receive it to cancel an incoming route.
|
|
81
|
-
// Hash changes, explicit reloads, downloads and modified/new-tab clicks keep
|
|
82
|
-
// their native behavior.
|
|
83
|
-
document.addEventListener(
|
|
84
|
-
"click",
|
|
85
|
-
(event) => {
|
|
86
|
-
if (
|
|
87
|
-
pendingNavigation !== 0 ||
|
|
88
|
-
event.defaultPrevented ||
|
|
89
|
-
event.button !== 0 ||
|
|
90
|
-
event.metaKey ||
|
|
91
|
-
event.ctrlKey ||
|
|
92
|
-
event.shiftKey ||
|
|
93
|
-
event.altKey
|
|
94
|
-
)
|
|
95
|
-
return;
|
|
96
|
-
const anchor =
|
|
97
|
-
event.target instanceof Element
|
|
98
|
-
? event.target.closest<HTMLAnchorElement>("a[href]")
|
|
99
|
-
: null;
|
|
100
|
-
if (
|
|
101
|
-
!anchor ||
|
|
102
|
-
anchor.href !== window.location.href ||
|
|
103
|
-
anchor.href.includes("#") ||
|
|
104
|
-
anchor.hasAttribute("download") ||
|
|
105
|
-
anchor.hasAttribute("data-astro-reload") ||
|
|
106
|
-
anchor.relList.contains("external")
|
|
107
|
-
)
|
|
108
|
-
return;
|
|
109
|
-
const target =
|
|
110
|
-
anchor.getAttribute("target") ??
|
|
111
|
-
document.querySelector("base[target]")?.getAttribute("target");
|
|
112
|
-
if (!target || target === "_self") event.preventDefault();
|
|
113
|
-
},
|
|
114
|
-
{ capture: true },
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
let generation = 0;
|
|
118
|
-
let pendingBrowserMotion = false;
|
|
119
|
-
let preset: RouteMotionPreset = "none";
|
|
120
|
-
let motion: RouteMotion | undefined;
|
|
121
|
-
window.addEventListener(
|
|
122
|
-
"popstate",
|
|
123
|
-
(event) => {
|
|
124
|
-
pendingBrowserMotion =
|
|
125
|
-
(event as PopStateEvent & { hasUAVisualTransition?: boolean })
|
|
126
|
-
.hasUAVisualTransition === true;
|
|
127
|
-
},
|
|
128
|
-
{ capture: true },
|
|
129
|
-
);
|
|
130
|
-
document.addEventListener("astro:before-preparation", (event) => {
|
|
131
|
-
const navigation = event as unknown as NavigationEvent;
|
|
132
|
-
const current = ++generation;
|
|
133
|
-
pendingNavigation = current;
|
|
134
|
-
const cancelPreparation = () => {
|
|
135
|
-
if (current !== generation) return;
|
|
136
|
-
pendingNavigation = 0;
|
|
137
|
-
clearNavigationState();
|
|
138
|
-
};
|
|
139
|
-
motion?.cancel();
|
|
140
|
-
clearNavigationState();
|
|
141
|
-
// CSS moves only explicitly marked content, never the root or sidebars.
|
|
142
|
-
// Long article geometry does not need to be measured or captured.
|
|
143
|
-
preset = getRouteMotionPreset({
|
|
144
|
-
from: new URL(navigation.from, window.location.href),
|
|
145
|
-
to: new URL(navigation.to, window.location.href),
|
|
146
|
-
navigationType: navigation.navigationType,
|
|
147
|
-
intent:
|
|
148
|
-
navigation.sourceElement
|
|
149
|
-
?.closest("[data-stalux-motion]")
|
|
150
|
-
?.getAttribute("data-stalux-motion") ?? undefined,
|
|
151
|
-
reducedMotion: window.matchMedia?.("(prefers-reduced-motion: reduce)").matches,
|
|
152
|
-
browserMotion: pendingBrowserMotion && navigation.navigationType === "traverse",
|
|
153
|
-
hidden: document.hidden,
|
|
154
|
-
});
|
|
155
|
-
pendingBrowserMotion = false;
|
|
156
|
-
setNavigationState(navigation, preset);
|
|
157
|
-
navigation.signal.addEventListener("abort", cancelPreparation, { once: true });
|
|
158
|
-
if (navigation.signal.aborted) cancelPreparation();
|
|
159
|
-
queueMicrotask(() => {
|
|
160
|
-
if (event.defaultPrevented) cancelPreparation();
|
|
161
|
-
});
|
|
162
|
-
const loader = navigation.loader;
|
|
163
|
-
navigation.loader = async () => {
|
|
164
|
-
try {
|
|
165
|
-
await loader();
|
|
166
|
-
if (event.defaultPrevented) cancelPreparation();
|
|
167
|
-
else {
|
|
168
|
-
await prepareNavigationFont(navigation.newDocument, navigation.signal);
|
|
169
|
-
}
|
|
170
|
-
} catch (error) {
|
|
171
|
-
cancelPreparation();
|
|
172
|
-
throw error;
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
document.addEventListener("astro:before-swap", (event) => {
|
|
178
|
-
const navigation = event as unknown as NavigationEvent;
|
|
179
|
-
if (navigation.signal.aborted) return;
|
|
180
|
-
const current = generation;
|
|
181
|
-
swappedNavigation = current;
|
|
182
|
-
preserveHeaderMetrics(navigation.newDocument);
|
|
183
|
-
// Astro replaces the root attributes during swap. Carry state into the
|
|
184
|
-
// new document; CSS keyframes read these data-* attributes to pick their
|
|
185
|
-
// own direction rather than letting Astro's default `fade` pair apply.
|
|
186
|
-
setNavigationState(navigation, preset, navigation.newDocument.documentElement);
|
|
187
|
-
if (preset !== "none") {
|
|
188
|
-
navigation.newDocument.documentElement.dataset.staluxVtPhase = "prepared";
|
|
189
|
-
}
|
|
190
|
-
// This release uses CSS/native VT only. Settle the existing installation
|
|
191
|
-
// gate without starting a second WAAPI effect; analytics/background keep
|
|
192
|
-
// the same shared ready/finished semantics and their existing listeners.
|
|
193
|
-
motion = prepareRouteMotion(navigation);
|
|
194
|
-
motion.start(null, { skip: true });
|
|
195
|
-
// Preparing the incoming content paused prevents a one-frame flash at
|
|
196
|
-
// its final position. Release it with the heading's native animation;
|
|
197
|
-
// page scripts and Astro's scroll restoration keep their normal order.
|
|
198
|
-
void Promise.resolve(navigation.viewTransition?.ready).then(
|
|
199
|
-
() => {
|
|
200
|
-
if (
|
|
201
|
-
current === generation &&
|
|
202
|
-
!navigation.signal.aborted &&
|
|
203
|
-
document.documentElement.dataset.staluxVtPhase === "prepared"
|
|
204
|
-
) {
|
|
205
|
-
document.documentElement.dataset.staluxVtPhase = "running";
|
|
206
|
-
}
|
|
207
|
-
},
|
|
208
|
-
() => {},
|
|
209
|
-
);
|
|
210
|
-
const finish = () => {
|
|
211
|
-
if (current === generation) clearNavigationState();
|
|
212
|
-
};
|
|
213
|
-
// Native ready starts the animation. Keep selectors until it finishes,
|
|
214
|
-
// including fallback/skip and abort; an older navigation cannot clear a
|
|
215
|
-
// newer one's state.
|
|
216
|
-
void motion.finished.then(finish, finish);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
document.addEventListener("astro:after-swap", () => {
|
|
220
|
-
setRouteState(window.location.pathname);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
document.addEventListener("astro:page-load", () => {
|
|
224
|
-
// A previous route's scripts can finish while a newer loader is pending.
|
|
225
|
-
if (swappedNavigation === generation) pendingNavigation = 0;
|
|
226
|
-
setRouteState(window.location.pathname);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
// Footer button and scroll sentinel persist across soft navigations, so the
|
|
230
|
-
// BackToTop observer is installed once per document.
|
|
231
|
-
installBackToTop();
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
setRouteState(window.location.pathname);
|
|
4
|
+
installSwupRouter();
|
package/src/scripts/word-list.ts
CHANGED
|
@@ -7,8 +7,14 @@ function scrollToWord(hash: string): void {
|
|
|
7
7
|
highlighted.removeAttribute("data-glow");
|
|
8
8
|
}
|
|
9
9
|
element.dataset.glow = "";
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
11
|
+
element.scrollIntoView({ behavior: reducedMotion ? "instant" : "smooth", block: "center" });
|
|
12
|
+
// Keep Swup's source/index so back from another page can restore this entry.
|
|
13
|
+
history.replaceState(
|
|
14
|
+
{ ...history.state, url: `${location.pathname}${location.search}${hash}` },
|
|
15
|
+
"",
|
|
16
|
+
hash,
|
|
17
|
+
);
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
registerPageLifecycle("word-list", () => {
|
|
@@ -17,7 +23,7 @@ registerPageLifecycle("word-list", () => {
|
|
|
17
23
|
let firstFrame = 0;
|
|
18
24
|
let secondFrame = 0;
|
|
19
25
|
|
|
20
|
-
if (location.hash.startsWith("#word-")) {
|
|
26
|
+
if (location.hash.startsWith("#word-") && !window.swup?.visit.history.popstate) {
|
|
21
27
|
firstFrame = requestAnimationFrame(() => {
|
|
22
28
|
firstFrame = 0;
|
|
23
29
|
secondFrame = requestAnimationFrame(() => {
|