@perkyzz/emvb 1.2.0 → 1.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/THIRD_PARTY_NOTICES.md +2 -2
- package/package.json +4 -4
- package/src/admin/editor/canvas/vnode-react.tsx +3 -0
- package/src/admin/editor/dnd/new-element.ts +60 -0
- package/src/admin/editor/element-chrome.ts +28 -0
- package/src/admin/editor/panels/settings/ItemList.tsx +33 -0
- package/src/astro/request-data.ts +6 -0
- package/src/astro/resolve.ts +9 -0
- package/src/astro/widgets/EmVBCountdownRuntime.astro +8 -0
- package/src/astro/widgets/EmVBCounterRuntime.astro +8 -0
- package/src/astro/widgets/EmVBHeadlineRuntime.astro +9 -0
- package/src/astro/widgets/EmVBMapRuntime.astro +8 -0
- package/src/astro/widgets/EmVBShareRuntime.astro +9 -0
- package/src/astro/widgets/EmVBWidgetRuntimes.astro +10 -0
- package/src/astro/widgets/countdown.ts +80 -0
- package/src/astro/widgets/counter.ts +98 -0
- package/src/astro/widgets/headline.ts +90 -0
- package/src/astro/widgets/map.ts +37 -0
- package/src/astro/widgets/share.ts +21 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +28 -5
- package/src/core/arrange.ts +52 -0
- package/src/core/canvas-collections.ts +28 -0
- package/src/core/elements/counter-format.ts +15 -0
- package/src/core/elements/index.ts +30 -0
- package/src/core/elements/marketing.ts +1490 -0
- package/src/core/elements/widgets.ts +28 -3
- package/src/core/export/runtimes.generated.ts +5 -0
- package/src/core/export/site.ts +5 -0
- package/src/core/import/elementor/widgets.ts +418 -281
- package/src/core/limits.ts +6 -0
- package/src/core/render/index.ts +21 -1
- package/src/core/render/vnode.ts +1 -1
- package/src/core/schema/layout.ts +420 -17
- package/src/core/theme/dynamic.ts +2 -0
- package/src/server/plugin.ts +2 -1
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -10,7 +10,7 @@ When an editor picks an icon, its SVG is stored with the page and shown inline o
|
|
|
10
10
|
|
|
11
11
|
## Lucide
|
|
12
12
|
|
|
13
|
-
- Package: `lucide-static` 1.
|
|
13
|
+
- Package: `lucide-static` 1.73.0, <https://lucide.dev>
|
|
14
14
|
- License: ISC, Copyright (c) 2026 Lucide Icons and Contributors. Icons derived from Feather: MIT, Copyright (c) 2013-present Cole Bemis. Full text: [`licenses/lucide-static.LICENSE`](licenses/lucide-static.LICENSE).
|
|
15
15
|
- The 26 icons EmVB bundled before the library (`src/core/icons/catalog.ts`) are also Lucide icons, under the same license.
|
|
16
16
|
|
|
@@ -24,7 +24,7 @@ When an editor picks an icon, its SVG is stored with the page and shown inline o
|
|
|
24
24
|
|
|
25
25
|
## Tabler Icons
|
|
26
26
|
|
|
27
|
-
- Package: `@tabler/icons` 3.
|
|
27
|
+
- Package: `@tabler/icons` 3.52.0, <https://tabler.io/icons>
|
|
28
28
|
- License: MIT, Copyright (c) 2020-2026 Paweł Kuna. Full text: [`licenses/tabler-icons.LICENSE`](licenses/tabler-icons.LICENSE).
|
|
29
29
|
- Changes: the invisible 24×24 bounding path (`M0 0h24v24H0z`) is left out.
|
|
30
30
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@perkyzz/emvb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Visual page builder plugin for EmDash CMS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"test:astro": "bun --bun vitest run"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@perkyzz/emvb-icons": "1.
|
|
53
|
+
"@perkyzz/emvb-icons": "1.3.0",
|
|
54
54
|
"photocn": "0.1.2",
|
|
55
55
|
"zod": ">=4.5.4 <5"
|
|
56
56
|
},
|
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
"@phosphor-icons/react": "2.1.10",
|
|
61
61
|
"@types/react": "19.3.0",
|
|
62
62
|
"@types/react-dom": "19.3.0",
|
|
63
|
-
"astro": "7.4.
|
|
63
|
+
"astro": "7.4.12",
|
|
64
64
|
"emdash": "1.5.0",
|
|
65
65
|
"react": "19.3.1",
|
|
66
66
|
"react-dom": "19.3.1",
|
|
67
|
-
"vitest": "5.0
|
|
67
|
+
"vitest": "5.1.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"@cloudflare/kumo": "2.6.0 || 2.15.0",
|
|
@@ -20,6 +20,9 @@ const REACT_ATTR: Record<string, string> = {
|
|
|
20
20
|
playsinline: "playsInline",
|
|
21
21
|
autoplay: "autoPlay",
|
|
22
22
|
datetime: "dateTime",
|
|
23
|
+
// W-1048: Hotspots open their notes with the Popover API.
|
|
24
|
+
popovertarget: "popoverTarget",
|
|
25
|
+
popovertargetaction: "popoverTargetAction",
|
|
23
26
|
// The canvas never updates a radio from props, so a tab chosen there stays chosen (QA-5).
|
|
24
27
|
checked: "defaultChecked",
|
|
25
28
|
};
|
|
@@ -112,6 +112,48 @@ export function newCarouselSlide(n: number, random?: () => number): LayoutNode {
|
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
/** W-1043: the rows a new Price list starts with, then its Add item cycles through them. */
|
|
116
|
+
const STARTER_PRICES = [
|
|
117
|
+
["Espresso", "$3", "A double shot, served short."],
|
|
118
|
+
["Cappuccino", "$4.50", "Espresso, steamed milk and foam."],
|
|
119
|
+
["Croissant", "$3.75", "Baked every morning."],
|
|
120
|
+
] as const;
|
|
121
|
+
|
|
122
|
+
/** W-1043: a Price list item for a new Price list or its Add item. */
|
|
123
|
+
export function newPriceListItem(n: number, random?: () => number): LayoutNode {
|
|
124
|
+
const [title, price, description] =
|
|
125
|
+
STARTER_PRICES[(n - 1) % STARTER_PRICES.length] ?? STARTER_PRICES[0];
|
|
126
|
+
return { id: newNodeId(random), type: "price-list-item", props: { title, price, description } };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** W-1047: the services new Share links cycle through. */
|
|
130
|
+
const STARTER_SHARES = ["facebook", "x", "linkedin", "email", "whatsapp", "bluesky"] as const;
|
|
131
|
+
|
|
132
|
+
/** W-1047: a Share link for new Share buttons or their Add link. */
|
|
133
|
+
export function newShareLink(n: number, random?: () => number): LayoutNode {
|
|
134
|
+
const network = STARTER_SHARES[(n - 1) % STARTER_SHARES.length] ?? STARTER_SHARES[0];
|
|
135
|
+
return { id: newNodeId(random), type: "share-link", props: { network } };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** W-1048: where new Hotspots land, so markers added one after another don't overlap. */
|
|
139
|
+
const STARTER_SPOTS = [
|
|
140
|
+
[25, 35],
|
|
141
|
+
[60, 25],
|
|
142
|
+
[45, 70],
|
|
143
|
+
[80, 60],
|
|
144
|
+
[15, 80],
|
|
145
|
+
] as const;
|
|
146
|
+
|
|
147
|
+
/** W-1048: a Hotspot for a new Hotspots picture or its Add marker. */
|
|
148
|
+
export function newHotspot(n: number, random?: () => number): LayoutNode {
|
|
149
|
+
const [x, y] = STARTER_SPOTS[(n - 1) % STARTER_SPOTS.length] ?? STARTER_SPOTS[0];
|
|
150
|
+
return {
|
|
151
|
+
id: newNodeId(random),
|
|
152
|
+
type: "hotspot",
|
|
153
|
+
props: { title: `Point ${n}`, text: `What point ${n} shows.`, x, y },
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
115
157
|
/** W-1019: an empty Gallery image for a Gallery's Add picture; its panel picks the picture. */
|
|
116
158
|
export function newGalleryImage(random?: () => number): LayoutNode {
|
|
117
159
|
return { id: newNodeId(random), type: "gallery-image", props: { src: "", alt: "Image" } };
|
|
@@ -180,6 +222,24 @@ export function newElement(
|
|
|
180
222
|
children: [1, 2, 3].map((n) => newCarouselSlide(n, random)),
|
|
181
223
|
};
|
|
182
224
|
}
|
|
225
|
+
if (type === "hotspots") {
|
|
226
|
+
return {
|
|
227
|
+
...defaultElement("hotspots", newNodeId(random)),
|
|
228
|
+
children: [1, 2, 3].map((n) => newHotspot(n, random)),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (type === "share-buttons") {
|
|
232
|
+
return {
|
|
233
|
+
...defaultElement("share-buttons", newNodeId(random)),
|
|
234
|
+
children: [1, 2, 3, 4].map((n) => newShareLink(n, random)),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
if (type === "price-list") {
|
|
238
|
+
return {
|
|
239
|
+
...defaultElement("price-list", newNodeId(random)),
|
|
240
|
+
children: [1, 2, 3].map((n) => newPriceListItem(n, random)),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
183
243
|
if (type === "social-icons") {
|
|
184
244
|
return {
|
|
185
245
|
...defaultElement("social-icons", newNodeId(random)),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
ArrowsClockwiseIcon,
|
|
2
3
|
ArrowsLeftRightIcon,
|
|
3
4
|
ArrowsVerticalIcon,
|
|
4
5
|
ArticleIcon,
|
|
@@ -13,29 +14,42 @@ import {
|
|
|
13
14
|
CaretRightIcon,
|
|
14
15
|
CheckSquareIcon,
|
|
15
16
|
ColumnsIcon,
|
|
17
|
+
CurrencyDollarIcon,
|
|
18
|
+
CrosshairIcon,
|
|
16
19
|
CursorClickIcon,
|
|
17
20
|
FrameCornersIcon,
|
|
18
21
|
GraphIcon,
|
|
22
|
+
GaugeIcon,
|
|
19
23
|
GridFourIcon,
|
|
24
|
+
HighlighterIcon,
|
|
25
|
+
TimerIcon,
|
|
26
|
+
HashIcon,
|
|
20
27
|
ImageIcon,
|
|
21
28
|
ImagesIcon,
|
|
22
29
|
ImageSquareIcon,
|
|
23
30
|
LinkIcon,
|
|
24
31
|
ListBulletsIcon,
|
|
32
|
+
ListDashesIcon,
|
|
25
33
|
ListIcon,
|
|
26
34
|
ListNumbersIcon,
|
|
27
35
|
TranslateIcon,
|
|
36
|
+
MapPinIcon,
|
|
37
|
+
MegaphoneIcon,
|
|
28
38
|
MinusIcon,
|
|
29
39
|
NotePencilIcon,
|
|
40
|
+
NumberCircleOneIcon,
|
|
30
41
|
PaperPlaneRightIcon,
|
|
31
42
|
QuotesIcon,
|
|
32
43
|
RadioButtonIcon,
|
|
33
44
|
RepeatIcon,
|
|
34
45
|
RowsIcon,
|
|
35
46
|
LinkSimpleIcon,
|
|
47
|
+
ShareFatIcon,
|
|
48
|
+
ShareIcon,
|
|
36
49
|
ShareNetworkIcon,
|
|
37
50
|
SmileyIcon,
|
|
38
51
|
SquareIcon,
|
|
52
|
+
TagIcon,
|
|
39
53
|
SquaresFourIcon,
|
|
40
54
|
SquareHalfBottomIcon,
|
|
41
55
|
StackIcon,
|
|
@@ -98,6 +112,20 @@ export const ELEMENT_ICONS: Record<string, Icon> = {
|
|
|
98
112
|
"gallery-image": FrameCornersIcon,
|
|
99
113
|
carousel: SlideshowIcon,
|
|
100
114
|
"carousel-slide": CardsIcon,
|
|
115
|
+
counter: HashIcon,
|
|
116
|
+
progress: GaugeIcon,
|
|
117
|
+
countdown: TimerIcon,
|
|
118
|
+
map: MapPinIcon,
|
|
119
|
+
"price-table": TagIcon,
|
|
120
|
+
"price-list": ListDashesIcon,
|
|
121
|
+
"price-list-item": CurrencyDollarIcon,
|
|
122
|
+
cta: MegaphoneIcon,
|
|
123
|
+
"flip-box": ArrowsClockwiseIcon,
|
|
124
|
+
headline: HighlighterIcon,
|
|
125
|
+
"share-buttons": ShareFatIcon,
|
|
126
|
+
"share-link": ShareIcon,
|
|
127
|
+
hotspots: CrosshairIcon,
|
|
128
|
+
hotspot: NumberCircleOneIcon,
|
|
101
129
|
video: VideoCameraIcon,
|
|
102
130
|
form: ArticleIcon,
|
|
103
131
|
"text-input": TextboxIcon,
|
|
@@ -10,6 +10,9 @@ import {
|
|
|
10
10
|
import {
|
|
11
11
|
MAX_CAROUSEL_SLIDES,
|
|
12
12
|
MAX_GALLERY_IMAGES,
|
|
13
|
+
MAX_HOTSPOTS,
|
|
14
|
+
MAX_PRICE_ITEMS,
|
|
15
|
+
MAX_SHARE_LINKS,
|
|
13
16
|
MAX_SOCIAL_LINKS,
|
|
14
17
|
MAX_TABS,
|
|
15
18
|
type FieldDescriptor,
|
|
@@ -20,6 +23,9 @@ import {
|
|
|
20
23
|
newAccordionItem,
|
|
21
24
|
newCarouselSlide,
|
|
22
25
|
newGalleryImage,
|
|
26
|
+
newHotspot,
|
|
27
|
+
newPriceListItem,
|
|
28
|
+
newShareLink,
|
|
23
29
|
newSocialLink,
|
|
24
30
|
newTabPanel,
|
|
25
31
|
} from "../../dnd/new-element.ts";
|
|
@@ -73,6 +79,33 @@ const KINDS: Record<string, Kind> = {
|
|
|
73
79
|
make: (n) => newCarouselSlide(n),
|
|
74
80
|
max: MAX_CAROUSEL_SLIDES,
|
|
75
81
|
},
|
|
82
|
+
// W-1043: each row edits the item's title; its price and the rest are in its own panel.
|
|
83
|
+
"price-list": {
|
|
84
|
+
noun: "item",
|
|
85
|
+
field: (n) => ({ key: "title", kind: "text", label: `Item ${n} title` }),
|
|
86
|
+
make: (n) => newPriceListItem(n),
|
|
87
|
+
max: MAX_PRICE_ITEMS,
|
|
88
|
+
},
|
|
89
|
+
// W-1047: each row names its link for screen readers; Edit picks the service in its panel.
|
|
90
|
+
"share-buttons": {
|
|
91
|
+
noun: "link",
|
|
92
|
+
field: (n) => ({
|
|
93
|
+
key: "label",
|
|
94
|
+
kind: "text",
|
|
95
|
+
label: `Link ${n} name`,
|
|
96
|
+
optional: true,
|
|
97
|
+
message: "Leave it empty for Share on and the service's name.",
|
|
98
|
+
}),
|
|
99
|
+
make: (n) => newShareLink(n),
|
|
100
|
+
max: MAX_SHARE_LINKS,
|
|
101
|
+
},
|
|
102
|
+
// W-1048: each row edits the marker's title; its place and note are in its own panel.
|
|
103
|
+
hotspots: {
|
|
104
|
+
noun: "marker",
|
|
105
|
+
field: (n) => ({ key: "title", kind: "text", label: `Marker ${n} title` }),
|
|
106
|
+
make: (n) => newHotspot(n),
|
|
107
|
+
max: MAX_HOTSPOTS,
|
|
108
|
+
},
|
|
76
109
|
};
|
|
77
110
|
|
|
78
111
|
/** True for the elements whose Content tab is a list of their items (W-130). */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { loopCollectionAllowed } from "../core/loop-collections.ts";
|
|
2
|
+
import { someNode } from "../core/tree-ops.ts";
|
|
2
3
|
import { emdashContent, type ContentPort } from "./content-port.ts";
|
|
3
4
|
import { siteFor } from "./render-session.ts";
|
|
4
5
|
import {
|
|
@@ -18,6 +19,7 @@ type RequestData = {
|
|
|
18
19
|
site?: Record<string, string>;
|
|
19
20
|
params?: Record<string, string>;
|
|
20
21
|
origin?: string;
|
|
22
|
+
pageUrl?: string;
|
|
21
23
|
collections?: Record<string, ThemePostFields[]>;
|
|
22
24
|
abArms?: Record<string, "a" | "b">;
|
|
23
25
|
visitor?: VisitorInfo;
|
|
@@ -94,6 +96,10 @@ export async function bindingDataFor(
|
|
|
94
96
|
const lists = await Promise.all(loops.map((loop) => loopEntries(loop, content)));
|
|
95
97
|
data.collections = Object.fromEntries(loops.map((loop, i) => [loop.nodeId, lists[i] ?? []]));
|
|
96
98
|
}
|
|
99
|
+
// W-1047: Share buttons share this page's address, without its query or hash.
|
|
100
|
+
if (someNode(layout.root, (node) => node.type === "share-buttons")) {
|
|
101
|
+
data.pageUrl = `${url.origin}${url.pathname}`;
|
|
102
|
+
}
|
|
97
103
|
if (layoutUsesSource(layout.root, "param")) {
|
|
98
104
|
data.params = paramsFromSearch(url.searchParams);
|
|
99
105
|
// W-307: URL-parameter links and images stay on this origin unless allowed outside.
|
package/src/astro/resolve.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PAGES_COLLECTION } from "../constants.ts";
|
|
2
|
+
import { unlistedCanvasCollectionFirstSeen } from "../core/canvas-collections.ts";
|
|
2
3
|
import { forEachNode } from "../core/tree-ops.ts";
|
|
3
4
|
import type { Layout, LayoutNode } from "../core/schema/layout.ts";
|
|
4
5
|
import { emdashContent, type ContentPort } from "./content-port.ts";
|
|
@@ -136,6 +137,14 @@ export async function resolveEmVBPageWith(
|
|
|
136
137
|
const stored = hosted ? data[options.field ?? "canvas"] : data["layout"];
|
|
137
138
|
// An empty canvas field leaves the entry to the host's own template.
|
|
138
139
|
if (hosted && (stored === null || stored === undefined || stored === "")) return null;
|
|
140
|
+
if (hosted && unlistedCanvasCollectionFirstSeen(options.collection as string)) {
|
|
141
|
+
// W-1036 (S-08): once per collection and process; 2.0 will refuse to render it.
|
|
142
|
+
// oxlint-disable-next-line no-console
|
|
143
|
+
console.warn(
|
|
144
|
+
"emvb: rendering a layout from a collection missing from emvb({ canvasCollections }); add it there (EmVB 2.0 will not render it)",
|
|
145
|
+
{ collection: options.collection },
|
|
146
|
+
);
|
|
147
|
+
}
|
|
139
148
|
const handler = content.pluginHandler(astro.locals as never);
|
|
140
149
|
const design = await designFor(astro, handler, astro.url);
|
|
141
150
|
const layout = readLayout(stored);
|
|
@@ -5,7 +5,12 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import EmVBAlertsRuntime from "./EmVBAlertsRuntime.astro";
|
|
7
7
|
import EmVBCarouselRuntime from "./EmVBCarouselRuntime.astro";
|
|
8
|
+
import EmVBCountdownRuntime from "./EmVBCountdownRuntime.astro";
|
|
9
|
+
import EmVBCounterRuntime from "./EmVBCounterRuntime.astro";
|
|
10
|
+
import EmVBHeadlineRuntime from "./EmVBHeadlineRuntime.astro";
|
|
8
11
|
import EmVBLightboxRuntime from "./EmVBLightboxRuntime.astro";
|
|
12
|
+
import EmVBMapRuntime from "./EmVBMapRuntime.astro";
|
|
13
|
+
import EmVBShareRuntime from "./EmVBShareRuntime.astro";
|
|
9
14
|
|
|
10
15
|
interface Props {
|
|
11
16
|
names?: readonly string[] | undefined;
|
|
@@ -17,3 +22,8 @@ const names = new Set(Astro.props.names ?? []);
|
|
|
17
22
|
{names.has("alerts") && <EmVBAlertsRuntime />}
|
|
18
23
|
{names.has("lightbox") && <EmVBLightboxRuntime />}
|
|
19
24
|
{names.has("carousel") && <EmVBCarouselRuntime />}
|
|
25
|
+
{names.has("counter") && <EmVBCounterRuntime />}
|
|
26
|
+
{names.has("countdown") && <EmVBCountdownRuntime />}
|
|
27
|
+
{names.has("map") && <EmVBMapRuntime />}
|
|
28
|
+
{names.has("headline") && <EmVBHeadlineRuntime />}
|
|
29
|
+
{names.has("share") && <EmVBShareRuntime />}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1040: the opt-in Countdown runtime. It shows each ticking Countdown's clock (hidden by CSS
|
|
3
|
+
* until this script marks it ready) and updates it every second. The clock is hidden from
|
|
4
|
+
* screen readers, which read the sentence with the date instead of a number every second. A
|
|
5
|
+
* Pause button stops the updates (WCAG 2.2.2). When time is up it shows the ended text, or hides
|
|
6
|
+
* the Countdown.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
10
|
+
|
|
11
|
+
/** Whole days, hours, minutes and seconds left until `target`, never below zero. */
|
|
12
|
+
export function remaining(target: number, now: number) {
|
|
13
|
+
const total = Math.max(0, Math.floor((target - now) / 1000));
|
|
14
|
+
return {
|
|
15
|
+
total,
|
|
16
|
+
days: Math.floor(total / 86_400),
|
|
17
|
+
hours: Math.floor((total % 86_400) / 3600),
|
|
18
|
+
minutes: Math.floor((total % 3600) / 60),
|
|
19
|
+
seconds: total % 60,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function start(root: HTMLElement, target: number): void {
|
|
24
|
+
const nums = new Map<string, HTMLElement>();
|
|
25
|
+
for (const el of root.querySelectorAll<HTMLElement>(".emvb-countdown-num[data-unit]"))
|
|
26
|
+
nums.set(el.dataset["unit"] ?? "", el);
|
|
27
|
+
const pause = root.querySelector<HTMLButtonElement>(".emvb-countdown-pause");
|
|
28
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
29
|
+
|
|
30
|
+
const end = () => {
|
|
31
|
+
if (timer !== undefined) clearInterval(timer);
|
|
32
|
+
timer = undefined;
|
|
33
|
+
if (root.dataset["expired"] === "hide") {
|
|
34
|
+
root.hidden = true;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const clock = root.querySelector<HTMLElement>(".emvb-countdown-clock");
|
|
38
|
+
if (clock) clock.hidden = true;
|
|
39
|
+
root.querySelector<HTMLElement>(".emvb-countdown-expired")?.setAttribute("data-emvb-shown", "");
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const tick = () => {
|
|
43
|
+
const left = remaining(target, Date.now());
|
|
44
|
+
const hasSeconds = nums.has("seconds");
|
|
45
|
+
// Without seconds, the days to minutes left round up, so the clock never shows 0 too early.
|
|
46
|
+
const shown = hasSeconds ? left : remaining(target + 59_000, Date.now());
|
|
47
|
+
for (const unit of ["days", "hours", "minutes", "seconds"] as const) {
|
|
48
|
+
const el = nums.get(unit);
|
|
49
|
+
if (el) el.textContent = pad(shown[unit]);
|
|
50
|
+
}
|
|
51
|
+
if (left.total === 0) end();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const run = () => {
|
|
55
|
+
tick();
|
|
56
|
+
if (root.hidden || root.querySelector(".emvb-countdown-clock[hidden]")) return;
|
|
57
|
+
timer = setInterval(tick, 1000);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
pause?.addEventListener("click", () => {
|
|
61
|
+
const paused = pause.getAttribute("aria-pressed") === "true";
|
|
62
|
+
pause.setAttribute("aria-pressed", String(!paused));
|
|
63
|
+
if (paused) run();
|
|
64
|
+
else if (timer !== undefined) {
|
|
65
|
+
clearInterval(timer);
|
|
66
|
+
timer = undefined;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
run();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function initCountdowns(root: ParentNode = document): void {
|
|
73
|
+
for (const el of root.querySelectorAll<HTMLElement>("[data-emvb-countdown]")) {
|
|
74
|
+
if (el.dataset["emvbReady"] !== undefined) continue;
|
|
75
|
+
const target = Date.parse(el.dataset["target"] ?? "");
|
|
76
|
+
if (Number.isNaN(target)) continue;
|
|
77
|
+
el.dataset["emvbReady"] = "";
|
|
78
|
+
start(el, target);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1038: the opt-in Counter runtime. A Counter that asks for it counts up from its start to its
|
|
3
|
+
* number when it scrolls into view. The page is rendered with the final number, so without this
|
|
4
|
+
* script, and under reduced motion, visitors see that at once. While it counts, screen readers
|
|
5
|
+
* get the final number from a hidden copy instead of every step.
|
|
6
|
+
*/
|
|
7
|
+
import { formatCounter } from "../../core/elements/counter-format.ts";
|
|
8
|
+
|
|
9
|
+
type Counter = {
|
|
10
|
+
el: HTMLElement;
|
|
11
|
+
from: number;
|
|
12
|
+
to: number;
|
|
13
|
+
decimals: number;
|
|
14
|
+
separator: string;
|
|
15
|
+
duration: number;
|
|
16
|
+
final: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const easeOut = (t: number) => 1 - (1 - t) ** 3;
|
|
20
|
+
|
|
21
|
+
function read(el: HTMLElement): Counter | undefined {
|
|
22
|
+
const num = (key: string, fallback: number) => {
|
|
23
|
+
const value = Number(el.dataset[key] ?? fallback);
|
|
24
|
+
return Number.isFinite(value) ? value : fallback;
|
|
25
|
+
};
|
|
26
|
+
const to = Number(el.dataset["to"]);
|
|
27
|
+
if (!Number.isFinite(to)) return undefined;
|
|
28
|
+
return {
|
|
29
|
+
el,
|
|
30
|
+
from: num("from", 0),
|
|
31
|
+
to,
|
|
32
|
+
decimals: Math.min(3, Math.max(0, Math.round(num("decimals", 0)))),
|
|
33
|
+
separator: el.dataset["separator"] ?? "",
|
|
34
|
+
duration: Math.min(5000, Math.max(0, num("duration", 2000))),
|
|
35
|
+
final: el.textContent ?? "",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Shows `c.from`, with the final number kept for screen readers. */
|
|
40
|
+
function prepare(c: Counter): void {
|
|
41
|
+
c.el.setAttribute("aria-hidden", "true");
|
|
42
|
+
const copy = c.el.ownerDocument.createElement("span");
|
|
43
|
+
copy.className = "emvb-counter-sr";
|
|
44
|
+
copy.textContent = c.final;
|
|
45
|
+
c.el.after(copy);
|
|
46
|
+
c.el.textContent = formatCounter(c.from, c.decimals, c.separator);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Ends on the rendered text, and gives the number back to screen readers. */
|
|
50
|
+
function finish(c: Counter): void {
|
|
51
|
+
c.el.textContent = c.final;
|
|
52
|
+
c.el.removeAttribute("aria-hidden");
|
|
53
|
+
const copy = c.el.nextElementSibling;
|
|
54
|
+
if (copy?.classList.contains("emvb-counter-sr")) copy.remove();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function run(c: Counter): void {
|
|
58
|
+
const began = performance.now();
|
|
59
|
+
const step = (now: number) => {
|
|
60
|
+
const t = c.duration > 0 ? Math.min(1, (now - began) / c.duration) : 1;
|
|
61
|
+
if (t >= 1) {
|
|
62
|
+
finish(c);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const value = c.from + (c.to - c.from) * easeOut(t);
|
|
66
|
+
c.el.textContent = formatCounter(value, c.decimals, c.separator);
|
|
67
|
+
requestAnimationFrame(step);
|
|
68
|
+
};
|
|
69
|
+
requestAnimationFrame(step);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function initCounters(root: ParentNode = document): void {
|
|
73
|
+
const reduced = globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
|
|
74
|
+
if (reduced || typeof IntersectionObserver === "undefined") return;
|
|
75
|
+
const counters = new Map<Element, Counter>();
|
|
76
|
+
for (const el of root.querySelectorAll<HTMLElement>("[data-emvb-counter]")) {
|
|
77
|
+
if (el.dataset["emvbReady"] !== undefined) continue;
|
|
78
|
+
el.dataset["emvbReady"] = "";
|
|
79
|
+
const c = read(el);
|
|
80
|
+
if (!c || c.from === c.to) continue;
|
|
81
|
+
prepare(c);
|
|
82
|
+
counters.set(el, c);
|
|
83
|
+
}
|
|
84
|
+
if (counters.size === 0) return;
|
|
85
|
+
const observer = new IntersectionObserver(
|
|
86
|
+
(entries) => {
|
|
87
|
+
for (const entry of entries) {
|
|
88
|
+
const c = counters.get(entry.target);
|
|
89
|
+
if (!entry.isIntersecting || !c) continue;
|
|
90
|
+
observer.unobserve(entry.target);
|
|
91
|
+
counters.delete(entry.target);
|
|
92
|
+
run(c);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
{ threshold: 0.5 },
|
|
96
|
+
);
|
|
97
|
+
for (const el of counters.keys()) observer.observe(el);
|
|
98
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1046: the opt-in Animated headline runtime. Each headline with rotating words shows them in
|
|
3
|
+
* turn in its highlight, every `data-interval` seconds for `data-turns` rounds, and stops on the
|
|
4
|
+
* first word. A Pause button after the heading stops it at any time (WCAG 2.2.2). Screen readers
|
|
5
|
+
* keep the sentence with the first word: the changing words are hidden from them meanwhile.
|
|
6
|
+
* Under reduced motion nothing rotates and no button is added.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type Rotation = { words: string[]; interval: number; turns: number };
|
|
10
|
+
|
|
11
|
+
function read(mark: HTMLElement): Rotation | undefined {
|
|
12
|
+
let words: unknown;
|
|
13
|
+
try {
|
|
14
|
+
words = JSON.parse(mark.dataset["emvbRotate"] ?? "");
|
|
15
|
+
} catch {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
if (!Array.isArray(words)) return undefined;
|
|
19
|
+
const list = words.filter((w): w is string => typeof w === "string" && w.trim() !== "");
|
|
20
|
+
if (list.length < 2) return undefined;
|
|
21
|
+
const interval = Number(mark.dataset["interval"]);
|
|
22
|
+
const turns = Number(mark.dataset["turns"]);
|
|
23
|
+
return {
|
|
24
|
+
words: list,
|
|
25
|
+
interval: Number.isFinite(interval) ? Math.min(10, Math.max(1.5, interval)) : 2.5,
|
|
26
|
+
turns: Number.isFinite(turns) ? Math.min(5, Math.max(1, Math.round(turns))) : 2,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function start(mark: HTMLElement, r: Rotation): void {
|
|
31
|
+
const doc = mark.ownerDocument;
|
|
32
|
+
const heading = mark.closest("h1, h2, h3, h4, h5, h6") ?? mark;
|
|
33
|
+
// The word changes in its own text node; the circle (if any) stays.
|
|
34
|
+
const found = [...mark.childNodes].find((n) => n.nodeType === Node.TEXT_NODE);
|
|
35
|
+
if (!found) return;
|
|
36
|
+
const text: Node = found;
|
|
37
|
+
const spoken = doc.createElement("span");
|
|
38
|
+
spoken.className = "emvb-headline-sr";
|
|
39
|
+
spoken.textContent = r.words[0] ?? "";
|
|
40
|
+
mark.before(spoken);
|
|
41
|
+
mark.setAttribute("aria-hidden", "true");
|
|
42
|
+
|
|
43
|
+
const pause = doc.createElement("button");
|
|
44
|
+
pause.type = "button";
|
|
45
|
+
pause.className = "emvb-headline-pause";
|
|
46
|
+
pause.setAttribute("aria-pressed", "false");
|
|
47
|
+
pause.textContent = "Pause";
|
|
48
|
+
heading.after(pause);
|
|
49
|
+
|
|
50
|
+
let step = 0;
|
|
51
|
+
const last = r.words.length * r.turns;
|
|
52
|
+
const timer = setInterval(() => {
|
|
53
|
+
step++;
|
|
54
|
+
text.textContent = r.words[step % r.words.length] ?? "";
|
|
55
|
+
if (step >= last) stop();
|
|
56
|
+
}, r.interval * 1000);
|
|
57
|
+
|
|
58
|
+
function stop(): void {
|
|
59
|
+
clearInterval(timer);
|
|
60
|
+
text.textContent = r.words[0] ?? "";
|
|
61
|
+
mark.removeAttribute("aria-hidden");
|
|
62
|
+
spoken.remove();
|
|
63
|
+
pause.remove();
|
|
64
|
+
}
|
|
65
|
+
// Pausing ends the rotation for good, back on the first word.
|
|
66
|
+
pause.addEventListener("click", () => {
|
|
67
|
+
const next = pause.nextElementSibling;
|
|
68
|
+
stop();
|
|
69
|
+
if (
|
|
70
|
+
next instanceof HTMLElement &&
|
|
71
|
+
next.matches("a[href], button, input, select, textarea, [tabindex]")
|
|
72
|
+
)
|
|
73
|
+
next.focus();
|
|
74
|
+
else if (heading instanceof HTMLElement) {
|
|
75
|
+
heading.tabIndex = -1;
|
|
76
|
+
heading.focus();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function initHeadlines(root: ParentNode = document): void {
|
|
82
|
+
const reduced = globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
|
|
83
|
+
if (reduced) return;
|
|
84
|
+
for (const mark of root.querySelectorAll<HTMLElement>("[data-emvb-rotate]")) {
|
|
85
|
+
if (mark.dataset["emvbReady"] !== undefined) continue;
|
|
86
|
+
mark.dataset["emvbReady"] = "";
|
|
87
|
+
const r = read(mark);
|
|
88
|
+
if (r) start(mark, r);
|
|
89
|
+
}
|
|
90
|
+
}
|