ferst-core 0.15.0 → 0.16.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 +13 -4
- package/bin/ferst-media.mjs +96 -0
- package/components/Card.astro +53 -51
- package/components/PostCard.astro +138 -136
- package/components/SiteHeader.astro +10 -14
- package/components/blocks/Grid.astro +49 -48
- package/components/blocks/Heading.astro +4 -1
- package/components/recipes/Banner.astro +146 -146
- package/components/recipes/FeatureCards.astro +112 -118
- package/components/recipes/Gallery.astro +57 -59
- package/components/recipes/Hero.astro +112 -10
- package/components/recipes/LatestPosts.astro +295 -292
- package/components/recipes/Locations.astro +146 -143
- package/components/recipes/Marquee.astro +75 -75
- package/components/recipes/MediaCards.astro +213 -219
- package/components/recipes/Pricing.astro +149 -150
- package/components/recipes/Stats.astro +150 -157
- package/components/recipes/Tiles.astro +342 -296
- package/content/blocks.ts +1072 -1024
- package/content/imageSlots.mjs +542 -0
- package/content/schemas.ts +13 -11
- package/examples/recipes.ts +4 -0
- package/fonts/OFL.txt +1332 -287
- package/fonts/anton-latin-400-normal.woff2 +0 -0
- package/fonts/bangers-latin-400-normal.woff2 +0 -0
- package/fonts/bebas-neue-latin-400-normal.woff2 +0 -0
- package/fonts/bowlby-one-latin-400-normal.woff2 +0 -0
- package/fonts/fredoka-latin-400-normal.woff2 +0 -0
- package/fonts/fredoka-latin-600-normal.woff2 +0 -0
- package/fonts/fredoka-latin-700-normal.woff2 +0 -0
- package/fonts/knewave-latin-400-normal.woff2 +0 -0
- package/fonts/lora-latin-400-normal.woff2 +0 -0
- package/fonts/lora-latin-600-normal.woff2 +0 -0
- package/fonts/lora-latin-700-normal.woff2 +0 -0
- package/fonts/merriweather-latin-400-normal.woff2 +0 -0
- package/fonts/merriweather-latin-600-normal.woff2 +0 -0
- package/fonts/merriweather-latin-700-normal.woff2 +0 -0
- package/fonts/nunito-latin-400-normal.woff2 +0 -0
- package/fonts/nunito-latin-600-normal.woff2 +0 -0
- package/fonts/nunito-latin-700-normal.woff2 +0 -0
- package/fonts/oswald-latin-400-normal.woff2 +0 -0
- package/fonts/oswald-latin-600-normal.woff2 +0 -0
- package/fonts/oswald-latin-700-normal.woff2 +0 -0
- package/fonts/rubik-dirt-latin-400-normal.woff2 +0 -0
- package/integration.mjs +4 -14
- package/layouts/Base.astro +7 -16
- package/lib/fonts.ts +130 -0
- package/lib/gridRows.ts +197 -0
- package/lib/image-fields.ts +140 -0
- package/lib/image-size.mjs +108 -73
- package/lib/image-sizes.ts +16 -0
- package/lib/logo-header.ts +46 -0
- package/lib/media-check.mjs +524 -0
- package/lib/theme-preview.ts +203 -140
- package/lib/themeTokens.ts +235 -220
- package/package.json +3 -2
- package/styles/fonts.css +239 -76
- package/styles/theme.css +228 -160
- package/lib/gridColumns.ts +0 -22
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/integration.mjs
CHANGED
|
@@ -48,7 +48,7 @@ import { copyFileSync, createReadStream, existsSync, mkdirSync, readdirSync, rea
|
|
|
48
48
|
import { deploymentBranch } from './bin/cms-config.mjs';
|
|
49
49
|
import { linksToPageAddresses, seoContext, withNoindexHeader } from './lib/seo.mjs';
|
|
50
50
|
import { writeSiteIcons } from './lib/site-icon.mjs';
|
|
51
|
-
import {
|
|
51
|
+
import { publicImageSizes } from './lib/image-size.mjs';
|
|
52
52
|
|
|
53
53
|
/** Every page of a build (its .html files, at any depth), as file URLs. */
|
|
54
54
|
function htmlFiles(dir) {
|
|
@@ -61,17 +61,6 @@ function htmlFiles(dir) {
|
|
|
61
61
|
);
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
/** The header logo's (logoSettings `primaryLogoLight`) own width and height, or null. */
|
|
65
|
-
function headerLogoSize(root) {
|
|
66
|
-
if (!root) return null;
|
|
67
|
-
try {
|
|
68
|
-
const logos = JSON.parse(readFileSync(new URL('src/content/logoSettings/index.json', root), 'utf8'));
|
|
69
|
-
return publicImageSize(root, logos.primaryLogoLight);
|
|
70
|
-
} catch {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
64
|
/** The self-hosted fonts the engine's CSS requests (/fonts/*.woff2), shipped in the package. */
|
|
76
65
|
const FONTS_DIR = new URL('./fonts/', import.meta.url);
|
|
77
66
|
const FONT_URL = /^\/fonts\/([a-z0-9-]+\.woff2)(?:\?.*)?$/;
|
|
@@ -196,8 +185,9 @@ export default function ferst(options = {}) {
|
|
|
196
185
|
'import.meta.env.FERST_ROUTES_BLOG': JSON.stringify(blog),
|
|
197
186
|
'import.meta.env.FERST_ROUTES_CALENDAR': JSON.stringify(calendar),
|
|
198
187
|
'import.meta.env.FERST_ROUTES_ACCESSIBILITY': JSON.stringify(accessibility),
|
|
199
|
-
//
|
|
200
|
-
'
|
|
188
|
+
// Each of the site's own images with its width and height, so a page keeps every
|
|
189
|
+
// picture's shape before it loads (lib/image-sizes.ts).
|
|
190
|
+
'import.meta.env.FERST_IMAGE_SIZES': JSON.stringify(publicImageSizes(config?.root)),
|
|
201
191
|
},
|
|
202
192
|
},
|
|
203
193
|
});
|
package/layouts/Base.astro
CHANGED
|
@@ -28,7 +28,6 @@ import CookieBanner from '../components/CookieBanner.astro';
|
|
|
28
28
|
import StickyCallout from '../components/StickyCallout.astro';
|
|
29
29
|
import SecondaryLogo from '../components/SecondaryLogo.astro';
|
|
30
30
|
import { loadSiteSettings } from '../lib/siteSettings';
|
|
31
|
-
import { logoHeaderSchema, type HeaderLogoOverride } from '../content/schemas';
|
|
32
31
|
import { getCalloutSettings } from '../lib/callout';
|
|
33
32
|
import { buildThemeStyle } from '../lib/themeTokens';
|
|
34
33
|
import { canonicalUrl, previewTags } from '../lib/seo.mjs';
|
|
@@ -52,8 +51,6 @@ interface Props {
|
|
|
52
51
|
indexable?: boolean;
|
|
53
52
|
/** A post: shared and described to search engines as an article with this date. */
|
|
54
53
|
article?: { date: Date | string; image?: string };
|
|
55
|
-
/** This page's header logo in place of the site's: a preview, or a test fixture. */
|
|
56
|
-
logo?: HeaderLogoOverride;
|
|
57
54
|
}
|
|
58
55
|
|
|
59
56
|
const {
|
|
@@ -67,7 +64,6 @@ const {
|
|
|
67
64
|
align = 'start',
|
|
68
65
|
indexable = true,
|
|
69
66
|
article,
|
|
70
|
-
logo,
|
|
71
67
|
} = Astro.props;
|
|
72
68
|
|
|
73
69
|
const settings = await loadSiteSettings();
|
|
@@ -83,20 +79,16 @@ const path = Astro.url.pathname;
|
|
|
83
79
|
// three source colours (light color-mix + dark OKLCH) and emits the font/knob
|
|
84
80
|
// overrides too; selectors are doubled (`:root:root`) so they win by
|
|
85
81
|
// specificity regardless of stylesheet order. `themeField` is the site-wide
|
|
86
|
-
// input treatment, applied as a `data-field` attribute on <html
|
|
87
|
-
|
|
82
|
+
// input treatment, applied as a `data-field` attribute on <html>; `titleEffect`
|
|
83
|
+
// the finish on display titles, a `data-title-effect` attribute (styles/theme.css).
|
|
84
|
+
const { css: themeCss, field: themeField, titleEffect } = buildThemeStyle(theme);
|
|
88
85
|
|
|
89
86
|
// Logos are optional. When a client hasn't configured one, SiteHeader renders a
|
|
90
87
|
// text wordmark (the site name) rather than pointing <img> at a baked-in path
|
|
91
88
|
// that may not exist — a broken logo by default was the old bug. Dark falls back
|
|
92
89
|
// to the light variant when only one is supplied.
|
|
93
|
-
const primaryLogoLight =
|
|
94
|
-
const primaryLogoDark =
|
|
95
|
-
// The header logo's size and placement, and its own width and height (read at build time by
|
|
96
|
-
// the ferst() integration) so the page keeps the logo's real shape before it loads.
|
|
97
|
-
const logoHeader = logoHeaderSchema.parse(logo ? logo.header ?? {} : settings.header);
|
|
98
|
-
const builtLogoSize = (import.meta.env.FERST_LOGO_SIZE ?? null) as { width: number; height: number } | null;
|
|
99
|
-
const logoSize = logo ? (logo.width && logo.height ? { width: logo.width, height: logo.height } : undefined) : builtLogoSize ?? undefined;
|
|
90
|
+
const primaryLogoLight = settings.primaryLogoLight;
|
|
91
|
+
const primaryLogoDark = settings.primaryLogoDark ?? settings.primaryLogoLight;
|
|
100
92
|
const secondaryLogoLight = settings.secondaryLogoLight;
|
|
101
93
|
const secondaryLogoDark = settings.secondaryLogoDark;
|
|
102
94
|
const secondaryLogoLink = settings.secondaryLogoLink;
|
|
@@ -146,7 +138,7 @@ const hasFeed = Boolean(siteUrl) && import.meta.env.FERST_ROUTES_BLOG !== false;
|
|
|
146
138
|
---
|
|
147
139
|
|
|
148
140
|
<!doctype html>
|
|
149
|
-
<html lang="en-GB" data-field={themeField}>
|
|
141
|
+
<html lang="en-GB" data-field={themeField} data-title-effect={titleEffect}>
|
|
150
142
|
<head>
|
|
151
143
|
<meta charset="utf-8" />
|
|
152
144
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
@@ -272,8 +264,7 @@ const hasFeed = Boolean(siteUrl) && import.meta.env.FERST_ROUTES_BLOG !== false;
|
|
|
272
264
|
headerClass={headerClass}
|
|
273
265
|
primaryLogoLight={primaryLogoLight}
|
|
274
266
|
primaryLogoDark={primaryLogoDark}
|
|
275
|
-
logoHeader={
|
|
276
|
-
logoSize={logoSize}
|
|
267
|
+
logoHeader={settings.header}
|
|
277
268
|
brandAlt={brandAlt}
|
|
278
269
|
primaryNav={navigation.primary}
|
|
279
270
|
dropdown={navigation.dropdown}
|
package/lib/fonts.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The curated fonts: every typeface a theme can pick, in ONE list. Theme pickers (the
|
|
3
|
+
* portal's Theme Studio, a CMS font select) read it to offer choices grouped by mood and
|
|
4
|
+
* limited to the roles each face suits; the build (`styles/fonts.css`, the files in
|
|
5
|
+
* `fonts/`) ships exactly these faces, and the tests hold the two to each other.
|
|
6
|
+
*
|
|
7
|
+
* Typography is config, like colour (docs/BLOCK-ARCHITECTURE.md §7): a theme sets the
|
|
8
|
+
* three font roles (`fonts.heading`, `fonts.body`, `fonts.display` in themeSettings) to
|
|
9
|
+
* one of these stacks. There are no per-block fonts.
|
|
10
|
+
*
|
|
11
|
+
* - Self-hosted, SIL Open Font License 1.1 only, latin subset, woff2, `font-display: swap`
|
|
12
|
+
* (licences in `fonts/OFL.txt`). A face downloads only when a page uses it, so listing
|
|
13
|
+
* more costs nothing for a site that doesn't pick them. Only the core defaults are
|
|
14
|
+
* preloaded (layouts/Base.astro).
|
|
15
|
+
* - Display faces (brush, poster, condensed) are offered for the display role, and some
|
|
16
|
+
* for headings, never for body text, so every page stays readable. `buildThemeStyle`
|
|
17
|
+
* ignores a body font that isn't suited to body text.
|
|
18
|
+
* - A single-weight display face declares the whole weight range in fonts.css, so a bold
|
|
19
|
+
* heading uses the face as drawn instead of a smeared, synthesised bold.
|
|
20
|
+
*
|
|
21
|
+
* Pure data + tiny helpers, no framework: safe to import from a build, a browser, or
|
|
22
|
+
* another app.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** The three font roles a theme sets (the fourth, mono, is fixed). */
|
|
26
|
+
export type FontRole = 'heading' | 'body' | 'display';
|
|
27
|
+
|
|
28
|
+
export const FONT_ROLES: readonly FontRole[] = ['heading', 'body', 'display'];
|
|
29
|
+
|
|
30
|
+
/** The mood groups a picker shows the fonts under, in this order. */
|
|
31
|
+
export const FONT_MOODS = [
|
|
32
|
+
{ id: 'clean', label: 'Clean and modern' },
|
|
33
|
+
{ id: 'rounded', label: 'Rounded and friendly' },
|
|
34
|
+
{ id: 'serif', label: 'Classic serif' },
|
|
35
|
+
{ id: 'condensed', label: 'Bold condensed' },
|
|
36
|
+
{ id: 'punchy', label: 'Punchy display' },
|
|
37
|
+
{ id: 'brush', label: 'Brush and marker' },
|
|
38
|
+
{ id: 'system', label: 'Built into every device' },
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
export type FontMood = (typeof FONT_MOODS)[number]['id'];
|
|
42
|
+
|
|
43
|
+
/** One shipped file: its weight (the `font-weight` descriptor in fonts.css) and its name in `fonts/`. */
|
|
44
|
+
export interface FontFile {
|
|
45
|
+
weight: string;
|
|
46
|
+
file: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface CuratedFont {
|
|
50
|
+
/** The family name, as declared in fonts.css and shown in a picker. */
|
|
51
|
+
family: string;
|
|
52
|
+
/** The value a theme stores for a role: the family with sensible fallbacks. */
|
|
53
|
+
stack: string;
|
|
54
|
+
mood: FontMood;
|
|
55
|
+
/** The roles this face is offered for. Display faces never include `body`. */
|
|
56
|
+
roles: readonly FontRole[];
|
|
57
|
+
/** The files shipped in `fonts/` (none for a face every device already has). */
|
|
58
|
+
files: readonly FontFile[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const ALL: readonly FontRole[] = ['heading', 'body', 'display'];
|
|
62
|
+
const TITLES: readonly FontRole[] = ['heading', 'display'];
|
|
63
|
+
const DISPLAY_ONLY: readonly FontRole[] = ['display'];
|
|
64
|
+
|
|
65
|
+
/** The files for a family at the given weights (`<slug>-latin-<weight>-normal.woff2`). */
|
|
66
|
+
const weights = (slug: string, ...ws: number[]): FontFile[] =>
|
|
67
|
+
ws.map((w) => ({ weight: String(w), file: `${slug}-latin-${w}-normal.woff2` }));
|
|
68
|
+
|
|
69
|
+
/** A single-weight display face, declared across the whole weight range (see above). */
|
|
70
|
+
const single = (slug: string): FontFile[] => [{ weight: '100 900', file: `${slug}-latin-400-normal.woff2` }];
|
|
71
|
+
|
|
72
|
+
export const FONTS: readonly CuratedFont[] = [
|
|
73
|
+
// Clean and modern: the core defaults (heading and body).
|
|
74
|
+
{ family: 'Inter', stack: 'Inter, system-ui, sans-serif', mood: 'clean', roles: ALL, files: weights('inter', 400, 500, 600) },
|
|
75
|
+
{ family: 'Poppins', stack: 'Poppins, system-ui, sans-serif', mood: 'clean', roles: ALL, files: weights('poppins', 300, 400, 500, 600, 700) },
|
|
76
|
+
// Rounded and friendly.
|
|
77
|
+
{ family: 'Nunito', stack: 'Nunito, system-ui, sans-serif', mood: 'rounded', roles: ALL, files: weights('nunito', 400, 600, 700) },
|
|
78
|
+
{ family: 'Fredoka', stack: 'Fredoka, system-ui, sans-serif', mood: 'rounded', roles: TITLES, files: weights('fredoka', 400, 600, 700) },
|
|
79
|
+
// Classic serif. Playfair Display (the core's default display face) ships its bold only.
|
|
80
|
+
{ family: 'Playfair Display', stack: "'Playfair Display', Georgia, serif", mood: 'serif', roles: TITLES, files: weights('playfair-display', 700) },
|
|
81
|
+
{ family: 'Lora', stack: 'Lora, Georgia, serif', mood: 'serif', roles: ALL, files: weights('lora', 400, 600, 700) },
|
|
82
|
+
{ family: 'Merriweather', stack: 'Merriweather, Georgia, serif', mood: 'serif', roles: ALL, files: weights('merriweather', 400, 600, 700) },
|
|
83
|
+
// Bold condensed: sport, posters.
|
|
84
|
+
{ family: 'Anton', stack: "Anton, Impact, 'Arial Narrow', sans-serif", mood: 'condensed', roles: TITLES, files: single('anton') },
|
|
85
|
+
{ family: 'Bebas Neue', stack: "'Bebas Neue', Impact, 'Arial Narrow', sans-serif", mood: 'condensed', roles: TITLES, files: single('bebas-neue') },
|
|
86
|
+
{ family: 'Oswald', stack: "Oswald, 'Arial Narrow', system-ui, sans-serif", mood: 'condensed', roles: TITLES, files: weights('oswald', 400, 600, 700) },
|
|
87
|
+
// Punchy display.
|
|
88
|
+
{ family: 'Bangers', stack: 'Bangers, Impact, system-ui, sans-serif', mood: 'punchy', roles: DISPLAY_ONLY, files: single('bangers') },
|
|
89
|
+
{ family: 'Bowlby One', stack: "'Bowlby One', Impact, system-ui, sans-serif", mood: 'punchy', roles: DISPLAY_ONLY, files: single('bowlby-one') },
|
|
90
|
+
// Brush and marker. (Rubik Dirt is a textured face, so its file is large, about 300 KB.)
|
|
91
|
+
{ family: 'Knewave', stack: 'Knewave, Impact, system-ui, sans-serif', mood: 'brush', roles: DISPLAY_ONLY, files: single('knewave') },
|
|
92
|
+
{ family: 'Rubik Dirt', stack: "'Rubik Dirt', Impact, system-ui, sans-serif", mood: 'brush', roles: DISPLAY_ONLY, files: single('rubik-dirt') },
|
|
93
|
+
// Built into every device: nothing to download.
|
|
94
|
+
{ family: 'System sans', stack: 'system-ui, sans-serif', mood: 'system', roles: ALL, files: [] },
|
|
95
|
+
{ family: 'Georgia', stack: "Georgia, 'Times New Roman', serif", mood: 'system', roles: ALL, files: [] },
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
/** The first family named in a CSS font stack, unquoted (`"'Bebas Neue', Impact"` → `Bebas Neue`). */
|
|
99
|
+
export function familyOf(stack: string): string {
|
|
100
|
+
const first = String(stack ?? '').split(',')[0] ?? '';
|
|
101
|
+
return first.trim().replace(/^['"]|['"]$/g, '').trim();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The curated font a stack starts with (by family, so a differently written fallback
|
|
105
|
+
* still matches), or undefined for a stack that isn't one of ours. */
|
|
106
|
+
export function findFont(stack: string): CuratedFont | undefined {
|
|
107
|
+
const family = familyOf(stack).toLowerCase();
|
|
108
|
+
if (!family) return undefined;
|
|
109
|
+
return FONTS.find((f) => familyOf(f.stack).toLowerCase() === family || f.family.toLowerCase() === family);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The fonts offered for a role, in list order. */
|
|
113
|
+
export function fontsForRole(role: FontRole): CuratedFont[] {
|
|
114
|
+
return FONTS.filter((f) => f.roles.includes(role));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The fonts offered for a role, grouped by mood in FONT_MOODS order (empty groups left out). */
|
|
118
|
+
export function fontGroupsForRole(role: FontRole): { mood: FontMood; label: string; fonts: CuratedFont[] }[] {
|
|
119
|
+
const offered = fontsForRole(role);
|
|
120
|
+
return FONT_MOODS.map((m) => ({ mood: m.id, label: m.label, fonts: offered.filter((f) => f.mood === m.id) })).filter(
|
|
121
|
+
(g) => g.fonts.length > 0,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** May this stack be used for this role? A curated face only for the roles it lists; a
|
|
126
|
+
* stack that isn't one of ours is the site's own choice, so it's allowed. */
|
|
127
|
+
export function fontSuitsRole(stack: string, role: FontRole): boolean {
|
|
128
|
+
const font = findFont(stack);
|
|
129
|
+
return !font || font.roles.includes(role);
|
|
130
|
+
}
|
package/lib/gridRows.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row planning for every grid of items (cards, tiles, numbers, pictures, places, posts):
|
|
3
|
+
* one rule, decided at build time for each class of screen, so any count looks composed and
|
|
4
|
+
* stays readable, with no script in the browser and no layout shift. Readability first:
|
|
5
|
+
* items are never shrunk to tidy a row.
|
|
6
|
+
*
|
|
7
|
+
* 1. **The cap.** A row never holds more than the block's `columns`, nor more items than fit
|
|
8
|
+
* at the block's minimum readable width (`ITEM_MINIMUMS`) on the narrowest screen of the
|
|
9
|
+
* class, so the cap holds across the whole class. Narrower screens drop columns; nothing
|
|
10
|
+
* ever adds columns to make rows even.
|
|
11
|
+
* 2. **It fits:** `count ≤ cap` is one row.
|
|
12
|
+
* 3. **Even rows, only by using fewer columns:** the largest `k` from `ceil(cap/2)` (at
|
|
13
|
+
* least 2) up to the cap that divides the count gives rows of `k`, so items only ever stay
|
|
14
|
+
* the same size or grow (6 in fours is 3 + 3, 8 in fours is 4 + 4).
|
|
15
|
+
* 4. **Otherwise full rows at the cap, and a ragged last row**, with no balancing across
|
|
16
|
+
* rows (5 in fours is 4 + 1, 7 in threes is 3 + 3 + 1).
|
|
17
|
+
* 5. **The last row is tidied without touching the others** (`lastRow`, default `auto`):
|
|
18
|
+
* its items **fill** the row when that makes them at most a third wider, otherwise they
|
|
19
|
+
* keep the full rows' width and sit **centred** (so a single item is always centred).
|
|
20
|
+
* `fill` always spreads it; `center` always centres it.
|
|
21
|
+
*
|
|
22
|
+
* Mechanics: a CSS grid on the least common multiple the rows need (12 tracks for 4 + 3),
|
|
23
|
+
* each item spanning its share and, in a centred row, starting where the row's margin ends.
|
|
24
|
+
* `planGrid` turns the phone, tablet and desktop plans into inline custom properties
|
|
25
|
+
* (`--tracks-d` on the grid, `--span-d` / `--start-d` on each item) that the global
|
|
26
|
+
* `.ferst-rows` rules in styles/theme.css read.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** The screen classes a grid plans for, narrowest first (tablets from 40em, desktops from 60em). */
|
|
30
|
+
export const SCREENS = ['phone', 'tablet', 'desktop'] as const;
|
|
31
|
+
export type Screen = (typeof SCREENS)[number];
|
|
32
|
+
/** The suffix of each class's custom properties: `--span-p`, `--start-t`, `--tracks-d`. */
|
|
33
|
+
const SUFFIX: Record<Screen, string> = { phone: 'p', tablet: 't', desktop: 'd' };
|
|
34
|
+
/** The narrowest screen of each class, in px, where its cap is worked out (a small phone). */
|
|
35
|
+
export const NARROWEST_PX: Record<Screen, number> = { phone: 320, tablet: 640, desktop: 960 };
|
|
36
|
+
|
|
37
|
+
/** The content column a recipe gets on a screen `px` wide: `.site-main` is `min(100%, 72rem)`
|
|
38
|
+
* with `padding-inline: clamp(1rem, 3vw, 2.5rem)` (layouts/Base), and the recipe's own
|
|
39
|
+
* `padding-inline: 1rem` (`inset`, in rem) inside it. */
|
|
40
|
+
export function contentWidth(px: number, inset = 1): number {
|
|
41
|
+
const main = Math.min(px, 72 * 16);
|
|
42
|
+
const pad = Math.min(Math.max(16, 0.03 * px), 40);
|
|
43
|
+
return main - 2 * pad - 2 * inset * 16;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The kinds of item a grid holds, each with its minimum readable width and the grid's gap
|
|
47
|
+
* (both rem; the gap mirrors the recipe's CSS, and a test keeps them in step). `min: null`
|
|
48
|
+
* means no readable minimum: one to a row on phones, two on tablets, `columns` on desktops. */
|
|
49
|
+
export const ITEM_MINIMUMS = {
|
|
50
|
+
/** Picture tiles carry designed posters and photos whose words must stay legible. */
|
|
51
|
+
pictureTiles: { min: 17, gap: 1.25 },
|
|
52
|
+
/** Worded tiles are short cards: an icon, a title, a line. */
|
|
53
|
+
tiles: { min: 12, gap: 1.25 },
|
|
54
|
+
/** A media card's photo header and its copy both need room. */
|
|
55
|
+
mediaCards: { min: 17, gap: 1.5 },
|
|
56
|
+
/** A post card shows a cropped photo, a title and a summary. */
|
|
57
|
+
latestPosts: { min: 17, gap: 1.5 },
|
|
58
|
+
/** A place shows a map; a narrower map stops being useful. */
|
|
59
|
+
locations: { min: 17, gap: 1.5 },
|
|
60
|
+
/** Gallery pictures are photos to look at; four abreast on a desktop, as galleries
|
|
61
|
+
* have always shown them, is still about 200px each. */
|
|
62
|
+
gallery: { min: 12.5, gap: 1 },
|
|
63
|
+
/** A feature card is an icon, a short title and a sentence or two. */
|
|
64
|
+
featureCards: { min: 12, gap: 1.5 },
|
|
65
|
+
/** A plan card holds a price and a short list, four abreast as is usual. */
|
|
66
|
+
pricing: { min: 12, gap: 1.5 },
|
|
67
|
+
/** A number and a short label. */
|
|
68
|
+
stats: { min: 9, gap: 1.25 },
|
|
69
|
+
/** The `grid` container's children vary (a paragraph, a picture, a whole recipe). */
|
|
70
|
+
grid: { min: null, gap: 0 },
|
|
71
|
+
} as const satisfies Record<string, { min: number | null; gap: number }>;
|
|
72
|
+
export type ItemKind = keyof typeof ITEM_MINIMUMS;
|
|
73
|
+
|
|
74
|
+
/** How far a last row's items may grow to fill it before it centres instead (`auto`). */
|
|
75
|
+
export const FILL_MAX_GROWTH = 4 / 3;
|
|
76
|
+
|
|
77
|
+
/** How the ragged last row lays out: `auto` fills when that grows its items at most a third,
|
|
78
|
+
* else centres; `fill` always spreads it; `center` always keeps the full rows' width. */
|
|
79
|
+
export type LastRow = 'auto' | 'fill' | 'center';
|
|
80
|
+
/** How a row lays out. */
|
|
81
|
+
export type RowLayout = 'full' | 'fill' | 'center';
|
|
82
|
+
|
|
83
|
+
/** Where one item sits: how many tracks it spans, and the track it starts on (1-based; only
|
|
84
|
+
* for items in a centred row, the rest flow in order). */
|
|
85
|
+
export interface Placement {
|
|
86
|
+
span: number;
|
|
87
|
+
start?: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface RowPlan {
|
|
91
|
+
/** Items per row, top to bottom. */
|
|
92
|
+
rows: number[];
|
|
93
|
+
/** How each row lays out. */
|
|
94
|
+
layout: RowLayout[];
|
|
95
|
+
/** The grid's track count: the least common multiple the rows need. */
|
|
96
|
+
tracks: number;
|
|
97
|
+
/** One placement per item, in order. */
|
|
98
|
+
items: Placement[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const gcd = (a: number, b: number): number => (b === 0 ? a : gcd(b, a % b));
|
|
102
|
+
const lcm = (a: number, b: number) => (a / gcd(a, b)) * b;
|
|
103
|
+
|
|
104
|
+
/** The row sizes for `count` items at most `cap` to a row (rules 2 to 4 above). */
|
|
105
|
+
export function rowSizes(count: number, cap: number): number[] {
|
|
106
|
+
const n = Math.max(0, Math.floor(count));
|
|
107
|
+
const c = Math.max(1, Math.floor(cap));
|
|
108
|
+
if (n === 0) return [];
|
|
109
|
+
if (n <= c) return [n];
|
|
110
|
+
// Even rows only by using fewer columns, never fewer than half the cap. (Not rows of one
|
|
111
|
+
// when the cap allows two: an odd count on a tablet goes in twos, the last one centred.)
|
|
112
|
+
for (let k = c; k >= Math.max(2, Math.ceil(c / 2)); k--) {
|
|
113
|
+
if (n % k === 0) return Array(n / k).fill(k);
|
|
114
|
+
}
|
|
115
|
+
const rows: number[] = Array(Math.floor(n / c)).fill(c);
|
|
116
|
+
if (n % c) rows.push(n % c);
|
|
117
|
+
return rows;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Plans `count` items at most `cap` to a row: the rows, how each lays out, and every item's
|
|
121
|
+
* span (and start, in a centred row) on a grid of `tracks` columns. */
|
|
122
|
+
export function planRows(count: number, cap: number, lastRow: LastRow = 'auto'): RowPlan {
|
|
123
|
+
const rows = rowSizes(count, cap);
|
|
124
|
+
if (rows.length === 0) return { rows, layout: [], tracks: 1, items: [] };
|
|
125
|
+
const full = rows[0];
|
|
126
|
+
const layout: RowLayout[] = rows.map((s) => {
|
|
127
|
+
if (s === full) return 'full';
|
|
128
|
+
if (lastRow !== 'auto') return lastRow;
|
|
129
|
+
return full / s <= FILL_MAX_GROWTH + 1e-9 ? 'fill' : 'center';
|
|
130
|
+
});
|
|
131
|
+
let tracks = full;
|
|
132
|
+
rows.forEach((s, i) => {
|
|
133
|
+
if (layout[i] === 'fill') tracks = lcm(tracks, s);
|
|
134
|
+
// A centred row's margins are half the missing items each: whole tracks only when the
|
|
135
|
+
// full row's span is even, or the number missing is.
|
|
136
|
+
if (layout[i] === 'center' && (full - s) % 2 === 1) tracks = lcm(tracks, 2 * full);
|
|
137
|
+
});
|
|
138
|
+
const items: Placement[] = [];
|
|
139
|
+
rows.forEach((s, i) => {
|
|
140
|
+
const span = layout[i] === 'fill' ? tracks / s : tracks / full;
|
|
141
|
+
const first = layout[i] === 'center' ? (tracks - s * span) / 2 + 1 : undefined;
|
|
142
|
+
for (let j = 0; j < s; j++) items.push(first === undefined ? { span } : { span, start: first + j * span });
|
|
143
|
+
});
|
|
144
|
+
return { rows, layout, tracks, items };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** How many items at least `min` rem wide, `gap` rem apart, fit across `width` px (at least 1). */
|
|
148
|
+
export function fits(width: number, min: number, gap: number): number {
|
|
149
|
+
return Math.max(1, Math.floor((width + gap * 16) / ((min + gap) * 16) + 1e-9));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The most items to a row on each screen class, for a block of `columns` holding `item`s. */
|
|
153
|
+
export function screenCaps(columns: number, item: ItemKind): Record<Screen, number> {
|
|
154
|
+
const c = Math.max(1, Math.floor(columns));
|
|
155
|
+
const { min, gap } = ITEM_MINIMUMS[item];
|
|
156
|
+
if (min === null) return { phone: 1, tablet: Math.min(2, c), desktop: c };
|
|
157
|
+
const cap = (s: Screen) => Math.min(c, fits(contentWidth(NARROWEST_PX[s]), min, gap));
|
|
158
|
+
return { phone: cap('phone'), tablet: cap('tablet'), desktop: cap('desktop') };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface GridOptions {
|
|
162
|
+
/** The block's `columns`: the most to a row. */
|
|
163
|
+
columns: number;
|
|
164
|
+
/** What the grid holds, for its readable minimum (`ITEM_MINIMUMS`). */
|
|
165
|
+
item: ItemKind;
|
|
166
|
+
/** How the ragged last row lays out (default `auto`). */
|
|
167
|
+
lastRow?: LastRow;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface GridPlan {
|
|
171
|
+
/** Each screen class's plan. */
|
|
172
|
+
screens: Record<Screen, RowPlan>;
|
|
173
|
+
/** Items in the desktop's fullest row: the grid's `data-cols`, for styling hooks. */
|
|
174
|
+
cols: number;
|
|
175
|
+
/** The grid element's inline style: its track count on each screen. */
|
|
176
|
+
gridStyle: string;
|
|
177
|
+
/** Each item's inline style: its span (and start) on each screen; none when it needs none. */
|
|
178
|
+
itemStyles: (string | undefined)[];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Plans a grid of `count` items per screen class, as inline styles for the `.ferst-rows`
|
|
182
|
+
* grid (styles/theme.css). */
|
|
183
|
+
export function planGrid(count: number, { columns, item, lastRow = 'auto' }: GridOptions): GridPlan {
|
|
184
|
+
const caps = screenCaps(columns, item);
|
|
185
|
+
const screens = Object.fromEntries(SCREENS.map((s) => [s, planRows(count, caps[s], lastRow)])) as Record<Screen, RowPlan>;
|
|
186
|
+
const gridStyle = SCREENS.map((s) => `--tracks-${SUFFIX[s]}: ${screens[s].tracks}`).join('; ');
|
|
187
|
+
const itemStyles = Array.from({ length: Math.max(0, Math.floor(count)) }, (_, i) => {
|
|
188
|
+
const parts: string[] = [];
|
|
189
|
+
for (const s of SCREENS) {
|
|
190
|
+
const { span, start } = screens[s].items[i];
|
|
191
|
+
if (span !== 1) parts.push(`--span-${SUFFIX[s]}: ${span}`);
|
|
192
|
+
if (start !== undefined) parts.push(`--start-${SUFFIX[s]}: ${start}`);
|
|
193
|
+
}
|
|
194
|
+
return parts.length ? parts.join('; ') : undefined;
|
|
195
|
+
});
|
|
196
|
+
return { screens, cols: screens.desktop.rows[0] ?? 1, gridStyle, itemStyles };
|
|
197
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every image field in the content schemas, found by walking the Zod schemas themselves, so a
|
|
3
|
+
* new picture field can't slip in without saying what size it expects (content/imageSlots.mjs;
|
|
4
|
+
* the coverage test is tests/image-slots.test.ts).
|
|
5
|
+
*
|
|
6
|
+
* The rule: a text field (or a list of text) is an image when its name is `src`, or a word of
|
|
7
|
+
* its name (split at capitals) names a picture (`image`, `images`, `poster`, `avatar`, `logo`,
|
|
8
|
+
* `favicon`, `photo`, `picture`, `thumbnail`, …), and no word describes the picture instead of
|
|
9
|
+
* being it (`heroImageAlt`, `imageCaption`, `logoLink`, `imageFocus`). So `heroImage`,
|
|
10
|
+
* `titleImage`, `primaryLogoLight` and `image.src` are images; `icon` (a name from the icon
|
|
11
|
+
* set), `file` and `video` are not.
|
|
12
|
+
*
|
|
13
|
+
* Alongside each image it lists the `ratio` fields of the objects the picture sits in (its
|
|
14
|
+
* block, its list entry, the image itself): a slot in a block with a ratio field should take
|
|
15
|
+
* its shape from it.
|
|
16
|
+
*/
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
|
|
19
|
+
const IMAGE_WORDS = new Set([
|
|
20
|
+
'src', 'image', 'images', 'img', 'picture', 'pictures', 'photo', 'photos', 'poster', 'posters',
|
|
21
|
+
'avatar', 'logo', 'logos', 'favicon', 'thumbnail', 'thumb', 'backdrop',
|
|
22
|
+
]);
|
|
23
|
+
const DESCRIBING_WORDS = new Set([
|
|
24
|
+
'alt', 'caption', 'label', 'link', 'href', 'focus', 'ratio', 'name', 'credit', 'size', 'width',
|
|
25
|
+
'height', 'position', 'overlay', 'layout', 'placement', 'shadow',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
/** The words of a field name: `primaryLogoLight` → primary, logo, light. */
|
|
29
|
+
export const fieldWords = (name: string) =>
|
|
30
|
+
name
|
|
31
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
32
|
+
.toLowerCase()
|
|
33
|
+
.split(/[^a-z0-9]+/)
|
|
34
|
+
.filter(Boolean);
|
|
35
|
+
|
|
36
|
+
/** Whether a text field by this name holds a picture's address. */
|
|
37
|
+
export function isImageFieldName(name: string): boolean {
|
|
38
|
+
const words = fieldWords(name);
|
|
39
|
+
return words.some((w) => IMAGE_WORDS.has(w)) && !words.some((w) => DESCRIBING_WORDS.has(w));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RatioField {
|
|
43
|
+
/** `mediaCards.ratio`, `bento.items[].size` style: the block type, then the path. */
|
|
44
|
+
key: string;
|
|
45
|
+
/** Its possible values, as text (an enum's options, a boolean's true / false). */
|
|
46
|
+
values: string[];
|
|
47
|
+
/** Its default, when it has one. */
|
|
48
|
+
default?: string;
|
|
49
|
+
}
|
|
50
|
+
export interface ImageField {
|
|
51
|
+
/** `tiles.tiles[].image.src`: the block type (or page / post / a settings collection), then the path. */
|
|
52
|
+
key: string;
|
|
53
|
+
/** The `ratio` fields of the objects the picture sits in, block first. */
|
|
54
|
+
ratioFields: RatioField[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Any = z.ZodTypeAny;
|
|
58
|
+
|
|
59
|
+
/** Peel optional / nullable / default / refine / brand wrappers. A lazy schema (a container's
|
|
60
|
+
* child blocks) is left alone: those blocks are walked as blocks of their own. */
|
|
61
|
+
function unwrap(s: Any): { inner: Any; def?: unknown } {
|
|
62
|
+
let def: unknown;
|
|
63
|
+
for (;;) {
|
|
64
|
+
if (s instanceof z.ZodOptional || s instanceof z.ZodNullable) s = s.unwrap();
|
|
65
|
+
else if (s instanceof z.ZodDefault) {
|
|
66
|
+
if (def === undefined) def = s._def.defaultValue();
|
|
67
|
+
s = s._def.innerType;
|
|
68
|
+
} else if (s instanceof z.ZodEffects) s = s.innerType();
|
|
69
|
+
else if (s instanceof z.ZodBranded) s = s.unwrap();
|
|
70
|
+
else if (s instanceof z.ZodCatch) s = s._def.innerType;
|
|
71
|
+
else return { inner: s, def };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function valuesOf(s: Any): string[] {
|
|
76
|
+
const { inner } = unwrap(s);
|
|
77
|
+
if (inner instanceof z.ZodEnum) return (inner.options as string[]).map(String);
|
|
78
|
+
if (inner instanceof z.ZodNativeEnum) return Object.values(inner.enum as Record<string, string>).map(String);
|
|
79
|
+
if (inner instanceof z.ZodBoolean) return ['true', 'false'];
|
|
80
|
+
if (inner instanceof z.ZodLiteral) return [String(inner.value)];
|
|
81
|
+
if (inner instanceof z.ZodUnion) return (inner.options as Any[]).flatMap(valuesOf);
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The image fields of one schema, keyed under `prefix` (a block type, `page`, `post`, a
|
|
87
|
+
* settings collection). `skip` leaves out top-level fields (a page's `blocks`).
|
|
88
|
+
*/
|
|
89
|
+
export function imageFieldsOf(prefix: string, schema: Any, skip: readonly string[] = []): ImageField[] {
|
|
90
|
+
const out: ImageField[] = [];
|
|
91
|
+
const walk = (s: Any, path: string[], name: string, ratios: RatioField[]) => {
|
|
92
|
+
const { inner } = unwrap(s);
|
|
93
|
+
if (inner instanceof z.ZodString) {
|
|
94
|
+
if (isImageFieldName(name)) out.push({ key: [prefix, ...path].join('.'), ratioFields: ratios });
|
|
95
|
+
} else if (inner instanceof z.ZodArray) {
|
|
96
|
+
const last = path[path.length - 1];
|
|
97
|
+
walk(inner.element, [...path.slice(0, -1), `${last}[]`], name, ratios);
|
|
98
|
+
} else if (inner instanceof z.ZodObject) {
|
|
99
|
+
const shape = inner.shape as Record<string, Any>;
|
|
100
|
+
const here = [...ratios];
|
|
101
|
+
if ('ratio' in shape) {
|
|
102
|
+
const { def } = unwrap(shape.ratio);
|
|
103
|
+
here.push({ key: [prefix, ...path, 'ratio'].join('.'), values: valuesOf(shape.ratio), ...(def === undefined ? {} : { default: String(def) }) });
|
|
104
|
+
}
|
|
105
|
+
for (const [k, v] of Object.entries(shape)) {
|
|
106
|
+
if (path.length === 0 && skip.includes(k)) continue;
|
|
107
|
+
walk(v, [...path, k], k, here);
|
|
108
|
+
}
|
|
109
|
+
} else if (inner instanceof z.ZodUnion || inner instanceof z.ZodDiscriminatedUnion) {
|
|
110
|
+
for (const o of inner.options as Any[]) walk(o, path, name, ratios);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
walk(schema, [], prefix, []);
|
|
114
|
+
// A union can reach one field twice: keep the first.
|
|
115
|
+
return out.filter((f, i) => out.findIndex((g) => g.key === f.key) === i);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The image fields of every block in a palette (each block keyed by its `type`). */
|
|
119
|
+
export function blockImageFields(blocks: readonly Any[]): ImageField[] {
|
|
120
|
+
return blocks.flatMap((b) => {
|
|
121
|
+
const shape = (unwrap(b).inner as z.AnyZodObject).shape as Record<string, Any>;
|
|
122
|
+
const type = (unwrap(shape.type).inner as z.ZodLiteral<string>).value;
|
|
123
|
+
return imageFieldsOf(type, b, ['blocks']);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The image fields of the content modules' exported schemas, each keyed by its export's name
|
|
129
|
+
* less `Schema` (`logoSettingsSchema` → `logoSettings`, `postSchema` → `post`).
|
|
130
|
+
*/
|
|
131
|
+
export function moduleImageFields(modules: Record<string, Record<string, unknown>>): ImageField[] {
|
|
132
|
+
const out: ImageField[] = [];
|
|
133
|
+
for (const mod of Object.values(modules)) {
|
|
134
|
+
for (const [name, value] of Object.entries(mod)) {
|
|
135
|
+
if (!name.endsWith('Schema') || !(value instanceof z.ZodType)) continue;
|
|
136
|
+
out.push(...imageFieldsOf(name.replace(/Schema$/, ''), value as Any, ['blocks']));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|