@conloca/cms-spa 0.2.3 → 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/dist/spa/main.css +6 -0
- package/dist/ts/.build-inputs.json +36 -27
- 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/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/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 +9 -9
- 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/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.map +1 -1
- package/dist/ts/components/editor/PageEditor.js +1 -2
- package/dist/ts/components/editor/PageEditor.js.map +1 -1
- package/dist/ts/components/editor/draft-media-config.d.ts +20 -8
- package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -1
- package/dist/ts/components/editor/draft-media-config.js +124 -60
- package/dist/ts/components/editor/draft-media-config.js.map +1 -1
- package/dist/ts/components/editor/preview-availability.d.ts +2 -0
- package/dist/ts/components/editor/preview-availability.d.ts.map +1 -1
- package/dist/ts/components/editor/preview-availability.js +2 -0
- 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.map +1 -1
- package/dist/ts/components/media/AssetCard.js +3 -3
- 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 +6 -3
- 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.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/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 +9 -4
- package/dist/ts/components/pages/PageList.js.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.js +3 -3
- package/dist/ts/components/sandbox/ContentSandbox.js.map +1 -1
- 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/frame/editor-frame.d.ts +1 -1
- package/dist/ts/frame/editor-frame.d.ts.map +1 -1
- package/dist/ts/frame/editor-frame.js +7 -2
- package/dist/ts/frame/editor-frame.js.map +1 -1
- package/dist/ts/frame/media-cache.d.ts +13 -2
- package/dist/ts/frame/media-cache.d.ts.map +1 -1
- package/dist/ts/frame/media-cache.js +24 -9
- package/dist/ts/frame/media-cache.js.map +1 -1
- package/dist/ts/frame/protocol.d.ts +9 -1
- package/dist/ts/frame/protocol.d.ts.map +1 -1
- package/dist/ts/frame/protocol.js +5 -0
- package/dist/ts/frame/protocol.js.map +1 -1
- 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/index.d.ts +2 -0
- package/dist/ts/index.d.ts.map +1 -1
- package/dist/ts/index.js +3 -0
- package/dist/ts/index.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/ui-config.d.ts +13 -3
- package/dist/ts/ui-config.d.ts.map +1 -1
- package/dist/ts/ui-config.js +14 -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 +4 -5
- 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 +9 -9
- 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 +1 -5
- package/src/components/editor/draft-media-config.ts +121 -65
- package/src/components/editor/preview-availability.ts +3 -0
- package/src/components/editors/DataEditor.tsx +65 -32
- package/src/components/fields/ImageField.tsx +2 -2
- package/src/components/media/AssetCard.tsx +4 -4
- package/src/components/media/AssetDetailSidebar.tsx +34 -21
- 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 +28 -23
- package/src/components/sandbox/ContentSandbox.tsx +3 -3
- package/src/convert-pages.ts +110 -0
- package/src/frame/editor-frame.ts +6 -2
- package/src/frame/media-cache.ts +28 -15
- package/src/frame/protocol.ts +10 -1
- package/src/hooks/index.ts +2 -0
- package/src/hooks/usePageConversion.ts +55 -0
- package/src/hooks/useUploadFlow.ts +44 -7
- package/src/index.ts +3 -0
- package/src/media/prepare-image-upload.ts +118 -0
- package/src/media/use-asset-serve-src.ts +22 -0
- package/src/ui-config.ts +23 -3
- package/src/utils/asset-url.ts +6 -0
- package/src/utils/data-entry-validation.ts +16 -0
|
@@ -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
|
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,9 @@ export { connectEditorFrame, type EditorFrame } from './frame/editor-frame.js';
|
|
|
26
26
|
export { awaitParentHello } from './frame/parent-hello.js';
|
|
27
27
|
// Export types from hooks (AssetEntry, etc.)
|
|
28
28
|
export type { AssetEntry, AssetUsage, FolderListing, FolderTreeNode } from './hooks/index.js';
|
|
29
|
+
export { buildUploadFormData } from './hooks/useUpload.js';
|
|
30
|
+
// Shrinks and checks an image before upload; the editor's parent page runs it on every upload it forwards
|
|
31
|
+
export { prepareImageUpload, prepareUploadForm } from './media/prepare-image-upload.js';
|
|
29
32
|
// Export configuration function
|
|
30
33
|
export {
|
|
31
34
|
configureUI,
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_ACCEPTED_FORMATS,
|
|
3
|
+
IMAGE_EXTENSION_MIME,
|
|
4
|
+
JPEG_FALLBACK_QUALITY,
|
|
5
|
+
MAX_IMAGE_EDGE,
|
|
6
|
+
tooLargeRefusal,
|
|
7
|
+
unreadableImageRefusal,
|
|
8
|
+
unsupportedTypeRefusal,
|
|
9
|
+
VIDEO_EXTENSIONS,
|
|
10
|
+
videoRefusal,
|
|
11
|
+
WEBP_QUALITY,
|
|
12
|
+
} from '@conloca/content-api-client';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The browser's half of the upload rules: a photo over 2560 px is shrunk, upright, and a file the site will not
|
|
16
|
+
* store is refused before any byte is sent. The editor runs it, and the editor's parent page runs it again for
|
|
17
|
+
* editor bundles built before it existed.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
type OutputType = 'image/webp' | 'image/jpeg' | 'image/png';
|
|
21
|
+
|
|
22
|
+
const OUTPUT_EXTENSION = { 'image/webp': 'webp', 'image/jpeg': 'jpg', 'image/png': 'png' } satisfies Record<
|
|
23
|
+
OutputType,
|
|
24
|
+
string
|
|
25
|
+
>;
|
|
26
|
+
|
|
27
|
+
/** Formats a canvas decodes and re-encodes; GIF, SVG and ICO are sent as they are. */
|
|
28
|
+
const SHRINKABLE: ReadonlySet<string> = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/avif']);
|
|
29
|
+
|
|
30
|
+
/** The size that fits `width`×`height` within `maxEdge` on its long edge, or `null` when it already fits. */
|
|
31
|
+
export function fitWithin(width: number, height: number, maxEdge: number): { width: number; height: number } | null {
|
|
32
|
+
const longEdge = Math.max(width, height);
|
|
33
|
+
if (longEdge <= maxEdge) return null;
|
|
34
|
+
const scale = maxEdge / longEdge;
|
|
35
|
+
return { width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)) };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `name` with its extension replaced by the one `outputType` is stored under. */
|
|
39
|
+
export function processedName(name: string, outputType: OutputType): string {
|
|
40
|
+
const dot = name.lastIndexOf('.');
|
|
41
|
+
return `${dot > 0 ? name.slice(0, dot) : name}.${OUTPUT_EXTENSION[outputType]}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function extensionOf(name: string): string {
|
|
45
|
+
const dot = name.lastIndexOf('.');
|
|
46
|
+
return dot < 0 ? '' : name.slice(dot + 1).toLowerCase();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The file to upload in place of `file`, shrunk when it is a photo over the edge; throws the refusal sentence. */
|
|
50
|
+
export async function prepareImageUpload(file: File, maxBytes: number): Promise<File> {
|
|
51
|
+
const extension = extensionOf(file.name);
|
|
52
|
+
if (VIDEO_EXTENSIONS.has(extension) || file.type.startsWith('video/')) throw new Error(videoRefusal(file.name));
|
|
53
|
+
const type = IMAGE_EXTENSION_MIME[extension];
|
|
54
|
+
if (type === undefined) throw new Error(unsupportedTypeRefusal(file.name, DEFAULT_ACCEPTED_FORMATS));
|
|
55
|
+
const prepared = SHRINKABLE.has(type) ? await shrink(file, type) : file;
|
|
56
|
+
if (prepared.size > maxBytes) throw new Error(tooLargeRefusal(file.name, prepared.size, maxBytes));
|
|
57
|
+
return prepared;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** `form` with its `file` prepared by {@link prepareImageUpload}; `alt` and `folder` are kept. */
|
|
61
|
+
export async function prepareUploadForm(form: FormData, maxBytes: number): Promise<FormData> {
|
|
62
|
+
const file = form.get('file');
|
|
63
|
+
// The server refuses a form without a file in its own words.
|
|
64
|
+
if (!(file instanceof File)) return form;
|
|
65
|
+
const prepared = await prepareImageUpload(file, maxBytes);
|
|
66
|
+
const next = new FormData();
|
|
67
|
+
for (const [key, value] of form) next.append(key, key === 'file' ? prepared : value);
|
|
68
|
+
return next;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function shrink(file: File, type: string): Promise<File> {
|
|
72
|
+
const bitmap = await decode(file);
|
|
73
|
+
if (!bitmap) throw new Error(unreadableImageRefusal(file.name));
|
|
74
|
+
try {
|
|
75
|
+
const size = fitWithin(bitmap.width, bitmap.height, MAX_IMAGE_EDGE);
|
|
76
|
+
if (!size) return file;
|
|
77
|
+
// Never larger than 2560×2560: inside the canvas area iOS allows.
|
|
78
|
+
const canvas = document.createElement('canvas');
|
|
79
|
+
canvas.width = size.width;
|
|
80
|
+
canvas.height = size.height;
|
|
81
|
+
const context = canvas.getContext('2d');
|
|
82
|
+
if (!context) throw new Error(unreadableImageRefusal(file.name));
|
|
83
|
+
context.imageSmoothingQuality = 'high';
|
|
84
|
+
context.drawImage(bitmap, 0, 0, size.width, size.height);
|
|
85
|
+
const encoded = await encode(canvas, type === 'image/png', file.name);
|
|
86
|
+
return new File([encoded.blob], processedName(file.name, encoded.type), { type: encoded.type });
|
|
87
|
+
} finally {
|
|
88
|
+
bitmap.close();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The decoded image, upright, or `null` when the bytes are not one the browser can read. */
|
|
93
|
+
async function decode(file: File): Promise<ImageBitmap | null> {
|
|
94
|
+
try {
|
|
95
|
+
return await createImageBitmap(file, { imageOrientation: 'from-image' });
|
|
96
|
+
} catch (error) {
|
|
97
|
+
// Older engines reject the options with a TypeError; they apply the EXIF orientation by default.
|
|
98
|
+
return error instanceof TypeError ? createImageBitmap(file).catch(() => null) : null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function encode(
|
|
103
|
+
canvas: HTMLCanvasElement,
|
|
104
|
+
png: boolean,
|
|
105
|
+
name: string,
|
|
106
|
+
): Promise<{ blob: Blob; type: OutputType }> {
|
|
107
|
+
if (png) return { blob: await toBlob(canvas, 'image/png', undefined, name), type: 'image/png' };
|
|
108
|
+
const webp = await toBlob(canvas, 'image/webp', WEBP_QUALITY, name);
|
|
109
|
+
if (webp.type === 'image/webp') return { blob: webp, type: 'image/webp' };
|
|
110
|
+
// Safari cannot encode WebP and answers PNG instead.
|
|
111
|
+
return { blob: await toBlob(canvas, 'image/jpeg', JPEG_FALLBACK_QUALITY, name), type: 'image/jpeg' };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function toBlob(canvas: HTMLCanvasElement, type: OutputType, quality: number | undefined, name: string): Promise<Blob> {
|
|
115
|
+
return new Promise((resolve, reject) =>
|
|
116
|
+
canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error(unreadableImageRefusal(name)))), type, quality),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AssetEntry } from '@conloca/content-api-client';
|
|
2
|
+
import { mediaSourceOf } from '../components/editor/draft-media-config.js';
|
|
3
|
+
import { useMediaObjectUrl } from '../frame/media-cache.js';
|
|
4
|
+
import type { MediaLoadInput } from '../frame/protocol.js';
|
|
5
|
+
import { getUIConfig } from '../ui-config.js';
|
|
6
|
+
import { assetPath, buildAssetServeUrl } from '../utils/asset-url.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The `src` of an asset thumbnail: with a loader, an object URL of the bytes its link names (an image in the site's
|
|
10
|
+
* folder loads from there, anything else from its upload address); without one, the serve URL.
|
|
11
|
+
*/
|
|
12
|
+
export function useAssetServeSrc(
|
|
13
|
+
assetsBasePath: string,
|
|
14
|
+
asset: Pick<AssetEntry, 'folder' | 'filename' | 'ref'>,
|
|
15
|
+
): string | undefined {
|
|
16
|
+
const source: MediaLoadInput = (asset.ref === undefined ? undefined : mediaSourceOf(asset.ref)) ?? {
|
|
17
|
+
route: 'serve',
|
|
18
|
+
path: assetPath(asset.folder, asset.filename),
|
|
19
|
+
};
|
|
20
|
+
const loaded = useMediaObjectUrl(source.route, source.path);
|
|
21
|
+
return getUIConfig().loadMedia ? loaded : buildAssetServeUrl(assetsBasePath, asset.folder, asset.filename);
|
|
22
|
+
}
|
package/src/ui-config.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* oxlint-disable no-underscore-dangle -- __UI_CONFIG__ is the existing browser-global compatibility contract read by embedded hosts. */
|
|
2
2
|
import type { ContentAPIClient, EditorDataBindings } from '@conloca/content-api-client';
|
|
3
3
|
import type { QueryClientConfig } from '@tanstack/react-query';
|
|
4
|
+
import type { PageConversionFacts } from './convert-pages.js';
|
|
4
5
|
import type { MediaLoadInput, PreviewOpenResult } from './frame/protocol.js';
|
|
5
6
|
import type { MediaBridge } from './media/types.js';
|
|
6
7
|
|
|
@@ -137,8 +138,6 @@ export interface UIConfig {
|
|
|
137
138
|
* literal default.
|
|
138
139
|
*/
|
|
139
140
|
assetsBasePath?: string;
|
|
140
|
-
/** Draft-image render base; render URLs must never replace canonical saved asset refs. */
|
|
141
|
-
draftAssetsBasePath?: string;
|
|
142
141
|
/**
|
|
143
142
|
* Base the STORED asset reference is built on (`buildAssetRef(folder,
|
|
144
143
|
* filename)`) — the sibling of `assetsBasePath`, and deliberately a
|
|
@@ -208,7 +207,9 @@ export interface UIConfig {
|
|
|
208
207
|
editorCss?: string;
|
|
209
208
|
/**
|
|
210
209
|
* Loads the bytes of a media file. When set, media surfaces never hand media
|
|
211
|
-
* URLs to `<img>`; they show object URLs of the loaded bytes.
|
|
210
|
+
* URLs to `<img>`; they show object URLs of the loaded bytes. Draft addresses
|
|
211
|
+
* (`/api/v1/assets/serve/<path>`) load through it from `serve` and show a
|
|
212
|
+
* transparent placeholder until their bytes arrive.
|
|
212
213
|
*/
|
|
213
214
|
loadMedia?: (input: MediaLoadInput) => Promise<Blob>;
|
|
214
215
|
/**
|
|
@@ -217,6 +218,11 @@ export interface UIConfig {
|
|
|
217
218
|
* dev-server endpoints do not exist.
|
|
218
219
|
*/
|
|
219
220
|
memoryRouter?: MemoryRouterConfig;
|
|
221
|
+
/**
|
|
222
|
+
* Set by the workspace editor around the framed editor or by the local
|
|
223
|
+
* integration; absent means the editor does not offer page conversion.
|
|
224
|
+
*/
|
|
225
|
+
pageConversion?: PageConversionFacts;
|
|
220
226
|
}
|
|
221
227
|
|
|
222
228
|
/** How the editor routes when a parent frame owns the address bar. */
|
|
@@ -257,6 +263,9 @@ declare global {
|
|
|
257
263
|
|
|
258
264
|
let config: UIConfig = { ...defaultConfig };
|
|
259
265
|
|
|
266
|
+
/** Fired on window when a parent frame changes `readOnly` or `pageConversion` after `configureUI`. */
|
|
267
|
+
export const EDITOR_CONFIG_CHANGED_EVENT = 'conloca:editor-config-changed';
|
|
268
|
+
|
|
260
269
|
export function configureUI(options: Partial<UIConfig>) {
|
|
261
270
|
// `readOnly` survives a reinstall unless the caller states a new answer.
|
|
262
271
|
//
|
|
@@ -294,6 +303,17 @@ export function setEditorReadOnly(readOnly: EditorWriteRefusal | null) {
|
|
|
294
303
|
// The runtime mirror `getUIConfig` reads from.
|
|
295
304
|
const runtimeConfig = window.__UI_CONFIG__;
|
|
296
305
|
if (runtimeConfig) runtimeConfig.readOnly = next;
|
|
306
|
+
window.dispatchEvent(new Event(EDITOR_CONFIG_CHANGED_EVENT));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Install (or clear, with `null`) the page conversion facts after `configureUI`, like {@link setEditorReadOnly}. */
|
|
310
|
+
export function setPageConversion(facts: PageConversionFacts | null) {
|
|
311
|
+
const next = facts ?? undefined;
|
|
312
|
+
config.pageConversion = next;
|
|
313
|
+
if (typeof window === 'undefined') return;
|
|
314
|
+
const runtimeConfig = window.__UI_CONFIG__;
|
|
315
|
+
if (runtimeConfig) runtimeConfig.pageConversion = next;
|
|
316
|
+
window.dispatchEvent(new Event(EDITOR_CONFIG_CHANGED_EVENT));
|
|
297
317
|
}
|
|
298
318
|
|
|
299
319
|
/**
|
package/src/utils/asset-url.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AssetEntry } from '@conloca/content-api-client';
|
|
1
2
|
import { getUIConfig } from '../ui-config.js';
|
|
2
3
|
|
|
3
4
|
/** An asset path relative to the media root; this is the identity accepted by asset routes. */
|
|
@@ -28,3 +29,8 @@ export function buildAssetServeUrl(basePath: string, folder: string | undefined,
|
|
|
28
29
|
export function buildAssetRef(folder: string | undefined, filename: string): string {
|
|
29
30
|
return buildAssetServeUrl(getUIConfig().assetRefBase ?? '/assets', folder, filename);
|
|
30
31
|
}
|
|
32
|
+
|
|
33
|
+
/** The link a pick of `asset` stores: the one the server gave it, else the site link `buildAssetRef` builds. */
|
|
34
|
+
export function assetRefOf(asset: Pick<AssetEntry, 'folder' | 'filename' | 'ref'>): string {
|
|
35
|
+
return asset.ref ?? buildAssetRef(asset.folder, asset.filename);
|
|
36
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DataCollectionEntry } from '@conloca/content-api-client';
|
|
2
|
+
import type { HostSchema } from '../host-schema.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Why the build would reject `values` as a data entry of `schema`, or null when it accepts them.
|
|
6
|
+
*
|
|
7
|
+
* Only the verdict is read: a pruning validator (`typia.plain.createValidatePrune`) drops undeclared
|
|
8
|
+
* keys from what it validates, so it gets a copy and the caller stores `values` as they are, keys
|
|
9
|
+
* written outside the form included.
|
|
10
|
+
*/
|
|
11
|
+
export function validateEntryData(schema: HostSchema, values: DataCollectionEntry['data']): string | null {
|
|
12
|
+
const result = schema.validate(structuredClone(values));
|
|
13
|
+
if (result.success) return null;
|
|
14
|
+
const fields = result.errors.map((error) => `${error.path.replace(/^\$input\./, '')} (expected ${error.expected})`);
|
|
15
|
+
return `Fix these fields before saving: ${fields.join(', ')}`;
|
|
16
|
+
}
|