maverick-wave-astro 1.10.0 → 1.12.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.
@@ -0,0 +1,298 @@
1
+ ---
2
+ // Story images in 9:16 for Instagram Story and WhatsApp Status, drawn in the
3
+ // browser from what the site keeps anyway: the upcoming events and the running
4
+ // promotion. Meant for a page of its own with noIndex and no link to it - the
5
+ // owner's tool, not a page of the homepage.
6
+ import '../styles/story-cards.css';
7
+
8
+ import { config } from '../dist/config.js';
9
+ import { PACKAGE_ICONS } from '../dist/icons.js';
10
+ import { withBase } from '../dist/path.js';
11
+ import {
12
+ discountText,
13
+ eventDateText,
14
+ eventExpiry,
15
+ type Promo,
16
+ promoEndDate,
17
+ promoExpiry,
18
+ promoInHtml,
19
+ promoStart,
20
+ promoText,
21
+ upcomingEvents,
22
+ } from '../dist/schedule.js';
23
+ import type { LookOptions, StoryIcon } from '../dist/story/base.js';
24
+ import {
25
+ promoOffers,
26
+ type StoryCard,
27
+ storyDate,
28
+ type StoryEvent,
29
+ storyFileName,
30
+ type StoryOffer,
31
+ } from '../dist/story/cards.js';
32
+ import type { StoryLook } from '../dist/types.js';
33
+
34
+ interface Props extends StoryLook {
35
+ events?: readonly StoryEvent[];
36
+ promo?: Promo | null;
37
+ // The prices the promotion applies to - as in Price, only those with an
38
+ // amount.
39
+ offers?: readonly StoryOffer[];
40
+ // Path data from the site's own icon set, so the story matches the page.
41
+ icons?: Partial<Record<StoryIcon, string>>;
42
+ texts?: {
43
+ share?: string;
44
+ save?: string;
45
+ empty?: string;
46
+ soldOut?: string;
47
+ free?: string;
48
+ currency?: string;
49
+ until?: string;
50
+ untilTomorrow?: string;
51
+ lastDay?: string;
52
+ // With {n} for the number of days.
53
+ daysLeft?: string;
54
+ // The label top left, e.g. "Workshop" instead of the general "Termin".
55
+ eventKicker?: string;
56
+ promoKicker?: string;
57
+ // The call to action below the card, e.g. where to sign up or book.
58
+ eventFooter?: string;
59
+ promoFooter?: string;
60
+ // A second line below both, e.g. an invitation to reply to the story.
61
+ reply?: string;
62
+ };
63
+ }
64
+
65
+ const german = config.site.lang.startsWith('de');
66
+
67
+ const {
68
+ logo,
69
+ background,
70
+ events = [],
71
+ promo,
72
+ offers = [],
73
+ icons = {},
74
+ texts = {},
75
+ } = Astro.props;
76
+
77
+ const {
78
+ share = german ? 'Bild teilen' : 'Share image',
79
+ save = german ? 'Bild speichern' : 'Save image',
80
+ empty = german
81
+ ? 'Gerade gibt es nichts zu teilen.'
82
+ : 'Nothing to share right now.',
83
+ soldOut = german ? 'Ausgebucht' : 'Sold out',
84
+ free = german ? 'kostenlos' : 'free',
85
+ currency = 'Euro',
86
+ until = german ? 'Nur noch bis' : 'Only until',
87
+ untilTomorrow = german ? 'Nur noch bis morgen' : 'Ends tomorrow',
88
+ lastDay = german ? 'Nur noch heute' : 'Last day today',
89
+ daysLeft = german ? 'Nur noch {n} Tage' : 'Only {n} days left',
90
+ eventKicker = german ? 'Termin' : 'Event',
91
+ promoKicker = german ? 'Aktion' : 'Offer',
92
+ eventFooter,
93
+ promoFooter,
94
+ reply,
95
+ } = texts;
96
+
97
+ const site = config.site.name;
98
+
99
+ interface Story {
100
+ card: StoryCard;
101
+ label: string;
102
+ file: string;
103
+ starts?: string | null;
104
+ expires?: string | null;
105
+ }
106
+
107
+ const eventStories = upcomingEvents(events).map((entry): Story => ({
108
+ card: {
109
+ kind: 'event',
110
+ title: entry.title,
111
+ date: eventDateText(entry),
112
+ stamp: storyDate(entry.date),
113
+ time: entry.time,
114
+ location: entry.location,
115
+ price: entry.price !== undefined ? String(entry.price) : undefined,
116
+ text: entry.text,
117
+ soldOut: entry.soldOut,
118
+ },
119
+ label: entry.title,
120
+ file: storyFileName(site, entry.title, entry.date),
121
+ expires: eventExpiry(entry),
122
+ }));
123
+
124
+ const discounted =
125
+ promo && promoInHtml(promo) ? promoOffers(promo, offers) : [];
126
+
127
+ const promoStories: Story[] =
128
+ promo && discounted.length > 0
129
+ ? [
130
+ {
131
+ card: {
132
+ kind: 'promo',
133
+ percent: discountText(promo),
134
+ title: promo.text,
135
+ until: promoEndDate(promo),
136
+ untilIso: promo.until,
137
+ offers: discounted,
138
+ },
139
+ label: promoText(promo),
140
+ file: storyFileName(
141
+ site,
142
+ german ? 'rabatt' : 'promo',
143
+ promo.until ?? ''
144
+ ),
145
+ starts: promoStart(promo),
146
+ expires: promoExpiry(promo),
147
+ },
148
+ ]
149
+ : [];
150
+
151
+ const look: LookOptions = {
152
+ logo: withBase(logo),
153
+ background,
154
+ icons: {
155
+ calendar: icons.calendar ?? PACKAGE_ICONS.calendar,
156
+ clock: icons.clock ?? PACKAGE_ICONS.clock,
157
+ location: icons.location ?? PACKAGE_ICONS['map-pin'],
158
+ sparkle: icons.sparkle ?? PACKAGE_ICONS.sparkle,
159
+ },
160
+ texts: {
161
+ soldOut,
162
+ free,
163
+ currency,
164
+ until,
165
+ untilTomorrow,
166
+ lastDay,
167
+ daysLeft,
168
+ eventKicker,
169
+ promoKicker,
170
+ eventFooter,
171
+ promoFooter,
172
+ reply,
173
+ },
174
+ };
175
+ ---
176
+
177
+ <div
178
+ class="mwa-story-cards"
179
+ data-story-cards={JSON.stringify(look)}
180
+ data-share={share}
181
+ data-save={save}
182
+ >
183
+ {
184
+ [...eventStories, ...promoStories].map(story => (
185
+ <article
186
+ class="mwa-story-card"
187
+ data-story={JSON.stringify(story.card)}
188
+ data-file={story.file}
189
+ data-starts={story.starts}
190
+ data-expires={story.expires}
191
+ >
192
+ <div class="mwa-story-card-stage">
193
+ <canvas
194
+ class="mwa-story-card-image"
195
+ role="img"
196
+ aria-label={story.label}
197
+ />
198
+ <button
199
+ type="button"
200
+ class="mw-btn mw-btn-primary mw-btn-lg mwa-story-card-btn"
201
+ data-story-save
202
+ />
203
+ </div>
204
+ </article>
205
+ ))
206
+ }
207
+ <p class="mwa-story-cards-empty" data-story-empty hidden>{empty}</p>
208
+ </div>
209
+
210
+ <script>
211
+ import {
212
+ HEIGHT,
213
+ loadLook,
214
+ type LookOptions,
215
+ WIDTH,
216
+ } from '../dist/story/base.js';
217
+ import type { StoryCard } from '../dist/story/cards.js';
218
+ import { drawEvent } from '../dist/story/event.js';
219
+ import { drawPromo } from '../dist/story/promo.js';
220
+
221
+ const root = document.querySelector<HTMLElement>('[data-story-cards]');
222
+ const now = new Date();
223
+ const stories = [
224
+ ...document.querySelectorAll<HTMLElement>('[data-story]'),
225
+ ].filter(el => {
226
+ // As on the start page: expired events and promotions drop out in the
227
+ // browser, promotions that have not started yet as well - no deployment
228
+ // needed for that.
229
+ const { starts, expires } = el.dataset;
230
+ if (
231
+ (expires && new Date(expires) < now) ||
232
+ (starts && new Date(starts) > now)
233
+ ) {
234
+ el.remove();
235
+ return false;
236
+ }
237
+ return true;
238
+ });
239
+
240
+ if (root && stories.length === 0) {
241
+ root.querySelector('[data-story-empty]')?.removeAttribute('hidden');
242
+ } else if (root) {
243
+ // On the phone sharing leads straight to Instagram or WhatsApp, the detour
244
+ // through the gallery goes away. On the desktop there is no such thing.
245
+ const canShare =
246
+ navigator.canShare?.({
247
+ files: [new File([], 'test.png', { type: 'image/png' })],
248
+ }) ?? false;
249
+
250
+ const save = async (canvas: HTMLCanvasElement, name: string) => {
251
+ const blob = await new Promise<Blob | null>(done =>
252
+ canvas.toBlob(done, 'image/png')
253
+ );
254
+ const file = blob && new File([blob], name, { type: 'image/png' });
255
+
256
+ if (file && canShare) {
257
+ try {
258
+ await navigator.share({ files: [file] });
259
+ return;
260
+ } catch {
261
+ // Cancelled or refused - then download it instead.
262
+ }
263
+ }
264
+
265
+ const link = document.createElement('a');
266
+ // data: instead of blob: - the CSP of the page only lets data: through.
267
+ link.href = canvas.toDataURL('image/png');
268
+ link.download = name;
269
+ link.click();
270
+ };
271
+
272
+ const look = await loadLook(
273
+ JSON.parse(root.dataset.storyCards ?? '{}') as LookOptions
274
+ );
275
+ const label = (canShare ? root.dataset.share : root.dataset.save) ?? '';
276
+
277
+ for (const el of stories) {
278
+ const canvas = el.querySelector('canvas');
279
+ const button = el.querySelector<HTMLButtonElement>('[data-story-save]');
280
+ const ctx = canvas?.getContext('2d');
281
+ if (!canvas || !button || !ctx) continue;
282
+
283
+ canvas.width = WIDTH;
284
+ canvas.height = HEIGHT;
285
+ const card = JSON.parse(el.dataset.story ?? '{}') as StoryCard;
286
+ if (card.kind === 'promo') {
287
+ drawPromo(ctx, card, look);
288
+ } else {
289
+ drawEvent(ctx, card, look);
290
+ }
291
+
292
+ button.textContent = label;
293
+ button.addEventListener('click', () =>
294
+ save(canvas, `${el.dataset.file}.png`)
295
+ );
296
+ }
297
+ }
298
+ </script>
package/dist/icons.d.ts CHANGED
@@ -15,5 +15,6 @@ export declare const PACKAGE_ICONS: {
15
15
  readonly 'map-pin': "<path d=\"M12 21s-6.5-5.6-6.5-11a6.5 6.5 0 0 1 13 0c0 5.4-6.5 11-6.5 11Z\"/><circle cx=\"12\" cy=\"10\" r=\"2.4\"/>";
16
16
  readonly mail: "<rect x=\"3\" y=\"5\" width=\"18\" height=\"14\" rx=\"2.4\"/><path d=\"m3.5 6.5 8.5 6.5 8.5-6.5\"/>";
17
17
  readonly 'calendar-check': "<rect x=\"3\" y=\"5\" width=\"18\" height=\"16\" rx=\"2.4\"/><path d=\"M3 10h18M8 3v4M16 3v4M9 15.5l2 2 4-4\"/>";
18
+ readonly sparkle: "<path d=\"M12 2.8 13.9 9l6.2 1.9-6.2 1.9L12 19l-1.9-6.2L3.9 10.9 10.1 9 12 2.8Z\"/><path d=\"M19 16.5l.7 2.2 2.2.7-2.2.7-.7 2.2-.7-2.2-2.2-.7 2.2-.7.7-2.2Z\"/>";
18
19
  readonly 'panel-right': "<rect x=\"3\" y=\"4\" width=\"18\" height=\"16\" rx=\"2.4\"/><path d=\"M15 4v16\"/>";
19
20
  };
package/dist/icons.js CHANGED
@@ -17,5 +17,6 @@ export const PACKAGE_ICONS = {
17
17
  'map-pin': '<path d="M12 21s-6.5-5.6-6.5-11a6.5 6.5 0 0 1 13 0c0 5.4-6.5 11-6.5 11Z"/><circle cx="12" cy="10" r="2.4"/>',
18
18
  mail: '<rect x="3" y="5" width="18" height="14" rx="2.4"/><path d="m3.5 6.5 8.5 6.5 8.5-6.5"/>',
19
19
  'calendar-check': '<rect x="3" y="5" width="18" height="16" rx="2.4"/><path d="M3 10h18M8 3v4M16 3v4M9 15.5l2 2 4-4"/>',
20
+ sparkle: '<path d="M12 2.8 13.9 9l6.2 1.9-6.2 1.9L12 19l-1.9-6.2L3.9 10.9 10.1 9 12 2.8Z"/><path d="M19 16.5l.7 2.2 2.2.7-2.2.7-.7 2.2-.7-2.2-2.2-.7 2.2-.7.7-2.2Z"/>',
20
21
  'panel-right': '<rect x="3" y="4" width="18" height="16" rx="2.4"/><path d="M15 4v16"/>',
21
22
  };
@@ -3,7 +3,7 @@ import type { AstroIntegration } from 'astro';
3
3
  import { type Werkbank } from './content.ts';
4
4
  import type { Config, OgImage, Site } from './types.ts';
5
5
  export type { Design } from './design.ts';
6
- export type { OgImage, Site } from './types.ts';
6
+ export type { OgImage, Site, StoryLook } from './types.ts';
7
7
  export type Options = {
8
8
  site: Site;
9
9
  ogImage: OgImage;
@@ -0,0 +1,122 @@
1
+ import type { StoryLook } from '../types.ts';
2
+ export declare const WIDTH = 1080;
3
+ export declare const HEIGHT = 1920;
4
+ export declare const PADDING = 76;
5
+ export declare const LEFT: number;
6
+ export declare const CONTENT_WIDTH: number;
7
+ export declare const OVERHANG = 72;
8
+ type Rgb = readonly [number, number, number];
9
+ export type StoryIcon = 'calendar' | 'clock' | 'location' | 'sparkle';
10
+ export interface StoryTexts {
11
+ soldOut: string;
12
+ free: string;
13
+ currency: string;
14
+ until: string;
15
+ untilTomorrow: string;
16
+ lastDay: string;
17
+ /** With {n} for the number of days. */
18
+ daysLeft: string;
19
+ eventKicker?: string;
20
+ promoKicker?: string;
21
+ eventFooter?: string;
22
+ promoFooter?: string;
23
+ reply?: string;
24
+ }
25
+ export interface LookOptions extends StoryLook {
26
+ icons: Record<StoryIcon, string>;
27
+ texts: StoryTexts;
28
+ }
29
+ export type Look = Awaited<ReturnType<typeof loadLook>>;
30
+ export interface Stage {
31
+ top: number;
32
+ bottom: number;
33
+ height: number;
34
+ }
35
+ export declare const loadLook: (options: LookOptions) => Promise<{
36
+ colors: {
37
+ primary: string;
38
+ secondary: string;
39
+ text: string;
40
+ muted: string;
41
+ graySurface: string;
42
+ accent: string;
43
+ shadowNear: string;
44
+ shadowFar: string;
45
+ primaryRgb: Rgb;
46
+ secondaryRgb: Rgb;
47
+ grayClear: string;
48
+ accentText: string;
49
+ onPrimary: string;
50
+ onSecondary: string;
51
+ };
52
+ font: (size: number, weight?: number) => string;
53
+ heading: (size: number, weight?: number) => string;
54
+ radii: number[];
55
+ logo: {
56
+ width: number;
57
+ height: number;
58
+ image: HTMLImageElement;
59
+ } | null;
60
+ icons: {
61
+ calendar: HTMLImageElement | null;
62
+ clock: HTMLImageElement | null;
63
+ location: HTMLImageElement | null;
64
+ sparkle: HTMLImageElement | null;
65
+ };
66
+ grain: HTMLCanvasElement | null;
67
+ background: readonly [string, string];
68
+ texts: StoryTexts;
69
+ }>;
70
+ export interface Block {
71
+ height: number;
72
+ gap: number;
73
+ paint: (y: number) => void;
74
+ }
75
+ export declare const totalHeight: (blocks: Block[]) => number;
76
+ export declare const paintColumn: (blocks: Block[], top: number) => number;
77
+ export declare const spaced: (ctx: CanvasRenderingContext2D, pixels: number) => void;
78
+ export declare const fitLine: (ctx: CanvasRenderingContext2D, text: string, maxWidth: number) => string;
79
+ interface TextStyle {
80
+ font: (size: number) => string;
81
+ color: string;
82
+ /** Tried from the first on - the largest at which the text fits wins. */
83
+ sizes: number[];
84
+ lineHeight: number;
85
+ maxLines: number;
86
+ letterSpacing?: number;
87
+ }
88
+ export declare const textBlock: (ctx: CanvasRenderingContext2D, text: string, style: TextStyle, gap: number, { x, maxWidth }?: {
89
+ x?: number | undefined;
90
+ maxWidth?: number | undefined;
91
+ }) => Block;
92
+ export declare const iconRow: (ctx: CanvasRenderingContext2D, image: HTMLImageElement | null, text: string, { font, color, gap, letterSpacing, }: {
93
+ font: string;
94
+ color: string;
95
+ gap: number;
96
+ letterSpacing?: number;
97
+ }, { x, maxWidth }?: {
98
+ x?: number | undefined;
99
+ maxWidth?: number | undefined;
100
+ }) => Block;
101
+ export declare const divider: (ctx: CanvasRenderingContext2D, look: Look, gap: number) => Block;
102
+ export interface PillStyle {
103
+ font: string;
104
+ color: string;
105
+ fill: string;
106
+ stroke?: string;
107
+ height: number;
108
+ padding: number;
109
+ letterSpacing?: number;
110
+ }
111
+ export declare const pill: (ctx: CanvasRenderingContext2D, text: string, style: PillStyle, maxWidth: number) => {
112
+ width: number;
113
+ height: number;
114
+ paint: (x: number, y: number) => void;
115
+ };
116
+ export declare const drawSurface: (ctx: CanvasRenderingContext2D, look: Look, x: number, y: number, width: number, height: number, radii?: number[]) => void;
117
+ export declare const drawStory: (ctx: CanvasRenderingContext2D, look: Look, { kicker, footer, content, }: {
118
+ kicker?: string;
119
+ footer?: string;
120
+ content: (card: Stage) => void;
121
+ }) => void;
122
+ export {};