payload-canvas 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -26
- package/dist/admin/editor/Canvas.js +30 -8
- package/dist/admin/editor/Editor.js +2 -0
- package/dist/admin/editor/Inspector.js +14 -7
- package/dist/admin/editor/Outline.js +15 -9
- package/dist/admin/editor/Overlay.js +2 -1
- package/dist/admin/editor/actions.d.ts +5 -0
- package/dist/admin/editor/actions.js +23 -1
- package/dist/admin/editor/counts.d.ts +6 -0
- package/dist/admin/editor/counts.js +10 -0
- package/dist/admin/editor/dnd/displace.js +3 -1
- package/dist/admin/editor/editor.scss +53 -0
- package/dist/admin/editor/inline.d.ts +12 -0
- package/dist/admin/editor/inline.js +48 -3
- package/dist/admin/editor/insert/spots.js +4 -2
- package/dist/admin/editor/live/useMultiplayer.js +8 -2
- package/dist/admin/editor/media/ImageEditor.js +21 -15
- package/dist/admin/editor/media/actions.d.ts +13 -9
- package/dist/admin/editor/media/actions.js +67 -28
- package/dist/admin/editor/media/model.d.ts +8 -0
- package/dist/admin/editor/media/model.js +14 -6
- package/dist/admin/editor/menu/blockMenu.js +13 -8
- package/dist/admin/editor/regions/RegionOverlay.d.ts +18 -0
- package/dist/admin/editor/regions/RegionOverlay.js +44 -0
- package/dist/admin/editor/regions/RegionPane.d.ts +5 -0
- package/dist/admin/editor/regions/RegionPane.js +26 -0
- package/dist/admin/editor/regions/RegionRows.d.ts +1 -0
- package/dist/admin/editor/regions/RegionRows.js +23 -0
- package/dist/admin/editor/regions/controller.d.ts +73 -0
- package/dist/admin/editor/regions/controller.js +231 -0
- package/dist/admin/editor/regions/select.d.ts +17 -0
- package/dist/admin/editor/regions/select.js +31 -0
- package/dist/admin/editor/runtime.d.ts +6 -0
- package/dist/admin/editor/runtime.js +49 -4
- package/dist/admin/editor/sections/controller.d.ts +6 -2
- package/dist/admin/editor/sections/controller.js +9 -3
- package/dist/admin/editor/shortcuts.js +11 -4
- package/dist/admin/editor/storage.d.ts +7 -0
- package/dist/admin/editor/storage.js +15 -0
- package/dist/admin/editor/store.d.ts +39 -0
- package/dist/admin/editor/store.js +106 -2
- package/dist/admin/editor/templates/Bindable.js +1 -1
- package/dist/admin/editor/templates/SamplePicker.js +1 -1
- package/dist/admin/editor/topbar/DocumentActions.js +38 -5
- package/dist/admin/editor/topbar/closeSave.d.ts +50 -0
- package/dist/admin/editor/topbar/closeSave.js +90 -0
- package/dist/admin/editor/topbar/document.d.ts +10 -2
- package/dist/admin/editor/topbar/document.js +9 -1
- package/dist/admin/editor/topbar/drawerLabels.d.ts +22 -0
- package/dist/admin/editor/topbar/drawerLabels.js +69 -0
- package/dist/admin/editor/topbar/problems.d.ts +9 -0
- package/dist/admin/editor/topbar/problems.js +15 -1
- package/dist/admin/editor/topbar/settingsDrawer.js +88 -4
- package/dist/admin/editor/topbar/settingsFocus.d.ts +11 -0
- package/dist/admin/editor/topbar/settingsFocus.js +46 -0
- package/dist/admin/editor/ui/payloadA11y.d.ts +17 -0
- package/dist/admin/editor/ui/payloadA11y.js +12 -5
- package/dist/admin/editor/valueStore.d.ts +2 -0
- package/dist/admin/editor/valueStore.js +6 -0
- package/dist/core/dropTarget.js +3 -1
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/payloadBlocks.d.ts +53 -0
- package/dist/core/payloadBlocks.js +391 -0
- package/dist/core/regions.d.ts +63 -0
- package/dist/core/regions.js +239 -0
- package/dist/core/types.d.ts +53 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/live/document.d.ts +9 -1
- package/dist/live/document.js +57 -17
- package/dist/live/draftAccess.d.ts +36 -0
- package/dist/live/draftAccess.js +146 -0
- package/dist/live/endpoints.d.ts +11 -2
- package/dist/live/endpoints.js +120 -10
- package/dist/live/fieldChecks.d.ts +3 -3
- package/dist/live/fieldsGuard.d.ts +56 -2
- package/dist/live/fieldsGuard.js +107 -11
- package/dist/live/index.d.ts +2 -0
- package/dist/live/index.js +2 -0
- package/dist/live/regions.d.ts +89 -0
- package/dist/live/regions.js +401 -0
- package/dist/live/runtime.d.ts +1 -1
- package/dist/live/runtime.js +5 -2
- package/dist/live/session.d.ts +74 -9
- package/dist/live/session.js +655 -123
- package/dist/live/storage.d.ts +97 -0
- package/dist/live/storage.js +269 -0
- package/dist/live/types.d.ts +64 -0
- package/dist/live/writeGate.d.ts +72 -0
- package/dist/live/writeGate.js +478 -0
- package/dist/mcp/index.js +56 -12
- package/dist/plugin/hook.d.ts +44 -6
- package/dist/plugin/hook.js +176 -20
- package/dist/plugin/index.d.ts +23 -1
- package/dist/plugin/index.js +172 -10
- package/dist/plugin/references.d.ts +4 -2
- package/dist/plugin/references.js +2 -0
- package/dist/plugin/sectionsBuilder.js +3 -3
- package/dist/protocol/index.d.ts +28 -2
- package/dist/react/canvas/BuilderCanvas.js +198 -8
- package/dist/react/canvas/frame.d.ts +70 -0
- package/dist/react/canvas/frame.js +66 -0
- package/dist/react/canvas/inline/dom.d.ts +7 -1
- package/dist/react/canvas/inline/dom.js +19 -9
- package/dist/react/canvas/inline/mapper.d.ts +26 -0
- package/dist/react/canvas/inline/mapper.js +59 -32
- package/dist/react/canvas/inline/session.d.ts +7 -1
- package/dist/react/canvas/inline/session.js +6 -2
- package/dist/react/canvas/measure.js +26 -1
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +2 -0
- package/dist/react/render/LayoutOutlet.d.ts +11 -0
- package/dist/react/render/LayoutOutlet.js +16 -0
- package/dist/react/render/canvasFrame.d.ts +46 -0
- package/dist/react/render/canvasFrame.js +59 -0
- package/dist/react/render/canvasServer.d.ts +13 -0
- package/dist/react/render/canvasServer.js +3 -0
- package/dist/react/render/canvasServerTypes.d.ts +17 -0
- package/dist/react/render/regions.d.ts +13 -0
- package/dist/react/render/regions.js +8 -0
- package/dist/react/server.d.ts +2 -1
- package/dist/react/server.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// Page regions: fixed parts of a page that show ordinary document fields outside the layout.
|
|
2
|
+
// Pure helpers shared by the plugin (config check, endpoints), the editor and the canvas.
|
|
3
|
+
// See docs/architecture.md section 7, "Page frame and regions".
|
|
4
|
+
import { textOf } from './fields.js';
|
|
5
|
+
import { isPlainObject } from './tree.js';
|
|
6
|
+
/** Fields the plugin or Payload owns. A region never edits them. */
|
|
7
|
+
const SYSTEM_FIELDS = new Set(['id', '_status', 'createdAt', 'updatedAt', 'deletedAt']);
|
|
8
|
+
const NAME = /^[A-Za-z][\w-]{0,63}$/;
|
|
9
|
+
const PATH = /^[A-Za-z_$][\w$-]*(\.[A-Za-z_$][\w$-]*)*$/;
|
|
10
|
+
const humanize = (name) => name
|
|
11
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
12
|
+
.replace(/[_-]+/g, ' ')
|
|
13
|
+
.trim()
|
|
14
|
+
.replace(/^./, (c) => c.toUpperCase());
|
|
15
|
+
/** The fields one level down, with the tab labels on the way. Rows, collapsibles, unnamed groups and unnamed tabs are flattened. */
|
|
16
|
+
function levelFields(fields, tabs = []) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const raw of Array.isArray(fields) ? fields : []) {
|
|
19
|
+
if (!isPlainObject(raw) || typeof raw.type !== 'string')
|
|
20
|
+
continue;
|
|
21
|
+
const field = raw;
|
|
22
|
+
if (field.type === 'row' || field.type === 'collapsible' || (field.type === 'group' && !field.name)) {
|
|
23
|
+
out.push(...levelFields(field.fields, tabs));
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (field.type === 'tabs') {
|
|
27
|
+
for (const tab of Array.isArray(field.tabs) ? field.tabs : []) {
|
|
28
|
+
if (!isPlainObject(tab))
|
|
29
|
+
continue;
|
|
30
|
+
const name = typeof tab.name === 'string' && tab.name ? tab.name : null;
|
|
31
|
+
const label = textOf(tab.label) ?? (name ? humanize(name) : '');
|
|
32
|
+
const inside = label ? [...tabs, label] : tabs;
|
|
33
|
+
// A named tab holds its fields under its name, like a group.
|
|
34
|
+
if (name)
|
|
35
|
+
out.push({ field: { ...tab, type: 'group', name, fields: tab.fields }, tabs: inside });
|
|
36
|
+
else
|
|
37
|
+
out.push(...levelFields(tab.fields, inside));
|
|
38
|
+
}
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (typeof field.name === 'string' && field.name)
|
|
42
|
+
out.push({ field, tabs });
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The region's fields, read from the collection's field configs. Throws a readable error for a
|
|
48
|
+
* name or path the region cannot use. `reserved`: the layout field and the plugin's own fields.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveRegions(collection, fields, options, reserved) {
|
|
51
|
+
const where = `[websiteBuilder] \`regions\` of "${collection}"`;
|
|
52
|
+
const names = new Set();
|
|
53
|
+
const paths = new Map();
|
|
54
|
+
return options.map((option) => {
|
|
55
|
+
if (!option || typeof option.name !== 'string' || !NAME.test(option.name)) {
|
|
56
|
+
throw new Error(`${where}: a region needs a \`name\` of letters, digits, "-" or "_" that starts with a letter.`);
|
|
57
|
+
}
|
|
58
|
+
if (names.has(option.name))
|
|
59
|
+
throw new Error(`${where}: two regions are named "${option.name}". Region names must be unique.`);
|
|
60
|
+
names.add(option.name);
|
|
61
|
+
if (!Array.isArray(option.fields))
|
|
62
|
+
throw new Error(`${where}: region "${option.name}" needs a \`fields\` list (it may be empty).`);
|
|
63
|
+
const resolved = option.fields.map((path) => {
|
|
64
|
+
const fail = (why) => {
|
|
65
|
+
throw new Error(`${where}: region "${option.name}" names "${String(path)}", ${why}`);
|
|
66
|
+
};
|
|
67
|
+
if (typeof path !== 'string' || !PATH.test(path))
|
|
68
|
+
fail('which is not a dot path of field names (for example "title" or "header.title").');
|
|
69
|
+
const segments = path.split('.');
|
|
70
|
+
if (reserved.fields.has(segments[0]))
|
|
71
|
+
fail('which the builder owns. Regions show other document fields.');
|
|
72
|
+
if (SYSTEM_FIELDS.has(segments[0]))
|
|
73
|
+
fail('which Payload owns. Regions show fields the editors write.');
|
|
74
|
+
if (reserved.legacy?.has(segments[0]))
|
|
75
|
+
fail('which is listed in `legacyFields`. Publish keeps the published value of those fields, so region edits would never go live.');
|
|
76
|
+
for (const [other, region] of paths) {
|
|
77
|
+
if (other === path || other.startsWith(`${path}.`) || path.startsWith(`${other}.`)) {
|
|
78
|
+
fail(`which ${region === option.name ? 'the region' : `region "${region}"`} already shows (as "${other}"). Each field belongs to one region.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
let level = fields;
|
|
82
|
+
let found;
|
|
83
|
+
let tabs = [];
|
|
84
|
+
for (let i = 0; i < segments.length; i++) {
|
|
85
|
+
found = levelFields(level, tabs).find((entry) => entry.field.name === segments[i]);
|
|
86
|
+
if (!found)
|
|
87
|
+
fail('which is not a field of the collection.');
|
|
88
|
+
const field = found.field;
|
|
89
|
+
tabs = found.tabs;
|
|
90
|
+
if (field.type === 'ui' || field.type === 'join' || field.virtual)
|
|
91
|
+
fail(`which is a ${field.virtual ? 'virtual' : String(field.type)} field and stores nothing.`);
|
|
92
|
+
if (i < segments.length - 1) {
|
|
93
|
+
if (field.type !== 'group')
|
|
94
|
+
fail(`which goes inside a "${String(field.type)}" field. A path may go through groups and named tabs only; name the array or blocks field itself.`);
|
|
95
|
+
level = field.fields;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const field = found.field;
|
|
99
|
+
paths.set(path, option.name);
|
|
100
|
+
return {
|
|
101
|
+
path,
|
|
102
|
+
field: field,
|
|
103
|
+
label: textOf(field.label) ?? humanize(String(field.name)),
|
|
104
|
+
tabs,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
return { name: option.name, label: option.label?.trim() || humanize(option.name), fields: resolved };
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const isIndex = (segment) => /^\d+$/.test(segment);
|
|
111
|
+
/** A path without its array row numbers: "links.2.label" -> "links.label". */
|
|
112
|
+
export const pathWithoutIndexes = (path) => path
|
|
113
|
+
.split('.')
|
|
114
|
+
.filter((segment) => !isIndex(segment))
|
|
115
|
+
.join('.');
|
|
116
|
+
/**
|
|
117
|
+
* The region and its field that hold a document path: the field's own path, or a path inside it
|
|
118
|
+
* ("links.0.label" inside the "links" array). Null when no region shows it.
|
|
119
|
+
*/
|
|
120
|
+
export function regionFieldOf(regions, path) {
|
|
121
|
+
for (const region of regions ?? []) {
|
|
122
|
+
for (const field of region.fields) {
|
|
123
|
+
if (path === field.path || path.startsWith(`${field.path}.`))
|
|
124
|
+
return { region, field };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
/** The value at a dot path of a document; number segments step into array rows. */
|
|
130
|
+
export function valueAtDocPath(doc, path) {
|
|
131
|
+
let current = doc;
|
|
132
|
+
for (const segment of path.split('.')) {
|
|
133
|
+
if (Array.isArray(current) && isIndex(segment))
|
|
134
|
+
current = current[Number(segment)];
|
|
135
|
+
else if (isPlainObject(current))
|
|
136
|
+
current = current[segment];
|
|
137
|
+
else
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
return current;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Sets the value at a dot path and returns the changed copy (objects and rows on the way are
|
|
144
|
+
* copied). Missing groups are made; a missing array row fails: the path is gone (someone removed
|
|
145
|
+
* the row). Returns `{ ok: false }` then.
|
|
146
|
+
*/
|
|
147
|
+
export function setDocPath(doc, path, value) {
|
|
148
|
+
const set = (current, segments) => {
|
|
149
|
+
const [segment, ...rest] = segments;
|
|
150
|
+
if (segment === undefined)
|
|
151
|
+
return { ok: true, value };
|
|
152
|
+
if (Array.isArray(current)) {
|
|
153
|
+
if (!isIndex(segment) || Number(segment) >= current.length)
|
|
154
|
+
return { ok: false };
|
|
155
|
+
const child = set(current[Number(segment)], rest);
|
|
156
|
+
if (!child.ok)
|
|
157
|
+
return child;
|
|
158
|
+
const next = current.slice();
|
|
159
|
+
next[Number(segment)] = child.value;
|
|
160
|
+
return { ok: true, value: next };
|
|
161
|
+
}
|
|
162
|
+
if (isIndex(segment))
|
|
163
|
+
return { ok: false };
|
|
164
|
+
const record = isPlainObject(current) ? current : {};
|
|
165
|
+
const child = set(record[segment], rest);
|
|
166
|
+
return child.ok ? { ok: true, value: { ...record, [segment]: child.value } } : child;
|
|
167
|
+
};
|
|
168
|
+
const result = set(isPlainObject(doc) ? doc : {}, path.split('.'));
|
|
169
|
+
return result.ok && isPlainObject(result.value) ? { ok: true, value: result.value } : { ok: false };
|
|
170
|
+
}
|
|
171
|
+
/** The document's values at the regions' paths, as one object of the document's shape. */
|
|
172
|
+
export function regionValues(doc, regions) {
|
|
173
|
+
let out = {};
|
|
174
|
+
for (const region of regions ?? []) {
|
|
175
|
+
for (const field of region.fields) {
|
|
176
|
+
const value = valueAtDocPath(doc, field.path);
|
|
177
|
+
if (value === undefined)
|
|
178
|
+
continue;
|
|
179
|
+
const next = setDocPath(out, field.path, value);
|
|
180
|
+
if (next.ok)
|
|
181
|
+
out = next.value;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
/** Payload's `select` for the regions' fields (with `updatedAt`), so a read loads nothing else. */
|
|
187
|
+
export function regionSelect(regions) {
|
|
188
|
+
let select = { updatedAt: true };
|
|
189
|
+
for (const region of regions ?? []) {
|
|
190
|
+
for (const field of region.fields) {
|
|
191
|
+
const next = setDocPath(select, field.path, true);
|
|
192
|
+
if (next.ok)
|
|
193
|
+
select = next.value;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return select;
|
|
197
|
+
}
|
|
198
|
+
/** The block type the editor and the canvas use for a region's fields. Never stored in a layout. */
|
|
199
|
+
export const regionType = (name) => `$region:${name}`;
|
|
200
|
+
/**
|
|
201
|
+
* A region's fields as a block definition, with groups along nested paths, so the inline editor,
|
|
202
|
+
* the automatic mapping and the image editor read the fields as they read block props. The
|
|
203
|
+
* definition never enters a layout and has no slots or styles.
|
|
204
|
+
*/
|
|
205
|
+
export function regionDefinition(region) {
|
|
206
|
+
const root = { children: new Map() };
|
|
207
|
+
for (const { path, field } of region.fields) {
|
|
208
|
+
let node = root;
|
|
209
|
+
const segments = path.split('.');
|
|
210
|
+
for (const segment of segments.slice(0, -1)) {
|
|
211
|
+
let child = node.children.get(segment);
|
|
212
|
+
if (!child) {
|
|
213
|
+
child = { children: new Map() };
|
|
214
|
+
node.children.set(segment, child);
|
|
215
|
+
}
|
|
216
|
+
node = child;
|
|
217
|
+
}
|
|
218
|
+
node.children.set(segments.at(-1), { field, children: new Map() });
|
|
219
|
+
}
|
|
220
|
+
const build = (node) => [...node.children].map(([name, child]) => child.field ?? { name, type: 'group', fields: build(child) });
|
|
221
|
+
return { type: regionType(region.name), label: region.label, fields: build(root), styles: false };
|
|
222
|
+
}
|
|
223
|
+
/** The region names a list of changed top-level fields touches. */
|
|
224
|
+
export function regionsTouched(regions, fields) {
|
|
225
|
+
const changed = new Set(fields);
|
|
226
|
+
return (regions ?? []).filter((region) => region.fields.some((field) => changed.has(field.path.split('.')[0]))).map((region) => region.name);
|
|
227
|
+
}
|
|
228
|
+
/** The rights at a document path: its own entry, else the nearest path above it. */
|
|
229
|
+
export function regionAccessAt(access, path) {
|
|
230
|
+
if (!access)
|
|
231
|
+
return { read: true, update: true };
|
|
232
|
+
const segments = pathWithoutIndexes(path).split('.');
|
|
233
|
+
for (let i = segments.length; i > 0; i--) {
|
|
234
|
+
const entry = access[segments.slice(0, i).join('.')];
|
|
235
|
+
if (entry)
|
|
236
|
+
return entry;
|
|
237
|
+
}
|
|
238
|
+
return { read: false, update: false };
|
|
239
|
+
}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -187,6 +187,52 @@ export type BuilderClientConfig = {
|
|
|
187
187
|
validateTypes?: string[];
|
|
188
188
|
/** The locales of the layout's localized props. Null without Payload localization (or when the collection turns it off). */
|
|
189
189
|
localization?: LocaleSettings | null;
|
|
190
|
+
/**
|
|
191
|
+
* Fixed parts of the page outside the layout (the collection's `regions` option). The canvas
|
|
192
|
+
* shows the page through the app's frame (`createCanvasServer({ frame })`) only when this has
|
|
193
|
+
* regions. Left out: the canvas shows the layout alone, as before.
|
|
194
|
+
*/
|
|
195
|
+
regions?: RegionConfig[];
|
|
196
|
+
/**
|
|
197
|
+
* `payload-blocks`: the layout field is the collection's own Payload `blocks` field, the one
|
|
198
|
+
* source of truth, and `blocks` lists only its block types. The builder then keeps nothing a
|
|
199
|
+
* blocks field cannot hold: no Tailwind classes, animations, bindings, hidden blocks or
|
|
200
|
+
* translations (translations are edited in Payload's editor). Left out: the builder's JSON layout.
|
|
201
|
+
*/
|
|
202
|
+
storage?: 'payload-blocks';
|
|
203
|
+
/** With `storage: 'payload-blocks'`: the block types the field allows at its top level. */
|
|
204
|
+
payloadRootTypes?: string[];
|
|
205
|
+
};
|
|
206
|
+
/** One document field a region shows. */
|
|
207
|
+
export type RegionField = {
|
|
208
|
+
/** Dot path from the document root: "title", or "header.title" inside a named group or tab. */
|
|
209
|
+
path: string;
|
|
210
|
+
/** The Payload field config (JSON-safe in the client config: functions are left out). */
|
|
211
|
+
field: Record<string, unknown> & {
|
|
212
|
+
name: string;
|
|
213
|
+
type: string;
|
|
214
|
+
};
|
|
215
|
+
/** The field's label, for the editor. */
|
|
216
|
+
label: string;
|
|
217
|
+
/** Labels of the tabs that hold the field, outermost first. The settings drawer opens them. */
|
|
218
|
+
tabs: string[];
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* A fixed part of a page that shows ordinary document fields, outside the layout: a header with
|
|
222
|
+
* the page title, an intro made of fields. The fields stay the source of truth. Editors can select
|
|
223
|
+
* the region and edit its text and images on the canvas, and edit every field in the settings
|
|
224
|
+
* drawer. A region is never dragged, duplicated or deleted.
|
|
225
|
+
*/
|
|
226
|
+
export type RegionConfig = {
|
|
227
|
+
/** Name of the region, unique in the collection. The app's frame marks the region with it. */
|
|
228
|
+
name: string;
|
|
229
|
+
label: string;
|
|
230
|
+
fields: RegionField[];
|
|
231
|
+
};
|
|
232
|
+
/** A region on the canvas: its name and its box, in iframe viewport coordinates. */
|
|
233
|
+
export type RegionRect = {
|
|
234
|
+
name: string;
|
|
235
|
+
rect: Rect;
|
|
190
236
|
};
|
|
191
237
|
/**
|
|
192
238
|
* What the builder needs from Payload's `localization` config (`localeSettingsOf`). Plain data, so
|
|
@@ -346,6 +392,13 @@ export type CanvasMeasurement = {
|
|
|
346
392
|
slots: SlotRect[];
|
|
347
393
|
/** Axis of the root list. */
|
|
348
394
|
rootAxis: Axis;
|
|
395
|
+
/**
|
|
396
|
+
* The box of the root list: the layout outlet inside the app's page frame. Left out without a
|
|
397
|
+
* frame (the layout fills the canvas). Fixed regions above or below it never count as layout.
|
|
398
|
+
*/
|
|
399
|
+
root?: Rect;
|
|
400
|
+
/** Fixed regions of the page frame. Left out without a frame. */
|
|
401
|
+
regions?: RegionRect[];
|
|
349
402
|
viewport: {
|
|
350
403
|
width: number;
|
|
351
404
|
height: number;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export { builderViewPath } from './plugin/links.js';
|
|
|
5
5
|
export { DEFAULT_TEMPLATES_SLUG, DOCUMENT_TEMPLATE_FIELD, TEMPLATE_DEFAULT_FIELD, TEMPLATE_LAYOUT_FIELD, TEMPLATE_PREVIEW_FIELD, TEMPLATE_TARGET_FIELD, } from './core/bindings.js';
|
|
6
6
|
export { defaultBlocks, fromPayloadBlocks, type DefaultBlocksOptions, type FromPayloadBlocksOptions, type PayloadBlockOverride } from './blocks/index.js';
|
|
7
7
|
export { convertPayloadBlocksLayout, type PayloadConversion, type PayloadConversionReport } from './core/convertPayload.js';
|
|
8
|
+
export { decodePayloadBlocks, encodePayloadBlocks, payloadBlocksProblems, payloadBlocksStorage, type PayloadBlocksDecode, type PayloadBlocksStorage, } from './core/payloadBlocks.js';
|
|
8
9
|
export { formatMigrationReport, migrateBlocksField, type MigrateBlocksOptions, type MigrateBlocksReport, type MigrateCounts, type MigrateIssue, } from './migrate/index.js';
|
|
9
10
|
export type { AiAdapter, AiChatRequest, AiClientConfig, AiMessage, AiOptions, AiStreamEvent } from './ai/types.js';
|
|
10
11
|
export { defineBlock } from './core/blocks.js';
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export { builderViewPath } from './plugin/links.js';
|
|
|
6
6
|
export { DEFAULT_TEMPLATES_SLUG, DOCUMENT_TEMPLATE_FIELD, TEMPLATE_DEFAULT_FIELD, TEMPLATE_LAYOUT_FIELD, TEMPLATE_PREVIEW_FIELD, TEMPLATE_TARGET_FIELD, } from './core/bindings.js';
|
|
7
7
|
export { defaultBlocks, fromPayloadBlocks } from './blocks/index.js';
|
|
8
8
|
export { convertPayloadBlocksLayout } from './core/convertPayload.js';
|
|
9
|
+
export { decodePayloadBlocks, encodePayloadBlocks, payloadBlocksProblems, payloadBlocksStorage, } from './core/payloadBlocks.js';
|
|
9
10
|
export { formatMigrationReport, migrateBlocksField, } from './migrate/index.js';
|
|
10
11
|
export { defineBlock } from './core/blocks.js';
|
|
11
12
|
export { EMPTY_LAYOUT } from './core/types.js';
|
package/dist/live/document.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type Endpoint, type PayloadRequest } from 'payload';
|
|
2
2
|
import { type PropAccessInfo } from '../core/fieldAccess.js';
|
|
3
3
|
import type { FieldRegistry } from '../core/fieldSemantics.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { PayloadBlocksStorage } from '../core/payloadBlocks.js';
|
|
5
|
+
import type { BlockDefinition, LocaleSettings, RegionConfig } from '../core/types.js';
|
|
5
6
|
import { type BindingCheck } from '../plugin/hook.js';
|
|
6
7
|
import { payloadErrorMessage } from './apply.js';
|
|
7
8
|
import type { LiveRuntime } from './runtime.js';
|
|
@@ -21,6 +22,13 @@ export type BuilderCollectionServer = {
|
|
|
21
22
|
legacyFields?: readonly string[];
|
|
22
23
|
/** The locales of the layout's localized props. Null or left out: not localized. */
|
|
23
24
|
localization?: LocaleSettings | null;
|
|
25
|
+
/** Fixed regions of the page outside the layout (the collection's `regions` option). Left out: none. */
|
|
26
|
+
regions?: RegionConfig[];
|
|
27
|
+
/**
|
|
28
|
+
* The layout field is the collection's own Payload `blocks` field: the block types it holds.
|
|
29
|
+
* Left out: the builder's JSON layout field.
|
|
30
|
+
*/
|
|
31
|
+
payloadBlocks?: PayloadBlocksStorage;
|
|
24
32
|
};
|
|
25
33
|
/** The builder collections and the templates collection. Stored on `config.custom` for the admin view. */
|
|
26
34
|
export type BuilderServerConfig = {
|
package/dist/live/document.js
CHANGED
|
@@ -15,6 +15,7 @@ import { TEMPLATE_DEFAULT_FIELD, TEMPLATE_PREVIEW_FIELD, TEMPLATE_TARGET_FIELD }
|
|
|
15
15
|
import { describeLayoutErrors, summarizeProblems } from '../core/issues.js';
|
|
16
16
|
import { normalizeLayout } from '../core/tree.js';
|
|
17
17
|
import { hasPropAccess } from '../core/fieldAccess.js';
|
|
18
|
+
import { blocksAccessProblem, codecOf } from './storage.js';
|
|
18
19
|
import { checkLayout, RAW_LAYOUT_CONTEXT, STORED_LAYOUT_CONTEXT } from '../plugin/hook.js';
|
|
19
20
|
import { documentPath, draftPreviewPath } from '../plugin/links.js';
|
|
20
21
|
import { payloadErrorMessage, payloadFieldErrors } from './apply.js';
|
|
@@ -56,7 +57,11 @@ export async function loadFieldAccess(req, target, access) {
|
|
|
56
57
|
context: { [RAW_LAYOUT_CONTEXT]: true },
|
|
57
58
|
});
|
|
58
59
|
const { [target.field]: layout, ...rest } = doc;
|
|
59
|
-
open = { sessionId: '', seq: 0, layout:
|
|
60
|
+
open = { sessionId: '', seq: 0, layout: (target.codec ?? codecOf(undefined)).decode(layout), doc: rest };
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// The other fields as stored now (a save may have changed them since the session read them).
|
|
64
|
+
open = { ...open, doc: (await runtime.sessions.currentDoc(target.collection, target.id)) ?? open.doc };
|
|
60
65
|
}
|
|
61
66
|
return propAccessFor(req, { blocks, registry, collection: target.collection, id: target.id, field: target.field, layout: open.layout, doc: open.doc });
|
|
62
67
|
}
|
|
@@ -125,8 +130,8 @@ export async function loadDocMeta(req, args) {
|
|
|
125
130
|
const titleField = useAsTitle && useAsTitle !== 'id' ? useAsTitle : null;
|
|
126
131
|
return {
|
|
127
132
|
collection: target.collection,
|
|
128
|
-
id: target.id,
|
|
129
|
-
title: (titleField ? text(draft[titleField]) : null) ?? target.id,
|
|
133
|
+
id: String(target.id),
|
|
134
|
+
title: (titleField ? text(draft[titleField]) : null) ?? String(target.id),
|
|
130
135
|
titleField,
|
|
131
136
|
drafts: target.drafts,
|
|
132
137
|
status,
|
|
@@ -268,13 +273,32 @@ export async function runPublishAction(req, { target, action, runtime, check, le
|
|
|
268
273
|
const published = await payload.findByID({ ...common, draft: false, context: { [RAW_LAYOUT_CONTEXT]: true } });
|
|
269
274
|
if (published._status !== 'published')
|
|
270
275
|
return { ok: false, status: 409, error: 'This document has no published version.' };
|
|
271
|
-
|
|
276
|
+
// A Payload blocks field: Payload's read hides values from this user, and the blocks field
|
|
277
|
+
// is saved whole. Only a user who may read every value (published and live) and update the
|
|
278
|
+
// field reverts it, with every value.
|
|
279
|
+
if (target.codec?.kind === 'payload-blocks') {
|
|
280
|
+
const full = await payload.findByID({ ...common, draft: false, overrideAccess: true });
|
|
281
|
+
const denied = await blocksAccessProblem(req, target, {
|
|
282
|
+
write: true,
|
|
283
|
+
layouts: [runtime.sessions.peek(target.collection, target.id)?.layout, target.codec.decode(full[target.field])],
|
|
284
|
+
});
|
|
285
|
+
if (denied)
|
|
286
|
+
return { ok: false, status: 403, error: denied };
|
|
287
|
+
published[target.field] = full[target.field];
|
|
288
|
+
}
|
|
272
289
|
const { id: _id, ...data } = published;
|
|
273
|
-
|
|
290
|
+
// The session shows the published layout while it is saved again. When that save fails, the
|
|
291
|
+
// document did not change: the session gets back its layout and its saved or unsaved state.
|
|
292
|
+
doc = await runtime.sessions.resetFor(target.collection, target.id, (target.codec ?? codecOf(undefined)).decode(published[target.field]), published[target.field], () => payload.update({ ...save, data: { ...data, _status: 'published' }, draft: false }));
|
|
274
293
|
}
|
|
275
294
|
else {
|
|
276
|
-
|
|
277
|
-
|
|
295
|
+
// The session's unsaved edits are saved first. When that save fails, nothing is published:
|
|
296
|
+
// the stored draft is not what the editors see.
|
|
297
|
+
const flushed = await runtime.sessions.flush(target.collection, target.id);
|
|
298
|
+
if (!flushed.ok)
|
|
299
|
+
return { ok: false, status: 409, error: flushed.error ?? 'The draft could not be saved, so nothing was published.' };
|
|
300
|
+
// A Payload blocks field: Payload validates its own field when it publishes.
|
|
301
|
+
if (action === 'publish' && check && target.codec?.kind !== 'payload-blocks') {
|
|
278
302
|
const draft = await payload.findByID({ ...common, draft: true, context: { [RAW_LAYOUT_CONTEXT]: true } });
|
|
279
303
|
const layout = runtime.sessions.peek(target.collection, target.id)?.layout ?? normalizeLayout(draft[target.field]);
|
|
280
304
|
const config = req.payload.collections[target.collection]?.config;
|
|
@@ -364,21 +388,41 @@ export async function restoreDocumentVersion(req, { target, versionId, runtime }
|
|
|
364
388
|
}
|
|
365
389
|
if (idOf(version.parent) !== String(target.id))
|
|
366
390
|
return { ok: false, status: 404, error: 'This version belongs to another document.' };
|
|
367
|
-
|
|
368
|
-
|
|
391
|
+
// A Payload blocks field: only a user who may read every value of the version and of the live
|
|
392
|
+
// session, and update the field, restores it (the version's blocks are written whole).
|
|
393
|
+
if (target.codec?.kind === 'payload-blocks') {
|
|
394
|
+
let full;
|
|
395
|
+
try {
|
|
396
|
+
full = (await payload.findVersionByID({ ...common, id: versionId, overrideAccess: true })).version;
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
return { ok: false, status: 404, error: 'This version was not found.' };
|
|
400
|
+
}
|
|
401
|
+
const denied = await blocksAccessProblem(req, target, {
|
|
402
|
+
write: true,
|
|
403
|
+
layouts: [runtime.sessions.peek(target.collection, target.id)?.layout, target.codec.decode(full?.[target.field])],
|
|
404
|
+
});
|
|
405
|
+
if (denied)
|
|
406
|
+
return { ok: false, status: 403, error: denied };
|
|
407
|
+
if (version.version)
|
|
408
|
+
version.version[target.field] = full?.[target.field];
|
|
409
|
+
}
|
|
369
410
|
try {
|
|
370
411
|
// A plugin save: keep the document lock of someone in the settings drawer (fieldsGuard.ts).
|
|
371
412
|
req.context = { ...req.context, [KEEP_LOCK_CONTEXT]: true, [STORED_LAYOUT_CONTEXT]: true };
|
|
413
|
+
// The session shows the version while it is restored. When the restore fails, the document
|
|
414
|
+
// did not change: the session gets back its layout and its unsaved edits, still unsaved.
|
|
415
|
+
const doc = await runtime.sessions.resetFor(target.collection, target.id, (target.codec ?? codecOf(undefined)).decode(version.version?.[target.field]), version.version?.[target.field],
|
|
372
416
|
// The operation, not `payload.restoreVersion`: the Local API drops the `draft` flag (Payload
|
|
373
417
|
// 3.90), and a restored published version would go live at once.
|
|
374
|
-
|
|
418
|
+
() => restoreVersionOperation({
|
|
375
419
|
collection,
|
|
376
420
|
id: versionId,
|
|
377
421
|
depth: 0,
|
|
378
422
|
draft: target.drafts,
|
|
379
423
|
overrideAccess: false,
|
|
380
424
|
req,
|
|
381
|
-
});
|
|
425
|
+
}));
|
|
382
426
|
const event = {
|
|
383
427
|
type: 'published',
|
|
384
428
|
action: 'restore',
|
|
@@ -391,9 +435,6 @@ export async function restoreDocumentVersion(req, { target, versionId, runtime }
|
|
|
391
435
|
return { ok: true, doc };
|
|
392
436
|
}
|
|
393
437
|
catch (error) {
|
|
394
|
-
// The document did not change: give the editors their layout back.
|
|
395
|
-
if (before)
|
|
396
|
-
await runtime.sessions.reset(target.collection, target.id, before);
|
|
397
438
|
const status = statusOf(error);
|
|
398
439
|
return { ok: false, status: status >= 400 && status < 600 ? status : 500, error: payloadErrorMessage(error, target.field) };
|
|
399
440
|
}
|
|
@@ -509,9 +550,8 @@ export function documentEndpoints({ collections, templates, sections, runtime, c
|
|
|
509
550
|
if (target instanceof Response)
|
|
510
551
|
return target;
|
|
511
552
|
const payload = req.payload;
|
|
512
|
-
// The lock row stores the id in the collection's id type.
|
|
513
|
-
const
|
|
514
|
-
const args = { payload, req, collection: target.collection, id, holders };
|
|
553
|
+
// The lock row stores the id in the collection's id type, as `targetOf` parsed it.
|
|
554
|
+
const args = { payload, req, collection: target.collection, id: target.id, holders };
|
|
515
555
|
try {
|
|
516
556
|
if (method === 'delete') {
|
|
517
557
|
await releaseSettingsLock({ ...args, user: { id: user.id } });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type PayloadRequest, type Where } from 'payload';
|
|
2
|
+
import type { Layout, Operation } from '../core/types.js';
|
|
3
|
+
/** Why the builder refuses a user who may not read the document as a draft. */
|
|
4
|
+
export declare const DRAFT_NOT_READABLE_MESSAGE = "You may not read the latest draft of this document, so the builder cannot show it.";
|
|
5
|
+
type Target = {
|
|
6
|
+
collection: string;
|
|
7
|
+
id: string | number;
|
|
8
|
+
field: string;
|
|
9
|
+
drafts: boolean;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The Where with its `_status` conditions decided for a draft (`drafts`): true (always), false
|
|
13
|
+
* (never), or what is left for Payload's query. A condition on `field` (the builder field) or
|
|
14
|
+
* one this cannot read counts as false. Payload's Where has no negation of a nested Where, so a
|
|
15
|
+
* part that counts as false only ever refuses more, never less.
|
|
16
|
+
*/
|
|
17
|
+
export declare function decideForDraft(where: Where, field: string, drafts: boolean): boolean | Where;
|
|
18
|
+
/**
|
|
19
|
+
* Null when the request's user may read the document as the live session holds it (a draft, for
|
|
20
|
+
* collections with drafts), else why the builder refuses. Payload's own access and query decide.
|
|
21
|
+
* Throws when the access or the query fails: callers refuse then.
|
|
22
|
+
*/
|
|
23
|
+
export declare function liveReadProblem(req: PayloadRequest, target: Target): Promise<string | null>;
|
|
24
|
+
type CommitAccess = (args: {
|
|
25
|
+
before: Layout;
|
|
26
|
+
after: Layout;
|
|
27
|
+
ops: readonly Operation[];
|
|
28
|
+
doc: Record<string, unknown>;
|
|
29
|
+
}) => Promise<string | null>;
|
|
30
|
+
/**
|
|
31
|
+
* A commit's access check that first asks `liveReadProblem` again, inside the commit (its lock,
|
|
32
|
+
* on the document as stored when the check ends), because the result goes back to the user. A
|
|
33
|
+
* failing check refuses.
|
|
34
|
+
*/
|
|
35
|
+
export declare function withLiveReadCheck(req: PayloadRequest, target: Target, access?: CommitAccess): CommitAccess;
|
|
36
|
+
export {};
|