@perkyzz/emvb 0.4.0 → 0.5.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/README.md +19 -3
- package/package.json +4 -3
- package/src/admin/agents-api.ts +39 -0
- package/src/admin/editor/CommandPalette.tsx +43 -2
- package/src/admin/editor/Editor.tsx +27 -0
- package/src/admin/editor/Timeline.tsx +108 -6
- package/src/admin/editor/editor-css.ts +4 -0
- package/src/admin/editor/feature-css.ts +22 -3
- package/src/admin/editor/palette.ts +6 -2
- package/src/admin/editor/panels/ColorControl.tsx +19 -8
- package/src/admin/editor/panels/ElementPanel.tsx +1 -1
- package/src/admin/editor/panels/TokenScales.tsx +4 -2
- package/src/admin/editor/panels/color-space.ts +12 -1
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +84 -28
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +42 -4
- package/src/admin/editor/panels/settings/DraftTextField.tsx +38 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
- package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +2 -3
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
- package/src/admin/editor/panels/settings/link-rules.ts +19 -0
- package/src/admin/editor/panels/settings/number-input.ts +11 -0
- package/src/admin/editor/panels/settings/query-style.ts +65 -0
- package/src/admin/editor/panels/variable-values.ts +4 -2
- package/src/admin/editor/remote-change.ts +67 -0
- package/src/admin/index.tsx +3 -1
- package/src/admin/media-api.ts +20 -0
- package/src/admin/pages/AgentsPage.tsx +274 -0
- package/src/admin/ui.ts +28 -0
- package/src/astro/floats/runtime.ts +10 -0
- package/src/astro/popups/runtime.ts +15 -0
- package/src/astro/tabs/runtime.ts +60 -1
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +202 -26
- package/src/core/arrange.ts +11 -3
- package/src/core/css/generate.ts +9 -1
- package/src/core/elements/form-elements.ts +19 -3
- package/src/core/elements/index.ts +39 -11
- package/src/core/render/dynamic-post.ts +21 -5
- package/src/core/render/form.ts +12 -5
- package/src/core/render/index.ts +50 -11
- package/src/core/render/pagination.ts +21 -2
- package/src/core/sanitize/embed-url.ts +7 -1
- package/src/core/sanitize/href.ts +13 -1
- package/src/core/schema/descriptors.ts +4 -1
- package/src/core/schema/layout.ts +10 -3
- package/src/core/share/link.ts +4 -1
- package/src/core/theme/dynamic.ts +12 -7
- package/src/mcp/errors.ts +44 -0
- package/src/mcp/index.ts +4 -0
- package/src/mcp/ports.ts +78 -0
- package/src/mcp/prompts.ts +76 -0
- package/src/mcp/publish.ts +60 -0
- package/src/mcp/queue.ts +19 -0
- package/src/mcp/rate-limit.ts +80 -0
- package/src/mcp/read.ts +419 -0
- package/src/mcp/refs.ts +84 -0
- package/src/mcp/settings.ts +18 -0
- package/src/mcp/tools.ts +265 -0
- package/src/mcp/write.ts +582 -0
- package/src/server/agent-settings.ts +134 -0
- package/src/server/design-routes.ts +71 -17
- package/src/server/icon-routes.ts +5 -1
- package/src/server/mcp-routes.ts +265 -0
- package/src/server/plugin.ts +32 -2
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
savableAttributes,
|
|
10
10
|
type AttributeRow,
|
|
11
11
|
} from "./advanced-rules.ts";
|
|
12
|
+
import { QueryStyleRows } from "./QueryStyleRows.tsx";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* CSS id: a value the page can't save (bad characters, or another element's id) stays in the
|
|
@@ -82,51 +83,106 @@ export function ContainerQueryFields({
|
|
|
82
83
|
node: LayoutNode;
|
|
83
84
|
onChange: (node: LayoutNode) => void;
|
|
84
85
|
}) {
|
|
85
|
-
|
|
86
|
+
// W-401: a draft committed on blur or Enter. Clearing the field while retyping a width used to
|
|
87
|
+
// delete the query and every style in it on the first keystroke.
|
|
88
|
+
const stored = node.query ? String(node.query.min) : "";
|
|
89
|
+
const [draft, setDraft] = React.useState(stored);
|
|
90
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
91
|
+
React.useEffect(() => {
|
|
92
|
+
setDraft(stored);
|
|
93
|
+
setError(null);
|
|
94
|
+
}, [node.id, stored]);
|
|
95
|
+
const [confirming, setConfirming] = React.useState(false);
|
|
96
|
+
React.useEffect(() => setConfirming(false), [node.id]);
|
|
97
|
+
const copyStyle = () => {
|
|
98
|
+
setConfirming(false);
|
|
99
|
+
onChange({ ...node, query: { min: node.query?.min ?? 480, style: node.style ?? {} } });
|
|
100
|
+
};
|
|
101
|
+
const commitMin = () => {
|
|
102
|
+
const next = draft.trim();
|
|
103
|
+
if (next === stored) {
|
|
104
|
+
setError(null);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (next === "") {
|
|
108
|
+
setError(null);
|
|
109
|
+
const { query: _query, ...rest } = node;
|
|
110
|
+
onChange(rest);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const width = Number(next);
|
|
114
|
+
if (!/^\d+$/.test(next) || width < 1 || width > 3840) {
|
|
115
|
+
setError("Use a whole number of px from 1 to 3840.");
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
setError(null);
|
|
119
|
+
onChange({ ...node, query: { min: width, style: node.query?.style ?? {} } });
|
|
120
|
+
};
|
|
86
121
|
return (
|
|
87
122
|
<div className="emvb-field-group" data-emvb-container="">
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
123
|
+
{/* W-398: any element can take container-query styles; only a box can be the container. */}
|
|
124
|
+
{"children" in node && (
|
|
125
|
+
<label className="emvb-check">
|
|
126
|
+
<input
|
|
127
|
+
type="checkbox"
|
|
128
|
+
checked={node.container === true}
|
|
129
|
+
onChange={(event) =>
|
|
130
|
+
onChange({ ...node, container: event.target.checked ? true : undefined })
|
|
131
|
+
}
|
|
132
|
+
/>
|
|
133
|
+
Query container
|
|
134
|
+
</label>
|
|
135
|
+
)}
|
|
98
136
|
<Input
|
|
99
137
|
label="Styles from container width (px)"
|
|
100
138
|
className={FIELD}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
onChange={(event) =>
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
onChange(rest);
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
const width = Number(next);
|
|
113
|
-
if (!Number.isInteger(width) || width < 1 || width > 3840) return;
|
|
114
|
-
onChange({ ...node, query: { min: width, style: node.query?.style ?? {} } });
|
|
139
|
+
inputMode="numeric"
|
|
140
|
+
value={draft}
|
|
141
|
+
error={error ?? undefined}
|
|
142
|
+
data-emvb-query-min=""
|
|
143
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
144
|
+
onBlur={commitMin}
|
|
145
|
+
onKeyDown={(event) => {
|
|
146
|
+
if (event.key === "Enter") commitMin();
|
|
115
147
|
}}
|
|
116
148
|
/>
|
|
117
149
|
<Button
|
|
118
150
|
type="button"
|
|
119
151
|
variant="secondary"
|
|
120
152
|
className={BUTTON}
|
|
153
|
+
data-emvb-query-copy=""
|
|
121
154
|
onClick={() => {
|
|
122
|
-
|
|
123
|
-
|
|
155
|
+
// W-402: ask before replacing container-query styles that differ from the style.
|
|
156
|
+
const own = Object.keys(node.query?.style ?? {}).length;
|
|
157
|
+
const same = JSON.stringify(node.query?.style ?? {}) === JSON.stringify(node.style ?? {});
|
|
158
|
+
if (own > 0 && !same) setConfirming(true);
|
|
159
|
+
else copyStyle();
|
|
124
160
|
}}
|
|
125
161
|
>
|
|
126
162
|
Use current style at that width
|
|
127
163
|
</Button>
|
|
164
|
+
{confirming && (
|
|
165
|
+
<div className="emvb-query-confirm" role="alert" data-emvb-query-confirm="">
|
|
166
|
+
<p className="emvb-helper">
|
|
167
|
+
Replace the {Object.keys(node.query?.style ?? {}).length} styles set for this width with
|
|
168
|
+
the current style?
|
|
169
|
+
</p>
|
|
170
|
+
<Button type="button" variant="destructive" className={BUTTON} onClick={copyStyle}>
|
|
171
|
+
Replace
|
|
172
|
+
</Button>
|
|
173
|
+
<Button
|
|
174
|
+
type="button"
|
|
175
|
+
variant="secondary"
|
|
176
|
+
className={BUTTON}
|
|
177
|
+
onClick={() => setConfirming(false)}
|
|
178
|
+
>
|
|
179
|
+
Keep them
|
|
180
|
+
</Button>
|
|
181
|
+
</div>
|
|
182
|
+
)}
|
|
183
|
+
<QueryStyleRows node={node} onChange={onChange} />
|
|
128
184
|
<p className="emvb-helper">
|
|
129
|
-
Query container makes this box the size children measure. The
|
|
185
|
+
Query container makes this box the size children measure. The styles listed here apply
|
|
130
186
|
inside @container once the container is at least that wide, not the viewport.
|
|
131
187
|
</p>
|
|
132
188
|
</div>
|
|
@@ -29,6 +29,25 @@ export function parseSegments(text: string): string[] {
|
|
|
29
29
|
return [...new Set(names)].slice(0, 20);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
const ZONE = /^[A-Za-z][A-Za-z0-9_+-]*(\/[A-Za-z0-9_+-]+){0,2}$/;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* W-438: the time zone as stored ("" for UTC), or null when it isn't a zone this server knows.
|
|
36
|
+
* "america/toronto" becomes "America/Toronto". An unknown zone used to be saved and silently
|
|
37
|
+
* read as UTC.
|
|
38
|
+
*/
|
|
39
|
+
export function readTimeZone(text: string): string | null {
|
|
40
|
+
const value = text.trim();
|
|
41
|
+
if (value === "") return "";
|
|
42
|
+
if (!ZONE.test(value)) return null;
|
|
43
|
+
try {
|
|
44
|
+
const zone = new Intl.DateTimeFormat("en-US", { timeZone: value }).resolvedOptions().timeZone;
|
|
45
|
+
return ZONE.test(zone) ? zone : null;
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
32
51
|
/** Drops empty rules so the stored audience stays valid (W-313). */
|
|
33
52
|
function tidy(audience: Audience): Audience | undefined {
|
|
34
53
|
const next: Audience = { ...audience };
|
|
@@ -61,9 +80,14 @@ export function AudienceEditor({
|
|
|
61
80
|
const [countryText, setCountryText] = React.useState(node.audience?.countries?.join(", ") ?? "");
|
|
62
81
|
const [segmentText, setSegmentText] = React.useState(node.audience?.segments?.join(", ") ?? "");
|
|
63
82
|
const [segmentError, setSegmentError] = React.useState<string | null>(null);
|
|
83
|
+
// W-438: the zone is typed in full before it's checked; checking each keystroke refused the "/".
|
|
84
|
+
const [zoneText, setZoneText] = React.useState(node.audience?.timeZone ?? "");
|
|
85
|
+
const [zoneError, setZoneError] = React.useState<string | null>(null);
|
|
64
86
|
React.useEffect(() => {
|
|
65
87
|
setCountryText(node.audience?.countries?.join(", ") ?? "");
|
|
66
88
|
setSegmentText(node.audience?.segments?.join(", ") ?? "");
|
|
89
|
+
setZoneText(node.audience?.timeZone ?? "");
|
|
90
|
+
setZoneError(null);
|
|
67
91
|
}, [node.id]);
|
|
68
92
|
if (isRoot) return null;
|
|
69
93
|
const audience = node.audience;
|
|
@@ -261,18 +285,32 @@ export function AudienceEditor({
|
|
|
261
285
|
<span className="emvb-bind-label">Time zone</span>
|
|
262
286
|
<input
|
|
263
287
|
className="emvb-native-input"
|
|
264
|
-
value={
|
|
288
|
+
value={zoneText}
|
|
265
289
|
placeholder="UTC"
|
|
266
290
|
data-emvb-audience-zone=""
|
|
291
|
+
aria-invalid={zoneError ? true : undefined}
|
|
292
|
+
aria-describedby={zoneError ? `${id}-zone-error` : undefined}
|
|
267
293
|
onChange={(event) => {
|
|
268
|
-
|
|
269
|
-
|
|
294
|
+
setZoneText(event.currentTarget.value);
|
|
295
|
+
setZoneError(null);
|
|
296
|
+
}}
|
|
297
|
+
onBlur={() => {
|
|
298
|
+
const zone = readTimeZone(zoneText);
|
|
299
|
+
if (zone === null) {
|
|
300
|
+
setZoneError("Unknown time zone. Use a name such as America/Toronto.");
|
|
270
301
|
return;
|
|
271
|
-
|
|
302
|
+
}
|
|
303
|
+
setZoneText(zone);
|
|
304
|
+
if (zone !== (base.timeZone ?? "")) set({ ...base, timeZone: zone });
|
|
272
305
|
}}
|
|
273
306
|
/>
|
|
274
307
|
</label>
|
|
275
308
|
)}
|
|
309
|
+
{(base.hours || base.days) && zoneError && (
|
|
310
|
+
<span className="emvb-helper" id={`${id}-zone-error`} data-emvb-audience-zone-error="">
|
|
311
|
+
{zoneError}
|
|
312
|
+
</span>
|
|
313
|
+
)}
|
|
276
314
|
<p className="emvb-helper" data-emvb-audience-help="">
|
|
277
315
|
{ruleCount(base) === 0
|
|
278
316
|
? "Add a rule: with none, every visitor matches."
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { Input } from "@cloudflare/kumo";
|
|
2
|
+
import { hrefError } from "./link-rules.ts";
|
|
3
|
+
import { fieldNumber } from "./number-input.ts";
|
|
2
4
|
import * as React from "react";
|
|
3
5
|
import {
|
|
4
6
|
MAX_TEXT_LENGTH,
|
|
@@ -99,10 +101,19 @@ export function DraftTextField({
|
|
|
99
101
|
.map((line) => line.trimEnd())
|
|
100
102
|
.filter((line) => line.length > 0);
|
|
101
103
|
if (items.length === 0 && field.optional) {
|
|
104
|
+
setError(null);
|
|
102
105
|
onChange(withProp(node, field.key, undefined));
|
|
103
106
|
return;
|
|
104
107
|
}
|
|
105
|
-
|
|
108
|
+
const next = withProp(node, field.key, items.length > 0 ? items : [""]);
|
|
109
|
+
// W-394: a line the schema refuses (a grid track like 4fr) stays in the field with the
|
|
110
|
+
// field's hint instead of making the page fail to save.
|
|
111
|
+
if (!savableNode(next) && savableNode(node)) {
|
|
112
|
+
setError(field.message ?? "One of these lines can't be saved.");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
setError(null);
|
|
116
|
+
onChange(next);
|
|
106
117
|
};
|
|
107
118
|
return (
|
|
108
119
|
<div className="emvb-field-group" data-emvb-field={field.key}>
|
|
@@ -115,6 +126,8 @@ export function DraftTextField({
|
|
|
115
126
|
dir="auto"
|
|
116
127
|
className={`${FIELD} emvb-textarea`}
|
|
117
128
|
rows={field.kind === "list-items" ? 4 : 5}
|
|
129
|
+
aria-invalid={error ? true : undefined}
|
|
130
|
+
aria-describedby={error ? `${areaId}-error` : undefined}
|
|
118
131
|
value={draft}
|
|
119
132
|
onChange={(event) => setDraft(event.target.value)}
|
|
120
133
|
onBlur={() => {
|
|
@@ -122,6 +135,11 @@ export function DraftTextField({
|
|
|
122
135
|
else onChange(withProp(node, field.key, draft));
|
|
123
136
|
}}
|
|
124
137
|
/>
|
|
138
|
+
{error && (
|
|
139
|
+
<span className="emvb-helper" id={`${areaId}-error`} data-emvb-field-error="">
|
|
140
|
+
{error}
|
|
141
|
+
</span>
|
|
142
|
+
)}
|
|
125
143
|
</div>
|
|
126
144
|
);
|
|
127
145
|
}
|
|
@@ -134,20 +152,33 @@ export function DraftTextField({
|
|
|
134
152
|
: "px";
|
|
135
153
|
const commit = () => {
|
|
136
154
|
if (commitClearable()) return;
|
|
137
|
-
const value =
|
|
155
|
+
const value = fieldNumber(draft, integer ? undefined : unit);
|
|
156
|
+
// W-388: a field may start at 0 (a focal point, a gap); others start at 1.
|
|
157
|
+
const min = integer ? (field.min ?? 1) : 0;
|
|
158
|
+
const range =
|
|
159
|
+
integer && field.max !== undefined
|
|
160
|
+
? `Enter a whole number from ${min} to ${field.max}.`
|
|
161
|
+
: undefined;
|
|
138
162
|
if (
|
|
163
|
+
draft.trim() === "" ||
|
|
139
164
|
!Number.isFinite(value) ||
|
|
140
165
|
(integer && !Number.isInteger(value)) ||
|
|
141
|
-
value <
|
|
166
|
+
value < min
|
|
142
167
|
) {
|
|
143
168
|
setError(
|
|
144
|
-
field.message ??
|
|
169
|
+
field.message ??
|
|
170
|
+
range ??
|
|
171
|
+
(integer
|
|
172
|
+
? min === 0
|
|
173
|
+
? "Enter 0 or a positive whole number."
|
|
174
|
+
: "Enter a positive whole number."
|
|
175
|
+
: "Enter 0 or more."),
|
|
145
176
|
);
|
|
146
177
|
return;
|
|
147
178
|
}
|
|
148
179
|
// A whole-number field past its schema's maximum would fail the save (W-223).
|
|
149
180
|
if (integer && field.max !== undefined && value > field.max) {
|
|
150
|
-
setError(field.message ?? `Enter a whole number from
|
|
181
|
+
setError(field.message ?? range ?? `Enter a whole number from ${min} to ${field.max}.`);
|
|
151
182
|
return;
|
|
152
183
|
}
|
|
153
184
|
if (value > MAX_FIELD_NUMBER) {
|
|
@@ -176,9 +207,7 @@ export function DraftTextField({
|
|
|
176
207
|
if (commitClearable()) return;
|
|
177
208
|
const safe = sanitizeHref(draft.trim());
|
|
178
209
|
if (!safe) {
|
|
179
|
-
setError(
|
|
180
|
-
field.message ?? "Use a full URL such as https://example.com or a path such as /pricing.",
|
|
181
|
-
);
|
|
210
|
+
setError(hrefError(draft, field.message));
|
|
182
211
|
return;
|
|
183
212
|
}
|
|
184
213
|
setError(null);
|
|
@@ -238,7 +267,7 @@ export function DraftTextField({
|
|
|
238
267
|
}
|
|
239
268
|
|
|
240
269
|
/** Whether `node` passes the layout schema on its own (inside a bare container). */
|
|
241
|
-
function savableNode(node: LayoutNode): boolean {
|
|
270
|
+
export function savableNode(node: LayoutNode): boolean {
|
|
242
271
|
return validateLayout({
|
|
243
272
|
schemaVersion: LAYOUT_SCHEMA_VERSION,
|
|
244
273
|
root: { id: "draftroot", type: "container", props: {}, children: [node] },
|
|
@@ -8,6 +8,7 @@ import { FieldBindControl } from "./FieldBindControl.tsx";
|
|
|
8
8
|
import { FormBindControl } from "./FormBindControl.tsx";
|
|
9
9
|
import { LoopItemBindControl } from "./LoopItemBindControl.tsx";
|
|
10
10
|
import { IconPicker } from "./IconPicker.tsx";
|
|
11
|
+
import { ImageSourcesField } from "./ImageSourcesField.tsx";
|
|
11
12
|
import { MediaPicker } from "./MediaPicker.tsx";
|
|
12
13
|
import { propsOf, withProp } from "./node-props.ts";
|
|
13
14
|
|
|
@@ -100,6 +101,10 @@ export function FieldControl({
|
|
|
100
101
|
return <IconPicker key={field.key} node={node} fetcher={fetcher} onChange={onChange} />;
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
if (field.kind === "image-sources") {
|
|
105
|
+
return <ImageSourcesField key={field.key} node={node} onChange={onChange} />;
|
|
106
|
+
}
|
|
107
|
+
|
|
103
108
|
if (field.kind === "media") {
|
|
104
109
|
if (!fetcher) {
|
|
105
110
|
return (
|
|
@@ -267,4 +272,5 @@ export const IMPLEMENTED_FIELD_KINDS = [
|
|
|
267
272
|
"media",
|
|
268
273
|
"icon",
|
|
269
274
|
"list-items",
|
|
275
|
+
"image-sources",
|
|
270
276
|
] as const;
|
|
@@ -117,9 +117,18 @@ export function IconPicker({
|
|
|
117
117
|
const patch: Record<string, unknown> = { iconId: icon.id, iconSvg: icon.markup };
|
|
118
118
|
// Keep the title in step with the glyph while it is still a default or the old icon's name
|
|
119
119
|
// (new Icon elements ship title "Star" with iconId "star"); a written title stays.
|
|
120
|
-
|
|
120
|
+
// W-387: only the Icon element has a title; a Button with one is refused on save.
|
|
121
|
+
if (node.type === "icon" && (!title || title === "Icon" || title.toLowerCase() === previous)) {
|
|
122
|
+
patch.title = icon.label;
|
|
123
|
+
}
|
|
121
124
|
onChange(withProps(node, patch));
|
|
122
125
|
};
|
|
126
|
+
// W-387: a Button's icon is optional, so it can be taken off again.
|
|
127
|
+
const removable = node.type !== "icon" && (current !== "" || typeof props.iconSvg === "string");
|
|
128
|
+
const remove = () => {
|
|
129
|
+
const { iconId: _id, iconSvg: _svg, ...rest } = propsOf(node);
|
|
130
|
+
onChange({ ...node, props: rest } as LayoutNode);
|
|
131
|
+
};
|
|
123
132
|
|
|
124
133
|
return (
|
|
125
134
|
<div ref={field} className="emvb-field-group" data-emvb-field="iconId" data-emvb-icon-picker="">
|
|
@@ -156,6 +165,17 @@ export function IconPicker({
|
|
|
156
165
|
>
|
|
157
166
|
Choose…
|
|
158
167
|
</Button>
|
|
168
|
+
{removable && (
|
|
169
|
+
<Button
|
|
170
|
+
variant="secondary"
|
|
171
|
+
size="sm"
|
|
172
|
+
className={BUTTON}
|
|
173
|
+
data-emvb-icon-remove=""
|
|
174
|
+
onClick={remove}
|
|
175
|
+
>
|
|
176
|
+
Remove
|
|
177
|
+
</Button>
|
|
178
|
+
)}
|
|
159
179
|
</div>
|
|
160
180
|
<IconLibraryDialog
|
|
161
181
|
open={open}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { sanitizeMediaUrl, type LayoutNode } from "../../../../core/index.ts";
|
|
3
|
+
import { FIELD } from "../../../ui.ts";
|
|
4
|
+
import { savableNode } from "./DraftTextField.tsx";
|
|
5
|
+
import { propsOf, withProp } from "./node-props.ts";
|
|
6
|
+
|
|
7
|
+
type Source = { src: string; width: number };
|
|
8
|
+
|
|
9
|
+
const MAX_SOURCES = 6;
|
|
10
|
+
|
|
11
|
+
/** Stored sources as lines: `URL WIDTH` (W-397). */
|
|
12
|
+
const sourcesText = (value: unknown): string =>
|
|
13
|
+
Array.isArray(value)
|
|
14
|
+
? value
|
|
15
|
+
.filter(
|
|
16
|
+
(s): s is Source =>
|
|
17
|
+
typeof s === "object" && s !== null && typeof (s as Source).src === "string",
|
|
18
|
+
)
|
|
19
|
+
.map((s) => `${s.src} ${s.width}`)
|
|
20
|
+
.join("\n")
|
|
21
|
+
: "";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `/photo-400.jpg 400` or `/photo-400.jpg 400w`, one per line → sources; or the reason the
|
|
25
|
+
* text can't be stored (W-397).
|
|
26
|
+
*/
|
|
27
|
+
export function parseSources(text: string): { sources: Source[] } | { error: string } {
|
|
28
|
+
// W-423: a srcset pasted from elsewhere ("a.jpg 480w, b.jpg 960w") splits into lines too.
|
|
29
|
+
const lines = text
|
|
30
|
+
.split(/\n|,\s+/)
|
|
31
|
+
.map((line) => line.trim())
|
|
32
|
+
.filter((line) => line.length > 0);
|
|
33
|
+
if (lines.length > MAX_SOURCES) return { error: `Up to ${MAX_SOURCES} extra sources.` };
|
|
34
|
+
const sources: Source[] = [];
|
|
35
|
+
for (const line of lines) {
|
|
36
|
+
const match = /^(\S+)\s+(\d{1,4})\s*(?:w|px)?$/i.exec(line);
|
|
37
|
+
const width = match ? Number(match[2]) : Number.NaN;
|
|
38
|
+
if (!match || !Number.isInteger(width) || width < 1 || width > 8000) {
|
|
39
|
+
return {
|
|
40
|
+
error: `"${line.slice(0, 60)}": write the URL, a space, then its width in px (1–8000).`,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const src = match[1] as string;
|
|
44
|
+
if (!sanitizeMediaUrl(src) || src.includes(",")) {
|
|
45
|
+
return { error: `"${src.slice(0, 60)}" isn't a URL or site path the page can use.` };
|
|
46
|
+
}
|
|
47
|
+
sources.push({ src, width });
|
|
48
|
+
}
|
|
49
|
+
return { sources };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The Image element's extra `srcset` sources (W-397): one `URL WIDTH` per line. */
|
|
53
|
+
export function ImageSourcesField({
|
|
54
|
+
node,
|
|
55
|
+
onChange,
|
|
56
|
+
}: {
|
|
57
|
+
node: LayoutNode;
|
|
58
|
+
onChange: (node: LayoutNode) => void;
|
|
59
|
+
}) {
|
|
60
|
+
const raw = propsOf(node)["sources"];
|
|
61
|
+
const [draft, setDraft] = React.useState(sourcesText(raw));
|
|
62
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
63
|
+
const id = React.useId();
|
|
64
|
+
React.useEffect(() => {
|
|
65
|
+
setDraft(sourcesText(raw));
|
|
66
|
+
setError(null);
|
|
67
|
+
}, [node.id, raw]);
|
|
68
|
+
|
|
69
|
+
const commit = () => {
|
|
70
|
+
const parsed = parseSources(draft);
|
|
71
|
+
if ("error" in parsed) {
|
|
72
|
+
setError(parsed.error);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const next = withProp(node, "sources", parsed.sources.length > 0 ? parsed.sources : undefined);
|
|
76
|
+
if (!savableNode(next) && savableNode(node)) {
|
|
77
|
+
setError("One of these sources can't be saved.");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
setError(null);
|
|
81
|
+
if (sourcesText(raw) === sourcesText(parsed.sources)) return;
|
|
82
|
+
onChange(next);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div className="emvb-field-group" data-emvb-field="sources">
|
|
87
|
+
<label className="emvb-field-label" htmlFor={id}>
|
|
88
|
+
Extra sizes (srcset)
|
|
89
|
+
</label>
|
|
90
|
+
<textarea
|
|
91
|
+
id={id}
|
|
92
|
+
className={`${FIELD} emvb-textarea`}
|
|
93
|
+
rows={3}
|
|
94
|
+
placeholder={"/photo-480.jpg 480\n/photo-1600.jpg 1600"}
|
|
95
|
+
aria-invalid={error ? true : undefined}
|
|
96
|
+
aria-describedby={`${id}-help`}
|
|
97
|
+
value={draft}
|
|
98
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
99
|
+
onBlur={commit}
|
|
100
|
+
/>
|
|
101
|
+
<span
|
|
102
|
+
className="emvb-helper"
|
|
103
|
+
id={`${id}-help`}
|
|
104
|
+
data-emvb-sources-error={error ? "" : undefined}
|
|
105
|
+
>
|
|
106
|
+
{error ??
|
|
107
|
+
"One per line: the URL of a smaller or larger copy, a space, then its width in px. Set Width (px) so the main image is listed too."}
|
|
108
|
+
</span>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
@@ -5,6 +5,7 @@ import type { Fetcher } from "../../../api.ts";
|
|
|
5
5
|
import {
|
|
6
6
|
listImages,
|
|
7
7
|
propsFromMedia,
|
|
8
|
+
propsFromUrl,
|
|
8
9
|
uploadImage,
|
|
9
10
|
type MediaLibraryItem,
|
|
10
11
|
} from "../../../media-api.ts";
|
|
@@ -103,9 +104,7 @@ export function MediaPicker({ node, fetcher, onChange, label = "Image" }: Props)
|
|
|
103
104
|
}
|
|
104
105
|
setError(null);
|
|
105
106
|
if (safe === src) return;
|
|
106
|
-
|
|
107
|
-
delete next.mediaId;
|
|
108
|
-
onChange(withProps(node, next));
|
|
107
|
+
onChange(withProps(node, propsFromUrl(propsOf(node), safe)));
|
|
109
108
|
};
|
|
110
109
|
|
|
111
110
|
return (
|