@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
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { EDITOR_DIRTY_CHANGED_EVENT, flushOpenEditor, isOpenEditorDirty } from '../editor-flush.js';
|
|
2
|
+
import type { AssetScope, MediaIssue } from '../media/types.js';
|
|
3
|
+
import { THEME_CHANGED_EVENT, THEME_STORAGE_KEY } from '../theme.js';
|
|
4
|
+
import { setEditorReadOnly, setPreviewCapability, type UIConfig } from '../ui-config.js';
|
|
5
|
+
import { type FrameChannel, type FramePort, openFrameChannel, RpcError } from './channel.js';
|
|
6
|
+
import {
|
|
7
|
+
EDITOR_FLUSH_METHOD,
|
|
8
|
+
type FrameEventPayloads,
|
|
9
|
+
type FrameMethod,
|
|
10
|
+
type Init,
|
|
11
|
+
isFramePath,
|
|
12
|
+
isPreviewCapability,
|
|
13
|
+
isThemePreference,
|
|
14
|
+
isWriteRefusal,
|
|
15
|
+
type PreviewOpenResult,
|
|
16
|
+
} from './protocol.js';
|
|
17
|
+
import { RpcContentClient } from './rpc-content-client.js';
|
|
18
|
+
import { isSaveCombo } from './save-shortcut.js';
|
|
19
|
+
|
|
20
|
+
/** The editor page's side of its connection to the parent frame, once the parent's settings arrived. */
|
|
21
|
+
export interface EditorFrame {
|
|
22
|
+
/** The parent's settings. */
|
|
23
|
+
init: Init;
|
|
24
|
+
/** The `UIConfig` part the page mounts with: content client, memory router, preview and media through the parent. */
|
|
25
|
+
config: Partial<UIConfig>;
|
|
26
|
+
/** Tells the parent the editor is mounted. */
|
|
27
|
+
ready(): void;
|
|
28
|
+
/** Tells the parent this page is out of date and should be reloaded. */
|
|
29
|
+
reportStale(): void;
|
|
30
|
+
/** Tells the parent the page could not load; `message` is for logs, the parent shows its own text. */
|
|
31
|
+
reportLoadFailed(stage: FrameEventPayloads['load.failed']['stage'], message: string): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isPreviewOpenResult(value: unknown): value is PreviewOpenResult {
|
|
35
|
+
if (typeof value !== 'object' || value === null || !('ok' in value)) return false;
|
|
36
|
+
if (value.ok === true) return true;
|
|
37
|
+
if (value.ok !== false || !('reason' in value)) return false;
|
|
38
|
+
if (value.reason === 'popup-blocked') return true;
|
|
39
|
+
return value.reason === 'not-deployed' && 'message' in value && typeof value.message === 'string';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isAssetScope(value: unknown): value is AssetScope | null {
|
|
43
|
+
return value === null || value === 'branch' || value === 'published';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isMediaIssue(value: unknown): value is MediaIssue | null {
|
|
47
|
+
if (value === null) return true;
|
|
48
|
+
if (typeof value !== 'object' || !('filename' in value) || typeof value.filename !== 'string') return false;
|
|
49
|
+
if (!('kind' in value)) return false;
|
|
50
|
+
if (value.kind === 'blocked') return 'reason' in value && typeof value.reason === 'string';
|
|
51
|
+
return (
|
|
52
|
+
value.kind === 'oversized' &&
|
|
53
|
+
'sizeBytes' in value &&
|
|
54
|
+
typeof value.sizeBytes === 'number' &&
|
|
55
|
+
'limitBytes' in value &&
|
|
56
|
+
typeof value.limitBytes === 'number'
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isBlob(value: unknown): value is Blob {
|
|
61
|
+
return value instanceof Blob;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Calls the parent and rejects an answer that is not the shape the method promises. */
|
|
65
|
+
async function checkedCall<T>(
|
|
66
|
+
channel: FrameChannel,
|
|
67
|
+
method: FrameMethod,
|
|
68
|
+
input: unknown,
|
|
69
|
+
isAnswer: (value: unknown) => value is T,
|
|
70
|
+
): Promise<T> {
|
|
71
|
+
const answer = await channel.call(method, [input]);
|
|
72
|
+
if (!isAnswer(answer)) throw new Error(`${method} answered with an unexpected value`);
|
|
73
|
+
return answer;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Opens the channel on the port the parent handed over and resolves on the parent's first `init`; later ones are
|
|
78
|
+
* ignored. From then on the parent can flush the open editor, and dirty state and Cmd/Ctrl+S go to the parent.
|
|
79
|
+
* Route, read-only, preview capability and theme follow the parent's events.
|
|
80
|
+
*/
|
|
81
|
+
export function connectEditorFrame(port: FramePort): Promise<EditorFrame> {
|
|
82
|
+
const routeListeners = new Set<(path: string) => void>();
|
|
83
|
+
// Executor form: the package's production lib predates Promise.withResolvers.
|
|
84
|
+
return new Promise((resolve) => {
|
|
85
|
+
let connected = false;
|
|
86
|
+
const channel = openFrameChannel(port, {
|
|
87
|
+
control(message) {
|
|
88
|
+
if (message.kind !== 'init' || connected) return;
|
|
89
|
+
connected = true;
|
|
90
|
+
resolve(start(channel, message, routeListeners));
|
|
91
|
+
},
|
|
92
|
+
async call(method) {
|
|
93
|
+
if (method === EDITOR_FLUSH_METHOD) return flushOpenEditor();
|
|
94
|
+
throw new RpcError(`Method not allowed: ${method}`, 'method_not_allowed');
|
|
95
|
+
},
|
|
96
|
+
event(name, payload) {
|
|
97
|
+
if (typeof payload !== 'object' || payload === null) return;
|
|
98
|
+
if (name === 'route.navigate' && 'path' in payload) {
|
|
99
|
+
const { path } = payload;
|
|
100
|
+
if (typeof path === 'string' && isFramePath(path)) for (const listener of routeListeners) listener(path);
|
|
101
|
+
} else if (
|
|
102
|
+
name === 'readOnly.changed' &&
|
|
103
|
+
'readOnly' in payload &&
|
|
104
|
+
(payload.readOnly === null || isWriteRefusal(payload.readOnly))
|
|
105
|
+
) {
|
|
106
|
+
setEditorReadOnly(payload.readOnly);
|
|
107
|
+
} else if (
|
|
108
|
+
name === 'previewCapability.changed' &&
|
|
109
|
+
'previewCapability' in payload &&
|
|
110
|
+
isPreviewCapability(payload.previewCapability)
|
|
111
|
+
) {
|
|
112
|
+
setPreviewCapability(payload.previewCapability);
|
|
113
|
+
} else if (name === 'theme.changed' && 'theme' in payload && isThemePreference(payload.theme)) {
|
|
114
|
+
localStorage.setItem(THEME_STORAGE_KEY, payload.theme);
|
|
115
|
+
window.dispatchEvent(new Event(THEME_CHANGED_EVENT));
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function start(channel: FrameChannel, init: Init, routeListeners: Set<(path: string) => void>): EditorFrame {
|
|
123
|
+
const emit = <K extends keyof FrameEventPayloads>(name: K, payload: FrameEventPayloads[K]) =>
|
|
124
|
+
channel.emit(name, payload);
|
|
125
|
+
|
|
126
|
+
localStorage.setItem(THEME_STORAGE_KEY, init.theme);
|
|
127
|
+
let dirty = false;
|
|
128
|
+
window.addEventListener(EDITOR_DIRTY_CHANGED_EVENT, () => {
|
|
129
|
+
const next = isOpenEditorDirty();
|
|
130
|
+
if (next === dirty) return;
|
|
131
|
+
dirty = next;
|
|
132
|
+
emit('editor.dirty', { dirty });
|
|
133
|
+
});
|
|
134
|
+
window.addEventListener('keydown', (event) => {
|
|
135
|
+
if (!isSaveCombo(event)) return;
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
emit('shortcut.save', null);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const config: Partial<UIConfig> = {
|
|
141
|
+
contentClient: new RpcContentClient(channel),
|
|
142
|
+
// Always present, so `configureUI` replaces a refusal left from before.
|
|
143
|
+
readOnly: init.readOnly ?? undefined,
|
|
144
|
+
previewCapability: init.previewCapability,
|
|
145
|
+
...(init.locales ? { locales: init.locales } : {}),
|
|
146
|
+
previewTab: 'parent',
|
|
147
|
+
openSiteUrl: async (input) => {
|
|
148
|
+
await channel.call('site.open', [input]);
|
|
149
|
+
},
|
|
150
|
+
openPreview: (input) => checkedCall(channel, 'preview.open', input, isPreviewOpenResult),
|
|
151
|
+
memoryRouter: {
|
|
152
|
+
initialPath: init.initialPath,
|
|
153
|
+
onNavigate: (path, replace) => {
|
|
154
|
+
// The parent refuses any other route, so it is not sent.
|
|
155
|
+
if (isFramePath(path)) emit('route.changed', { path, replace });
|
|
156
|
+
},
|
|
157
|
+
subscribe: (navigate) => {
|
|
158
|
+
routeListeners.add(navigate);
|
|
159
|
+
return () => routeListeners.delete(navigate);
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
...(init.mediaEnabled
|
|
163
|
+
? {
|
|
164
|
+
mediaBridge: {
|
|
165
|
+
getAssetScope: (input) => checkedCall(channel, 'mediaBridge.getAssetScope', input, isAssetScope),
|
|
166
|
+
getMediaIssue: (input) => checkedCall(channel, 'mediaBridge.getMediaIssue', input, isMediaIssue),
|
|
167
|
+
},
|
|
168
|
+
loadMedia: (input) => checkedCall(channel, 'media.load', input, isBlob),
|
|
169
|
+
}
|
|
170
|
+
: {}),
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
init,
|
|
175
|
+
config,
|
|
176
|
+
ready: () => channel.post({ kind: 'ready' }),
|
|
177
|
+
reportStale: () => emit('editor.stale', null),
|
|
178
|
+
reportLoadFailed: (stage, message) => emit('load.failed', { stage, message }),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/** Object URLs of media bytes loaded through `UIConfig.loadMedia`, for surfaces that cannot point `<img>` at a media route. */
|
|
2
|
+
import { useRef, useSyncExternalStore } from 'react';
|
|
3
|
+
import { getUIConfig } from '../ui-config.js';
|
|
4
|
+
import { assetPath, buildAssetServeUrl } from '../utils/asset-url.js';
|
|
5
|
+
import type { MediaLoadInput } from './protocol.js';
|
|
6
|
+
|
|
7
|
+
/** shortcut: count cap, not bytes; switch to a byte budget if large libraries show memory pressure. */
|
|
8
|
+
export const MEDIA_CACHE_LIMIT = 300;
|
|
9
|
+
|
|
10
|
+
/** No `url` means loading or failed; either way the key is not loaded again until the cache is cleared. */
|
|
11
|
+
interface MediaEntry {
|
|
12
|
+
url?: string;
|
|
13
|
+
blob?: Blob;
|
|
14
|
+
/** Settles when the load does, whether it loaded, failed or was discarded. */
|
|
15
|
+
loaded?: Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Map order is recency: a read moves its entry to the end, eviction takes from the front.
|
|
19
|
+
const entries = new Map<string, MediaEntry>();
|
|
20
|
+
const listeners = new Set<() => void>();
|
|
21
|
+
let version = 0;
|
|
22
|
+
// Bumped by every clear, so a mounted hook stops holding on to a URL from before the clear.
|
|
23
|
+
let generation = 0;
|
|
24
|
+
|
|
25
|
+
function notify() {
|
|
26
|
+
version += 1;
|
|
27
|
+
for (const listener of listeners) listener();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function subscribe(listener: () => void) {
|
|
31
|
+
listeners.add(listener);
|
|
32
|
+
return () => {
|
|
33
|
+
listeners.delete(listener);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function currentVersion() {
|
|
38
|
+
return version;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The entry for `key`, moved to most recently used when it holds a URL. */
|
|
42
|
+
function touch(key: string): MediaEntry | undefined {
|
|
43
|
+
const entry = entries.get(key);
|
|
44
|
+
if (entry?.url) {
|
|
45
|
+
entries.delete(key);
|
|
46
|
+
entries.set(key, entry);
|
|
47
|
+
}
|
|
48
|
+
return entry;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function evict() {
|
|
52
|
+
let held = 0;
|
|
53
|
+
for (const entry of entries.values()) if (entry.url) held += 1;
|
|
54
|
+
for (const [key, entry] of entries) {
|
|
55
|
+
if (held <= MEDIA_CACHE_LIMIT) return;
|
|
56
|
+
if (!entry.url) continue;
|
|
57
|
+
URL.revokeObjectURL(entry.url);
|
|
58
|
+
entries.delete(key);
|
|
59
|
+
held -= 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The entry of `path` on `route`: a hit refreshes its recency; a miss starts its one load. `undefined` without a loader. */
|
|
64
|
+
function entryFor(route: MediaLoadInput['route'], path: string): MediaEntry | undefined {
|
|
65
|
+
const key = `${route}:${path}`;
|
|
66
|
+
const entry = touch(key);
|
|
67
|
+
if (entry) return entry;
|
|
68
|
+
const loadMedia = getUIConfig().loadMedia;
|
|
69
|
+
if (!loadMedia) return undefined;
|
|
70
|
+
const pending: MediaEntry = {};
|
|
71
|
+
entries.set(key, pending);
|
|
72
|
+
pending.loaded = loadMedia({ route, path }).then(
|
|
73
|
+
(blob) => {
|
|
74
|
+
// Cleared (or replaced) while loading: these bytes may be stale.
|
|
75
|
+
if (entries.get(key) !== pending) return;
|
|
76
|
+
pending.url = URL.createObjectURL(blob);
|
|
77
|
+
pending.blob = blob;
|
|
78
|
+
touch(key);
|
|
79
|
+
evict();
|
|
80
|
+
notify();
|
|
81
|
+
},
|
|
82
|
+
() => {},
|
|
83
|
+
);
|
|
84
|
+
return pending;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The object URL of `path` on `route`: a hit refreshes its recency; a miss starts one load and
|
|
89
|
+
* returns `undefined` (so does a key that is loading or failed, and every key without a loader).
|
|
90
|
+
*/
|
|
91
|
+
export function mediaObjectUrl(route: MediaLoadInput['route'], path: string): string | undefined {
|
|
92
|
+
return entryFor(route, path)?.url;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The object URL and bytes of `path` on `route` once loaded through the same cache; `undefined` if that fails or there is no loader. */
|
|
96
|
+
export async function loadedMedia(
|
|
97
|
+
route: MediaLoadInput['route'],
|
|
98
|
+
path: string,
|
|
99
|
+
): Promise<{ url: string; blob: Blob } | undefined> {
|
|
100
|
+
const entry = entryFor(route, path);
|
|
101
|
+
await entry?.loaded;
|
|
102
|
+
return entry?.url && entry.blob ? { url: entry.url, blob: entry.blob } : undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Revokes every URL and forgets every key, failed ones included; loads still running are discarded when they land. */
|
|
106
|
+
export function clearMediaCache(): void {
|
|
107
|
+
for (const entry of entries.values()) if (entry.url) URL.revokeObjectURL(entry.url);
|
|
108
|
+
entries.clear();
|
|
109
|
+
generation += 1;
|
|
110
|
+
notify();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A number that changes whenever a URL arrives or the cache is cleared; re-renders the caller then. */
|
|
114
|
+
export function useMediaCacheVersion(): number {
|
|
115
|
+
return useSyncExternalStore(subscribe, currentVersion, currentVersion);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The object URL of `path` on `route`, re-rendering when it arrives. Once shown, a URL stays
|
|
120
|
+
* for the same key until the cache is cleared, even if eviction drops its entry meanwhile, so
|
|
121
|
+
* a mounted thumbnail neither flickers nor loads again.
|
|
122
|
+
*/
|
|
123
|
+
export function useMediaObjectUrl(route: MediaLoadInput['route'], path: string): string | undefined {
|
|
124
|
+
useMediaCacheVersion();
|
|
125
|
+
const key = `${route}:${path}`;
|
|
126
|
+
const shown = useRef<{ key: string; url: string; generation: number } | undefined>(undefined);
|
|
127
|
+
if (shown.current?.key === key && shown.current.generation === generation) {
|
|
128
|
+
touch(key);
|
|
129
|
+
return shown.current.url;
|
|
130
|
+
}
|
|
131
|
+
const url = mediaObjectUrl(route, path);
|
|
132
|
+
if (url) shown.current = { key, url, generation };
|
|
133
|
+
return url;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The `src` of an asset thumbnail: an object URL of loaded bytes with a loader, else the serve URL. */
|
|
137
|
+
export function useAssetServeSrc(
|
|
138
|
+
assetsBasePath: string,
|
|
139
|
+
folder: string | undefined,
|
|
140
|
+
filename: string,
|
|
141
|
+
): string | undefined {
|
|
142
|
+
const loaded = useMediaObjectUrl('serve', assetPath(folder, filename));
|
|
143
|
+
return getUIConfig().loadMedia ? loaded : buildAssetServeUrl(assetsBasePath, folder, filename);
|
|
144
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createPath, type DataRouter, NavigationType, parsePath } from 'react-router-dom';
|
|
2
|
+
import type { MemoryRouterConfig } from '../ui-config.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Mirrors the editor's in-memory route to a parent frame: each committed route
|
|
6
|
+
* (`pathname + search`) goes to `onNavigate`, and each route the parent asks for
|
|
7
|
+
* is navigated to without being reported back. Returns the function that stops
|
|
8
|
+
* both directions.
|
|
9
|
+
*/
|
|
10
|
+
export function syncMemoryRouter(router: DataRouter, options: MemoryRouterConfig): () => void {
|
|
11
|
+
const { location } = router.state;
|
|
12
|
+
let current = location.pathname + location.search;
|
|
13
|
+
let parentPath: string | undefined;
|
|
14
|
+
|
|
15
|
+
const stopRouter = router.subscribe((state) => {
|
|
16
|
+
if (state.navigation.state !== 'idle') return;
|
|
17
|
+
const path = state.location.pathname + state.location.search;
|
|
18
|
+
if (path === current) return;
|
|
19
|
+
current = path;
|
|
20
|
+
// Any commit settles the parent's request, so an interrupted one cannot swallow a later route.
|
|
21
|
+
const fromParent = path === parentPath;
|
|
22
|
+
parentPath = undefined;
|
|
23
|
+
if (!fromParent) options.onNavigate(path, state.historyAction === NavigationType.Replace);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const stopParent = options.subscribe((path) => {
|
|
27
|
+
// In the router's own spelling (`/media?` is `/media`), or the commit would not match and be echoed back.
|
|
28
|
+
const normalized = createPath(parsePath(path));
|
|
29
|
+
// Already there: navigating would only push a duplicate entry.
|
|
30
|
+
if (normalized === current) return;
|
|
31
|
+
parentPath = normalized;
|
|
32
|
+
void router.navigate(path);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return () => {
|
|
36
|
+
stopRouter();
|
|
37
|
+
stopParent();
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -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
|
+
}
|