@brimsites/brimjs 0.0.0-stage → 1.0.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 +110 -0
- package/README.md +22 -2
- package/bin/brim.js +5 -0
- package/dist/cli.js +3802 -0
- package/editor-dist/editor.css +1 -0
- package/editor-dist/editor.js +91 -0
- package/package.json +71 -3
- package/skills/brim-convert/SKILL.md +256 -0
- package/skills/brim-hosting/SKILL.md +221 -0
- package/src/astro-shim.d.ts +5 -0
- package/src/auth/accounts.ts +341 -0
- package/src/auth/password.ts +34 -0
- package/src/auth/permissions.ts +44 -0
- package/src/blocks/accordion/Accordion.astro +48 -0
- package/src/blocks/accordion/accordion.css +63 -0
- package/src/blocks/analytics/Analytics.astro +45 -0
- package/src/blocks/analytics/ConsentBanner.astro +41 -0
- package/src/blocks/analytics/analytics.client.ts +125 -0
- package/src/blocks/analytics/analytics.css +51 -0
- package/src/blocks/analytics/consent.ts +51 -0
- package/src/blocks/button/Button.astro +64 -0
- package/src/blocks/button/button.css +100 -0
- package/src/blocks/carousel/Carousel.astro +140 -0
- package/src/blocks/carousel/carousel.client.ts +153 -0
- package/src/blocks/carousel/carousel.css +181 -0
- package/src/blocks/data/icons.ts +285 -0
- package/src/blocks/data/image-shapes.ts +91 -0
- package/src/blocks/data/social-icons.ts +41 -0
- package/src/blocks/data/social-networks.ts +256 -0
- package/src/blocks/divider/Divider.astro +27 -0
- package/src/blocks/divider/divider.css +17 -0
- package/src/blocks/form/Captcha.astro +27 -0
- package/src/blocks/form/Form.astro +250 -0
- package/src/blocks/form/FormField.astro +138 -0
- package/src/blocks/form/Newsletter.astro +80 -0
- package/src/blocks/form/form.client.ts +339 -0
- package/src/blocks/form/form.css +271 -0
- package/src/blocks/gallery/ImageGallery.astro +126 -0
- package/src/blocks/gallery/gallery.css +43 -0
- package/src/blocks/gallery/lightbox.client.ts +178 -0
- package/src/blocks/gallery/lightbox.css +134 -0
- package/src/blocks/icon/Icon.astro +41 -0
- package/src/blocks/icon/icon.css +16 -0
- package/src/blocks/image/Image.astro +158 -0
- package/src/blocks/image/image.css +42 -0
- package/src/blocks/images.ts +64 -0
- package/src/blocks/in-editor.client.ts +8 -0
- package/src/blocks/listing/Listing.astro +288 -0
- package/src/blocks/listing/ListingCards.astro +48 -0
- package/src/blocks/listing/listing.css +136 -0
- package/src/blocks/listing/query.ts +363 -0
- package/src/blocks/map/Map.astro +75 -0
- package/src/blocks/map/map.css +32 -0
- package/src/blocks/schemas/accordion.ts +118 -0
- package/src/blocks/schemas/button.ts +78 -0
- package/src/blocks/schemas/carousel.ts +133 -0
- package/src/blocks/schemas/divider.ts +29 -0
- package/src/blocks/schemas/form-field-types.ts +29 -0
- package/src/blocks/schemas/form.ts +350 -0
- package/src/blocks/schemas/icon.ts +24 -0
- package/src/blocks/schemas/image-gallery.ts +54 -0
- package/src/blocks/schemas/image.ts +87 -0
- package/src/blocks/schemas/index.ts +33 -0
- package/src/blocks/schemas/listing.ts +14 -0
- package/src/blocks/schemas/map.ts +81 -0
- package/src/blocks/schemas/newsletter.ts +324 -0
- package/src/blocks/schemas/social-media.ts +348 -0
- package/src/blocks/schemas/text.ts +151 -0
- package/src/blocks/schemas/video.ts +57 -0
- package/src/blocks/search/Search.astro +147 -0
- package/src/blocks/search/search.css +69 -0
- package/src/blocks/search/state.ts +28 -0
- package/src/blocks/settings.ts +94 -0
- package/src/blocks/social/SocialMedia.astro +102 -0
- package/src/blocks/social/social-links.ts +16 -0
- package/src/blocks/social/social.css +31 -0
- package/src/blocks/theme.ts +35 -0
- package/src/blocks/types.ts +52 -0
- package/src/blocks/video/Video.astro +90 -0
- package/src/blocks/video/video-source.ts +77 -0
- package/src/blocks/video/video.client.ts +43 -0
- package/src/blocks/video/video.css +29 -0
- package/src/check.ts +1119 -0
- package/src/cli.ts +493 -0
- package/src/components/Blocks.astro +68 -0
- package/src/components/Field.astro +61 -0
- package/src/components/Footer.astro +36 -0
- package/src/components/Link.astro +18 -0
- package/src/components/MobileMenu.astro +77 -0
- package/src/components/Navbar.astro +42 -0
- package/src/components/RichText.astro +32 -0
- package/src/components/Seo.astro +202 -0
- package/src/components/Setting.astro +46 -0
- package/src/components/Shared.astro +17 -0
- package/src/components/Text.astro +91 -0
- package/src/components/blocks.css +86 -0
- package/src/components/index.ts +27 -0
- package/src/components/mobile-menu.client.ts +21 -0
- package/src/components/mobile-menu.css +52 -0
- package/src/components/text.css +48 -0
- package/src/config.ts +413 -0
- package/src/content/block-layout.ts +63 -0
- package/src/content/block-values.ts +48 -0
- package/src/content/content-check.ts +174 -0
- package/src/content/content-types.ts +408 -0
- package/src/content/drafts.ts +115 -0
- package/src/content/feed.ts +237 -0
- package/src/content/field-types.ts +107 -0
- package/src/content/history.ts +140 -0
- package/src/content/ids.ts +50 -0
- package/src/content/image-size.ts +212 -0
- package/src/content/image-sizes.ts +126 -0
- package/src/content/item-fields.ts +110 -0
- package/src/content/item-head.ts +126 -0
- package/src/content/item-image.ts +26 -0
- package/src/content/item-path.ts +122 -0
- package/src/content/items.ts +580 -0
- package/src/content/language-scopes.ts +40 -0
- package/src/content/media.ts +271 -0
- package/src/content/menus.ts +51 -0
- package/src/content/page-templates.ts +52 -0
- package/src/content/redirects.ts +112 -0
- package/src/content/references.ts +165 -0
- package/src/content/rich-text.ts +63 -0
- package/src/content/search.ts +167 -0
- package/src/content/seed.ts +229 -0
- package/src/content/site-settings.ts +111 -0
- package/src/content/sitemap.ts +143 -0
- package/src/content/store.ts +225 -0
- package/src/content/tags.ts +67 -0
- package/src/content/text-default.ts +139 -0
- package/src/content/types.ts +137 -0
- package/src/content/validate.ts +253 -0
- package/src/content/validation-error.ts +2 -0
- package/src/db/cached.ts +140 -0
- package/src/db/database.ts +15 -0
- package/src/db/libsql.ts +29 -0
- package/src/db/schema.ts +189 -0
- package/src/db/url.ts +44 -0
- package/src/editor/client/BlockSettings.tsx +1063 -0
- package/src/editor/client/Chrome.tsx +284 -0
- package/src/editor/client/ContentBrowser.tsx +1299 -0
- package/src/editor/client/Editor.tsx +1756 -0
- package/src/editor/client/HistoryPanel.tsx +114 -0
- package/src/editor/client/ImagesWidget.tsx +113 -0
- package/src/editor/client/Inspector.tsx +127 -0
- package/src/editor/client/ItemForm.tsx +535 -0
- package/src/editor/client/LinkWidget.tsx +440 -0
- package/src/editor/client/ListingPanel.tsx +99 -0
- package/src/editor/client/Login.tsx +338 -0
- package/src/editor/client/Menu.tsx +64 -0
- package/src/editor/client/PageSettings.tsx +270 -0
- package/src/editor/client/RedirectsSettings.tsx +130 -0
- package/src/editor/client/Resize.tsx +204 -0
- package/src/editor/client/Sidebar.tsx +51 -0
- package/src/editor/client/SiteMap.tsx +568 -0
- package/src/editor/client/SiteSettings.tsx +409 -0
- package/src/editor/client/TextTools.tsx +391 -0
- package/src/editor/client/Toolbar.tsx +449 -0
- package/src/editor/client/VideoUpload.tsx +251 -0
- package/src/editor/client/api.ts +417 -0
- package/src/editor/client/brand.ts +20 -0
- package/src/editor/client/connection.ts +98 -0
- package/src/editor/client/context.ts +19 -0
- package/src/editor/client/css.d.ts +2 -0
- package/src/editor/client/editor.css +4656 -0
- package/src/editor/client/fields.tsx +812 -0
- package/src/editor/client/icons.tsx +174 -0
- package/src/editor/client/keys.ts +15 -0
- package/src/editor/client/kinds.ts +289 -0
- package/src/editor/client/languages.ts +32 -0
- package/src/editor/client/main.tsx +100 -0
- package/src/editor/client/responses.ts +26 -0
- package/src/editor/client/rich-text.ts +326 -0
- package/src/editor/client/site-tree.ts +239 -0
- package/src/editor/client/theme.ts +55 -0
- package/src/editor/client/tsconfig.json +18 -0
- package/src/editor/client/tus.ts +137 -0
- package/src/editor/client/use-rect.ts +50 -0
- package/src/editor/client/widgets.tsx +1218 -0
- package/src/forms/captcha.ts +83 -0
- package/src/forms/define.ts +166 -0
- package/src/forms/fields.ts +197 -0
- package/src/forms/file-types.ts +108 -0
- package/src/forms/files.ts +216 -0
- package/src/forms/form-page.ts +9 -0
- package/src/forms/handler.ts +622 -0
- package/src/forms/spec.ts +89 -0
- package/src/forms/store.ts +96 -0
- package/src/forms/work.ts +50 -0
- package/src/icons/library.ts +144 -0
- package/src/icons/load.ts +63 -0
- package/src/index.ts +99 -0
- package/src/init.ts +257 -0
- package/src/integration.ts +469 -0
- package/src/mail/mailer.ts +197 -0
- package/src/mail/notify.ts +272 -0
- package/src/mail/settings.ts +173 -0
- package/src/mail/templates.ts +191 -0
- package/src/mcp/page-nodes.ts +209 -0
- package/src/mcp/server.ts +111 -0
- package/src/mcp/tools.ts +637 -0
- package/src/runtime/TemplatePage.astro +39 -0
- package/src/runtime/TemplatePageStatic.astro +13 -0
- package/src/runtime/admin-route.ts +80 -0
- package/src/runtime/connect.ts +53 -0
- package/src/runtime/content.ts +188 -0
- package/src/runtime/context.ts +131 -0
- package/src/runtime/form-files.ts +18 -0
- package/src/runtime/forms-route.ts +33 -0
- package/src/runtime/forms.ts +28 -0
- package/src/runtime/icons.ts +5 -0
- package/src/runtime/media.ts +175 -0
- package/src/runtime/middleware.ts +186 -0
- package/src/runtime/nav.ts +13 -0
- package/src/runtime/query-reads.ts +49 -0
- package/src/runtime/query-store.ts +16 -0
- package/src/runtime/request-state.ts +69 -0
- package/src/runtime/robots.ts +29 -0
- package/src/runtime/rss-static.ts +16 -0
- package/src/runtime/rss.ts +29 -0
- package/src/runtime/server.ts +29 -0
- package/src/runtime/sitemap.ts +34 -0
- package/src/runtime/template-paths.ts +34 -0
- package/src/runtime/uploads-route.ts +13 -0
- package/src/seed-cli.ts +253 -0
- package/src/server/auth.ts +31 -0
- package/src/server/fetch-image.ts +219 -0
- package/src/server/handler.ts +2106 -0
- package/src/server/http.ts +162 -0
- package/src/server/locales.ts +73 -0
- package/src/server/node.ts +57 -0
- package/src/server/query-params.ts +249 -0
- package/src/server/rate-limit.ts +72 -0
- package/src/server/video-host.ts +162 -0
- package/src/starter.ts +70 -0
- package/src/version.ts +4 -0
- package/src/virtual.d.ts +65 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
---
|
|
2
|
+
import type { FormField } from "../../forms/fields.ts";
|
|
3
|
+
import { MAX_FILES, MAX_FILE_BYTES, acceptAttribute, fileTypes } from "../../forms/file-types.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One question of a Form: its label, its input and the line under it. A Form draws its fields with it, and a site
|
|
7
|
+
* that draws a form its own way places it wherever each field goes: `<Field field={field} />`.
|
|
8
|
+
*/
|
|
9
|
+
interface Props {
|
|
10
|
+
/** A field as a Form's function child gets it, with the `id` its input takes. */
|
|
11
|
+
field: FormField & { id: string };
|
|
12
|
+
class?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const { field, class: className } = Astro.props;
|
|
16
|
+
const helpId = field.help ? `${field.id}-help` : undefined;
|
|
17
|
+
// An icon is a picture when it is an address; otherwise it is shown as text (an emoji, a letter).
|
|
18
|
+
const picture = (icon: string): boolean => /^(\/(?!\/)|https?:\/\/|data:image\/)/i.test(icon);
|
|
19
|
+
// A field of several choices is a group of checkboxes under one name; its "required" is checked by the form's script.
|
|
20
|
+
const inputType = field.type === "checkboxes" ? "checkbox" : "radio";
|
|
21
|
+
const autocomplete =
|
|
22
|
+
field.type === "name" ? "name" : field.type === "email" ? "email" : field.type === "tel" ? "tel" : undefined;
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
<div
|
|
26
|
+
class:list={["brim-form__field", className]}
|
|
27
|
+
data-type={field.type}
|
|
28
|
+
data-width={field.width ?? "full"}
|
|
29
|
+
data-name={field.name}
|
|
30
|
+
data-show-when={field.showWhen ? JSON.stringify(field.showWhen) : undefined}
|
|
31
|
+
>
|
|
32
|
+
{field.type === "checkbox" ? (
|
|
33
|
+
<label class="brim-form__check">
|
|
34
|
+
<input type="checkbox" name={field.name} required={field.required} aria-describedby={helpId} />
|
|
35
|
+
<span>{field.label}</span>
|
|
36
|
+
</label>
|
|
37
|
+
) : (
|
|
38
|
+
<>
|
|
39
|
+
<label for={field.id} id={`${field.id}-label`}>
|
|
40
|
+
{field.label}
|
|
41
|
+
{field.required && <span aria-hidden="true"> *</span>}
|
|
42
|
+
</label>
|
|
43
|
+
{field.type === "textarea" && (
|
|
44
|
+
<textarea
|
|
45
|
+
id={field.id}
|
|
46
|
+
name={field.name}
|
|
47
|
+
rows="5"
|
|
48
|
+
placeholder={field.placeholder}
|
|
49
|
+
required={field.required}
|
|
50
|
+
aria-describedby={helpId}
|
|
51
|
+
/>
|
|
52
|
+
)}
|
|
53
|
+
{field.type === "select" && (
|
|
54
|
+
<select id={field.id} name={field.name} required={field.required} aria-describedby={helpId}>
|
|
55
|
+
<option value="">{field.placeholder ?? "Choose…"}</option>
|
|
56
|
+
{field.options?.map((option) => (
|
|
57
|
+
<option value={option}>{option}</option>
|
|
58
|
+
))}
|
|
59
|
+
</select>
|
|
60
|
+
)}
|
|
61
|
+
{(field.type === "radio" || field.type === "checkboxes") && (
|
|
62
|
+
<div
|
|
63
|
+
class="brim-form__choices"
|
|
64
|
+
role={field.type === "radio" ? "radiogroup" : "group"}
|
|
65
|
+
data-display={field.display ?? "list"}
|
|
66
|
+
data-required={field.type === "checkboxes" && field.required ? "" : undefined}
|
|
67
|
+
aria-labelledby={`${field.id}-label`}
|
|
68
|
+
aria-describedby={helpId}
|
|
69
|
+
>
|
|
70
|
+
{(field.choices ?? field.options?.map((label) => ({ label })) ?? []).map((choice) =>
|
|
71
|
+
field.display === "cards" ? (
|
|
72
|
+
<label class="brim-form__choice" data-image={choice.image ? "" : undefined}>
|
|
73
|
+
<input
|
|
74
|
+
type={inputType}
|
|
75
|
+
name={field.name}
|
|
76
|
+
value={choice.label}
|
|
77
|
+
required={field.type === "radio" && field.required}
|
|
78
|
+
/>
|
|
79
|
+
{choice.image && (
|
|
80
|
+
<span class="brim-form__choice-image">
|
|
81
|
+
<img src={choice.image} alt="" loading="lazy" decoding="async" />
|
|
82
|
+
</span>
|
|
83
|
+
)}
|
|
84
|
+
{choice.icon && (
|
|
85
|
+
<span class="brim-form__choice-icon" aria-hidden="true">
|
|
86
|
+
{picture(choice.icon) ? <img src={choice.icon} alt="" loading="lazy" /> : choice.icon}
|
|
87
|
+
</span>
|
|
88
|
+
)}
|
|
89
|
+
<span class="brim-form__choice-label">{choice.label}</span>
|
|
90
|
+
{choice.description && <span class="brim-form__choice-description">{choice.description}</span>}
|
|
91
|
+
</label>
|
|
92
|
+
) : (
|
|
93
|
+
<label class="brim-form__check">
|
|
94
|
+
<input
|
|
95
|
+
type={inputType}
|
|
96
|
+
name={field.name}
|
|
97
|
+
value={choice.label}
|
|
98
|
+
required={field.type === "radio" && field.required}
|
|
99
|
+
/>
|
|
100
|
+
<span>{choice.label}</span>
|
|
101
|
+
</label>
|
|
102
|
+
),
|
|
103
|
+
)}
|
|
104
|
+
</div>
|
|
105
|
+
)}
|
|
106
|
+
{field.type === "file" && (
|
|
107
|
+
// `accept` only guides the visitor's file picker; the server reads what each file really is.
|
|
108
|
+
<input
|
|
109
|
+
id={field.id}
|
|
110
|
+
name={field.name}
|
|
111
|
+
type="file"
|
|
112
|
+
accept={acceptAttribute(fileTypes(field.accept))}
|
|
113
|
+
multiple={field.multiple}
|
|
114
|
+
required={field.required}
|
|
115
|
+
data-max-bytes={MAX_FILE_BYTES}
|
|
116
|
+
data-max-files={field.multiple ? MAX_FILES : 1}
|
|
117
|
+
aria-describedby={helpId}
|
|
118
|
+
/>
|
|
119
|
+
)}
|
|
120
|
+
{!["textarea", "select", "radio", "checkboxes", "file"].includes(field.type) && (
|
|
121
|
+
<input
|
|
122
|
+
id={field.id}
|
|
123
|
+
name={field.name}
|
|
124
|
+
type={field.type === "name" ? "text" : field.type}
|
|
125
|
+
autocomplete={autocomplete}
|
|
126
|
+
placeholder={field.placeholder}
|
|
127
|
+
required={field.required}
|
|
128
|
+
aria-describedby={helpId}
|
|
129
|
+
/>
|
|
130
|
+
)}
|
|
131
|
+
</>
|
|
132
|
+
)}
|
|
133
|
+
{field.help && (
|
|
134
|
+
<p class="brim-form__help" id={helpId}>
|
|
135
|
+
{field.help}
|
|
136
|
+
</p>
|
|
137
|
+
)}
|
|
138
|
+
</div>
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
import "../button/button.css";
|
|
3
|
+
import "./form.css";
|
|
4
|
+
import Captcha from "./Captcha.astro";
|
|
5
|
+
import type { NodeScope } from "../../runtime/context.ts";
|
|
6
|
+
import { signForm } from "../../runtime/forms.ts";
|
|
7
|
+
import { newsletter } from "../schemas/newsletter.ts";
|
|
8
|
+
import { plainText } from "../theme.ts";
|
|
9
|
+
|
|
10
|
+
/** An e-mail sign-up. Addresses are kept with the site's content and shown in the editor. */
|
|
11
|
+
interface Props {
|
|
12
|
+
nodeId: string;
|
|
13
|
+
scope?: NodeScope;
|
|
14
|
+
title?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
disclaimer?: string;
|
|
17
|
+
formName?: string;
|
|
18
|
+
placeholder?: string;
|
|
19
|
+
buttonText?: string;
|
|
20
|
+
successMessage?: string;
|
|
21
|
+
errorMessage?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const { nodeId, scope = "site", ...source } = Astro.props;
|
|
25
|
+
const settings = Astro.locals.brim.settings(newsletter, nodeId, scope, source);
|
|
26
|
+
|
|
27
|
+
const signed = await signForm(
|
|
28
|
+
{
|
|
29
|
+
// The same sign-up appears on every page, so it is one form wherever it is filled in.
|
|
30
|
+
form: scope === "site" ? `#${nodeId}` : `${Astro.url.pathname}#${nodeId}`,
|
|
31
|
+
title: String(settings.formName || "Newsletter"),
|
|
32
|
+
fields: [{ name: "email", label: "Email", type: "email", required: true }],
|
|
33
|
+
consentRequired: false,
|
|
34
|
+
},
|
|
35
|
+
Astro.locals.brim.preview,
|
|
36
|
+
);
|
|
37
|
+
const description = plainText(settings.description);
|
|
38
|
+
const disclaimer = plainText(settings.disclaimer);
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
<brim-form
|
|
42
|
+
class="brim-form brim-newsletter"
|
|
43
|
+
data-spec={signed.spec}
|
|
44
|
+
data-signature={signed.signature}
|
|
45
|
+
data-error={settings.errorMessage}
|
|
46
|
+
{...Astro.locals.brim.marker("newsletter", nodeId, scope, settings)}
|
|
47
|
+
>
|
|
48
|
+
<form novalidate>
|
|
49
|
+
{settings.title && <h2 class="brim-form__title">{settings.title}</h2>}
|
|
50
|
+
{description && <p class="brim-form__info">{description}</p>}
|
|
51
|
+
<div class="brim-newsletter__row">
|
|
52
|
+
<input
|
|
53
|
+
type="email"
|
|
54
|
+
name="email"
|
|
55
|
+
autocomplete="email"
|
|
56
|
+
required
|
|
57
|
+
placeholder={String(settings.placeholder ?? "")}
|
|
58
|
+
aria-label="Email"
|
|
59
|
+
/>
|
|
60
|
+
<button type="submit" class="brim-btn brim-btn--block brim-btn--md">
|
|
61
|
+
{settings.buttonText}
|
|
62
|
+
</button>
|
|
63
|
+
</div>
|
|
64
|
+
<div class="brim-form__trap" aria-hidden="true">
|
|
65
|
+
<label>
|
|
66
|
+
Leave this field empty <input type="text" name="website" tabindex="-1" autocomplete="off" />
|
|
67
|
+
</label>
|
|
68
|
+
</div>
|
|
69
|
+
<Captcha />
|
|
70
|
+
{disclaimer && <p class="brim-form__disclaimer">{disclaimer}</p>}
|
|
71
|
+
<p class="brim-form__error" role="alert" hidden></p>
|
|
72
|
+
</form>
|
|
73
|
+
<p class="brim-form__success" role="status" hidden>
|
|
74
|
+
{settings.successMessage}
|
|
75
|
+
</p>
|
|
76
|
+
</brim-form>
|
|
77
|
+
|
|
78
|
+
<script>
|
|
79
|
+
import "./form.client.ts";
|
|
80
|
+
</script>
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sends a form to the site's server and shows the outcome in place.
|
|
3
|
+
* The token is asked for when the visitor first touches the form, so by the time they send it
|
|
4
|
+
* the server can tell a person from a script that posts straight away.
|
|
5
|
+
*/
|
|
6
|
+
const ENDPOINT = "/_brim/forms";
|
|
7
|
+
/** The hidden fields a challenge's widget fills (Turnstile, hCaptcha): sent as the challenge, never as an answer. */
|
|
8
|
+
const CAPTCHA_FIELDS = ["cf-turnstile-response", "h-captcha-response"];
|
|
9
|
+
|
|
10
|
+
/** The providers' own scripts, when the page loaded one. */
|
|
11
|
+
declare global {
|
|
12
|
+
interface Window {
|
|
13
|
+
turnstile?: { reset: (widget?: Element) => void };
|
|
14
|
+
hcaptcha?: { reset: () => void };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class BrimForm extends HTMLElement {
|
|
19
|
+
#token: Promise<{ token: string; work: number }> | undefined;
|
|
20
|
+
#step = 0;
|
|
21
|
+
|
|
22
|
+
connectedCallback(): void {
|
|
23
|
+
const form = this.querySelector("form");
|
|
24
|
+
if (!form) return;
|
|
25
|
+
this.#setUpConditions(form);
|
|
26
|
+
this.#setUpSteps(form);
|
|
27
|
+
form.addEventListener("focusin", () => this.#requestToken(), { once: true });
|
|
28
|
+
form.addEventListener("submit", (event) => {
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
void this.#submit(form);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A field with `data-show-when` (`{ field, equals }`) shows once that other field has one of those answers; hidden,
|
|
36
|
+
* its inputs are disabled, so they are neither checked nor sent. A field that appears takes the focus.
|
|
37
|
+
*/
|
|
38
|
+
#setUpConditions(form: HTMLFormElement): void {
|
|
39
|
+
const dependent = [...form.querySelectorAll<HTMLElement>("[data-show-when]")];
|
|
40
|
+
if (dependent.length === 0) return;
|
|
41
|
+
const update = (focus: boolean) => {
|
|
42
|
+
for (const field of dependent) {
|
|
43
|
+
let condition: { field?: string; equals?: string[] } = {};
|
|
44
|
+
try {
|
|
45
|
+
condition = JSON.parse(field.dataset.showWhen ?? "{}");
|
|
46
|
+
} catch {}
|
|
47
|
+
const answers = [...form.elements]
|
|
48
|
+
.filter(
|
|
49
|
+
(element): element is HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement =>
|
|
50
|
+
"name" in element && (element as HTMLInputElement).name === condition.field,
|
|
51
|
+
)
|
|
52
|
+
.filter(
|
|
53
|
+
(element) =>
|
|
54
|
+
!(element instanceof HTMLInputElement) || !/radio|checkbox/.test(element.type) || element.checked,
|
|
55
|
+
)
|
|
56
|
+
.map((element) => element.value.trim());
|
|
57
|
+
const shown = answers.some((answer) => condition.equals?.includes(answer));
|
|
58
|
+
const appeared = shown && field.hidden;
|
|
59
|
+
field.hidden = !shown;
|
|
60
|
+
for (const input of field.querySelectorAll<HTMLInputElement>("input, select, textarea"))
|
|
61
|
+
input.disabled = !shown;
|
|
62
|
+
if (appeared && focus)
|
|
63
|
+
field.querySelector<HTMLElement>("input:not([type=radio]):not([type=checkbox]), textarea, select")?.focus();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
form.addEventListener("change", () => update(true));
|
|
67
|
+
update(false);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A form asked in steps shows one `[data-step]` at a time. Continue and the tabs ahead check the steps they pass;
|
|
72
|
+
* the consent and the send button (`[data-last-step]`) wait for the last step. Without this script every step shows.
|
|
73
|
+
*/
|
|
74
|
+
#setUpSteps(form: HTMLFormElement): void {
|
|
75
|
+
const panels = [...form.querySelectorAll<HTMLElement>("[data-step]")];
|
|
76
|
+
if (panels.length < 2 || form.dataset.steps) return;
|
|
77
|
+
form.dataset.steps = String(panels.length);
|
|
78
|
+
this.setAttribute("data-stepping", "");
|
|
79
|
+
const go = (target: number) => {
|
|
80
|
+
for (let index = this.#step; index < target; index++) {
|
|
81
|
+
if (!this.#valid(panels[index])) return this.#show(form, panels, index);
|
|
82
|
+
}
|
|
83
|
+
this.#show(form, panels, target);
|
|
84
|
+
};
|
|
85
|
+
// The editor shows the step its sidebar has open, without checking the steps before it.
|
|
86
|
+
this.addEventListener("brim:show-step", (event) => this.#show(form, panels, Number((event as CustomEvent).detail)));
|
|
87
|
+
form.addEventListener("click", (event) => {
|
|
88
|
+
const button = (event.target as HTMLElement).closest<HTMLElement>(
|
|
89
|
+
"[data-go-step], [data-next-step], [data-back-step]",
|
|
90
|
+
);
|
|
91
|
+
if (!button || !form.contains(button)) return;
|
|
92
|
+
if (button.hasAttribute("data-next-step")) go(this.#step + 1);
|
|
93
|
+
else if (button.hasAttribute("data-back-step")) this.#show(form, panels, this.#step - 1);
|
|
94
|
+
else go(Number(button.dataset.goStep));
|
|
95
|
+
});
|
|
96
|
+
// With `data-auto-advance`, a step that asks one single choice moves on once it is answered.
|
|
97
|
+
form.addEventListener("change", (event) => {
|
|
98
|
+
const input = event.target as HTMLInputElement;
|
|
99
|
+
if (!this.hasAttribute("data-auto-advance") || input.type !== "radio") return;
|
|
100
|
+
const panel = panels[this.#step];
|
|
101
|
+
if (!panel?.contains(input) || this.#step >= panels.length - 1) return;
|
|
102
|
+
// Only when the step asks nothing else: a field that has just appeared waits for its answer.
|
|
103
|
+
if ([...panel.querySelectorAll<HTMLElement>(".brim-form__field")].filter((field) => !field.hidden).length !== 1)
|
|
104
|
+
return;
|
|
105
|
+
const step = this.#step;
|
|
106
|
+
// A moment to see the choice taken; a click elsewhere meanwhile wins.
|
|
107
|
+
window.setTimeout(() => {
|
|
108
|
+
if (this.#step === step) go(step + 1);
|
|
109
|
+
}, 350);
|
|
110
|
+
});
|
|
111
|
+
// Enter in a one-line field moves on instead of sending half the form.
|
|
112
|
+
form.addEventListener("keydown", (event) => {
|
|
113
|
+
const target = event.target as HTMLElement;
|
|
114
|
+
if (event.key === "Enter" && target.tagName === "INPUT" && this.#step < panels.length - 1) {
|
|
115
|
+
event.preventDefault();
|
|
116
|
+
go(this.#step + 1);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
this.#show(form, panels, 0);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
#valid(panel: HTMLElement | undefined): boolean {
|
|
123
|
+
requireGroups(panel);
|
|
124
|
+
return [...(panel?.querySelectorAll<HTMLInputElement>("input, select, textarea") ?? [])].every((input) =>
|
|
125
|
+
input.reportValidity(),
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#show(form: HTMLFormElement, panels: HTMLElement[], target: number): void {
|
|
130
|
+
const index = Math.min(panels.length - 1, Math.max(0, Number.isFinite(target) ? target : 0));
|
|
131
|
+
const last = index === panels.length - 1;
|
|
132
|
+
this.#step = index;
|
|
133
|
+
panels.forEach((panel, n) => {
|
|
134
|
+
panel.hidden = n !== index;
|
|
135
|
+
});
|
|
136
|
+
for (const tab of form.querySelectorAll<HTMLElement>("[data-go-step]")) {
|
|
137
|
+
const n = Number(tab.dataset.goStep);
|
|
138
|
+
tab.setAttribute("aria-selected", String(n === index));
|
|
139
|
+
tab.toggleAttribute("data-done", n < index);
|
|
140
|
+
}
|
|
141
|
+
for (const element of form.querySelectorAll<HTMLElement>("[data-last-step]")) element.hidden = !last;
|
|
142
|
+
for (const element of form.querySelectorAll<HTMLElement>("[data-next-step]")) element.hidden = last;
|
|
143
|
+
for (const element of form.querySelectorAll<HTMLElement>("[data-back-step]")) element.hidden = index === 0;
|
|
144
|
+
this.style.setProperty("--brim-form-progress", `${((index + 1) / panels.length) * 100}%`);
|
|
145
|
+
this.dataset.step = String(index);
|
|
146
|
+
// For the site's CSS, e.g. content: attr(data-step-number) " / " attr(data-steps).
|
|
147
|
+
this.dataset.stepNumber = String(index + 1);
|
|
148
|
+
this.dataset.steps = String(panels.length);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The challenge's answer, when the form has a challenge. */
|
|
152
|
+
#captcha(data: FormData): string | undefined {
|
|
153
|
+
const field = CAPTCHA_FIELDS.find((name) => data.has(name));
|
|
154
|
+
return field === undefined ? undefined : String(data.get(field) ?? "");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** A challenge's answer is good once: after a failed sending the widget asks again. */
|
|
158
|
+
#resetCaptcha(): void {
|
|
159
|
+
const widget = this.querySelector(".brim-form__captcha");
|
|
160
|
+
if (!widget) return;
|
|
161
|
+
if (widget.classList.contains("cf-turnstile")) window.turnstile?.reset(widget);
|
|
162
|
+
else window.hcaptcha?.reset();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Why a file input's files cannot be sent (too many, too large), or nothing. */
|
|
166
|
+
#fileProblem(input: HTMLInputElement): string | undefined {
|
|
167
|
+
const files = [...(input.files ?? [])];
|
|
168
|
+
const label = input.closest(".brim-form__field")?.querySelector("label")?.textContent?.replace("*", "").trim();
|
|
169
|
+
const maxFiles = Number(input.dataset.maxFiles) || 1;
|
|
170
|
+
const maxBytes = Number(input.dataset.maxBytes) || Infinity;
|
|
171
|
+
const name = label ? `"${label}"` : "This field";
|
|
172
|
+
if (files.length > maxFiles) return `${name} takes at most ${maxFiles} file${maxFiles === 1 ? "" : "s"}.`;
|
|
173
|
+
if (files.some((file) => file.size > maxBytes)) {
|
|
174
|
+
return `${name}: a file is larger than ${Math.floor(maxBytes / (1024 * 1024))} MB.`;
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** A token, and the work the server asks for it, done while the visitor fills the form in. */
|
|
180
|
+
#requestToken(): Promise<{ token: string; work: number }> {
|
|
181
|
+
this.#token ??= fetch(`${ENDPOINT}/token`)
|
|
182
|
+
.then((response) => response.json() as Promise<{ token?: string; work?: number }>)
|
|
183
|
+
.then(async ({ token = "", work: bits = 0 }) => {
|
|
184
|
+
// `data-working` while the browser works, for the site's CSS (it takes seconds while the site is under attack).
|
|
185
|
+
this.toggleAttribute("data-working", true);
|
|
186
|
+
try {
|
|
187
|
+
return { token, work: await solve(token, bits) };
|
|
188
|
+
} finally {
|
|
189
|
+
this.removeAttribute("data-working");
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return this.#token;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async #submit(form: HTMLFormElement): Promise<void> {
|
|
196
|
+
const error = this.querySelector<HTMLElement>(".brim-form__error");
|
|
197
|
+
const button = form.querySelector<HTMLButtonElement>('button[type="submit"]');
|
|
198
|
+
if (form.dataset.steps) {
|
|
199
|
+
// A step left wrong (its fields are hidden now) is shown with what is missing.
|
|
200
|
+
const panels = [...form.querySelectorAll<HTMLElement>("[data-step]")];
|
|
201
|
+
const wrong = panels.findIndex((panel) => panel.querySelector(":invalid") !== null);
|
|
202
|
+
if (wrong !== -1) {
|
|
203
|
+
this.#show(form, panels, wrong);
|
|
204
|
+
this.#valid(panels[wrong]);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
requireGroups(form);
|
|
209
|
+
if (!form.reportValidity()) return;
|
|
210
|
+
|
|
211
|
+
const data = new FormData(form);
|
|
212
|
+
const values: Record<string, string | boolean | string[]> = {};
|
|
213
|
+
for (const element of form.elements) {
|
|
214
|
+
if (!(
|
|
215
|
+
element instanceof HTMLInputElement ||
|
|
216
|
+
element instanceof HTMLTextAreaElement ||
|
|
217
|
+
element instanceof HTMLSelectElement
|
|
218
|
+
))
|
|
219
|
+
continue;
|
|
220
|
+
if (!element.name || element.name === "website" || element.name === "consent" || element.disabled) continue;
|
|
221
|
+
if (CAPTCHA_FIELDS.includes(element.name)) continue;
|
|
222
|
+
// Files travel as files, beside the answers.
|
|
223
|
+
if (element instanceof HTMLInputElement && element.type === "file") continue;
|
|
224
|
+
// The checkboxes of a field of several choices send the ones ticked, as a list.
|
|
225
|
+
if (element.closest('.brim-form__field[data-type="checkboxes"]'))
|
|
226
|
+
values[element.name] = data.getAll(element.name).map(String);
|
|
227
|
+
else if (element instanceof HTMLInputElement && element.type === "checkbox")
|
|
228
|
+
values[element.name] = element.checked;
|
|
229
|
+
else values[element.name] = String(data.get(element.name) ?? "");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const fileInputs = [...form.querySelectorAll<HTMLInputElement>('input[type="file"]')].filter(
|
|
233
|
+
(input) => input.name && (input.files?.length ?? 0) > 0,
|
|
234
|
+
);
|
|
235
|
+
// Said here before anything is sent; the server checks the same, and what each file really is.
|
|
236
|
+
const refused = fileInputs.map((input) => this.#fileProblem(input)).find(Boolean);
|
|
237
|
+
if (refused) {
|
|
238
|
+
if (error) {
|
|
239
|
+
error.textContent = refused;
|
|
240
|
+
error.hidden = false;
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (button) button.disabled = true;
|
|
246
|
+
if (error) error.hidden = true;
|
|
247
|
+
try {
|
|
248
|
+
const { token, work } = await this.#requestToken();
|
|
249
|
+
const message = {
|
|
250
|
+
spec: this.dataset.spec,
|
|
251
|
+
signature: this.dataset.signature,
|
|
252
|
+
trap: data.get("website"),
|
|
253
|
+
consent: data.get("consent") === "on",
|
|
254
|
+
...(this.#captcha(data) !== undefined && { captcha: this.#captcha(data) }),
|
|
255
|
+
};
|
|
256
|
+
let request: RequestInit;
|
|
257
|
+
if (fileInputs.length === 0) {
|
|
258
|
+
request = {
|
|
259
|
+
method: "POST",
|
|
260
|
+
headers: { "content-type": "application/json" },
|
|
261
|
+
body: JSON.stringify({ ...message, token, work, values }),
|
|
262
|
+
};
|
|
263
|
+
} else {
|
|
264
|
+
// A message with files is sent as a multipart form; its token and work go in headers, which the server checks
|
|
265
|
+
// before it reads the files.
|
|
266
|
+
const body = new FormData();
|
|
267
|
+
body.set("spec", message.spec ?? "");
|
|
268
|
+
body.set("signature", message.signature ?? "");
|
|
269
|
+
body.set("trap", String(message.trap ?? ""));
|
|
270
|
+
body.set("consent", String(message.consent));
|
|
271
|
+
if (message.captcha !== undefined) body.set("captcha", message.captcha);
|
|
272
|
+
body.set("values", JSON.stringify(values));
|
|
273
|
+
for (const input of fileInputs) for (const file of input.files ?? []) body.append(input.name, file);
|
|
274
|
+
request = { method: "POST", headers: { "x-brim-form-token": token, "x-brim-form-work": String(work) }, body };
|
|
275
|
+
}
|
|
276
|
+
const response = await fetch(`${ENDPOINT}/submit`, request);
|
|
277
|
+
if (!response.ok) {
|
|
278
|
+
const body = (await response.json().catch(() => ({}))) as { error?: string };
|
|
279
|
+
throw new Error(this.dataset.error || body.error || "Something went wrong. Please try again.");
|
|
280
|
+
}
|
|
281
|
+
form.hidden = true;
|
|
282
|
+
const success = this.querySelector<HTMLElement>(".brim-form__success");
|
|
283
|
+
if (success) success.hidden = false;
|
|
284
|
+
} catch (failure) {
|
|
285
|
+
// A token is good for one attempt; the next one gets a fresh token, and a fresh challenge.
|
|
286
|
+
this.#token = undefined;
|
|
287
|
+
this.#resetCaptcha();
|
|
288
|
+
if (error) {
|
|
289
|
+
error.textContent = failure instanceof Error ? failure.message : "Something went wrong. Please try again.";
|
|
290
|
+
error.hidden = false;
|
|
291
|
+
}
|
|
292
|
+
} finally {
|
|
293
|
+
if (button) button.disabled = false;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* A required field of several choices needs one ticked: the browser has no such rule for a group of checkboxes, so
|
|
300
|
+
* its first box carries the message while none is.
|
|
301
|
+
*/
|
|
302
|
+
function requireGroups(root: ParentNode | undefined): void {
|
|
303
|
+
for (const group of root?.querySelectorAll<HTMLElement>(".brim-form__choices[data-required]") ?? []) {
|
|
304
|
+
const boxes = [...group.querySelectorAll<HTMLInputElement>('input[type="checkbox"]')];
|
|
305
|
+
const none = !boxes.some((box) => box.checked);
|
|
306
|
+
boxes.forEach((box, index) => box.setCustomValidity(none && index === 0 ? "Please choose at least one." : ""));
|
|
307
|
+
if (group.dataset.watched) continue;
|
|
308
|
+
group.dataset.watched = "";
|
|
309
|
+
group.addEventListener("change", () => requireGroups(group.parentElement ?? undefined));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* The number that, after the token, gives a SHA-256 starting with `bits` zero bits (forms/work.ts checks it). Hashes
|
|
315
|
+
* are asked for in batches: the browser computes them off the page's thread, so typing stays smooth meanwhile.
|
|
316
|
+
*/
|
|
317
|
+
async function solve(token: string, bits: number): Promise<number> {
|
|
318
|
+
const encoder = new TextEncoder();
|
|
319
|
+
const BATCH = 256;
|
|
320
|
+
for (let start = 0; start < 2 ** 32; start += BATCH) {
|
|
321
|
+
const hashes = await Promise.all(
|
|
322
|
+
Array.from({ length: BATCH }, (_, i) => crypto.subtle.digest("SHA-256", encoder.encode(`${token}.${start + i}`))),
|
|
323
|
+
);
|
|
324
|
+
const found = hashes.findIndex((hash) => leadingZeroBits(new Uint8Array(hash)) >= bits);
|
|
325
|
+
if (found !== -1) return start + found;
|
|
326
|
+
}
|
|
327
|
+
return 0;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function leadingZeroBits(bytes: Uint8Array): number {
|
|
331
|
+
let bits = 0;
|
|
332
|
+
for (const byte of bytes) {
|
|
333
|
+
if (byte !== 0) return bits + Math.clz32(byte) - 24;
|
|
334
|
+
bits += 8;
|
|
335
|
+
}
|
|
336
|
+
return bits;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (!customElements.get("brim-form")) customElements.define("brim-form", BrimForm);
|