@perkyzz/emvb 0.5.0 → 0.6.0
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 +4 -0
- package/THIRD_PARTY_NOTICES.md +23 -1
- package/licenses/exif-js.LICENSE +21 -0
- package/licenses/fflate.LICENSE +21 -0
- package/licenses/glfx.js.LICENSE +19 -0
- package/licenses/mini-photo-editor.LICENSE +21 -0
- package/licenses/photocn.LICENSE +21 -0
- package/package.json +2 -1
- package/src/admin/editor/Editor.tsx +6 -0
- package/src/admin/editor/collection-previews.ts +6 -1
- package/src/admin/editor/editor-chrome.tsx +49 -0
- package/src/admin/editor/editor-commands.ts +13 -0
- package/src/admin/editor/editor-css.ts +3 -0
- package/src/admin/editor/panels/ColorControl.tsx +5 -4
- package/src/admin/editor/panels/LayersPanel.tsx +13 -3
- package/src/admin/editor/panels/VariableSection.tsx +5 -3
- package/src/admin/editor/panels/settings/FieldControl.tsx +18 -7
- package/src/admin/editor/panels/settings/MediaPicker.tsx +154 -4
- package/src/admin/editor/panels/settings/RequiredFieldsNotice.tsx +76 -0
- package/src/admin/editor/panels/variable-values.ts +14 -0
- package/src/admin/editor/store.ts +7 -3
- package/src/admin/image-editor/BlurFocus.tsx +112 -0
- package/src/admin/image-editor/Canvas.tsx +261 -0
- package/src/admin/image-editor/CropOverlay.tsx +390 -0
- package/src/admin/image-editor/CurvesPanel.tsx +394 -0
- package/src/admin/image-editor/ExportDialog.tsx +354 -0
- package/src/admin/image-editor/ImageEditButton.tsx +83 -0
- package/src/admin/image-editor/ImageEditorDialog.tsx +786 -0
- package/src/admin/image-editor/MetadataPanel.tsx +183 -0
- package/src/admin/image-editor/announce.tsx +65 -0
- package/src/admin/image-editor/crop-geometry.ts +161 -0
- package/src/admin/image-editor/curve-math.ts +177 -0
- package/src/admin/image-editor/data-url-fetch.ts +59 -0
- package/src/admin/image-editor/engine.ts +55 -0
- package/src/admin/image-editor/exif.ts +82 -0
- package/src/admin/image-editor/flatten.ts +54 -0
- package/src/admin/image-editor/gpu-fit.ts +81 -0
- package/src/admin/image-editor/image-editor-css.ts +398 -0
- package/src/admin/image-editor/load.ts +109 -0
- package/src/admin/image-editor/panels.tsx +287 -0
- package/src/admin/image-editor/parts.tsx +565 -0
- package/src/admin/image-editor/save.ts +125 -0
- package/src/admin/image-editor/tone-panels.tsx +338 -0
- package/src/admin/image-editor/tools.ts +59 -0
- package/src/admin/image-editor/worker.ts +111 -0
- package/src/admin/media-api.ts +113 -3
- package/src/admin/ui.ts +5 -0
- package/src/astro/EmVBPage.astro +11 -2
- package/src/astro/render.ts +3 -0
- package/src/astro/resolve-theme.ts +15 -4
- package/src/astro/visitor.ts +4 -3
- package/src/constants.ts +1 -1
- package/src/core/arrange.ts +2 -0
- package/src/core/data/bindings.ts +12 -6
- package/src/core/design/classes.ts +5 -3
- package/src/core/design/diff.ts +3 -1
- package/src/core/design/merge.ts +3 -1
- package/src/core/design/transfer.ts +13 -2
- package/src/core/design/variables.ts +2 -1
- package/src/core/elements/dynamic-elements.ts +9 -1
- package/src/core/elements/form-elements.ts +16 -17
- package/src/core/elements/plain-text.ts +2 -2
- package/src/core/forms/binding.ts +148 -1
- package/src/core/history/diff.ts +32 -4
- package/src/core/image-edit/schema.ts +145 -0
- package/src/core/image-edit/summary.ts +58 -0
- package/src/core/index.ts +14 -0
- package/src/core/limits.ts +10 -0
- package/src/core/migrate/index.ts +3 -1
- package/src/core/perf/meter.ts +9 -1
- package/src/core/render/dynamic-post.ts +2 -1
- package/src/core/render/form.ts +24 -6
- package/src/core/render/index.ts +26 -5
- package/src/core/schema/layout.ts +11 -1
- package/src/core/share/link.ts +5 -2
- package/src/core/stable-json.ts +15 -0
- package/src/core/theme/dynamic.ts +21 -18
- package/src/core/theme/pagination.ts +30 -3
- package/src/core/theme/popup-rules.ts +10 -5
- package/src/mcp/ports.ts +8 -1
- package/src/mcp/read.ts +68 -1
- package/src/mcp/tools.ts +1 -1
- package/src/mcp/write.ts +100 -18
- package/src/server/agent-settings.ts +28 -3
- package/src/server/icon-routes.ts +8 -5
- package/src/server/mcp-routes.ts +68 -10
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Save copy" export step (W-485), following photocn's `export-dialog.tsx`
|
|
3
|
+
* (https://github.com/rbbydotdev/photocn, apps/www/src/registry/image-editor), MIT License,
|
|
4
|
+
* Copyright (c) 2026 rbbydotdev: format, quality, W × H with "Full size", an error alert, and
|
|
5
|
+
* Download next to the primary Save. Rebuilt with Kumo and EmVB tokens. EmVB changes: WebP 85%
|
|
6
|
+
* first, the size starts capped at 2560 px on the longest side, and a JPEG of a photo with a
|
|
7
|
+
* location gets "Remove location (GPS)", on by default. On phones the dialog sits at the bottom
|
|
8
|
+
* like photocn's drawer.
|
|
9
|
+
*/
|
|
10
|
+
import { Button, Dialog, Select, Switch } from "@cloudflare/kumo";
|
|
11
|
+
import {
|
|
12
|
+
DownloadSimpleIcon,
|
|
13
|
+
UploadSimpleIcon,
|
|
14
|
+
WarningCircleIcon,
|
|
15
|
+
XIcon,
|
|
16
|
+
} from "@phosphor-icons/react";
|
|
17
|
+
import * as React from "react";
|
|
18
|
+
import { SOLID_PRIMARY } from "../ui.ts";
|
|
19
|
+
import { withDataUrlFetch } from "./data-url-fetch.ts";
|
|
20
|
+
import { downloadBlob, flattenToJpeg, needsFlatten } from "./flatten.ts";
|
|
21
|
+
import { useImageEditor } from "./engine.ts";
|
|
22
|
+
import { ValueSlider } from "./parts.tsx";
|
|
23
|
+
import {
|
|
24
|
+
cappedWidth,
|
|
25
|
+
DEFAULT_MAX_SIDE,
|
|
26
|
+
EXPORT_FORMATS,
|
|
27
|
+
formatInfo,
|
|
28
|
+
outputSize,
|
|
29
|
+
type ExportFormat,
|
|
30
|
+
} from "./save.ts";
|
|
31
|
+
|
|
32
|
+
export type ExportSettings = {
|
|
33
|
+
format: ExportFormat;
|
|
34
|
+
/** 0..1, lossy formats only. */
|
|
35
|
+
quality?: number;
|
|
36
|
+
/** Set when the copy is smaller than the crop: its longest side. */
|
|
37
|
+
maxSide?: number;
|
|
38
|
+
stripGps: boolean;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** What photocn's `exportImage` returns, as far as saving needs it. */
|
|
42
|
+
export type ExportResult = { blob: Blob; width: number; height: number; type: string };
|
|
43
|
+
|
|
44
|
+
const MAX_PX = 16384;
|
|
45
|
+
|
|
46
|
+
export function ExportDialog({
|
|
47
|
+
open,
|
|
48
|
+
onOpenChange,
|
|
49
|
+
mainThread,
|
|
50
|
+
sourceHasGps,
|
|
51
|
+
sourceType = "",
|
|
52
|
+
downloadName = "image-edited.jpg",
|
|
53
|
+
onSave,
|
|
54
|
+
onClosed,
|
|
55
|
+
}: {
|
|
56
|
+
open: boolean;
|
|
57
|
+
onOpenChange: (open: boolean) => void;
|
|
58
|
+
/** Main-thread exports fetch `data:` URLs, which the admin CSP blocks without the shim. */
|
|
59
|
+
mainThread: boolean;
|
|
60
|
+
sourceHasGps: boolean;
|
|
61
|
+
/** The source's MIME type: non-JPEG sources may be transparent (W-495). */
|
|
62
|
+
sourceType?: string;
|
|
63
|
+
downloadName?: string;
|
|
64
|
+
onSave: (result: ExportResult, settings: ExportSettings) => Promise<void>;
|
|
65
|
+
onClosed?: () => void;
|
|
66
|
+
}) {
|
|
67
|
+
return (
|
|
68
|
+
<Dialog.Root
|
|
69
|
+
open={open}
|
|
70
|
+
onOpenChange={(next) => onOpenChange(next)}
|
|
71
|
+
onOpenChangeComplete={(next) => {
|
|
72
|
+
if (!next) onClosed?.();
|
|
73
|
+
}}
|
|
74
|
+
>
|
|
75
|
+
<Dialog className="emvb-ie-export">
|
|
76
|
+
{open ? (
|
|
77
|
+
<ExportForm
|
|
78
|
+
mainThread={mainThread}
|
|
79
|
+
sourceHasGps={sourceHasGps}
|
|
80
|
+
sourceType={sourceType}
|
|
81
|
+
downloadName={downloadName}
|
|
82
|
+
onSave={onSave}
|
|
83
|
+
/>
|
|
84
|
+
) : null}
|
|
85
|
+
</Dialog>
|
|
86
|
+
</Dialog.Root>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function ExportForm({
|
|
91
|
+
mainThread,
|
|
92
|
+
sourceHasGps,
|
|
93
|
+
sourceType,
|
|
94
|
+
downloadName,
|
|
95
|
+
onSave,
|
|
96
|
+
}: {
|
|
97
|
+
mainThread: boolean;
|
|
98
|
+
sourceHasGps: boolean;
|
|
99
|
+
sourceType: string;
|
|
100
|
+
downloadName: string;
|
|
101
|
+
onSave: (result: ExportResult, settings: ExportSettings) => Promise<void>;
|
|
102
|
+
}) {
|
|
103
|
+
const editor = useImageEditor();
|
|
104
|
+
const id = React.useId();
|
|
105
|
+
const full = editor.geometry.outputSize;
|
|
106
|
+
const [format, setFormat] = React.useState<ExportFormat>("webp");
|
|
107
|
+
const selected = formatInfo(format);
|
|
108
|
+
const [quality, setQuality] = React.useState<number>(selected.defaultQuality);
|
|
109
|
+
// Output width in px; the height follows the crop. null = full size.
|
|
110
|
+
const [width, setWidth] = React.useState<number | null>(() =>
|
|
111
|
+
full ? cappedWidth(full, DEFAULT_MAX_SIDE) : null,
|
|
112
|
+
);
|
|
113
|
+
const [stripGps, setStripGps] = React.useState(true);
|
|
114
|
+
const [pending, setPending] = React.useState<"download" | "save" | null>(null);
|
|
115
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
116
|
+
const [failedSave, setFailedSave] = React.useState(false);
|
|
117
|
+
const output = full ? outputSize(full, width) : null;
|
|
118
|
+
const canExport = editor.isReady && !editor.disabled;
|
|
119
|
+
const lossy = selected.lossy;
|
|
120
|
+
const jpegGps = format === "jpeg" && sourceHasGps;
|
|
121
|
+
|
|
122
|
+
const run = async (kind: "download" | "save") => {
|
|
123
|
+
setError(null);
|
|
124
|
+
setPending(kind);
|
|
125
|
+
const options = {
|
|
126
|
+
format,
|
|
127
|
+
quality: lossy ? quality : undefined,
|
|
128
|
+
width: output && width ? output.width : undefined,
|
|
129
|
+
height: output && width ? output.height : undefined,
|
|
130
|
+
};
|
|
131
|
+
const flatten = needsFlatten(format, sourceType);
|
|
132
|
+
const exportTask = async (): Promise<ExportResult> => {
|
|
133
|
+
if (!flatten) {
|
|
134
|
+
return kind === "download" ? editor.download(options) : editor.exportImage(options);
|
|
135
|
+
}
|
|
136
|
+
// W-495: render as PNG, then fill the transparent parts with white for the JPEG.
|
|
137
|
+
const png = await editor.exportImage({ ...options, format: "png", quality: undefined });
|
|
138
|
+
const blob = await flattenToJpeg(png.blob, options.quality);
|
|
139
|
+
if (kind === "download") downloadBlob(blob, downloadName);
|
|
140
|
+
return { ...png, blob, type: "image/jpeg" };
|
|
141
|
+
};
|
|
142
|
+
try {
|
|
143
|
+
const result = mainThread ? await withDataUrlFetch(exportTask) : await exportTask();
|
|
144
|
+
if (kind === "save") {
|
|
145
|
+
const settings: ExportSettings = {
|
|
146
|
+
format,
|
|
147
|
+
...(lossy ? { quality } : {}),
|
|
148
|
+
...(width && output ? { maxSide: Math.max(output.width, output.height) } : {}),
|
|
149
|
+
// Only JPEG copies carry EXIF over; WebP and PNG never have a location.
|
|
150
|
+
stripGps: format === "jpeg" ? stripGps : true,
|
|
151
|
+
};
|
|
152
|
+
await onSave(result, settings);
|
|
153
|
+
}
|
|
154
|
+
setFailedSave(false);
|
|
155
|
+
} catch (caught) {
|
|
156
|
+
setError(caught instanceof Error && caught.message ? caught.message : "Export failed.");
|
|
157
|
+
if (kind === "save") setFailedSave(true);
|
|
158
|
+
} finally {
|
|
159
|
+
setPending(null);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const setSide = (raw: string, side: "width" | "height") => {
|
|
164
|
+
if (!full) return;
|
|
165
|
+
const next = Number(raw);
|
|
166
|
+
if (!(next > 0)) {
|
|
167
|
+
setWidth(null);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const w = side === "width" ? Math.round(next) : Math.round((next * full.width) / full.height);
|
|
171
|
+
setWidth(Math.max(1, Math.min(MAX_PX, w)));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<div
|
|
176
|
+
className="emvb-ie emvb-ie-export-body"
|
|
177
|
+
data-slot="image-editor-export-dialog"
|
|
178
|
+
data-emvb-image-export=""
|
|
179
|
+
>
|
|
180
|
+
<div className="emvb-ie-export-head">
|
|
181
|
+
<Dialog.Title className="emvb-ie-export-title">Export image</Dialog.Title>
|
|
182
|
+
<Dialog.Description className="emvb-ie-export-desc">
|
|
183
|
+
Pick a format and size. Rendering always starts from the original.
|
|
184
|
+
</Dialog.Description>
|
|
185
|
+
<Dialog.Close
|
|
186
|
+
disabled={pending === "save"}
|
|
187
|
+
render={(props) => (
|
|
188
|
+
<Button
|
|
189
|
+
{...props}
|
|
190
|
+
type="button"
|
|
191
|
+
variant="ghost"
|
|
192
|
+
size="sm"
|
|
193
|
+
shape="square"
|
|
194
|
+
className="emvb-ie-btn emvb-ie-btn-icon emvb-ie-export-close"
|
|
195
|
+
aria-label="Close"
|
|
196
|
+
icon={<XIcon aria-hidden="true" />}
|
|
197
|
+
/>
|
|
198
|
+
)}
|
|
199
|
+
/>
|
|
200
|
+
</div>
|
|
201
|
+
<div className="emvb-ie-export-fields">
|
|
202
|
+
<div className="emvb-ie-export-row">
|
|
203
|
+
<span id={`${id}-format`} className="emvb-ie-label">
|
|
204
|
+
Format
|
|
205
|
+
</span>
|
|
206
|
+
<Select
|
|
207
|
+
aria-labelledby={`${id}-format`}
|
|
208
|
+
className="emvb-ie-select emvb-ie-export-format"
|
|
209
|
+
value={format}
|
|
210
|
+
items={Object.fromEntries(EXPORT_FORMATS.map((o) => [o.value, o.label]))}
|
|
211
|
+
onValueChange={(value) => {
|
|
212
|
+
const next = EXPORT_FORMATS.find((o) => o.value === value);
|
|
213
|
+
if (!next) return;
|
|
214
|
+
setFormat(next.value);
|
|
215
|
+
setQuality(next.defaultQuality);
|
|
216
|
+
}}
|
|
217
|
+
>
|
|
218
|
+
{EXPORT_FORMATS.map((o) => (
|
|
219
|
+
<Select.Option key={o.value} value={o.value}>
|
|
220
|
+
{o.label}
|
|
221
|
+
</Select.Option>
|
|
222
|
+
))}
|
|
223
|
+
</Select>
|
|
224
|
+
</div>
|
|
225
|
+
|
|
226
|
+
{lossy ? (
|
|
227
|
+
<ValueSlider
|
|
228
|
+
label="Quality"
|
|
229
|
+
name="export-quality"
|
|
230
|
+
value={Math.round(quality * 100)}
|
|
231
|
+
min={10}
|
|
232
|
+
max={100}
|
|
233
|
+
step={1}
|
|
234
|
+
format={(v) => `${Math.round(v)}%`}
|
|
235
|
+
onChange={(v) => setQuality(v / 100)}
|
|
236
|
+
/>
|
|
237
|
+
) : null}
|
|
238
|
+
|
|
239
|
+
{full && output ? (
|
|
240
|
+
<div className="emvb-ie-export-size">
|
|
241
|
+
<div className="emvb-ie-export-row">
|
|
242
|
+
<label htmlFor={`${id}-width`} className="emvb-ie-label">
|
|
243
|
+
Size
|
|
244
|
+
</label>
|
|
245
|
+
{width ? (
|
|
246
|
+
<button
|
|
247
|
+
type="button"
|
|
248
|
+
className="emvb-ie-link"
|
|
249
|
+
onClick={() => setWidth(null)}
|
|
250
|
+
data-emvb-image-export-full=""
|
|
251
|
+
>
|
|
252
|
+
Full size
|
|
253
|
+
</button>
|
|
254
|
+
) : null}
|
|
255
|
+
</div>
|
|
256
|
+
<div className="emvb-ie-export-dims">
|
|
257
|
+
<input
|
|
258
|
+
id={`${id}-width`}
|
|
259
|
+
className="emvb-ie-number emvb-ie-export-px"
|
|
260
|
+
aria-label="Width in pixels"
|
|
261
|
+
type="number"
|
|
262
|
+
inputMode="numeric"
|
|
263
|
+
min={1}
|
|
264
|
+
max={MAX_PX}
|
|
265
|
+
value={output.width}
|
|
266
|
+
data-emvb-image-export-width=""
|
|
267
|
+
onChange={(event) => setSide(event.currentTarget.value, "width")}
|
|
268
|
+
/>
|
|
269
|
+
<span aria-hidden="true">×</span>
|
|
270
|
+
<input
|
|
271
|
+
className="emvb-ie-number emvb-ie-export-px"
|
|
272
|
+
aria-label="Height in pixels"
|
|
273
|
+
type="number"
|
|
274
|
+
inputMode="numeric"
|
|
275
|
+
min={1}
|
|
276
|
+
max={MAX_PX}
|
|
277
|
+
value={output.height}
|
|
278
|
+
data-emvb-image-export-height=""
|
|
279
|
+
onChange={(event) => setSide(event.currentTarget.value, "height")}
|
|
280
|
+
/>
|
|
281
|
+
<span className="emvb-ie-subtle">px</span>
|
|
282
|
+
</div>
|
|
283
|
+
<p className="emvb-ie-subtle" data-emvb-image-export-note="">
|
|
284
|
+
{width
|
|
285
|
+
? `${Math.round((output.width / full.width) * 100)}% of the crop`
|
|
286
|
+
: "Full resolution"}
|
|
287
|
+
{format === "jpeg"
|
|
288
|
+
? jpegGps && stripGps
|
|
289
|
+
? " · EXIF kept, location removed"
|
|
290
|
+
: " · EXIF preserved"
|
|
291
|
+
: null}
|
|
292
|
+
</p>
|
|
293
|
+
</div>
|
|
294
|
+
) : null}
|
|
295
|
+
|
|
296
|
+
{jpegGps ? (
|
|
297
|
+
<Switch
|
|
298
|
+
label="Remove location (GPS)"
|
|
299
|
+
checked={stripGps}
|
|
300
|
+
onCheckedChange={(checked) => setStripGps(checked === true)}
|
|
301
|
+
data-emvb-image-export-gps=""
|
|
302
|
+
/>
|
|
303
|
+
) : null}
|
|
304
|
+
|
|
305
|
+
{error ? (
|
|
306
|
+
<div className="emvb-ie-alert" role="alert" data-emvb-image-export-error="">
|
|
307
|
+
<WarningCircleIcon aria-hidden="true" />
|
|
308
|
+
<span>{error}</span>
|
|
309
|
+
</div>
|
|
310
|
+
) : null}
|
|
311
|
+
</div>
|
|
312
|
+
<div className="emvb-ie-export-actions">
|
|
313
|
+
<Button
|
|
314
|
+
type="button"
|
|
315
|
+
variant="outline"
|
|
316
|
+
size="sm"
|
|
317
|
+
className="emvb-ie-btn"
|
|
318
|
+
disabled={!canExport || pending !== null}
|
|
319
|
+
icon={
|
|
320
|
+
pending === "download" ? (
|
|
321
|
+
<span className="emvb-ie-spinner emvb-ie-spinner-sm" aria-hidden="true" />
|
|
322
|
+
) : (
|
|
323
|
+
<DownloadSimpleIcon aria-hidden="true" />
|
|
324
|
+
)
|
|
325
|
+
}
|
|
326
|
+
onClick={() => void run("download")}
|
|
327
|
+
data-emvb-image-download=""
|
|
328
|
+
>
|
|
329
|
+
Download
|
|
330
|
+
</Button>
|
|
331
|
+
<Button
|
|
332
|
+
type="button"
|
|
333
|
+
variant="primary"
|
|
334
|
+
size="sm"
|
|
335
|
+
className="emvb-ie-btn"
|
|
336
|
+
style={SOLID_PRIMARY}
|
|
337
|
+
disabled={!canExport || pending !== null}
|
|
338
|
+
aria-busy={pending === "save" || undefined}
|
|
339
|
+
icon={
|
|
340
|
+
pending === "save" ? (
|
|
341
|
+
<span className="emvb-ie-spinner emvb-ie-spinner-sm" aria-hidden="true" />
|
|
342
|
+
) : (
|
|
343
|
+
<UploadSimpleIcon aria-hidden="true" />
|
|
344
|
+
)
|
|
345
|
+
}
|
|
346
|
+
onClick={() => void run("save")}
|
|
347
|
+
data-emvb-image-export-save=""
|
|
348
|
+
>
|
|
349
|
+
{pending === "save" ? "Saving…" : failedSave ? "Retry" : "Save copy"}
|
|
350
|
+
</Button>
|
|
351
|
+
</div>
|
|
352
|
+
</div>
|
|
353
|
+
);
|
|
354
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Button } from "@cloudflare/kumo";
|
|
2
|
+
import { MagicWandIcon } from "@phosphor-icons/react";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { BUTTON } from "../ui.ts";
|
|
5
|
+
import type { SavedCopy } from "./ImageEditorDialog.tsx";
|
|
6
|
+
|
|
7
|
+
/** The only static import of the image editor: everything behind it is a lazy chunk (W-482). */
|
|
8
|
+
const loadDialog = () => import("./ImageEditorDialog.tsx");
|
|
9
|
+
const ImageEditorDialog = React.lazy(loadDialog);
|
|
10
|
+
/** Starts fetching the editor chunk (hover, focus, a menu opening). */
|
|
11
|
+
export const prefetchImageEditor = () => void loadDialog().catch(() => undefined);
|
|
12
|
+
|
|
13
|
+
export type ImageEditSave = (file: File, copy: SavedCopy) => Promise<void>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The editor dialog, mounted on first open and kept for its closing animation. Used by the
|
|
17
|
+
* Edit image button, the media picker's Edit… and the command palette (W-486).
|
|
18
|
+
*/
|
|
19
|
+
export function ImageEditorLauncher({
|
|
20
|
+
open,
|
|
21
|
+
onOpenChange,
|
|
22
|
+
imageProps,
|
|
23
|
+
onSave,
|
|
24
|
+
onClosed,
|
|
25
|
+
}: {
|
|
26
|
+
open: boolean;
|
|
27
|
+
onOpenChange: (open: boolean) => void;
|
|
28
|
+
imageProps: Record<string, unknown>;
|
|
29
|
+
onSave?: ImageEditSave;
|
|
30
|
+
onClosed?: () => void;
|
|
31
|
+
}) {
|
|
32
|
+
const [mounted, setMounted] = React.useState(open);
|
|
33
|
+
if (open && !mounted) setMounted(true);
|
|
34
|
+
if (!mounted) return null;
|
|
35
|
+
return (
|
|
36
|
+
<React.Suspense fallback={null}>
|
|
37
|
+
<ImageEditorDialog
|
|
38
|
+
open={open}
|
|
39
|
+
onOpenChange={onOpenChange}
|
|
40
|
+
imageProps={imageProps}
|
|
41
|
+
onSave={onSave}
|
|
42
|
+
onClosed={onClosed}
|
|
43
|
+
/>
|
|
44
|
+
</React.Suspense>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** "Edit image" under the Image settings preview. Hover or focus starts fetching the editor. */
|
|
49
|
+
export function ImageEditButton({
|
|
50
|
+
imageProps,
|
|
51
|
+
onSave,
|
|
52
|
+
}: {
|
|
53
|
+
imageProps: Record<string, unknown>;
|
|
54
|
+
/** Upload the edited copy and apply it to the element (W-485). */
|
|
55
|
+
onSave?: ImageEditSave;
|
|
56
|
+
}) {
|
|
57
|
+
const [open, setOpen] = React.useState(false);
|
|
58
|
+
const buttonRef = React.useRef<HTMLButtonElement>(null);
|
|
59
|
+
return (
|
|
60
|
+
<>
|
|
61
|
+
<Button
|
|
62
|
+
ref={buttonRef}
|
|
63
|
+
type="button"
|
|
64
|
+
variant="secondary"
|
|
65
|
+
className={BUTTON}
|
|
66
|
+
icon={<MagicWandIcon aria-hidden="true" />}
|
|
67
|
+
data-emvb-image-edit=""
|
|
68
|
+
onPointerEnter={prefetchImageEditor}
|
|
69
|
+
onFocus={prefetchImageEditor}
|
|
70
|
+
onClick={() => setOpen(true)}
|
|
71
|
+
>
|
|
72
|
+
Edit image
|
|
73
|
+
</Button>
|
|
74
|
+
<ImageEditorLauncher
|
|
75
|
+
open={open}
|
|
76
|
+
onOpenChange={setOpen}
|
|
77
|
+
imageProps={imageProps}
|
|
78
|
+
onSave={onSave}
|
|
79
|
+
onClosed={() => buttonRef.current?.focus()}
|
|
80
|
+
/>
|
|
81
|
+
</>
|
|
82
|
+
);
|
|
83
|
+
}
|