@conloca/cms-spa 0.2.1 → 0.2.3
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 +26 -0
- package/dist/spa/main.css +240 -4
- package/dist/ts/.build-inputs.json +178 -0
- package/dist/ts/App.d.ts +10 -2
- package/dist/ts/App.d.ts.map +1 -1
- package/dist/ts/App.js +20 -8
- package/dist/ts/App.js.map +1 -1
- package/dist/ts/CmsSpaApp.d.ts.map +1 -1
- package/dist/ts/CmsSpaApp.js +4 -1
- package/dist/ts/CmsSpaApp.js.map +1 -1
- package/dist/ts/components/CMSDashboard.d.ts.map +1 -1
- package/dist/ts/components/CMSDashboard.js +6 -2
- package/dist/ts/components/CMSDashboard.js.map +1 -1
- package/dist/ts/components/RouteErrorPage.d.ts +11 -0
- package/dist/ts/components/RouteErrorPage.d.ts.map +1 -0
- package/dist/ts/components/RouteErrorPage.js +44 -0
- package/dist/ts/components/RouteErrorPage.js.map +1 -0
- package/dist/ts/components/dashboard/RecentActivity.d.ts.map +1 -1
- package/dist/ts/components/dashboard/RecentActivity.js +2 -8
- package/dist/ts/components/dashboard/RecentActivity.js.map +1 -1
- package/dist/ts/components/dialogs/CreateDataDialog.js +2 -2
- package/dist/ts/components/dialogs/CreateDataDialog.js.map +1 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.js +2 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.js.map +1 -1
- package/dist/ts/components/dialogs/CreatePageFlow.d.ts +13 -0
- package/dist/ts/components/dialogs/CreatePageFlow.d.ts.map +1 -0
- package/dist/ts/components/dialogs/CreatePageFlow.js +100 -0
- package/dist/ts/components/dialogs/CreatePageFlow.js.map +1 -0
- package/dist/ts/components/dialogs/DataPropertiesDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/DataPropertiesDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/DataPropertiesDialog.js +2 -2
- package/dist/ts/components/dialogs/DataPropertiesDialog.js.map +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.js +2 -2
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.js.map +1 -1
- package/dist/ts/components/dialogs/PageMetadataDialog.d.ts +2 -12
- package/dist/ts/components/dialogs/PageMetadataDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/PageMetadataDialog.js +3 -21
- package/dist/ts/components/dialogs/PageMetadataDialog.js.map +1 -1
- package/dist/ts/components/editor/CMSMDXEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/CMSMDXEditor.js +46 -55
- package/dist/ts/components/editor/CMSMDXEditor.js.map +1 -1
- package/dist/ts/components/editor/DrawerItemOverride.d.ts +2 -0
- package/dist/ts/components/editor/DrawerItemOverride.d.ts.map +1 -1
- package/dist/ts/components/editor/DrawerItemOverride.js +4 -3
- package/dist/ts/components/editor/DrawerItemOverride.js.map +1 -1
- package/dist/ts/components/editor/PageEditor.d.ts +6 -7
- package/dist/ts/components/editor/PageEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditor.js +124 -34
- package/dist/ts/components/editor/PageEditor.js.map +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.js +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.js.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.js +48 -12
- package/dist/ts/components/editor/PageEditorWrapper.js.map +1 -1
- package/dist/ts/components/editor/draft-media-config.d.ts +22 -0
- package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -0
- package/dist/ts/components/editor/draft-media-config.js +186 -0
- package/dist/ts/components/editor/draft-media-config.js.map +1 -0
- package/dist/ts/components/editor/preview-availability.d.ts +6 -5
- package/dist/ts/components/editor/preview-availability.d.ts.map +1 -1
- package/dist/ts/components/editor/preview-availability.js +6 -5
- package/dist/ts/components/editor/preview-availability.js.map +1 -1
- package/dist/ts/components/editors/DataEditor.d.ts +2 -2
- package/dist/ts/components/editors/DataEditor.d.ts.map +1 -1
- package/dist/ts/components/forms/HintForm.js +8 -5
- package/dist/ts/components/forms/HintForm.js.map +1 -1
- package/dist/ts/components/forms/SchemaForm.d.ts +17 -5
- package/dist/ts/components/forms/SchemaForm.d.ts.map +1 -1
- package/dist/ts/components/forms/SchemaForm.js +16 -9
- package/dist/ts/components/forms/SchemaForm.js.map +1 -1
- package/dist/ts/components/media/AssetCard.d.ts.map +1 -1
- package/dist/ts/components/media/AssetCard.js +3 -2
- package/dist/ts/components/media/AssetCard.js.map +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.js +4 -2
- package/dist/ts/components/media/AssetDetailSidebar.js.map +1 -1
- package/dist/ts/components/media/FolderTreeSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/FolderTreeSidebar.js +4 -1
- package/dist/ts/components/media/FolderTreeSidebar.js.map +1 -1
- package/dist/ts/components/pages/PageList.d.ts.map +1 -1
- package/dist/ts/components/pages/PageList.js +13 -99
- package/dist/ts/components/pages/PageList.js.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.d.ts.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.js +36 -5
- package/dist/ts/components/sandbox/ContentSandbox.js.map +1 -1
- package/dist/ts/content-form-fields.d.ts +41 -0
- package/dist/ts/content-form-fields.d.ts.map +1 -0
- package/dist/ts/content-form-fields.js +70 -0
- package/dist/ts/content-form-fields.js.map +1 -0
- package/dist/ts/content-wrapper.d.ts +65 -0
- package/dist/ts/content-wrapper.d.ts.map +1 -0
- package/dist/ts/content-wrapper.js +190 -0
- package/dist/ts/content-wrapper.js.map +1 -0
- package/dist/ts/data-schemas.d.ts +4 -3
- package/dist/ts/data-schemas.d.ts.map +1 -1
- package/dist/ts/data-schemas.js.map +1 -1
- package/dist/ts/frame/channel.d.ts +40 -0
- package/dist/ts/frame/channel.d.ts.map +1 -0
- package/dist/ts/frame/channel.js +233 -0
- package/dist/ts/frame/channel.js.map +1 -0
- package/dist/ts/frame/editor-frame.d.ts +23 -0
- package/dist/ts/frame/editor-frame.d.ts.map +1 -0
- package/dist/ts/frame/editor-frame.js +154 -0
- package/dist/ts/frame/editor-frame.js.map +1 -0
- package/dist/ts/frame/media-cache.d.ts +26 -0
- package/dist/ts/frame/media-cache.d.ts.map +1 -0
- package/dist/ts/frame/media-cache.js +123 -0
- package/dist/ts/frame/media-cache.js.map +1 -0
- package/dist/ts/frame/memory-router.d.ts +10 -0
- package/dist/ts/frame/memory-router.d.ts.map +1 -0
- package/dist/ts/frame/memory-router.js +39 -0
- package/dist/ts/frame/memory-router.js.map +1 -0
- package/dist/ts/frame/parent-hello.d.ts +11 -0
- package/dist/ts/frame/parent-hello.d.ts.map +1 -0
- package/dist/ts/frame/parent-hello.js +53 -0
- package/dist/ts/frame/parent-hello.js.map +1 -0
- package/dist/ts/frame/protocol.d.ts +201 -0
- package/dist/ts/frame/protocol.d.ts.map +1 -0
- package/dist/ts/frame/protocol.js +195 -0
- package/dist/ts/frame/protocol.js.map +1 -0
- package/dist/ts/frame/rpc-content-client.d.ts +10 -0
- package/dist/ts/frame/rpc-content-client.d.ts.map +1 -0
- package/dist/ts/frame/rpc-content-client.js +51 -0
- package/dist/ts/frame/rpc-content-client.js.map +1 -0
- package/dist/ts/frame/save-shortcut.d.ts +8 -0
- package/dist/ts/frame/save-shortcut.d.ts.map +1 -0
- package/dist/ts/frame/save-shortcut.js +26 -0
- package/dist/ts/frame/save-shortcut.js.map +1 -0
- package/dist/ts/host-schema.d.ts +73 -0
- package/dist/ts/host-schema.d.ts.map +1 -0
- package/dist/ts/host-schema.js +66 -0
- package/dist/ts/host-schema.js.map +1 -0
- package/dist/ts/host-style-probe.d.ts +32 -37
- package/dist/ts/host-style-probe.d.ts.map +1 -1
- package/dist/ts/host-style-probe.js +102 -68
- package/dist/ts/host-style-probe.js.map +1 -1
- package/dist/ts/index.d.ts +4 -2
- package/dist/ts/index.d.ts.map +1 -1
- package/dist/ts/index.js +6 -2
- package/dist/ts/index.js.map +1 -1
- package/dist/ts/main-spa.d.ts.map +1 -1
- package/dist/ts/main-spa.js +2 -1
- package/dist/ts/main-spa.js.map +1 -1
- package/dist/ts/page-schema-define.d.ts +14 -12
- package/dist/ts/page-schema-define.d.ts.map +1 -1
- package/dist/ts/page-schema-define.js +8 -5
- package/dist/ts/page-schema-define.js.map +1 -1
- package/dist/ts/prose-surface-css.d.ts +3 -5
- package/dist/ts/prose-surface-css.d.ts.map +1 -1
- package/dist/ts/prose-surface-css.js +3 -5
- package/dist/ts/prose-surface-css.js.map +1 -1
- package/dist/ts/site-styles.d.ts +4 -21
- package/dist/ts/site-styles.d.ts.map +1 -1
- package/dist/ts/site-styles.js.map +1 -1
- package/dist/ts/ui-config.d.ts +52 -1
- package/dist/ts/ui-config.d.ts.map +1 -1
- package/dist/ts/ui-config.js +4 -0
- package/dist/ts/ui-config.js.map +1 -1
- package/dist/ts/utils/asset-url.d.ts +2 -0
- package/dist/ts/utils/asset-url.d.ts.map +1 -1
- package/dist/ts/utils/asset-url.js +7 -0
- package/dist/ts/utils/asset-url.js.map +1 -1
- package/dist/ts/utils/pageMetadata.d.ts.map +1 -1
- package/dist/ts/utils/pageMetadata.js +27 -10
- package/dist/ts/utils/pageMetadata.js.map +1 -1
- package/dist/ts/utils/schema-fields.d.ts +23 -0
- package/dist/ts/utils/schema-fields.d.ts.map +1 -0
- package/dist/ts/utils/schema-fields.js +82 -0
- package/dist/ts/utils/schema-fields.js.map +1 -0
- package/package.json +56 -18
- package/src/App.tsx +35 -14
- package/src/CmsSpaApp.tsx +5 -1
- package/src/components/CMSDashboard.tsx +12 -4
- package/src/components/RouteErrorPage.tsx +75 -0
- package/src/components/dashboard/RecentActivity.tsx +3 -7
- package/src/components/dialogs/CreateDataDialog.tsx +2 -2
- package/src/components/dialogs/CreateFolderDialog.tsx +2 -1
- package/src/components/dialogs/CreatePageFlow.tsx +117 -0
- package/src/components/dialogs/DataPropertiesDialog.tsx +3 -2
- package/src/components/dialogs/FragmentPropertiesDialog.tsx +3 -2
- package/src/components/dialogs/PageMetadataDialog.tsx +5 -28
- package/src/components/editor/CMSMDXEditor.tsx +49 -69
- package/src/components/editor/DrawerItemOverride.tsx +14 -8
- package/src/components/editor/PageEditor.tsx +135 -36
- package/src/components/editor/PageEditorHeaderActions.tsx +1 -1
- package/src/components/editor/PageEditorWrapper.tsx +51 -12
- package/src/components/editor/draft-media-config.ts +197 -0
- package/src/components/editor/preview-availability.ts +6 -5
- package/src/components/editors/DataEditor.tsx +2 -2
- package/src/components/forms/HintForm.tsx +7 -5
- package/src/components/forms/SchemaForm.tsx +41 -12
- package/src/components/media/AssetCard.tsx +10 -7
- package/src/components/media/AssetDetailSidebar.tsx +6 -6
- package/src/components/media/FolderTreeSidebar.tsx +3 -1
- package/src/components/pages/PageList.tsx +17 -119
- package/src/components/sandbox/ContentSandbox.tsx +34 -5
- package/src/content-form-fields.ts +79 -0
- package/src/content-wrapper.ts +284 -0
- package/src/data-schemas.ts +4 -3
- package/src/frame/channel.ts +258 -0
- package/src/frame/editor-frame.ts +180 -0
- package/src/frame/media-cache.ts +144 -0
- package/src/frame/memory-router.ts +39 -0
- package/src/frame/parent-hello.ts +62 -0
- package/src/frame/protocol.ts +323 -0
- package/src/frame/rpc-content-client.ts +58 -0
- package/src/frame/save-shortcut.ts +25 -0
- package/src/host-schema.ts +122 -0
- package/src/host-style-probe.ts +120 -90
- package/src/index.ts +7 -2
- package/src/main-spa.tsx +2 -0
- package/src/main.css +12 -8
- package/src/page-schema-define.ts +29 -17
- package/src/prose-surface-css.ts +3 -5
- package/src/site-styles.ts +4 -22
- package/src/ui-config.ts +54 -1
- package/src/utils/asset-url.ts +8 -0
- package/src/utils/pageMetadata.ts +27 -10
- package/src/utils/schema-fields.ts +106 -0
- package/dist/ts/host-discovery.d.ts +0 -40
- package/dist/ts/host-discovery.d.ts.map +0 -1
- package/dist/ts/host-discovery.js +0 -39
- package/dist/ts/host-discovery.js.map +0 -1
- package/dist/ts/utils/zodIntrospect.d.ts +0 -19
- package/dist/ts/utils/zodIntrospect.d.ts.map +0 -1
- package/dist/ts/utils/zodIntrospect.js +0 -95
- package/dist/ts/utils/zodIntrospect.js.map +0 -1
- package/src/host-discovery.ts +0 -77
- package/src/utils/zodIntrospect.ts +0 -136
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { DataContext, LocalizedEntry, UpdateResult } from '@conloca/content-api-client';
|
|
2
|
-
import type
|
|
2
|
+
import { type Config, createUsePuck } from '@puckeditor/core';
|
|
3
3
|
import { Monitor, Smartphone, Tablet } from 'lucide-react';
|
|
4
4
|
import React, { useCallback, useMemo, useRef, useState } from 'react';
|
|
5
5
|
import { notifyEditorDirtyChanged, registerEditorFlushHandler } from '../../editor-flush.js';
|
|
6
|
+
import { useCanvasSaveShortcut } from '../../frame/save-shortcut.js';
|
|
6
7
|
import { useSiteBaseUrl } from '../../hooks/index.js';
|
|
7
8
|
import { useCanvasTheme } from '../../hooks/useCanvasTheme.js';
|
|
8
9
|
import { useUnsavedChangesGuard } from '../../hooks/useUnsavedChangesGuard.js';
|
|
@@ -14,7 +15,8 @@ import { ConflictDialog } from '../dialogs/ConflictDialog.js';
|
|
|
14
15
|
import { UnsavedChangesDialog } from '../dialogs/UnsavedChangesDialog.js';
|
|
15
16
|
import { ImageFieldRender } from '../fields/ImageField.js';
|
|
16
17
|
import { toast } from '../toast.js';
|
|
17
|
-
import { DrawerItemOverride } from './DrawerItemOverride.js';
|
|
18
|
+
import { DrawerItemOverride, PUCK_ROOT_DROP_ZONE } from './DrawerItemOverride.js';
|
|
19
|
+
import { withDraftMediaRendering } from './draft-media-config.js';
|
|
18
20
|
import { PageEditorHeaderActions } from './PageEditorHeaderActions.js';
|
|
19
21
|
import { PuckWithUntouchedMetadata } from './PuckWithUntouchedMetadata.js';
|
|
20
22
|
import {
|
|
@@ -44,6 +46,20 @@ const viewports = [
|
|
|
44
46
|
{ width: 1280, height: 'auto' as const, label: 'Desktop', icon: <Monitor size={16} /> },
|
|
45
47
|
];
|
|
46
48
|
|
|
49
|
+
/**
|
|
50
|
+
* The canvas options, a module constant on purpose. `<Puck>` re-runs its canvas viewport effect whenever `iframe`
|
|
51
|
+
* changes identity (https://github.com/puckeditor/puck/pull/1864, open against 0.23), and while the undo history
|
|
52
|
+
* still holds only its first entry that effect overwrites the entry with the page as it is by then. This editor
|
|
53
|
+
* re-renders on every edit, so an inline object would replace the loaded page in the history with the result of the
|
|
54
|
+
* first edit before Puck records that edit: Undo would then flip the history buttons and leave the edit on the canvas.
|
|
55
|
+
*
|
|
56
|
+
* The canvas shows the SITE, so it gets the site's stylesheets and nothing else. Puck's default mirrors this admin
|
|
57
|
+
* document's head into the iframe, which for a consumer whose editor and site are separate apps means the CMS
|
|
58
|
+
* chrome's own CSS lands on top of the page being edited — Tailwind's preflight there flattens headings the site
|
|
59
|
+
* styles by element rather than by utility class.
|
|
60
|
+
*/
|
|
61
|
+
const CANVAS_IFRAME = { syncHostStyles: false };
|
|
62
|
+
|
|
47
63
|
/**
|
|
48
64
|
* Stable field type overrides for Puck.
|
|
49
65
|
* Defined at module level to maintain referential stability across renders.
|
|
@@ -77,6 +93,29 @@ const drawerItemOverride = ({ children, name }: { children: React.ReactNode; nam
|
|
|
77
93
|
<DrawerItemOverride name={name}>{children}</DrawerItemOverride>
|
|
78
94
|
);
|
|
79
95
|
|
|
96
|
+
/** On the canvas's scaled-down view (about half size) this still reads at a normal text size. */
|
|
97
|
+
const EMPTY_PAGE_HINT_FONT_SIZE = '24px';
|
|
98
|
+
|
|
99
|
+
const EMPTY_PAGE_HINT = 'Drag a block here, or double-click one in the list';
|
|
100
|
+
|
|
101
|
+
const EMPTY_PAGE_HINT_DECLARATIONS = [
|
|
102
|
+
`content: ${JSON.stringify(EMPTY_PAGE_HINT)}`,
|
|
103
|
+
'display: flex',
|
|
104
|
+
'align-items: center',
|
|
105
|
+
'justify-content: center',
|
|
106
|
+
'box-sizing: border-box',
|
|
107
|
+
'height: 100%',
|
|
108
|
+
'min-height: inherit',
|
|
109
|
+
'padding: 16px',
|
|
110
|
+
'text-align: center',
|
|
111
|
+
'font-family: var(--puck-font-family)',
|
|
112
|
+
`font-size: ${EMPTY_PAGE_HINT_FONT_SIZE}`,
|
|
113
|
+
'color: var(--puck-color-grey-05)',
|
|
114
|
+
].join('; ');
|
|
115
|
+
|
|
116
|
+
const CAN_INSERT_ATTRIBUTE = 'data-conloca-can-insert';
|
|
117
|
+
const CAN_INSERT_SELECTOR = `html[${CAN_INSERT_ATTRIBUTE}]`;
|
|
118
|
+
|
|
80
119
|
/**
|
|
81
120
|
* An editing rule the preview canvas gets from the CMS. The <Puck> below
|
|
82
121
|
* opts out of Puck's host-style mirror, so main.css never reaches the canvas;
|
|
@@ -91,9 +130,27 @@ const drawerItemOverride = ({ children, name }: { children: React.ReactNode; nam
|
|
|
91
130
|
* a child), keeps Puck's highlight. `[data-puck-dropzone]` is the attribute
|
|
92
131
|
* Puck puts on every drop area; matching class names would also catch Puck's
|
|
93
132
|
* drop-position line and hide it.
|
|
133
|
+
*
|
|
134
|
+
* A page with no blocks also says how to add one: the empty root area is the
|
|
135
|
+
* whole canvas, so a line of text centred in it is where an author looks first.
|
|
136
|
+
* It is the area's `::before`, so it goes as soon as the area holds a block or
|
|
137
|
+
* a drop preview, and it never becomes content that can be saved. It is sized
|
|
138
|
+
* for the canvas's scaled-down view, where it must stay readable. It shows in
|
|
139
|
+
* edit mode only (the interactive preview renders the published page) and only
|
|
140
|
+
* while the editor marks the canvas as one that can take a block, because both
|
|
141
|
+
* ways the hint names do nothing when no block may be inserted.
|
|
94
142
|
*/
|
|
95
|
-
const EMPTY_DROP_AREA_CSS =
|
|
96
|
-
'[data-puck-dropzone]:empty { --puck-slot-color-bg: transparent; --puck-slot-color-border: var(--puck-color-grey-07); }'
|
|
143
|
+
const EMPTY_DROP_AREA_CSS = [
|
|
144
|
+
'[data-puck-dropzone]:empty { --puck-slot-color-bg: transparent; --puck-slot-color-border: var(--puck-color-grey-07); }',
|
|
145
|
+
`${CAN_INSERT_SELECTOR} [data-puck-entry][data-puck-preview-mode="edit"] [data-puck-dropzone="${PUCK_ROOT_DROP_ZONE}"]:empty::before { ${EMPTY_PAGE_HINT_DECLARATIONS} }`,
|
|
146
|
+
].join('\n');
|
|
147
|
+
|
|
148
|
+
const usePuck = createUsePuck();
|
|
149
|
+
|
|
150
|
+
/** Whether any block may be added to the page: the hint's two ways of adding one both need it. */
|
|
151
|
+
function usePageCanTakeAnInsert(): boolean {
|
|
152
|
+
return usePuck((state) => Object.keys(state.config.components).some((type) => state.getPermissions({ type }).insert));
|
|
153
|
+
}
|
|
97
154
|
|
|
98
155
|
/**
|
|
99
156
|
* Bridges CMS editor state into Puck's preview iframe.
|
|
@@ -108,12 +165,11 @@ const EMPTY_DROP_AREA_CSS =
|
|
|
108
165
|
* rules (e.g. `body { color: var(...) }`) cannot cascade into Puck's
|
|
109
166
|
* chrome — the iframe is the only intended consumer.
|
|
110
167
|
*
|
|
111
|
-
* The `<Puck>` below turns off Puck's host-style mirror
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* cleanup.
|
|
168
|
+
* The `<Puck>` below turns off Puck's host-style mirror (`CANVAS_IFRAME`),
|
|
169
|
+
* so the iframe head holds only Puck's own interaction CSS, the
|
|
170
|
+
* empty-drop-area rule (point 3), the page-setting cursor rule (point 4)
|
|
171
|
+
* and these tags. Nothing else touches them, so they are appended on
|
|
172
|
+
* every `siteStyles` change and removed on cleanup.
|
|
117
173
|
*
|
|
118
174
|
* 3. Injects `EMPTY_DROP_AREA_CSS` once per iframe document.
|
|
119
175
|
*
|
|
@@ -130,6 +186,7 @@ export function IframeBridge({ children, document: iframeDoc }: { children: Reac
|
|
|
130
186
|
const { canvasTheme } = useCanvasTheme();
|
|
131
187
|
const siteStyles = useSiteStyles();
|
|
132
188
|
usePageSettingTargets(iframeDoc);
|
|
189
|
+
useCanvasSaveShortcut(iframeDoc);
|
|
133
190
|
|
|
134
191
|
React.useEffect(() => {
|
|
135
192
|
if (!iframeDoc?.documentElement) return;
|
|
@@ -188,6 +245,34 @@ export function IframeBridge({ children, document: iframeDoc }: { children: Reac
|
|
|
188
245
|
return <>{children}</>;
|
|
189
246
|
}
|
|
190
247
|
|
|
248
|
+
/**
|
|
249
|
+
* `IframeBridge` for the canvas inside <Puck>: also marks the canvas document while the page can take a block, which
|
|
250
|
+
* the empty-page hint in `EMPTY_DROP_AREA_CSS` waits for. Separate because the marker reads Puck's state, so it only
|
|
251
|
+
* works inside <Puck>, while the bridge also renders on its own.
|
|
252
|
+
*/
|
|
253
|
+
function IframeWithInsertMarker({ children, document: iframeDoc }: { children: React.ReactNode; document?: Document }) {
|
|
254
|
+
const canTakeAnInsert = usePageCanTakeAnInsert();
|
|
255
|
+
React.useEffect(() => {
|
|
256
|
+
const html = iframeDoc?.documentElement;
|
|
257
|
+
if (!html) return undefined;
|
|
258
|
+
html.toggleAttribute(CAN_INSERT_ATTRIBUTE, canTakeAnInsert);
|
|
259
|
+
return () => html.removeAttribute(CAN_INSERT_ATTRIBUTE);
|
|
260
|
+
}, [iframeDoc, canTakeAnInsert]);
|
|
261
|
+
|
|
262
|
+
return <IframeBridge document={iframeDoc}>{children}</IframeBridge>;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* A genuine preview failure, which is a DIFFERENT situation and must not read as though the site is misconfigured.
|
|
267
|
+
* Not latched: a transient fault must not disable a working button.
|
|
268
|
+
*/
|
|
269
|
+
function reportPreviewFailure(error: unknown) {
|
|
270
|
+
toast.error('Could not open preview', {
|
|
271
|
+
description: 'Something went wrong creating the preview link. Please try again.',
|
|
272
|
+
});
|
|
273
|
+
console.error('[PageEditor] openPreview failed:', error);
|
|
274
|
+
}
|
|
275
|
+
|
|
191
276
|
export function PageEditor({
|
|
192
277
|
entry,
|
|
193
278
|
config,
|
|
@@ -200,6 +285,11 @@ export function PageEditor({
|
|
|
200
285
|
onReload,
|
|
201
286
|
onPublish,
|
|
202
287
|
}: PageEditorProps) {
|
|
288
|
+
const draftAssetsBasePath = getUIConfig().draftAssetsBasePath;
|
|
289
|
+
const renderConfig = useMemo(
|
|
290
|
+
() => withDraftMediaRendering(config, draftAssetsBasePath),
|
|
291
|
+
[config, draftAssetsBasePath],
|
|
292
|
+
);
|
|
203
293
|
const [data, setData] = useState(entry.localized.content.puckData);
|
|
204
294
|
const [saveState, setSaveState] = useState<SaveState>('idle');
|
|
205
295
|
const [isDirty, setIsDirty] = useState(false);
|
|
@@ -327,7 +417,7 @@ export function PageEditor({
|
|
|
327
417
|
|
|
328
418
|
const handlePreview = useCallback(() => {
|
|
329
419
|
const pathname = entry.localized.pathname || '/';
|
|
330
|
-
const openPreview = getUIConfig()
|
|
420
|
+
const { openPreview, openSiteUrl, previewTab: previewTabOwner } = getUIConfig();
|
|
331
421
|
if (openPreview) {
|
|
332
422
|
// Already known to be impossible: explain it without flashing a blank
|
|
333
423
|
// tab open and shut, which reads as a bug even when it is correct.
|
|
@@ -335,6 +425,26 @@ export function PageEditor({
|
|
|
335
425
|
toast.error(PREVIEW_UNAVAILABLE_TITLE, { description: previewBlockedReason, duration: 10_000 });
|
|
336
426
|
return;
|
|
337
427
|
}
|
|
428
|
+
// The site does not serve draft preview, so no URL exists that could
|
|
429
|
+
// work. Say so where the user is looking instead of navigating a tab to
|
|
430
|
+
// a 404 on their own domain, and remember it so the control goes
|
|
431
|
+
// disabled rather than staying a trap.
|
|
432
|
+
const markPreviewUnavailable = (message: string) => {
|
|
433
|
+
const detail = message !== '' ? message : PREVIEW_UNAVAILABLE_DETAIL;
|
|
434
|
+
setPreviewBlockedReason(detail);
|
|
435
|
+
setPreviewCapability('unavailable');
|
|
436
|
+
toast.error(PREVIEW_UNAVAILABLE_TITLE, { description: detail, duration: 10_000 });
|
|
437
|
+
};
|
|
438
|
+
// The parent frame opens the tab during the click itself; a blocked
|
|
439
|
+
// popup is the parent's to explain, so the editor shows nothing for it.
|
|
440
|
+
if (previewTabOwner === 'parent') {
|
|
441
|
+
void openPreview({ path: pathname })
|
|
442
|
+
.then((result) => {
|
|
443
|
+
if (!result.ok && result.reason === 'not-deployed') markPreviewUnavailable(result.message);
|
|
444
|
+
})
|
|
445
|
+
.catch(reportPreviewFailure);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
338
448
|
// Popup blockers only allow window creation during the click itself.
|
|
339
449
|
// `openPreview` answers asynchronously, so reserve a blank tab now and
|
|
340
450
|
// navigate it once the preview URL arrives.
|
|
@@ -347,31 +457,26 @@ export function PageEditor({
|
|
|
347
457
|
void openPreview({ path: pathname })
|
|
348
458
|
.then((result) => {
|
|
349
459
|
if (result.ok) {
|
|
350
|
-
previewTab.location.href = result.url;
|
|
460
|
+
if ('url' in result) previewTab.location.href = result.url;
|
|
461
|
+
else previewTab.close();
|
|
351
462
|
return;
|
|
352
463
|
}
|
|
353
|
-
// The site does not serve draft preview, so no URL exists that could
|
|
354
|
-
// work. Say so where the user is looking instead of navigating the
|
|
355
|
-
// tab to a 404 on their own domain, and remember it so the control
|
|
356
|
-
// goes disabled rather than staying a trap.
|
|
357
464
|
previewTab.close();
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
setPreviewCapability('unavailable');
|
|
361
|
-
toast.error(PREVIEW_UNAVAILABLE_TITLE, { description: detail, duration: 10_000 });
|
|
465
|
+
// `popup-blocked` cannot happen here, the editor holds the tab.
|
|
466
|
+
if (result.reason === 'not-deployed') markPreviewUnavailable(result.message);
|
|
362
467
|
})
|
|
363
468
|
.catch((error: unknown) => {
|
|
364
|
-
// A genuine failure, which is a DIFFERENT situation and must not
|
|
365
|
-
// read as though the site is misconfigured. Not latched:
|
|
366
|
-
// a transient fault must not disable a working button.
|
|
367
469
|
previewTab.close();
|
|
368
|
-
|
|
369
|
-
description: 'Something went wrong creating the preview link. Please try again.',
|
|
370
|
-
});
|
|
371
|
-
console.error('[PageEditor] openPreview failed:', error);
|
|
470
|
+
reportPreviewFailure(error);
|
|
372
471
|
});
|
|
373
472
|
return;
|
|
374
473
|
}
|
|
474
|
+
if (openSiteUrl) {
|
|
475
|
+
void openSiteUrl({ path: pathname }).catch((error: unknown) =>
|
|
476
|
+
console.error('[PageEditor] openSiteUrl failed:', error),
|
|
477
|
+
);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
375
480
|
const previewUrl = buildUrl(pathname);
|
|
376
481
|
const cacheBustedUrl = `${previewUrl}${previewUrl.includes('?') ? '&' : '?'}_t=${Date.now()}`;
|
|
377
482
|
window.open(cacheBustedUrl, '_blank');
|
|
@@ -436,7 +541,7 @@ export function PageEditor({
|
|
|
436
541
|
},
|
|
437
542
|
drawerItem: drawerItemOverride,
|
|
438
543
|
fields: FieldsWithPageSettingsHint,
|
|
439
|
-
iframe:
|
|
544
|
+
iframe: IframeWithInsertMarker,
|
|
440
545
|
}),
|
|
441
546
|
[],
|
|
442
547
|
);
|
|
@@ -465,20 +570,14 @@ export function PageEditor({
|
|
|
465
570
|
<PageSettingTargetsProvider onOpen={onOpenMetadata}>
|
|
466
571
|
{/* Not <Puck> itself: on the canvas Puck rewrites the page data it hands blocks; see PuckWithUntouchedMetadata.tsx. */}
|
|
467
572
|
<PuckWithUntouchedMetadata
|
|
468
|
-
config={
|
|
573
|
+
config={renderConfig}
|
|
469
574
|
data={data}
|
|
470
575
|
metadata={metadata}
|
|
471
576
|
onChange={handleDataChange}
|
|
472
577
|
headerTitle={entry.localized.meta.title || 'Untitled Page'}
|
|
473
578
|
viewports={viewports}
|
|
474
579
|
overrides={overrides}
|
|
475
|
-
|
|
476
|
-
// nothing else. Puck's default mirrors this admin document's head
|
|
477
|
-
// into the iframe, which for a consumer whose editor and site are
|
|
478
|
-
// separate apps means the CMS chrome's own CSS lands on top of the
|
|
479
|
-
// page being edited — Tailwind's preflight there flattens headings
|
|
480
|
-
// the site styles by element rather than by utility class.
|
|
481
|
-
iframe={{ syncHostStyles: false }}
|
|
580
|
+
iframe={CANVAS_IFRAME}
|
|
482
581
|
/>
|
|
483
582
|
</PageSettingTargetsProvider>
|
|
484
583
|
</div>
|
|
@@ -109,7 +109,7 @@ export function PageEditorHeaderActions({
|
|
|
109
109
|
onClick={onSave}
|
|
110
110
|
disabled={!isDirty && saveState !== 'error' && saveState !== 'conflict'}
|
|
111
111
|
className={cn(
|
|
112
|
-
'w-[100px] px-4 py-2 rounded-md text-sm transition-all duration-150 flex items-center justify-center gap-1 whitespace-nowrap font-medium',
|
|
112
|
+
'w-[100px] px-4 py-2 rounded-md text-sm transition-all duration-150 flex items-center justify-center gap-1 whitespace-nowrap font-medium disabled:cursor-not-allowed',
|
|
113
113
|
saveState === 'saved' && 'bg-green-10 text-green-06 cursor-pointer',
|
|
114
114
|
saveState === 'error' && 'bg-red-10 text-red-06 hover:bg-red-09 cursor-pointer',
|
|
115
115
|
saveState === 'conflict' && 'bg-yellow-10 text-yellow-06 hover:bg-yellow-09 cursor-pointer',
|
|
@@ -259,20 +259,30 @@ export function PageEditorWrapper({ puckConfig }: PageEditorWrapperProps) {
|
|
|
259
259
|
return <MdxPageEditor />;
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
|
|
262
|
+
// Keyed by page: the route element survives navigation between pages, and
|
|
263
|
+
// the locale state must restart from the newly opened page's locales.
|
|
264
|
+
return <PuckPageEditorInner key={id} puckConfig={puckConfig} initialLocale={initialPageLocale(content.locales)} />;
|
|
263
265
|
}
|
|
264
266
|
|
|
265
|
-
|
|
267
|
+
/**
|
|
268
|
+
* The locale the editor opens a page in: English when the page has it, as
|
|
269
|
+
* before, otherwise a locale the page actually has — opening on a locale with
|
|
270
|
+
* no variant fails the load with no way to reach the switcher.
|
|
271
|
+
*/
|
|
272
|
+
function initialPageLocale(locales: Record<string, unknown>): string {
|
|
273
|
+
if (locales.en) return 'en';
|
|
274
|
+
return Object.keys(locales).find((locale) => locales[locale]) ?? 'en';
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function PuckPageEditorInner({ puckConfig, initialLocale }: PageEditorWrapperProps & { initialLocale: string }) {
|
|
266
278
|
const { id } = useParams();
|
|
267
279
|
const navigate = useNavigate();
|
|
268
280
|
const updateContent = useUpdateLocalized();
|
|
269
281
|
|
|
270
282
|
// Mirrors FragmentEditor / MdxPageEditor: derive the locale list from the
|
|
271
|
-
// host's sites config and let the editor switch between them.
|
|
272
|
-
//
|
|
273
|
-
|
|
274
|
-
// from the switcher PageEditor renders when availableLocales.length > 1.
|
|
275
|
-
const [currentLocale, setCurrentLocale] = useState<string>('en');
|
|
283
|
+
// host's sites config and let the editor switch between them. It opens on
|
|
284
|
+
// a locale the page has (see initialPageLocale).
|
|
285
|
+
const [currentLocale, setCurrentLocale] = useState<string>(initialLocale);
|
|
276
286
|
const { data: sitesConfig } = useSitesConfig();
|
|
277
287
|
const availableLocales = sitesConfig?.globalLocales || ['en'];
|
|
278
288
|
|
|
@@ -320,9 +330,28 @@ function PuckPageEditorInner({ puckConfig }: PageEditorWrapperProps) {
|
|
|
320
330
|
const puckData = content?.localized?.content?.puckData;
|
|
321
331
|
|
|
322
332
|
// Resolving again replaces the editor, unsaved block edits included, so it
|
|
323
|
-
// runs when the blocks
|
|
324
|
-
//
|
|
325
|
-
// canvas takes from
|
|
333
|
+
// runs when the blocks change other than by the editor's own save, or the
|
|
334
|
+
// data their route binds changes, and reads the page when it runs: a Page
|
|
335
|
+
// settings save changes only the page, which the canvas takes from
|
|
336
|
+
// `metadata` without remounting.
|
|
337
|
+
//
|
|
338
|
+
// The editor's own save puts the page it read back into the cache, but the
|
|
339
|
+
// editor already holds that page, and anything typed while the save was
|
|
340
|
+
// out, which a new editor would drop. So a change to the blocks counts only
|
|
341
|
+
// when it is not that page: another etag, or another locale than the one the
|
|
342
|
+
// editor holds.
|
|
343
|
+
const ownSaveRef = useRef<{ locale: string; etag: string } | null>(null);
|
|
344
|
+
const blocksRef = useRef({ puckData, locale: currentLocale, changes: 0 });
|
|
345
|
+
if (puckData !== blocksRef.current.puckData) {
|
|
346
|
+
const own = ownSaveRef.current;
|
|
347
|
+
const ownSave =
|
|
348
|
+
own !== null &&
|
|
349
|
+
own.locale === currentLocale &&
|
|
350
|
+
blocksRef.current.locale === currentLocale &&
|
|
351
|
+
own.etag === content?.localized?.etag;
|
|
352
|
+
blocksRef.current = { puckData, locale: currentLocale, changes: blocksRef.current.changes + (ownSave ? 0 : 1) };
|
|
353
|
+
}
|
|
354
|
+
const blockChanges = blocksRef.current.changes;
|
|
326
355
|
const dataContextRef = useRef(dataContext);
|
|
327
356
|
dataContextRef.current = dataContext;
|
|
328
357
|
const routeCollections = dataContext?.collections;
|
|
@@ -331,7 +360,7 @@ function PuckPageEditorInner({ puckConfig }: PageEditorWrapperProps) {
|
|
|
331
360
|
// Pre-resolve component data using resolveAllData.
|
|
332
361
|
// This runs each component's resolveData with the data context as its
|
|
333
362
|
// metadata, ensuring data-bound components have their data BEFORE Puck mounts.
|
|
334
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: the route's bound data
|
|
363
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the blocks' changes and the route's bound data are the events; the page and its data context are read when it runs.
|
|
335
364
|
useEffect(() => {
|
|
336
365
|
let cancelled = false;
|
|
337
366
|
|
|
@@ -365,7 +394,7 @@ function PuckPageEditorInner({ puckConfig }: PageEditorWrapperProps) {
|
|
|
365
394
|
return () => {
|
|
366
395
|
cancelled = true;
|
|
367
396
|
};
|
|
368
|
-
}, [
|
|
397
|
+
}, [blockChanges, enhancedConfig, routeCollections, routePages]);
|
|
369
398
|
|
|
370
399
|
const pageMetadata = useMemo(() => (content?.localized ? extractPageMetadata(content) : null), [content]);
|
|
371
400
|
|
|
@@ -429,6 +458,16 @@ function PuckPageEditorInner({ puckConfig }: PageEditorWrapperProps) {
|
|
|
429
458
|
if (result.success && result.etag) {
|
|
430
459
|
setCurrentEtag(result.etag);
|
|
431
460
|
}
|
|
461
|
+
// Recorded before this component renders the page the save read back: the cache tells its readers
|
|
462
|
+
// on a later task, and `mutateAsync` settles first. Only a read-back of exactly this write is the
|
|
463
|
+
// editor's own; one someone else saved since must open, or the next save would write over it.
|
|
464
|
+
if (
|
|
465
|
+
'updatedContent' in result &&
|
|
466
|
+
result.updatedContent &&
|
|
467
|
+
result.updatedContent.localized.etag === result.etag
|
|
468
|
+
) {
|
|
469
|
+
ownSaveRef.current = { locale: currentLocale, etag: result.updatedContent.localized.etag };
|
|
470
|
+
}
|
|
432
471
|
|
|
433
472
|
return result;
|
|
434
473
|
} catch (error) {
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/** Resolve unpublished draft images for site renderers without changing Puck's persisted data. */
|
|
2
|
+
/* oxlint-disable anti-slop/no-unsafe-dictionary-type, anti-slop/no-unknown-returns -- block props are whatever the site's components take; this walk only swaps image strings and passes every other value through as it came. */
|
|
3
|
+
import { IMAGE_EXTENSION_MIME } from '@conloca/content-api-client';
|
|
4
|
+
import type { Config, Field, Fields } from '@puckeditor/core';
|
|
5
|
+
import { type ComponentType, createElement, isValidElement } from 'react';
|
|
6
|
+
import { loadedMedia, mediaObjectUrl, useMediaCacheVersion } from '../../frame/media-cache.js';
|
|
7
|
+
import { getUIConfig } from '../../ui-config.js';
|
|
8
|
+
import { appendAssetUrlPath } from '../../utils/asset-url.js';
|
|
9
|
+
|
|
10
|
+
/** What a validated draft image path renders as; `undefined` keeps the canonical value. */
|
|
11
|
+
type DraftImageResolver = (path: string) => string | undefined;
|
|
12
|
+
|
|
13
|
+
/** The raw media path of a canonical `/assets/<path>` image reference, or `undefined` when it is not a safe image path. */
|
|
14
|
+
function draftImagePath(value: string): string | undefined {
|
|
15
|
+
if (!value.startsWith('/assets/')) return undefined;
|
|
16
|
+
const path = value.slice('/assets/'.length);
|
|
17
|
+
if (/[\\?#]/.test(path)) return undefined;
|
|
18
|
+
const segments = path.split('/');
|
|
19
|
+
for (const part of segments) {
|
|
20
|
+
if (
|
|
21
|
+
part === '' ||
|
|
22
|
+
part === '.' ||
|
|
23
|
+
part === '..' ||
|
|
24
|
+
[...part].some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127)
|
|
25
|
+
) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const filename = segments[segments.length - 1] ?? '';
|
|
30
|
+
const dot = filename.lastIndexOf('.');
|
|
31
|
+
if (dot <= 0) return undefined;
|
|
32
|
+
const extension = filename.slice(dot + 1).toLowerCase();
|
|
33
|
+
if (!Object.prototype.hasOwnProperty.call(IMAGE_EXTENSION_MIME, extension)) return undefined;
|
|
34
|
+
return path;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function draftImageUrl(path: string, base: string): string {
|
|
38
|
+
const encoded = path
|
|
39
|
+
.split('/')
|
|
40
|
+
.map((part) =>
|
|
41
|
+
encodeURIComponent(part).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`),
|
|
42
|
+
);
|
|
43
|
+
return appendAssetUrlPath(base, encoded.join('/'));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* What the MDX editor canvas shows for an image `src`: the loaded bytes of a draft image, else `src` itself.
|
|
48
|
+
* The URL is the media cache's own, which a media write or eviction revokes; mdxeditor resolves an image again only
|
|
49
|
+
* when its `src` changes or it remounts, so a mounted image keeps painting its decoded bytes from a revoked URL.
|
|
50
|
+
*/
|
|
51
|
+
export async function draftImagePreview(src: string): Promise<string> {
|
|
52
|
+
const path = draftImagePath(src);
|
|
53
|
+
return (path === undefined ? undefined : (await loadedMedia('draft', path))?.url) ?? src;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function dataUrl(blob: Blob): Promise<string> {
|
|
57
|
+
// The package's lib target predates Promise.withResolvers.
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const reader = new FileReader();
|
|
60
|
+
reader.addEventListener('load', () => {
|
|
61
|
+
if (typeof reader.result === 'string') resolve(reader.result);
|
|
62
|
+
else reject(new Error('The image could not be read as a data URL.'));
|
|
63
|
+
});
|
|
64
|
+
reader.addEventListener('error', () => {
|
|
65
|
+
reject(reader.error ?? new Error('The image could not be read.'));
|
|
66
|
+
});
|
|
67
|
+
reader.readAsDataURL(blob);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Keyed by the cached Blob, so a cleared or evicted cache entry frees its string too.
|
|
72
|
+
const dataUrls = new WeakMap<Blob, Promise<string>>();
|
|
73
|
+
|
|
74
|
+
async function draftImageDataUrl(src: string): Promise<string | undefined> {
|
|
75
|
+
const path = draftImagePath(src);
|
|
76
|
+
const media = path === undefined ? undefined : await loadedMedia('draft', path);
|
|
77
|
+
if (!media) return undefined;
|
|
78
|
+
let url = dataUrls.get(media.blob);
|
|
79
|
+
if (!url) {
|
|
80
|
+
url = dataUrl(media.blob);
|
|
81
|
+
dataUrls.set(media.blob, url);
|
|
82
|
+
}
|
|
83
|
+
return url;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** How long a preview update waits for its draft images before it goes without the ones still loading. */
|
|
87
|
+
export const DRAFT_IMAGE_WAIT_MS = 5000;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Data URLs of the draft images a compiled MDX body names, keyed by their canonical `src`, for the
|
|
91
|
+
* opaque-origin preview frame, which cannot read this page's blob URLs. Images that fail to load, or are
|
|
92
|
+
* still loading after `DRAFT_IMAGE_WAIT_MS`, are left out.
|
|
93
|
+
*/
|
|
94
|
+
export async function draftImageData(code: string): Promise<Record<string, string>> {
|
|
95
|
+
// shortcut: finds srcs only as plain double-quoted literals, the form the MDX compiler emits for these paths; widen if an escaped path must preview.
|
|
96
|
+
const srcs = new Set(code.match(/"\/assets\/[^"\\]*"/g)?.map((literal) => literal.slice(1, -1)));
|
|
97
|
+
const images: Record<string, string> = {};
|
|
98
|
+
let timer: number | undefined;
|
|
99
|
+
const expired = new Promise<undefined>((resolve) => {
|
|
100
|
+
timer = window.setTimeout(resolve, DRAFT_IMAGE_WAIT_MS, undefined);
|
|
101
|
+
});
|
|
102
|
+
await Promise.all(
|
|
103
|
+
[...srcs].map(async (src) => {
|
|
104
|
+
const url = await Promise.race([draftImageDataUrl(src).catch(() => undefined), expired]);
|
|
105
|
+
if (url !== undefined) images[src] = url;
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
window.clearTimeout(timer);
|
|
109
|
+
return images;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
113
|
+
if (value === null || typeof value !== 'object' || isValidElement(value)) return false;
|
|
114
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
115
|
+
return prototype === Object.prototype || prototype === null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function fieldValue(value: unknown, field: Field, resolve: DraftImageResolver): unknown {
|
|
119
|
+
if (field.type === 'slot') return value;
|
|
120
|
+
if (typeof value === 'string') {
|
|
121
|
+
const path = draftImagePath(value);
|
|
122
|
+
return path === undefined ? value : (resolve(path) ?? value);
|
|
123
|
+
}
|
|
124
|
+
if (field.type === 'object' && isRecord(value)) return fieldProps(value, field.objectFields, resolve);
|
|
125
|
+
if (field.type === 'array' && Array.isArray(value)) {
|
|
126
|
+
const next = value.map((item: unknown) => (isRecord(item) ? fieldProps(item, field.arrayFields, resolve) : item));
|
|
127
|
+
return next.some((item, index) => item !== value[index]) ? next : value;
|
|
128
|
+
}
|
|
129
|
+
return value;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function fieldProps(
|
|
133
|
+
props: Record<string, unknown>,
|
|
134
|
+
fields: Fields | undefined,
|
|
135
|
+
resolve: DraftImageResolver,
|
|
136
|
+
): Record<string, unknown> {
|
|
137
|
+
let next = props;
|
|
138
|
+
for (const [name, field] of Object.entries(fields ?? {})) {
|
|
139
|
+
if (name === 'puck' || name === 'children' || name === 'editMode') continue;
|
|
140
|
+
if (field === undefined || !Object.prototype.hasOwnProperty.call(props, name)) continue;
|
|
141
|
+
const value = fieldValue(props[name], field, resolve);
|
|
142
|
+
if (value === props[name]) continue;
|
|
143
|
+
if (next === props) next = { ...props };
|
|
144
|
+
next[name] = value;
|
|
145
|
+
}
|
|
146
|
+
return next;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Static declared fields bound traversal; dynamic field resolvers and runtime contexts remain untouched.
|
|
151
|
+
* With `UIConfig.loadMedia` set, `base` is ignored: draft images show object URLs of the loaded bytes,
|
|
152
|
+
* the canonical value until they arrive.
|
|
153
|
+
*/
|
|
154
|
+
export function withDraftMediaRendering(config: Config, base: string | undefined): Config {
|
|
155
|
+
const resolve: DraftImageResolver | undefined = getUIConfig().loadMedia
|
|
156
|
+
? (path) => mediaObjectUrl('draft', path)
|
|
157
|
+
: base === undefined
|
|
158
|
+
? undefined
|
|
159
|
+
: (path) => draftImageUrl(path, base);
|
|
160
|
+
if (resolve === undefined) return config;
|
|
161
|
+
const root = config.root;
|
|
162
|
+
const rootRender = root?.render;
|
|
163
|
+
return {
|
|
164
|
+
...config,
|
|
165
|
+
...(root && rootRender
|
|
166
|
+
? {
|
|
167
|
+
root: {
|
|
168
|
+
...root,
|
|
169
|
+
// Rendered as an element so memo/forwardRef roots keep working. Root props are a
|
|
170
|
+
// children/no-children union, so name the component's own prop type for createElement.
|
|
171
|
+
render: (props: Parameters<typeof rootRender>[0]) => {
|
|
172
|
+
// Re-render when a loaded image arrives.
|
|
173
|
+
useMediaCacheVersion();
|
|
174
|
+
// SAFETY: `props` is typed from `rootRender` itself; the cast only picks one member of Puck's root union.
|
|
175
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- see the SAFETY note.
|
|
176
|
+
return createElement(rootRender as ComponentType<typeof props>, {
|
|
177
|
+
...props,
|
|
178
|
+
...fieldProps(props, root.fields, resolve),
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
: {}),
|
|
184
|
+
components: Object.fromEntries(
|
|
185
|
+
Object.entries(config.components).map(([name, component]) => [
|
|
186
|
+
name,
|
|
187
|
+
{
|
|
188
|
+
...component,
|
|
189
|
+
render: (props: Parameters<typeof component.render>[0]) => {
|
|
190
|
+
useMediaCacheVersion();
|
|
191
|
+
return createElement(component.render, { ...props, ...fieldProps(props, component.fields, resolve) });
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
]),
|
|
195
|
+
),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -8,13 +8,14 @@
|
|
|
8
8
|
* `@lexical`.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
/** Toast heading when the
|
|
12
|
-
export const PREVIEW_UNAVAILABLE_TITLE = 'Preview is not available for this
|
|
11
|
+
/** Toast heading when the branch cannot show a preview. */
|
|
12
|
+
export const PREVIEW_UNAVAILABLE_TITLE = 'Preview is not available for this branch';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* The explanation, used both as the toast body and as the disabled control's
|
|
16
|
-
* reason.
|
|
17
|
-
*
|
|
16
|
+
* reason. Preview support is checked per branch (one origin can serve one
|
|
17
|
+
* branch's drafts and not another's), so it names the branch, never the whole
|
|
18
|
+
* site; it reassures that nothing they did is broken.
|
|
18
19
|
*/
|
|
19
20
|
export const PREVIEW_UNAVAILABLE_DETAIL =
|
|
20
|
-
'This
|
|
21
|
+
'This branch has no preview server, so there is no preview page to open. Your draft is still saved, and publishing and the live site are unaffected.';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getContentAPIClient, useLocalizedContent, useUpdateLocalized } from '@conloca/content-api-client';
|
|
2
2
|
import { AlertCircle, Code2, Loader2, Save } from 'lucide-react';
|
|
3
3
|
import { useEffect, useRef, useState } from 'react';
|
|
4
|
-
import type {
|
|
4
|
+
import type { HostSchema } from '../../host-schema.js';
|
|
5
5
|
import { contentFailureMessage } from '../../utils/content-failure.js';
|
|
6
6
|
import { UnsavedChangesDialog } from '../dialogs/UnsavedChangesDialog.js';
|
|
7
7
|
import { LocaleSelector } from '../editor/LocaleSelector.js';
|
|
@@ -12,7 +12,7 @@ interface DataEditorProps {
|
|
|
12
12
|
collection: string;
|
|
13
13
|
initialLocale: string;
|
|
14
14
|
existingLocales: string[];
|
|
15
|
-
schema:
|
|
15
|
+
schema: HostSchema | null;
|
|
16
16
|
onSave?: () => void;
|
|
17
17
|
onCancel?: () => void;
|
|
18
18
|
}
|
|
@@ -138,7 +138,9 @@ function HintField({ name, hint, value, onChange }: HintFieldProps) {
|
|
|
138
138
|
id={inputId}
|
|
139
139
|
type="datetime-local"
|
|
140
140
|
value={formatDateValue(value)}
|
|
141
|
-
|
|
141
|
+
// The picker's digits carry no zone or seconds; a host's `Format<'date-time'>`
|
|
142
|
+
// validator needs a full timestamp, so they are saved as the UTC instant they mean.
|
|
143
|
+
onChange={(e) => onChange(e.target.value ? `${e.target.value}:00.000Z` : undefined)}
|
|
142
144
|
/>
|
|
143
145
|
</Wrapper>
|
|
144
146
|
);
|
|
@@ -417,10 +419,10 @@ function formatLabel(name: string): string {
|
|
|
417
419
|
}
|
|
418
420
|
|
|
419
421
|
/**
|
|
420
|
-
* Host date hints
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
* authors west of UTC.
|
|
422
|
+
* Host date hints save the picker's digits as a UTC instant, and older pages
|
|
423
|
+
* may hold the bare zone-less digits, which readers also take as UTC. Slicing
|
|
424
|
+
* UTC hours off a Date or ISO string keeps the picker on that convention; local
|
|
425
|
+
* hours would shift the stored day across midnight for authors west of UTC.
|
|
424
426
|
*/
|
|
425
427
|
function formatDateValue(value: unknown): string {
|
|
426
428
|
if (!value) return '';
|