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
|
@@ -8,10 +8,13 @@ import { keyAction, post, setPropPath, unwrap, } from '../../protocol/index.js';
|
|
|
8
8
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
|
|
9
9
|
import { flushSync } from 'react-dom';
|
|
10
10
|
import { defaultResolveLink, RenderLayout } from '../index.js';
|
|
11
|
+
import { LayoutOutlet, LayoutOutletContext, LayoutOutletPresence } from '../render/LayoutOutlet.js';
|
|
12
|
+
import { LAYOUT_ROOT_ATTRIBUTE, REGION_ATTRIBUTE } from '../render/regions.js';
|
|
11
13
|
import { createCanvasDrag } from './drag.js';
|
|
12
14
|
import { forgetDoc } from './fetchDocs.js';
|
|
13
15
|
import { editableAt, firstEditable } from './inline/dom.js';
|
|
14
|
-
import {
|
|
16
|
+
import { FrameBoundary, FrameProblem, NO_OUTLET, NO_SERVER, regionData, requestFrame } from './frame.js';
|
|
17
|
+
import { createCanvasMapper, ownerOf } from './inline/mapper.js';
|
|
15
18
|
import { bindingFor, inlineKind, valueAtPath, withPropValue } from './inline/model.js';
|
|
16
19
|
import { startPlainSession } from './inline/session.js';
|
|
17
20
|
import { measure, sameMeasurement } from './measure.js';
|
|
@@ -37,6 +40,10 @@ const EDITOR_CSS = `
|
|
|
37
40
|
[data-builder-repeat] { pointer-events: none; }
|
|
38
41
|
[data-builder-repeat]:not([data-builder-repeat] [data-builder-repeat]) { opacity: 0.6; }
|
|
39
42
|
[data-builder-empty-page] { min-height: 240px; }
|
|
43
|
+
/* The layout inside the page frame: the same box the canvas root gives a layout without a frame. */
|
|
44
|
+
[data-builder-layout] { display: flow-root; }
|
|
45
|
+
/* Fixed regions of the page frame: text and images edit in place, but they never drag. */
|
|
46
|
+
[data-builder-region] { user-select: none; }
|
|
40
47
|
/* Inline text editing. The admin overlay draws the editing outline. */
|
|
41
48
|
[data-builder-editing] { user-select: text; -webkit-user-select: text; cursor: text; outline: none; }
|
|
42
49
|
[data-builder-editing='lines'] { white-space: pre-wrap; }
|
|
@@ -86,6 +93,8 @@ const SCROLL_MARGIN = 48;
|
|
|
86
93
|
function send(message) {
|
|
87
94
|
post(window.parent, message);
|
|
88
95
|
}
|
|
96
|
+
/** Equal values of a text field: empty text, null and a missing value are one value. */
|
|
97
|
+
const sameText = (a, b) => JSON.stringify(a === '' || a === undefined ? null : a) === JSON.stringify(b === '' || b === undefined ? null : b);
|
|
89
98
|
/** The block as the canvas shows it (bindings resolved). */
|
|
90
99
|
const shownBlockIn = (latest, id) => (latest.resolved ? findBlock(latest.resolved, id) : null);
|
|
91
100
|
const definitionIn = (latest, type) => latest.definitions?.find((d) => d.type === type);
|
|
@@ -166,7 +175,9 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
166
175
|
shown: () => latest.current.shown,
|
|
167
176
|
stored: (id) => (latest.current.layout ? findBlock(latest.current.layout, id) : null),
|
|
168
177
|
definition: (type) => definitionIn(latest.current, type),
|
|
169
|
-
skip: (id) => activeRef.current?.id === id,
|
|
178
|
+
skip: (id) => !activeRef.current?.region && activeRef.current?.id === id,
|
|
179
|
+
region: (name) => regionsRef.current.get(name) ?? null,
|
|
180
|
+
skipRegion: (name) => activeRef.current?.region === name,
|
|
170
181
|
}));
|
|
171
182
|
useEffect(() => {
|
|
172
183
|
if (process.env.NODE_ENV === 'production')
|
|
@@ -179,6 +190,20 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
179
190
|
// `undefined` while it loads. The canvas waits for it, so blocks never render without it.
|
|
180
191
|
const [pageData, setPageData] = useState(server ? undefined : null);
|
|
181
192
|
const [serverVersion, setServerVersion] = useState(0);
|
|
193
|
+
// The page frame (collections with `regions`). `frameVersion` asks for it again.
|
|
194
|
+
const [frame, setFrame] = useState({ status: 'off' });
|
|
195
|
+
const [frameVersion, setFrameVersion] = useState(0);
|
|
196
|
+
const [outletCount, setOutletCount] = useState(0);
|
|
197
|
+
const registerOutlet = useCallback(() => {
|
|
198
|
+
setOutletCount((count) => count + 1);
|
|
199
|
+
return () => setOutletCount((count) => count - 1);
|
|
200
|
+
}, []);
|
|
201
|
+
// A frame that arrived while a region was edited inline: it shows when editing ends.
|
|
202
|
+
const pendingFrame = useRef(null);
|
|
203
|
+
// Region text kept on screen after editing, until the frame with the new value replaces it.
|
|
204
|
+
const keptRegions = useRef([]);
|
|
205
|
+
const keptRegionValues = useRef(new Map());
|
|
206
|
+
const regionsRef = useRef(regionData(undefined, null));
|
|
182
207
|
// Coalesce every trigger (render, resize, scroll) into one measurement per frame. An unchanged
|
|
183
208
|
// measurement is not sent: the admin would redraw the overlay for nothing.
|
|
184
209
|
const lastMeasure = useRef(null);
|
|
@@ -209,6 +234,32 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
209
234
|
activeRef.current = null;
|
|
210
235
|
const { session, id, path, inline } = active;
|
|
211
236
|
const connected = inline.element.isConnected;
|
|
237
|
+
if (active.region) {
|
|
238
|
+
// The new value comes back in a new frame (the admin saves the field). Until then the edited
|
|
239
|
+
// text stays on screen, so the old text never flashes.
|
|
240
|
+
const region = active.region;
|
|
241
|
+
const { changed, value, restore } = inline.finish({ keep: sync && connected });
|
|
242
|
+
const edited = !sameText(value, active.start);
|
|
243
|
+
if (edited)
|
|
244
|
+
keptRegionValues.current.set(JSON.stringify([region, path]), value);
|
|
245
|
+
if (changed)
|
|
246
|
+
send({ type: 'inlineChange', session, id: '', path, value, region });
|
|
247
|
+
send({ type: 'inlineEnd', session, id: '', region });
|
|
248
|
+
// Nothing changed: no save follows, so React's DOM comes back now.
|
|
249
|
+
if (restore && !edited)
|
|
250
|
+
restore();
|
|
251
|
+
else if (restore)
|
|
252
|
+
keptRegions.current.push(restore);
|
|
253
|
+
// A frame requested before the edit ended still holds the old text. Keep the edited DOM
|
|
254
|
+
// until the save requests a new frame, including when an older request is still in flight.
|
|
255
|
+
const next = edited ? null : pendingFrame.current;
|
|
256
|
+
if (edited)
|
|
257
|
+
frameRun.current++;
|
|
258
|
+
pendingFrame.current = null;
|
|
259
|
+
if (next)
|
|
260
|
+
showFrameRef.current(next);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
212
263
|
const { changed, value } = inline.finish();
|
|
213
264
|
if (changed)
|
|
214
265
|
send({ type: 'inlineChange', session, id, path, value });
|
|
@@ -226,6 +277,64 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
226
277
|
// Synchronously, so the restored DOM never paints with the old text.
|
|
227
278
|
flushSync(() => setFreeze(settled || !final ? null : { id, key: final.key, value: final.value, release: current }));
|
|
228
279
|
}, []);
|
|
280
|
+
/** Shows a new frame. Region text kept from editing goes back to React's DOM first, in the same task. */
|
|
281
|
+
const showFrameRef = useRef((next) => {
|
|
282
|
+
// Re-entering a field captures its previous retained DOM. Restore those snapshots in reverse.
|
|
283
|
+
for (const undo of keptRegions.current.splice(0).toReversed())
|
|
284
|
+
undo();
|
|
285
|
+
keptRegionValues.current.clear();
|
|
286
|
+
flushSync(() => setFrame(next));
|
|
287
|
+
});
|
|
288
|
+
/**
|
|
289
|
+
* Starts inline editing of a region's text field. The value comes from the frame's values; the
|
|
290
|
+
* admin saves it to the document when editing ends. No list keys and no bindings in regions.
|
|
291
|
+
*/
|
|
292
|
+
const startRegionInline = useCallback(async (target, point, offset) => {
|
|
293
|
+
const region = target.region;
|
|
294
|
+
const data = region ? regionsRef.current.get(region) : undefined;
|
|
295
|
+
if (!region || !data)
|
|
296
|
+
return;
|
|
297
|
+
const { path, element } = target;
|
|
298
|
+
const key = JSON.stringify([region, path]);
|
|
299
|
+
const retained = keptRegionValues.current.has(key);
|
|
300
|
+
const value = retained ? keptRegionValues.current.get(key) : valueAtPath(data.block.props, path);
|
|
301
|
+
const kind = inlineKind(data.definition, path, value);
|
|
302
|
+
if (!kind)
|
|
303
|
+
return;
|
|
304
|
+
startingRef.current = true;
|
|
305
|
+
try {
|
|
306
|
+
const session = createId();
|
|
307
|
+
const options = {
|
|
308
|
+
point,
|
|
309
|
+
offset,
|
|
310
|
+
onChange: (next) => send({ type: 'inlineChange', session, id: '', path, value: next, region }),
|
|
311
|
+
onExit: () => stopInline(),
|
|
312
|
+
onFormat: (format) => send({ type: 'inlineFormat', session, format }),
|
|
313
|
+
onLinkRequest: () => send({ type: 'inlineLink', session }),
|
|
314
|
+
};
|
|
315
|
+
let inline = null;
|
|
316
|
+
if (kind === 'rich') {
|
|
317
|
+
const { startRichSession } = await import('./inline/rich/index.js');
|
|
318
|
+
if (element.isConnected)
|
|
319
|
+
inline = startRichSession(element, value, options);
|
|
320
|
+
if (!inline && element.isConnected)
|
|
321
|
+
send({ type: 'inlineRefused', id: '', path, reason: 'unsupported', region });
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
inline = startPlainSession(element, kind, options);
|
|
325
|
+
}
|
|
326
|
+
if (!inline)
|
|
327
|
+
return;
|
|
328
|
+
activeRef.current = { session, id: '', path, inline, region, start: value };
|
|
329
|
+
// A retained value is the exact prior edit. Other rich text uses the admin's unpopulated value.
|
|
330
|
+
send({ type: 'inlineStart', session, id: '', path, kind, region, ...(kind === 'rich' && !retained ? {} : { base: value ?? null }) });
|
|
331
|
+
}
|
|
332
|
+
finally {
|
|
333
|
+
startingRef.current = false;
|
|
334
|
+
}
|
|
335
|
+
// `stopInline` is stable (no dependencies).
|
|
336
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
337
|
+
}, []);
|
|
229
338
|
/**
|
|
230
339
|
* Starts inline editing of one text prop. `offset` (characters) or `point` (a double-click)
|
|
231
340
|
* places the caret, else it goes to the end.
|
|
@@ -233,6 +342,10 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
233
342
|
const startInline = useCallback(async (target, point, offset) => {
|
|
234
343
|
if (activeRef.current || startingRef.current || !target.path)
|
|
235
344
|
return;
|
|
345
|
+
if (target.region) {
|
|
346
|
+
await startRegionInline(target, point, offset);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
236
349
|
const { blockId: id, path, element } = target;
|
|
237
350
|
const stored = latest.current.layout ? findBlock(latest.current.layout, id) : null;
|
|
238
351
|
const shown = shownBlockIn(latest.current, id);
|
|
@@ -395,6 +508,19 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
395
508
|
setLocale(message.locale);
|
|
396
509
|
return;
|
|
397
510
|
case 'inlineStart': {
|
|
511
|
+
if (message.region) {
|
|
512
|
+
for (const el of document.querySelectorAll(`[${REGION_ATTRIBUTE}="${CSS.escape(message.region)}"]`)) {
|
|
513
|
+
if (el.getClientRects().length === 0)
|
|
514
|
+
continue;
|
|
515
|
+
mapper.ensure(el);
|
|
516
|
+
const target = firstEditable(el);
|
|
517
|
+
if (!target)
|
|
518
|
+
continue;
|
|
519
|
+
void startInline(target, null, message.offset);
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
398
524
|
// The screen must show the admin's newest layout first (a joined list item has new text).
|
|
399
525
|
const current = shownSeq.current === layoutSeq.current;
|
|
400
526
|
const el = current ? document.querySelector(`[data-block-id="${CSS.escape(message.id)}"]`) : null;
|
|
@@ -417,6 +543,11 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
417
543
|
case 'docChanged':
|
|
418
544
|
forgetDoc(message.collection, message.id);
|
|
419
545
|
setDocsVersion((version) => version + 1);
|
|
546
|
+
// A media document of a region (its alt text) or the page itself changed.
|
|
547
|
+
setFrameVersion((version) => version + 1);
|
|
548
|
+
return;
|
|
549
|
+
case 'frameRefresh':
|
|
550
|
+
setFrameVersion((version) => version + 1);
|
|
420
551
|
return;
|
|
421
552
|
case 'dragStart':
|
|
422
553
|
drag.start(message.drag);
|
|
@@ -461,9 +592,9 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
461
592
|
const onDoubleClick = (e) => {
|
|
462
593
|
if (isEditing(e.target) || !(e.target instanceof Element))
|
|
463
594
|
return;
|
|
464
|
-
const
|
|
465
|
-
if (
|
|
466
|
-
mapper.ensure(
|
|
595
|
+
const ownerEl = ownerOf(e.target)?.element;
|
|
596
|
+
if (ownerEl)
|
|
597
|
+
mapper.ensure(ownerEl);
|
|
467
598
|
const target = editableAt(e.target);
|
|
468
599
|
if (target) {
|
|
469
600
|
e.preventDefault();
|
|
@@ -685,6 +816,41 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
685
816
|
cancelled = true;
|
|
686
817
|
};
|
|
687
818
|
}, [serverBlocks, init, context, locale]);
|
|
819
|
+
// The page frame: asked for when the document has regions, again for another scope (locale) and
|
|
820
|
+
// after a region's field changed (`frameRefresh`). Without regions nothing is asked for.
|
|
821
|
+
const wantsFrame = Boolean(init?.regions?.length);
|
|
822
|
+
const frameScope = useMemo(() => (init ? canvasScope(init, context, locale) : null), [init, context, locale]);
|
|
823
|
+
const frameRun = useRef(0);
|
|
824
|
+
useEffect(() => {
|
|
825
|
+
if (!wantsFrame || !frameScope) {
|
|
826
|
+
setFrame({ status: 'off' });
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
if (!server) {
|
|
830
|
+
setFrame({ status: 'error', error: NO_SERVER });
|
|
831
|
+
send({ type: 'error', message: NO_SERVER });
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
const run = ++frameRun.current;
|
|
835
|
+
setFrame((current) => (current.status === 'ready' || current.status === 'error' ? current : { status: 'loading' }));
|
|
836
|
+
void requestFrame(server, frameScope).then((next) => {
|
|
837
|
+
if (run !== frameRun.current)
|
|
838
|
+
return;
|
|
839
|
+
if (next.status === 'error')
|
|
840
|
+
send({ type: 'error', message: next.error });
|
|
841
|
+
// A region edited inline keeps its DOM until editing ends.
|
|
842
|
+
if (activeRef.current?.region)
|
|
843
|
+
pendingFrame.current = next;
|
|
844
|
+
else
|
|
845
|
+
showFrameRef.current(next);
|
|
846
|
+
});
|
|
847
|
+
}, [wantsFrame, frameScope, server, frameVersion]);
|
|
848
|
+
const frameValues = frame.status === 'ready' ? frame.values : null;
|
|
849
|
+
const regions = useMemo(() => regionData(init?.regions, frameValues), [init?.regions, frameValues]);
|
|
850
|
+
useLayoutEffect(() => {
|
|
851
|
+
regionsRef.current = regions;
|
|
852
|
+
mapper.schedule();
|
|
853
|
+
}, [regions, mapper]);
|
|
688
854
|
// The stored layout server blocks are sent from. A prop being edited inline keeps its value from
|
|
689
855
|
// the start of editing (as on screen), so the block around it asks again only once editing ends.
|
|
690
856
|
const editing = freeze && freeze.release === null ? freeze : null;
|
|
@@ -740,7 +906,9 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
740
906
|
const timer = window.setTimeout(() => setFreeze((current) => (current === freeze ? null : current)), RELEASE_MS);
|
|
741
907
|
return () => window.clearTimeout(timer);
|
|
742
908
|
}, [freeze]);
|
|
743
|
-
|
|
909
|
+
// With regions the first frame is awaited too (also in the render before its request starts), so nothing moves when it arrives.
|
|
910
|
+
const frameLoading = Boolean(init?.regions?.length) && (frame.status === 'loading' || frame.status === 'off');
|
|
911
|
+
const visible = shown && compiler.status !== 'loading' && pageData !== undefined && !frameLoading;
|
|
744
912
|
// Observes the current block and slot elements and measures.
|
|
745
913
|
const observeBlocks = useCallback(() => {
|
|
746
914
|
const ro = observer.current;
|
|
@@ -755,7 +923,14 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
755
923
|
}, [scheduleMeasure]);
|
|
756
924
|
// After the rendered blocks change, and after a server block shows new content.
|
|
757
925
|
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
758
|
-
useLayoutEffect(observeBlocks, [visible, shown, css, observeBlocks]);
|
|
926
|
+
useLayoutEffect(observeBlocks, [visible, shown, css, observeBlocks, frame]);
|
|
927
|
+
// A frame that never renders `layout`: the canvas shows the layout under it, and says so.
|
|
928
|
+
const outletMissing = Boolean(visible && frame.status === 'ready' && frame.node && outletCount === 0);
|
|
929
|
+
useEffect(() => {
|
|
930
|
+
if (outletMissing && !rootRef.current?.querySelector(`[${LAYOUT_ROOT_ATTRIBUTE}]:not([data-builder-outlet-fallback])`)) {
|
|
931
|
+
send({ type: 'error', message: NO_OUTLET });
|
|
932
|
+
}
|
|
933
|
+
}, [outletMissing]);
|
|
759
934
|
useEffect(() => {
|
|
760
935
|
if (!serverBlocks)
|
|
761
936
|
return;
|
|
@@ -768,5 +943,20 @@ function EditorCanvas({ blocks, components, plugins, resolveLink, server }) {
|
|
|
768
943
|
useLayoutEffect(() => drag.rendered(resolvedFrom.current), [shown, drag]);
|
|
769
944
|
// A render for another reason (a new layout still loading its data) reuses the element.
|
|
770
945
|
const rendered = useMemo(() => visible ? (_jsx(RenderLayout, { layout: shown, blocks: definitions, components: canvasComponents, css: css, mode: "canvas", resolveLink: resolveLink, context: context, pageData: pageData })) : null, [visible, shown, definitions, canvasComponents, css, resolveLink, context, pageData]);
|
|
771
|
-
|
|
946
|
+
const layoutContent = (_jsxs(_Fragment, { children: [visible && shown.blocks.length === 0 && _jsx("div", { "data-builder-empty-page": "" }), _jsx(ServerBlocksContext.Provider, { value: serverContext, children: rendered })] }));
|
|
947
|
+
let page = layoutContent;
|
|
948
|
+
if (frame.status !== 'off' && visible) {
|
|
949
|
+
// The layout's own box, as the canvas root gave it before: the frame's outlet renders it.
|
|
950
|
+
const outlet = _jsx("div", { [LAYOUT_ROOT_ATTRIBUTE]: '', children: layoutContent });
|
|
951
|
+
if (frame.status === 'error') {
|
|
952
|
+
page = (_jsxs(_Fragment, { children: [_jsx(FrameProblem, { text: frame.error }), outlet] }));
|
|
953
|
+
}
|
|
954
|
+
else if (frame.status === 'ready' && frame.node) {
|
|
955
|
+
page = (_jsxs(LayoutOutletContext.Provider, { value: outlet, children: [_jsx(LayoutOutletPresence.Provider, { value: registerOutlet, children: _jsx(FrameBoundary, { resetKey: frame.node, renderError: (error) => (_jsxs(_Fragment, { children: [_jsx(FrameProblem, { text: error }), _jsx(LayoutOutlet, {})] })), children: frame.node }) }), outletMissing && (_jsxs(_Fragment, { children: [_jsx(FrameProblem, { text: NO_OUTLET }), _jsx("div", { [LAYOUT_ROOT_ATTRIBUTE]: '', "data-builder-outlet-fallback": "", children: layoutContent })] }))] }));
|
|
956
|
+
}
|
|
957
|
+
else {
|
|
958
|
+
page = outlet;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return (_jsxs(_Fragment, { children: [_jsx("style", { "data-builder-editor-css": "", children: EDITOR_CSS }), _jsx("div", { ref: rootRef, "data-builder-root": "", children: page })] }));
|
|
772
962
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type Block, type BlockDefinition, type RegionConfig } from '../../core/index.js';
|
|
2
|
+
import { Component, type ReactNode } from 'react';
|
|
3
|
+
import type { CanvasScope, CanvasServer } from '../render/canvasServerTypes.js';
|
|
4
|
+
/** What the canvas shows around the layout. */
|
|
5
|
+
export type FrameView =
|
|
6
|
+
/** No regions: the layout alone, as before. */
|
|
7
|
+
{
|
|
8
|
+
status: 'off';
|
|
9
|
+
}
|
|
10
|
+
/** The first frame is on its way. The canvas waits for it, so nothing moves when it arrives. */
|
|
11
|
+
| {
|
|
12
|
+
status: 'loading';
|
|
13
|
+
}
|
|
14
|
+
/** `node` null: the frame shows the layout alone. `values`: the regions' values as the frame got them. */
|
|
15
|
+
| {
|
|
16
|
+
status: 'ready';
|
|
17
|
+
node: ReactNode | null;
|
|
18
|
+
values: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
/** The frame could not be shown. The canvas shows the problem above the layout. */
|
|
21
|
+
| {
|
|
22
|
+
status: 'error';
|
|
23
|
+
error: string;
|
|
24
|
+
};
|
|
25
|
+
/** The text shown when a collection has regions and the canvas page has no server action. */
|
|
26
|
+
export declare const NO_SERVER: string;
|
|
27
|
+
/** The text shown when the server action has no `frame` option. */
|
|
28
|
+
export declare const NO_FRAME: string;
|
|
29
|
+
/** The text shown when the frame never renders the layout outlet. */
|
|
30
|
+
export declare const NO_OUTLET = "The page frame does not render `layout`, so the canvas shows the layout under it. Render `layout` where the page shows the layout.";
|
|
31
|
+
/** Asks the server action for the frame of `scope`. Never throws. */
|
|
32
|
+
export declare function requestFrame(server: CanvasServer, scope: CanvasScope): Promise<FrameView>;
|
|
33
|
+
/** One region as the inline editor and the mapping read it: its values as block props, its fields as a definition. */
|
|
34
|
+
export type RegionData = {
|
|
35
|
+
block: Block;
|
|
36
|
+
definition: BlockDefinition;
|
|
37
|
+
};
|
|
38
|
+
/** The regions' data for the mapping and inline editing. New objects only when the values change. */
|
|
39
|
+
export declare function regionData(regions: readonly RegionConfig[] | undefined, values: Record<string, unknown> | null): Map<string, RegionData>;
|
|
40
|
+
/** A visible problem with the page frame, above the layout. Never part of the site. */
|
|
41
|
+
export declare function FrameProblem({ text }: {
|
|
42
|
+
text: string;
|
|
43
|
+
}): import("react").JSX.Element;
|
|
44
|
+
type BoundaryProps = {
|
|
45
|
+
resetKey: unknown;
|
|
46
|
+
renderError: (error: string) => ReactNode;
|
|
47
|
+
children: ReactNode;
|
|
48
|
+
};
|
|
49
|
+
/** Catches errors the frame's output throws while it renders (a server component that failed). */
|
|
50
|
+
export declare class FrameBoundary extends Component<BoundaryProps, {
|
|
51
|
+
error: string | null;
|
|
52
|
+
resetKey: unknown;
|
|
53
|
+
}> {
|
|
54
|
+
state: {
|
|
55
|
+
error: string | null;
|
|
56
|
+
resetKey: unknown;
|
|
57
|
+
};
|
|
58
|
+
static getDerivedStateFromError(error: unknown): {
|
|
59
|
+
error: string;
|
|
60
|
+
};
|
|
61
|
+
static getDerivedStateFromProps(props: BoundaryProps, state: {
|
|
62
|
+
error: string | null;
|
|
63
|
+
resetKey: unknown;
|
|
64
|
+
}): {
|
|
65
|
+
error: null;
|
|
66
|
+
resetKey: unknown;
|
|
67
|
+
} | null;
|
|
68
|
+
render(): ReactNode;
|
|
69
|
+
}
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
// The page frame in the canvas: the app's page around the layout, rendered by the canvas server
|
|
4
|
+
// action (`createCanvasServer({ frame })`) for collections with `regions`. The layout stays a live
|
|
5
|
+
// client render inside the frame's outlet; the regions show the document's own fields.
|
|
6
|
+
import { regionDefinition } from '../../core/index.js';
|
|
7
|
+
import { Component } from 'react';
|
|
8
|
+
/** The text shown when a collection has regions and the canvas page has no server action. */
|
|
9
|
+
export const NO_SERVER = 'This page has fixed regions (the collection’s `regions` option), but the canvas page gives `BuilderCanvas` no `server`. ' +
|
|
10
|
+
'Pass the canvas server action, made with `createCanvasServer({ frame })`.';
|
|
11
|
+
/** The text shown when the server action has no `frame` option. */
|
|
12
|
+
export const NO_FRAME = 'This page has fixed regions (the collection’s `regions` option), but `createCanvasServer` has no `frame`. ' +
|
|
13
|
+
'Pass the page frame, the component that renders the page around the layout.';
|
|
14
|
+
/** The text shown when the frame never renders the layout outlet. */
|
|
15
|
+
export const NO_OUTLET = 'The page frame does not render `layout`, so the canvas shows the layout under it. Render `layout` where the page shows the layout.';
|
|
16
|
+
const message = (error) => (error instanceof Error ? error.message : String(error));
|
|
17
|
+
/** Asks the server action for the frame of `scope`. Never throws. */
|
|
18
|
+
export async function requestFrame(server, scope) {
|
|
19
|
+
try {
|
|
20
|
+
const response = await server({ kind: 'frame', scope });
|
|
21
|
+
if (response.kind === 'frame')
|
|
22
|
+
return response.missing ? { status: 'error', error: NO_FRAME } : { status: 'ready', node: response.node, values: response.values };
|
|
23
|
+
if (response.kind === 'error')
|
|
24
|
+
return { status: 'error', error: response.error };
|
|
25
|
+
return { status: 'error', error: 'Page frame: unexpected answer from the canvas server action.' };
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return { status: 'error', error: `Page frame: ${message(error)}` };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** The regions' data for the mapping and inline editing. New objects only when the values change. */
|
|
32
|
+
export function regionData(regions, values) {
|
|
33
|
+
const out = new Map();
|
|
34
|
+
for (const region of regions ?? []) {
|
|
35
|
+
const definition = regionDefinition(region);
|
|
36
|
+
// Regions have no block id: the mapping and inline editing key them by name.
|
|
37
|
+
out.set(region.name, { block: { id: '', type: definition.type, props: values ?? {} }, definition });
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
const PROBLEM_STYLE = {
|
|
42
|
+
margin: 8,
|
|
43
|
+
padding: '8px 12px',
|
|
44
|
+
border: '1px solid rgb(180 30 30 / 0.5)',
|
|
45
|
+
borderRadius: 4,
|
|
46
|
+
color: 'rgb(180 30 30)',
|
|
47
|
+
background: 'rgb(255 240 240)',
|
|
48
|
+
font: '13px/1.4 system-ui, sans-serif',
|
|
49
|
+
};
|
|
50
|
+
/** A visible problem with the page frame, above the layout. Never part of the site. */
|
|
51
|
+
export function FrameProblem({ text }) {
|
|
52
|
+
return (_jsx("div", { "data-builder-frame-error": "", role: "alert", style: PROBLEM_STYLE, children: text }));
|
|
53
|
+
}
|
|
54
|
+
/** Catches errors the frame's output throws while it renders (a server component that failed). */
|
|
55
|
+
export class FrameBoundary extends Component {
|
|
56
|
+
state = { error: null, resetKey: this.props.resetKey };
|
|
57
|
+
static getDerivedStateFromError(error) {
|
|
58
|
+
return { error: `Page frame: ${message(error)}` };
|
|
59
|
+
}
|
|
60
|
+
static getDerivedStateFromProps(props, state) {
|
|
61
|
+
return props.resetKey === state.resetKey ? null : { error: null, resetKey: props.resetKey };
|
|
62
|
+
}
|
|
63
|
+
render() {
|
|
64
|
+
return this.state.error === null ? this.props.children : this.props.renderError(this.state.error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -10,6 +10,8 @@ export type DomSnapshot = {
|
|
|
10
10
|
};
|
|
11
11
|
export declare function snapshotDom(el: HTMLElement): DomSnapshot;
|
|
12
12
|
export declare function restoreDom(el: HTMLElement, snapshot: DomSnapshot): void;
|
|
13
|
+
/** Puts back only the element's own attributes (the editing marks go); the edited text stays. */
|
|
14
|
+
export declare function restoreAttributes(el: HTMLElement, snapshot: DomSnapshot): void;
|
|
13
15
|
/**
|
|
14
16
|
* Puts the caret `offset` characters into the text of `el`, else at a viewport point inside `el`,
|
|
15
17
|
* else at the end of `el`.
|
|
@@ -26,11 +28,15 @@ export type Throttle = {
|
|
|
26
28
|
cancel: () => void;
|
|
27
29
|
};
|
|
28
30
|
export declare function createThrottle(fn: () => void, ms: number): Throttle;
|
|
29
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* An element that shows a text prop of a block, or a field of a region. For a region `blockId`
|
|
33
|
+
* is empty, `region` names it and `path` is a document path.
|
|
34
|
+
*/
|
|
30
35
|
export type EditableTarget = {
|
|
31
36
|
element: HTMLElement;
|
|
32
37
|
blockId: string;
|
|
33
38
|
path: string;
|
|
39
|
+
region?: string;
|
|
34
40
|
};
|
|
35
41
|
/**
|
|
36
42
|
* Finds the text prop under `target` (a double-click): the nearest element with an `editableText`
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// DOM helpers for inline editing: keep and restore what React rendered, place the caret,
|
|
2
2
|
// find the element that shows a text prop, and a small throttle.
|
|
3
3
|
import { EDITABLE_TEXT_ATTRIBUTE } from '../../render/editable.js';
|
|
4
|
-
import { AUTO_TEXT_ATTRIBUTE, TEXT_MARK_SELECTOR } from './mapper.js';
|
|
4
|
+
import { AUTO_TEXT_ATTRIBUTE, ownerOf, TEXT_MARK_SELECTOR } from './mapper.js';
|
|
5
5
|
export function snapshotDom(el) {
|
|
6
6
|
const children = [];
|
|
7
7
|
const texts = [];
|
|
@@ -23,6 +23,10 @@ export function restoreDom(el, snapshot) {
|
|
|
23
23
|
for (const [node, data] of snapshot.texts)
|
|
24
24
|
if (node.data !== data)
|
|
25
25
|
node.data = data;
|
|
26
|
+
restoreAttributes(el, snapshot);
|
|
27
|
+
}
|
|
28
|
+
/** Puts back only the element's own attributes (the editing marks go); the edited text stays. */
|
|
29
|
+
export function restoreAttributes(el, snapshot) {
|
|
26
30
|
const keep = new Map(snapshot.attributes);
|
|
27
31
|
for (const name of el.getAttributeNames())
|
|
28
32
|
if (!keep.has(name))
|
|
@@ -103,9 +107,17 @@ export function createThrottle(fn, ms) {
|
|
|
103
107
|
cancel,
|
|
104
108
|
};
|
|
105
109
|
}
|
|
106
|
-
/** The block element `el` belongs to (its own
|
|
110
|
+
/** The block or region element `el` belongs to (its own or the nearest one above it). */
|
|
107
111
|
function ownerBlock(el) {
|
|
108
|
-
return el
|
|
112
|
+
return ownerOf(el)?.element ?? null;
|
|
113
|
+
}
|
|
114
|
+
function targetOf(owner, element) {
|
|
115
|
+
const found = ownerOf(owner);
|
|
116
|
+
if (!found)
|
|
117
|
+
return null;
|
|
118
|
+
return found.kind === 'region'
|
|
119
|
+
? { element, blockId: '', path: markPath(element), region: found.name }
|
|
120
|
+
: { element, blockId: found.id, path: markPath(element) };
|
|
109
121
|
}
|
|
110
122
|
const markPath = (el) => el.getAttribute(EDITABLE_TEXT_ATTRIBUTE) ?? el.getAttribute(AUTO_TEXT_ATTRIBUTE) ?? '';
|
|
111
123
|
/**
|
|
@@ -115,24 +127,22 @@ const markPath = (el) => el.getAttribute(EDITABLE_TEXT_ATTRIBUTE) ?? el.getAttri
|
|
|
115
127
|
*/
|
|
116
128
|
export function editableAt(target) {
|
|
117
129
|
const blockEl = ownerBlock(target);
|
|
118
|
-
|
|
119
|
-
if (!blockEl || !blockId)
|
|
130
|
+
if (!blockEl)
|
|
120
131
|
return null;
|
|
121
132
|
const marked = target.closest(TEXT_MARK_SELECTOR);
|
|
122
133
|
if (!marked || ownerBlock(marked) !== blockEl)
|
|
123
134
|
return null;
|
|
124
|
-
return
|
|
135
|
+
return targetOf(blockEl, marked);
|
|
125
136
|
}
|
|
126
137
|
/**
|
|
127
138
|
* The first text prop element of a block (Enter on a selected block), or null. Explicit marks
|
|
128
139
|
* come first, then the marks of the automatic mapping, in document order.
|
|
129
140
|
*/
|
|
130
141
|
export function firstEditable(blockEl) {
|
|
131
|
-
|
|
132
|
-
if (!blockId)
|
|
142
|
+
if (!ownerOf(blockEl) || ownerOf(blockEl)?.element !== blockEl)
|
|
133
143
|
return null;
|
|
134
144
|
const usable = (el) => ownerBlock(el) === blockEl && el.getClientRects().length > 0;
|
|
135
145
|
const all = [blockEl, ...blockEl.querySelectorAll(TEXT_MARK_SELECTOR)].filter((el) => el.matches(TEXT_MARK_SELECTOR));
|
|
136
146
|
const found = all.find((el) => el.hasAttribute(EDITABLE_TEXT_ATTRIBUTE) && usable(el)) ?? all.find(usable);
|
|
137
|
-
return found ?
|
|
147
|
+
return found ? targetOf(blockEl, found) : null;
|
|
138
148
|
}
|
|
@@ -4,6 +4,22 @@ import type { CanvasImageTarget } from '../../../protocol/index.js';
|
|
|
4
4
|
export declare const AUTO_TEXT_ATTRIBUTE = "data-builder-text-auto";
|
|
5
5
|
/** Explicit or automatic text marks. */
|
|
6
6
|
export declare const TEXT_MARK_SELECTOR = "[data-builder-text], [data-builder-text-auto]";
|
|
7
|
+
/** Elements that own props: blocks and the page frame's regions. */
|
|
8
|
+
export declare const OWNER_SELECTOR = "[data-block-id], [data-builder-region]";
|
|
9
|
+
/** A block or a region, and its key in the mapping (`b:<id>` or `r:<name>`, so the two never collide). */
|
|
10
|
+
export type MapOwner = {
|
|
11
|
+
kind: 'block';
|
|
12
|
+
id: string;
|
|
13
|
+
key: string;
|
|
14
|
+
element: HTMLElement;
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'region';
|
|
17
|
+
name: string;
|
|
18
|
+
key: string;
|
|
19
|
+
element: HTMLElement;
|
|
20
|
+
};
|
|
21
|
+
/** The block or region element `el` belongs to: its own or the nearest one above it. */
|
|
22
|
+
export declare function ownerOf(el: Element | null | undefined): MapOwner | null;
|
|
7
23
|
export type MapperStats = {
|
|
8
24
|
/** Mapping passes that mapped at least one block. */
|
|
9
25
|
passes: number;
|
|
@@ -45,5 +61,15 @@ export type MapperOptions = {
|
|
|
45
61
|
definition: (type: string) => BlockDefinition | undefined;
|
|
46
62
|
/** True for the block being edited inline: its DOM is the user's, not React's. */
|
|
47
63
|
skip: (id: string) => boolean;
|
|
64
|
+
/**
|
|
65
|
+
* The page frame's regions: their values as the frame got them (documents loaded) as a block,
|
|
66
|
+
* and their fields as a definition. A new block object maps the region again.
|
|
67
|
+
*/
|
|
68
|
+
region?: (name: string) => {
|
|
69
|
+
block: Block;
|
|
70
|
+
definition: BlockDefinition;
|
|
71
|
+
} | null;
|
|
72
|
+
/** True for the region being edited inline. */
|
|
73
|
+
skipRegion?: (name: string) => boolean;
|
|
48
74
|
};
|
|
49
75
|
export declare function createCanvasMapper(options: MapperOptions): CanvasMapper;
|