@perkyzz/emvb 1.1.0 → 1.2.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 +1 -0
- package/package.json +10 -9
- package/src/admin/agents-api.ts +5 -1
- package/src/admin/content-api.ts +29 -0
- package/src/admin/editor/Editor.tsx +6 -1
- package/src/admin/editor/ScheduleDialog.tsx +167 -0
- package/src/admin/editor/TopBar.tsx +33 -1
- package/src/admin/editor/dnd/new-element.ts +46 -0
- package/src/admin/editor/editor-chrome.tsx +9 -0
- package/src/admin/editor/element-chrome.ts +22 -0
- package/src/admin/editor/panels/ElementPanel.tsx +5 -1
- package/src/admin/editor/panels/settings/DraftTextField.tsx +33 -1
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +6 -1
- package/src/admin/editor/panels/settings/FieldControl.tsx +10 -1
- package/src/admin/editor/panels/settings/IconPicker.tsx +4 -1
- package/src/admin/editor/panels/settings/ItemList.tsx +36 -2
- package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
- package/src/admin/editor/store.ts +7 -1
- package/src/admin/editor/useEditorData.ts +4 -0
- package/src/admin/editor/useSave.ts +47 -3
- package/src/admin/export-api.ts +20 -3
- package/src/admin/menus-api.ts +113 -0
- package/src/admin/pages/ImportElementorDialog.tsx +25 -1
- package/src/astro/EmVBPage.astro +3 -0
- package/src/astro/render.ts +43 -18
- package/src/astro/request-data.ts +3 -0
- package/src/astro/resolve-theme.ts +77 -44
- package/src/astro/visitor.ts +23 -4
- package/src/astro/widgets/EmVBAlertsRuntime.astro +8 -0
- package/src/astro/widgets/EmVBCarouselRuntime.astro +8 -0
- package/src/astro/widgets/EmVBLightboxRuntime.astro +8 -0
- package/src/astro/widgets/EmVBWidgetRuntimes.astro +19 -0
- package/src/astro/widgets/alerts.ts +37 -0
- package/src/astro/widgets/carousel.ts +167 -0
- package/src/astro/widgets/index.ts +1 -0
- package/src/astro/widgets/lightbox.ts +123 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +101 -2
- package/src/core/arrange.ts +57 -1
- package/src/core/bounded-array.ts +16 -13
- package/src/core/contract.ts +4 -1
- package/src/core/elements/icon-svg.ts +135 -0
- package/src/core/elements/index.ts +33 -127
- package/src/core/elements/widgets.ts +907 -0
- package/src/core/export/runtimes.generated.ts +3 -0
- package/src/core/export/site.ts +20 -3
- package/src/core/extensions/schema.ts +2 -1
- package/src/core/geo-trust.ts +18 -0
- package/src/core/icons/social.generated.ts +29 -0
- package/src/core/image-edit/schema.ts +2 -1
- package/src/core/import/elementor/context.ts +18 -0
- package/src/core/import/elementor/index.ts +50 -7
- package/src/core/import/elementor/widgets.ts +394 -133
- package/src/core/issue-cap.ts +19 -0
- package/src/core/limits.ts +6 -0
- package/src/core/loop-collections.ts +33 -0
- package/src/core/proposals.ts +8 -2
- package/src/core/render/index.ts +27 -4
- package/src/core/render/vnode.ts +44 -6
- package/src/core/sanitize/svg-upload.ts +276 -23
- package/src/core/sanitize/svg.ts +3 -3
- package/src/core/schema/descriptors.ts +4 -0
- package/src/core/schema/design.ts +6 -5
- package/src/core/schema/layout.ts +395 -15
- package/src/core/schema/style.ts +2 -1
- package/src/core/shadowing-ids.ts +41 -0
- package/src/core/theme/conditions.ts +2 -1
- package/src/core/theme/parse-doc.ts +10 -5
- package/src/core/theme/triggers.ts +3 -2
- package/src/core/validate.ts +3 -2
- package/src/index.ts +23 -2
- package/src/mcp/ports.ts +18 -7
- package/src/mcp/proposals.ts +36 -11
- package/src/mcp/tools.ts +1 -1
- package/src/server/agent-settings.ts +12 -1
- package/src/server/hooks.ts +41 -6
- package/src/server/mcp-routes.ts +11 -5
- package/src/server/plugin.ts +27 -2
- package/src/server/presence.ts +5 -1
- package/src/server/proposals.ts +16 -4
- package/src/server/requests.ts +60 -11
- package/src/server/site-origin.ts +13 -0
- package/src/server/targets.ts +26 -0
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
} from "../core/index.ts";
|
|
38
38
|
import { THEME_PARTS_COLLECTION } from "../constants.ts";
|
|
39
39
|
import { readLayout, renderStored, type RenderedPage } from "./render.ts";
|
|
40
|
+
import type { WidgetRuntime } from "../core/elements/widgets.ts";
|
|
40
41
|
import { designFor, formsFor, layoutFor, partsFor } from "./render-session.ts";
|
|
41
42
|
import type { HostVisitor, VisitorAstro } from "./visitor.ts";
|
|
42
43
|
import { bindingDataFor } from "./request-data.ts";
|
|
@@ -98,6 +99,11 @@ export type ResolvedThemeParts = {
|
|
|
98
99
|
needsMenuRuntime: boolean;
|
|
99
100
|
/** Hosts load `EmVBFormsRuntime` only when a winning part renders a form. */
|
|
100
101
|
needsFormsRuntime: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* W-1025: the opt-in widget scripts the winning parts need; set only when there is one. Mount
|
|
104
|
+
* `<EmVBWidgetRuntimes names={theme.runtimes} />` from `@perkyzz/emvb/astro/widgets`.
|
|
105
|
+
*/
|
|
106
|
+
runtimes?: WidgetRuntime[];
|
|
101
107
|
/**
|
|
102
108
|
* True for an archive page past the last one (`/posts/page/9` with no posts there). Hosts
|
|
103
109
|
* answer 404 (W-221). Page 1 with no posts is not a 404.
|
|
@@ -411,6 +417,12 @@ async function buildDynamicForContent(
|
|
|
411
417
|
return undefined;
|
|
412
418
|
}
|
|
413
419
|
|
|
420
|
+
/** W-1025: every widget runtime the rendered parts need, once each, or nothing. */
|
|
421
|
+
function partRuntimes(rendered: readonly RenderedPage[]): { runtimes?: WidgetRuntime[] } {
|
|
422
|
+
const names = [...new Set(rendered.flatMap((page) => page.runtimes ?? []))].toSorted();
|
|
423
|
+
return names.length > 0 ? { runtimes: names } : {};
|
|
424
|
+
}
|
|
425
|
+
|
|
414
426
|
/**
|
|
415
427
|
* Resolve winning published theme parts for this request (R-062 / S7b–S7d).
|
|
416
428
|
* Returns plain HTML/CSS; popup JS is optional and only when `needsPopupsRuntime`.
|
|
@@ -441,6 +453,20 @@ export async function resolveThemeParts(
|
|
|
441
453
|
return resolveThemePartsWith(emdashContent, astro, ctx, options);
|
|
442
454
|
}
|
|
443
455
|
|
|
456
|
+
/** S-02: a part that fails to render is left out, so the rest of the site still renders. */
|
|
457
|
+
async function orSkip<T>(id: string, render: () => Promise<T | null>): Promise<T | null> {
|
|
458
|
+
try {
|
|
459
|
+
return await render();
|
|
460
|
+
} catch (error) {
|
|
461
|
+
// oxlint-disable-next-line no-console
|
|
462
|
+
console.error("emvb: theme part failed to render", {
|
|
463
|
+
partId: id,
|
|
464
|
+
error: error instanceof Error ? error.name : "unknown",
|
|
465
|
+
});
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
444
470
|
/** Same as `resolveThemeParts`, with a content port so tests can pass a fake (W-359). */
|
|
445
471
|
export async function resolveThemePartsWith(
|
|
446
472
|
port: ContentPort,
|
|
@@ -605,7 +631,9 @@ export async function resolveThemePartsWith(
|
|
|
605
631
|
|
|
606
632
|
let pastLastPage = false;
|
|
607
633
|
let archiveInfo: { title?: string; page: number; titleText?: string } | undefined;
|
|
608
|
-
const renderPart =
|
|
634
|
+
const renderPart = (winner: ThemePartCandidate | null): Promise<RenderedThemePart | null> =>
|
|
635
|
+
orSkip(winner?.id ?? "", () => renderPartOf(winner));
|
|
636
|
+
const renderPartOf = async (
|
|
609
637
|
winner: ThemePartCandidate | null,
|
|
610
638
|
): Promise<RenderedThemePart | null> => {
|
|
611
639
|
const stored = winner ? byId.get(winner.id) : undefined;
|
|
@@ -665,54 +693,58 @@ export async function resolveThemePartsWith(
|
|
|
665
693
|
|
|
666
694
|
const popups = (
|
|
667
695
|
await Promise.all(
|
|
668
|
-
popupMatches.map(
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
696
|
+
popupMatches.map((match) =>
|
|
697
|
+
orSkip(match.id, async () => {
|
|
698
|
+
const stored = byId.get(match.id);
|
|
699
|
+
if (!stored) return null;
|
|
700
|
+
const { html, css } = await renderOne(
|
|
701
|
+
stored,
|
|
702
|
+
extraTemplates.get(stored.id),
|
|
703
|
+
extraBound.get(stored.id) ?? {},
|
|
704
|
+
);
|
|
705
|
+
const popup: RenderedPopup = {
|
|
706
|
+
id: stored.id,
|
|
707
|
+
title: stored.title,
|
|
708
|
+
html: wrapPopupMarkup(stored.id, html, stored.triggers, options.labels),
|
|
709
|
+
css,
|
|
710
|
+
triggers: stored.triggers,
|
|
711
|
+
};
|
|
712
|
+
return popup;
|
|
713
|
+
}),
|
|
714
|
+
),
|
|
685
715
|
)
|
|
686
716
|
).filter((popup): popup is RenderedPopup => popup !== null);
|
|
687
717
|
|
|
688
718
|
const floats = (
|
|
689
719
|
await Promise.all(
|
|
690
|
-
floatMatches.map(
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
720
|
+
floatMatches.map((match) =>
|
|
721
|
+
orSkip(match.id, async () => {
|
|
722
|
+
const stored = byId.get(match.id);
|
|
723
|
+
if (!stored) return null;
|
|
724
|
+
const { html, css } = await renderOne(
|
|
725
|
+
stored,
|
|
726
|
+
extraTemplates.get(stored.id),
|
|
727
|
+
extraBound.get(stored.id) ?? {},
|
|
728
|
+
);
|
|
729
|
+
const layout = readLayout(stored.layout);
|
|
730
|
+
const surface = !layout || !floatHasOwnSurface(layout, design);
|
|
731
|
+
const item: RenderedFloat = {
|
|
732
|
+
id: stored.id,
|
|
733
|
+
title: stored.title,
|
|
734
|
+
html: wrapFloatMarkup(
|
|
735
|
+
stored.id,
|
|
736
|
+
stored.title,
|
|
737
|
+
html,
|
|
738
|
+
stored.float,
|
|
739
|
+
surface,
|
|
740
|
+
options.labels?.close,
|
|
741
|
+
),
|
|
742
|
+
css,
|
|
743
|
+
settings: stored.float,
|
|
744
|
+
};
|
|
745
|
+
return item;
|
|
746
|
+
}),
|
|
747
|
+
),
|
|
716
748
|
)
|
|
717
749
|
).filter((item): item is RenderedFloat => item !== null);
|
|
718
750
|
|
|
@@ -737,6 +769,7 @@ export async function resolveThemePartsWith(
|
|
|
737
769
|
needsTabsRuntime: rendered.some((page) => page.needsTabsRuntime),
|
|
738
770
|
needsMenuRuntime: rendered.some((page) => page.needsMenuRuntime),
|
|
739
771
|
needsFormsRuntime: rendered.some((page) => page.needsFormsRuntime),
|
|
772
|
+
...partRuntimes(rendered),
|
|
740
773
|
notFound: pastLastPage,
|
|
741
774
|
...(design.direction ? { dir: design.direction } : {}),
|
|
742
775
|
...(archiveInfo?.title ? { archiveTitle: archiveInfo.title } : {}),
|
package/src/astro/visitor.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { geoTrust } from "../core/geo-trust.ts";
|
|
1
2
|
import {
|
|
2
3
|
AUDIENCE_SEGMENT,
|
|
3
4
|
abCookieName,
|
|
@@ -113,16 +114,34 @@ const COUNTRY = /^[A-Z]{2}$/;
|
|
|
113
114
|
/** XX is Cloudflare's "unknown" and T1 is Tor: neither is a country (W-514: host facts too). */
|
|
114
115
|
const isCountry = (code: string) => COUNTRY.test(code) && code !== "XX" && code !== "T1";
|
|
115
116
|
|
|
117
|
+
const onCloudflare = () => globalThis.navigator?.userAgent === "Cloudflare-Workers";
|
|
118
|
+
const onVercel = () =>
|
|
119
|
+
Boolean(
|
|
120
|
+
(globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env?.[
|
|
121
|
+
"VERCEL"
|
|
122
|
+
],
|
|
123
|
+
);
|
|
124
|
+
|
|
116
125
|
/**
|
|
117
|
-
* The visitor's country from the edge: Cloudflare's header or
|
|
126
|
+
* The visitor's country from the edge: `request.cf`, Cloudflare's header or Vercel's header.
|
|
118
127
|
* Not `Astro.locals.runtime.cf`: since Astro 6 the Cloudflare adapter throws when it is read.
|
|
128
|
+
* S-10 (W-999): a header counts only where that edge sets it (on Cloudflare Workers, on
|
|
129
|
+
* Vercel); elsewhere the visitor could send it. `emvb({ trustGeoHeaders: true })` trusts them
|
|
130
|
+
* behind a proxy that strips client copies; `false` trusts none.
|
|
119
131
|
*/
|
|
120
|
-
function countryOf(
|
|
132
|
+
export function countryOf(
|
|
133
|
+
astro: VisitorAstro,
|
|
134
|
+
trust: boolean | undefined = geoTrust(),
|
|
135
|
+
): string | undefined {
|
|
121
136
|
const headers = astro.request?.headers;
|
|
122
137
|
const cf = (astro.request as { cf?: { country?: unknown } } | undefined)?.cf;
|
|
123
138
|
const candidates = [
|
|
124
|
-
|
|
125
|
-
|
|
139
|
+
trust === true || (trust === undefined && onCloudflare())
|
|
140
|
+
? headers?.get("cf-ipcountry")
|
|
141
|
+
: undefined,
|
|
142
|
+
trust === true || (trust === undefined && onVercel())
|
|
143
|
+
? headers?.get("x-vercel-ip-country")
|
|
144
|
+
: undefined,
|
|
126
145
|
cf?.country,
|
|
127
146
|
];
|
|
128
147
|
for (const value of candidates) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* W-1025: the opt-in scripts of the 1.2 widgets, each mounted only when `names` asks for it.
|
|
4
|
+
* `EmVBPage` renders this for its page; for theme parts, pass `theme.runtimes`.
|
|
5
|
+
*/
|
|
6
|
+
import EmVBAlertsRuntime from "./EmVBAlertsRuntime.astro";
|
|
7
|
+
import EmVBCarouselRuntime from "./EmVBCarouselRuntime.astro";
|
|
8
|
+
import EmVBLightboxRuntime from "./EmVBLightboxRuntime.astro";
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
names?: readonly string[] | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const names = new Set(Astro.props.names ?? []);
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
{names.has("alerts") && <EmVBAlertsRuntime />}
|
|
18
|
+
{names.has("lightbox") && <EmVBLightboxRuntime />}
|
|
19
|
+
{names.has("carousel") && <EmVBCarouselRuntime />}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1025: the opt-in Alert runtime. It shows each dismissible Alert's close button (hidden until
|
|
3
|
+
* this script runs, so a page without it never shows a button that does nothing) and hides the
|
|
4
|
+
* Alert when it is pressed, moving focus to the next thing a keyboard reaches.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const FOCUSABLE =
|
|
8
|
+
"a[href], button:not([disabled]), input:not([disabled]):not([type=hidden]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex='-1']), summary, iframe";
|
|
9
|
+
|
|
10
|
+
/** The first focusable element after `from` in the document, else the last one before it. */
|
|
11
|
+
function neighbour(from: Element): HTMLElement | null {
|
|
12
|
+
const all = [...from.ownerDocument.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(
|
|
13
|
+
(el) => !from.contains(el) && !el.closest("[hidden]"),
|
|
14
|
+
);
|
|
15
|
+
const after = all.find(
|
|
16
|
+
(el) => from.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING,
|
|
17
|
+
);
|
|
18
|
+
return (
|
|
19
|
+
after ??
|
|
20
|
+
all.findLast((el) => from.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_PRECEDING) ??
|
|
21
|
+
null
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function initAlerts(root: ParentNode = document): void {
|
|
26
|
+
for (const button of root.querySelectorAll<HTMLButtonElement>("[data-emvb-alert-dismiss]")) {
|
|
27
|
+
if (button.dataset["emvbReady"] !== undefined) continue;
|
|
28
|
+
button.dataset["emvbReady"] = "";
|
|
29
|
+
button.addEventListener("click", () => {
|
|
30
|
+
const alert = button.closest<HTMLElement>(".emvb-alert");
|
|
31
|
+
if (!alert) return;
|
|
32
|
+
const next = neighbour(alert);
|
|
33
|
+
alert.hidden = true;
|
|
34
|
+
next?.focus();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1018: the opt-in Carousel controls. A carousel works without this script (a snapping track
|
|
3
|
+
* you swipe, scroll or move with the arrow keys); this adds what it asked for:
|
|
4
|
+
* - Previous and Next buttons (`data-emvb-arrows`), disabled at the ends unless it loops;
|
|
5
|
+
* - one button per slide (`data-emvb-dots`), the current one marked aria-current;
|
|
6
|
+
* - autoplay (`data-emvb-autoplay="<seconds>"`) with a visible Pause/Play button placed before
|
|
7
|
+
* the slides, paused while the carousel is hovered or holds focus, and starting paused when
|
|
8
|
+
* the visitor prefers reduced motion (WCAG 2.2.2).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type Carousel = {
|
|
12
|
+
root: HTMLElement;
|
|
13
|
+
track: HTMLElement;
|
|
14
|
+
slides: HTMLElement[];
|
|
15
|
+
loop: boolean;
|
|
16
|
+
index: number;
|
|
17
|
+
prev?: HTMLButtonElement;
|
|
18
|
+
next?: HTMLButtonElement;
|
|
19
|
+
dots: HTMLButtonElement[];
|
|
20
|
+
timer?: ReturnType<typeof setInterval>;
|
|
21
|
+
/** The visitor pressed Pause (or reduced motion started it paused). */
|
|
22
|
+
stopped: boolean;
|
|
23
|
+
/** Hovered or focused: paused for now. */
|
|
24
|
+
held: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function button(label: string, text: string, className = ""): HTMLButtonElement {
|
|
28
|
+
const el = document.createElement("button");
|
|
29
|
+
el.type = "button";
|
|
30
|
+
el.setAttribute("aria-label", label);
|
|
31
|
+
el.textContent = text;
|
|
32
|
+
if (className) el.className = className;
|
|
33
|
+
return el;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How many slides show at once, from the layout (1 when it can't be measured). */
|
|
37
|
+
function perView(c: Carousel): number {
|
|
38
|
+
const first = c.slides[0];
|
|
39
|
+
if (!first || first.offsetWidth === 0) return 1;
|
|
40
|
+
return Math.max(1, Math.round(c.track.clientWidth / first.offsetWidth));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The last slide a view can start at. */
|
|
44
|
+
const lastStart = (c: Carousel) => Math.max(0, c.slides.length - perView(c));
|
|
45
|
+
|
|
46
|
+
function update(c: Carousel): void {
|
|
47
|
+
const last = lastStart(c);
|
|
48
|
+
if (c.prev) c.prev.disabled = !c.loop && c.index <= 0;
|
|
49
|
+
if (c.next) c.next.disabled = !c.loop && c.index >= last;
|
|
50
|
+
c.dots.forEach((dot, i) => {
|
|
51
|
+
if (i === c.index) dot.setAttribute("aria-current", "true");
|
|
52
|
+
else dot.removeAttribute("aria-current");
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Shows slide `index` (wrapped when looping or playing, else kept in range). */
|
|
57
|
+
function go(c: Carousel, index: number, wrap = c.loop): void {
|
|
58
|
+
const last = lastStart(c);
|
|
59
|
+
c.index = wrap
|
|
60
|
+
? index > last
|
|
61
|
+
? 0
|
|
62
|
+
: index < 0
|
|
63
|
+
? last
|
|
64
|
+
: index
|
|
65
|
+
: Math.min(Math.max(index, 0), last);
|
|
66
|
+
const target = c.slides[c.index];
|
|
67
|
+
const origin = c.slides[0];
|
|
68
|
+
if (target && origin) c.track.scrollTo({ left: target.offsetLeft - origin.offsetLeft });
|
|
69
|
+
update(c);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const present = (els: (HTMLElement | undefined)[]): HTMLElement[] =>
|
|
73
|
+
els.filter((el): el is HTMLElement => el !== undefined);
|
|
74
|
+
|
|
75
|
+
function setup(root: HTMLElement): void {
|
|
76
|
+
const track = root.querySelector<HTMLElement>(":scope > .emvb-carousel-track");
|
|
77
|
+
if (!track) return;
|
|
78
|
+
const c: Carousel = {
|
|
79
|
+
root,
|
|
80
|
+
track,
|
|
81
|
+
slides: [...track.children].filter((el): el is HTMLElement => el instanceof HTMLElement),
|
|
82
|
+
loop: root.hasAttribute("data-emvb-loop"),
|
|
83
|
+
index: 0,
|
|
84
|
+
dots: [],
|
|
85
|
+
stopped: false,
|
|
86
|
+
held: false,
|
|
87
|
+
};
|
|
88
|
+
if (c.slides.length < 2) return;
|
|
89
|
+
const controls = document.createElement("div");
|
|
90
|
+
controls.className = "emvb-carousel-controls";
|
|
91
|
+
if (root.hasAttribute("data-emvb-arrows")) {
|
|
92
|
+
c.prev = button("Previous slide", "‹");
|
|
93
|
+
c.next = button("Next slide", "›");
|
|
94
|
+
c.prev.addEventListener("click", () => go(c, c.index - 1));
|
|
95
|
+
c.next.addEventListener("click", () => go(c, c.index + 1));
|
|
96
|
+
}
|
|
97
|
+
if (root.hasAttribute("data-emvb-dots")) {
|
|
98
|
+
const dots = document.createElement("div");
|
|
99
|
+
dots.className = "emvb-carousel-dots";
|
|
100
|
+
for (let i = 0; i <= lastStart(c); i++) {
|
|
101
|
+
const dot = button(`Go to slide ${i + 1}`, "");
|
|
102
|
+
dot.addEventListener("click", () => go(c, i, false));
|
|
103
|
+
c.dots.push(dot);
|
|
104
|
+
dots.append(dot);
|
|
105
|
+
}
|
|
106
|
+
controls.append(...present([c.prev, dots, c.next]));
|
|
107
|
+
} else {
|
|
108
|
+
controls.append(...present([c.prev, c.next]));
|
|
109
|
+
}
|
|
110
|
+
if (controls.childElementCount > 0) track.after(controls);
|
|
111
|
+
const seconds = Number(root.dataset["emvbAutoplay"]);
|
|
112
|
+
if (Number.isFinite(seconds) && seconds > 0) autoplay(c, seconds);
|
|
113
|
+
// Swipes and scrolls move the current slide too.
|
|
114
|
+
let frame = 0;
|
|
115
|
+
track.addEventListener("scroll", () => {
|
|
116
|
+
cancelAnimationFrame(frame);
|
|
117
|
+
frame = requestAnimationFrame(() => {
|
|
118
|
+
const first = c.slides[0];
|
|
119
|
+
const step = first?.offsetWidth ?? 0;
|
|
120
|
+
if (step <= 0) return;
|
|
121
|
+
c.index = Math.min(Math.round(track.scrollLeft / step), lastStart(c));
|
|
122
|
+
update(c);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
update(c);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function autoplay(c: Carousel, seconds: number): void {
|
|
129
|
+
const reduced = globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
|
|
130
|
+
c.stopped = reduced;
|
|
131
|
+
const toggle = button("", "", "emvb-carousel-play");
|
|
132
|
+
const label = () => {
|
|
133
|
+
toggle.textContent = c.stopped ? "Play" : "Pause";
|
|
134
|
+
toggle.setAttribute("aria-label", c.stopped ? "Play slides" : "Pause slides");
|
|
135
|
+
};
|
|
136
|
+
const tick = () => {
|
|
137
|
+
if (!c.stopped && !c.held) go(c, c.index + 1, true);
|
|
138
|
+
};
|
|
139
|
+
toggle.addEventListener("click", () => {
|
|
140
|
+
c.stopped = !c.stopped;
|
|
141
|
+
label();
|
|
142
|
+
});
|
|
143
|
+
label();
|
|
144
|
+
// The rotation control comes before the slides, so keyboard users reach it first.
|
|
145
|
+
const bar = document.createElement("div");
|
|
146
|
+
bar.className = "emvb-carousel-controls emvb-carousel-autoplay";
|
|
147
|
+
bar.append(toggle);
|
|
148
|
+
c.track.before(bar);
|
|
149
|
+
const hold = (held: boolean) => () => {
|
|
150
|
+
c.held = held;
|
|
151
|
+
};
|
|
152
|
+
c.root.addEventListener("pointerenter", hold(true));
|
|
153
|
+
c.root.addEventListener("pointerleave", hold(false));
|
|
154
|
+
c.root.addEventListener("focusin", hold(true));
|
|
155
|
+
c.root.addEventListener("focusout", (event) => {
|
|
156
|
+
if (!c.root.contains(event.relatedTarget as Node | null)) c.held = false;
|
|
157
|
+
});
|
|
158
|
+
c.timer = setInterval(tick, seconds * 1000);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function initCarousels(root: ParentNode = document): void {
|
|
162
|
+
for (const el of root.querySelectorAll<HTMLElement>("section.emvb-carousel")) {
|
|
163
|
+
if (el.dataset["emvbCarouselReady"] !== undefined) continue;
|
|
164
|
+
el.dataset["emvbCarouselReady"] = "";
|
|
165
|
+
setup(el);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as EmVBWidgetRuntimes } from "./EmVBWidgetRuntimes.astro";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* W-1019: the opt-in Gallery lightbox. In each gallery marked `data-emvb-lightbox` every picture
|
|
3
|
+
* becomes a button that opens it large in one native modal <dialog>: Previous and Next (also the
|
|
4
|
+
* arrow keys), a counter that screen readers hear, Close and Escape. Focus goes back to the
|
|
5
|
+
* picture's button when the dialog closes. Without this script the gallery is plain pictures.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
type Viewer = {
|
|
9
|
+
dialog: HTMLDialogElement;
|
|
10
|
+
image: HTMLImageElement;
|
|
11
|
+
caption: HTMLElement;
|
|
12
|
+
count: HTMLElement;
|
|
13
|
+
prev: HTMLButtonElement;
|
|
14
|
+
next: HTMLButtonElement;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type Show = { images: HTMLImageElement[]; index: number; opener: HTMLElement };
|
|
18
|
+
|
|
19
|
+
let viewer: Viewer | undefined;
|
|
20
|
+
let showing: Show | undefined;
|
|
21
|
+
|
|
22
|
+
const captionOf = (image: HTMLImageElement) =>
|
|
23
|
+
image.closest("figure")?.querySelector("figcaption")?.textContent?.trim() ?? "";
|
|
24
|
+
|
|
25
|
+
/** What a picture's button is called: its alt text, else its caption, else its place. */
|
|
26
|
+
export function pictureName(image: HTMLImageElement, index: number, total: number): string {
|
|
27
|
+
return image.alt.trim() || captionOf(image) || `Picture ${index + 1} of ${total}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function button(label: string, text: string): HTMLButtonElement {
|
|
31
|
+
const el = document.createElement("button");
|
|
32
|
+
el.type = "button";
|
|
33
|
+
el.setAttribute("aria-label", label);
|
|
34
|
+
el.textContent = text;
|
|
35
|
+
return el;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function build(doc: Document): Viewer {
|
|
39
|
+
const dialog = doc.createElement("dialog");
|
|
40
|
+
dialog.className = "emvb-lightbox";
|
|
41
|
+
dialog.setAttribute("aria-label", "Picture viewer");
|
|
42
|
+
const figure = doc.createElement("figure");
|
|
43
|
+
figure.className = "emvb-lightbox-figure";
|
|
44
|
+
const image = doc.createElement("img");
|
|
45
|
+
const bar = doc.createElement("div");
|
|
46
|
+
bar.className = "emvb-lightbox-bar";
|
|
47
|
+
const prev = button("Previous picture", "‹");
|
|
48
|
+
const next = button("Next picture", "›");
|
|
49
|
+
const close = button("Close", "×");
|
|
50
|
+
const caption = doc.createElement("figcaption");
|
|
51
|
+
caption.className = "emvb-lightbox-caption";
|
|
52
|
+
const count = doc.createElement("span");
|
|
53
|
+
count.setAttribute("aria-live", "polite");
|
|
54
|
+
caption.append(count);
|
|
55
|
+
bar.append(prev, caption, next, close);
|
|
56
|
+
figure.append(image, bar);
|
|
57
|
+
dialog.append(figure);
|
|
58
|
+
doc.body.append(dialog);
|
|
59
|
+
prev.addEventListener("click", () => step(-1));
|
|
60
|
+
next.addEventListener("click", () => step(1));
|
|
61
|
+
close.addEventListener("click", () => dialog.close());
|
|
62
|
+
dialog.addEventListener("keydown", (event) => {
|
|
63
|
+
if (event.key === "ArrowLeft") step(document.dir === "rtl" ? 1 : -1);
|
|
64
|
+
else if (event.key === "ArrowRight") step(document.dir === "rtl" ? -1 : 1);
|
|
65
|
+
});
|
|
66
|
+
dialog.addEventListener("close", () => {
|
|
67
|
+
showing?.opener.focus();
|
|
68
|
+
showing = undefined;
|
|
69
|
+
});
|
|
70
|
+
return { dialog, image, caption, count, prev, next };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function render(): void {
|
|
74
|
+
if (!viewer || !showing) return;
|
|
75
|
+
const { images, index } = showing;
|
|
76
|
+
const source = images[index];
|
|
77
|
+
if (!source) return;
|
|
78
|
+
viewer.image.src = source.currentSrc || source.src;
|
|
79
|
+
viewer.image.alt = source.alt;
|
|
80
|
+
const words = captionOf(source);
|
|
81
|
+
viewer.count.textContent = `${words ? `${words} · ` : ""}${index + 1} of ${images.length}`;
|
|
82
|
+
const single = images.length < 2;
|
|
83
|
+
viewer.prev.hidden = single;
|
|
84
|
+
viewer.next.hidden = single;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function step(by: number): void {
|
|
88
|
+
if (!showing) return;
|
|
89
|
+
const total = showing.images.length;
|
|
90
|
+
showing.index = (showing.index + by + total) % total;
|
|
91
|
+
render();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function open(images: HTMLImageElement[], index: number, opener: HTMLElement): void {
|
|
95
|
+
viewer ??= build(opener.ownerDocument);
|
|
96
|
+
showing = { images, index, opener };
|
|
97
|
+
render();
|
|
98
|
+
if (!viewer.dialog.open) viewer.dialog.showModal();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function initLightbox(root: ParentNode = document): void {
|
|
102
|
+
for (const list of root.querySelectorAll<HTMLElement>("[data-emvb-lightbox]")) {
|
|
103
|
+
if (list.dataset["emvbLightboxReady"] !== undefined) continue;
|
|
104
|
+
list.dataset["emvbLightboxReady"] = "";
|
|
105
|
+
const images = [
|
|
106
|
+
...list.querySelectorAll<HTMLImageElement>(":scope > .emvb-gallery-image > figure > img"),
|
|
107
|
+
];
|
|
108
|
+
images.forEach((image, index) => {
|
|
109
|
+
const opener = button(`View larger: ${pictureName(image, index, images.length)}`, "");
|
|
110
|
+
opener.className = "emvb-gallery-open";
|
|
111
|
+
image.replaceWith(opener);
|
|
112
|
+
opener.append(image);
|
|
113
|
+
opener.addEventListener("click", () => open(images, index, opener));
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Test seam: forget the shared viewer between tests. */
|
|
119
|
+
export function resetLightboxForTest(): void {
|
|
120
|
+
viewer?.dialog.remove();
|
|
121
|
+
viewer = undefined;
|
|
122
|
+
showing = undefined;
|
|
123
|
+
}
|
package/src/constants.ts
CHANGED