@conloca/cms-spa 0.2.2 → 0.2.4

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.
Files changed (259) hide show
  1. package/README.md +26 -0
  2. package/dist/spa/main.css +229 -1
  3. package/dist/ts/.build-inputs.json +187 -0
  4. package/dist/ts/App.d.ts +5 -1
  5. package/dist/ts/App.d.ts.map +1 -1
  6. package/dist/ts/App.js +13 -6
  7. package/dist/ts/App.js.map +1 -1
  8. package/dist/ts/CmsSpaApp.d.ts.map +1 -1
  9. package/dist/ts/CmsSpaApp.js +4 -1
  10. package/dist/ts/CmsSpaApp.js.map +1 -1
  11. package/dist/ts/components/CMSDashboard.d.ts.map +1 -1
  12. package/dist/ts/components/CMSDashboard.js +2 -1
  13. package/dist/ts/components/CMSDashboard.js.map +1 -1
  14. package/dist/ts/components/cards/DataEntryCard.d.ts +1 -1
  15. package/dist/ts/components/cards/DataEntryCard.d.ts.map +1 -1
  16. package/dist/ts/components/cards/SectionCard.d.ts +1 -1
  17. package/dist/ts/components/cards/SectionCard.d.ts.map +1 -1
  18. package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts +6 -1
  19. package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts.map +1 -1
  20. package/dist/ts/components/dialogs/DeleteConfirmDialog.js +2 -2
  21. package/dist/ts/components/dialogs/DeleteConfirmDialog.js.map +1 -1
  22. package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts +1 -1
  23. package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts.map +1 -1
  24. package/dist/ts/components/dialogs/MakePagesEditableDialog.d.ts +10 -0
  25. package/dist/ts/components/dialogs/MakePagesEditableDialog.d.ts.map +1 -0
  26. package/dist/ts/components/dialogs/MakePagesEditableDialog.js +35 -0
  27. package/dist/ts/components/dialogs/MakePagesEditableDialog.js.map +1 -0
  28. package/dist/ts/components/dialogs/MoveFolderDialog.d.ts +6 -1
  29. package/dist/ts/components/dialogs/MoveFolderDialog.d.ts.map +1 -1
  30. package/dist/ts/components/dialogs/MoveFolderDialog.js +3 -3
  31. package/dist/ts/components/dialogs/MoveFolderDialog.js.map +1 -1
  32. package/dist/ts/components/editor/CMSMDXEditor.d.ts.map +1 -1
  33. package/dist/ts/components/editor/CMSMDXEditor.js +53 -62
  34. package/dist/ts/components/editor/CMSMDXEditor.js.map +1 -1
  35. package/dist/ts/components/editor/DocsPreviewButton.d.ts +6 -0
  36. package/dist/ts/components/editor/DocsPreviewButton.d.ts.map +1 -0
  37. package/dist/ts/components/editor/DocsPreviewButton.js +15 -0
  38. package/dist/ts/components/editor/DocsPreviewButton.js.map +1 -0
  39. package/dist/ts/components/editor/ImagePickerDialog.js +2 -2
  40. package/dist/ts/components/editor/ImagePickerDialog.js.map +1 -1
  41. package/dist/ts/components/editor/LocaleSelector.d.ts +1 -1
  42. package/dist/ts/components/editor/LocaleSelector.d.ts.map +1 -1
  43. package/dist/ts/components/editor/MdxPageEditor.d.ts.map +1 -1
  44. package/dist/ts/components/editor/MdxPageEditor.js +2 -1
  45. package/dist/ts/components/editor/MdxPageEditor.js.map +1 -1
  46. package/dist/ts/components/editor/PageEditor.d.ts +1 -1
  47. package/dist/ts/components/editor/PageEditor.d.ts.map +1 -1
  48. package/dist/ts/components/editor/PageEditor.js +47 -19
  49. package/dist/ts/components/editor/PageEditor.js.map +1 -1
  50. package/dist/ts/components/editor/PageEditorHeaderActions.d.ts +1 -1
  51. package/dist/ts/components/editor/PageEditorHeaderActions.d.ts.map +1 -1
  52. package/dist/ts/components/editor/PageEditorWrapper.js +31 -5
  53. package/dist/ts/components/editor/PageEditorWrapper.js.map +1 -1
  54. package/dist/ts/components/editor/draft-media-config.d.ts +32 -2
  55. package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -1
  56. package/dist/ts/components/editor/draft-media-config.js +194 -39
  57. package/dist/ts/components/editor/draft-media-config.js.map +1 -1
  58. package/dist/ts/components/editor/page-setting-targets.d.ts +1 -1
  59. package/dist/ts/components/editor/page-setting-targets.d.ts.map +1 -1
  60. package/dist/ts/components/editor/preview-availability.d.ts +8 -5
  61. package/dist/ts/components/editor/preview-availability.d.ts.map +1 -1
  62. package/dist/ts/components/editor/preview-availability.js +8 -5
  63. package/dist/ts/components/editor/preview-availability.js.map +1 -1
  64. package/dist/ts/components/editors/DataEditor.d.ts +6 -2
  65. package/dist/ts/components/editors/DataEditor.d.ts.map +1 -1
  66. package/dist/ts/components/editors/DataEditor.js +46 -19
  67. package/dist/ts/components/editors/DataEditor.js.map +1 -1
  68. package/dist/ts/components/fields/ImageField.js +2 -2
  69. package/dist/ts/components/fields/ImageField.js.map +1 -1
  70. package/dist/ts/components/media/AssetCard.d.ts +1 -1
  71. package/dist/ts/components/media/AssetCard.d.ts.map +1 -1
  72. package/dist/ts/components/media/AssetCard.js +3 -2
  73. package/dist/ts/components/media/AssetCard.js.map +1 -1
  74. package/dist/ts/components/media/AssetDetailSidebar.d.ts +1 -1
  75. package/dist/ts/components/media/AssetDetailSidebar.d.ts.map +1 -1
  76. package/dist/ts/components/media/AssetDetailSidebar.js +7 -2
  77. package/dist/ts/components/media/AssetDetailSidebar.js.map +1 -1
  78. package/dist/ts/components/media/AssetUsageList.d.ts +13 -0
  79. package/dist/ts/components/media/AssetUsageList.d.ts.map +1 -0
  80. package/dist/ts/components/media/AssetUsageList.js +35 -0
  81. package/dist/ts/components/media/AssetUsageList.js.map +1 -0
  82. package/dist/ts/components/media/MediaLibrary.d.ts +1 -1
  83. package/dist/ts/components/media/MediaLibrary.d.ts.map +1 -1
  84. package/dist/ts/components/media/MediaLibrary.js +6 -4
  85. package/dist/ts/components/media/MediaLibrary.js.map +1 -1
  86. package/dist/ts/components/media/MediaToolbar.d.ts +1 -1
  87. package/dist/ts/components/media/MediaToolbar.d.ts.map +1 -1
  88. package/dist/ts/components/media/index.d.ts +1 -0
  89. package/dist/ts/components/media/index.d.ts.map +1 -1
  90. package/dist/ts/components/media/index.js +1 -0
  91. package/dist/ts/components/media/index.js.map +1 -1
  92. package/dist/ts/components/pages/DataList.d.ts.map +1 -1
  93. package/dist/ts/components/pages/DataList.js +48 -31
  94. package/dist/ts/components/pages/DataList.js.map +1 -1
  95. package/dist/ts/components/pages/MediaPage.d.ts.map +1 -1
  96. package/dist/ts/components/pages/MediaPage.js +7 -5
  97. package/dist/ts/components/pages/MediaPage.js.map +1 -1
  98. package/dist/ts/components/pages/NoPagesYet.d.ts +5 -0
  99. package/dist/ts/components/pages/NoPagesYet.d.ts.map +1 -0
  100. package/dist/ts/components/pages/NoPagesYet.js +16 -0
  101. package/dist/ts/components/pages/NoPagesYet.js.map +1 -0
  102. package/dist/ts/components/pages/PageList.d.ts.map +1 -1
  103. package/dist/ts/components/pages/PageList.js +19 -8
  104. package/dist/ts/components/pages/PageList.js.map +1 -1
  105. package/dist/ts/components/sandbox/ContentSandbox.d.ts +1 -1
  106. package/dist/ts/components/sandbox/ContentSandbox.d.ts.map +1 -1
  107. package/dist/ts/components/sandbox/ContentSandbox.js +36 -5
  108. package/dist/ts/components/sandbox/ContentSandbox.js.map +1 -1
  109. package/dist/ts/components/ui/IconButton.d.ts +1 -1
  110. package/dist/ts/components/ui/IconButton.d.ts.map +1 -1
  111. package/dist/ts/content-wrapper.d.ts +65 -0
  112. package/dist/ts/content-wrapper.d.ts.map +1 -0
  113. package/dist/ts/content-wrapper.js +190 -0
  114. package/dist/ts/content-wrapper.js.map +1 -0
  115. package/dist/ts/convert-pages.d.ts +30 -0
  116. package/dist/ts/convert-pages.d.ts.map +1 -0
  117. package/dist/ts/convert-pages.js +85 -0
  118. package/dist/ts/convert-pages.js.map +1 -0
  119. package/dist/ts/data-schemas.d.ts.map +1 -1
  120. package/dist/ts/frame/channel.d.ts +40 -0
  121. package/dist/ts/frame/channel.d.ts.map +1 -0
  122. package/dist/ts/frame/channel.js +233 -0
  123. package/dist/ts/frame/channel.js.map +1 -0
  124. package/dist/ts/frame/editor-frame.d.ts +23 -0
  125. package/dist/ts/frame/editor-frame.d.ts.map +1 -0
  126. package/dist/ts/frame/editor-frame.js +159 -0
  127. package/dist/ts/frame/editor-frame.js.map +1 -0
  128. package/dist/ts/frame/media-cache.d.ts +37 -0
  129. package/dist/ts/frame/media-cache.d.ts.map +1 -0
  130. package/dist/ts/frame/media-cache.js +138 -0
  131. package/dist/ts/frame/media-cache.js.map +1 -0
  132. package/dist/ts/frame/memory-router.d.ts +10 -0
  133. package/dist/ts/frame/memory-router.d.ts.map +1 -0
  134. package/dist/ts/frame/memory-router.js +39 -0
  135. package/dist/ts/frame/memory-router.js.map +1 -0
  136. package/dist/ts/frame/parent-hello.d.ts +11 -0
  137. package/dist/ts/frame/parent-hello.d.ts.map +1 -0
  138. package/dist/ts/frame/parent-hello.js +53 -0
  139. package/dist/ts/frame/parent-hello.js.map +1 -0
  140. package/dist/ts/frame/protocol.d.ts +209 -0
  141. package/dist/ts/frame/protocol.d.ts.map +1 -0
  142. package/dist/ts/frame/protocol.js +200 -0
  143. package/dist/ts/frame/protocol.js.map +1 -0
  144. package/dist/ts/frame/rpc-content-client.d.ts +10 -0
  145. package/dist/ts/frame/rpc-content-client.d.ts.map +1 -0
  146. package/dist/ts/frame/rpc-content-client.js +51 -0
  147. package/dist/ts/frame/rpc-content-client.js.map +1 -0
  148. package/dist/ts/frame/save-shortcut.d.ts +8 -0
  149. package/dist/ts/frame/save-shortcut.d.ts.map +1 -0
  150. package/dist/ts/frame/save-shortcut.js +26 -0
  151. package/dist/ts/frame/save-shortcut.js.map +1 -0
  152. package/dist/ts/hooks/index.d.ts +2 -0
  153. package/dist/ts/hooks/index.d.ts.map +1 -1
  154. package/dist/ts/hooks/index.js +1 -0
  155. package/dist/ts/hooks/index.js.map +1 -1
  156. package/dist/ts/hooks/usePageConversion.d.ts +11 -0
  157. package/dist/ts/hooks/usePageConversion.d.ts.map +1 -0
  158. package/dist/ts/hooks/usePageConversion.js +47 -0
  159. package/dist/ts/hooks/usePageConversion.js.map +1 -0
  160. package/dist/ts/hooks/useUploadFlow.d.ts.map +1 -1
  161. package/dist/ts/hooks/useUploadFlow.js +41 -7
  162. package/dist/ts/hooks/useUploadFlow.js.map +1 -1
  163. package/dist/ts/host-style-probe.d.ts +32 -37
  164. package/dist/ts/host-style-probe.d.ts.map +1 -1
  165. package/dist/ts/host-style-probe.js +86 -68
  166. package/dist/ts/host-style-probe.js.map +1 -1
  167. package/dist/ts/index.d.ts +6 -2
  168. package/dist/ts/index.d.ts.map +1 -1
  169. package/dist/ts/index.js +9 -2
  170. package/dist/ts/index.js.map +1 -1
  171. package/dist/ts/main-spa.d.ts.map +1 -1
  172. package/dist/ts/main-spa.js +2 -1
  173. package/dist/ts/main-spa.js.map +1 -1
  174. package/dist/ts/media/prepare-image-upload.d.ts +19 -0
  175. package/dist/ts/media/prepare-image-upload.d.ts.map +1 -0
  176. package/dist/ts/media/prepare-image-upload.js +93 -0
  177. package/dist/ts/media/prepare-image-upload.js.map +1 -0
  178. package/dist/ts/media/use-asset-serve-src.d.ts +7 -0
  179. package/dist/ts/media/use-asset-serve-src.d.ts.map +1 -0
  180. package/dist/ts/media/use-asset-serve-src.js +17 -0
  181. package/dist/ts/media/use-asset-serve-src.js.map +1 -0
  182. package/dist/ts/page-schemas.d.ts.map +1 -1
  183. package/dist/ts/prose-surface-css.d.ts +3 -5
  184. package/dist/ts/prose-surface-css.d.ts.map +1 -1
  185. package/dist/ts/prose-surface-css.js +3 -5
  186. package/dist/ts/prose-surface-css.js.map +1 -1
  187. package/dist/ts/puck-config.d.ts.map +1 -1
  188. package/dist/ts/selected-block.d.ts.map +1 -1
  189. package/dist/ts/site-styles.d.ts +4 -21
  190. package/dist/ts/site-styles.d.ts.map +1 -1
  191. package/dist/ts/site-styles.js.map +1 -1
  192. package/dist/ts/ui-config.d.ts +62 -3
  193. package/dist/ts/ui-config.d.ts.map +1 -1
  194. package/dist/ts/ui-config.js +18 -0
  195. package/dist/ts/ui-config.js.map +1 -1
  196. package/dist/ts/utils/asset-url.d.ts +3 -0
  197. package/dist/ts/utils/asset-url.d.ts.map +1 -1
  198. package/dist/ts/utils/asset-url.js +4 -0
  199. package/dist/ts/utils/asset-url.js.map +1 -1
  200. package/dist/ts/utils/data-entry-validation.d.ts +11 -0
  201. package/dist/ts/utils/data-entry-validation.d.ts.map +1 -0
  202. package/dist/ts/utils/data-entry-validation.js +15 -0
  203. package/dist/ts/utils/data-entry-validation.js.map +1 -0
  204. package/package.json +20 -11
  205. package/src/App.tsx +22 -10
  206. package/src/CmsSpaApp.tsx +5 -1
  207. package/src/components/CMSDashboard.tsx +16 -8
  208. package/src/components/dialogs/DeleteConfirmDialog.tsx +14 -1
  209. package/src/components/dialogs/MakePagesEditableDialog.tsx +112 -0
  210. package/src/components/dialogs/MoveFolderDialog.tsx +9 -2
  211. package/src/components/editor/CMSMDXEditor.tsx +56 -76
  212. package/src/components/editor/DocsPreviewButton.tsx +25 -0
  213. package/src/components/editor/ImagePickerDialog.tsx +2 -2
  214. package/src/components/editor/MdxPageEditor.tsx +2 -0
  215. package/src/components/editor/PageEditor.tsx +46 -22
  216. package/src/components/editor/PageEditorWrapper.tsx +34 -5
  217. package/src/components/editor/draft-media-config.ts +199 -41
  218. package/src/components/editor/preview-availability.ts +9 -5
  219. package/src/components/editors/DataEditor.tsx +65 -32
  220. package/src/components/fields/ImageField.tsx +2 -2
  221. package/src/components/media/AssetCard.tsx +12 -9
  222. package/src/components/media/AssetDetailSidebar.tsx +38 -25
  223. package/src/components/media/AssetUsageList.tsx +84 -0
  224. package/src/components/media/MediaLibrary.tsx +6 -4
  225. package/src/components/media/index.ts +1 -0
  226. package/src/components/pages/DataList.tsx +53 -29
  227. package/src/components/pages/MediaPage.tsx +14 -5
  228. package/src/components/pages/NoPagesYet.tsx +41 -0
  229. package/src/components/pages/PageList.tsx +40 -27
  230. package/src/components/sandbox/ContentSandbox.tsx +34 -5
  231. package/src/content-wrapper.ts +284 -0
  232. package/src/convert-pages.ts +110 -0
  233. package/src/frame/channel.ts +258 -0
  234. package/src/frame/editor-frame.ts +184 -0
  235. package/src/frame/media-cache.ts +157 -0
  236. package/src/frame/memory-router.ts +39 -0
  237. package/src/frame/parent-hello.ts +62 -0
  238. package/src/frame/protocol.ts +332 -0
  239. package/src/frame/rpc-content-client.ts +58 -0
  240. package/src/frame/save-shortcut.ts +25 -0
  241. package/src/hooks/index.ts +2 -0
  242. package/src/hooks/usePageConversion.ts +55 -0
  243. package/src/hooks/useUploadFlow.ts +44 -7
  244. package/src/host-style-probe.ts +104 -90
  245. package/src/index.ts +10 -2
  246. package/src/main-spa.tsx +2 -0
  247. package/src/main.css +2 -1
  248. package/src/media/prepare-image-upload.ts +118 -0
  249. package/src/media/use-asset-serve-src.ts +22 -0
  250. package/src/prose-surface-css.ts +3 -5
  251. package/src/site-styles.ts +4 -22
  252. package/src/ui-config.ts +74 -3
  253. package/src/utils/asset-url.ts +6 -0
  254. package/src/utils/data-entry-validation.ts +16 -0
  255. package/dist/ts/host-discovery.d.ts +0 -40
  256. package/dist/ts/host-discovery.d.ts.map +0 -1
  257. package/dist/ts/host-discovery.js +0 -39
  258. package/dist/ts/host-discovery.js.map +0 -1
  259. package/src/host-discovery.ts +0 -77
@@ -1,33 +1,122 @@
1
1
  /** Resolve unpublished draft images for site renderers without changing Puck's persisted data. */
2
- import { IMAGE_EXTENSION_MIME } from '@conloca/content-api-client';
3
- import type { Config, Field, Fields } from '@puckeditor/core';
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 { draftMediaRefPath, mediaRefPath } from '@conloca/content-api-client';
4
+ import type { Config, Field, Fields, PuckContext } from '@puckeditor/core';
4
5
  import { type ComponentType, createElement, isValidElement } from 'react';
5
- import { appendAssetUrlPath } from '../../utils/asset-url.js';
6
-
7
- function draftImageUrl(value: string, base: string): string {
8
- if (!value.startsWith('/assets/')) return value;
9
- const path = value.slice('/assets/'.length);
10
- if (/[\\?#]/.test(path)) return value;
11
- const segments = path.split('/');
12
- for (const part of segments) {
13
- if (
14
- part === '' ||
15
- part === '.' ||
16
- part === '..' ||
17
- [...part].some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127)
18
- ) {
19
- return value;
6
+ import { loadedMedia, mediaLoadState, useMediaCacheVersion } from '../../frame/media-cache.js';
7
+ import type { MediaLoadInput } from '../../frame/protocol.js';
8
+ import { getUIConfig } from '../../ui-config.js';
9
+
10
+ /** A 1×1 transparent GIF: what an image shows until its bytes arrive, and a draft address after a failed load. */
11
+ export const DRAFT_IMAGE_PLACEHOLDER = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
12
+
13
+ /** What a stored image link renders as; `undefined` keeps the stored value. */
14
+ type DraftImageResolver = (source: MediaLoadInput) => string | undefined;
15
+
16
+ /**
17
+ * The one decision of where an image value's bytes come from: a draft address, or a link under the image folder
18
+ * URL (`UIConfig.assetRefBase`, `/assets` by default). A link is URL text, so each segment is decoded once, as
19
+ * the site's static host does; an escape that would change the path (a bad one, or a decoded `/`, `\`, control
20
+ * character, `.` or `..`) makes it no image of ours.
21
+ */
22
+ export function mediaSourceOf(value: string): MediaLoadInput | undefined {
23
+ const draftPath = draftMediaRefPath(value);
24
+ if (draftPath !== undefined) return { route: 'serve', path: draftPath };
25
+ const linked = mediaRefPath(value, getUIConfig().assetRefBase ?? '/assets');
26
+ if (linked === undefined) return undefined;
27
+ const segments: string[] = [];
28
+ for (const segment of linked.split('/')) {
29
+ let part: string;
30
+ try {
31
+ part = decodeURIComponent(segment);
32
+ } catch {
33
+ return undefined;
20
34
  }
35
+ const control = Array.from(part, (char) => char.charCodeAt(0)).some((code) => code < 32 || code === 127);
36
+ if (part === '.' || part === '..' || /[/\\]/.test(part) || control) return undefined;
37
+ segments.push(part);
21
38
  }
22
- const filename = segments[segments.length - 1] ?? '';
23
- const dot = filename.lastIndexOf('.');
24
- if (dot <= 0) return value;
25
- const extension = filename.slice(dot + 1).toLowerCase();
26
- if (!Object.prototype.hasOwnProperty.call(IMAGE_EXTENSION_MIME, extension)) return value;
27
- const encoded = segments.map((part) =>
28
- encodeURIComponent(part).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`),
39
+ return { route: 'repo', path: segments.join('/') };
40
+ }
41
+
42
+ /** What an image value shows in the canvas once a loader is set: its bytes when loaded, else the placeholder. */
43
+ function shownImage({ route, path }: MediaLoadInput): string {
44
+ const state = mediaLoadState(route, path);
45
+ return state?.status === 'loaded' ? state.url : DRAFT_IMAGE_PLACEHOLDER;
46
+ }
47
+
48
+ /**
49
+ * What the MDX editor canvas shows for an image `src`: the loaded bytes of a draft or repository image, else the
50
+ * placeholder; any other `src` itself, as is a repository link when no loader is set (the site serves it).
51
+ * The URL is the media cache's own, which a media write or eviction revokes; mdxeditor resolves an image again only
52
+ * when its `src` changes or it remounts, so a mounted image keeps painting its decoded bytes from a revoked URL.
53
+ */
54
+ export async function draftImagePreview(src: string): Promise<string> {
55
+ const source = mediaSourceOf(src);
56
+ if (source === undefined) return src;
57
+ const url = (await loadedMedia(source.route, source.path))?.url;
58
+ if (url !== undefined) return url;
59
+ return source.route === 'repo' && !getUIConfig().loadMedia ? src : DRAFT_IMAGE_PLACEHOLDER;
60
+ }
61
+
62
+ function dataUrl(blob: Blob): Promise<string> {
63
+ // The package's lib target predates Promise.withResolvers.
64
+ return new Promise((resolve, reject) => {
65
+ const reader = new FileReader();
66
+ reader.addEventListener('load', () => {
67
+ if (typeof reader.result === 'string') resolve(reader.result);
68
+ else reject(new Error('The image could not be read as a data URL.'));
69
+ });
70
+ reader.addEventListener('error', () => {
71
+ reject(reader.error ?? new Error('The image could not be read.'));
72
+ });
73
+ reader.readAsDataURL(blob);
74
+ });
75
+ }
76
+
77
+ // Keyed by the cached Blob, so a cleared or evicted cache entry frees its string too.
78
+ const dataUrls = new WeakMap<Blob, Promise<string>>();
79
+
80
+ async function draftImageDataUrl({ route, path }: MediaLoadInput): Promise<string | undefined> {
81
+ const media = await loadedMedia(route, path);
82
+ if (!media) return undefined;
83
+ let url = dataUrls.get(media.blob);
84
+ if (!url) {
85
+ url = dataUrl(media.blob);
86
+ dataUrls.set(media.blob, url);
87
+ }
88
+ return url;
89
+ }
90
+
91
+ /** How long a preview update waits for its draft images before it goes without the ones still loading. */
92
+ export const DRAFT_IMAGE_WAIT_MS = 5000;
93
+
94
+ /**
95
+ * Data URLs of the images compiled MDX or sanitized HTML names, keyed by their stored value, for the opaque-origin
96
+ * preview frame, which cannot read this page's blob URLs: an `src`, an HTML `src="…"` attribute or an MDX
97
+ * component's prop. Images that fail to load, or are still loading after `DRAFT_IMAGE_WAIT_MS`, show the placeholder;
98
+ * without a loader a repository link is left out, so the frame loads it from the site.
99
+ */
100
+ export async function draftImageData(code: string): Promise<Record<string, string>> {
101
+ // shortcut: finds values only as plain double-quoted literals, the form the MDX compiler and the HTML sanitizer emit; widen if an escaped path must preview.
102
+ const literals = code.match(/"[^"\\]*"/g);
103
+ const srcs = new Set(literals?.map((literal) => literal.slice(1, -1)));
104
+ const images: Record<string, string> = {};
105
+ let timer: number | undefined;
106
+ const expired = new Promise<undefined>((resolve) => {
107
+ timer = window.setTimeout(resolve, DRAFT_IMAGE_WAIT_MS, undefined);
108
+ });
109
+ const loader = getUIConfig().loadMedia !== undefined;
110
+ await Promise.all(
111
+ [...srcs].map(async (src) => {
112
+ const source = mediaSourceOf(src);
113
+ if (source === undefined || (source.route === 'repo' && !loader)) return;
114
+ const url = await Promise.race([draftImageDataUrl(source).catch(() => undefined), expired]);
115
+ images[src] = url ?? DRAFT_IMAGE_PLACEHOLDER;
116
+ }),
29
117
  );
30
- return appendAssetUrlPath(base, encoded.join('/'));
118
+ window.clearTimeout(timer);
119
+ return images;
31
120
  }
32
121
 
33
122
  function isRecord(value: unknown): value is Record<string, unknown> {
@@ -36,23 +125,39 @@ function isRecord(value: unknown): value is Record<string, unknown> {
36
125
  return prototype === Object.prototype || prototype === null;
37
126
  }
38
127
 
39
- function fieldValue(value: unknown, field: Field, base: string): unknown {
128
+ /** What the canvas shows for a string: what `resolve` decides when `mediaSourceOf` reads it as an image, else itself. */
129
+ function imageValue(value: string, resolve: DraftImageResolver): string {
130
+ const source = mediaSourceOf(value);
131
+ return source === undefined ? value : (resolve(source) ?? value);
132
+ }
133
+
134
+ /** `items` mapped by `map`, or `items` itself when no item changed. */
135
+ function mapped(items: unknown[], map: (item: unknown) => unknown): unknown[] {
136
+ const next = items.map(map);
137
+ return next.some((item, index) => item !== items[index]) ? next : items;
138
+ }
139
+
140
+ function fieldValue(value: unknown, field: Field, resolve: DraftImageResolver): unknown {
40
141
  if (field.type === 'slot') return value;
41
- if (typeof value === 'string') return draftImageUrl(value, base);
42
- if (field.type === 'object' && isRecord(value)) return fieldProps(value, field.objectFields, base);
142
+ if (typeof value === 'string') return imageValue(value, resolve);
143
+ if (field.type === 'object' && isRecord(value)) return fieldProps(value, field.objectFields, resolve);
43
144
  if (field.type === 'array' && Array.isArray(value)) {
44
- const next = value.map((item: unknown) => (isRecord(item) ? fieldProps(item, field.arrayFields, base) : item));
45
- return next.some((item, index) => item !== value[index]) ? next : value;
145
+ return mapped(value, (item) => (isRecord(item) ? fieldProps(item, field.arrayFields, resolve) : item));
46
146
  }
47
147
  return value;
48
148
  }
49
149
 
50
- function fieldProps(props: Record<string, unknown>, fields: Fields | undefined, base: string): Record<string, unknown> {
150
+ function fieldProps(
151
+ props: Record<string, unknown>,
152
+ fields: Fields | undefined,
153
+ resolve: DraftImageResolver,
154
+ ): Record<string, unknown> {
51
155
  let next = props;
52
156
  for (const [name, field] of Object.entries(fields ?? {})) {
53
157
  if (name === 'puck' || name === 'children' || name === 'editMode') continue;
158
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: Object.hasOwn is past this package's ES2020 lib.
54
159
  if (field === undefined || !Object.prototype.hasOwnProperty.call(props, name)) continue;
55
- const value = fieldValue(props[name], field, base);
160
+ const value = fieldValue(props[name], field, resolve);
56
161
  if (value === props[name]) continue;
57
162
  if (next === props) next = { ...props };
58
163
  next[name] = value;
@@ -60,9 +165,50 @@ function fieldProps(props: Record<string, unknown>, fields: Fields | undefined,
60
165
  return next;
61
166
  }
62
167
 
63
- /** Static declared fields bound traversal; dynamic field resolvers and runtime contexts remain untouched. */
64
- export function withDraftMediaRendering(config: Config, base: string | undefined): Config {
65
- if (base === undefined) return config;
168
+ /** Page settings with every string in them read as a possible image, as the branch save scans them for uploads. */
169
+ function settingsValue(value: unknown, resolve: DraftImageResolver): unknown {
170
+ if (typeof value === 'string') return imageValue(value, resolve);
171
+ if (Array.isArray(value)) return mapped(value, (item) => settingsValue(item, resolve));
172
+ if (!isRecord(value)) return value;
173
+ let next = value;
174
+ for (const [name, item] of Object.entries(value)) {
175
+ const shown = settingsValue(item, resolve);
176
+ if (shown === item) continue;
177
+ if (next === value) next = { ...value };
178
+ next[name] = shown;
179
+ }
180
+ return next;
181
+ }
182
+
183
+ /** The page with its settings' images shown; everything else on it, its Dates included, as given. */
184
+ function pageWithImages(page: unknown, resolve: DraftImageResolver): unknown {
185
+ if (!isRecord(page)) return page;
186
+ const meta = settingsValue(page.meta, resolve);
187
+ return meta === page.meta ? page : { ...page, meta };
188
+ }
189
+
190
+ /**
191
+ * `puck` with the images in the page settings its metadata carries, the page's own and each page its route binds,
192
+ * shown as `resolve` decides: a block such as an article header shows a page setting without a field of its own.
193
+ */
194
+ function puckWithImages(puck: PuckContext, resolve: DraftImageResolver): PuckContext {
195
+ const { metadata } = puck;
196
+ if (!isRecord(metadata)) return puck;
197
+ const page = pageWithImages(metadata.page, resolve);
198
+ const pages = Array.isArray(metadata.pages)
199
+ ? mapped(metadata.pages, (item) => pageWithImages(item, resolve))
200
+ : metadata.pages;
201
+ if (page === metadata.page && pages === metadata.pages) return puck;
202
+ return { ...puck, metadata: { ...metadata, page, pages } };
203
+ }
204
+
205
+ /**
206
+ * Static declared fields bound traversal, plus the page settings in `puck.metadata`; dynamic field resolvers and
207
+ * other runtime contexts remain untouched. Every image value `mediaSourceOf` accepts shows what `shownImage`
208
+ * decides; `config` itself without `UIConfig.loadMedia`, so every value renders as stored.
209
+ */
210
+ export function withDraftMediaRendering(config: Config): Config {
211
+ if (getUIConfig().loadMedia === undefined) return config;
66
212
  const root = config.root;
67
213
  const rootRender = root?.render;
68
214
  return {
@@ -73,11 +219,17 @@ export function withDraftMediaRendering(config: Config, base: string | undefined
73
219
  ...root,
74
220
  // Rendered as an element so memo/forwardRef roots keep working. Root props are a
75
221
  // children/no-children union, so name the component's own prop type for createElement.
76
- render: (props: Parameters<typeof rootRender>[0]) =>
77
- createElement(rootRender as ComponentType<typeof props>, {
222
+ render: (props: Parameters<typeof rootRender>[0]) => {
223
+ // Re-render when a loaded image arrives.
224
+ useMediaCacheVersion();
225
+ // SAFETY: `props` is typed from `rootRender` itself; the cast only picks one member of Puck's root union.
226
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- see the SAFETY note.
227
+ return createElement(rootRender as ComponentType<typeof props>, {
78
228
  ...props,
79
- ...fieldProps(props, root.fields, base),
80
- }),
229
+ ...fieldProps(props, root.fields, shownImage),
230
+ puck: puckWithImages(props.puck, shownImage),
231
+ });
232
+ },
81
233
  },
82
234
  }
83
235
  : {}),
@@ -86,8 +238,14 @@ export function withDraftMediaRendering(config: Config, base: string | undefined
86
238
  name,
87
239
  {
88
240
  ...component,
89
- render: (props: Parameters<typeof component.render>[0]) =>
90
- createElement(component.render, { ...props, ...fieldProps(props, component.fields, base) }),
241
+ render: (props: Parameters<typeof component.render>[0]) => {
242
+ useMediaCacheVersion();
243
+ return createElement(component.render, {
244
+ ...props,
245
+ ...fieldProps(props, component.fields, shownImage),
246
+ puck: puckWithImages(props.puck, shownImage),
247
+ });
248
+ },
91
249
  },
92
250
  ]),
93
251
  ),
@@ -8,13 +8,17 @@
8
8
  * `@lexical`.
9
9
  */
10
10
 
11
- /** Toast heading when the site cannot show a preview. */
12
- export const PREVIEW_UNAVAILABLE_TITLE = 'Preview is not available for this site';
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. States the fact about the site, reassures that nothing they
17
- * did is broken, and never implies user error.
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 site does not serve draft previews, so there is no preview page to open. Your draft is still saved, and publishing and the live site are unaffected.';
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.';
22
+
23
+ /** What the Preview control on a docs (MDX) page says until docs pages render drafts. */
24
+ export const DOCS_PREVIEW_UNAVAILABLE = "Full-page preview isn't available for docs pages yet";
@@ -1,18 +1,27 @@
1
- import { getContentAPIClient, useLocalizedContent, useUpdateLocalized } from '@conloca/content-api-client';
1
+ import {
2
+ type DataCollectionEntry,
3
+ getContentAPIClient,
4
+ useLocalizedContent,
5
+ useUpdateLocalized,
6
+ } from '@conloca/content-api-client';
2
7
  import { AlertCircle, Code2, Loader2, Save } from 'lucide-react';
3
8
  import { useEffect, useRef, useState } from 'react';
4
- import type { HostSchema } from '../../host-schema.js';
9
+ import { type HostSchema, isHostSchema } from '../../host-schema.js';
5
10
  import { contentFailureMessage } from '../../utils/content-failure.js';
11
+ import { validateEntryData } from '../../utils/data-entry-validation.js';
6
12
  import { UnsavedChangesDialog } from '../dialogs/UnsavedChangesDialog.js';
7
13
  import { LocaleSelector } from '../editor/LocaleSelector.js';
8
14
  import { SchemaForm } from '../forms/SchemaForm.js';
9
15
 
10
16
  interface DataEditorProps {
17
+ /** The entry's id, or '' for a new entry, which `create` writes on the first Save. */
11
18
  id: string;
12
19
  collection: string;
13
20
  initialLocale: string;
14
21
  existingLocales: string[];
15
22
  schema: HostSchema | null;
23
+ /** Writes a new entry holding `data`; resolves true once it is created. */
24
+ create?: (data: DataCollectionEntry['data']) => Promise<boolean>;
16
25
  onSave?: () => void;
17
26
  onCancel?: () => void;
18
27
  }
@@ -28,6 +37,7 @@ export function DataEditor({
28
37
  initialLocale,
29
38
  existingLocales,
30
39
  schema,
40
+ create,
31
41
  onSave,
32
42
  onCancel,
33
43
  }: DataEditorProps) {
@@ -47,6 +57,8 @@ export function DataEditor({
47
57
  const [values, setValues] = useState<Record<string, unknown>>({});
48
58
  const [hasChanges, setHasChanges] = useState(false);
49
59
  const [saveError, setSaveError] = useState<string | null>(null);
60
+ const [saving, setSaving] = useState(false);
61
+ const isNew = id === '';
50
62
 
51
63
  // Initialize form values and etag when entry loads
52
64
  useEffect(() => {
@@ -67,28 +79,46 @@ export function DataEditor({
67
79
  setHasChanges(true);
68
80
  };
69
81
 
70
- // Save handler
71
- const handleSave = async () => {
72
- setSaveError(null);
82
+ /**
83
+ * Writes the form once it passes the schema, as the build validates the entry; resolves whether it
84
+ * was written.
85
+ */
86
+ const handleSave = async (): Promise<boolean> => {
87
+ if (saving || !isHostSchema(schema)) return false;
88
+ const invalid = validateEntryData(schema, values);
89
+ setSaveError(invalid);
90
+ if (invalid) return false;
73
91
 
74
- const result = await updateLocalized.mutateAsync({
75
- id,
76
- locale: currentLocale,
77
- data: {
78
- content: { data: values },
79
- },
80
- etag: currentEtag,
81
- });
92
+ setSaving(true);
93
+ try {
94
+ if (isNew) {
95
+ const created = (await create?.(values)) ?? false;
96
+ if (created) onSave?.();
97
+ return created;
98
+ }
82
99
 
83
- if (result.success) {
100
+ const result = await updateLocalized.mutateAsync({
101
+ id,
102
+ locale: currentLocale,
103
+ data: {
104
+ content: { data: values },
105
+ },
106
+ etag: currentEtag,
107
+ });
108
+
109
+ if (!result.success) {
110
+ setSaveError(contentFailureMessage(result, 'Failed to save data'));
111
+ return false;
112
+ }
84
113
  setHasChanges(false);
85
114
  savedValuesRef.current = values;
86
115
  if (result.etag) {
87
116
  setCurrentEtag(result.etag);
88
117
  }
89
118
  onSave?.();
90
- } else {
91
- setSaveError(contentFailureMessage(result, 'Failed to save data'));
119
+ return true;
120
+ } finally {
121
+ setSaving(false);
92
122
  }
93
123
  };
94
124
 
@@ -131,11 +161,12 @@ export function DataEditor({
131
161
  const handleUnsavedDialogSave = async () => {
132
162
  if (!pendingLocaleSwitch) return;
133
163
 
134
- // Save current content first
164
+ // Save current content first; a refused save keeps the locale and the edits, with the reason shown.
135
165
  try {
136
- await handleSave();
166
+ const saved = await handleSave();
137
167
  setShowUnsavedDialog(false);
138
- await switchLocale(pendingLocaleSwitch);
168
+ if (saved) await switchLocale(pendingLocaleSwitch);
169
+ else setPendingLocaleSwitch(null);
139
170
  } catch (error) {
140
171
  // Don't switch if save failed
141
172
  console.error('Failed to save before locale switch:', error);
@@ -196,15 +227,17 @@ export function DataEditor({
196
227
  return (
197
228
  <>
198
229
  <div className="space-y-6">
199
- {/* Locale selector header - only show existing locales */}
200
- <div className="flex items-center gap-3 pb-4 border-b border-grey-09 dark:border-grey-03">
201
- <span className="text-sm text-grey-04 dark:text-grey-07">Locale:</span>
202
- <LocaleSelector
203
- currentLocale={currentLocale}
204
- availableLocales={existingLocales}
205
- onChange={handleLocaleChange}
206
- />
207
- </div>
230
+ {/* Locale selector header - only show existing locales; a new entry has one, the one it is created in */}
231
+ {!isNew && (
232
+ <div className="flex items-center gap-3 pb-4 border-b border-grey-09 dark:border-grey-03">
233
+ <span className="text-sm text-grey-04 dark:text-grey-07">Locale:</span>
234
+ <LocaleSelector
235
+ currentLocale={currentLocale}
236
+ availableLocales={existingLocales}
237
+ onChange={handleLocaleChange}
238
+ />
239
+ </div>
240
+ )}
208
241
 
209
242
  <SchemaForm schema={schema} values={values} onChange={handleChange} />
210
243
 
@@ -220,7 +253,7 @@ export function DataEditor({
220
253
  {hasChanges ? (
221
254
  <span className="text-yellow-05 dark:text-yellow-07">Unsaved changes</span>
222
255
  ) : (
223
- <span>No changes</span>
256
+ <span>{isNew ? 'Not saved yet' : 'No changes'}</span>
224
257
  )}
225
258
  </div>
226
259
 
@@ -229,7 +262,7 @@ export function DataEditor({
229
262
  <button
230
263
  type="button"
231
264
  onClick={onCancel}
232
- disabled={updateLocalized.isPending}
265
+ disabled={saving}
233
266
  className="px-4 py-2 border border-grey-09 dark:border-grey-03 rounded-md hover:bg-grey-11 dark:hover:bg-grey-03 transition-colors disabled:opacity-50"
234
267
  >
235
268
  Cancel
@@ -238,10 +271,10 @@ export function DataEditor({
238
271
  <button
239
272
  type="button"
240
273
  onClick={handleSave}
241
- disabled={!hasChanges || updateLocalized.isPending}
274
+ disabled={(!hasChanges && !isNew) || saving || !isHostSchema(schema)}
242
275
  className="px-4 py-2 bg-azure-04 text-white rounded-md hover:bg-azure-03 transition-colors disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center gap-2"
243
276
  >
244
- {updateLocalized.isPending ? (
277
+ {saving ? (
245
278
  <>
246
279
  <Loader2 className="h-4 w-4 animate-spin" />
247
280
  Saving...
@@ -1,7 +1,7 @@
1
1
  import { FolderOpen } from 'lucide-react';
2
2
  import { useEffect, useState } from 'react';
3
3
  import type { AssetEntry } from '../../hooks/index.js';
4
- import { buildAssetRef } from '../../utils/asset-url.js';
4
+ import { assetRefOf } from '../../utils/asset-url.js';
5
5
  import { ImagePicker } from '../media/ImagePicker.js';
6
6
  import { Input } from '../ui/index.js';
7
7
 
@@ -33,7 +33,7 @@ export function ImageFieldRender({ value, onChange, inputId }: ImageFieldRenderP
33
33
  }, [value]);
34
34
 
35
35
  const handleSelect = (asset: AssetEntry) => {
36
- const assetPath = buildAssetRef(asset.folder, asset.filename);
36
+ const assetPath = assetRefOf(asset);
37
37
  onChange(assetPath);
38
38
  setLocalValue(assetPath);
39
39
  setModalOpen(false);
@@ -2,8 +2,8 @@ import type { AssetEntry } from '../../hooks/index.js';
2
2
  import { AssetScopePill } from '../../media/AssetScopePill.js';
3
3
  import { MediaIssueBadge } from '../../media/MediaIssueBadge.js';
4
4
  import { useAssetScope } from '../../media/use-asset-scope.js';
5
+ import { useAssetServeSrc } from '../../media/use-asset-serve-src.js';
5
6
  import { useMediaIssue } from '../../media/use-media-issue.js';
6
- import { buildAssetServeUrl } from '../../utils/asset-url.js';
7
7
  import { cn } from '../../utils/cn.js';
8
8
 
9
9
  interface AssetCardProps {
@@ -33,6 +33,7 @@ export function AssetCard({
33
33
  // reports. Returns null when the host has no opinion — surfaces
34
34
  // degrade exactly like the scope pill in standalone mounts.
35
35
  const issue = useMediaIssue(asset.filename, asset.folder);
36
+ const src = useAssetServeSrc(assetsBasePath, asset);
36
37
 
37
38
  const handleClick = () => {
38
39
  onClick?.();
@@ -55,12 +56,14 @@ export function AssetCard({
55
56
  >
56
57
  {/* Thumbnail */}
57
58
  <div className="relative aspect-square bg-grey-11 dark:bg-grey-03 flex items-center justify-center overflow-hidden">
58
- <img
59
- src={buildAssetServeUrl(assetsBasePath, asset.folder, asset.filename)}
60
- alt={asset.alt || asset.originalName}
61
- className="w-full h-full object-contain"
62
- loading="lazy"
63
- />
59
+ {src ? (
60
+ <img
61
+ src={src}
62
+ alt={asset.alt || asset.originalName}
63
+ className="w-full h-full object-contain"
64
+ loading="lazy"
65
+ />
66
+ ) : null}
64
67
  {/* Scope pill — renders only when a host bridge supplied a
65
68
  non-null scope. Top-left corner so it sits above the
66
69
  asset's own visual focus point without clipping the
@@ -75,8 +78,8 @@ export function AssetCard({
75
78
 
76
79
  {/* Info - filename only */}
77
80
  <div className="px-2 py-1.5">
78
- <p className="text-xs text-grey-01 dark:text-grey-12 truncate" title={asset.originalName}>
79
- {asset.originalName}
81
+ <p className="text-xs text-grey-01 dark:text-grey-12 truncate" title={asset.filename}>
82
+ {asset.filename}
80
83
  </p>
81
84
  </div>
82
85
  </button>