@perkyzz/emvb 0.2.0 → 0.3.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 +2 -2
- package/package.json +2 -2
- package/src/admin/editor/A11yCopilot.tsx +126 -0
- package/src/admin/editor/CommandPalette.tsx +118 -0
- package/src/admin/editor/Editor.tsx +274 -12
- package/src/admin/editor/EditorOverlay.tsx +6 -0
- package/src/admin/editor/PerfMeter.tsx +121 -0
- package/src/admin/editor/ShareLink.tsx +164 -0
- package/src/admin/editor/Timeline.tsx +274 -0
- package/src/admin/editor/TopBar.tsx +4 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +12 -3
- package/src/admin/editor/canvas/SelectionOverlay.tsx +1 -1
- package/src/admin/editor/collection-previews.ts +91 -0
- package/src/admin/editor/editor-css.ts +5 -3
- package/src/admin/editor/feature-css.ts +105 -0
- package/src/admin/editor/host.ts +2 -0
- package/src/admin/editor/live-data.tsx +71 -0
- package/src/admin/editor/palette-items.ts +105 -0
- package/src/admin/editor/palette.ts +67 -0
- package/src/admin/editor/panels/AddPanel.tsx +47 -2
- package/src/admin/editor/panels/ClassesSection.tsx +5 -1
- package/src/admin/editor/panels/ElementPanel.tsx +34 -3
- package/src/admin/editor/panels/LayersPanel.tsx +38 -1
- package/src/admin/editor/panels/LeftPanel.tsx +4 -1
- package/src/admin/editor/panels/SettingsPanel.tsx +12 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +2 -0
- package/src/admin/editor/panels/TokenScales.tsx +168 -0
- package/src/admin/editor/panels/VariableSection.tsx +9 -4
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +215 -0
- package/src/admin/editor/panels/settings/BindingsEditor.tsx +221 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +1 -1
- package/src/admin/editor/panels/settings/EffectsControls.tsx +4 -2
- package/src/admin/editor/panels/settings/MotionControl.tsx +252 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +1 -1
- package/src/admin/editor/panels/settings/StyleRow.tsx +12 -0
- package/src/admin/editor/panels/settings/StyleSources.tsx +94 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +1 -1
- package/src/admin/editor/panels/settings/VariantEditor.tsx +83 -0
- package/src/admin/editor/panels/settings/length-units.ts +1 -1
- package/src/admin/editor/panels/settings/style-sections.ts +2 -1
- package/src/admin/editor/shortcuts.ts +2 -1
- package/src/admin/editor/useEditorShortcuts.ts +8 -0
- package/src/admin/editor/useNodeActions.ts +38 -15
- package/src/admin/ui.ts +4 -4
- package/src/astro/request-data.ts +97 -0
- package/src/astro/resolve-theme.ts +39 -9
- package/src/astro/resolve.ts +8 -2
- package/src/astro/theme-context.ts +14 -2
- package/src/astro/theme-posts.ts +51 -1
- package/src/astro/visitor.ts +142 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +282 -0
- package/src/core/arrange.ts +91 -0
- package/src/core/audience/rules.ts +104 -0
- package/src/core/audience/variants.ts +51 -0
- package/src/core/css/generate.ts +20 -2
- package/src/core/css/trace.ts +169 -0
- package/src/core/data/bindings.ts +257 -0
- package/src/core/design/tokens.ts +127 -0
- package/src/core/elements/dynamic-elements.ts +87 -2
- package/src/core/elements/index.ts +2 -0
- package/src/core/history/diff.ts +152 -0
- package/src/core/index.ts +111 -0
- package/src/core/migrate/index.ts +4 -1
- package/src/core/nesting.ts +1 -0
- package/src/core/perf/meter.ts +160 -0
- package/src/core/perf/scripts.ts +58 -0
- package/src/core/recipes/index.ts +433 -0
- package/src/core/render/index.ts +51 -4
- package/src/core/render/loop.ts +54 -7
- package/src/core/render/pagination.ts +9 -1
- package/src/core/sanitize/css.ts +39 -3
- package/src/core/sanitize/motion.ts +129 -0
- package/src/core/schema/design.ts +25 -2
- package/src/core/schema/layout.ts +168 -18
- package/src/core/schema/motion.ts +14 -0
- package/src/core/schema/style.ts +48 -3
- package/src/core/share/link.ts +147 -0
- package/src/core/theme/dynamic.ts +49 -0
- package/src/core/theme/pagination.ts +56 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Scroll motion effects (W-319). */
|
|
2
|
+
export const MOTION_EFFECTS = ["fade", "move-x", "move-y", "scale", "rotate", "blur"] as const;
|
|
3
|
+
export type MotionEffect = (typeof MOTION_EFFECTS)[number];
|
|
4
|
+
/**
|
|
5
|
+
* Allowed `from`/`to` per effect: opacity %, px offsets, scale %, degrees, blur px (W-319).
|
|
6
|
+
*/
|
|
7
|
+
export const MOTION_LIMITS: Record<MotionEffect, readonly [number, number]> = {
|
|
8
|
+
fade: [0, 100],
|
|
9
|
+
"move-x": [-1000, 1000],
|
|
10
|
+
"move-y": [-1000, 1000],
|
|
11
|
+
scale: [0, 400],
|
|
12
|
+
rotate: [-720, 720],
|
|
13
|
+
blur: [0, 50],
|
|
14
|
+
};
|
package/src/core/schema/style.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { isSafeFontStack } from "../sanitize/css.ts";
|
|
3
3
|
import { sanitizeMediaUrl } from "../sanitize/media-url.ts";
|
|
4
4
|
import { POPUP_DEVICES } from "../theme/popup-rules.ts";
|
|
5
|
+
import { MOTION_EFFECTS, MOTION_LIMITS } from "./motion.ts";
|
|
5
6
|
|
|
6
7
|
/** Colour refs omit `from` (legacy). Length/font refs set `from` (W-028). */
|
|
7
8
|
export const VariableRef = z.strictObject({
|
|
@@ -287,6 +288,45 @@ export const StyleProps = z.strictObject({
|
|
|
287
288
|
z.strictObject({ type: z.literal("none") }),
|
|
288
289
|
])
|
|
289
290
|
.optional(),
|
|
291
|
+
/**
|
|
292
|
+
* Scroll motion (W-319): effects tied to the element's trip through the screen (or the page's
|
|
293
|
+
* scroll), with CSS scroll-driven animations. Browsers without them, and visitors who prefer
|
|
294
|
+
* reduced motion, see the element still. `{ type: "none" }` stops motion a wider device sets.
|
|
295
|
+
*/
|
|
296
|
+
scrollMotion: z
|
|
297
|
+
.union([
|
|
298
|
+
z.strictObject({
|
|
299
|
+
effects: z
|
|
300
|
+
.array(
|
|
301
|
+
z.strictObject({
|
|
302
|
+
type: z.enum(MOTION_EFFECTS),
|
|
303
|
+
from: z.number().min(-1000).max(1000),
|
|
304
|
+
to: z.number().min(-1000).max(1000),
|
|
305
|
+
}),
|
|
306
|
+
)
|
|
307
|
+
.min(1)
|
|
308
|
+
.max(MOTION_EFFECTS.length)
|
|
309
|
+
.superRefine((effects, ctx) => {
|
|
310
|
+
const seen = new Set<string>();
|
|
311
|
+
for (const effect of effects) {
|
|
312
|
+
const [min, max] = MOTION_LIMITS[effect.type];
|
|
313
|
+
if ([effect.from, effect.to].some((n) => n < min || n > max)) {
|
|
314
|
+
ctx.addIssue({
|
|
315
|
+
code: "custom",
|
|
316
|
+
message: `${effect.type} goes from ${min} to ${max}`,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
if (seen.has(effect.type)) {
|
|
320
|
+
ctx.addIssue({ code: "custom", message: `${effect.type} is set twice` });
|
|
321
|
+
}
|
|
322
|
+
seen.add(effect.type);
|
|
323
|
+
}
|
|
324
|
+
}),
|
|
325
|
+
range: z.enum(["enter", "cross", "exit", "page"]),
|
|
326
|
+
}),
|
|
327
|
+
z.strictObject({ type: z.literal("none") }),
|
|
328
|
+
])
|
|
329
|
+
.optional(),
|
|
290
330
|
/**
|
|
291
331
|
* Plays once (W-101, W-107). `delay` waits before it starts. `view` starts as the element
|
|
292
332
|
* scrolls into view, using CSS scroll-driven animations, and falls back to playing on load.
|
|
@@ -306,10 +346,15 @@ export type StyleProps = z.infer<typeof StyleProps>;
|
|
|
306
346
|
|
|
307
347
|
/**
|
|
308
348
|
* `states` on a node or class (D-032, W-089): hover, focus (`:focus-visible`) and active, each with
|
|
309
|
-
* the same keys and limits as `style` except `transition`, `entrance
|
|
310
|
-
* states are refused.
|
|
349
|
+
* the same keys and limits as `style` except `transition`, `entrance`, `iconAnimation` and
|
|
350
|
+
* `scrollMotion`. Unknown states are refused.
|
|
311
351
|
*/
|
|
312
|
-
const StateStyle = StyleProps.omit({
|
|
352
|
+
const StateStyle = StyleProps.omit({
|
|
353
|
+
transition: true,
|
|
354
|
+
entrance: true,
|
|
355
|
+
iconAnimation: true,
|
|
356
|
+
scrollMotion: true,
|
|
357
|
+
});
|
|
313
358
|
|
|
314
359
|
export const StyleStates = z.strictObject({
|
|
315
360
|
hover: StateStyle.optional(),
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { MAX_DESIGN_BYTES, MAX_LAYOUT_BYTES } from "../limits.ts";
|
|
2
|
+
import type { DesignSystem } from "../schema/design.ts";
|
|
3
|
+
import type { Layout } from "../schema/layout.ts";
|
|
4
|
+
import { validateDesign, validateLayout } from "../validate.ts";
|
|
5
|
+
|
|
6
|
+
/** Where shared pages open (W-321). */
|
|
7
|
+
export const PLAYGROUND_URL = "https://emvb.dev/playground/";
|
|
8
|
+
/** `#p=` holds the page, so it never reaches a server log. */
|
|
9
|
+
const SHARE_HASH_KEY = "p";
|
|
10
|
+
/** Format tag, so a later encoding can be told apart. */
|
|
11
|
+
const VERSION = "1";
|
|
12
|
+
/** Past this, some chat apps and browsers cut the link (W-321). */
|
|
13
|
+
export const LONG_LINK_CHARS = 8000;
|
|
14
|
+
/** A link longer than this is refused outright. */
|
|
15
|
+
const MAX_LINK_CHARS = 200_000;
|
|
16
|
+
const MAX_TITLE = 120;
|
|
17
|
+
/** Unpacked text: a layout and a design at their limits, plus a little JSON. */
|
|
18
|
+
const MAX_UNPACKED_BYTES = MAX_LAYOUT_BYTES + MAX_DESIGN_BYTES + 4096;
|
|
19
|
+
|
|
20
|
+
export type SharedPage = { title: string; layout: Layout; design?: DesignSystem };
|
|
21
|
+
|
|
22
|
+
export type ShareRead =
|
|
23
|
+
| { ok: true; page: SharedPage }
|
|
24
|
+
| { ok: false; reason: "empty" | "too-long" | "unreadable" | "invalid"; detail?: string };
|
|
25
|
+
|
|
26
|
+
function toBase64Url(bytes: Uint8Array): string {
|
|
27
|
+
let binary = "";
|
|
28
|
+
const CHUNK = 0x8000;
|
|
29
|
+
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
30
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
31
|
+
}
|
|
32
|
+
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fromBase64Url(text: string): Uint8Array {
|
|
36
|
+
const b64 = text.replaceAll("-", "+").replaceAll("_", "/");
|
|
37
|
+
const binary = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
|
|
38
|
+
const bytes = new Uint8Array(binary.length);
|
|
39
|
+
for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
|
|
40
|
+
return bytes;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function pipe(
|
|
44
|
+
bytes: Uint8Array,
|
|
45
|
+
stream: CompressionStream | DecompressionStream,
|
|
46
|
+
max: number,
|
|
47
|
+
) {
|
|
48
|
+
const writer = stream.writable.getWriter();
|
|
49
|
+
void writer
|
|
50
|
+
.write(bytes as Uint8Array<ArrayBuffer>)
|
|
51
|
+
.then(() => writer.close())
|
|
52
|
+
.catch(() => {});
|
|
53
|
+
const reader = stream.readable.getReader();
|
|
54
|
+
const parts: Uint8Array[] = [];
|
|
55
|
+
let size = 0;
|
|
56
|
+
for (;;) {
|
|
57
|
+
// Read in order; stop early so a tiny link can't unpack into something huge.
|
|
58
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
59
|
+
const { done, value } = await reader.read();
|
|
60
|
+
if (done) break;
|
|
61
|
+
size += value.byteLength;
|
|
62
|
+
if (size > max) {
|
|
63
|
+
void reader.cancel();
|
|
64
|
+
throw new Error("too big");
|
|
65
|
+
}
|
|
66
|
+
parts.push(value);
|
|
67
|
+
}
|
|
68
|
+
const out = new Uint8Array(size);
|
|
69
|
+
let at = 0;
|
|
70
|
+
for (const part of parts) {
|
|
71
|
+
out.set(part, at);
|
|
72
|
+
at += part.byteLength;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The page and (optionally) Site styles packed for a link (W-321): JSON, deflated, base64url,
|
|
79
|
+
* prefixed with a version.
|
|
80
|
+
*/
|
|
81
|
+
export async function encodeSharedPage(page: SharedPage): Promise<string> {
|
|
82
|
+
const json = JSON.stringify({
|
|
83
|
+
t: page.title.slice(0, MAX_TITLE),
|
|
84
|
+
l: page.layout,
|
|
85
|
+
...(page.design ? { d: page.design } : {}),
|
|
86
|
+
});
|
|
87
|
+
const packed = await pipe(
|
|
88
|
+
new TextEncoder().encode(json),
|
|
89
|
+
new CompressionStream("deflate-raw"),
|
|
90
|
+
Number.MAX_SAFE_INTEGER,
|
|
91
|
+
);
|
|
92
|
+
return `${VERSION}.${toBase64Url(packed)}`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The full link: the playground with the page in its hash. */
|
|
96
|
+
export const shareUrl = (encoded: string, base: string = PLAYGROUND_URL): string =>
|
|
97
|
+
`${base}#${SHARE_HASH_KEY}=${encoded}`;
|
|
98
|
+
|
|
99
|
+
/** The packed page in a location hash (`#p=…`), if any. */
|
|
100
|
+
export function sharedFromHash(hash: string): string | null {
|
|
101
|
+
const params = new URLSearchParams(hash.replace(/^#/, ""));
|
|
102
|
+
const value = params.get(SHARE_HASH_KEY);
|
|
103
|
+
return value ? value.trim() : null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Unpacks and checks a shared page (W-321). Everything goes through the same validation as a
|
|
108
|
+
* saved page, with the same size limits, so a link can't carry anything a page couldn't.
|
|
109
|
+
*/
|
|
110
|
+
export async function decodeSharedPage(encoded: string): Promise<ShareRead> {
|
|
111
|
+
const text = encoded.trim();
|
|
112
|
+
if (!text) return { ok: false, reason: "empty" };
|
|
113
|
+
if (text.length > MAX_LINK_CHARS) return { ok: false, reason: "too-long" };
|
|
114
|
+
const [version, body] = text.split(".", 2);
|
|
115
|
+
if (version !== VERSION || !body || !/^[A-Za-z0-9_-]+$/.test(body)) {
|
|
116
|
+
return { ok: false, reason: "unreadable" };
|
|
117
|
+
}
|
|
118
|
+
let parsed: unknown;
|
|
119
|
+
try {
|
|
120
|
+
const bytes = await pipe(
|
|
121
|
+
fromBase64Url(body),
|
|
122
|
+
new DecompressionStream("deflate-raw"),
|
|
123
|
+
MAX_UNPACKED_BYTES,
|
|
124
|
+
);
|
|
125
|
+
parsed = JSON.parse(new TextDecoder().decode(bytes));
|
|
126
|
+
} catch {
|
|
127
|
+
return { ok: false, reason: "unreadable" };
|
|
128
|
+
}
|
|
129
|
+
if (typeof parsed !== "object" || parsed === null) return { ok: false, reason: "unreadable" };
|
|
130
|
+
const { t, l, d } = parsed as { t?: unknown; l?: unknown; d?: unknown };
|
|
131
|
+
const layout = validateLayout(l);
|
|
132
|
+
if (!layout.ok) {
|
|
133
|
+
return { ok: false, reason: "invalid", detail: layout.issues[0]?.message ?? "Bad layout" };
|
|
134
|
+
}
|
|
135
|
+
const page: SharedPage = {
|
|
136
|
+
title: typeof t === "string" && t.trim() ? t.trim().slice(0, MAX_TITLE) : "Shared page",
|
|
137
|
+
layout: layout.layout,
|
|
138
|
+
};
|
|
139
|
+
if (d !== undefined) {
|
|
140
|
+
const design = validateDesign(d);
|
|
141
|
+
if (!design.ok) {
|
|
142
|
+
return { ok: false, reason: "invalid", detail: design.issues[0]?.message ?? "Bad styles" };
|
|
143
|
+
}
|
|
144
|
+
page.design = design.design;
|
|
145
|
+
}
|
|
146
|
+
return { ok: true, page };
|
|
147
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { VisitorInfo } from "../audience/rules.ts";
|
|
1
2
|
import type { ArchivePagination } from "./pagination.ts";
|
|
2
3
|
import type { Layout, LayoutNode } from "../schema/layout.ts";
|
|
3
4
|
import { isParentNode } from "../schema/layout.ts";
|
|
@@ -18,6 +19,11 @@ export type ThemePostFields = {
|
|
|
18
19
|
/** ISO date or a display string from the post. */
|
|
19
20
|
publishedAt?: string;
|
|
20
21
|
authorName?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The entry's other plain fields (text, numbers, true/false) by field name, for data bindings
|
|
24
|
+
* (W-307): a Loop over a Team collection binds a heading to `role`.
|
|
25
|
+
*/
|
|
26
|
+
fields?: Readonly<Record<string, string | number | boolean>>;
|
|
21
27
|
};
|
|
22
28
|
|
|
23
29
|
/** Data passed into `renderPage` when resolving Single Post / Archive / Loop Item. */
|
|
@@ -40,6 +46,18 @@ export type ThemeDynamicData = {
|
|
|
40
46
|
pagination?: ArchivePagination;
|
|
41
47
|
/** Set inside a Loop's item, where a Pagination would repeat once per post (W-228). */
|
|
42
48
|
inLoopItem?: boolean;
|
|
49
|
+
/** What the host knows about the visitor, for visitor-aware elements (W-313). */
|
|
50
|
+
visitor?: VisitorInfo;
|
|
51
|
+
/** The visitor's arm per A/B test (W-312); hosts pick it on the server. */
|
|
52
|
+
abArms?: Readonly<Record<string, "a" | "b">>;
|
|
53
|
+
/** Entries for collection Loops (W-308), keyed by the Loop's node id. Hosts fill this. */
|
|
54
|
+
collections?: Readonly<Record<string, ThemePostFields[]>>;
|
|
55
|
+
/** Site settings bound fields can read (W-307), e.g. `title`, `tagline`, `url`. */
|
|
56
|
+
site?: Readonly<Record<string, string>>;
|
|
57
|
+
/** The request's URL parameters, first value per name (W-307). */
|
|
58
|
+
params?: Readonly<Record<string, string>>;
|
|
59
|
+
/** The page's own origin, e.g. `https://example.com`: URL-parameter links stay on it (W-307). */
|
|
60
|
+
origin?: string;
|
|
43
61
|
};
|
|
44
62
|
|
|
45
63
|
/** Editor canvas placeholders when no live post is bound. */
|
|
@@ -105,6 +123,37 @@ export function collectLoopItemPartIds(layout: Layout): string[] {
|
|
|
105
123
|
return [...ids];
|
|
106
124
|
}
|
|
107
125
|
|
|
126
|
+
/** A Loop that lists a collection (W-308): what hosts fetch for it. */
|
|
127
|
+
export type CollectionLoop = {
|
|
128
|
+
nodeId: string;
|
|
129
|
+
collection: string;
|
|
130
|
+
limit: number;
|
|
131
|
+
order: "newest" | "oldest" | "title";
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/** Every collection Loop in a layout, so hosts can fetch their entries (W-308). */
|
|
135
|
+
export function collectCollectionLoops(layout: Layout): CollectionLoop[] {
|
|
136
|
+
const loops: CollectionLoop[] = [];
|
|
137
|
+
const walk = (node: LayoutNode): void => {
|
|
138
|
+
if (node.type === "loop" && loops.length < 20) {
|
|
139
|
+
const props = node.props as { collection?: unknown; limit?: unknown; order?: unknown };
|
|
140
|
+
if (typeof props.collection === "string" && /^[a-z][a-z0-9_]{0,63}$/.test(props.collection)) {
|
|
141
|
+
const limit =
|
|
142
|
+
typeof props.limit === "number" && props.limit >= 1 && props.limit <= 50
|
|
143
|
+
? Math.floor(props.limit)
|
|
144
|
+
: 6;
|
|
145
|
+
const order = props.order === "oldest" || props.order === "title" ? props.order : "newest";
|
|
146
|
+
loops.push({ nodeId: node.id, collection: props.collection, limit, order });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (isParentNode(node)) {
|
|
150
|
+
for (const child of node.children) walk(child);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
walk(layout.root);
|
|
154
|
+
return loops;
|
|
155
|
+
}
|
|
156
|
+
|
|
108
157
|
/** Collect `section.partId` values so hosts can load Section templates. */
|
|
109
158
|
export function collectSectionPartIds(layout: Layout): string[] {
|
|
110
159
|
const ids = new Set<string>();
|
|
@@ -13,8 +13,40 @@ export type ArchivePagination = {
|
|
|
13
13
|
hasMore: boolean;
|
|
14
14
|
/** The archive's own path without `/page/N`, e.g. `/posts` or `/category/news`. */
|
|
15
15
|
basePath: string;
|
|
16
|
+
/**
|
|
17
|
+
* How many pages there are, when the host knows (W-309). EmDash 1.2's public collection query
|
|
18
|
+
* gives no total, so its hosts leave this out and "Page N of M" shows "Page N".
|
|
19
|
+
*/
|
|
20
|
+
totalPages?: number;
|
|
16
21
|
};
|
|
17
22
|
|
|
23
|
+
/** A usable total page count, or undefined (W-309). */
|
|
24
|
+
function knownTotal(p: ArchivePagination): number | undefined {
|
|
25
|
+
const total = p.totalPages;
|
|
26
|
+
return typeof total === "number" && Number.isInteger(total) && total >= p.page && total >= 1
|
|
27
|
+
? Math.min(total, MAX_ARCHIVE_PAGE)
|
|
28
|
+
: undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The "Page N of M" text (W-309). `{page}` and `{total}` are filled in; without a known total
|
|
33
|
+
* the text stops before ` of {total}` (or drops `{total}`), so it never shows a wrong count.
|
|
34
|
+
*/
|
|
35
|
+
export function pageCountText(p: ArchivePagination, template: string | undefined): string {
|
|
36
|
+
const text = template?.trim() || "Page {page} of {total}";
|
|
37
|
+
const total = knownTotal(p);
|
|
38
|
+
if (total !== undefined) {
|
|
39
|
+
return text.replaceAll("{page}", String(p.page)).replaceAll("{total}", String(total));
|
|
40
|
+
}
|
|
41
|
+
const cut = text.indexOf("{total}");
|
|
42
|
+
const before =
|
|
43
|
+
cut === -1
|
|
44
|
+
? text
|
|
45
|
+
: text.slice(0, cut).replace(/\s*(of|\/|from|sur|de|von|di|van|из)?\s*$/i, "");
|
|
46
|
+
// A template that starts with {total} ("{total} pages · page {page}") keeps at least the number.
|
|
47
|
+
return before.replaceAll("{page}", String(p.page)).trim() || String(p.page);
|
|
48
|
+
}
|
|
49
|
+
|
|
18
50
|
/** The URL path of page `n` of an archive; page 1 is the bare archive path (W-221). */
|
|
19
51
|
export function archivePagePath(basePath: string, n: number): string {
|
|
20
52
|
const base = basePath.replace(/\/+$/, "") || "/";
|
|
@@ -76,6 +108,8 @@ export type PaginationItem =
|
|
|
76
108
|
* with a gap after 1 when the current page is far in (W-222).
|
|
77
109
|
*/
|
|
78
110
|
export function paginationItems(p: ArchivePagination): PaginationItem[] {
|
|
111
|
+
const total = knownTotal(p);
|
|
112
|
+
if (total !== undefined) return totalItems(p, total);
|
|
79
113
|
if (p.page <= 1 && !p.hasMore) return [];
|
|
80
114
|
const items: PaginationItem[] = [];
|
|
81
115
|
const page = (n: number): PaginationItem => ({
|
|
@@ -95,3 +129,25 @@ export function paginationItems(p: ArchivePagination): PaginationItem[] {
|
|
|
95
129
|
}
|
|
96
130
|
return items;
|
|
97
131
|
}
|
|
132
|
+
|
|
133
|
+
/** With a known total (W-309): 1, a window around the current page, and the last page. */
|
|
134
|
+
function totalItems(p: ArchivePagination, total: number): PaginationItem[] {
|
|
135
|
+
if (total <= 1) return [];
|
|
136
|
+
const items: PaginationItem[] = [];
|
|
137
|
+
const page = (n: number): PaginationItem => ({
|
|
138
|
+
kind: "page",
|
|
139
|
+
n,
|
|
140
|
+
href: archivePagePath(p.basePath, n),
|
|
141
|
+
current: n === p.page,
|
|
142
|
+
});
|
|
143
|
+
if (p.page > 1) items.push({ kind: "prev", href: archivePagePath(p.basePath, p.page - 1) });
|
|
144
|
+
const from = Math.max(1, p.page - 2);
|
|
145
|
+
const to = Math.min(total, p.page + 2);
|
|
146
|
+
if (from > 1) items.push(page(1));
|
|
147
|
+
if (from > 2) items.push({ kind: "gap" });
|
|
148
|
+
for (let n = from; n <= to; n++) items.push(page(n));
|
|
149
|
+
if (to < total - 1) items.push({ kind: "gap" });
|
|
150
|
+
if (to < total) items.push(page(total));
|
|
151
|
+
if (p.page < total) items.push({ kind: "next", href: archivePagePath(p.basePath, p.page + 1) });
|
|
152
|
+
return items;
|
|
153
|
+
}
|