@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,1063 @@
|
|
|
1
|
+
import { type ReactNode, useState } from "react";
|
|
2
|
+
import { VideoUpload } from "./VideoUpload.tsx";
|
|
3
|
+
import type { BlockDefinition, SchemaProperty } from "../../blocks/types.ts";
|
|
4
|
+
import { isEmbeddedVideo } from "../../blocks/video/video-source.ts";
|
|
5
|
+
import { DEFAULT_FILE_TYPES, FILE_TYPES, MAX_FILES, MAX_FILE_BYTES, fileSize } from "../../forms/file-types.ts";
|
|
6
|
+
import { FIELD_TYPES, STEP } from "../../forms/fields.ts";
|
|
7
|
+
import { useEditor } from "./context.ts";
|
|
8
|
+
import { CheckboxWidget, SelectWidget, TextWidget } from "./fields.tsx";
|
|
9
|
+
import { AlignCenterIcon, AlignLeftIcon, AlignRightIcon } from "./icons.tsx";
|
|
10
|
+
import type { FormValue } from "./kinds.ts";
|
|
11
|
+
import { LinkWidget } from "./LinkWidget.tsx";
|
|
12
|
+
import { ImagesWidget, type ListedImage } from "./ImagesWidget.tsx";
|
|
13
|
+
import { Fieldset } from "./Sidebar.tsx";
|
|
14
|
+
import {
|
|
15
|
+
BreakpointNumberWidget,
|
|
16
|
+
CardList,
|
|
17
|
+
ColorSelectWidget,
|
|
18
|
+
FitWidget,
|
|
19
|
+
FocusWidget,
|
|
20
|
+
IconPickerWidget,
|
|
21
|
+
ImageUploadWidget,
|
|
22
|
+
LocationSearch,
|
|
23
|
+
RangeWidget,
|
|
24
|
+
ShapeWidget,
|
|
25
|
+
SizeWidget,
|
|
26
|
+
SocialNetworksWidget,
|
|
27
|
+
} from "./widgets.tsx";
|
|
28
|
+
|
|
29
|
+
/** The named button looks, in the order BrimCMS offered them. */
|
|
30
|
+
const BUTTON_VARIANTS = [
|
|
31
|
+
{ value: "light-1", label: "Light 1" },
|
|
32
|
+
{ value: "light-2", label: "Light 2" },
|
|
33
|
+
{ value: "light-3", label: "Light 3" },
|
|
34
|
+
{ value: "accent", label: "Accent" },
|
|
35
|
+
{ value: "block", label: "Default" },
|
|
36
|
+
{ value: "dark-1", label: "Dark 1" },
|
|
37
|
+
{ value: "dark-2", label: "Dark 2" },
|
|
38
|
+
{ value: "ghost", label: "Outline" },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
/** Used when a schema names no choices for an alignment, as BrimCMS's widget did. */
|
|
42
|
+
const DEFAULT_ALIGNMENTS = [
|
|
43
|
+
{ value: "left", label: "Left" },
|
|
44
|
+
{ value: "center", label: "Center" },
|
|
45
|
+
{ value: "right", label: "Right" },
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const ALIGN_ICONS: Record<string, () => ReactNode> = {
|
|
49
|
+
left: AlignLeftIcon,
|
|
50
|
+
center: AlignCenterIcon,
|
|
51
|
+
right: AlignRightIcon,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Widgets this inspector can draw. A setting with any other widget is listed as not editable yet. */
|
|
55
|
+
const SUPPORTED_WIDGETS = new Set([
|
|
56
|
+
"checkbox",
|
|
57
|
+
"boolean",
|
|
58
|
+
"select",
|
|
59
|
+
"size",
|
|
60
|
+
"button-variant",
|
|
61
|
+
"icon-picker",
|
|
62
|
+
"alignment",
|
|
63
|
+
"range",
|
|
64
|
+
"number",
|
|
65
|
+
"integer",
|
|
66
|
+
"icon-grid-breakpoint-number",
|
|
67
|
+
"carousel-slides-per-view",
|
|
68
|
+
"color-select",
|
|
69
|
+
"url",
|
|
70
|
+
"textarea",
|
|
71
|
+
"richtext",
|
|
72
|
+
"text",
|
|
73
|
+
"string",
|
|
74
|
+
"image",
|
|
75
|
+
"image-focus",
|
|
76
|
+
"image-shape",
|
|
77
|
+
"carousel-images",
|
|
78
|
+
"accordion-items",
|
|
79
|
+
"image-fit",
|
|
80
|
+
"video",
|
|
81
|
+
"social-network-order",
|
|
82
|
+
"map-points",
|
|
83
|
+
"form-fields",
|
|
84
|
+
"form-richtext",
|
|
85
|
+
"form-email-field",
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
const FIELD_TYPE_LABELS: Record<string, string> = {
|
|
89
|
+
text: "Short text",
|
|
90
|
+
name: "Name",
|
|
91
|
+
email: "Email",
|
|
92
|
+
tel: "Phone",
|
|
93
|
+
url: "Web address",
|
|
94
|
+
number: "Number",
|
|
95
|
+
date: "Date",
|
|
96
|
+
textarea: "Long text",
|
|
97
|
+
select: "Dropdown",
|
|
98
|
+
radio: "Single choice",
|
|
99
|
+
checkboxes: "Several choices",
|
|
100
|
+
checkbox: "Checkbox",
|
|
101
|
+
file: "File upload",
|
|
102
|
+
};
|
|
103
|
+
const FIELD_TYPE_OPTIONS = [
|
|
104
|
+
...FIELD_TYPES.map((type) => ({ value: type, label: FIELD_TYPE_LABELS[type] ?? type })),
|
|
105
|
+
{ value: STEP, label: "New step (starts a page of the form)" },
|
|
106
|
+
];
|
|
107
|
+
const FIELD_WIDTH_OPTIONS = [
|
|
108
|
+
{ value: "full", label: "Whole row" },
|
|
109
|
+
{ value: "half", label: "Half, next to another half" },
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
const FIELD_DISPLAY_OPTIONS = [
|
|
113
|
+
{ value: "list", label: "A list" },
|
|
114
|
+
{ value: "cards", label: "Cards to click" },
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
/** The words of a choice, kept as text or as `{ label, image, icon, description }`. */
|
|
118
|
+
const choiceText = (option: unknown): string =>
|
|
119
|
+
typeof option === "string" ? option : String((option as FormValue | null)?.label ?? "");
|
|
120
|
+
|
|
121
|
+
/** `Your e-mail address` → `your_e_mail_address`, the name a field's answer is sent under. */
|
|
122
|
+
function fieldName(label: string): string {
|
|
123
|
+
const name = label
|
|
124
|
+
.toLowerCase()
|
|
125
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
126
|
+
.replace(/^_+|_+$/g, "");
|
|
127
|
+
return /^[a-z]/.test(name) ? name.slice(0, 40) : `field_${name}`.slice(0, 40);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type Properties = Record<string, SchemaProperty>;
|
|
131
|
+
|
|
132
|
+
/** A block's settings, laid out in the collapsible sections its schema declares. */
|
|
133
|
+
export function BlockSettings({
|
|
134
|
+
definition,
|
|
135
|
+
value,
|
|
136
|
+
openFirst = true,
|
|
137
|
+
only,
|
|
138
|
+
onChange,
|
|
139
|
+
}: {
|
|
140
|
+
definition: BlockDefinition;
|
|
141
|
+
value: FormValue;
|
|
142
|
+
/** The first section starts open, as it did in BrimCMS, unless another section above already is. */
|
|
143
|
+
openFirst?: boolean;
|
|
144
|
+
/** Leaves out the settings it says no to, e.g. an image's address when that belongs to an item. */
|
|
145
|
+
only?: ((name: string) => boolean) | undefined;
|
|
146
|
+
onChange: (value: FormValue) => void;
|
|
147
|
+
}): ReactNode {
|
|
148
|
+
const { fieldsets, properties } = definition.schema;
|
|
149
|
+
const listed = fieldsets.flatMap((fieldset) =>
|
|
150
|
+
fieldset.fields.flatMap((name) => {
|
|
151
|
+
const property = properties[name];
|
|
152
|
+
return property && isShown(property, value) && (only?.(name) ?? true) ? [{ fieldset, name, property }] : [];
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
const sections = fieldsets
|
|
156
|
+
.map((fieldset) => ({
|
|
157
|
+
fieldset,
|
|
158
|
+
fields: listed.filter((field) => field.fieldset === fieldset && isSupported(field.property)),
|
|
159
|
+
}))
|
|
160
|
+
.filter((section) => section.fields.length > 0);
|
|
161
|
+
const missing = listed.filter((field) => !isSupported(field.property)).map((field) => field.property.title);
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<>
|
|
165
|
+
{sections.map(({ fieldset, fields }, index) => (
|
|
166
|
+
<Fieldset key={fieldset.id} title={fieldset.title} open={openFirst && index === 0}>
|
|
167
|
+
{fields.map(({ name, property }) => (
|
|
168
|
+
<PropertyField
|
|
169
|
+
key={name}
|
|
170
|
+
name={name}
|
|
171
|
+
property={property}
|
|
172
|
+
properties={properties}
|
|
173
|
+
settings={value}
|
|
174
|
+
onPatch={(patch) => onChange({ ...value, ...patch })}
|
|
175
|
+
/>
|
|
176
|
+
))}
|
|
177
|
+
</Fieldset>
|
|
178
|
+
))}
|
|
179
|
+
{missing.length > 0 && <p className="brim-sidebar-note">Not editable here yet: {missing.join(", ")}.</p>}
|
|
180
|
+
</>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isSupported(property: SchemaProperty): boolean {
|
|
185
|
+
return SUPPORTED_WIDGETS.has(property.widget ?? property.type ?? "");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type Condition = Record<string, unknown>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* `showWhen` lists other settings and the values they must have for this one to appear.
|
|
192
|
+
* A list means "any of these"; `$or` holds alternative conditions.
|
|
193
|
+
*/
|
|
194
|
+
function isShown(property: SchemaProperty, settings: FormValue): boolean {
|
|
195
|
+
return property.widget !== "hidden" && matches(property.showWhen, settings);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function matches(condition: Condition | undefined, settings: FormValue): boolean {
|
|
199
|
+
return Object.entries(condition ?? {}).every(([name, expected]) => {
|
|
200
|
+
if (name === "$or") return (expected as Condition[]).some((alternative) => matches(alternative, settings));
|
|
201
|
+
const actual = settings[name];
|
|
202
|
+
if (Array.isArray(expected)) return expected.includes(actual);
|
|
203
|
+
return typeof expected === "boolean" ? Boolean(actual) === expected : actual === expected;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function numberOf(value: unknown, fallback: number): number {
|
|
208
|
+
return typeof value === "number" ? value : fallback;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function bounds(property: SchemaProperty | undefined): { min: number; max: number; step: number } {
|
|
212
|
+
return {
|
|
213
|
+
min: property?.min ?? property?.minimum ?? 0,
|
|
214
|
+
max: property?.max ?? property?.maximum ?? 100,
|
|
215
|
+
step: property?.step ?? 1,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function PropertyField({
|
|
220
|
+
name,
|
|
221
|
+
property,
|
|
222
|
+
properties,
|
|
223
|
+
settings,
|
|
224
|
+
onPatch,
|
|
225
|
+
}: {
|
|
226
|
+
name: string;
|
|
227
|
+
property: SchemaProperty;
|
|
228
|
+
/** Every setting of the block: some widgets edit a second setting next to their own. */
|
|
229
|
+
properties: Properties;
|
|
230
|
+
settings: FormValue;
|
|
231
|
+
onPatch: (patch: FormValue) => void;
|
|
232
|
+
}): ReactNode {
|
|
233
|
+
const { viewport } = useEditor();
|
|
234
|
+
const label = property.title ?? "";
|
|
235
|
+
const description = property.description;
|
|
236
|
+
const current = settings[name] ?? property.default;
|
|
237
|
+
const options = (property.options ?? []).map((option) => ({ value: String(option.value), label: option.label }));
|
|
238
|
+
const placeholder = typeof property.placeholder === "string" ? property.placeholder : undefined;
|
|
239
|
+
const set = (value: unknown): void => onPatch({ [name]: value });
|
|
240
|
+
const key = (field: string): string | undefined =>
|
|
241
|
+
typeof property[field] === "string" ? (property[field] as string) : undefined;
|
|
242
|
+
|
|
243
|
+
switch (property.widget ?? property.type) {
|
|
244
|
+
case "checkbox":
|
|
245
|
+
case "boolean":
|
|
246
|
+
return (
|
|
247
|
+
<CheckboxWidget
|
|
248
|
+
label={label}
|
|
249
|
+
description={description}
|
|
250
|
+
checked={current === true || current === "cover"}
|
|
251
|
+
onChange={set}
|
|
252
|
+
/>
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
case "select":
|
|
256
|
+
return (
|
|
257
|
+
<SelectWidget
|
|
258
|
+
label={label}
|
|
259
|
+
description={description}
|
|
260
|
+
options={options}
|
|
261
|
+
value={String(current)}
|
|
262
|
+
// Numeric choices are stored as numbers, as the schema declares them.
|
|
263
|
+
onChange={(next) => set(property.type === "number" ? Number(next) : next)}
|
|
264
|
+
/>
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
case "size": {
|
|
268
|
+
const customField = key("customValueField");
|
|
269
|
+
const customProperty = customField ? properties[customField] : undefined;
|
|
270
|
+
return (
|
|
271
|
+
<SizeWidget
|
|
272
|
+
label={label}
|
|
273
|
+
description={description}
|
|
274
|
+
options={options}
|
|
275
|
+
value={String(current)}
|
|
276
|
+
custom={
|
|
277
|
+
customField && customProperty
|
|
278
|
+
? {
|
|
279
|
+
title: customProperty.title ?? "Custom",
|
|
280
|
+
value: numberOf(settings[customField], numberOf(customProperty.default, 0)),
|
|
281
|
+
...bounds(customProperty),
|
|
282
|
+
onChange: (next) => onPatch({ [customField]: next }),
|
|
283
|
+
}
|
|
284
|
+
: undefined
|
|
285
|
+
}
|
|
286
|
+
onChange={set}
|
|
287
|
+
/>
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
case "button-variant":
|
|
292
|
+
return <SelectWidget label={label} options={BUTTON_VARIANTS} value={String(current)} onChange={set} />;
|
|
293
|
+
|
|
294
|
+
case "icon-picker":
|
|
295
|
+
return <IconPickerWidget label={label} value={String(current)} onChange={set} />;
|
|
296
|
+
|
|
297
|
+
case "alignment":
|
|
298
|
+
return (
|
|
299
|
+
<AlignmentWidget
|
|
300
|
+
label={label}
|
|
301
|
+
description={description}
|
|
302
|
+
options={options.length > 0 ? options : DEFAULT_ALIGNMENTS}
|
|
303
|
+
value={String(current)}
|
|
304
|
+
onChange={set}
|
|
305
|
+
/>
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
case "range":
|
|
309
|
+
case "number":
|
|
310
|
+
case "integer":
|
|
311
|
+
return (
|
|
312
|
+
<RangeWidget
|
|
313
|
+
label={label}
|
|
314
|
+
description={description}
|
|
315
|
+
value={numberOf(current, 0)}
|
|
316
|
+
{...bounds(property)}
|
|
317
|
+
defaultValue={numberOf(property.default, 0)}
|
|
318
|
+
onChange={set}
|
|
319
|
+
/>
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
case "carousel-slides-per-view":
|
|
323
|
+
case "icon-grid-breakpoint-number": {
|
|
324
|
+
// The carousel names its tablet and mobile settings by convention rather than in the schema.
|
|
325
|
+
const prefixed = (prefix: string): string => `${prefix}${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
326
|
+
const fields = [name, key("tabletField") ?? prefixed("tablet"), key("mobileField") ?? prefixed("mobile")];
|
|
327
|
+
return (
|
|
328
|
+
<BreakpointNumberWidget
|
|
329
|
+
label={label}
|
|
330
|
+
description={description}
|
|
331
|
+
values={fields.map((field) => {
|
|
332
|
+
const fieldProperty = field ? properties[field] : undefined;
|
|
333
|
+
if (!field || !fieldProperty) return undefined;
|
|
334
|
+
return {
|
|
335
|
+
value: numberOf(settings[field], numberOf(fieldProperty.default, 1)),
|
|
336
|
+
...bounds(fieldProperty),
|
|
337
|
+
onChange: (next: number) => onPatch({ [field]: next }),
|
|
338
|
+
};
|
|
339
|
+
})}
|
|
340
|
+
/>
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
case "color-select":
|
|
345
|
+
return (
|
|
346
|
+
<ColorSelectWidget
|
|
347
|
+
label={label}
|
|
348
|
+
description={description}
|
|
349
|
+
options={options}
|
|
350
|
+
value={String(current ?? "")}
|
|
351
|
+
allowCustom={property.allowCustomColor === true}
|
|
352
|
+
onChange={set}
|
|
353
|
+
/>
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
case "url": {
|
|
357
|
+
const newTabField = key("newTabField");
|
|
358
|
+
return (
|
|
359
|
+
<LinkWidget
|
|
360
|
+
label={label}
|
|
361
|
+
description={description}
|
|
362
|
+
value={String(current ?? "")}
|
|
363
|
+
placeholder={placeholder}
|
|
364
|
+
newTab={
|
|
365
|
+
newTabField
|
|
366
|
+
? { checked: settings[newTabField] === true, onChange: (checked) => onPatch({ [newTabField]: checked }) }
|
|
367
|
+
: undefined
|
|
368
|
+
}
|
|
369
|
+
onChange={set}
|
|
370
|
+
/>
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
case "image":
|
|
375
|
+
return <ImageUploadWidget label={label} description={description} value={String(current ?? "")} onChange={set} />;
|
|
376
|
+
|
|
377
|
+
case "image-focus":
|
|
378
|
+
return (
|
|
379
|
+
<ImageFocusField label={label} property={property} settings={settings} viewport={viewport} onPatch={onPatch} />
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
case "image-shape":
|
|
383
|
+
return <ShapeWidget label={label} value={String(current)} onChange={set} />;
|
|
384
|
+
|
|
385
|
+
case "image-fit":
|
|
386
|
+
return <FitWidget label={label} description={description} value={String(current)} onChange={set} />;
|
|
387
|
+
|
|
388
|
+
case "video": {
|
|
389
|
+
const posterField = key("posterField");
|
|
390
|
+
return (
|
|
391
|
+
<>
|
|
392
|
+
<TextWidget
|
|
393
|
+
label={label}
|
|
394
|
+
description="A YouTube or Vimeo link, or the address of a video file."
|
|
395
|
+
placeholder="https://www.youtube.com/watch?v=…"
|
|
396
|
+
value={String(current ?? "")}
|
|
397
|
+
// Some settings only apply to video files, so the kind of source is kept next to the address.
|
|
398
|
+
onChange={(url) => onPatch({ [name]: url, isYouTube: isEmbeddedVideo(url) })}
|
|
399
|
+
/>
|
|
400
|
+
<VideoUpload
|
|
401
|
+
onUploaded={(url, posterUrl) =>
|
|
402
|
+
onPatch({
|
|
403
|
+
[name]: url,
|
|
404
|
+
isYouTube: false,
|
|
405
|
+
// The video's own first frame, unless the owner already chose a poster.
|
|
406
|
+
...(posterField && posterUrl && !settings[posterField] && { [posterField]: posterUrl }),
|
|
407
|
+
})
|
|
408
|
+
}
|
|
409
|
+
/>
|
|
410
|
+
</>
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
case "social-network-order":
|
|
415
|
+
return (
|
|
416
|
+
<SocialNetworksWidget
|
|
417
|
+
label={label}
|
|
418
|
+
description="Turn networks on or off and set their order."
|
|
419
|
+
order={current}
|
|
420
|
+
settings={settings}
|
|
421
|
+
withProfiles={settings.mode === "profile"}
|
|
422
|
+
onPatch={onPatch}
|
|
423
|
+
/>
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
case "map-points":
|
|
427
|
+
return (
|
|
428
|
+
<CardList<FormValue>
|
|
429
|
+
label={label}
|
|
430
|
+
addLabel="Add location"
|
|
431
|
+
entries={Array.isArray(current) ? (current as FormValue[]) : []}
|
|
432
|
+
title={(entry, index) => String(entry.title || `Location ${index + 1}`)}
|
|
433
|
+
create={(index) => ({
|
|
434
|
+
id: `point-${Date.now()}`,
|
|
435
|
+
lat: 45.9432,
|
|
436
|
+
lng: 24.9668,
|
|
437
|
+
title: `Location ${index + 1}`,
|
|
438
|
+
address: "",
|
|
439
|
+
})}
|
|
440
|
+
render={(entry, change) => {
|
|
441
|
+
const field = (key: string, title: string, multiline = false): ReactNode => (
|
|
442
|
+
<TextWidget
|
|
443
|
+
label={title}
|
|
444
|
+
value={String(entry[key] ?? "")}
|
|
445
|
+
multiline={multiline}
|
|
446
|
+
onChange={(next) => change({ ...entry, [key]: next })}
|
|
447
|
+
/>
|
|
448
|
+
);
|
|
449
|
+
const coordinate = (key: "lat" | "lng", title: string): ReactNode => (
|
|
450
|
+
<TextWidget
|
|
451
|
+
label={title}
|
|
452
|
+
value={String(entry[key] ?? "")}
|
|
453
|
+
onChange={(next) => Number.isFinite(Number(next)) && change({ ...entry, [key]: Number(next) })}
|
|
454
|
+
/>
|
|
455
|
+
);
|
|
456
|
+
return (
|
|
457
|
+
<>
|
|
458
|
+
{field("title", "Name")}
|
|
459
|
+
{field("address", "Address")}
|
|
460
|
+
<LocationSearch
|
|
461
|
+
address={String(entry.address ?? "")}
|
|
462
|
+
onFound={(lat, lng) => change({ ...entry, lat, lng })}
|
|
463
|
+
/>
|
|
464
|
+
{coordinate("lat", "Latitude")}
|
|
465
|
+
{coordinate("lng", "Longitude")}
|
|
466
|
+
{field("details", "Details", true)}
|
|
467
|
+
{field("hours", "Opening hours")}
|
|
468
|
+
{field("phone", "Phone")}
|
|
469
|
+
{field("directionsUrl", "Directions link")}
|
|
470
|
+
</>
|
|
471
|
+
);
|
|
472
|
+
}}
|
|
473
|
+
onChange={set}
|
|
474
|
+
/>
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
case "carousel-images":
|
|
478
|
+
return (
|
|
479
|
+
<ImagesWidget
|
|
480
|
+
label={label || "Images"}
|
|
481
|
+
images={Array.isArray(current) ? (current as ListedImage[]) : []}
|
|
482
|
+
extras
|
|
483
|
+
onChange={set}
|
|
484
|
+
/>
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
case "accordion-items":
|
|
488
|
+
return (
|
|
489
|
+
<CardList<FormValue>
|
|
490
|
+
label={label || "Accordion Items"}
|
|
491
|
+
addLabel="Add item"
|
|
492
|
+
entries={Array.isArray(current) ? (current as FormValue[]) : []}
|
|
493
|
+
title={(entry, index) => String(entry.title || `Item ${index + 1}`)}
|
|
494
|
+
create={(index) => ({ id: `item_${Date.now()}`, title: `Item ${index + 1}`, content: "Add panel content." })}
|
|
495
|
+
render={(entry, change) => (
|
|
496
|
+
<>
|
|
497
|
+
<TextWidget
|
|
498
|
+
label="Title"
|
|
499
|
+
value={String(entry.title ?? "")}
|
|
500
|
+
onChange={(title) => change({ ...entry, title })}
|
|
501
|
+
/>
|
|
502
|
+
<TextWidget
|
|
503
|
+
label="Content"
|
|
504
|
+
value={plain(entry.content)}
|
|
505
|
+
multiline
|
|
506
|
+
onChange={(content) => change({ ...entry, content })}
|
|
507
|
+
/>
|
|
508
|
+
</>
|
|
509
|
+
)}
|
|
510
|
+
onChange={set}
|
|
511
|
+
/>
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
case "form-fields":
|
|
515
|
+
return <FormFieldsEditor label={label || "Fields"} entries={formFieldsOf(current)} onChange={set} />;
|
|
516
|
+
|
|
517
|
+
case "form-email-field": {
|
|
518
|
+
// The form's own email fields, as they are in the side panel now: a confirmation goes to one of them.
|
|
519
|
+
const emails = (Array.isArray(settings.fields) ? (settings.fields as FormValue[]) : []).filter(
|
|
520
|
+
(field) => field.type === "email" && typeof field.name === "string",
|
|
521
|
+
);
|
|
522
|
+
return (
|
|
523
|
+
<SelectWidget
|
|
524
|
+
label={label}
|
|
525
|
+
description={
|
|
526
|
+
emails.length === 0 ? "This form has no email field yet: add one, or no confirmation is sent." : description
|
|
527
|
+
}
|
|
528
|
+
options={[
|
|
529
|
+
{ value: "", label: "The form's first email field" },
|
|
530
|
+
...emails.map((field) => ({ value: String(field.name), label: String(field.label || field.name) })),
|
|
531
|
+
]}
|
|
532
|
+
value={emails.some((field) => field.name === current) ? String(current) : ""}
|
|
533
|
+
onChange={set}
|
|
534
|
+
/>
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
case "textarea":
|
|
539
|
+
case "richtext":
|
|
540
|
+
case "form-richtext":
|
|
541
|
+
return (
|
|
542
|
+
<TextWidget
|
|
543
|
+
label={label}
|
|
544
|
+
description={description}
|
|
545
|
+
value={plain(current)}
|
|
546
|
+
placeholder={placeholder}
|
|
547
|
+
multiline
|
|
548
|
+
onChange={set}
|
|
549
|
+
/>
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
default:
|
|
553
|
+
return (
|
|
554
|
+
<TextWidget
|
|
555
|
+
label={label}
|
|
556
|
+
description={description}
|
|
557
|
+
value={String(current ?? "")}
|
|
558
|
+
placeholder={placeholder}
|
|
559
|
+
onChange={set}
|
|
560
|
+
/>
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* A form's fields, in its steps. Without steps they are one list; with steps each step is a section of an accordion
|
|
567
|
+
* holding its own questions, and opening a step shows that step on the page (the editor's `brim-editor:form-step`).
|
|
568
|
+
*/
|
|
569
|
+
function FormFieldsEditor({
|
|
570
|
+
label,
|
|
571
|
+
entries,
|
|
572
|
+
onChange,
|
|
573
|
+
}: {
|
|
574
|
+
label: string;
|
|
575
|
+
entries: FormValue[];
|
|
576
|
+
onChange: (entries: FormValue[]) => void;
|
|
577
|
+
}) {
|
|
578
|
+
const groups = stepGroups(entries);
|
|
579
|
+
const stepped = groups.some((group) => group.step);
|
|
580
|
+
const [open, setOpen] = useState<number | null>(() => (stepped ? openFormStep : null));
|
|
581
|
+
const newField = (index: number): FormValue => ({
|
|
582
|
+
id: `field_${Date.now()}`,
|
|
583
|
+
type: "text",
|
|
584
|
+
name: `field_${Date.now().toString(36)}_${index + 1}`,
|
|
585
|
+
label: "New field",
|
|
586
|
+
});
|
|
587
|
+
const newStep = (count: number): FormValue => ({ id: `step_${Date.now()}`, type: STEP, label: `Step ${count + 1}` });
|
|
588
|
+
const write = (next: StepGroup[]) =>
|
|
589
|
+
onChange(next.flatMap((group) => (group.step ? [group.step] : []).concat(group.fields)));
|
|
590
|
+
const fieldList = (group: StepGroup, index: number) => (
|
|
591
|
+
<CardList<FormValue>
|
|
592
|
+
label={stepped ? "Questions" : label}
|
|
593
|
+
addLabel="Add question"
|
|
594
|
+
entries={group.fields}
|
|
595
|
+
title={(entry, at) => String(entry.label || `Question ${at + 1}`)}
|
|
596
|
+
create={newField}
|
|
597
|
+
render={(entry, change) => <FieldEditor entry={entry} all={entries} change={change} />}
|
|
598
|
+
onChange={(fields) => write(groups.map((other, at) => (at === index ? { ...other, fields } : other)))}
|
|
599
|
+
/>
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
/** Opens a step here and on the page; the page counts only steps that ask something. */
|
|
603
|
+
function show(index: number | null): void {
|
|
604
|
+
setOpen(index);
|
|
605
|
+
if (index === null) return;
|
|
606
|
+
openFormStep = index;
|
|
607
|
+
const shown = groups.slice(0, index).filter((group) => group.fields.length > 0).length;
|
|
608
|
+
window.dispatchEvent(new CustomEvent("brim-editor:form-step", { detail: shown }));
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function moveStep(index: number, offset: number): void {
|
|
612
|
+
const next = [...groups];
|
|
613
|
+
const [group] = next.splice(index, 1);
|
|
614
|
+
if (!group) return;
|
|
615
|
+
next.splice(index + offset, 0, group);
|
|
616
|
+
write(next);
|
|
617
|
+
show(index + offset);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** A removed step gives its questions to the step before it. */
|
|
621
|
+
function removeStep(index: number): void {
|
|
622
|
+
const next = groups.map((group) => ({ ...group, fields: [...group.fields] }));
|
|
623
|
+
const [removed] = next.splice(index, 1);
|
|
624
|
+
if (!removed) return;
|
|
625
|
+
const before = next[index - 1];
|
|
626
|
+
if (before) before.fields.push(...removed.fields);
|
|
627
|
+
else next.splice(0, 0, { step: undefined, fields: removed.fields });
|
|
628
|
+
write(next);
|
|
629
|
+
setOpen(null);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
if (!stepped) {
|
|
633
|
+
return (
|
|
634
|
+
<>
|
|
635
|
+
{fieldList(groups[0] ?? { step: undefined, fields: [] }, 0)}
|
|
636
|
+
<button
|
|
637
|
+
type="button"
|
|
638
|
+
className="brim-toolbar-btn brim-toolbar-btn--outline form-steps-add"
|
|
639
|
+
onClick={() => onChange([newStep(0), ...entries])}
|
|
640
|
+
>
|
|
641
|
+
Ask in steps
|
|
642
|
+
</button>
|
|
643
|
+
</>
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return (
|
|
648
|
+
<div className="field-group form-steps">
|
|
649
|
+
<div className="card-list-header">
|
|
650
|
+
<span className="field-label">Steps</span>
|
|
651
|
+
<span className="card-list-count">{groups.filter((group) => group.step).length}</span>
|
|
652
|
+
</div>
|
|
653
|
+
{groups.map((group, index) =>
|
|
654
|
+
group.step ? (
|
|
655
|
+
<div key={index} className="form-step" data-open={open === index}>
|
|
656
|
+
<div className="card-list-row">
|
|
657
|
+
<button
|
|
658
|
+
type="button"
|
|
659
|
+
className="card-list-toggle"
|
|
660
|
+
aria-expanded={open === index}
|
|
661
|
+
onClick={() => show(open === index ? null : index)}
|
|
662
|
+
>
|
|
663
|
+
<span className="form-step-number">{groups.slice(0, index + 1).filter((g) => g.step).length}</span>
|
|
664
|
+
<span>
|
|
665
|
+
{String(group.step.label || "Untitled step")}
|
|
666
|
+
<small className="form-step-summary">
|
|
667
|
+
{group.fields.map((field) => String(field.label || field.name)).join(" · ") || "No questions yet"}
|
|
668
|
+
</small>
|
|
669
|
+
</span>
|
|
670
|
+
</button>
|
|
671
|
+
<button
|
|
672
|
+
type="button"
|
|
673
|
+
className="card-list-btn"
|
|
674
|
+
disabled={index === 0 || !groups[index - 1]?.step}
|
|
675
|
+
title="Move up"
|
|
676
|
+
aria-label="Move step up"
|
|
677
|
+
onClick={() => moveStep(index, -1)}
|
|
678
|
+
>
|
|
679
|
+
↑
|
|
680
|
+
</button>
|
|
681
|
+
<button
|
|
682
|
+
type="button"
|
|
683
|
+
className="card-list-btn"
|
|
684
|
+
disabled={index === groups.length - 1}
|
|
685
|
+
title="Move down"
|
|
686
|
+
aria-label="Move step down"
|
|
687
|
+
onClick={() => moveStep(index, 1)}
|
|
688
|
+
>
|
|
689
|
+
↓
|
|
690
|
+
</button>
|
|
691
|
+
<button
|
|
692
|
+
type="button"
|
|
693
|
+
className="card-list-btn"
|
|
694
|
+
title="Remove the step (its questions join the step before)"
|
|
695
|
+
aria-label="Remove step"
|
|
696
|
+
onClick={() => removeStep(index)}
|
|
697
|
+
>
|
|
698
|
+
×
|
|
699
|
+
</button>
|
|
700
|
+
</div>
|
|
701
|
+
{open === index && (
|
|
702
|
+
<div className="card-list-body">
|
|
703
|
+
<TextWidget
|
|
704
|
+
label="Step title"
|
|
705
|
+
value={String(group.step.label ?? "")}
|
|
706
|
+
onChange={(next) =>
|
|
707
|
+
write(
|
|
708
|
+
groups.map((other, at) =>
|
|
709
|
+
at === index && other.step ? { ...other, step: { ...other.step, label: next } } : other,
|
|
710
|
+
),
|
|
711
|
+
)
|
|
712
|
+
}
|
|
713
|
+
/>
|
|
714
|
+
<TextWidget
|
|
715
|
+
label="Line under the title"
|
|
716
|
+
value={String(group.step.help ?? "")}
|
|
717
|
+
onChange={(help) =>
|
|
718
|
+
write(
|
|
719
|
+
groups.map((other, at) =>
|
|
720
|
+
at === index && other.step ? { ...other, step: { ...other.step, help } } : other,
|
|
721
|
+
),
|
|
722
|
+
)
|
|
723
|
+
}
|
|
724
|
+
/>
|
|
725
|
+
{fieldList(group, index)}
|
|
726
|
+
</div>
|
|
727
|
+
)}
|
|
728
|
+
</div>
|
|
729
|
+
) : (
|
|
730
|
+
// Questions written before the first step are asked first, untitled.
|
|
731
|
+
<div key={index} className="form-step">
|
|
732
|
+
{fieldList(group, index)}
|
|
733
|
+
</div>
|
|
734
|
+
),
|
|
735
|
+
)}
|
|
736
|
+
<button
|
|
737
|
+
type="button"
|
|
738
|
+
className="brim-toolbar-btn brim-toolbar-btn--outline form-steps-add"
|
|
739
|
+
onClick={() => {
|
|
740
|
+
const count = groups.filter((group) => group.step).length;
|
|
741
|
+
write([...groups, { step: newStep(count), fields: [newField(0)] }]);
|
|
742
|
+
show(groups.length);
|
|
743
|
+
}}
|
|
744
|
+
>
|
|
745
|
+
Add step
|
|
746
|
+
</button>
|
|
747
|
+
</div>
|
|
748
|
+
);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/** The step open last, kept while the sidebar is drawn again after each change. */
|
|
752
|
+
let openFormStep: number | null = null;
|
|
753
|
+
|
|
754
|
+
interface StepGroup {
|
|
755
|
+
step: FormValue | undefined;
|
|
756
|
+
fields: FormValue[];
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** The entries in their steps: a step entry and the questions after it, up to the next step. */
|
|
760
|
+
function stepGroups(entries: FormValue[]): StepGroup[] {
|
|
761
|
+
const groups: StepGroup[] = [];
|
|
762
|
+
for (const entry of entries) {
|
|
763
|
+
if (entry.type === STEP) groups.push({ step: entry, fields: [] });
|
|
764
|
+
else {
|
|
765
|
+
if (groups.length === 0) groups.push({ step: undefined, fields: [] });
|
|
766
|
+
groups[groups.length - 1]?.fields.push(entry);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return groups;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** One question of a form: its kind, words, choices and rules. */
|
|
773
|
+
function FieldEditor({
|
|
774
|
+
entry,
|
|
775
|
+
all,
|
|
776
|
+
change,
|
|
777
|
+
}: {
|
|
778
|
+
entry: FormValue;
|
|
779
|
+
all: FormValue[];
|
|
780
|
+
change: (next: FormValue) => void;
|
|
781
|
+
}) {
|
|
782
|
+
return (
|
|
783
|
+
<>
|
|
784
|
+
<SelectWidget
|
|
785
|
+
label="Type"
|
|
786
|
+
options={FIELD_TYPE_OPTIONS.filter((option) => option.value !== STEP)}
|
|
787
|
+
value={String(entry.type)}
|
|
788
|
+
onChange={(type) => change({ ...entry, type })}
|
|
789
|
+
/>
|
|
790
|
+
<>
|
|
791
|
+
<TextWidget
|
|
792
|
+
label="Label"
|
|
793
|
+
value={String(entry.label ?? "")}
|
|
794
|
+
// A new question takes its name from its label, so answers are stored under something readable; once named,
|
|
795
|
+
// it keeps the name, which its answers and the questions asked after it refer to.
|
|
796
|
+
onChange={(next) =>
|
|
797
|
+
change({
|
|
798
|
+
...entry,
|
|
799
|
+
label: next,
|
|
800
|
+
name: String(entry.name ?? "").startsWith("field_") ? fieldName(next) : entry.name,
|
|
801
|
+
})
|
|
802
|
+
}
|
|
803
|
+
/>
|
|
804
|
+
{entry.type === "file" ? (
|
|
805
|
+
<>
|
|
806
|
+
<TextWidget
|
|
807
|
+
label="Accepted files"
|
|
808
|
+
description={`Any of ${Object.keys(FILE_TYPES).join(", ")}, separated by commas. Empty takes ${DEFAULT_FILE_TYPES.join(", ")}. Files are checked by their content, each up to ${fileSize(MAX_FILE_BYTES)}; only people who can read form responses can download them.`}
|
|
809
|
+
value={Array.isArray(entry.accept) ? entry.accept.join(", ") : String(entry.accept ?? "")}
|
|
810
|
+
placeholder={DEFAULT_FILE_TYPES.join(", ")}
|
|
811
|
+
onChange={(accept) => change({ ...entry, accept })}
|
|
812
|
+
/>
|
|
813
|
+
<CheckboxWidget
|
|
814
|
+
label={`Allow several files (up to ${MAX_FILES})`}
|
|
815
|
+
checked={entry.multiple === true}
|
|
816
|
+
onChange={(multiple) => change({ ...entry, multiple })}
|
|
817
|
+
/>
|
|
818
|
+
</>
|
|
819
|
+
) : (
|
|
820
|
+
<TextWidget
|
|
821
|
+
label="Placeholder"
|
|
822
|
+
value={String(entry.placeholder ?? "")}
|
|
823
|
+
onChange={(placeholder) => change({ ...entry, placeholder })}
|
|
824
|
+
/>
|
|
825
|
+
)}
|
|
826
|
+
{(entry.type === "radio" || entry.type === "checkboxes") && entry.display === "cards" ? (
|
|
827
|
+
<ChoiceCards entry={entry} change={change} />
|
|
828
|
+
) : (
|
|
829
|
+
(entry.type === "select" || entry.type === "radio" || entry.type === "checkboxes") && (
|
|
830
|
+
<TextWidget
|
|
831
|
+
label="Choices"
|
|
832
|
+
description="One per line."
|
|
833
|
+
multiline
|
|
834
|
+
value={(Array.isArray(entry.options) ? entry.options : []).map(choiceText).join("\n")}
|
|
835
|
+
onChange={(text) => {
|
|
836
|
+
// A choice whose words stay keeps its icon and description.
|
|
837
|
+
const before = Array.isArray(entry.options) ? entry.options : [];
|
|
838
|
+
const options = text
|
|
839
|
+
.split("\n")
|
|
840
|
+
.map((line) => line.trim())
|
|
841
|
+
.filter(Boolean)
|
|
842
|
+
.map((line) => {
|
|
843
|
+
const kept = before.find((option) => typeof option === "object" && choiceText(option) === line);
|
|
844
|
+
return kept ? { ...(kept as FormValue), label: line } : line;
|
|
845
|
+
});
|
|
846
|
+
change({ ...entry, options });
|
|
847
|
+
}}
|
|
848
|
+
/>
|
|
849
|
+
)
|
|
850
|
+
)}
|
|
851
|
+
{(entry.type === "radio" || entry.type === "checkboxes") && (
|
|
852
|
+
<SelectWidget
|
|
853
|
+
label="Show choices as"
|
|
854
|
+
options={FIELD_DISPLAY_OPTIONS}
|
|
855
|
+
value={entry.display === "cards" ? "cards" : "list"}
|
|
856
|
+
onChange={(display) => change({ ...entry, display })}
|
|
857
|
+
/>
|
|
858
|
+
)}
|
|
859
|
+
<TextWidget
|
|
860
|
+
label="Help"
|
|
861
|
+
description="A line under the question."
|
|
862
|
+
value={String(entry.help ?? "")}
|
|
863
|
+
onChange={(help) => change({ ...entry, help })}
|
|
864
|
+
/>
|
|
865
|
+
<SelectWidget
|
|
866
|
+
label="Width"
|
|
867
|
+
options={FIELD_WIDTH_OPTIONS}
|
|
868
|
+
value={entry.width === "half" ? "half" : "full"}
|
|
869
|
+
onChange={(width) => change({ ...entry, width })}
|
|
870
|
+
/>
|
|
871
|
+
<CheckboxWidget
|
|
872
|
+
label="Required"
|
|
873
|
+
checked={entry.required === true}
|
|
874
|
+
onChange={(required) => change({ ...entry, required })}
|
|
875
|
+
/>
|
|
876
|
+
<ShowWhenWidget entry={entry} fields={all} change={change} />
|
|
877
|
+
</>
|
|
878
|
+
</>
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/** The entries of a form's fields setting. */
|
|
883
|
+
const formFieldsOf = (value: unknown): FormValue[] => (Array.isArray(value) ? (value as FormValue[]) : []);
|
|
884
|
+
|
|
885
|
+
/** A field's choices as cards, each with its words, line, photo and icon: what a visitor sees on each card. */
|
|
886
|
+
function ChoiceCards({ entry, change }: { entry: FormValue; change: (next: FormValue) => void }) {
|
|
887
|
+
const choices = (Array.isArray(entry.options) ? entry.options : []).map((option) =>
|
|
888
|
+
typeof option === "string" ? { label: option } : (option as FormValue),
|
|
889
|
+
);
|
|
890
|
+
return (
|
|
891
|
+
<CardList<FormValue>
|
|
892
|
+
label="Choices"
|
|
893
|
+
addLabel="Add choice"
|
|
894
|
+
entries={choices}
|
|
895
|
+
title={(choice, index) => String(choice.label || `Choice ${index + 1}`)}
|
|
896
|
+
create={(index) => ({ label: `Choice ${index + 1}` })}
|
|
897
|
+
render={(choice, changeChoice) => (
|
|
898
|
+
<>
|
|
899
|
+
<TextWidget
|
|
900
|
+
label="Words"
|
|
901
|
+
value={String(choice.label ?? "")}
|
|
902
|
+
onChange={(label) => changeChoice({ ...choice, label })}
|
|
903
|
+
/>
|
|
904
|
+
<TextWidget
|
|
905
|
+
label="Line under"
|
|
906
|
+
value={String(choice.description ?? "")}
|
|
907
|
+
onChange={(description) => changeChoice({ ...choice, description })}
|
|
908
|
+
/>
|
|
909
|
+
<ImageUploadWidget
|
|
910
|
+
label="Photo"
|
|
911
|
+
description="Fills the card's top."
|
|
912
|
+
value={String(choice.image ?? "")}
|
|
913
|
+
onChange={(image) => changeChoice({ ...choice, image })}
|
|
914
|
+
/>
|
|
915
|
+
<TextWidget
|
|
916
|
+
label="Icon"
|
|
917
|
+
description="A picture's address (an SVG of the site) or an emoji."
|
|
918
|
+
value={String(choice.icon ?? "")}
|
|
919
|
+
onChange={(icon) => changeChoice({ ...choice, icon })}
|
|
920
|
+
/>
|
|
921
|
+
</>
|
|
922
|
+
)}
|
|
923
|
+
onChange={(options) => change({ ...entry, options })}
|
|
924
|
+
/>
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/** "Ask only when": another field of the form with choices, and the answer of it that brings this field. */
|
|
929
|
+
function ShowWhenWidget({
|
|
930
|
+
entry,
|
|
931
|
+
fields,
|
|
932
|
+
change,
|
|
933
|
+
}: {
|
|
934
|
+
entry: FormValue;
|
|
935
|
+
fields: FormValue[];
|
|
936
|
+
change: (next: FormValue) => void;
|
|
937
|
+
}) {
|
|
938
|
+
const sources = fields.filter(
|
|
939
|
+
(field) => field.name !== entry.name && ["select", "radio", "checkboxes"].includes(String(field.type)),
|
|
940
|
+
);
|
|
941
|
+
if (sources.length === 0) return null;
|
|
942
|
+
const condition = (entry.showWhen ?? {}) as { field?: string; equals?: string | string[] };
|
|
943
|
+
const source = sources.find((field) => field.name === condition.field);
|
|
944
|
+
const answer = Array.isArray(condition.equals) ? (condition.equals[0] ?? "") : String(condition.equals ?? "");
|
|
945
|
+
const options = (Array.isArray(source?.options) ? source.options : []).map(choiceText).filter(Boolean);
|
|
946
|
+
const { showWhen: _showWhen, ...rest } = entry;
|
|
947
|
+
return (
|
|
948
|
+
<>
|
|
949
|
+
<SelectWidget
|
|
950
|
+
label="Ask only when"
|
|
951
|
+
options={[
|
|
952
|
+
{ value: "", label: "Always" },
|
|
953
|
+
...sources.map((field) => ({ value: String(field.name), label: String(field.label || field.name) })),
|
|
954
|
+
]}
|
|
955
|
+
value={source ? String(source.name) : ""}
|
|
956
|
+
onChange={(field) => change(field ? { ...entry, showWhen: { field, equals: options[0] ?? "" } } : rest)}
|
|
957
|
+
/>
|
|
958
|
+
{source && (
|
|
959
|
+
<SelectWidget
|
|
960
|
+
label="is answered"
|
|
961
|
+
options={options.map((option) => ({ value: option, label: option }))}
|
|
962
|
+
value={answer}
|
|
963
|
+
onChange={(equals) => change({ ...entry, showWhen: { field: source.name, equals } })}
|
|
964
|
+
/>
|
|
965
|
+
)}
|
|
966
|
+
</>
|
|
967
|
+
);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
/** BrimCMS kept some texts as HTML; they are edited as the plain text they show. */
|
|
971
|
+
function plain(value: unknown): string {
|
|
972
|
+
return typeof value === "string"
|
|
973
|
+
? value
|
|
974
|
+
.replace(/<\/(p|div|h[1-6]|li)>/gi, "\n")
|
|
975
|
+
.replace(/<[^>]*>/g, "")
|
|
976
|
+
.trim()
|
|
977
|
+
: "";
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/**
|
|
981
|
+
* The focal point is kept per preview size, in settings the schema names.
|
|
982
|
+
* A size without its own value shows the one it inherits from the larger size.
|
|
983
|
+
*/
|
|
984
|
+
function ImageFocusField({
|
|
985
|
+
label,
|
|
986
|
+
property,
|
|
987
|
+
settings,
|
|
988
|
+
viewport,
|
|
989
|
+
onPatch,
|
|
990
|
+
}: {
|
|
991
|
+
label: string;
|
|
992
|
+
property: SchemaProperty;
|
|
993
|
+
settings: FormValue;
|
|
994
|
+
viewport: "desktop" | "tablet" | "mobile";
|
|
995
|
+
onPatch: (patch: FormValue) => void;
|
|
996
|
+
}) {
|
|
997
|
+
const field = (name: string): string => String(property[name] ?? "");
|
|
998
|
+
const fields = {
|
|
999
|
+
desktop: [field("xField"), field("yField")],
|
|
1000
|
+
tablet: [field("tabletXField"), field("tabletYField")],
|
|
1001
|
+
mobile: [field("mobileXField"), field("mobileYField")],
|
|
1002
|
+
} as const;
|
|
1003
|
+
const inheritance = {
|
|
1004
|
+
desktop: ["desktop"],
|
|
1005
|
+
tablet: ["tablet", "desktop"],
|
|
1006
|
+
mobile: ["mobile", "tablet", "desktop"],
|
|
1007
|
+
} as const;
|
|
1008
|
+
const [xField, yField] = fields[viewport];
|
|
1009
|
+
const read = (axis: 0 | 1): number | undefined =>
|
|
1010
|
+
inheritance[viewport]
|
|
1011
|
+
.map((size) => settings[fields[size][axis]])
|
|
1012
|
+
.find((value): value is number => typeof value === "number");
|
|
1013
|
+
|
|
1014
|
+
return (
|
|
1015
|
+
<FocusWidget
|
|
1016
|
+
label={label}
|
|
1017
|
+
image={String(settings[field("imageField")] ?? "")}
|
|
1018
|
+
x={typeof settings[xField] === "number" ? (settings[xField] as number) : undefined}
|
|
1019
|
+
y={typeof settings[yField] === "number" ? (settings[yField] as number) : undefined}
|
|
1020
|
+
onChange={(x, y) => onPatch({ [xField]: x, [yField]: y })}
|
|
1021
|
+
inherited={{ x: read(0), y: read(1) }}
|
|
1022
|
+
/>
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
function AlignmentWidget({
|
|
1027
|
+
label,
|
|
1028
|
+
description,
|
|
1029
|
+
options,
|
|
1030
|
+
value,
|
|
1031
|
+
onChange,
|
|
1032
|
+
}: {
|
|
1033
|
+
label: string;
|
|
1034
|
+
description?: string | undefined;
|
|
1035
|
+
options: { value: string; label: string }[];
|
|
1036
|
+
value: string;
|
|
1037
|
+
onChange: (value: string) => void;
|
|
1038
|
+
}) {
|
|
1039
|
+
return (
|
|
1040
|
+
<div className="field-group">
|
|
1041
|
+
<span className="field-label">{label}</span>
|
|
1042
|
+
{description && <p className="field-description">{description}</p>}
|
|
1043
|
+
<div className="brim-align" role="group" aria-label={label}>
|
|
1044
|
+
{options.map((option) => {
|
|
1045
|
+
const Icon = ALIGN_ICONS[option.value];
|
|
1046
|
+
return (
|
|
1047
|
+
<button
|
|
1048
|
+
key={option.value}
|
|
1049
|
+
type="button"
|
|
1050
|
+
className="brim-align__option"
|
|
1051
|
+
aria-pressed={option.value === value}
|
|
1052
|
+
aria-label={option.label}
|
|
1053
|
+
title={option.label}
|
|
1054
|
+
onClick={() => onChange(option.value)}
|
|
1055
|
+
>
|
|
1056
|
+
{Icon ? <Icon /> : option.label}
|
|
1057
|
+
</button>
|
|
1058
|
+
);
|
|
1059
|
+
})}
|
|
1060
|
+
</div>
|
|
1061
|
+
</div>
|
|
1062
|
+
);
|
|
1063
|
+
}
|