payload-canvas 0.5.2 → 0.6.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.
- package/README.md +342 -30
- package/dist/admin/ArrayRowLabel.d.ts +12 -0
- package/dist/admin/ArrayRowLabel.js +21 -0
- package/dist/admin/client.d.ts +1 -0
- package/dist/admin/client.js +2 -0
- package/dist/admin/editor/Editor.js +8 -1
- package/dist/admin/editor/Inspector.js +23 -17
- package/dist/admin/editor/Library.d.ts +2 -2
- package/dist/admin/editor/Library.js +7 -5
- package/dist/admin/editor/Outline.js +20 -12
- package/dist/admin/editor/Overlay.js +6 -4
- package/dist/admin/editor/actions.d.ts +7 -2
- package/dist/admin/editor/actions.js +44 -12
- package/dist/admin/editor/assistant/AssistantPanel.js +32 -20
- package/dist/admin/editor/comments/controller.d.ts +7 -0
- package/dist/admin/editor/comments/controller.js +12 -0
- package/dist/admin/editor/comments/useCommentFromURL.d.ts +2 -0
- package/dist/admin/editor/comments/useCommentFromURL.js +36 -0
- package/dist/admin/editor/dnd/smooth.js +1 -1
- package/dist/admin/editor/editor.scss +28 -0
- package/dist/admin/editor/fields/ArrayField.js +3 -3
- package/dist/admin/editor/fields/FormBlock.d.ts +20 -0
- package/dist/admin/editor/fields/FormBlock.js +167 -0
- package/dist/admin/editor/fields/FormFieldName.d.ts +25 -0
- package/dist/admin/editor/fields/FormFieldName.js +58 -0
- package/dist/admin/editor/fields/GroupField.d.ts +22 -2
- package/dist/admin/editor/fields/GroupField.js +30 -1
- package/dist/admin/editor/fields/MergeTags.d.ts +8 -0
- package/dist/admin/editor/fields/MergeTags.js +62 -0
- package/dist/admin/editor/fields/arrayRows.d.ts +0 -2
- package/dist/admin/editor/fields/arrayRows.js +0 -4
- package/dist/admin/editor/fields/fields.scss +150 -0
- package/dist/admin/editor/fields/values.d.ts +0 -2
- package/dist/admin/editor/fields/values.js +0 -14
- package/dist/admin/editor/icons.d.ts +3 -0
- package/dist/admin/editor/icons.js +3 -0
- package/dist/admin/editor/insert/InsertHandle.js +1 -1
- package/dist/admin/editor/insert/picker.d.ts +3 -2
- package/dist/admin/editor/insert/picker.js +9 -3
- package/dist/admin/editor/layout/LeftPanel.js +5 -3
- package/dist/admin/editor/layout/leftTabs.d.ts +15 -0
- package/dist/admin/editor/layout/leftTabs.js +18 -0
- package/dist/admin/editor/media/ImageEditor.d.ts +8 -2
- package/dist/admin/editor/media/ImageEditor.js +16 -10
- package/dist/admin/editor/media/media.scss +12 -2
- package/dist/admin/editor/media/model.d.ts +10 -1
- package/dist/admin/editor/media/model.js +19 -0
- package/dist/admin/editor/menu/blockMenu.js +3 -3
- package/dist/admin/editor/names.d.ts +9 -0
- package/dist/admin/editor/names.js +18 -0
- package/dist/admin/editor/regions/RegionFields.d.ts +6 -0
- package/dist/admin/editor/regions/RegionFields.js +122 -0
- package/dist/admin/editor/regions/RegionPane.js +5 -3
- package/dist/admin/editor/regions/RegionRows.js +2 -2
- package/dist/admin/editor/regions/required.d.ts +16 -0
- package/dist/admin/editor/regions/required.js +43 -0
- package/dist/admin/editor/renderField.d.ts +9 -2
- package/dist/admin/editor/renderField.js +86 -32
- package/dist/admin/editor/runtime.d.ts +3 -2
- package/dist/admin/editor/runtime.js +12 -18
- package/dist/admin/editor/styles/StylesPanel.js +4 -2
- package/dist/admin/editor/styles/context.js +9 -4
- package/dist/admin/editor/topbar/ContentModeToggle.js +1 -1
- package/dist/admin/editor/topbar/DocumentActions.js +4 -3
- package/dist/admin/editor/topbar/contentMode.scss +0 -9
- package/dist/admin/editor/topbar/topbar.scss +1 -41
- package/dist/admin/editor/ui/Tabs.d.ts +17 -0
- package/dist/admin/editor/ui/Tabs.js +17 -0
- package/dist/admin/forms/SubmissionCells.d.ts +6 -0
- package/dist/admin/forms/SubmissionCells.js +31 -0
- package/dist/admin/forms/SubmissionView.d.ts +5 -0
- package/dist/admin/forms/SubmissionView.js +106 -0
- package/dist/admin/forms/submissionData.d.ts +32 -0
- package/dist/admin/forms/submissionData.js +63 -0
- package/dist/admin/forms/submissions.scss +197 -0
- package/dist/admin/rsc.d.ts +2 -0
- package/dist/admin/rsc.js +5 -0
- package/dist/ai/endpoint.d.ts +5 -0
- package/dist/ai/endpoint.js +18 -8
- package/dist/ai/prompt.d.ts +8 -0
- package/dist/ai/prompt.js +30 -0
- package/dist/ai/tools.d.ts +6 -1
- package/dist/ai/tools.js +90 -6
- package/dist/blocks/builtins/carousel.js +10 -1
- package/dist/blocks/defaults.js +27 -2
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/payload.d.ts +7 -0
- package/dist/blocks/payload.js +16 -2
- package/dist/comments/shared.d.ts +5 -0
- package/dist/comments/shared.js +5 -0
- package/dist/core/blocks.d.ts +17 -3
- package/dist/core/blocks.js +92 -10
- package/dist/core/classes.d.ts +5 -0
- package/dist/core/classes.js +26 -1
- package/dist/core/conditions.d.ts +7 -0
- package/dist/core/conditions.js +22 -0
- package/dist/core/dropTarget.d.ts +3 -2
- package/dist/core/dropTarget.js +7 -1
- package/dist/core/index.d.ts +6 -3
- package/dist/core/index.js +6 -3
- package/dist/core/payloadBlocks.js +25 -3
- package/dist/core/references.d.ts +7 -0
- package/dist/core/references.js +29 -11
- package/dist/core/regions.d.ts +14 -0
- package/dist/core/regions.js +17 -1
- package/dist/core/rowLabel.d.ts +8 -0
- package/dist/core/rowLabel.js +98 -0
- package/dist/core/starter.d.ts +10 -0
- package/dist/core/starter.js +40 -0
- package/dist/core/tree.js +4 -0
- package/dist/core/types.d.ts +96 -0
- package/dist/core/uniqueProps.d.ts +40 -0
- package/dist/core/uniqueProps.js +156 -0
- package/dist/core/validate.js +43 -3
- package/dist/forms/answers.d.ts +21 -0
- package/dist/forms/answers.js +82 -0
- package/dist/forms/blocks.d.ts +33 -0
- package/dist/forms/blocks.js +178 -0
- package/dist/forms/conditions.d.ts +78 -0
- package/dist/forms/conditions.js +184 -0
- package/dist/forms/emails.d.ts +23 -0
- package/dist/forms/emails.js +64 -0
- package/dist/forms/fieldConfigs.d.ts +35 -0
- package/dist/forms/fieldConfigs.js +167 -0
- package/dist/forms/fieldOps.d.ts +78 -0
- package/dist/forms/fieldOps.js +378 -0
- package/dist/forms/fieldSettings.d.ts +81 -0
- package/dist/forms/fieldSettings.js +239 -0
- package/dist/forms/files.d.ts +54 -0
- package/dist/forms/files.js +128 -0
- package/dist/forms/index.d.ts +16 -0
- package/dist/forms/index.js +18 -0
- package/dist/forms/mergeTags.d.ts +103 -0
- package/dist/forms/mergeTags.js +150 -0
- package/dist/forms/noScript.d.ts +60 -0
- package/dist/forms/noScript.js +109 -0
- package/dist/forms/pageForms.d.ts +99 -0
- package/dist/forms/pageForms.js +294 -0
- package/dist/forms/pageSync.d.ts +91 -0
- package/dist/forms/pageSync.js +325 -0
- package/dist/forms/rateLimit.d.ts +21 -0
- package/dist/forms/rateLimit.js +50 -0
- package/dist/forms/settings.d.ts +13 -0
- package/dist/forms/settings.js +92 -0
- package/dist/forms/spam.d.ts +100 -0
- package/dist/forms/spam.js +218 -0
- package/dist/forms/spamSettings.d.ts +43 -0
- package/dist/forms/spamSettings.js +25 -0
- package/dist/forms/submission.d.ts +34 -0
- package/dist/forms/submission.js +89 -0
- package/dist/forms/submissionsAdmin.d.ts +26 -0
- package/dist/forms/submissionsAdmin.js +108 -0
- package/dist/forms/summary.d.ts +60 -0
- package/dist/forms/summary.js +113 -0
- package/dist/forms/uploads.d.ts +41 -0
- package/dist/forms/uploads.js +132 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.js +8 -2
- package/dist/live/document.d.ts +12 -0
- package/dist/live/document.js +8 -1
- package/dist/live/endpoints.d.ts +1 -0
- package/dist/live/session.js +3 -1
- package/dist/live/storage.d.ts +13 -2
- package/dist/live/storage.js +18 -7
- package/dist/mcp/forms.d.ts +26 -0
- package/dist/mcp/forms.js +280 -0
- package/dist/mcp/index.d.ts +7 -0
- package/dist/mcp/index.js +29 -10
- package/dist/mcp/shared.d.ts +1 -0
- package/dist/mcp/shared.js +1 -0
- package/dist/plugin/forms.d.ts +30 -0
- package/dist/plugin/forms.js +63 -0
- package/dist/plugin/formsSync.d.ts +138 -0
- package/dist/plugin/formsSync.js +345 -0
- package/dist/plugin/hook.d.ts +22 -0
- package/dist/plugin/hook.js +19 -7
- package/dist/plugin/index.d.ts +14 -0
- package/dist/plugin/index.js +99 -28
- package/dist/plugin/links.d.ts +7 -2
- package/dist/plugin/links.js +9 -3
- package/dist/plugin/references.d.ts +6 -0
- package/dist/plugin/references.js +12 -0
- package/dist/react/canvas/BuilderCanvas.d.ts +2 -0
- package/dist/react/canvas/BuilderCanvas.js +29 -6
- package/dist/react/canvas/inline/mapper.d.ts +6 -1
- package/dist/react/canvas/inline/mapper.js +35 -4
- package/dist/react/components/AutoplayVideo.d.ts +20 -0
- package/dist/react/components/AutoplayVideo.js +34 -0
- package/dist/react/components/Video.d.ts +9 -1
- package/dist/react/components/Video.js +39 -11
- package/dist/react/components/lazy.d.ts +2 -0
- package/dist/react/components/lazy.js +2 -0
- package/dist/react/components/videoPlayback.d.ts +30 -0
- package/dist/react/components/videoPlayback.js +33 -0
- package/dist/react/render/LayoutOutlet.d.ts +7 -3
- package/dist/react/render/LayoutOutlet.js +9 -4
- package/dist/react/render/RenderLayout.js +18 -9
- package/dist/react/render/resolve.d.ts +1 -0
- package/dist/react/render/resolve.js +5 -1
- package/dist/react/render/types.d.ts +1 -1
- package/dist/react/server.js +19 -0
- package/package.json +5 -1
package/dist/core/blocks.js
CHANGED
|
@@ -203,6 +203,36 @@ export function withInlineBlocks(blocks) {
|
|
|
203
203
|
return { ...def, slots: next };
|
|
204
204
|
});
|
|
205
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* The layout with `BlockDefinition.upgrade` run on every block (slots too, children first). The
|
|
208
|
+
* same layout when no block changes, or when no definition has an upgrade.
|
|
209
|
+
*/
|
|
210
|
+
export function upgradeLayout(layout, blocks) {
|
|
211
|
+
const upgrades = new Map(blocks.flatMap((def) => (def.upgrade ? [[def.type, def.upgrade]] : [])));
|
|
212
|
+
if (upgrades.size === 0)
|
|
213
|
+
return layout;
|
|
214
|
+
const visitList = (list) => {
|
|
215
|
+
let changed = false;
|
|
216
|
+
const out = list.map((block) => {
|
|
217
|
+
const next = visit(block);
|
|
218
|
+
if (next !== block)
|
|
219
|
+
changed = true;
|
|
220
|
+
return next;
|
|
221
|
+
});
|
|
222
|
+
return changed ? out : list;
|
|
223
|
+
};
|
|
224
|
+
const visit = (block) => {
|
|
225
|
+
let next = block;
|
|
226
|
+
if (block.slots) {
|
|
227
|
+
const slots = Object.fromEntries(Object.entries(block.slots).map(([name, children]) => [name, visitList(children)]));
|
|
228
|
+
if (Object.keys(slots).some((name) => slots[name] !== block.slots?.[name]))
|
|
229
|
+
next = { ...block, slots };
|
|
230
|
+
}
|
|
231
|
+
return upgrades.get(block.type)?.(next) ?? next;
|
|
232
|
+
};
|
|
233
|
+
const list = visitList(layout.blocks);
|
|
234
|
+
return list === layout.blocks ? layout : { ...layout, blocks: list };
|
|
235
|
+
}
|
|
206
236
|
export function getBlockDefinition(blocks, type) {
|
|
207
237
|
return blocks.find((b) => b.type === type);
|
|
208
238
|
}
|
|
@@ -222,18 +252,65 @@ function subtreeTypes(block, out = new Set()) {
|
|
|
222
252
|
subtreeTypes(child, out);
|
|
223
253
|
return out;
|
|
224
254
|
}
|
|
255
|
+
/** "Email can only go inside Form", or "… inside Form or Survey". */
|
|
256
|
+
function ancestorsMessage(blocks, type) {
|
|
257
|
+
const label = (t) => getBlockDefinition(blocks, t)?.label ?? t;
|
|
258
|
+
const ancestors = getBlockDefinition(blocks, type)?.ancestors ?? [];
|
|
259
|
+
return `${label(type)} can only go inside ${ancestors.map(label).join(' or ')}`;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Why a block (a type, or a whole block with its children) cannot sit below blocks of the types
|
|
263
|
+
* `above` (outermost first; empty for the root list) by the `ancestors` rules of its types, or
|
|
264
|
+
* `null`. Inside the placed subtree, a block's own ancestors in the subtree count too.
|
|
265
|
+
*/
|
|
266
|
+
export function ancestorsError(blocks, block, above) {
|
|
267
|
+
const rootType = typeof block === 'string' ? block : block.type;
|
|
268
|
+
const label = (t) => getBlockDefinition(blocks, t)?.label ?? t;
|
|
269
|
+
const visit = (current, path) => {
|
|
270
|
+
const type = typeof current === 'string' ? current : current.type;
|
|
271
|
+
const rule = getBlockDefinition(blocks, type)?.ancestors;
|
|
272
|
+
if (rule && !rule.some((t) => path.includes(t))) {
|
|
273
|
+
return type === rootType && current === block ? ancestorsMessage(blocks, type) : `${label(rootType)} contains ${ancestorsMessage(blocks, type).replace(' can only', ', which can only')}`;
|
|
274
|
+
}
|
|
275
|
+
if (typeof current === 'string')
|
|
276
|
+
return null;
|
|
277
|
+
const inner = [...path, type];
|
|
278
|
+
for (const children of Object.values(current.slots ?? {})) {
|
|
279
|
+
for (const child of children) {
|
|
280
|
+
const error = visit(child, inner);
|
|
281
|
+
if (error)
|
|
282
|
+
return error;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
};
|
|
287
|
+
return visit(block, above);
|
|
288
|
+
}
|
|
289
|
+
/** The types of `parentId` and its ancestors, outermost first. Empty for the root list. */
|
|
290
|
+
function typesAbove(index, parentId) {
|
|
291
|
+
const out = [];
|
|
292
|
+
for (let id = parentId; id !== null;) {
|
|
293
|
+
const entry = index.get(id);
|
|
294
|
+
if (!entry)
|
|
295
|
+
break;
|
|
296
|
+
out.unshift(entry.block.type);
|
|
297
|
+
id = entry.parentId;
|
|
298
|
+
}
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
225
301
|
/**
|
|
226
302
|
* Why `block` (a type, or a whole block with its children) cannot go into `slot` of `parentId`,
|
|
227
303
|
* or `null` when it can. Checks the direct slot's `allow` and `disallow`, and the `disallow` of
|
|
228
304
|
* every ancestor slot up to the root, against every type in the placed subtree, the placed
|
|
229
|
-
* block's own `parents` rule,
|
|
230
|
-
* moved within it, does not count as one more).
|
|
231
|
-
*
|
|
305
|
+
* block's own `parents` rule, the `ancestors` rules of every type in the subtree, and the direct
|
|
306
|
+
* slot's `max` (a block already in that slot, being moved within it, does not count as one more).
|
|
307
|
+
* The root list accepts every type without a `parents` or `ancestors` rule. The message is
|
|
308
|
+
* readable, e.g. `Button cannot go inside Link`.
|
|
232
309
|
*/
|
|
233
310
|
export function placementError(blocks, layout, parentId, slot, block, index = indexLayout(layout)) {
|
|
234
311
|
const rootType = typeof block === 'string' ? block : block.type;
|
|
235
312
|
if (parentId === null)
|
|
236
|
-
return fitsParent(blocks, rootType, null) ?
|
|
313
|
+
return fitsParent(blocks, rootType, null) ? ancestorsError(blocks, block, []) : parentsMessage(blocks, rootType);
|
|
237
314
|
const types = typeof block === 'string' ? new Set([block]) : subtreeTypes(block);
|
|
238
315
|
const label = (type) => getBlockDefinition(blocks, type)?.label ?? type;
|
|
239
316
|
const skip = typeof block === 'string' ? new Set() : new Set(subtreeIds(block));
|
|
@@ -277,7 +354,7 @@ export function placementError(blocks, layout, parentId, slot, block, index = in
|
|
|
277
354
|
slotName = owner.slot;
|
|
278
355
|
ownerId = owner.parentId;
|
|
279
356
|
}
|
|
280
|
-
return
|
|
357
|
+
return ancestorsError(blocks, block, typesAbove(index, parentId));
|
|
281
358
|
}
|
|
282
359
|
/** The slot's name for people: its `label`, else its name. */
|
|
283
360
|
export function slotLabel(def, slot) {
|
|
@@ -305,13 +382,18 @@ export function slotFullError(blocks, owner, slot) {
|
|
|
305
382
|
* a slot without a limit, or an unknown parent.
|
|
306
383
|
*/
|
|
307
384
|
export function slotRoom(blocks, layout, parentId, slot) {
|
|
308
|
-
|
|
385
|
+
const max = slotDefinitionAt(blocks, layout, parentId, slot)?.max;
|
|
386
|
+
if (typeof max !== 'number')
|
|
309
387
|
return Infinity;
|
|
388
|
+
const owner = parentId === null ? null : indexLayout(layout).get(parentId)?.block;
|
|
389
|
+
return Math.max(0, max - (owner?.slots?.[slot]?.length ?? 0));
|
|
390
|
+
}
|
|
391
|
+
/** The definition of `slot` of block `parentId`. Undefined for the root list and undeclared slots. */
|
|
392
|
+
export function slotDefinitionAt(blocks, layout, parentId, slot) {
|
|
393
|
+
if (parentId === null)
|
|
394
|
+
return undefined;
|
|
310
395
|
const owner = indexLayout(layout).get(parentId)?.block;
|
|
311
|
-
|
|
312
|
-
if (!owner || typeof max !== 'number')
|
|
313
|
-
return Infinity;
|
|
314
|
-
return Math.max(0, max - (owner.slots?.[slot]?.length ?? 0));
|
|
396
|
+
return owner ? getBlockDefinition(blocks, owner.type)?.slots?.[slot] : undefined;
|
|
315
397
|
}
|
|
316
398
|
/** True when `block` (a type or a whole block) may go into `slot` of `parentId`. See `placementError`. */
|
|
317
399
|
export function slotAcceptsAt(blocks, layout, parentId, slot, block) {
|
package/dist/core/classes.d.ts
CHANGED
|
@@ -20,3 +20,8 @@ export declare function defaultPartClassNames(def: Pick<BlockDefinition, 'slots'
|
|
|
20
20
|
* in the layout.
|
|
21
21
|
*/
|
|
22
22
|
export declare function collectClasses(layout: Layout, blocks?: readonly BlockDefinition[]): string[];
|
|
23
|
+
/**
|
|
24
|
+
* The classes a block's part shows: its own (`partClassName`), else, for a part that `inherits`,
|
|
25
|
+
* the classes of that part of the nearest ancestor block of the named type. '' when it has none.
|
|
26
|
+
*/
|
|
27
|
+
export declare function partClassNameOf(layout: Layout, blockId: string, part: string, definitions: readonly BlockDefinition[]): string;
|
package/dist/core/classes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { walkBlocks } from './tree.js';
|
|
1
|
+
import { indexLayout, walkBlocks } from './tree.js';
|
|
2
2
|
/**
|
|
3
3
|
* The block's parts: its slots with `styles: true` (in slot order), then its `parts`. A part name
|
|
4
4
|
* that is also a slot name is left out of `parts`.
|
|
@@ -60,3 +60,28 @@ export function collectClasses(layout, blocks) {
|
|
|
60
60
|
}
|
|
61
61
|
return [...classes].toSorted();
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* The classes a block's part shows: its own (`partClassName`), else, for a part that `inherits`,
|
|
65
|
+
* the classes of that part of the nearest ancestor block of the named type. '' when it has none.
|
|
66
|
+
*/
|
|
67
|
+
export function partClassNameOf(layout, blockId, part, definitions) {
|
|
68
|
+
const index = indexLayout(layout);
|
|
69
|
+
const entry = index.get(blockId);
|
|
70
|
+
if (!entry)
|
|
71
|
+
return '';
|
|
72
|
+
const own = entry.block.partClassName?.[part];
|
|
73
|
+
if (own)
|
|
74
|
+
return own;
|
|
75
|
+
const rule = definitions.find((d) => d.type === entry.block.type)?.parts?.[part]?.inherits;
|
|
76
|
+
if (!rule)
|
|
77
|
+
return '';
|
|
78
|
+
for (let id = entry.parentId; id !== null;) {
|
|
79
|
+
const above = index.get(id);
|
|
80
|
+
if (!above)
|
|
81
|
+
return '';
|
|
82
|
+
if (above.block.type === rule.ancestor)
|
|
83
|
+
return above.block.partClassName?.[rule.part ?? part] ?? '';
|
|
84
|
+
id = above.parentId;
|
|
85
|
+
}
|
|
86
|
+
return '';
|
|
87
|
+
}
|
|
@@ -28,4 +28,11 @@ export declare function conditionMet(condition: BuilderCondition, siblings: Reco
|
|
|
28
28
|
* Minified code works too. Anything else (document data, user, several tests) returns null.
|
|
29
29
|
*/
|
|
30
30
|
export declare function conditionFromFunction(fn: unknown): BuilderCondition | null;
|
|
31
|
+
/**
|
|
32
|
+
* A copy of a field config whose simple `admin.condition` functions (also in nested fields) are
|
|
33
|
+
* JSON rules in `admin.custom.builderCondition`, so the inspector can show and hide the field.
|
|
34
|
+
* A field that has a rule already keeps it. For document fields the inspector edits (regions
|
|
35
|
+
* with `panel`); `fromPayloadBlocks` does the same for block fields.
|
|
36
|
+
*/
|
|
37
|
+
export declare function withBuilderConditions<T>(field: T): T;
|
|
31
38
|
export {};
|
package/dist/core/conditions.js
CHANGED
|
@@ -154,3 +154,25 @@ function fieldOf(match, from) {
|
|
|
154
154
|
function escape(text) {
|
|
155
155
|
return text.replace(/[$]/g, '\\$');
|
|
156
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* A copy of a field config whose simple `admin.condition` functions (also in nested fields) are
|
|
159
|
+
* JSON rules in `admin.custom.builderCondition`, so the inspector can show and hide the field.
|
|
160
|
+
* A field that has a rule already keeps it. For document fields the inspector edits (regions
|
|
161
|
+
* with `panel`); `fromPayloadBlocks` does the same for block fields.
|
|
162
|
+
*/
|
|
163
|
+
export function withBuilderConditions(field) {
|
|
164
|
+
if (!isPlainObject(field))
|
|
165
|
+
return field;
|
|
166
|
+
const out = { ...field };
|
|
167
|
+
const admin = isPlainObject(field.admin) ? field.admin : null;
|
|
168
|
+
if (admin && typeof admin.condition === 'function' && !readCondition(field)) {
|
|
169
|
+
const condition = conditionFromFunction(admin.condition);
|
|
170
|
+
if (condition)
|
|
171
|
+
out.admin = { ...admin, custom: { ...(isPlainObject(admin.custom) ? admin.custom : {}), builderCondition: condition } };
|
|
172
|
+
}
|
|
173
|
+
if (Array.isArray(field.fields))
|
|
174
|
+
out.fields = field.fields.map(withBuilderConditions);
|
|
175
|
+
if (Array.isArray(field.tabs))
|
|
176
|
+
out.tabs = field.tabs.map(withBuilderConditions);
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BlockDefinition, CanvasMeasurement, DragSource, DropTarget, Layout, OutlineRow, Point } from './types.js';
|
|
1
|
+
import type { BlockDefinition, CanvasMeasurement, DragSource, DropTarget, Layout, OutlineRow, Point, Rect } from './types.js';
|
|
2
2
|
/** The deepest block whose rect contains the point. Ties go to the smaller rect. */
|
|
3
3
|
export declare function deepestBlockAt(layout: Layout, measurement: CanvasMeasurement, p: Point, excluded?: Set<string>): string | null;
|
|
4
4
|
/**
|
|
@@ -19,8 +19,9 @@ export declare function canvasDropTarget(layout: Layout, blocks: BlockDefinition
|
|
|
19
19
|
* bottom 30% first child (or after the row when the container is empty).
|
|
20
20
|
* When the preferred option is not allowed, the next one is tried; `null` when none fits.
|
|
21
21
|
* Below the last row: appended to the root list.
|
|
22
|
+
* No rows (an empty page): `area`, the tree's box, takes the drop as the root list's first block.
|
|
22
23
|
*/
|
|
23
|
-
export declare function outlineDropTarget(layout: Layout, blocks: BlockDefinition[], rows: OutlineRow[], p: Point, source: DragSource, indent: number): DropTarget | null;
|
|
24
|
+
export declare function outlineDropTarget(layout: Layout, blocks: BlockDefinition[], rows: OutlineRow[], p: Point, source: DragSource, indent: number, area?: Rect): DropTarget | null;
|
|
24
25
|
/** A slot that takes the dragged block. `ownerId` null is the root list. */
|
|
25
26
|
export type AcceptingSlot = {
|
|
26
27
|
ownerId: string | null;
|
package/dist/core/dropTarget.js
CHANGED
|
@@ -215,11 +215,17 @@ export function canvasDropTarget(layout, blocks, measurement, p, source) {
|
|
|
215
215
|
* bottom 30% first child (or after the row when the container is empty).
|
|
216
216
|
* When the preferred option is not allowed, the next one is tried; `null` when none fits.
|
|
217
217
|
* Below the last row: appended to the root list.
|
|
218
|
+
* No rows (an empty page): `area`, the tree's box, takes the drop as the root list's first block.
|
|
218
219
|
*/
|
|
219
|
-
export function outlineDropTarget(layout, blocks, rows, p, source, indent) {
|
|
220
|
+
export function outlineDropTarget(layout, blocks, rows, p, source, indent, area) {
|
|
220
221
|
const { index, type, excluded, slotsOf, accepts } = context(layout, blocks, source);
|
|
221
222
|
if (type === null)
|
|
222
223
|
return null;
|
|
224
|
+
if (rows.length === 0) {
|
|
225
|
+
if (!area || !rectContains(area, p) || !accepts(null, DEFAULT_SLOT))
|
|
226
|
+
return null;
|
|
227
|
+
return finalize(index, source, null, DEFAULT_SLOT, 0, { kind: 'box', rect: area });
|
|
228
|
+
}
|
|
223
229
|
const line = (r, side, depth) => {
|
|
224
230
|
const y = side === 'before' ? r.rect.y : r.rect.y + r.rect.height;
|
|
225
231
|
return {
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export * from './types.js';
|
|
2
2
|
export { checkAttrs, htmlAttributes, htmlIdClash, htmlIdProblem, htmlIdsOf, patchAttrs, withoutTakenHtmlIds } from './attrs.js';
|
|
3
3
|
export { createId } from './ids.js';
|
|
4
|
+
export { duplicateOperation, freeValue, uniquePropClashes, uniquePropValues, uniquePropValuesAt, uniqueScopeOf, withUniqueProps, type UniquePropClash } from './uniqueProps.js';
|
|
5
|
+
export { newBlockOf } from './starter.js';
|
|
4
6
|
export { findBlock, findLocation, walkBlocks, isSelfOrDescendant, normalizeLayout, type BlockLocation } from './tree.js';
|
|
5
7
|
export { applyOperation, applyOperations, type ApplyOptions } from './operations.js';
|
|
6
8
|
export { layerAt, reachableAt } from './layers.js';
|
|
7
9
|
export { acceptingSlots, type AcceptingSlot, deepestBlockAt, canvasDropTarget, outlineDropTarget } from './dropTarget.js';
|
|
8
|
-
export { blockParts, type BlockPart, collectClasses, defaultPartClassNames } from './classes.js';
|
|
10
|
+
export { blockParts, type BlockPart, collectClasses, defaultPartClassNames, partClassNameOf } from './classes.js';
|
|
9
11
|
export { headingId, richTextHeadings, type RichTextHeading } from './headings.js';
|
|
10
12
|
export { blockJsonSchema, layoutJsonSchema } from './schema.js';
|
|
11
13
|
export { validateLayout, isBlockingError, isLayoutWarning, PUBLISH_ONLY_CODES, type LayoutError, type LayoutErrorCode, type ValidateOptions } from './validate.js';
|
|
@@ -16,7 +18,7 @@ export * from './bindings.js';
|
|
|
16
18
|
export * from './visibility.js';
|
|
17
19
|
export * from './components.js';
|
|
18
20
|
export { TEXT_LIST_BLOCK, TEXT_LIST_ITEM_BLOCK, TEXT_LIST_SLOT, joinListItem, splitListItem, type ListItemEdit } from './textList.js';
|
|
19
|
-
export { blockName, defineBlock, defineSection, type SectionBlockInput, type SectionInput, fitsParent, getBlockDefinition, placementError, checkSectionVariants, sectionsWithDefaultClassNames, sectionsWithVariants, slotAccepts, slotAcceptsAt, slotNames, starterSlots, withDefaultClasses, withDefaultClassNames, withInlineBlocks } from './blocks.js';
|
|
21
|
+
export { ancestorsError, blockName, defineBlock, defineSection, type SectionBlockInput, type SectionInput, fitsParent, getBlockDefinition, placementError, upgradeLayout, checkSectionVariants, sectionsWithDefaultClassNames, sectionsWithVariants, slotAccepts, slotAcceptsAt, slotNames, starterSlots, withDefaultClasses, withDefaultClassNames, withInlineBlocks } from './blocks.js';
|
|
20
22
|
export { carryContent, DEFAULT_VARIANT_LABEL, findSection, findSectionVersion, hasVariants, listedVariants, sectionVersion, sectionVersions, switchSectionVariant, versionIndex, withoutSectionOrigin, withSectionOrigin, type SectionVersion, type VariantSwitch, } from './sections.js';
|
|
21
23
|
export { checkSectionOrigin } from './sectionOrigin.js';
|
|
22
24
|
export { FORMATS, formatProblem, isPlayableVideoUrl, parseVideoUrl, videoUrlProblem, type FormatCheck, type VideoEmbed, type VideoOptions } from './formats.js';
|
|
@@ -25,5 +27,6 @@ export { conditionFromFunction, conditionMet, readCondition, type BuilderConditi
|
|
|
25
27
|
export { convertPayloadBlocksLayout, definitionForBlockType, isPayloadBlocksValue, payloadSlugOf, toPayloadBlock, withFieldDefaults, withLayoutDefaults, hasStaticDefault, type ConvertPayloadOptions, type PayloadBlockData, type PayloadConversion, type PayloadConversionReport, } from './convertPayload.js';
|
|
26
28
|
export { decodePayloadBlocks, encodePayloadBlocks, payloadBlocksProblems, payloadBlocksStorage, type PayloadBlocksDecode, type PayloadBlocksStorage, } from './payloadBlocks.js';
|
|
27
29
|
export { dataFields, fieldAtPropPath, type DataField } from './fields.js';
|
|
30
|
+
export { rowLabel } from './rowLabel.js';
|
|
28
31
|
export { pathWithoutIndexes, regionAccessAt, regionDefinition, regionFieldOf, regionSelect, regionsTouched, regionType, regionValues, resolveRegions, setDocPath, valueAtDocPath, type RegionOption, } from './regions.js';
|
|
29
|
-
export { collectReferences, readReferences, referenceKey, referenceTargets, sameReferences, type Reference, type ReferenceTargets } from './references.js';
|
|
32
|
+
export { collectReferences, fieldReferences, readReferences, referenceKey, referenceTargets, sameReferences, type Reference, type ReferenceTargets } from './references.js';
|
package/dist/core/index.js
CHANGED
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
export * from './types.js';
|
|
4
4
|
export { checkAttrs, htmlAttributes, htmlIdClash, htmlIdProblem, htmlIdsOf, patchAttrs, withoutTakenHtmlIds } from './attrs.js';
|
|
5
5
|
export { createId } from './ids.js';
|
|
6
|
+
export { duplicateOperation, freeValue, uniquePropClashes, uniquePropValues, uniquePropValuesAt, uniqueScopeOf, withUniqueProps } from './uniqueProps.js';
|
|
7
|
+
export { newBlockOf } from './starter.js';
|
|
6
8
|
export { findBlock, findLocation, walkBlocks, isSelfOrDescendant, normalizeLayout } from './tree.js';
|
|
7
9
|
export { applyOperation, applyOperations } from './operations.js';
|
|
8
10
|
export { layerAt, reachableAt } from './layers.js';
|
|
9
11
|
export { acceptingSlots, deepestBlockAt, canvasDropTarget, outlineDropTarget } from './dropTarget.js';
|
|
10
|
-
export { blockParts, collectClasses, defaultPartClassNames } from './classes.js';
|
|
12
|
+
export { blockParts, collectClasses, defaultPartClassNames, partClassNameOf } from './classes.js';
|
|
11
13
|
export { headingId, richTextHeadings } from './headings.js';
|
|
12
14
|
export { blockJsonSchema, layoutJsonSchema } from './schema.js';
|
|
13
15
|
export { validateLayout, isBlockingError, isLayoutWarning, PUBLISH_ONLY_CODES } from './validate.js';
|
|
@@ -18,7 +20,7 @@ export * from './bindings.js';
|
|
|
18
20
|
export * from './visibility.js';
|
|
19
21
|
export * from './components.js';
|
|
20
22
|
export { TEXT_LIST_BLOCK, TEXT_LIST_ITEM_BLOCK, TEXT_LIST_SLOT, joinListItem, splitListItem } from './textList.js';
|
|
21
|
-
export { blockName, defineBlock, defineSection, fitsParent, getBlockDefinition, placementError, checkSectionVariants, sectionsWithDefaultClassNames, sectionsWithVariants, slotAccepts, slotAcceptsAt, slotNames, starterSlots, withDefaultClasses, withDefaultClassNames, withInlineBlocks } from './blocks.js';
|
|
23
|
+
export { ancestorsError, blockName, defineBlock, defineSection, fitsParent, getBlockDefinition, placementError, upgradeLayout, checkSectionVariants, sectionsWithDefaultClassNames, sectionsWithVariants, slotAccepts, slotAcceptsAt, slotNames, starterSlots, withDefaultClasses, withDefaultClassNames, withInlineBlocks } from './blocks.js';
|
|
22
24
|
export { carryContent, DEFAULT_VARIANT_LABEL, findSection, findSectionVersion, hasVariants, listedVariants, sectionVersion, sectionVersions, switchSectionVariant, versionIndex, withoutSectionOrigin, withSectionOrigin, } from './sections.js';
|
|
23
25
|
export { checkSectionOrigin } from './sectionOrigin.js';
|
|
24
26
|
export { FORMATS, formatProblem, isPlayableVideoUrl, parseVideoUrl, videoUrlProblem } from './formats.js';
|
|
@@ -27,5 +29,6 @@ export { conditionFromFunction, conditionMet, readCondition } from './conditions
|
|
|
27
29
|
export { convertPayloadBlocksLayout, definitionForBlockType, isPayloadBlocksValue, payloadSlugOf, toPayloadBlock, withFieldDefaults, withLayoutDefaults, hasStaticDefault, } from './convertPayload.js';
|
|
28
30
|
export { decodePayloadBlocks, encodePayloadBlocks, payloadBlocksProblems, payloadBlocksStorage, } from './payloadBlocks.js';
|
|
29
31
|
export { dataFields, fieldAtPropPath } from './fields.js';
|
|
32
|
+
export { rowLabel } from './rowLabel.js';
|
|
30
33
|
export { pathWithoutIndexes, regionAccessAt, regionDefinition, regionFieldOf, regionSelect, regionsTouched, regionType, regionValues, resolveRegions, setDocPath, valueAtDocPath, } from './regions.js';
|
|
31
|
-
export { collectReferences, readReferences, referenceKey, referenceTargets, sameReferences } from './references.js';
|
|
34
|
+
export { collectReferences, fieldReferences, readReferences, referenceKey, referenceTargets, sameReferences } from './references.js';
|
|
@@ -61,9 +61,10 @@ export function decodePayloadBlocks(value, storage) {
|
|
|
61
61
|
}
|
|
62
62
|
seen.add(id);
|
|
63
63
|
const slotNames = Object.keys(def.slots ?? {});
|
|
64
|
+
const classKeys = def.payload?.classKeys ?? {};
|
|
64
65
|
const props = {};
|
|
65
66
|
for (const [key, raw] of Object.entries(row)) {
|
|
66
|
-
if (ROW_KEYS.has(key) || raw === undefined || slotNames.includes(key))
|
|
67
|
+
if (ROW_KEYS.has(key) || raw === undefined || slotNames.includes(key) || key === classKeys.className || key === classKeys.partClassName)
|
|
67
68
|
continue;
|
|
68
69
|
props[key] = raw;
|
|
69
70
|
}
|
|
@@ -82,6 +83,16 @@ export function decodePayloadBlocks(value, storage) {
|
|
|
82
83
|
block.slots = slots;
|
|
83
84
|
if (typeof row.blockName === 'string' && row.blockName !== '')
|
|
84
85
|
block.label = row.blockName;
|
|
86
|
+
// Classes the block's class keys store (a form field's `cssClass`). Empty values mean none.
|
|
87
|
+
const className = classKeys.className ? row[classKeys.className] : undefined;
|
|
88
|
+
if (typeof className === 'string' && className.trim())
|
|
89
|
+
block.className = className;
|
|
90
|
+
const parts = classKeys.partClassName ? row[classKeys.partClassName] : undefined;
|
|
91
|
+
if (isPlainObject(parts)) {
|
|
92
|
+
const kept = Object.fromEntries(Object.entries(parts).filter((entry) => typeof entry[1] === 'string' && entry[1].trim() !== ''));
|
|
93
|
+
if (Object.keys(kept).length > 0)
|
|
94
|
+
block.partClassName = kept;
|
|
95
|
+
}
|
|
85
96
|
return block;
|
|
86
97
|
};
|
|
87
98
|
const blocks = readList(value, '');
|
|
@@ -105,7 +116,8 @@ export function payloadBlocksProblems(layout, storage) {
|
|
|
105
116
|
}
|
|
106
117
|
if (allowed && !allowed.includes(block.type))
|
|
107
118
|
problems.push(`${name} blocks are not allowed here.`);
|
|
108
|
-
|
|
119
|
+
const classKeys = def.payload?.classKeys;
|
|
120
|
+
if ((block.className && !classKeys?.className) || (block.partClassName && !classKeys?.partClassName))
|
|
109
121
|
problems.push(`${name}: this field cannot store Tailwind classes.`);
|
|
110
122
|
if (block.motion)
|
|
111
123
|
problems.push(`${name}: this field cannot store animations.`);
|
|
@@ -149,10 +161,15 @@ export function encodePayloadBlocks(layout, storage, previous) {
|
|
|
149
161
|
const row = { id: block.id, blockType: def ? slugOf(def) : block.type };
|
|
150
162
|
if (block.label)
|
|
151
163
|
row.blockName = block.label;
|
|
164
|
+
const classKeys = def?.payload?.classKeys ?? {};
|
|
152
165
|
for (const [key, value] of Object.entries(block.props ?? {})) {
|
|
153
|
-
if (!ROW_KEYS.has(key))
|
|
166
|
+
if (!ROW_KEYS.has(key) && key !== classKeys.className && key !== classKeys.partClassName)
|
|
154
167
|
row[key] = value;
|
|
155
168
|
}
|
|
169
|
+
if (classKeys.className && block.className)
|
|
170
|
+
row[classKeys.className] = block.className;
|
|
171
|
+
if (classKeys.partClassName && block.partClassName && Object.keys(block.partClassName).length > 0)
|
|
172
|
+
row[classKeys.partClassName] = block.partClassName;
|
|
156
173
|
for (const slot of Object.keys(def?.slots ?? {}))
|
|
157
174
|
row[slot] = writeList(block.slots?.[slot] ?? []);
|
|
158
175
|
return row;
|
|
@@ -189,6 +206,11 @@ function clearRemoved(rows, previous, storage) {
|
|
|
189
206
|
row.blockName = null;
|
|
190
207
|
if (def)
|
|
191
208
|
clearFields(def.fields, old, row, storage);
|
|
209
|
+
// Cleared classes: the class keys are not in the definition's fields.
|
|
210
|
+
for (const key of Object.values(def?.payload?.classKeys ?? {})) {
|
|
211
|
+
if (!(key in row) && old[key] !== undefined && old[key] !== null)
|
|
212
|
+
row[key] = null;
|
|
213
|
+
}
|
|
192
214
|
}
|
|
193
215
|
for (const slot of Object.keys(def?.slots ?? {})) {
|
|
194
216
|
const children = row[slot];
|
|
@@ -11,6 +11,13 @@ export type ReferenceTargets = {
|
|
|
11
11
|
};
|
|
12
12
|
/** A stable key of a reference. "5" and 5 give the same key. */
|
|
13
13
|
export declare function referenceKey(ref: Reference): string;
|
|
14
|
+
/**
|
|
15
|
+
* Every document one document's data references, read along its Payload field config: upload and
|
|
16
|
+
* relationship fields (also inside groups, tabs, arrays and blocks fields), rich text nodes, and
|
|
17
|
+
* `{ relationTo, value }` pairs inside JSON fields (a builder layout). In field order, without
|
|
18
|
+
* duplicates. Use it for any collection, not only builder layouts: "is this image used anywhere?".
|
|
19
|
+
*/
|
|
20
|
+
export declare function fieldReferences(fields: readonly unknown[] | undefined, data: Record<string, unknown>): Reference[];
|
|
14
21
|
/**
|
|
15
22
|
* Every document a layout references, in layout order, without duplicates: upload and
|
|
16
23
|
* relationship props (also inside groups, arrays and blocks fields), link groups that point at a
|
package/dist/core/references.js
CHANGED
|
@@ -129,6 +129,33 @@ function visitFields(fields, props, add) {
|
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
|
+
/** A reference collector that skips empty IDs and duplicates. */
|
|
133
|
+
function collector() {
|
|
134
|
+
const found = new Map();
|
|
135
|
+
return {
|
|
136
|
+
add: (relationTo, value) => {
|
|
137
|
+
const id = idOf(value);
|
|
138
|
+
if (id === null)
|
|
139
|
+
return;
|
|
140
|
+
const ref = { relationTo, value: id };
|
|
141
|
+
const key = referenceKey(ref);
|
|
142
|
+
if (!found.has(key))
|
|
143
|
+
found.set(key, ref);
|
|
144
|
+
},
|
|
145
|
+
list: () => [...found.values()],
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Every document one document's data references, read along its Payload field config: upload and
|
|
150
|
+
* relationship fields (also inside groups, tabs, arrays and blocks fields), rich text nodes, and
|
|
151
|
+
* `{ relationTo, value }` pairs inside JSON fields (a builder layout). In field order, without
|
|
152
|
+
* duplicates. Use it for any collection, not only builder layouts: "is this image used anywhere?".
|
|
153
|
+
*/
|
|
154
|
+
export function fieldReferences(fields, data) {
|
|
155
|
+
const refs = collector();
|
|
156
|
+
visitFields(fields, data, refs.add);
|
|
157
|
+
return refs.list();
|
|
158
|
+
}
|
|
132
159
|
/**
|
|
133
160
|
* Every document a layout references, in layout order, without duplicates: upload and
|
|
134
161
|
* relationship props (also inside groups, arrays and blocks fields), link groups that point at a
|
|
@@ -139,16 +166,7 @@ function visitFields(fields, props, add) {
|
|
|
139
166
|
*/
|
|
140
167
|
export function collectReferences(layout, blocks) {
|
|
141
168
|
const definitions = new Map(blocks.map((definition) => [definition.type, definition]));
|
|
142
|
-
const
|
|
143
|
-
const add = (relationTo, value) => {
|
|
144
|
-
const id = idOf(value);
|
|
145
|
-
if (id === null)
|
|
146
|
-
return;
|
|
147
|
-
const ref = { relationTo, value: id };
|
|
148
|
-
const key = referenceKey(ref);
|
|
149
|
-
if (!found.has(key))
|
|
150
|
-
found.set(key, ref);
|
|
151
|
-
};
|
|
169
|
+
const { add, list } = collector();
|
|
152
170
|
walkBlocks(layout, (block) => {
|
|
153
171
|
const definition = definitions.get(block.type);
|
|
154
172
|
if (!definition)
|
|
@@ -159,7 +177,7 @@ export function collectReferences(layout, blocks) {
|
|
|
159
177
|
for (const values of Object.values(block.locales ?? {}))
|
|
160
178
|
visitFields(definition.fields, values, add);
|
|
161
179
|
});
|
|
162
|
-
return
|
|
180
|
+
return list();
|
|
163
181
|
}
|
|
164
182
|
/**
|
|
165
183
|
* The collections the blocks' upload, relationship and link fields point at, at any depth, in
|
package/dist/core/regions.d.ts
CHANGED
|
@@ -7,6 +7,20 @@ export type RegionOption = {
|
|
|
7
7
|
label?: string;
|
|
8
8
|
/** Dot paths of the document fields the region shows, e.g. `['title', 'header.image']`. */
|
|
9
9
|
fields: string[];
|
|
10
|
+
/**
|
|
11
|
+
* The inspector shows the region's fields itself, also when no block is selected, instead of
|
|
12
|
+
* links to the settings drawer. For document settings that sit next to the layout.
|
|
13
|
+
*/
|
|
14
|
+
panel?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Panel regions: sections with a heading that opens and closes them, each over some of the
|
|
17
|
+
* region's fields (in the region's order). The first section starts open. Fields in no section
|
|
18
|
+
* show above the sections; a `group` field in no section gets its own section.
|
|
19
|
+
*/
|
|
20
|
+
sections?: Array<{
|
|
21
|
+
label: string;
|
|
22
|
+
fields: string[];
|
|
23
|
+
}>;
|
|
10
24
|
};
|
|
11
25
|
/**
|
|
12
26
|
* The region's fields, read from the collection's field configs. Throws a readable error for a
|
package/dist/core/regions.js
CHANGED
|
@@ -104,7 +104,23 @@ export function resolveRegions(collection, fields, options, reserved) {
|
|
|
104
104
|
tabs,
|
|
105
105
|
};
|
|
106
106
|
});
|
|
107
|
-
|
|
107
|
+
const sections = (option.sections ?? []).map((section) => {
|
|
108
|
+
if (!section || typeof section.label !== 'string' || !Array.isArray(section.fields)) {
|
|
109
|
+
throw new Error(`${where}: each of the \`sections\` of region "${option.name}" needs a \`label\` and a \`fields\` list.`);
|
|
110
|
+
}
|
|
111
|
+
for (const path of section.fields) {
|
|
112
|
+
if (!resolved.some((f) => f.path === path))
|
|
113
|
+
throw new Error(`${where}: section "${section.label}" of region "${option.name}" names "${String(path)}", which the region does not show.`);
|
|
114
|
+
}
|
|
115
|
+
return { label: section.label, fields: [...section.fields] };
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
name: option.name,
|
|
119
|
+
label: option.label?.trim() || humanize(option.name),
|
|
120
|
+
fields: resolved,
|
|
121
|
+
...(option.panel ? { panel: true } : {}),
|
|
122
|
+
...(sections.length > 0 ? { sections } : {}),
|
|
123
|
+
};
|
|
108
124
|
});
|
|
109
125
|
}
|
|
110
126
|
const isIndex = (segment) => /^\d+$/.test(segment);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The label of an array row: the value of its most telling text field (a field named label,
|
|
3
|
+
* title, name, heading, question, text, value, url, …; else the first text field), also inside
|
|
4
|
+
* groups. A label with a different `value` beside it shows both: "Contact (contact)", and a
|
|
5
|
+
* `value` with a `field` or `key` beside it shows "email: ada@example.com". Without
|
|
6
|
+
* any text, the singular name and the 1-based row number: "Item 1".
|
|
7
|
+
*/
|
|
8
|
+
export declare function rowLabel(row: unknown, fields: readonly unknown[] | undefined, index: number, singular?: string): string;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The title of one array row, read from its own values: "Contact (contact)" instead of "Item 01".
|
|
2
|
+
// Pure, so the builder's inspector and the Payload admin (through a RowLabel component) name rows
|
|
3
|
+
// the same way.
|
|
4
|
+
import { richTextToPlain } from './bindings.js';
|
|
5
|
+
import { dataFields, optionList } from './fields.js';
|
|
6
|
+
import { isPlainObject } from './tree.js';
|
|
7
|
+
/** Field names that usually hold a row's title, best first. */
|
|
8
|
+
const TITLE_NAMES = [
|
|
9
|
+
'label',
|
|
10
|
+
'title',
|
|
11
|
+
'name',
|
|
12
|
+
'heading',
|
|
13
|
+
'question',
|
|
14
|
+
'text',
|
|
15
|
+
'value',
|
|
16
|
+
'url',
|
|
17
|
+
'href',
|
|
18
|
+
'slug',
|
|
19
|
+
'email',
|
|
20
|
+
'caption',
|
|
21
|
+
'alt',
|
|
22
|
+
];
|
|
23
|
+
const RANK = new Map(TITLE_NAMES.map((name, rank) => [name.toLowerCase(), rank]));
|
|
24
|
+
/** Field types whose value reads as text. */
|
|
25
|
+
const TEXT_TYPES = new Set(['text', 'textarea', 'email', 'select', 'radio', 'number', 'code', 'richText']);
|
|
26
|
+
/** Longest label, in characters. Longer values end with "…". */
|
|
27
|
+
const MAX_LENGTH = 60;
|
|
28
|
+
/** One line of text, at most `MAX_LENGTH` characters. */
|
|
29
|
+
function oneLine(text) {
|
|
30
|
+
const line = text.replace(/\s+/g, ' ').trim();
|
|
31
|
+
return line.length > MAX_LENGTH ? `${line.slice(0, MAX_LENGTH - 1).trimEnd()}…` : line;
|
|
32
|
+
}
|
|
33
|
+
/** A field value as label text: option labels for selects, plain text for rich text. */
|
|
34
|
+
function valueText(field, value) {
|
|
35
|
+
if (field.type === 'richText')
|
|
36
|
+
return oneLine(richTextToPlain(value));
|
|
37
|
+
if (field.type === 'select' || field.type === 'radio') {
|
|
38
|
+
const options = optionList(field);
|
|
39
|
+
const labelOf = (item) => options.find((option) => option.value === item)?.label ?? (typeof item === 'string' ? item : '');
|
|
40
|
+
return oneLine((Array.isArray(value) ? value : [value]).map(labelOf).filter(Boolean).join(', '));
|
|
41
|
+
}
|
|
42
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
43
|
+
return String(value);
|
|
44
|
+
if (typeof value === 'string')
|
|
45
|
+
return oneLine(value);
|
|
46
|
+
if (Array.isArray(value))
|
|
47
|
+
return oneLine(value.filter((item) => typeof item === 'string' || typeof item === 'number').join(', '));
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
/** The text values of a row and its groups, in field order. Groups count one level deeper. */
|
|
51
|
+
function candidates(fields, data, depth, out) {
|
|
52
|
+
const level = [];
|
|
53
|
+
for (const field of dataFields(fields)) {
|
|
54
|
+
const value = data[field.name];
|
|
55
|
+
if (field.type === 'group' && isPlainObject(value)) {
|
|
56
|
+
candidates(field.fields, value, depth + 1, out);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (!TEXT_TYPES.has(field.type))
|
|
60
|
+
continue;
|
|
61
|
+
const text = valueText(field, value);
|
|
62
|
+
if (!text)
|
|
63
|
+
continue;
|
|
64
|
+
const candidate = { name: field.name, rank: RANK.get(field.name.toLowerCase()) ?? TITLE_NAMES.length, depth, order: out.length, text, siblings: level };
|
|
65
|
+
level.push(candidate);
|
|
66
|
+
out.push(candidate);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The label of an array row: the value of its most telling text field (a field named label,
|
|
72
|
+
* title, name, heading, question, text, value, url, …; else the first text field), also inside
|
|
73
|
+
* groups. A label with a different `value` beside it shows both: "Contact (contact)", and a
|
|
74
|
+
* `value` with a `field` or `key` beside it shows "email: ada@example.com". Without
|
|
75
|
+
* any text, the singular name and the 1-based row number: "Item 1".
|
|
76
|
+
*/
|
|
77
|
+
export function rowLabel(row, fields, index, singular = 'Item') {
|
|
78
|
+
const fallback = `${singular || 'Item'} ${index + 1}`;
|
|
79
|
+
if (!isPlainObject(row))
|
|
80
|
+
return fallback;
|
|
81
|
+
const found = candidates(fields, row, 0, []);
|
|
82
|
+
if (found.length === 0)
|
|
83
|
+
return fallback;
|
|
84
|
+
const best = found.toSorted((a, b) => a.rank - b.rank || a.depth - b.depth || a.order - b.order)[0];
|
|
85
|
+
const sibling = (name) => best.siblings.find((candidate) => candidate.name.toLowerCase() === name);
|
|
86
|
+
const name = best.name.toLowerCase();
|
|
87
|
+
if (name === 'value') {
|
|
88
|
+
// Key-value rows (a form submission's answers): "email: ada@example.com".
|
|
89
|
+
const key = sibling('field') ?? sibling('key');
|
|
90
|
+
return key ? `${key.text}: ${best.text}` : best.text;
|
|
91
|
+
}
|
|
92
|
+
if (name !== 'label')
|
|
93
|
+
return best.text;
|
|
94
|
+
const value = sibling('value');
|
|
95
|
+
if (!value || value.text.toLowerCase() === best.text.toLowerCase())
|
|
96
|
+
return best.text;
|
|
97
|
+
return `${best.text} (${value.text})`;
|
|
98
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Block, BlockDefinition } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A new block of `type` as the editor adds it: the type's default classes, its field defaults,
|
|
4
|
+
* then its `starter` (props over the defaults, children per slot), else its `starterSlots` (a new
|
|
5
|
+
* list starts with one list item). `classes: false`: no default classes (a Payload blocks field
|
|
6
|
+
* without class storage).
|
|
7
|
+
*/
|
|
8
|
+
export declare function newBlockOf(blocks: readonly BlockDefinition[], type: string, makeId: () => string, options?: {
|
|
9
|
+
classes?: boolean;
|
|
10
|
+
}): Block | null;
|