@agence-zef/astro 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +4 -2
- package/components/Button.astro +42 -0
- package/components/CookieBanner.astro +183 -0
- package/components/Form.astro +291 -0
- package/components/Image.astro +57 -0
- package/components/Popups.astro +210 -0
- package/components/RichText.astro +11 -0
- package/components/Video.astro +38 -0
- package/components/ZefSections.astro +68 -0
- package/components/routes/page.astro +24 -0
- package/components/routes/preview-section.astro +37 -0
- package/components/routes/preview-thumbnail.astro +72 -0
- package/components/routes/preview.astro +77 -0
- package/components/routes/robots.ts +16 -0
- package/components/virtual.d.ts +12 -0
- package/dist/env.d.ts +5 -0
- package/dist/env.js +17 -0
- package/dist/helpers.d.ts +54 -0
- package/dist/helpers.js +101 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +145 -0
- package/dist/overlay.d.ts +110 -0
- package/dist/overlay.js +561 -0
- package/package.json +50 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agence ZEF
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agence-zef/astro
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Intégration Astro du CMS de l'agence ZEF : route des pages, aperçu de l'éditeur, composants `Button`, `Image`, `Video`, `RichText`, `Form`, `Popups`, `CookieBanner`.
|
|
4
|
+
|
|
5
|
+
Paquet du CMS de l'agence ZEF (dépôt privé). Licence MIT.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Bouton du CMS (cahier des charges § 5.4), sans style imposé : lien, ancre, fichier,
|
|
4
|
+
* email, téléphone ou popup. La variante et la taille sont exposées en `data-*` pour le CSS du site.
|
|
5
|
+
*/
|
|
6
|
+
import type { Button } from "@agence-zef/schema";
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
button: Button | null;
|
|
10
|
+
class?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const { button, class: className } = Astro.props;
|
|
14
|
+
const data = button
|
|
15
|
+
? {
|
|
16
|
+
"data-variant": button.variant ?? undefined,
|
|
17
|
+
"data-size": button.size ?? undefined,
|
|
18
|
+
"data-icon": button.icon ?? undefined,
|
|
19
|
+
"data-icon-position": button.icon ? button.iconPosition : undefined,
|
|
20
|
+
}
|
|
21
|
+
: {};
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
{
|
|
25
|
+
button &&
|
|
26
|
+
(button.href ? (
|
|
27
|
+
<a
|
|
28
|
+
href={button.href}
|
|
29
|
+
target={button.target ?? undefined}
|
|
30
|
+
rel={button.target ? "noopener" : undefined}
|
|
31
|
+
download={button.download || undefined}
|
|
32
|
+
class={className}
|
|
33
|
+
{...data}
|
|
34
|
+
>
|
|
35
|
+
{button.label}
|
|
36
|
+
</a>
|
|
37
|
+
) : (
|
|
38
|
+
<button type="button" class={className} data-zef-popup={button.popup ?? undefined} {...data}>
|
|
39
|
+
{button.label}
|
|
40
|
+
</button>
|
|
41
|
+
))
|
|
42
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Bandeau cookies (C-28, CNIL), sans style imposé : réglages lus à l'exécution au CMS.
|
|
4
|
+
* « Tout refuser » au même niveau que « Tout accepter », choix par catégorie, gardé 6 mois
|
|
5
|
+
* (redemandé si les catégories changent). Google Analytics 4 (`gaId`) n'est chargé
|
|
6
|
+
* qu'après accord sur « Mesure d'audience ». Un élément `data-zef-cookies` (lien « Gérer
|
|
7
|
+
* les cookies » du footer) rouvre le bandeau. Le choix est publié dans `window.zefConsent`
|
|
8
|
+
* et l'événement `zef:consent`. À placer une fois dans le gabarit, avant `</body>`.
|
|
9
|
+
*/
|
|
10
|
+
interface Props {
|
|
11
|
+
/** Identifiant de mesure GA4 (`G-XXXXXXX`), chargé seulement après accord. */
|
|
12
|
+
gaId?: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const { gaId } = Astro.props;
|
|
16
|
+
const cms = (process.env.ZEF_URL ?? "").replace(/\/$/, "");
|
|
17
|
+
const locale = Astro.currentLocale ?? "";
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
{cms && <div data-zef-cookie-banner={`${cms}/api/public/cookies`} data-locale={locale} data-ga={gaId ?? undefined} hidden />}
|
|
21
|
+
|
|
22
|
+
<script>
|
|
23
|
+
type Texts = {
|
|
24
|
+
title: string;
|
|
25
|
+
message: string;
|
|
26
|
+
accept: string;
|
|
27
|
+
reject: string;
|
|
28
|
+
customize: string;
|
|
29
|
+
policyLabel: string;
|
|
30
|
+
policyUrl: string | null;
|
|
31
|
+
};
|
|
32
|
+
type Banner = {
|
|
33
|
+
layout: string;
|
|
34
|
+
consentDays: number;
|
|
35
|
+
version: string;
|
|
36
|
+
texts: Record<string, Texts>;
|
|
37
|
+
categories: {
|
|
38
|
+
key: string;
|
|
39
|
+
required: boolean;
|
|
40
|
+
services: string;
|
|
41
|
+
label: Record<string, string>;
|
|
42
|
+
description: Record<string, string>;
|
|
43
|
+
}[];
|
|
44
|
+
};
|
|
45
|
+
type Consent = { version: string; at: number; choices: Record<string, boolean> };
|
|
46
|
+
|
|
47
|
+
const KEY = "zef-consent";
|
|
48
|
+
const read = (): Consent | null => {
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(localStorage.getItem(KEY) ?? "null");
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function loadGa(id: string) {
|
|
57
|
+
if (document.querySelector("script[data-zef-ga]")) return;
|
|
58
|
+
const s = document.createElement("script");
|
|
59
|
+
s.async = true;
|
|
60
|
+
s.dataset.zefGa = "";
|
|
61
|
+
s.src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(id)}`;
|
|
62
|
+
document.head.append(s);
|
|
63
|
+
const w = window as unknown as { dataLayer: unknown[]; gtag: (...args: unknown[]) => void };
|
|
64
|
+
w.dataLayer = w.dataLayer || [];
|
|
65
|
+
w.gtag = (...args: unknown[]) => {
|
|
66
|
+
w.dataLayer.push(args);
|
|
67
|
+
};
|
|
68
|
+
w.gtag("js", new Date());
|
|
69
|
+
w.gtag("config", id, { anonymize_ip: true });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function apply(consent: Consent, gaId: string | undefined) {
|
|
73
|
+
(window as unknown as { zefConsent: Consent }).zefConsent = consent;
|
|
74
|
+
document.dispatchEvent(new CustomEvent("zef:consent", { detail: consent.choices }));
|
|
75
|
+
if (gaId && consent.choices.mesure) loadGa(gaId);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function el<K extends keyof HTMLElementTagNameMap>(tag: K, className: string, text?: string) {
|
|
79
|
+
const node = document.createElement(tag);
|
|
80
|
+
node.className = className;
|
|
81
|
+
if (text !== undefined) node.textContent = text;
|
|
82
|
+
return node;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function start(root: HTMLElement) {
|
|
86
|
+
const gaId = root.dataset.ga;
|
|
87
|
+
let banner: Banner | null = null;
|
|
88
|
+
try {
|
|
89
|
+
banner = (await (await fetch(root.dataset.zefCookieBanner ?? "")).json()).banner;
|
|
90
|
+
} catch {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!banner) return;
|
|
94
|
+
const b = banner;
|
|
95
|
+
const locale = root.dataset.locale || document.documentElement.lang || "fr";
|
|
96
|
+
const texts = b.texts[locale] ?? Object.values(b.texts)[0];
|
|
97
|
+
const pick = (v: Record<string, string>) => v[locale] || Object.values(v)[0] || "";
|
|
98
|
+
|
|
99
|
+
const save = (choices: Record<string, boolean>) => {
|
|
100
|
+
const consent = { version: b.version, at: Date.now(), choices };
|
|
101
|
+
try {
|
|
102
|
+
localStorage.setItem(KEY, JSON.stringify(consent));
|
|
103
|
+
} catch {
|
|
104
|
+
// Navigation privée : le choix vaut pour la page.
|
|
105
|
+
}
|
|
106
|
+
apply(consent, gaId);
|
|
107
|
+
document.querySelector(".zef-cookies")?.remove();
|
|
108
|
+
};
|
|
109
|
+
const all = (value: boolean) =>
|
|
110
|
+
Object.fromEntries(b.categories.map((c) => [c.key, c.required || value]));
|
|
111
|
+
|
|
112
|
+
const show = (customize = false) => {
|
|
113
|
+
document.querySelector(".zef-cookies")?.remove();
|
|
114
|
+
const current = read();
|
|
115
|
+
const box = el("section", "zef-cookies");
|
|
116
|
+
box.dataset.layout = b.layout;
|
|
117
|
+
box.setAttribute("role", "dialog");
|
|
118
|
+
box.setAttribute("aria-label", texts.title);
|
|
119
|
+
box.append(el("h2", "zef-cookies__title", texts.title), el("p", "zef-cookies__message", texts.message));
|
|
120
|
+
if (texts.policyUrl) {
|
|
121
|
+
const a = el("a", "zef-cookies__policy", texts.policyLabel);
|
|
122
|
+
a.href = texts.policyUrl;
|
|
123
|
+
box.append(a);
|
|
124
|
+
}
|
|
125
|
+
const boxes = new Map<string, HTMLInputElement>();
|
|
126
|
+
if (customize) {
|
|
127
|
+
const list = el("ul", "zef-cookies__categories");
|
|
128
|
+
for (const c of b.categories) {
|
|
129
|
+
const item = el("li", "zef-cookies__category");
|
|
130
|
+
const label = el("label", "zef-cookies__check");
|
|
131
|
+
const input = document.createElement("input");
|
|
132
|
+
input.type = "checkbox";
|
|
133
|
+
input.checked = c.required || Boolean(current?.choices[c.key]);
|
|
134
|
+
input.disabled = c.required;
|
|
135
|
+
boxes.set(c.key, input);
|
|
136
|
+
label.append(input, el("span", "", pick(c.label)));
|
|
137
|
+
item.append(label, el("p", "zef-cookies__description", pick(c.description)));
|
|
138
|
+
if (c.services) item.append(el("p", "zef-cookies__services", c.services));
|
|
139
|
+
list.append(item);
|
|
140
|
+
}
|
|
141
|
+
box.append(list);
|
|
142
|
+
}
|
|
143
|
+
const actions = el("div", "zef-cookies__actions");
|
|
144
|
+
const button = (text: string, kind: string, onClick: () => void) => {
|
|
145
|
+
const btn = el("button", "zef-cookies__button", text);
|
|
146
|
+
btn.type = "button";
|
|
147
|
+
btn.dataset.kind = kind;
|
|
148
|
+
btn.addEventListener("click", onClick);
|
|
149
|
+
actions.append(btn);
|
|
150
|
+
};
|
|
151
|
+
button(texts.reject, "reject", () => save(all(false)));
|
|
152
|
+
button(texts.accept, "accept", () => save(all(true)));
|
|
153
|
+
if (customize) {
|
|
154
|
+
button(locale === "en" ? "Save my choices" : "Enregistrer mes choix", "save", () =>
|
|
155
|
+
save(Object.fromEntries([...boxes].map(([k, input]) => [k, input.checked]))),
|
|
156
|
+
);
|
|
157
|
+
} else {
|
|
158
|
+
button(texts.customize, "customize", () => show(true));
|
|
159
|
+
}
|
|
160
|
+
box.append(actions);
|
|
161
|
+
document.body.append(box);
|
|
162
|
+
box.querySelector<HTMLButtonElement>("button")?.focus();
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// « Gérer les cookies » (footer) rouvre le bandeau avec les choix en cours.
|
|
166
|
+
document.addEventListener("click", (e) => {
|
|
167
|
+
if (!(e.target as Element | null)?.closest("[data-zef-cookies]")) return;
|
|
168
|
+
e.preventDefault();
|
|
169
|
+
show(true);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const current = read();
|
|
173
|
+
const valid =
|
|
174
|
+
current &&
|
|
175
|
+
current.version === b.version &&
|
|
176
|
+
Date.now() - current.at < b.consentDays * 86_400_000;
|
|
177
|
+
if (valid) apply(current, gaId);
|
|
178
|
+
else show();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const root = document.querySelector<HTMLElement>("[data-zef-cookie-banner]");
|
|
182
|
+
if (root) start(root);
|
|
183
|
+
</script>
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Formulaire du CMS (C-09), sans style imposé : rendu depuis une référence `forms`
|
|
4
|
+
* (`f.reference({ to: ["forms"] })`). Logique conditionnelle, anti-spam Turnstile et
|
|
5
|
+
* champ piège, envoi en JavaScript au CMS avec message de confirmation ou redirection ;
|
|
6
|
+
* sans JavaScript, envoi HTML classique. Classes `zef-form__*` et attributs `data-*`
|
|
7
|
+
* pour le CSS du site.
|
|
8
|
+
*/
|
|
9
|
+
import type { PublicForm, Reference } from "@agence-zef/schema";
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
/** Référence renvoyée par l'API (`{ type: "form", form }`) ou formulaire seul. */
|
|
13
|
+
form: Reference | PublicForm | null | undefined;
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const { form: input, class: className } = Astro.props;
|
|
18
|
+
const form: PublicForm | null =
|
|
19
|
+
input && "fields" in input ? input : ((input as Reference | null)?.form ?? null);
|
|
20
|
+
const locale = Astro.currentLocale ?? "";
|
|
21
|
+
const id = form ? `zef-form-${form.id.slice(0, 8)}` : "";
|
|
22
|
+
const COUNTRIES = [
|
|
23
|
+
"France",
|
|
24
|
+
"Belgique",
|
|
25
|
+
"Suisse",
|
|
26
|
+
"Luxembourg",
|
|
27
|
+
"Canada",
|
|
28
|
+
"Allemagne",
|
|
29
|
+
"Espagne",
|
|
30
|
+
"Italie",
|
|
31
|
+
"Royaume-Uni",
|
|
32
|
+
"Pays-Bas",
|
|
33
|
+
];
|
|
34
|
+
const inputType = {
|
|
35
|
+
email: "email",
|
|
36
|
+
phone: "tel",
|
|
37
|
+
number: "number",
|
|
38
|
+
date: "date",
|
|
39
|
+
text: "text",
|
|
40
|
+
} as const;
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
form && (
|
|
45
|
+
<form
|
|
46
|
+
id={id}
|
|
47
|
+
class:list={["zef-form", className]}
|
|
48
|
+
action={form.action}
|
|
49
|
+
method="post"
|
|
50
|
+
enctype={form.fields.some((f) => f.type === "file") ? "multipart/form-data" : "application/x-www-form-urlencoded"}
|
|
51
|
+
data-zef-form={form.id}
|
|
52
|
+
novalidate
|
|
53
|
+
>
|
|
54
|
+
<input type="hidden" name="zef_locale" value={locale} />
|
|
55
|
+
<input type="hidden" name="zef_page" value={Astro.url.pathname} />
|
|
56
|
+
{form.honeypot && (
|
|
57
|
+
<div class="zef-form__trap" aria-hidden="true" style="position:absolute;left:-10000px;width:1px;height:1px;overflow:hidden">
|
|
58
|
+
<label>
|
|
59
|
+
Site web
|
|
60
|
+
<input type="text" name={form.honeypot} tabindex="-1" autocomplete="off" />
|
|
61
|
+
</label>
|
|
62
|
+
</div>
|
|
63
|
+
)}
|
|
64
|
+
{form.fields.map((f) => {
|
|
65
|
+
const fieldId = `${id}-${f.name}`;
|
|
66
|
+
const attrs = {
|
|
67
|
+
"data-field": f.name,
|
|
68
|
+
"data-type": f.type,
|
|
69
|
+
"data-width": f.width,
|
|
70
|
+
"data-conditions": f.conditions ? JSON.stringify(f.conditions) : undefined,
|
|
71
|
+
};
|
|
72
|
+
if (f.type === "message") {
|
|
73
|
+
return (
|
|
74
|
+
<p class="zef-form__message" {...attrs}>
|
|
75
|
+
{f.label}
|
|
76
|
+
</p>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (f.type === "consent") {
|
|
80
|
+
return (
|
|
81
|
+
<div class="zef-form__field" {...attrs}>
|
|
82
|
+
<label class="zef-form__check">
|
|
83
|
+
<input type="checkbox" name={f.name} value="true" required={f.required} />
|
|
84
|
+
<span>{f.label}</span>
|
|
85
|
+
</label>
|
|
86
|
+
<p class="zef-form__error" role="alert" hidden />
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return (
|
|
91
|
+
<div class="zef-form__field" {...attrs}>
|
|
92
|
+
{f.type === "checkboxes" || f.type === "radio" ? (
|
|
93
|
+
<fieldset class="zef-form__choices">
|
|
94
|
+
<legend class="zef-form__label">
|
|
95
|
+
{f.label}
|
|
96
|
+
{f.required && <span aria-hidden="true"> *</span>}
|
|
97
|
+
</legend>
|
|
98
|
+
{f.options.map((o) => (
|
|
99
|
+
<label class="zef-form__check">
|
|
100
|
+
<input
|
|
101
|
+
type={f.type === "radio" ? "radio" : "checkbox"}
|
|
102
|
+
name={f.name}
|
|
103
|
+
value={o.value}
|
|
104
|
+
checked={f.defaultValue === o.value}
|
|
105
|
+
/>
|
|
106
|
+
<span>{o.label}</span>
|
|
107
|
+
</label>
|
|
108
|
+
))}
|
|
109
|
+
</fieldset>
|
|
110
|
+
) : (
|
|
111
|
+
<>
|
|
112
|
+
<label class="zef-form__label" for={fieldId}>
|
|
113
|
+
{f.label}
|
|
114
|
+
{f.required && <span aria-hidden="true"> *</span>}
|
|
115
|
+
</label>
|
|
116
|
+
{f.type === "textarea" ? (
|
|
117
|
+
<textarea id={fieldId} name={f.name} rows="5" placeholder={f.placeholder ?? undefined} required={f.required}>
|
|
118
|
+
{f.defaultValue ?? ""}
|
|
119
|
+
</textarea>
|
|
120
|
+
) : f.type === "select" || f.type === "country" ? (
|
|
121
|
+
<select id={fieldId} name={f.name} required={f.required}>
|
|
122
|
+
<option value="">{f.placeholder ?? ""}</option>
|
|
123
|
+
{(f.type === "country" ? COUNTRIES.map((c) => ({ value: c, label: c })) : f.options).map((o) => (
|
|
124
|
+
<option value={o.value} selected={f.defaultValue === o.value}>
|
|
125
|
+
{o.label}
|
|
126
|
+
</option>
|
|
127
|
+
))}
|
|
128
|
+
</select>
|
|
129
|
+
) : f.type === "file" ? (
|
|
130
|
+
<input
|
|
131
|
+
id={fieldId}
|
|
132
|
+
type="file"
|
|
133
|
+
name={f.name}
|
|
134
|
+
multiple
|
|
135
|
+
required={f.required}
|
|
136
|
+
accept={f.accept.length ? f.accept.map((a) => (a === "jpg" ? ".jpg,.jpeg" : `.${a}`)).join(",") : undefined}
|
|
137
|
+
data-max-files="3"
|
|
138
|
+
/>
|
|
139
|
+
) : (
|
|
140
|
+
<input
|
|
141
|
+
id={fieldId}
|
|
142
|
+
type={inputType[f.type as keyof typeof inputType] ?? "text"}
|
|
143
|
+
name={f.name}
|
|
144
|
+
value={f.defaultValue ?? undefined}
|
|
145
|
+
placeholder={f.placeholder ?? undefined}
|
|
146
|
+
required={f.required}
|
|
147
|
+
/>
|
|
148
|
+
)}
|
|
149
|
+
</>
|
|
150
|
+
)}
|
|
151
|
+
<p class="zef-form__error" role="alert" hidden />
|
|
152
|
+
</div>
|
|
153
|
+
);
|
|
154
|
+
})}
|
|
155
|
+
{form.turnstileSiteKey && <div class="cf-turnstile zef-form__turnstile" data-sitekey={form.turnstileSiteKey} data-size="flexible" />}
|
|
156
|
+
<p class="zef-form__status" role="status" hidden />
|
|
157
|
+
<button type="submit" class="zef-form__submit">
|
|
158
|
+
{form.submitLabel}
|
|
159
|
+
</button>
|
|
160
|
+
<template data-zef-confirmation={form.confirmation.type} data-url={form.confirmation.type === "redirect" ? (form.confirmation.url ?? "") : undefined}>
|
|
161
|
+
{form.confirmation.type === "message" && <div class="zef-form__confirmation" set:html={form.confirmation.html} />}
|
|
162
|
+
</template>
|
|
163
|
+
</form>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
{form?.turnstileSiteKey && <script is:inline src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer />}
|
|
167
|
+
|
|
168
|
+
<script>
|
|
169
|
+
import { type FormConditions, isFieldVisible } from "@agence-zef/schema/forms";
|
|
170
|
+
|
|
171
|
+
/** Valeurs saisies, par nom de champ (liste pour les cases à cocher). */
|
|
172
|
+
function valuesOf(form: HTMLFormElement) {
|
|
173
|
+
const values: Record<string, string | string[]> = {};
|
|
174
|
+
for (const [key, value] of new FormData(form).entries()) {
|
|
175
|
+
if (typeof value !== "string") continue;
|
|
176
|
+
const prev = values[key];
|
|
177
|
+
values[key] = prev === undefined ? value : [...(Array.isArray(prev) ? prev : [prev]), value];
|
|
178
|
+
}
|
|
179
|
+
for (const box of form.querySelectorAll<HTMLElement>('[data-type="checkboxes"]')) {
|
|
180
|
+
const name = box.dataset.field ?? "";
|
|
181
|
+
if (!(name in values)) values[name] = [];
|
|
182
|
+
else if (!Array.isArray(values[name])) values[name] = [values[name] as string];
|
|
183
|
+
}
|
|
184
|
+
return values;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Affiche ou masque les champs selon leurs conditions ; un champ masqué n'est pas envoyé. */
|
|
188
|
+
function applyConditions(form: HTMLFormElement) {
|
|
189
|
+
const values = valuesOf(form);
|
|
190
|
+
for (const el of form.querySelectorAll<HTMLElement>("[data-conditions]")) {
|
|
191
|
+
const visible = isFieldVisible(JSON.parse(el.dataset.conditions ?? "null") as FormConditions, values);
|
|
192
|
+
el.hidden = !visible;
|
|
193
|
+
for (const control of el.querySelectorAll<HTMLInputElement>("input, select, textarea")) {
|
|
194
|
+
control.disabled = !visible;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for (const form of document.querySelectorAll<HTMLFormElement>("form[data-zef-form]")) {
|
|
200
|
+
applyConditions(form);
|
|
201
|
+
form.addEventListener("input", () => applyConditions(form));
|
|
202
|
+
form.addEventListener("change", () => applyConditions(form));
|
|
203
|
+
form.addEventListener("submit", async (event) => {
|
|
204
|
+
event.preventDefault();
|
|
205
|
+
const status = form.querySelector<HTMLElement>(".zef-form__status");
|
|
206
|
+
const submit = form.querySelector<HTMLButtonElement>(".zef-form__submit");
|
|
207
|
+
for (const error of form.querySelectorAll<HTMLElement>(".zef-form__error")) {
|
|
208
|
+
error.hidden = true;
|
|
209
|
+
error.textContent = "";
|
|
210
|
+
}
|
|
211
|
+
const values = valuesOf(form);
|
|
212
|
+
const meta = (key: string) => {
|
|
213
|
+
const value = values[key];
|
|
214
|
+
delete values[key];
|
|
215
|
+
return typeof value === "string" ? value : undefined;
|
|
216
|
+
};
|
|
217
|
+
const body = {
|
|
218
|
+
locale: meta("zef_locale") || document.documentElement.lang || undefined,
|
|
219
|
+
pageUrl: meta("zef_page") || location.pathname,
|
|
220
|
+
turnstileToken: meta("cf-turnstile-response"),
|
|
221
|
+
values,
|
|
222
|
+
};
|
|
223
|
+
// Fichiers vérifiés avant l'envoi (le CMS les revérifie) : 3 au plus, 10 Mo chacun.
|
|
224
|
+
for (const input of form.querySelectorAll<HTMLInputElement>('input[type="file"]:not(:disabled)')) {
|
|
225
|
+
const list = [...(input.files ?? [])];
|
|
226
|
+
const message =
|
|
227
|
+
list.length > 3
|
|
228
|
+
? "3 fichiers maximum."
|
|
229
|
+
: list.some((f) => f.size > 10 * 1024 * 1024)
|
|
230
|
+
? "10 Mo maximum par fichier."
|
|
231
|
+
: "";
|
|
232
|
+
if (message) {
|
|
233
|
+
const error = input.closest(".zef-form__field")?.querySelector<HTMLElement>(".zef-form__error");
|
|
234
|
+
if (error) {
|
|
235
|
+
error.textContent = message;
|
|
236
|
+
error.hidden = false;
|
|
237
|
+
}
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (submit) submit.disabled = true;
|
|
242
|
+
form.setAttribute("aria-busy", "true");
|
|
243
|
+
// Avec un champ fichier : envoi multipart (les champs masqués, désactivés, n'en font pas partie).
|
|
244
|
+
const multipart = form.querySelector('input[type="file"]:not(:disabled)') !== null;
|
|
245
|
+
try {
|
|
246
|
+
const res = await fetch(form.action, {
|
|
247
|
+
method: "POST",
|
|
248
|
+
headers: multipart ? { accept: "application/json" } : { "content-type": "application/json" },
|
|
249
|
+
body: multipart ? new FormData(form) : JSON.stringify(body),
|
|
250
|
+
});
|
|
251
|
+
const result = await res.json();
|
|
252
|
+
if (result.ok) {
|
|
253
|
+
if (result.confirmation.type === "redirect" && result.confirmation.url) {
|
|
254
|
+
location.assign(result.confirmation.url);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const template = form.querySelector<HTMLTemplateElement>("template[data-zef-confirmation]");
|
|
258
|
+
const done = document.createElement("div");
|
|
259
|
+
done.className = "zef-form__done";
|
|
260
|
+
done.setAttribute("role", "status");
|
|
261
|
+
done.append(template?.content.cloneNode(true) ?? "");
|
|
262
|
+
form.replaceWith(done);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
for (const [name, message] of Object.entries<string>(result.fields ?? {})) {
|
|
266
|
+
const error = form.querySelector<HTMLElement>(`[data-field="${CSS.escape(name)}"] .zef-form__error`);
|
|
267
|
+
if (error) {
|
|
268
|
+
error.textContent = message;
|
|
269
|
+
error.hidden = false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (status) {
|
|
273
|
+
status.textContent = result.message;
|
|
274
|
+
status.hidden = false;
|
|
275
|
+
}
|
|
276
|
+
// Un jeton Turnstile ne sert qu'une fois.
|
|
277
|
+
(window as { turnstile?: { reset: (el?: Element) => void } }).turnstile?.reset(
|
|
278
|
+
form.querySelector(".cf-turnstile") ?? undefined,
|
|
279
|
+
);
|
|
280
|
+
} catch {
|
|
281
|
+
if (status) {
|
|
282
|
+
status.textContent = "Envoi impossible. Vérifiez votre connexion et réessayez.";
|
|
283
|
+
status.hidden = false;
|
|
284
|
+
}
|
|
285
|
+
} finally {
|
|
286
|
+
if (submit) submit.disabled = false;
|
|
287
|
+
form.removeAttribute("aria-busy");
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
</script>
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { Image as AstroImage } from "astro:assets";
|
|
3
|
+
/**
|
|
4
|
+
* Image de la médiathèque, avec son texte alternatif. Sans style imposé.
|
|
5
|
+
* JPG, PNG, WebP et AVIF sont optimisés au build (format et poids, `srcset` si `sizes`
|
|
6
|
+
* est donné) ; SVG, GIF et les adresses hors médiathèque sont rendus tels quels.
|
|
7
|
+
*/
|
|
8
|
+
import type { Media } from "@agence-zef/schema";
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
media: Media | null;
|
|
12
|
+
class?: string;
|
|
13
|
+
/** `eager` pour l'image principale de la page. */
|
|
14
|
+
loading?: "lazy" | "eager";
|
|
15
|
+
/** Largeur affichée (`(min-width: 48rem) 33vw, 100vw`) : active le `srcset`. */
|
|
16
|
+
sizes?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { media, class: className, loading = "lazy", sizes } = Astro.props;
|
|
20
|
+
const OPTIMIZED = ["image/jpeg", "image/png", "image/webp", "image/avif"];
|
|
21
|
+
const optimize = Boolean(
|
|
22
|
+
media?.width && media.height && media.mime && OPTIMIZED.includes(media.mime),
|
|
23
|
+
);
|
|
24
|
+
const STEPS = [480, 800, 1200, 1600, 2400];
|
|
25
|
+
const widths =
|
|
26
|
+
sizes && media?.width
|
|
27
|
+
? [...STEPS.filter((w) => w < (media.width as number)), media.width]
|
|
28
|
+
: undefined;
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
{
|
|
32
|
+
media &&
|
|
33
|
+
(optimize ? (
|
|
34
|
+
<AstroImage
|
|
35
|
+
src={media.url}
|
|
36
|
+
alt={media.alt}
|
|
37
|
+
width={media.width as number}
|
|
38
|
+
height={media.height as number}
|
|
39
|
+
loading={loading}
|
|
40
|
+
decoding="async"
|
|
41
|
+
sizes={sizes}
|
|
42
|
+
widths={widths}
|
|
43
|
+
class={className}
|
|
44
|
+
/>
|
|
45
|
+
) : (
|
|
46
|
+
<img
|
|
47
|
+
src={media.url}
|
|
48
|
+
alt={media.alt}
|
|
49
|
+
width={media.width}
|
|
50
|
+
height={media.height}
|
|
51
|
+
loading={loading}
|
|
52
|
+
decoding="async"
|
|
53
|
+
sizes={sizes}
|
|
54
|
+
class={className}
|
|
55
|
+
/>
|
|
56
|
+
))
|
|
57
|
+
}
|