@perkyzz/emvb 0.1.0 → 0.2.1

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.
Files changed (154) hide show
  1. package/README.md +6 -4
  2. package/THIRD_PARTY_NOTICES.md +32 -0
  3. package/licenses/fontawesome-free.LICENSE +165 -0
  4. package/licenses/lucide-static.LICENSE +43 -0
  5. package/licenses/remixicon.LICENSE +201 -0
  6. package/licenses/tabler-icons.LICENSE +21 -0
  7. package/package.json +9 -5
  8. package/src/admin/api.ts +27 -0
  9. package/src/admin/content-api.ts +72 -9
  10. package/src/admin/editor/Editor.tsx +89 -14
  11. package/src/admin/editor/EditorOverlay.tsx +12 -0
  12. package/src/admin/editor/SmallScreenNotice.tsx +4 -3
  13. package/src/admin/editor/TopBar.tsx +76 -12
  14. package/src/admin/editor/canvas/CanvasFrame.tsx +224 -43
  15. package/src/admin/editor/canvas/DeviceBar.tsx +43 -2
  16. package/src/admin/editor/canvas/SelectionOverlay.tsx +60 -6
  17. package/src/admin/editor/canvas/canvas-events.ts +20 -7
  18. package/src/admin/editor/canvas/tab-reveal.ts +66 -0
  19. package/src/admin/editor/canvas/vnode-react.tsx +21 -1
  20. package/src/admin/editor/dialogs.tsx +39 -0
  21. package/src/admin/editor/dnd/drop-target.ts +63 -16
  22. package/src/admin/editor/dnd/new-element.ts +76 -1
  23. package/src/admin/editor/editor-css.ts +101 -15
  24. package/src/admin/editor/host.ts +30 -0
  25. package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
  26. package/src/admin/editor/icons/library.ts +243 -0
  27. package/src/admin/editor/icons/set-format.ts +29 -0
  28. package/src/admin/editor/icons/sets/fontawesome.json +1 -0
  29. package/src/admin/editor/icons/sets/lucide.json +1 -0
  30. package/src/admin/editor/icons/sets/remix.json +1 -0
  31. package/src/admin/editor/icons/sets/tabler.json +1 -0
  32. package/src/admin/editor/panels/AddPanel.tsx +7 -3
  33. package/src/admin/editor/panels/ClassesSection.tsx +44 -6
  34. package/src/admin/editor/panels/ColorControl.tsx +268 -21
  35. package/src/admin/editor/panels/ElementPanel.tsx +237 -93
  36. package/src/admin/editor/panels/LayersPanel.tsx +303 -66
  37. package/src/admin/editor/panels/LeftPanel.tsx +4 -0
  38. package/src/admin/editor/panels/PageSettings.tsx +134 -5
  39. package/src/admin/editor/panels/SettingsPanel.tsx +12 -0
  40. package/src/admin/editor/panels/SiteStylesDrawer.tsx +158 -2
  41. package/src/admin/editor/panels/TriggersEditor.tsx +19 -2
  42. package/src/admin/editor/panels/VariableSection.tsx +47 -9
  43. package/src/admin/editor/panels/color-space.ts +195 -0
  44. package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
  45. package/src/admin/editor/panels/settings/BackgroundControls.tsx +153 -55
  46. package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
  47. package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
  48. package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
  49. package/src/admin/editor/panels/settings/ClassChipInput.tsx +104 -2
  50. package/src/admin/editor/panels/settings/DraftTextField.tsx +44 -3
  51. package/src/admin/editor/panels/settings/EffectsControls.tsx +25 -16
  52. package/src/admin/editor/panels/settings/FieldBindControl.tsx +53 -17
  53. package/src/admin/editor/panels/settings/FieldControl.tsx +129 -9
  54. package/src/admin/editor/panels/settings/FormBindControl.tsx +14 -5
  55. package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
  56. package/src/admin/editor/panels/settings/IconPicker.tsx +133 -72
  57. package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
  58. package/src/admin/editor/panels/settings/LengthRow.tsx +97 -61
  59. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +10 -4
  60. package/src/admin/editor/panels/settings/MediaPicker.tsx +4 -2
  61. package/src/admin/editor/panels/settings/StateSwitcher.tsx +1 -1
  62. package/src/admin/editor/panels/settings/StyleRow.tsx +158 -75
  63. package/src/admin/editor/panels/settings/VariableBinding.tsx +5 -1
  64. package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
  65. package/src/admin/editor/panels/settings/background-mode.ts +112 -0
  66. package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
  67. package/src/admin/editor/panels/settings/box-sides.ts +153 -0
  68. package/src/admin/editor/panels/settings/button-url.ts +27 -0
  69. package/src/admin/editor/panels/settings/class-suggest.ts +5 -3
  70. package/src/admin/editor/panels/settings/form-names.ts +38 -0
  71. package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
  72. package/src/admin/editor/panels/settings/image-alt.ts +25 -0
  73. package/src/admin/editor/panels/settings/length-units.ts +40 -7
  74. package/src/admin/editor/panels/settings/loop-context.ts +26 -0
  75. package/src/admin/editor/panels/settings/style-sections.ts +114 -11
  76. package/src/admin/editor/panels/settings/svg-images.ts +14 -0
  77. package/src/admin/editor/panels/site-list.tsx +3 -0
  78. package/src/admin/editor/restore-deleted.ts +80 -0
  79. package/src/admin/editor/shortcuts.ts +1 -1
  80. package/src/admin/editor/store.ts +35 -7
  81. package/src/admin/editor/useEditorCommands.ts +66 -5
  82. package/src/admin/editor/useEditorData.ts +29 -3
  83. package/src/admin/editor/useEditorShortcuts.ts +34 -2
  84. package/src/admin/editor/useNodeActions.ts +68 -6
  85. package/src/admin/editor/useSave.ts +60 -3
  86. package/src/admin/entry-status.ts +19 -0
  87. package/src/admin/icon-uploads-api.ts +44 -0
  88. package/src/admin/pages/PageList.tsx +22 -2
  89. package/src/admin/pages/ThemePartList.tsx +23 -4
  90. package/src/admin/pages/list-kit.tsx +52 -2
  91. package/src/admin/setup/spec.ts +7 -0
  92. package/src/admin/theme-api.ts +61 -14
  93. package/src/admin/ui.ts +17 -4
  94. package/src/astro/EmVBPage.astro +28 -2
  95. package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
  96. package/src/astro/floats/index.ts +1 -0
  97. package/src/astro/floats/runtime.ts +78 -0
  98. package/src/astro/forms-definitions.ts +22 -0
  99. package/src/astro/index.ts +1 -0
  100. package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
  101. package/src/astro/menu/index.ts +1 -0
  102. package/src/astro/menu/runtime.ts +37 -0
  103. package/src/astro/popups/runtime.ts +54 -4
  104. package/src/astro/render.ts +47 -13
  105. package/src/astro/resolve-theme.ts +168 -24
  106. package/src/astro/tabs/runtime.ts +6 -1
  107. package/src/astro/theme-context.ts +25 -5
  108. package/src/astro/theme-posts.ts +19 -1
  109. package/src/constants.ts +1 -1
  110. package/src/core/arrange.ts +98 -10
  111. package/src/core/clipboard.ts +91 -9
  112. package/src/core/css/generate.ts +21 -4
  113. package/src/core/design/classes.ts +95 -9
  114. package/src/core/design/transfer.ts +101 -2
  115. package/src/core/design/variables.ts +51 -11
  116. package/src/core/elements/dynamic-elements.ts +57 -5
  117. package/src/core/elements/field-id.ts +6 -0
  118. package/src/core/elements/form-elements.ts +23 -8
  119. package/src/core/elements/index.ts +267 -49
  120. package/src/core/elements/menu.ts +190 -0
  121. package/src/core/forms/definition.ts +11 -0
  122. package/src/core/index.ts +67 -3
  123. package/src/core/limits.ts +2 -0
  124. package/src/core/migrate/index.ts +51 -1
  125. package/src/core/nesting.ts +51 -0
  126. package/src/core/render/context.ts +7 -0
  127. package/src/core/render/dynamic-post.ts +39 -11
  128. package/src/core/render/extras.ts +22 -3
  129. package/src/core/render/form.ts +134 -36
  130. package/src/core/render/index.ts +123 -25
  131. package/src/core/render/loop.ts +10 -3
  132. package/src/core/render/pagination.ts +76 -0
  133. package/src/core/render/section.ts +5 -1
  134. package/src/core/render/tabs.ts +8 -4
  135. package/src/core/render/vnode.ts +1 -1
  136. package/src/core/sanitize/css.ts +197 -17
  137. package/src/core/sanitize/embed-url.ts +64 -7
  138. package/src/core/sanitize/svg-upload.ts +203 -0
  139. package/src/core/sanitize/svg.ts +102 -21
  140. package/src/core/schema/descriptors.ts +4 -0
  141. package/src/core/schema/design.ts +7 -2
  142. package/src/core/schema/layout.ts +210 -19
  143. package/src/core/schema/style.ts +87 -8
  144. package/src/core/tabs/presence.ts +9 -0
  145. package/src/core/theme/conditions.ts +4 -1
  146. package/src/core/theme/dynamic.ts +10 -0
  147. package/src/core/theme/float.ts +113 -0
  148. package/src/core/theme/pagination.ts +97 -0
  149. package/src/core/theme/part-types.ts +2 -0
  150. package/src/core/theme/popup-markup.ts +3 -1
  151. package/src/core/theme/popup-rules.ts +4 -3
  152. package/src/server/hooks.ts +20 -0
  153. package/src/server/icon-routes.ts +79 -0
  154. package/src/server/plugin.ts +5 -1
@@ -1,3 +1,4 @@
1
+ import { Button, Input } from "@cloudflare/kumo";
1
2
  import { MapPinIcon, PlusIcon, TagSimpleIcon } from "@phosphor-icons/react";
2
3
  import * as React from "react";
3
4
  import {
@@ -7,9 +8,12 @@ import {
7
8
  moveClassId,
8
9
  removeClassId,
9
10
  renameClass,
11
+ classNameTaken,
12
+ classNameTakenMessage,
10
13
  replaceClassId,
11
14
  type DesignSystem,
12
15
  } from "../../../../core/index.ts";
16
+ import { BUTTON, FIELD } from "../../../ui.ts";
13
17
  import { uniqueId } from "../site-list.tsx";
14
18
  import { ClassChip, stopEditorShortcuts } from "./ClassChip.tsx";
15
19
  import {
@@ -28,13 +32,18 @@ type Props = {
28
32
  onEdit: (classId: string | null) => void;
29
33
  onChange: (classes: string[] | undefined) => void;
30
34
  onDesignChange: (design: DesignSystem) => Promise<void>;
35
+ /**
36
+ * Moves this element's local styles into a new class named `name` (W-134). Absent when the
37
+ * element has no local styles or a class is being edited. Throws with the reason it can't.
38
+ */
39
+ onSaveLocal?: (name: string) => Promise<void>;
31
40
  };
32
41
 
33
42
  const optionKey = (option: ClassOption) =>
34
43
  option.kind === "class" ? option.cls.id : `create-${option.name}`;
35
44
 
36
45
  /**
37
- * The Style tab's Classes field (W-087, DESIGN.md "class chip input"): a Local chip, one chip per
46
+ * The Style tab's Classes field (W-087): a Local chip, one chip per
38
47
  * applied class in cascade order, and a combobox that suggests classes or creates one.
39
48
  */
40
49
  export function ClassChipInput({
@@ -44,6 +53,7 @@ export function ClassChipInput({
44
53
  onEdit,
45
54
  onChange,
46
55
  onDesignChange,
56
+ onSaveLocal,
47
57
  }: Props) {
48
58
  const ids = applied ?? [];
49
59
  const catalog = design.classes ?? [];
@@ -55,6 +65,9 @@ export function ClassChipInput({
55
65
  const [active, setActive] = React.useState(0);
56
66
  const [error, setError] = React.useState<string | null>(null);
57
67
  const [focusAt, setFocusAt] = React.useState<{ index: number } | null>(null);
68
+ const [naming, setNaming] = React.useState(false);
69
+ const [localName, setLocalName] = React.useState("");
70
+ const [savedNote, setSavedNote] = React.useState<string | null>(null);
58
71
  const chipRefs = React.useRef<Array<HTMLButtonElement | null>>([]);
59
72
  const inputRef = React.useRef<HTMLInputElement>(null);
60
73
 
@@ -120,7 +133,13 @@ export function ClassChipInput({
120
133
  }
121
134
  };
122
135
 
123
- const rename = (id: string, name: string) => void saveDesign(renameClass(design, id, name));
136
+ const rename = (id: string, name: string) => {
137
+ if (classNameTaken(design, name, id)) {
138
+ setError(classNameTakenMessage(name));
139
+ return;
140
+ }
141
+ void saveDesign(renameClass(design, id, name));
142
+ };
124
143
 
125
144
  /** Copies the class and swaps the copy in on this element only, ready to edit. */
126
145
  const duplicate = async (id: string) => {
@@ -131,6 +150,30 @@ export function ClassChipInput({
131
150
  onEdit(copy.id);
132
151
  };
133
152
 
153
+ const closeNaming = () => {
154
+ setNaming(false);
155
+ setLocalName("");
156
+ };
157
+
158
+ /** "Save local styles as class" (W-134): one page edit, so one Ctrl+Z puts them back. */
159
+ const saveLocal = async () => {
160
+ const name = localName.trim();
161
+ if (!name || !onSaveLocal) return;
162
+ if (catalog.some((cls) => cls.name.toLowerCase() === name.toLowerCase())) {
163
+ setError(`A class named "${name}" already exists. Pick another name.`);
164
+ return;
165
+ }
166
+ setError(null);
167
+ try {
168
+ await onSaveLocal(name);
169
+ } catch (err) {
170
+ setError(err instanceof Error ? err.message : "Couldn't save the class. Try again.");
171
+ return;
172
+ }
173
+ closeNaming();
174
+ setSavedNote(`Saved as class "${name}". Publish site styles to show it on the site.`);
175
+ };
176
+
134
177
  const reorder = (index: number, delta: -1 | 1) => commit(moveClassId(ids, index, delta));
135
178
 
136
179
  const remove = (id: string, focusIndex: number) => {
@@ -334,6 +377,65 @@ export function ClassChipInput({
334
377
  </button>
335
378
  </p>
336
379
  )}
380
+ {onSaveLocal && editing === null && !naming && (
381
+ <button
382
+ type="button"
383
+ className="emvb-link-button emvb-save-local"
384
+ data-emvb-save-local=""
385
+ onClick={() => {
386
+ setNaming(true);
387
+ setSavedNote(null);
388
+ setError(null);
389
+ }}
390
+ >
391
+ Save local styles as class
392
+ </button>
393
+ )}
394
+ {naming && (
395
+ <div
396
+ className="emvb-save-local-row"
397
+ data-emvb-save-local-row=""
398
+ onKeyDown={stopEditorShortcuts}
399
+ >
400
+ <Input
401
+ label="New class name"
402
+ className={FIELD}
403
+ value={localName}
404
+ maxLength={60}
405
+ // oxlint-disable-next-line jsx-a11y/no-autofocus -- the user asked to name the class; focus moves into the field
406
+ autoFocus
407
+ onChange={(event) => {
408
+ setLocalName(event.target.value);
409
+ setError(null);
410
+ }}
411
+ onKeyDown={(event) => {
412
+ if (event.key === "Enter") {
413
+ event.preventDefault();
414
+ void saveLocal();
415
+ } else if (event.key === "Escape") closeNaming();
416
+ }}
417
+ />
418
+ <div className="emvb-save-local-actions">
419
+ <Button
420
+ type="button"
421
+ variant="primary"
422
+ className={BUTTON}
423
+ disabled={localName.trim() === ""}
424
+ onClick={() => void saveLocal()}
425
+ >
426
+ Save as class
427
+ </Button>
428
+ <Button type="button" variant="ghost" className={BUTTON} onClick={closeNaming}>
429
+ Cancel
430
+ </Button>
431
+ </div>
432
+ </div>
433
+ )}
434
+ {savedNote && (
435
+ <p className="emvb-helper" role="status" data-emvb-save-local-done="">
436
+ {savedNote}
437
+ </p>
438
+ )}
337
439
  {error && (
338
440
  <p className="emvb-inline-error" role="alert" data-emvb-class-error="">
339
441
  {error}
@@ -2,7 +2,9 @@ import { Input } from "@cloudflare/kumo";
2
2
  import * as React from "react";
3
3
  import {
4
4
  MAX_TEXT_LENGTH,
5
+ LAYOUT_SCHEMA_VERSION,
5
6
  sanitizeHref,
7
+ validateLayout,
6
8
  type FieldDescriptor,
7
9
  type LayoutNode,
8
10
  } from "../../../../core/index.ts";
@@ -65,6 +67,8 @@ export function DraftTextField({
65
67
  const raw = props[field.key];
66
68
  const [draft, setDraft] = React.useState(String(raw ?? ""));
67
69
  const [error, setError] = React.useState<string | null>(null);
70
+ // The textarea's label must name it, for screen readers and click-to-focus (W-124).
71
+ const areaId = React.useId();
68
72
 
69
73
  React.useEffect(() => {
70
74
  if (field.kind === "number" && raw && typeof raw === "object" && "value" in raw) {
@@ -98,8 +102,13 @@ export function DraftTextField({
98
102
  };
99
103
  return (
100
104
  <div className="emvb-field-group" data-emvb-field={field.key}>
101
- <label className="emvb-field-label">{field.label}</label>
105
+ <label className="emvb-field-label" htmlFor={areaId}>
106
+ {field.label}
107
+ </label>
102
108
  <textarea
109
+ id={areaId}
110
+ // W-272: right-to-left text (Arabic, Hebrew) lines up from the right as typed.
111
+ dir="auto"
103
112
  className={`${FIELD} emvb-textarea`}
104
113
  rows={field.kind === "list-items" ? 4 : 5}
105
114
  value={draft}
@@ -132,6 +141,11 @@ export function DraftTextField({
132
141
  );
133
142
  return;
134
143
  }
144
+ // A whole-number field past its schema's maximum would fail the save (W-223).
145
+ if (integer && field.max !== undefined && value > field.max) {
146
+ setError(field.message ?? `Enter a whole number from 1 to ${field.max}.`);
147
+ return;
148
+ }
135
149
  if (value > MAX_FIELD_NUMBER) {
136
150
  setError(`Enter a number up to ${MAX_FIELD_NUMBER}.`);
137
151
  return;
@@ -185,6 +199,8 @@ export function DraftTextField({
185
199
  <Input
186
200
  label={field.label}
187
201
  className={FIELD}
202
+ // W-272: the field follows its text's direction, so RTL text reads from the right.
203
+ dir="auto"
188
204
  value={draft}
189
205
  error={
190
206
  tooLong
@@ -193,9 +209,34 @@ export function DraftTextField({
193
209
  }
194
210
  data-emvb-field={field.key}
195
211
  onChange={(event) => {
196
- setDraft(event.target.value);
197
- onChange(withProp(node, field.key, event.target.value));
212
+ const next = event.target.value;
213
+ setDraft(next);
214
+ if (next.trim() === "" && field.optional) {
215
+ setError(null);
216
+ onChange(withProp(node, field.key, undefined));
217
+ return;
218
+ }
219
+ // A value that would make a savable element unsavable (an emptied required field, an
220
+ // overlong one) stays in the box with its error and is not stored (W-192).
221
+ const candidate = withProp(node, field.key, next);
222
+ if (next.length > MAX_TEXT_LENGTH) return;
223
+ if (savableNode(node) && !savableNode(candidate)) {
224
+ setError(
225
+ next.trim() === "" ? `${field.label} can't be empty.` : "This value can't be saved.",
226
+ );
227
+ return;
228
+ }
229
+ setError(null);
230
+ onChange(candidate);
198
231
  }}
199
232
  />
200
233
  );
201
234
  }
235
+
236
+ /** Whether `node` passes the layout schema on its own (inside a bare container). */
237
+ function savableNode(node: LayoutNode): boolean {
238
+ return validateLayout({
239
+ schemaVersion: LAYOUT_SCHEMA_VERSION,
240
+ root: { id: "draftroot", type: "container", props: {}, children: [node] },
241
+ }).ok;
242
+ }
@@ -1,4 +1,4 @@
1
- import { Button, Checkbox, Select } from "@cloudflare/kumo";
1
+ import { Button, Select } from "@cloudflare/kumo";
2
2
  import { PlusIcon } from "@phosphor-icons/react";
3
3
  import type * as React from "react";
4
4
  import type { DesignSystem, StyleProps } from "../../../../core/index.ts";
@@ -11,15 +11,15 @@ type Filter = NonNullable<StyleProps["filter"]>;
11
11
  type Transition = NonNullable<StyleProps["transition"]>;
12
12
  type Entrance = NonNullable<StyleProps["entrance"]>;
13
13
 
14
- /** DESIGN.md: Add shadow starts at 0 4 12 0, 18 % black. */
14
+ /** Add shadow starts at 0 4 12 0, 18 % black. */
15
15
  const NEW_SHADOW: Shadow = { x: 0, y: 4, blur: 12, spread: 0, color: "#0000002e" };
16
16
 
17
17
  const OFFSET: NumberSpec = { min: -1000, max: 1000, example: "4" };
18
18
  const SHADOW_FIELDS: ReadonlyArray<
19
19
  [key: "x" | "y" | "blur" | "spread", label: string, NumberSpec]
20
20
  > = [
21
- ["x", "X", OFFSET],
22
- ["y", "Y", OFFSET],
21
+ ["x", "Horizontal", OFFSET],
22
+ ["y", "Vertical", OFFSET],
23
23
  ["blur", "Blur", { min: 0, max: 1000, example: "12" }],
24
24
  ["spread", "Spread", OFFSET],
25
25
  ];
@@ -54,6 +54,27 @@ export function ShadowControl({
54
54
  <span className="emvb-var-field-label">Box shadow</span>
55
55
  {value ? (
56
56
  <>
57
+ <ColorControl
58
+ label="Shadow color"
59
+ value={value.color}
60
+ design={design}
61
+ onChange={(color) => patch({ color })}
62
+ onDesignChange={onDesignChange}
63
+ />
64
+ <Select
65
+ label="Position"
66
+ className={FIELD}
67
+ value={value.inset === true ? "inset" : "outset"}
68
+ // W-306: Kumo's Select shows the raw value ("inset") without a renderValue.
69
+ renderValue={(v: unknown) => (v === "inset" ? "Inset" : "Outset")}
70
+ onValueChange={(next) => {
71
+ const checked = next === "inset";
72
+ patch({ inset: checked === true ? true : undefined });
73
+ }}
74
+ >
75
+ <Select.Option value="outset">Outset</Select.Option>
76
+ <Select.Option value="inset">Inset</Select.Option>
77
+ </Select>
57
78
  <div className="emvb-shadow-grid">
58
79
  {SHADOW_FIELDS.map(([key, label, spec]) => (
59
80
  <NumberField
@@ -66,18 +87,6 @@ export function ShadowControl({
66
87
  />
67
88
  ))}
68
89
  </div>
69
- <ColorControl
70
- label="Shadow color"
71
- value={value.color}
72
- design={design}
73
- onChange={(color) => patch({ color })}
74
- onDesignChange={onDesignChange}
75
- />
76
- <Checkbox
77
- label="Inset"
78
- checked={value.inset === true}
79
- onCheckedChange={(checked) => patch({ inset: checked === true ? true : undefined })}
80
- />
81
90
  </>
82
91
  ) : (
83
92
  <Button
@@ -2,11 +2,59 @@ import { Input, Select } from "@cloudflare/kumo";
2
2
  import * as React from "react";
3
3
  import type { Fetcher } from "../../../api.ts";
4
4
  import { loadFormFields, type DefinitionField } from "../../../forms-api.ts";
5
+ import { FIELD_NAME_PATTERN, MAX_FIELD_NAME } from "../../../../core/index.ts";
5
6
  import { FIELD } from "../../../ui.ts";
6
7
 
7
8
  const MANUAL = "__manual__";
8
9
  const CHOOSE = "Choose a field…";
9
10
 
11
+ /** Why a typed field name can't be stored, or null (W-202: same rule as save). */
12
+ export function fieldNameError(name: string): string | null {
13
+ if (!name) return "Give the field a name.";
14
+ if (name.length > MAX_FIELD_NAME) return `Use at most ${MAX_FIELD_NAME} characters.`;
15
+ if (!FIELD_NAME_PATTERN.test(name))
16
+ return "Start with a letter, then use letters, digits, - or _ (no spaces).";
17
+ return null;
18
+ }
19
+
20
+ /** The Field name box: keeps what you type, stores it only once it's a valid name (W-202). */
21
+ function FieldNameInput({
22
+ value,
23
+ onChange,
24
+ manual,
25
+ }: {
26
+ value: string;
27
+ onChange: (name: string) => void;
28
+ manual?: boolean;
29
+ }) {
30
+ const [draft, setDraft] = React.useState(value);
31
+ React.useEffect(() => {
32
+ setDraft((current) => (fieldNameError(current.trim()) === null ? value : current));
33
+ }, [value]);
34
+ const error = fieldNameError(draft.trim());
35
+ return (
36
+ <>
37
+ <Input
38
+ label="Field name"
39
+ className={`${FIELD} emvb-mono`}
40
+ value={draft}
41
+ aria-invalid={error ? true : undefined}
42
+ {...(manual ? { "data-emvb-field-bind": "manual" } : {})}
43
+ onChange={(event) => {
44
+ const next = event.target.value;
45
+ setDraft(next);
46
+ if (fieldNameError(next.trim()) === null) onChange(next.trim());
47
+ }}
48
+ />
49
+ {error && (
50
+ <p className="emvb-inline-error" data-emvb-field-name-error>
51
+ {error}
52
+ </p>
53
+ )}
54
+ </>
55
+ );
56
+ }
57
+
10
58
  const fieldLabel = (field: DefinitionField) => `${field.label}${field.required ? " *" : ""}`;
11
59
 
12
60
  /** Field name binder: options from public definition when the parent form id is known. */
@@ -19,7 +67,7 @@ export function FieldBindControl({
19
67
  value: string;
20
68
  formId: string | undefined;
21
69
  fetcher: Fetcher;
22
- onChange: (field: string) => void;
70
+ onChange: (field: string, label?: string) => void;
23
71
  }) {
24
72
  const [fields, setFields] = React.useState<DefinitionField[] | null>(null);
25
73
 
@@ -47,7 +95,8 @@ export function FieldBindControl({
47
95
  value={known ? value : MANUAL}
48
96
  onValueChange={(next) => {
49
97
  if (!next || next === MANUAL) return;
50
- onChange(next);
98
+ const picked = fields.find((each) => each.name === next);
99
+ onChange(next, picked?.label);
51
100
  }}
52
101
  renderValue={(current: unknown) => {
53
102
  const field = fields.find((each) => each.name === current);
@@ -61,23 +110,10 @@ export function FieldBindControl({
61
110
  </Select.Option>
62
111
  ))}
63
112
  </Select>
64
- <Input
65
- label="Field name"
66
- className={`${FIELD} emvb-mono`}
67
- value={value}
68
- onChange={(event) => onChange(event.target.value.trim())}
69
- />
113
+ <FieldNameInput value={value} onChange={(name) => onChange(name)} />
70
114
  </div>
71
115
  );
72
116
  }
73
117
 
74
- return (
75
- <Input
76
- label="Field name"
77
- className={`${FIELD} emvb-mono`}
78
- value={value}
79
- data-emvb-field-bind="manual"
80
- onChange={(event) => onChange(event.target.value.trim())}
81
- />
82
- );
118
+ return <FieldNameInput value={value} onChange={(name) => onChange(name)} manual />;
83
119
  }
@@ -1,3 +1,4 @@
1
+ import * as React from "react";
1
2
  import { Select, Switch } from "@cloudflare/kumo";
2
3
  import type { FieldDescriptor, LayoutNode } from "../../../../core/index.ts";
3
4
  import type { Fetcher } from "../../../api.ts";
@@ -17,14 +18,20 @@ export function FieldControl({
17
18
  onChange,
18
19
  fetcher,
19
20
  parentFormId,
21
+ device = "desktop",
22
+ duplicateName = false,
20
23
  }: {
21
24
  field: FieldDescriptor;
22
25
  node: LayoutNode;
26
+ /** The canvas device: a field with `devices` edits that device's value (W-139). */
27
+ device?: "desktop" | "tablet" | "mobile";
23
28
  onChange: (node: LayoutNode) => void;
24
29
  /** Required for media library / upload fields (W-024). */
25
30
  fetcher?: Fetcher;
26
31
  /** Enclosing form's formId for field pickers (W-036). */
27
32
  parentFormId?: string;
33
+ /** Another control in the same form uses this field name (W-194). */
34
+ duplicateName?: boolean;
28
35
  }) {
29
36
  const props = propsOf(node);
30
37
  const raw = props[field.key];
@@ -58,7 +65,7 @@ export function FieldControl({
58
65
  value={typeof raw === "string" ? raw : ""}
59
66
  fetcher={fetcher}
60
67
  partType="section"
61
- label="Section"
68
+ label="Section part"
62
69
  emptyLabel="Inline section"
63
70
  marker="section"
64
71
  onChange={(partId) => onChange(withProp(node, "partId", partId || undefined))}
@@ -68,18 +75,29 @@ export function FieldControl({
68
75
 
69
76
  if (field.key === "field" && fetcher) {
70
77
  return (
71
- <FieldBindControl
72
- key={field.key}
73
- value={typeof raw === "string" ? raw : ""}
74
- formId={parentFormId}
75
- fetcher={fetcher}
76
- onChange={(name) => onChange(withProp(node, "field", name))}
77
- />
78
+ <React.Fragment key={field.key}>
79
+ <FieldBindControl
80
+ key={field.key}
81
+ value={typeof raw === "string" ? raw : ""}
82
+ formId={parentFormId}
83
+ fetcher={fetcher}
84
+ onChange={(name, label) => {
85
+ const next = withProp(node, "field", name);
86
+ onChange(label ? withProp(next, "label", label) : next);
87
+ }}
88
+ />
89
+ {duplicateName && (
90
+ <p className="emvb-inline-error" role="alert" data-emvb-duplicate-field>
91
+ Another field in this form uses this name, so only one value is submitted. Give each
92
+ field its own name.
93
+ </p>
94
+ )}
95
+ </React.Fragment>
78
96
  );
79
97
  }
80
98
 
81
99
  if (field.kind === "icon") {
82
- return <IconPicker key={field.key} node={node} onChange={onChange} />;
100
+ return <IconPicker key={field.key} node={node} fetcher={fetcher} onChange={onChange} />;
83
101
  }
84
102
 
85
103
  if (field.kind === "media") {
@@ -105,6 +123,18 @@ export function FieldControl({
105
123
  );
106
124
  }
107
125
 
126
+ if (field.kind === "select" && field.options && field.devices) {
127
+ return (
128
+ <DeviceSelectField
129
+ field={field}
130
+ devices={field.devices}
131
+ node={node}
132
+ device={device}
133
+ onChange={onChange}
134
+ />
135
+ );
136
+ }
137
+
108
138
  if (field.kind === "select" && field.options) {
109
139
  const value = raw ?? field.options[0]?.value ?? "";
110
140
  return (
@@ -135,6 +165,96 @@ export function FieldControl({
135
165
  return <DraftTextField field={field} node={node} onChange={onChange} />;
136
166
  }
137
167
 
168
+ const DEVICE_NAMES = { tablet: "tablet", mobile: "mobile" } as const;
169
+
170
+ /**
171
+ * A select with tablet and mobile values (W-139): on Desktop it edits the field and lists the
172
+ * device values under it; on Tablet or Mobile it edits that device's value, where the first
173
+ * option follows the next wider device.
174
+ */
175
+ function DeviceSelectField({
176
+ field,
177
+ devices,
178
+ node,
179
+ device,
180
+ onChange,
181
+ }: {
182
+ field: FieldDescriptor;
183
+ devices: { tablet: string; mobile: string };
184
+ node: LayoutNode;
185
+ device: "desktop" | "tablet" | "mobile";
186
+ onChange: (node: LayoutNode) => void;
187
+ }) {
188
+ const props = propsOf(node);
189
+ const options = field.options ?? [];
190
+ const labelOf = (value: unknown) =>
191
+ options.find((o) => String(o.value) === String(value))?.label ?? String(value);
192
+ const desktop = props[field.key] ?? options[0]?.value;
193
+ const tablet = props[devices.tablet];
194
+ const mobile = props[devices.mobile];
195
+ if (device === "desktop") {
196
+ const notes = [
197
+ tablet !== undefined ? `Tablet ${labelOf(tablet)}` : null,
198
+ mobile !== undefined ? `Mobile ${labelOf(mobile)}` : null,
199
+ ].filter((note): note is string => note !== null);
200
+ return (
201
+ <div data-emvb-field={field.key}>
202
+ <Select
203
+ label={field.label}
204
+ className={FIELD}
205
+ value={String(desktop ?? "")}
206
+ onValueChange={(next) => {
207
+ const option = options.find((o) => String(o.value) === String(next));
208
+ onChange(withProp(node, field.key, option ? option.value : next));
209
+ }}
210
+ renderValue={(current: unknown) => labelOf(current)}
211
+ >
212
+ {options.map((option) => (
213
+ <Select.Option key={String(option.value)} value={String(option.value)}>
214
+ {option.label}
215
+ </Select.Option>
216
+ ))}
217
+ </Select>
218
+ {notes.length > 0 && (
219
+ <p className="emvb-helper" data-emvb-device-values={field.key}>
220
+ {notes.join(" · ")}. Switch the canvas to Tablet or Mobile to change them.
221
+ </p>
222
+ )}
223
+ </div>
224
+ );
225
+ }
226
+ const key = devices[device];
227
+ const own = props[key];
228
+ const wider = device === "mobile" && tablet !== undefined ? tablet : desktop;
229
+ const SAME = "same";
230
+ const sameLabel = `Same as ${device === "mobile" && tablet !== undefined ? "tablet" : "desktop"} (${labelOf(wider)})`;
231
+ return (
232
+ <div data-emvb-field={key}>
233
+ <Select
234
+ label={`${field.label} on ${DEVICE_NAMES[device]}`}
235
+ className={FIELD}
236
+ value={own === undefined ? SAME : String(own)}
237
+ onValueChange={(next) => {
238
+ if (next === SAME) {
239
+ onChange(withProp(node, key, undefined));
240
+ return;
241
+ }
242
+ const option = options.find((o) => String(o.value) === String(next));
243
+ onChange(withProp(node, key, option ? option.value : next));
244
+ }}
245
+ renderValue={(current: unknown) => (current === SAME ? sameLabel : labelOf(current))}
246
+ >
247
+ <Select.Option value={SAME}>{sameLabel}</Select.Option>
248
+ {options.map((option) => (
249
+ <Select.Option key={String(option.value)} value={String(option.value)}>
250
+ {option.label}
251
+ </Select.Option>
252
+ ))}
253
+ </Select>
254
+ </div>
255
+ );
256
+ }
257
+
138
258
  /** Field kinds the panel implements — coverage tests assert this stays complete. */
139
259
  export const IMPLEMENTED_FIELD_KINDS = [
140
260
  "text",
@@ -7,6 +7,7 @@ import {
7
7
  type FormsCapability,
8
8
  } from "../../../forms-api.ts";
9
9
  import { FIELD } from "../../../ui.ts";
10
+ import { useAdminLinks } from "../../host.ts";
10
11
 
11
12
  const MANUAL = "__manual__";
12
13
 
@@ -23,6 +24,7 @@ export function FormBindControl({
23
24
  onChange: (formId: string) => void;
24
25
  }) {
25
26
  const [capability, setCapability] = React.useState<FormsCapability | null>(null);
27
+ const adminLinks = useAdminLinks();
26
28
 
27
29
  React.useEffect(() => {
28
30
  let cancelled = false;
@@ -73,9 +75,11 @@ export function FormBindControl({
73
75
  value={value}
74
76
  onChange={(event) => onChange(event.target.value.trim())}
75
77
  />
76
- <p className="emvb-helper">
77
- <a href="/_emdash/admin/plugins/emdash-forms/pages">Create or edit forms</a>
78
- </p>
78
+ {adminLinks && (
79
+ <p className="emvb-helper">
80
+ <a href="/_emdash/admin/plugins/emdash-forms/pages">Create or edit forms</a>
81
+ </p>
82
+ )}
79
83
  </div>
80
84
  );
81
85
  }
@@ -89,8 +93,13 @@ export function FormBindControl({
89
93
  onChange={(event) => onChange(event.target.value.trim())}
90
94
  />
91
95
  <p className="emvb-helper">
92
- Paste a forms-plugin form id.{" "}
93
- <a href="/_emdash/admin/plugins/emdash-forms/pages">Open Forms</a>
96
+ Paste a forms-plugin form id.
97
+ {adminLinks && (
98
+ <>
99
+ {" "}
100
+ <a href="/_emdash/admin/plugins/emdash-forms/pages">Open Forms</a>
101
+ </>
102
+ )}
94
103
  </p>
95
104
  </div>
96
105
  );