@actuate-media/cms-admin 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/dist/__tests__/fields/component-block-helpers.test.d.ts +7 -0
- package/dist/__tests__/fields/component-block-helpers.test.d.ts.map +1 -0
- package/dist/__tests__/fields/component-block-helpers.test.js +592 -0
- package/dist/__tests__/fields/component-block-helpers.test.js.map +1 -0
- package/dist/fields/ComponentBlockField.d.ts +25 -0
- package/dist/fields/ComponentBlockField.d.ts.map +1 -0
- package/dist/fields/ComponentBlockField.js +74 -0
- package/dist/fields/ComponentBlockField.js.map +1 -0
- package/dist/fields/FieldRenderer.d.ts +3 -0
- package/dist/fields/FieldRenderer.d.ts.map +1 -1
- package/dist/fields/FieldRenderer.js +3 -1
- package/dist/fields/FieldRenderer.js.map +1 -1
- package/dist/fields/PropInput.d.ts +14 -0
- package/dist/fields/PropInput.d.ts.map +1 -0
- package/dist/fields/PropInput.js +163 -0
- package/dist/fields/PropInput.js.map +1 -0
- package/dist/fields/component-block-helpers.d.ts +96 -0
- package/dist/fields/component-block-helpers.d.ts.map +1 -0
- package/dist/fields/component-block-helpers.js +323 -0
- package/dist/fields/component-block-helpers.js.map +1 -0
- package/dist/fields/index.d.ts +4 -0
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +2 -0
- package/dist/fields/index.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +10 -3
- package/src/__tests__/fields/component-block-helpers.test.ts +674 -0
- package/src/fields/ComponentBlockField.tsx +179 -0
- package/src/fields/FieldRenderer.tsx +8 -0
- package/src/fields/PropInput.tsx +552 -0
- package/src/fields/component-block-helpers.ts +341 -0
- package/src/fields/index.ts +4 -0
- package/src/index.ts +7 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers behind `<ComponentBlockField>`. Extracted so vitest can
|
|
3
|
+
* exercise them in node without spinning up jsdom — the React component
|
|
4
|
+
* just composes these into UI.
|
|
5
|
+
*
|
|
6
|
+
* Kept structurally typed (no peer-dep import of types here would be
|
|
7
|
+
* cleaner, but the `PropType` discriminated union is the contract we
|
|
8
|
+
* promise consumers, so we lean on the package's types).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
detectDiscriminator,
|
|
13
|
+
findVariant,
|
|
14
|
+
} from '@actuate-media/component-blocks/discriminated-union'
|
|
15
|
+
import type {
|
|
16
|
+
DiscriminatedUnion,
|
|
17
|
+
DiscriminatedVariant,
|
|
18
|
+
} from '@actuate-media/component-blocks/discriminated-union'
|
|
19
|
+
import type { ComponentSpec, Manifest, PropSpec, PropType } from '@actuate-media/component-blocks'
|
|
20
|
+
|
|
21
|
+
import type { ComponentBlockValue } from './ComponentBlockField.js'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Compute the list of components an editor can pick from given the
|
|
25
|
+
* manifest + the optional allow-list. Returns the full list when no
|
|
26
|
+
* filter is provided; returns `[]` when every entry is filtered out
|
|
27
|
+
* (the caller renders an error in that case).
|
|
28
|
+
*/
|
|
29
|
+
export function getAllowedComponents(
|
|
30
|
+
manifest: Manifest,
|
|
31
|
+
allow: string[] | undefined,
|
|
32
|
+
): ComponentSpec[] {
|
|
33
|
+
if (!allow || allow.length === 0) return manifest.components
|
|
34
|
+
return manifest.components.filter((c) => allow.includes(c.name))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* JSON-encode any value so it round-trips cleanly through the
|
|
39
|
+
* JsonFallback textarea. Previously the helper short-circuited for
|
|
40
|
+
* strings (returning the raw string), which made the textarea
|
|
41
|
+
* stateful: typing `"hello"` parsed to the JS string `hello`,
|
|
42
|
+
* re-rendered as bare `hello`, then `JSON.parse('hello')` threw on
|
|
43
|
+
* the next edit and the field "locked" as a raw string. Always
|
|
44
|
+
* stringifying keeps the textarea visually consistent with the
|
|
45
|
+
* underlying JSON value.
|
|
46
|
+
*
|
|
47
|
+
* Falls back to `String(v)` for values that can't be JSON-encoded
|
|
48
|
+
* (e.g. circular refs) so the field remains editable.
|
|
49
|
+
*/
|
|
50
|
+
export function safeJsonStringify(v: unknown): string {
|
|
51
|
+
try {
|
|
52
|
+
return JSON.stringify(v, null, 2)
|
|
53
|
+
} catch {
|
|
54
|
+
return String(v)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Map an `<select>` change-event value to an enum-prop value.
|
|
60
|
+
*
|
|
61
|
+
* The placeholder `<option value="">Select…</option>` clears the field
|
|
62
|
+
* to `undefined`; any other value is the stringified array index into
|
|
63
|
+
* `options`. Extracted so we can test the clear semantics without
|
|
64
|
+
* spinning up jsdom: previously the inline handler used `Number("")`,
|
|
65
|
+
* which evaluates to `0` and silently picked the first option instead
|
|
66
|
+
* of clearing.
|
|
67
|
+
*/
|
|
68
|
+
export function parseEnumSelection<T>(raw: string, options: readonly T[]): T | undefined {
|
|
69
|
+
if (raw === '') return undefined
|
|
70
|
+
const idx = Number(raw)
|
|
71
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= options.length) return undefined
|
|
72
|
+
return options[idx]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Produce a sensible empty value for a freshly-created array item or
|
|
77
|
+
* required prop. The admin form generator never wants `undefined` in
|
|
78
|
+
* an array; that breaks downstream PropInput defaults and confuses
|
|
79
|
+
* validators.
|
|
80
|
+
*/
|
|
81
|
+
export function defaultForType(type: PropType): unknown {
|
|
82
|
+
switch (type.kind) {
|
|
83
|
+
case 'string':
|
|
84
|
+
return ''
|
|
85
|
+
case 'number':
|
|
86
|
+
return 0
|
|
87
|
+
case 'boolean':
|
|
88
|
+
return false
|
|
89
|
+
case 'enum':
|
|
90
|
+
return type.values[0] ?? ''
|
|
91
|
+
case 'literal':
|
|
92
|
+
return type.value
|
|
93
|
+
case 'array':
|
|
94
|
+
return []
|
|
95
|
+
case 'object':
|
|
96
|
+
return Object.fromEntries(
|
|
97
|
+
type.fields.filter((f) => f.required).map((f) => [f.name, defaultForType(f.type)]),
|
|
98
|
+
)
|
|
99
|
+
case 'union': {
|
|
100
|
+
// Discriminated unions DO have a sensible structural default:
|
|
101
|
+
// pick the first variant and seed its required fields (plus the
|
|
102
|
+
// discriminator). This makes a Hero with a `cta: Link | Modal`
|
|
103
|
+
// prop render an editable form on first paint instead of forcing
|
|
104
|
+
// the editor through the JSON textarea fallback.
|
|
105
|
+
const detected = detectDiscriminator(type)
|
|
106
|
+
if (detected) {
|
|
107
|
+
const variant = detected.variants[0]!
|
|
108
|
+
const out: Record<string, unknown> = { [detected.field]: variant.value }
|
|
109
|
+
for (const field of variant.remainingFields) {
|
|
110
|
+
if (field.required) out[field.name] = defaultForType(field.type)
|
|
111
|
+
}
|
|
112
|
+
return out
|
|
113
|
+
}
|
|
114
|
+
return null
|
|
115
|
+
}
|
|
116
|
+
default:
|
|
117
|
+
// `reference`, `unknown`: there's no single sensible structural
|
|
118
|
+
// default. We return `null` instead of `undefined` so the value
|
|
119
|
+
// survives a JSON round-trip — `JSON.stringify` turns
|
|
120
|
+
// `undefined` array items into `null` anyway, which would
|
|
121
|
+
// contradict the documented "never undefined in an array"
|
|
122
|
+
// invariant and surface as the literal string `null` in the
|
|
123
|
+
// JsonFallback textarea.
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Seed an empty props object for a freshly-picked component using each
|
|
130
|
+
* prop's explicit `defaultValue` when set, else the structural default
|
|
131
|
+
* from {@link defaultForType} (only for required props — optional
|
|
132
|
+
* unset props stay absent so they render as empty inputs).
|
|
133
|
+
*/
|
|
134
|
+
export function seedPropsForComponent(spec: ComponentSpec): Record<string, unknown> {
|
|
135
|
+
const out: Record<string, unknown> = {}
|
|
136
|
+
for (const prop of spec.props) {
|
|
137
|
+
if (prop.defaultValue !== undefined && prop.defaultValue !== null) {
|
|
138
|
+
out[prop.name] = prop.defaultValue
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (prop.required) {
|
|
142
|
+
const seed = defaultForType(prop.type)
|
|
143
|
+
// For non-discriminated `union` / `reference` / `unknown` kinds
|
|
144
|
+
// defaultForType returns `null` (so arrays survive a JSON
|
|
145
|
+
// round-trip), but seeding a top-level required prop with
|
|
146
|
+
// `null` would immediately trip the validator's "Missing
|
|
147
|
+
// required prop" branch on first render — the user would see
|
|
148
|
+
// a red error before they touched anything. Leave those
|
|
149
|
+
// required props absent so the absence and the validation
|
|
150
|
+
// message describe the same state honestly; the user provides
|
|
151
|
+
// a value via the JsonFallback. Discriminated unions return
|
|
152
|
+
// a structural default and seed cleanly.
|
|
153
|
+
if (seed !== null) {
|
|
154
|
+
out[prop.name] = seed
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return out
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Compute the next value for a discriminated-union field when the
|
|
163
|
+
* editor switches variants. Pure helper so the React component stays
|
|
164
|
+
* thin and the seeding rules can be unit-tested without rendering.
|
|
165
|
+
*
|
|
166
|
+
* Seeding rules:
|
|
167
|
+
* 1. The discriminator field is always written to the variant's
|
|
168
|
+
* tag value.
|
|
169
|
+
* 2. Optional fields shared by both variants survive the switch
|
|
170
|
+
* (preserves "edited the same label" intent across, e.g.,
|
|
171
|
+
* Link → Modal where both carry `label`).
|
|
172
|
+
* 3. Required fields exclusive to the new variant are seeded by
|
|
173
|
+
* priority: explicit `field.defaultValue` first (matches
|
|
174
|
+
* `seedPropsForComponent`), else the structural default from
|
|
175
|
+
* `defaultForType` — but only when that default is non-null.
|
|
176
|
+
* A `null` from `defaultForType` (reference / unknown / non-
|
|
177
|
+
* discriminated union types) is treated as "no sensible default"
|
|
178
|
+
* and the field is left absent so the validator's
|
|
179
|
+
* "missing required prop" message and the data agree.
|
|
180
|
+
* 4. Fields exclusive to the previous variant are dropped so the
|
|
181
|
+
* value stays structurally valid against the chosen variant.
|
|
182
|
+
*/
|
|
183
|
+
export function switchUnionVariant(
|
|
184
|
+
current: unknown,
|
|
185
|
+
union: DiscriminatedUnion,
|
|
186
|
+
variant: DiscriminatedVariant,
|
|
187
|
+
): Record<string, unknown> {
|
|
188
|
+
const obj = isPlainObject(current) ? current : {}
|
|
189
|
+
const next: Record<string, unknown> = { [union.field]: variant.value }
|
|
190
|
+
const carryable = new Set(variant.remainingFields.map((f) => f.name))
|
|
191
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
192
|
+
if (k === union.field) continue
|
|
193
|
+
if (carryable.has(k)) next[k] = v
|
|
194
|
+
}
|
|
195
|
+
for (const field of variant.remainingFields) {
|
|
196
|
+
if (!field.required || next[field.name] !== undefined) continue
|
|
197
|
+
if (field.defaultValue !== undefined && field.defaultValue !== null) {
|
|
198
|
+
next[field.name] = field.defaultValue
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
const seed = defaultForType(field.type)
|
|
202
|
+
if (seed !== null) {
|
|
203
|
+
next[field.name] = seed
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return next
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
210
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Build a minimal structural validator that mirrors the rules the
|
|
215
|
+
* cms-core `validateComponentBlockValue` runs server-side, so the form
|
|
216
|
+
* can show feedback without a round-trip. Kept in sync with cms-core
|
|
217
|
+
* by inspection; the canonical implementation lives there.
|
|
218
|
+
*/
|
|
219
|
+
export function buildClientValidator(
|
|
220
|
+
manifest: Manifest,
|
|
221
|
+
allow: string[] | undefined,
|
|
222
|
+
): (value: ComponentBlockValue) => string | true {
|
|
223
|
+
// An empty `allow` array is treated the same way `getAllowedComponents`
|
|
224
|
+
// treats it: as "no filter". Without this, the picker shows every
|
|
225
|
+
// component while the validator rejects every selection, leading to
|
|
226
|
+
// a UI/validator deadlock.
|
|
227
|
+
const allowed = allow && allow.length > 0 ? new Set(allow) : null
|
|
228
|
+
return (value) => {
|
|
229
|
+
const spec = manifest.components.find((c) => c.name === value.component)
|
|
230
|
+
if (!spec) {
|
|
231
|
+
const known = manifest.components.map((c) => c.name).join(', ') || '<none>'
|
|
232
|
+
return `Unknown component '${value.component}'. Manifest knows: ${known}.`
|
|
233
|
+
}
|
|
234
|
+
if (allowed && !allowed.has(value.component)) {
|
|
235
|
+
return `Component '${value.component}' is not in the allow-list for this field.`
|
|
236
|
+
}
|
|
237
|
+
for (const propSpec of spec.props) {
|
|
238
|
+
const v = value.props[propSpec.name]
|
|
239
|
+
if (v === undefined || v === null) {
|
|
240
|
+
if (propSpec.required) {
|
|
241
|
+
return `Missing required prop '${propSpec.name}' for component '${value.component}'.`
|
|
242
|
+
}
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
const err = clientShapeError(v, propSpec)
|
|
246
|
+
if (err) return err
|
|
247
|
+
}
|
|
248
|
+
return true
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function clientShapeError(value: unknown, prop: PropSpec): string | null {
|
|
253
|
+
const t = prop.type
|
|
254
|
+
switch (t.kind) {
|
|
255
|
+
case 'string':
|
|
256
|
+
return typeof value === 'string' ? null : `Prop '${prop.name}' must be a string.`
|
|
257
|
+
case 'number':
|
|
258
|
+
return typeof value === 'number' && !Number.isNaN(value)
|
|
259
|
+
? null
|
|
260
|
+
: `Prop '${prop.name}' must be a number.`
|
|
261
|
+
case 'boolean':
|
|
262
|
+
return typeof value === 'boolean' ? null : `Prop '${prop.name}' must be a boolean.`
|
|
263
|
+
case 'enum':
|
|
264
|
+
return t.values.includes(value as never)
|
|
265
|
+
? null
|
|
266
|
+
: `Prop '${prop.name}' must be one of: ${t.values.join(', ')}.`
|
|
267
|
+
case 'array':
|
|
268
|
+
return Array.isArray(value) ? null : `Prop '${prop.name}' must be an array.`
|
|
269
|
+
case 'object': {
|
|
270
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
271
|
+
return `Prop '${prop.name}' must be an object.`
|
|
272
|
+
}
|
|
273
|
+
// Recurse: check that each required nested field is present and
|
|
274
|
+
// structurally compatible. Without this, passing `{}` for an
|
|
275
|
+
// object prop with required `label` and `href` children sailed
|
|
276
|
+
// through the client validator while the server-side
|
|
277
|
+
// validateComponentBlockValue would reject it on save — a
|
|
278
|
+
// silent gap in the "live structural validation" promise.
|
|
279
|
+
const obj = value as Record<string, unknown>
|
|
280
|
+
for (const field of t.fields) {
|
|
281
|
+
const child = obj[field.name]
|
|
282
|
+
if (child === undefined || child === null) {
|
|
283
|
+
if (field.required) {
|
|
284
|
+
return `Prop '${prop.name}.${field.name}' is required but missing.`
|
|
285
|
+
}
|
|
286
|
+
continue
|
|
287
|
+
}
|
|
288
|
+
const childError = clientShapeError(child, {
|
|
289
|
+
...field,
|
|
290
|
+
// Build a dotted path so parsePerPropErrors can attribute
|
|
291
|
+
// the error to the nested input, not just the outer prop.
|
|
292
|
+
name: `${prop.name}.${field.name}`,
|
|
293
|
+
})
|
|
294
|
+
if (childError) return childError
|
|
295
|
+
}
|
|
296
|
+
return null
|
|
297
|
+
}
|
|
298
|
+
case 'union': {
|
|
299
|
+
// Discriminated unions get the same recursive treatment as plain
|
|
300
|
+
// objects: route the value into the matching variant's schema
|
|
301
|
+
// and validate against that. Non-discriminated unions remain
|
|
302
|
+
// unvalidated on the client (server still catches them) — the
|
|
303
|
+
// shape is too open to assert structurally.
|
|
304
|
+
const detected = detectDiscriminator(t)
|
|
305
|
+
if (!detected) return null
|
|
306
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
307
|
+
return `Prop '${prop.name}' must be an object with a '${detected.field}' discriminator.`
|
|
308
|
+
}
|
|
309
|
+
const variant = findVariant(value, detected)
|
|
310
|
+
if (!variant) {
|
|
311
|
+
const allowed = detected.variants.map((v) => JSON.stringify(v.value)).join(', ')
|
|
312
|
+
return `Prop '${prop.name}.${detected.field}' must be one of: ${allowed}.`
|
|
313
|
+
}
|
|
314
|
+
// Recurse into the variant's fields, scoped to the prop path.
|
|
315
|
+
// We treat the variant as an inline object so the existing
|
|
316
|
+
// object-branch logic applies verbatim.
|
|
317
|
+
return clientShapeError(value, {
|
|
318
|
+
name: prop.name,
|
|
319
|
+
required: prop.required,
|
|
320
|
+
type: variant.type,
|
|
321
|
+
})
|
|
322
|
+
}
|
|
323
|
+
default:
|
|
324
|
+
return null
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The validator returns a single error string. The form wants to show
|
|
330
|
+
* the message under the specific input that triggered it — so we parse
|
|
331
|
+
* the message back into a sparse `{ propPath → message }` map. Pure
|
|
332
|
+
* function so it's trivial to unit-test.
|
|
333
|
+
*/
|
|
334
|
+
export function parsePerPropErrors(message: string | null): Record<string, string> {
|
|
335
|
+
if (!message) return {}
|
|
336
|
+
const propMatch = /Prop '([^']+)'/.exec(message)
|
|
337
|
+
if (propMatch) return { [propMatch[1]!]: message }
|
|
338
|
+
const missing = /Missing required prop '([^']+)'/.exec(message)
|
|
339
|
+
if (missing) return { [missing[1]!]: message }
|
|
340
|
+
return {}
|
|
341
|
+
}
|
package/src/fields/index.ts
CHANGED
|
@@ -15,3 +15,7 @@ export type { BlockTypeDefinition } from './block-types.js'
|
|
|
15
15
|
export { GroupField } from './GroupField.js'
|
|
16
16
|
export { NavBuilderField } from './NavBuilderField.js'
|
|
17
17
|
export { NumberField } from './NumberField.js'
|
|
18
|
+
export { ComponentBlockField } from './ComponentBlockField.js'
|
|
19
|
+
export type { ComponentBlockFieldProps, ComponentBlockValue } from './ComponentBlockField.js'
|
|
20
|
+
export { PropInput, defaultForType } from './PropInput.js'
|
|
21
|
+
export type { PropInputProps } from './PropInput.js'
|
package/src/index.ts
CHANGED
|
@@ -105,3 +105,10 @@ export { useKeyboardShortcuts } from './hooks/useKeyboardShortcuts.js'
|
|
|
105
105
|
export { cmsApi, setApiBase, ensureCsrfToken } from './lib/api.js'
|
|
106
106
|
export { useApiData } from './lib/useApiData.js'
|
|
107
107
|
export type { UseApiDataResult } from './lib/useApiData.js'
|
|
108
|
+
|
|
109
|
+
export { ComponentBlockField, PropInput, defaultForType } from './fields/index.js'
|
|
110
|
+
export type {
|
|
111
|
+
ComponentBlockFieldProps,
|
|
112
|
+
ComponentBlockValue,
|
|
113
|
+
PropInputProps,
|
|
114
|
+
} from './fields/index.js'
|