@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.
- package/README.md +26 -0
- package/dist/spa/main.css +229 -1
- package/dist/ts/.build-inputs.json +187 -0
- package/dist/ts/App.d.ts +5 -1
- package/dist/ts/App.d.ts.map +1 -1
- package/dist/ts/App.js +13 -6
- 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 +2 -1
- package/dist/ts/components/CMSDashboard.js.map +1 -1
- package/dist/ts/components/cards/DataEntryCard.d.ts +1 -1
- package/dist/ts/components/cards/DataEntryCard.d.ts.map +1 -1
- package/dist/ts/components/cards/SectionCard.d.ts +1 -1
- package/dist/ts/components/cards/SectionCard.d.ts.map +1 -1
- package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts +6 -1
- package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/DeleteConfirmDialog.js +2 -2
- package/dist/ts/components/dialogs/DeleteConfirmDialog.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/MakePagesEditableDialog.d.ts +10 -0
- package/dist/ts/components/dialogs/MakePagesEditableDialog.d.ts.map +1 -0
- package/dist/ts/components/dialogs/MakePagesEditableDialog.js +35 -0
- package/dist/ts/components/dialogs/MakePagesEditableDialog.js.map +1 -0
- package/dist/ts/components/dialogs/MoveFolderDialog.d.ts +6 -1
- package/dist/ts/components/dialogs/MoveFolderDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/MoveFolderDialog.js +3 -3
- package/dist/ts/components/dialogs/MoveFolderDialog.js.map +1 -1
- package/dist/ts/components/editor/CMSMDXEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/CMSMDXEditor.js +53 -62
- package/dist/ts/components/editor/CMSMDXEditor.js.map +1 -1
- package/dist/ts/components/editor/DocsPreviewButton.d.ts +6 -0
- package/dist/ts/components/editor/DocsPreviewButton.d.ts.map +1 -0
- package/dist/ts/components/editor/DocsPreviewButton.js +15 -0
- package/dist/ts/components/editor/DocsPreviewButton.js.map +1 -0
- package/dist/ts/components/editor/ImagePickerDialog.js +2 -2
- package/dist/ts/components/editor/ImagePickerDialog.js.map +1 -1
- package/dist/ts/components/editor/LocaleSelector.d.ts +1 -1
- package/dist/ts/components/editor/LocaleSelector.d.ts.map +1 -1
- package/dist/ts/components/editor/MdxPageEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/MdxPageEditor.js +2 -1
- package/dist/ts/components/editor/MdxPageEditor.js.map +1 -1
- package/dist/ts/components/editor/PageEditor.d.ts +1 -1
- package/dist/ts/components/editor/PageEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditor.js +47 -19
- package/dist/ts/components/editor/PageEditor.js.map +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.d.ts +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.js +31 -5
- package/dist/ts/components/editor/PageEditorWrapper.js.map +1 -1
- package/dist/ts/components/editor/draft-media-config.d.ts +32 -2
- package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -1
- package/dist/ts/components/editor/draft-media-config.js +194 -39
- package/dist/ts/components/editor/draft-media-config.js.map +1 -1
- package/dist/ts/components/editor/page-setting-targets.d.ts +1 -1
- package/dist/ts/components/editor/page-setting-targets.d.ts.map +1 -1
- package/dist/ts/components/editor/preview-availability.d.ts +8 -5
- package/dist/ts/components/editor/preview-availability.d.ts.map +1 -1
- package/dist/ts/components/editor/preview-availability.js +8 -5
- package/dist/ts/components/editor/preview-availability.js.map +1 -1
- package/dist/ts/components/editors/DataEditor.d.ts +6 -2
- package/dist/ts/components/editors/DataEditor.d.ts.map +1 -1
- package/dist/ts/components/editors/DataEditor.js +46 -19
- package/dist/ts/components/editors/DataEditor.js.map +1 -1
- package/dist/ts/components/fields/ImageField.js +2 -2
- package/dist/ts/components/fields/ImageField.js.map +1 -1
- package/dist/ts/components/media/AssetCard.d.ts +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 +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.js +7 -2
- package/dist/ts/components/media/AssetDetailSidebar.js.map +1 -1
- package/dist/ts/components/media/AssetUsageList.d.ts +13 -0
- package/dist/ts/components/media/AssetUsageList.d.ts.map +1 -0
- package/dist/ts/components/media/AssetUsageList.js +35 -0
- package/dist/ts/components/media/AssetUsageList.js.map +1 -0
- package/dist/ts/components/media/MediaLibrary.d.ts +1 -1
- package/dist/ts/components/media/MediaLibrary.d.ts.map +1 -1
- package/dist/ts/components/media/MediaLibrary.js +6 -4
- package/dist/ts/components/media/MediaLibrary.js.map +1 -1
- package/dist/ts/components/media/MediaToolbar.d.ts +1 -1
- package/dist/ts/components/media/MediaToolbar.d.ts.map +1 -1
- package/dist/ts/components/media/index.d.ts +1 -0
- package/dist/ts/components/media/index.d.ts.map +1 -1
- package/dist/ts/components/media/index.js +1 -0
- package/dist/ts/components/media/index.js.map +1 -1
- package/dist/ts/components/pages/DataList.d.ts.map +1 -1
- package/dist/ts/components/pages/DataList.js +48 -31
- package/dist/ts/components/pages/DataList.js.map +1 -1
- package/dist/ts/components/pages/MediaPage.d.ts.map +1 -1
- package/dist/ts/components/pages/MediaPage.js +7 -5
- package/dist/ts/components/pages/MediaPage.js.map +1 -1
- package/dist/ts/components/pages/NoPagesYet.d.ts +5 -0
- package/dist/ts/components/pages/NoPagesYet.d.ts.map +1 -0
- package/dist/ts/components/pages/NoPagesYet.js +16 -0
- package/dist/ts/components/pages/NoPagesYet.js.map +1 -0
- package/dist/ts/components/pages/PageList.d.ts.map +1 -1
- package/dist/ts/components/pages/PageList.js +19 -8
- package/dist/ts/components/pages/PageList.js.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.d.ts +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/components/ui/IconButton.d.ts +1 -1
- package/dist/ts/components/ui/IconButton.d.ts.map +1 -1
- 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/convert-pages.d.ts +30 -0
- package/dist/ts/convert-pages.d.ts.map +1 -0
- package/dist/ts/convert-pages.js +85 -0
- package/dist/ts/convert-pages.js.map +1 -0
- package/dist/ts/data-schemas.d.ts.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 +159 -0
- package/dist/ts/frame/editor-frame.js.map +1 -0
- package/dist/ts/frame/media-cache.d.ts +37 -0
- package/dist/ts/frame/media-cache.d.ts.map +1 -0
- package/dist/ts/frame/media-cache.js +138 -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 +209 -0
- package/dist/ts/frame/protocol.d.ts.map +1 -0
- package/dist/ts/frame/protocol.js +200 -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/hooks/index.d.ts +2 -0
- package/dist/ts/hooks/index.d.ts.map +1 -1
- package/dist/ts/hooks/index.js +1 -0
- package/dist/ts/hooks/index.js.map +1 -1
- package/dist/ts/hooks/usePageConversion.d.ts +11 -0
- package/dist/ts/hooks/usePageConversion.d.ts.map +1 -0
- package/dist/ts/hooks/usePageConversion.js +47 -0
- package/dist/ts/hooks/usePageConversion.js.map +1 -0
- package/dist/ts/hooks/useUploadFlow.d.ts.map +1 -1
- package/dist/ts/hooks/useUploadFlow.js +41 -7
- package/dist/ts/hooks/useUploadFlow.js.map +1 -1
- 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 +86 -68
- package/dist/ts/host-style-probe.js.map +1 -1
- package/dist/ts/index.d.ts +6 -2
- package/dist/ts/index.d.ts.map +1 -1
- package/dist/ts/index.js +9 -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/media/prepare-image-upload.d.ts +19 -0
- package/dist/ts/media/prepare-image-upload.d.ts.map +1 -0
- package/dist/ts/media/prepare-image-upload.js +93 -0
- package/dist/ts/media/prepare-image-upload.js.map +1 -0
- package/dist/ts/media/use-asset-serve-src.d.ts +7 -0
- package/dist/ts/media/use-asset-serve-src.d.ts.map +1 -0
- package/dist/ts/media/use-asset-serve-src.js +17 -0
- package/dist/ts/media/use-asset-serve-src.js.map +1 -0
- package/dist/ts/page-schemas.d.ts.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/puck-config.d.ts.map +1 -1
- package/dist/ts/selected-block.d.ts.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 +62 -3
- package/dist/ts/ui-config.d.ts.map +1 -1
- package/dist/ts/ui-config.js +18 -0
- package/dist/ts/ui-config.js.map +1 -1
- package/dist/ts/utils/asset-url.d.ts +3 -0
- package/dist/ts/utils/asset-url.d.ts.map +1 -1
- package/dist/ts/utils/asset-url.js +4 -0
- package/dist/ts/utils/asset-url.js.map +1 -1
- package/dist/ts/utils/data-entry-validation.d.ts +11 -0
- package/dist/ts/utils/data-entry-validation.d.ts.map +1 -0
- package/dist/ts/utils/data-entry-validation.js +15 -0
- package/dist/ts/utils/data-entry-validation.js.map +1 -0
- package/package.json +20 -11
- package/src/App.tsx +22 -10
- package/src/CmsSpaApp.tsx +5 -1
- package/src/components/CMSDashboard.tsx +16 -8
- package/src/components/dialogs/DeleteConfirmDialog.tsx +14 -1
- package/src/components/dialogs/MakePagesEditableDialog.tsx +112 -0
- package/src/components/dialogs/MoveFolderDialog.tsx +9 -2
- package/src/components/editor/CMSMDXEditor.tsx +56 -76
- package/src/components/editor/DocsPreviewButton.tsx +25 -0
- package/src/components/editor/ImagePickerDialog.tsx +2 -2
- package/src/components/editor/MdxPageEditor.tsx +2 -0
- package/src/components/editor/PageEditor.tsx +46 -22
- package/src/components/editor/PageEditorWrapper.tsx +34 -5
- package/src/components/editor/draft-media-config.ts +199 -41
- package/src/components/editor/preview-availability.ts +9 -5
- package/src/components/editors/DataEditor.tsx +65 -32
- package/src/components/fields/ImageField.tsx +2 -2
- package/src/components/media/AssetCard.tsx +12 -9
- package/src/components/media/AssetDetailSidebar.tsx +38 -25
- package/src/components/media/AssetUsageList.tsx +84 -0
- package/src/components/media/MediaLibrary.tsx +6 -4
- package/src/components/media/index.ts +1 -0
- package/src/components/pages/DataList.tsx +53 -29
- package/src/components/pages/MediaPage.tsx +14 -5
- package/src/components/pages/NoPagesYet.tsx +41 -0
- package/src/components/pages/PageList.tsx +40 -27
- package/src/components/sandbox/ContentSandbox.tsx +34 -5
- package/src/content-wrapper.ts +284 -0
- package/src/convert-pages.ts +110 -0
- package/src/frame/channel.ts +258 -0
- package/src/frame/editor-frame.ts +184 -0
- package/src/frame/media-cache.ts +157 -0
- package/src/frame/memory-router.ts +39 -0
- package/src/frame/parent-hello.ts +62 -0
- package/src/frame/protocol.ts +332 -0
- package/src/frame/rpc-content-client.ts +58 -0
- package/src/frame/save-shortcut.ts +25 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/usePageConversion.ts +55 -0
- package/src/hooks/useUploadFlow.ts +44 -7
- package/src/host-style-probe.ts +104 -90
- package/src/index.ts +10 -2
- package/src/main-spa.tsx +2 -0
- package/src/main.css +2 -1
- package/src/media/prepare-image-upload.ts +118 -0
- package/src/media/use-asset-serve-src.ts +22 -0
- package/src/prose-surface-css.ts +3 -5
- package/src/site-styles.ts +4 -22
- package/src/ui-config.ts +74 -3
- package/src/utils/asset-url.ts +6 -0
- package/src/utils/data-entry-validation.ts +16 -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/src/host-discovery.ts +0 -77
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Accepted,
|
|
3
|
+
EDITOR_HOST_PROTOCOL,
|
|
4
|
+
type EditorHostPageData,
|
|
5
|
+
HELLO_TYPE,
|
|
6
|
+
type Hello,
|
|
7
|
+
type Nack,
|
|
8
|
+
} from './protocol.js';
|
|
9
|
+
|
|
10
|
+
export type HelloWindow = Pick<Window, 'parent' | 'top' | 'addEventListener' | 'removeEventListener'>;
|
|
11
|
+
|
|
12
|
+
function isHello(data: unknown): data is Hello {
|
|
13
|
+
if (typeof data !== 'object' || data === null || !('type' in data) || !('protocols' in data)) return false;
|
|
14
|
+
const { type, protocols } = data;
|
|
15
|
+
if (type !== HELLO_TYPE || typeof protocols !== 'object' || protocols === null) return false;
|
|
16
|
+
if (!('min' in protocols) || !('max' in protocols)) return false;
|
|
17
|
+
return Number.isInteger(protocols.min) && Number.isInteger(protocols.max);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Resolves with the port of the first hello from this window's parent whose origin `acceptsOrigin`
|
|
22
|
+
* allows and whose range includes the page's protocol; the answer carries the page's `repo` and
|
|
23
|
+
* `siteId` so the parent can refuse a page that is not the one it framed. A wrong origin is refused
|
|
24
|
+
* and the page keeps listening; a protocol mismatch is refused and rejects. The port is neither
|
|
25
|
+
* started nor given a handler, so messages the parent sends next stay queued for the channel.
|
|
26
|
+
*/
|
|
27
|
+
export function awaitParentHello(
|
|
28
|
+
win: HelloWindow,
|
|
29
|
+
acceptsOrigin: (origin: string) => boolean,
|
|
30
|
+
page: Pick<EditorHostPageData, 'repo' | 'siteId'>,
|
|
31
|
+
): Promise<MessagePort> {
|
|
32
|
+
// Executor form: the package's production lib predates Promise.withResolvers.
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const onMessage = (event: MessageEvent) => {
|
|
35
|
+
// Only the top-level parent: a page nested deeper would hand its channel to a frame in between.
|
|
36
|
+
if (!isHello(event.data) || event.source !== win.parent || win.parent !== win.top) return;
|
|
37
|
+
if (event.ports.length !== 1) return;
|
|
38
|
+
const [port] = event.ports;
|
|
39
|
+
if (!acceptsOrigin(event.origin)) {
|
|
40
|
+
port.postMessage({ kind: 'nack', code: 'origin-rejected' } satisfies Nack);
|
|
41
|
+
port.close();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
win.removeEventListener('message', onMessage);
|
|
45
|
+
const { min, max } = event.data.protocols;
|
|
46
|
+
if (EDITOR_HOST_PROTOCOL < min || EDITOR_HOST_PROTOCOL > max) {
|
|
47
|
+
port.postMessage({ kind: 'nack', code: 'unsupported-protocol', page: EDITOR_HOST_PROTOCOL } satisfies Nack);
|
|
48
|
+
port.close();
|
|
49
|
+
reject(new Error('unsupported-protocol'));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
port.postMessage({
|
|
53
|
+
kind: 'accepted',
|
|
54
|
+
protocol: EDITOR_HOST_PROTOCOL,
|
|
55
|
+
repo: page.repo,
|
|
56
|
+
siteId: page.siteId,
|
|
57
|
+
} satisfies Accepted);
|
|
58
|
+
resolve(port);
|
|
59
|
+
};
|
|
60
|
+
win.addEventListener('message', onMessage);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract between the editor page and the parent frame around it: the
|
|
3
|
+
* messages on their shared port, the methods either side may call, and the
|
|
4
|
+
* parent origins the page accepts. Both sides import it from here.
|
|
5
|
+
*/
|
|
6
|
+
import type { ContentAPIClient } from '@conloca/content-api-client';
|
|
7
|
+
import { type PageConversionFacts, readPageConversionFacts } from '../convert-pages.js';
|
|
8
|
+
import type { ThemePreference } from '../theme.js';
|
|
9
|
+
import type { EditorWriteRefusal, PreviewCapability } from '../ui-config.js';
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
EDITOR_HOST_PROTOCOL,
|
|
13
|
+
EDITOR_HOST_PROTOCOLS,
|
|
14
|
+
type EditorFrameFailure,
|
|
15
|
+
type EditorHostPageData,
|
|
16
|
+
} from '@conloca/content-api-client';
|
|
17
|
+
export type { PageConversionFacts } from '../convert-pages.js';
|
|
18
|
+
export { CONTENT_METHOD_CLASSIFICATION } from '../write-access.js';
|
|
19
|
+
|
|
20
|
+
export interface ProtocolRange {
|
|
21
|
+
min: number;
|
|
22
|
+
max: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const HELLO_TYPE = 'conloca:hello';
|
|
26
|
+
|
|
27
|
+
/** The parent's window message; it carries exactly one port. */
|
|
28
|
+
export interface Hello {
|
|
29
|
+
type: typeof HELLO_TYPE;
|
|
30
|
+
protocols: ProtocolRange;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The page's answer to a hello, with the identity its build baked in, so the parent can compare it. */
|
|
34
|
+
export interface Accepted {
|
|
35
|
+
kind: 'accepted';
|
|
36
|
+
protocol: number;
|
|
37
|
+
repo: string | null;
|
|
38
|
+
siteId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface Nack {
|
|
42
|
+
kind: 'nack';
|
|
43
|
+
code: 'origin-rejected' | 'unsupported-protocol';
|
|
44
|
+
/** The protocol the page speaks, sent with `unsupported-protocol`. */
|
|
45
|
+
page?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The parent's settings, sent once after `accepted`. */
|
|
49
|
+
export interface Init {
|
|
50
|
+
kind: 'init';
|
|
51
|
+
initialPath: string;
|
|
52
|
+
readOnly: EditorWriteRefusal | null;
|
|
53
|
+
previewCapability: PreviewCapability;
|
|
54
|
+
theme: ThemePreference;
|
|
55
|
+
locales?: { list: string[]; defaultLocale: string };
|
|
56
|
+
mediaEnabled: boolean;
|
|
57
|
+
/** Facts the prompt for converting existing pages needs; absent while the parent does not know them. */
|
|
58
|
+
pageConversion?: PageConversionFacts;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface Ready {
|
|
62
|
+
kind: 'ready';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface Call {
|
|
66
|
+
kind: 'call';
|
|
67
|
+
id: number;
|
|
68
|
+
method: string;
|
|
69
|
+
args: unknown[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type Result =
|
|
73
|
+
| { kind: 'result'; id: number; ok: true; value: unknown }
|
|
74
|
+
| { kind: 'result'; id: number; ok: false; error: WireError };
|
|
75
|
+
|
|
76
|
+
export interface FrameEvent {
|
|
77
|
+
kind: 'event';
|
|
78
|
+
name: string;
|
|
79
|
+
payload: unknown;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type ControlMessage = Accepted | Nack | Init | Ready;
|
|
83
|
+
export type FrameMessage = ControlMessage | Call | Result | FrameEvent;
|
|
84
|
+
|
|
85
|
+
/** Codes of the errors the channel itself raises. */
|
|
86
|
+
export type RpcErrorCode = 'method_not_allowed' | 'bad_arguments' | 'rate_limited';
|
|
87
|
+
|
|
88
|
+
/** An error as it crosses the port; the receiving side rebuilds the matching class. */
|
|
89
|
+
export interface WireError {
|
|
90
|
+
/** `APIClientError`, `StaleWriteError`, `ContentWriteRefusedError`, `RpcError` or `Error`. */
|
|
91
|
+
name: string;
|
|
92
|
+
message: string;
|
|
93
|
+
/** `APIClientError.code`, or an {@link RpcErrorCode}. */
|
|
94
|
+
code?: string;
|
|
95
|
+
/** `APIClientError.details`. */
|
|
96
|
+
details?: unknown;
|
|
97
|
+
/** `StaleWriteError.data`. */
|
|
98
|
+
data?: unknown;
|
|
99
|
+
/** `ContentWriteRefusedError.method`. */
|
|
100
|
+
method?: string;
|
|
101
|
+
refusal?: { reason: string };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The argument of `media.load`. */
|
|
105
|
+
export interface MediaLoadInput {
|
|
106
|
+
/** `'serve'` loads a draft address, `'repo'` an image file on the branch. */
|
|
107
|
+
route: 'serve' | 'repo';
|
|
108
|
+
/** Relative to the media root (`uploads/hero.png`): raw, never URL-encoded, no leading slash. */
|
|
109
|
+
path: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The answer to `preview.open`. */
|
|
113
|
+
export type PreviewOpenResult =
|
|
114
|
+
| { ok: true }
|
|
115
|
+
| { ok: false; reason: 'not-deployed'; message: string }
|
|
116
|
+
| { ok: false; reason: 'popup-blocked' };
|
|
117
|
+
|
|
118
|
+
/** The answer to `site.open`; a refused path rejects with `RpcError` `bad_arguments`. */
|
|
119
|
+
export type SiteOpenResult = { ok: true } | { ok: false; reason: 'popup-blocked' };
|
|
120
|
+
|
|
121
|
+
/** Each event name and its payload. */
|
|
122
|
+
export interface FrameEventPayloads {
|
|
123
|
+
'editor.dirty': { dirty: boolean };
|
|
124
|
+
'shortcut.save': null;
|
|
125
|
+
'editor.stale': null;
|
|
126
|
+
'load.failed': {
|
|
127
|
+
stage: 'site' | 'css' | 'ui';
|
|
128
|
+
/** For logs; the parent shows its own text. */
|
|
129
|
+
message: string;
|
|
130
|
+
};
|
|
131
|
+
'route.changed': { path: string; replace: boolean };
|
|
132
|
+
'route.navigate': { path: string };
|
|
133
|
+
'readOnly.changed': { readOnly: EditorWriteRefusal | null };
|
|
134
|
+
'previewCapability.changed': { previewCapability: PreviewCapability };
|
|
135
|
+
'theme.changed': { theme: ThemePreference };
|
|
136
|
+
'pageConversion.changed': { pageConversion: PageConversionFacts | null };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Content methods the page may call; the parent refuses every other client method. */
|
|
140
|
+
export const FRAME_CONTENT_METHODS = [
|
|
141
|
+
'getContent',
|
|
142
|
+
'getLocalized',
|
|
143
|
+
'getSitePages',
|
|
144
|
+
'getFragments',
|
|
145
|
+
'getData',
|
|
146
|
+
'getDataCollections',
|
|
147
|
+
'listAllContent',
|
|
148
|
+
'getSitesConfig',
|
|
149
|
+
'getPageByPathname',
|
|
150
|
+
'getFragmentByName',
|
|
151
|
+
'getDataByName',
|
|
152
|
+
'isPathnameAvailable',
|
|
153
|
+
'isDataNameAvailable',
|
|
154
|
+
'compileMDX',
|
|
155
|
+
'createContent',
|
|
156
|
+
'updateLocalized',
|
|
157
|
+
'deleteContent',
|
|
158
|
+
'deleteLocalized',
|
|
159
|
+
'batchUpdate',
|
|
160
|
+
'movePage',
|
|
161
|
+
'listAssets',
|
|
162
|
+
'getAsset',
|
|
163
|
+
'listFolder',
|
|
164
|
+
'getFolderTree',
|
|
165
|
+
'getAssetUsage',
|
|
166
|
+
'uploadAsset',
|
|
167
|
+
'importAssetUrl',
|
|
168
|
+
'deleteAsset',
|
|
169
|
+
'createFolder',
|
|
170
|
+
'updateAssetMetadata',
|
|
171
|
+
'moveAssets',
|
|
172
|
+
] as const satisfies readonly (keyof ContentAPIClient)[];
|
|
173
|
+
|
|
174
|
+
/** Parent-side methods the page may call; `site.open` takes `{ path }`, the page pathname relative to the site base. */
|
|
175
|
+
export const FRAME_HOST_METHODS = [
|
|
176
|
+
'mediaBridge.getAssetScope',
|
|
177
|
+
'mediaBridge.getMediaIssue',
|
|
178
|
+
'media.load',
|
|
179
|
+
'preview.open',
|
|
180
|
+
'site.open',
|
|
181
|
+
] as const;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The parent calls this on the page and gets an `EditorFlushResult`. Its `message` is for logs; the parent shows its
|
|
185
|
+
* own text.
|
|
186
|
+
*/
|
|
187
|
+
export const EDITOR_FLUSH_METHOD = 'editor.flush';
|
|
188
|
+
|
|
189
|
+
export type FrameMethod = (typeof FRAME_CONTENT_METHODS)[number] | (typeof FRAME_HOST_METHODS)[number];
|
|
190
|
+
|
|
191
|
+
/** The most arguments each method takes, as the client declares them. */
|
|
192
|
+
export const FRAME_METHOD_MAX_ARGS = {
|
|
193
|
+
getContent: 1,
|
|
194
|
+
getLocalized: 2,
|
|
195
|
+
getSitePages: 2,
|
|
196
|
+
getFragments: 2,
|
|
197
|
+
getData: 2,
|
|
198
|
+
getDataCollections: 0,
|
|
199
|
+
listAllContent: 1,
|
|
200
|
+
getSitesConfig: 0,
|
|
201
|
+
getPageByPathname: 3,
|
|
202
|
+
getFragmentByName: 3,
|
|
203
|
+
getDataByName: 3,
|
|
204
|
+
isPathnameAvailable: 4,
|
|
205
|
+
isDataNameAvailable: 3,
|
|
206
|
+
compileMDX: 1,
|
|
207
|
+
createContent: 1,
|
|
208
|
+
updateLocalized: 1,
|
|
209
|
+
deleteContent: 2,
|
|
210
|
+
deleteLocalized: 1,
|
|
211
|
+
batchUpdate: 1,
|
|
212
|
+
movePage: 5,
|
|
213
|
+
listAssets: 0,
|
|
214
|
+
getAsset: 1,
|
|
215
|
+
listFolder: 1,
|
|
216
|
+
getFolderTree: 0,
|
|
217
|
+
getAssetUsage: 1,
|
|
218
|
+
uploadAsset: 1,
|
|
219
|
+
importAssetUrl: 3,
|
|
220
|
+
deleteAsset: 1,
|
|
221
|
+
createFolder: 1,
|
|
222
|
+
updateAssetMetadata: 2,
|
|
223
|
+
moveAssets: 3,
|
|
224
|
+
'mediaBridge.getAssetScope': 1,
|
|
225
|
+
'mediaBridge.getMediaIssue': 1,
|
|
226
|
+
'media.load': 1,
|
|
227
|
+
'preview.open': 1,
|
|
228
|
+
'site.open': 1,
|
|
229
|
+
} as const satisfies Readonly<Record<FrameMethod, number>>;
|
|
230
|
+
|
|
231
|
+
const FRAME_PATH_RE = /^\/[A-Za-z0-9/_.:%-]{0,512}(\?[A-Za-z0-9_.:%=&-]{0,512})?$/;
|
|
232
|
+
const DNS_LABEL_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
233
|
+
|
|
234
|
+
/** Whether `path` is an editor route (`pathname + search`) the page may report or the parent may send. */
|
|
235
|
+
export function isFramePath(path: string): boolean {
|
|
236
|
+
return FRAME_PATH_RE.test(path) && !path.includes('//');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/* oxlint-disable anti-slop/no-unknown-parameters -- the guards below read what arrives on the port, untyped by nature. */
|
|
240
|
+
|
|
241
|
+
export function isThemePreference(value: unknown): value is ThemePreference {
|
|
242
|
+
return value === 'light' || value === 'dark' || value === 'system';
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function isPreviewCapability(value: unknown): value is PreviewCapability {
|
|
246
|
+
return value === 'available' || value === 'unavailable' || value === 'unknown';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function isWriteRefusal(value: unknown): value is EditorWriteRefusal {
|
|
250
|
+
return typeof value === 'object' && value !== null && 'reason' in value && typeof value.reason === 'string';
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function readLocales(value: unknown): Init['locales'] | undefined {
|
|
254
|
+
if (typeof value !== 'object' || value === null || !('list' in value) || !('defaultLocale' in value))
|
|
255
|
+
return undefined;
|
|
256
|
+
const { list, defaultLocale } = value;
|
|
257
|
+
if (!Array.isArray(list) || typeof defaultLocale !== 'string') return undefined;
|
|
258
|
+
const locales = list.filter((locale): locale is string => typeof locale === 'string');
|
|
259
|
+
return locales.length === list.length ? { list: locales, defaultLocale } : undefined;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function readInit(data: unknown): Init | undefined {
|
|
263
|
+
if (typeof data !== 'object' || data === null) return undefined;
|
|
264
|
+
if (!('initialPath' in data) || typeof data.initialPath !== 'string' || !isFramePath(data.initialPath)) {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
if (!('readOnly' in data) || !(data.readOnly === null || isWriteRefusal(data.readOnly))) return undefined;
|
|
268
|
+
if (!('previewCapability' in data) || !isPreviewCapability(data.previewCapability)) return undefined;
|
|
269
|
+
if (!('theme' in data) || !isThemePreference(data.theme)) return undefined;
|
|
270
|
+
if (!('mediaEnabled' in data) || typeof data.mediaEnabled !== 'boolean') return undefined;
|
|
271
|
+
const init: Init = {
|
|
272
|
+
kind: 'init',
|
|
273
|
+
initialPath: data.initialPath,
|
|
274
|
+
readOnly: data.readOnly,
|
|
275
|
+
previewCapability: data.previewCapability,
|
|
276
|
+
theme: data.theme,
|
|
277
|
+
mediaEnabled: data.mediaEnabled,
|
|
278
|
+
};
|
|
279
|
+
// Unlike `locales`, malformed facts only cost the prompt, not the whole init.
|
|
280
|
+
const pageConversion = 'pageConversion' in data ? readPageConversionFacts(data.pageConversion) : undefined;
|
|
281
|
+
if (pageConversion) init.pageConversion = pageConversion;
|
|
282
|
+
if (!('locales' in data) || data.locales === undefined) return init;
|
|
283
|
+
const locales = readLocales(data.locales);
|
|
284
|
+
return locales ? { ...init, locales } : undefined;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Reads a control message off the port; `undefined` when `data` is not a well-formed one. */
|
|
288
|
+
export function readControlMessage(data: unknown): ControlMessage | undefined {
|
|
289
|
+
if (typeof data !== 'object' || data === null || !('kind' in data)) return undefined;
|
|
290
|
+
switch (data.kind) {
|
|
291
|
+
case 'ready':
|
|
292
|
+
return { kind: 'ready' };
|
|
293
|
+
case 'init':
|
|
294
|
+
return readInit(data);
|
|
295
|
+
case 'accepted':
|
|
296
|
+
if (!('protocol' in data) || typeof data.protocol !== 'number') return undefined;
|
|
297
|
+
if (!('repo' in data) || !(data.repo === null || typeof data.repo === 'string')) return undefined;
|
|
298
|
+
if (!('siteId' in data) || typeof data.siteId !== 'string') return undefined;
|
|
299
|
+
return { kind: 'accepted', protocol: data.protocol, repo: data.repo, siteId: data.siteId };
|
|
300
|
+
case 'nack': {
|
|
301
|
+
if (!('code' in data) || (data.code !== 'origin-rejected' && data.code !== 'unsupported-protocol'))
|
|
302
|
+
return undefined;
|
|
303
|
+
const nack: Nack = { kind: 'nack', code: data.code };
|
|
304
|
+
return 'page' in data && typeof data.page === 'number' ? { ...nack, page: data.page } : nack;
|
|
305
|
+
}
|
|
306
|
+
default:
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* oxlint-enable anti-slop/no-unknown-parameters */
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Whether the page accepts a hello from `origin`: `https://<label>.<root>` without a port, `<label>` one DNS label
|
|
315
|
+
* that is not reserved. Under a `.localhost` root, `http:` and any port are allowed too.
|
|
316
|
+
*/
|
|
317
|
+
export function acceptsParentOrigin(origin: string, root: string, isReserved: (label: string) => boolean): boolean {
|
|
318
|
+
let url: URL;
|
|
319
|
+
try {
|
|
320
|
+
url = new URL(origin);
|
|
321
|
+
} catch {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
const local = root.endsWith('.localhost');
|
|
325
|
+
if (url.origin !== origin) return false;
|
|
326
|
+
if (url.protocol !== 'https:' && !(local && url.protocol === 'http:')) return false;
|
|
327
|
+
if (url.port !== '' && !local) return false;
|
|
328
|
+
const suffix = `.${root}`;
|
|
329
|
+
if (!url.hostname.endsWith(suffix)) return false;
|
|
330
|
+
const label = url.hostname.slice(0, -suffix.length);
|
|
331
|
+
return DNS_LABEL_RE.test(label) && !isReserved(label);
|
|
332
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ContentAPIClient } from '@conloca/content-api-client';
|
|
2
|
+
import type { FrameChannel } from './channel.js';
|
|
3
|
+
import { clearMediaCache } from './media-cache.js';
|
|
4
|
+
import { CONTENT_METHOD_CLASSIFICATION } from './protocol.js';
|
|
5
|
+
|
|
6
|
+
/** Writes after which loaded media bytes may be stale. */
|
|
7
|
+
const MEDIA_WRITES = new Set([
|
|
8
|
+
'uploadAsset',
|
|
9
|
+
'importAssetUrl',
|
|
10
|
+
'deleteAsset',
|
|
11
|
+
'createFolder',
|
|
12
|
+
'updateAssetMetadata',
|
|
13
|
+
'moveAssets',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
/** The name/value pairs of an upload form, each File replaced by a copy held in memory. */
|
|
17
|
+
function uploadPairs(form: FormData): Promise<[string, FormDataEntryValue][]> {
|
|
18
|
+
return Promise.all(
|
|
19
|
+
[...form.entries()].map(
|
|
20
|
+
async ([name, value]): Promise<[string, FormDataEntryValue]> =>
|
|
21
|
+
typeof value === 'string'
|
|
22
|
+
? [name, value]
|
|
23
|
+
: [
|
|
24
|
+
name,
|
|
25
|
+
new File([await value.arrayBuffer()], value.name, {
|
|
26
|
+
type: value.type,
|
|
27
|
+
lastModified: value.lastModified,
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The content client of an editor frame: it holds no credentials, so every content method becomes a call the
|
|
36
|
+
* parent frame runs on its own client. All methods cross, writes and reads alike; the parent decides what it refuses.
|
|
37
|
+
*/
|
|
38
|
+
export class RpcContentClient extends ContentAPIClient {
|
|
39
|
+
constructor(channel: Pick<FrameChannel, 'call'>) {
|
|
40
|
+
super();
|
|
41
|
+
for (const method of Object.keys(CONTENT_METHOD_CLASSIFICATION)) {
|
|
42
|
+
Object.defineProperty(this, method, {
|
|
43
|
+
// oxlint-disable-next-line anti-slop/no-unknown-returns -- it replaces a typed ContentAPIClient method; callers see that method's types.
|
|
44
|
+
value: async (...args: unknown[]): Promise<unknown> => {
|
|
45
|
+
// FormData cannot be cloned onto a port; its name/value pairs can. A picked File is backed by a disk
|
|
46
|
+
// path only this frame's process may read, so each File crosses as an in-memory copy of its bytes.
|
|
47
|
+
const sent = method === 'uploadAsset' && args[0] instanceof FormData ? [await uploadPairs(args[0])] : args;
|
|
48
|
+
const result = await channel.call(method, sent);
|
|
49
|
+
if (MEDIA_WRITES.has(method)) clearMediaCache();
|
|
50
|
+
return result;
|
|
51
|
+
},
|
|
52
|
+
writable: false,
|
|
53
|
+
enumerable: false,
|
|
54
|
+
configurable: false,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { inEditorFrame } from '../ui-config.js';
|
|
3
|
+
|
|
4
|
+
/** Whether a key press is Cmd/Ctrl+S. */
|
|
5
|
+
export function isSaveCombo(event: KeyboardEvent): boolean {
|
|
6
|
+
return (event.metaKey || event.ctrlKey) && (event.key === 's' || event.key === 'S');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* In a parent frame, hands Cmd/Ctrl+S pressed inside the editing canvas to the editor's `window`, where the frame
|
|
11
|
+
* connection sends it to the parent. The canvas is its own document, so its key presses never reach `window`.
|
|
12
|
+
*/
|
|
13
|
+
export function useCanvasSaveShortcut(canvasDocument: Document | undefined): void {
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (!canvasDocument || !inEditorFrame()) return undefined;
|
|
16
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
17
|
+
if (!isSaveCombo(event)) return;
|
|
18
|
+
event.preventDefault();
|
|
19
|
+
const { key, metaKey, ctrlKey } = event;
|
|
20
|
+
window.dispatchEvent(new KeyboardEvent('keydown', { key, metaKey, ctrlKey, cancelable: true }));
|
|
21
|
+
};
|
|
22
|
+
canvasDocument.addEventListener('keydown', onKeyDown);
|
|
23
|
+
return () => canvasDocument.removeEventListener('keydown', onKeyDown);
|
|
24
|
+
}, [canvasDocument]);
|
|
25
|
+
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -16,6 +16,8 @@ export {
|
|
|
16
16
|
export { useClickOutside } from './useClickOutside.js';
|
|
17
17
|
export { useDialogState } from './useDialogState.js';
|
|
18
18
|
export { useErrorModal } from './useErrorModal.js';
|
|
19
|
+
export type { PageConversionOffer } from './usePageConversion.js';
|
|
20
|
+
export { usePageConversion } from './usePageConversion.js';
|
|
19
21
|
export { useSiteBaseUrl } from './useSiteBaseUrl.js';
|
|
20
22
|
export { buildUploadFormData, useImportUrl, useUpload } from './useUpload.js';
|
|
21
23
|
export type { UploadProgress, UseUploadFlowReturn } from './useUploadFlow.js';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type ContentManifest, isEditorRoute, routesOf } from '@conloca/content-api-client';
|
|
2
|
+
import { useQuery } from '@tanstack/react-query';
|
|
3
|
+
import { useSyncExternalStore } from 'react';
|
|
4
|
+
import { type PageConversionFacts, pageCollection, unconvertedRoutes } from '../convert-pages.js';
|
|
5
|
+
import { EDITOR_CONFIG_CHANGED_EVENT, getUIConfig } from '../ui-config.js';
|
|
6
|
+
import { editorWriteAccess } from '../write-access.js';
|
|
7
|
+
|
|
8
|
+
export interface PageConversionOffer {
|
|
9
|
+
facts: PageConversionFacts;
|
|
10
|
+
collection: string;
|
|
11
|
+
/** Routes to offer; `undefined` while unknown, so the prompt asks the agent to list them. */
|
|
12
|
+
candidates: string[] | undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let configVersion = 0;
|
|
16
|
+
|
|
17
|
+
function subscribeToConfig(onChange: () => void): () => void {
|
|
18
|
+
const changed = () => {
|
|
19
|
+
configVersion++;
|
|
20
|
+
onChange();
|
|
21
|
+
};
|
|
22
|
+
window.addEventListener(EDITOR_CONFIG_CHANGED_EVENT, changed);
|
|
23
|
+
return () => window.removeEventListener(EDITOR_CONFIG_CHANGED_EVENT, changed);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** What this editor offers for converting Astro pages; null when it offers nothing. Re-renders on EDITOR_CONFIG_CHANGED_EVENT. */
|
|
27
|
+
export function usePageConversion(pages: readonly ContentManifest[]): PageConversionOffer | null {
|
|
28
|
+
useSyncExternalStore(
|
|
29
|
+
subscribeToConfig,
|
|
30
|
+
() => configVersion,
|
|
31
|
+
() => configVersion,
|
|
32
|
+
);
|
|
33
|
+
const config = getUIConfig();
|
|
34
|
+
const facts = config.pageConversion;
|
|
35
|
+
const writable = editorWriteAccess(config).writable;
|
|
36
|
+
const local = config.hosted !== true;
|
|
37
|
+
// The local dev server lists the site's static routes; the workspace editor sends them with the facts.
|
|
38
|
+
const staticRoutes = useQuery({
|
|
39
|
+
queryKey: ['conloca', 'static-routes'],
|
|
40
|
+
queryFn: async () => {
|
|
41
|
+
const response = await fetch(`${config.apiBaseUrl ?? '/__cms/api'}/static-routes`);
|
|
42
|
+
if (!response.ok) throw new Error(`Listing the site's routes failed with ${response.status}`);
|
|
43
|
+
const body: unknown = await response.json();
|
|
44
|
+
if (typeof body !== 'object' || body === null) throw new Error("The site's route list is not an object");
|
|
45
|
+
return routesOf({ routes: Reflect.get(body, 'routes') }) ?? null;
|
|
46
|
+
},
|
|
47
|
+
enabled: writable && facts !== undefined && local,
|
|
48
|
+
});
|
|
49
|
+
if (!writable || facts === undefined) return null;
|
|
50
|
+
if (local && staticRoutes.isPending) return null;
|
|
51
|
+
const routes = local ? (staticRoutes.data ?? undefined) : facts.routes?.filter(isEditorRoute);
|
|
52
|
+
const candidates = routes === undefined ? undefined : unconvertedRoutes(routes, pages);
|
|
53
|
+
if (candidates?.length === 0) return null;
|
|
54
|
+
return { facts, collection: pageCollection(config.dataBindings), candidates };
|
|
55
|
+
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_MAX_FILE_SIZE,
|
|
3
|
+
IMAGE_EXTENSION_MIME,
|
|
4
|
+
useImportAssetUrl,
|
|
5
|
+
useUploadAsset,
|
|
6
|
+
} from '@conloca/content-api-client';
|
|
2
7
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
8
|
+
import { prepareImageUpload } from '../media/prepare-image-upload.js';
|
|
3
9
|
import { buildUploadFormData } from './useUpload.js';
|
|
4
10
|
|
|
5
11
|
/**
|
|
@@ -60,6 +66,10 @@ export function useUploadFlow({ folder, onUploadComplete, onSuccess }: UseUpload
|
|
|
60
66
|
const [activeTab, setActiveTab] = useState<'file' | 'url'>('file');
|
|
61
67
|
const [importUrl, setImportUrl] = useState('');
|
|
62
68
|
const [uploadProgress, setUploadProgress] = useState<UploadProgress | null>(null);
|
|
69
|
+
/** A file refused in the browser, before any request. */
|
|
70
|
+
const [processingError, setProcessingError] = useState<Error | null>(null);
|
|
71
|
+
/** True while a picked photo is shrunk, so a second click cannot send it twice. */
|
|
72
|
+
const [isPreparing, setIsPreparing] = useState(false);
|
|
63
73
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
64
74
|
|
|
65
75
|
const uploadMutation = useUploadAsset();
|
|
@@ -78,14 +88,16 @@ export function useUploadFlow({ folder, onUploadComplete, onSuccess }: UseUpload
|
|
|
78
88
|
};
|
|
79
89
|
}, [previewUrl]);
|
|
80
90
|
|
|
81
|
-
const isUploading =
|
|
82
|
-
|
|
91
|
+
const isUploading =
|
|
92
|
+
isPreparing || uploadMutation.isPending || importMutation.isPending || (uploadProgress?.inProgress ?? false);
|
|
93
|
+
const error = processingError || uploadMutation.error || importMutation.error;
|
|
83
94
|
|
|
84
95
|
const reset = useCallback(() => {
|
|
85
96
|
setPendingFile(null);
|
|
86
97
|
setAltText('');
|
|
87
98
|
setImportUrl('');
|
|
88
99
|
setUploadProgress(null);
|
|
100
|
+
setProcessingError(null);
|
|
89
101
|
setActiveTab('file');
|
|
90
102
|
setIsDragOver(false);
|
|
91
103
|
if (fileInputRef.current) fileInputRef.current.value = '';
|
|
@@ -94,6 +106,7 @@ export function useUploadFlow({ folder, onUploadComplete, onSuccess }: UseUpload
|
|
|
94
106
|
const handleSingleFile = useCallback((file: File) => {
|
|
95
107
|
setPendingFile(file);
|
|
96
108
|
setAltText('');
|
|
109
|
+
setProcessingError(null);
|
|
97
110
|
}, []);
|
|
98
111
|
|
|
99
112
|
const handleMultipleFiles = useCallback(
|
|
@@ -108,12 +121,24 @@ export function useUploadFlow({ folder, onUploadComplete, onSuccess }: UseUpload
|
|
|
108
121
|
// Multi-file upload: upload all in parallel without alt text prompts
|
|
109
122
|
setUploadProgress({ total: files.length, completed: 0, failed: 0, inProgress: true });
|
|
110
123
|
|
|
124
|
+
let refusal: Error | null = null;
|
|
125
|
+
// One file decoded at a time: decoding many camera photos at once can exhaust a phone's memory.
|
|
126
|
+
let previous: Promise<unknown> = Promise.resolve();
|
|
111
127
|
const uploadPromises = files.map(async (file) => {
|
|
112
|
-
const
|
|
113
|
-
|
|
128
|
+
const preparing = previous.then(() => prepareImageUpload(file, DEFAULT_MAX_FILE_SIZE));
|
|
129
|
+
previous = preparing.catch(() => undefined);
|
|
130
|
+
let ready: File;
|
|
131
|
+
try {
|
|
132
|
+
ready = await preparing;
|
|
133
|
+
} catch (reason) {
|
|
134
|
+
refusal ??= reason instanceof Error ? reason : new Error(String(reason));
|
|
135
|
+
throw reason;
|
|
136
|
+
}
|
|
137
|
+
return uploadMutation.mutateAsync(buildUploadFormData(ready, undefined, folder));
|
|
114
138
|
});
|
|
115
139
|
|
|
116
140
|
const results = await Promise.allSettled(uploadPromises);
|
|
141
|
+
setProcessingError(refusal);
|
|
117
142
|
|
|
118
143
|
const completed = results.filter((r) => r.status === 'fulfilled').length;
|
|
119
144
|
const failed = results.filter((r) => r.status === 'rejected').length;
|
|
@@ -161,9 +186,20 @@ export function useUploadFlow({ folder, onUploadComplete, onSuccess }: UseUpload
|
|
|
161
186
|
);
|
|
162
187
|
|
|
163
188
|
const handleUpload = async () => {
|
|
164
|
-
if (!pendingFile) return;
|
|
189
|
+
if (!pendingFile || isUploading) return;
|
|
165
190
|
|
|
166
|
-
|
|
191
|
+
setProcessingError(null);
|
|
192
|
+
setIsPreparing(true);
|
|
193
|
+
let file: File;
|
|
194
|
+
try {
|
|
195
|
+
file = await prepareImageUpload(pendingFile, DEFAULT_MAX_FILE_SIZE);
|
|
196
|
+
} catch (reason) {
|
|
197
|
+
setProcessingError(reason instanceof Error ? reason : new Error(String(reason)));
|
|
198
|
+
return;
|
|
199
|
+
} finally {
|
|
200
|
+
setIsPreparing(false);
|
|
201
|
+
}
|
|
202
|
+
const formData = buildUploadFormData(file, altText || undefined, folder);
|
|
167
203
|
uploadMutation.mutate(formData, {
|
|
168
204
|
onSuccess: () => {
|
|
169
205
|
// Reset shared state
|
|
@@ -196,6 +232,7 @@ export function useUploadFlow({ folder, onUploadComplete, onSuccess }: UseUpload
|
|
|
196
232
|
const handleCancel = () => {
|
|
197
233
|
setPendingFile(null);
|
|
198
234
|
setAltText('');
|
|
235
|
+
setProcessingError(null);
|
|
199
236
|
if (fileInputRef.current) fileInputRef.current.value = '';
|
|
200
237
|
};
|
|
201
238
|
|