@panneau/media 5.0.0-alpha.4 → 5.0.0-alpha.6
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 +94 -7
- package/dist/ImageItem.d.ts +23 -0
- package/dist/ImageItem.d.ts.map +1 -0
- package/dist/ImageItem.js +292 -0
- package/dist/ImageItem.js.map +1 -0
- package/dist/MediaField.css +127 -0
- package/dist/MediaField.d.ts +67 -15
- package/dist/MediaField.d.ts.map +1 -1
- package/dist/MediaField.js +354 -148
- package/dist/MediaField.js.map +1 -1
- package/dist/MediaField.module.js +28 -2
- package/dist/MediaField.module.js.map +1 -1
- package/dist/editor/ImageEditor.css +369 -0
- package/dist/editor/ImageEditor.d.ts +13 -0
- package/dist/editor/ImageEditor.d.ts.map +1 -0
- package/dist/editor/ImageEditor.js +461 -0
- package/dist/editor/ImageEditor.js.map +1 -0
- package/dist/editor/ImageEditor.module.js +86 -0
- package/dist/editor/ImageEditor.module.js.map +1 -0
- package/dist/editor/draw.d.ts +37 -0
- package/dist/editor/draw.d.ts.map +1 -0
- package/dist/editor/draw.js +93 -0
- package/dist/editor/draw.js.map +1 -0
- package/dist/editor/geometry.d.ts +54 -0
- package/dist/editor/geometry.d.ts.map +1 -0
- package/dist/editor/geometry.js +227 -0
- package/dist/editor/geometry.js.map +1 -0
- package/dist/editor/index.d.ts +5 -0
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.js +3 -0
- package/dist/editor/tools.d.ts +11 -0
- package/dist/editor/tools.d.ts.map +1 -0
- package/dist/editor/tools.js +1199 -0
- package/dist/editor/tools.js.map +1 -0
- package/dist/editor/types.d.ts +111 -0
- package/dist/editor/types.d.ts.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/picker/MediaDetails.d.ts +22 -0
- package/dist/picker/MediaDetails.d.ts.map +1 -0
- package/dist/picker/MediaDetails.js +368 -0
- package/dist/picker/MediaDetails.js.map +1 -0
- package/dist/picker/MediaGrid.d.ts +30 -0
- package/dist/picker/MediaGrid.d.ts.map +1 -0
- package/dist/picker/MediaGrid.js +260 -0
- package/dist/picker/MediaGrid.js.map +1 -0
- package/dist/picker/MediaPicker.css +642 -0
- package/dist/picker/MediaPicker.d.ts +9 -0
- package/dist/picker/MediaPicker.d.ts.map +1 -0
- package/dist/picker/MediaPicker.js +601 -0
- package/dist/picker/MediaPicker.js.map +1 -0
- package/dist/picker/MediaPicker.module.js +132 -0
- package/dist/picker/MediaPicker.module.js.map +1 -0
- package/dist/picker/PickerToolbar.d.ts +19 -0
- package/dist/picker/PickerToolbar.d.ts.map +1 -0
- package/dist/picker/PickerToolbar.js +266 -0
- package/dist/picker/PickerToolbar.js.map +1 -0
- package/dist/picker/UploadPanel.d.ts +22 -0
- package/dist/picker/UploadPanel.d.ts.map +1 -0
- package/dist/picker/UploadPanel.js +186 -0
- package/dist/picker/UploadPanel.js.map +1 -0
- package/dist/picker/format.d.ts +18 -0
- package/dist/picker/format.d.ts.map +1 -0
- package/dist/picker/format.js +64 -0
- package/dist/picker/format.js.map +1 -0
- package/dist/picker/index.d.ts +3 -0
- package/dist/picker/index.d.ts.map +1 -0
- package/dist/picker/index.js +1 -0
- package/dist/picker/types.d.ts +119 -0
- package/dist/picker/types.d.ts.map +1 -0
- package/dist/picker/useMediaBrowser.d.ts +50 -0
- package/dist/picker/useMediaBrowser.d.ts.map +1 -0
- package/dist/picker/useMediaBrowser.js +314 -0
- package/dist/picker/useMediaBrowser.js.map +1 -0
- package/dist/plugin.d.ts +1 -1
- package/dist/plugin.js +5 -2
- package/dist/plugin.js.map +1 -1
- package/dist/resourceSource.d.ts +21 -0
- package/dist/resourceSource.d.ts.map +1 -0
- package/dist/resourceSource.js +99 -0
- package/dist/resourceSource.js.map +1 -0
- package/package.json +18 -7
package/dist/MediaField.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import MediaField_module_default from "./MediaField.module.js";
|
|
2
|
+
import { ImageItem, editedImageOf } from "./ImageItem.js";
|
|
3
|
+
import { useImageEditorTools } from "./editor/tools.js";
|
|
4
|
+
import { ImageEditorDialog } from "./editor/ImageEditor.js";
|
|
5
|
+
import { MediaPicker } from "./picker/MediaPicker.js";
|
|
6
|
+
import { createResourceMediaSource } from "./resourceSource.js";
|
|
2
7
|
import { c } from "react/compiler-runtime";
|
|
3
|
-
import { FileImage, X } from "lucide-react";
|
|
8
|
+
import { FileImage, Images, X } from "lucide-react";
|
|
4
9
|
import { useEffect, useRef, useState } from "react";
|
|
5
10
|
import { usePanneau } from "@panneau/core";
|
|
6
11
|
import { useOptionalDataAdapter } from "@panneau/data/query";
|
|
7
12
|
import { FilePicker, UploadList, formatSize, getAccept, getFieldProps, getMaxSize, useFieldsContext, useRejectionMessage, useUploads } from "@panneau/fields";
|
|
8
13
|
import { fieldsMessages, mediaMessages } from "@panneau/intl";
|
|
9
|
-
import {
|
|
14
|
+
import { getMediaLibraries } from "@panneau/protocol/runtime";
|
|
15
|
+
import { Badge, Button, Fieldset, IconButton, Spinner, usePanneauIntl } from "@panneau/ui";
|
|
10
16
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
11
17
|
//#region src/MediaField.tsx
|
|
12
18
|
/**
|
|
@@ -16,10 +22,11 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
|
16
22
|
function toMediaReference(value, itemTitle = "name") {
|
|
17
23
|
if (typeof value === "string" || typeof value === "number") return { id: String(value) };
|
|
18
24
|
if (typeof value !== "object" || value === null) return null;
|
|
19
|
-
const { id, label, media, transform } = value;
|
|
25
|
+
const { id, resource, label, media, transform } = value;
|
|
20
26
|
if (typeof id !== "string" && typeof id !== "number") return null;
|
|
21
27
|
const title = value[itemTitle];
|
|
22
28
|
const reference = { id: String(id) };
|
|
29
|
+
if (typeof resource === "string") reference.resource = resource;
|
|
23
30
|
if (typeof label === "string") reference.label = label;
|
|
24
31
|
else if (typeof title === "string") reference.label = title;
|
|
25
32
|
if (typeof media === "object" && media !== null) reference.media = media;
|
|
@@ -30,26 +37,28 @@ function toMediaReference(value, itemTitle = "name") {
|
|
|
30
37
|
function toMediaReferences(value, itemTitle) {
|
|
31
38
|
return (Array.isArray(value) ? value : [value]).map((entry) => toMediaReference(entry, itemTitle)).filter((reference) => reference !== null);
|
|
32
39
|
}
|
|
40
|
+
/** The key of a reference: its library and its id, which are unique only together. */
|
|
41
|
+
var keyOf = (reference, resource) => `${reference.resource ?? resource}\n${reference.id}`;
|
|
33
42
|
/** Whether the file of a media is still being processed. */
|
|
34
43
|
function isProcessing(reference) {
|
|
35
44
|
const status = reference.media?.status;
|
|
36
45
|
return status === "pending" || status === "processing";
|
|
37
46
|
}
|
|
38
47
|
/**
|
|
39
|
-
* The library
|
|
40
|
-
* definition.
|
|
48
|
+
* The first library a `media` field allows (decision 0027): the one its `resource` names, the
|
|
49
|
+
* first one it lists, or the first resource of kind `media` of the definition when it names none.
|
|
50
|
+
* The field uploads to it.
|
|
41
51
|
*/
|
|
42
52
|
function getLibrary(definition, resource) {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
return libraries.length === 1 ? libraries[0] ?? null : null;
|
|
53
|
+
const [first] = getMediaLibraries(definition, { resource });
|
|
54
|
+
return definition?.resources.find((candidate) => candidate.id === first) ?? null;
|
|
46
55
|
}
|
|
47
56
|
/**
|
|
48
57
|
* Reads media again while they are being ingested, until they are ready or failed, and calls
|
|
49
58
|
* `onRefresh` with the media that changed.
|
|
50
59
|
*/
|
|
51
60
|
function useMediaRefresh(media, resource, adapter, interval, onRefresh) {
|
|
52
|
-
const $ = c(
|
|
61
|
+
const $ = c(16);
|
|
53
62
|
const latest = useRef(onRefresh);
|
|
54
63
|
let t0;
|
|
55
64
|
if ($[0] !== onRefresh) {
|
|
@@ -61,70 +70,96 @@ function useMediaRefresh(media, resource, adapter, interval, onRefresh) {
|
|
|
61
70
|
} else t0 = $[1];
|
|
62
71
|
useEffect(t0);
|
|
63
72
|
let t1;
|
|
64
|
-
if ($[2] !== media) {
|
|
65
|
-
|
|
73
|
+
if ($[2] !== media || $[3] !== resource) {
|
|
74
|
+
let t2;
|
|
75
|
+
if ($[5] !== resource) {
|
|
76
|
+
t2 = (item) => JSON.stringify([
|
|
77
|
+
item.resource ?? resource,
|
|
78
|
+
item.id,
|
|
79
|
+
item.media?.status ?? ""
|
|
80
|
+
]);
|
|
81
|
+
$[5] = resource;
|
|
82
|
+
$[6] = t2;
|
|
83
|
+
} else t2 = $[6];
|
|
84
|
+
t1 = media.filter(isProcessing).map(t2).join("\n");
|
|
66
85
|
$[2] = media;
|
|
67
|
-
$[3] =
|
|
68
|
-
|
|
86
|
+
$[3] = resource;
|
|
87
|
+
$[4] = t1;
|
|
88
|
+
} else t1 = $[4];
|
|
69
89
|
const key = t1;
|
|
70
90
|
let t2;
|
|
71
|
-
|
|
72
|
-
if ($[4] !== adapter || $[5] !== interval || $[6] !== key || $[7] !== resource) {
|
|
91
|
+
if ($[7] !== adapter || $[8] !== interval || $[9] !== key) {
|
|
73
92
|
t2 = () => {
|
|
74
93
|
if (adapter === null || key === "") return;
|
|
75
|
-
const
|
|
94
|
+
const entries = key.split("\n").map(_temp);
|
|
76
95
|
const controller = new AbortController();
|
|
77
96
|
const timer = setTimeout(() => {
|
|
78
|
-
Promise.all(
|
|
97
|
+
Promise.all(entries.map((t3) => {
|
|
98
|
+
const { library: library_0, id: id_0 } = t3;
|
|
99
|
+
return adapter.get(library_0, id_0, { signal: controller.signal }).then((item_0) => ({
|
|
100
|
+
resource: library_0,
|
|
101
|
+
item: item_0
|
|
102
|
+
}));
|
|
103
|
+
})).then((refreshed) => {
|
|
79
104
|
if (!controller.signal.aborted) latest.current(refreshed);
|
|
80
|
-
}).catch(
|
|
105
|
+
}).catch(_temp2);
|
|
81
106
|
}, interval);
|
|
82
107
|
return () => {
|
|
83
108
|
clearTimeout(timer);
|
|
84
109
|
controller.abort();
|
|
85
110
|
};
|
|
86
111
|
};
|
|
112
|
+
$[7] = adapter;
|
|
113
|
+
$[8] = interval;
|
|
114
|
+
$[9] = key;
|
|
115
|
+
$[10] = t2;
|
|
116
|
+
} else t2 = $[10];
|
|
117
|
+
let t3;
|
|
118
|
+
if ($[11] !== adapter || $[12] !== interval || $[13] !== key || $[14] !== resource) {
|
|
87
119
|
t3 = [
|
|
88
120
|
adapter,
|
|
89
121
|
resource,
|
|
90
122
|
key,
|
|
91
123
|
interval
|
|
92
124
|
];
|
|
93
|
-
$[
|
|
94
|
-
$[
|
|
95
|
-
$[
|
|
96
|
-
$[
|
|
97
|
-
$[
|
|
98
|
-
|
|
99
|
-
} else {
|
|
100
|
-
t2 = $[8];
|
|
101
|
-
t3 = $[9];
|
|
102
|
-
}
|
|
125
|
+
$[11] = adapter;
|
|
126
|
+
$[12] = interval;
|
|
127
|
+
$[13] = key;
|
|
128
|
+
$[14] = resource;
|
|
129
|
+
$[15] = t3;
|
|
130
|
+
} else t3 = $[15];
|
|
103
131
|
useEffect(t2, t3);
|
|
104
132
|
}
|
|
105
133
|
/**
|
|
106
|
-
* Media of
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
134
|
+
* Media of libraries (the `media` type, decisions 0026 and 0027). "Choose media" ("Add media"
|
|
135
|
+
* with `multiple`) opens the media picker on the `sources`, and a file chosen or dropped becomes
|
|
136
|
+
* a media of the library, an item of its resource (`create(resource, { file })`), sent directly
|
|
137
|
+
* to storage first when there is an upload source. Values are references to the media, with
|
|
138
|
+
* their library, their `media` and their `transform` for this use. The field shows each media
|
|
139
|
+
* with its thumbnail and its status, and reads it again until its file is processed.
|
|
112
140
|
*/
|
|
113
|
-
function
|
|
114
|
-
function
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
141
|
+
function _temp2() {}
|
|
142
|
+
function _temp(entry) {
|
|
143
|
+
const [library, id] = JSON.parse(entry);
|
|
144
|
+
return {
|
|
145
|
+
library,
|
|
146
|
+
id
|
|
147
|
+
};
|
|
119
148
|
}
|
|
120
149
|
function MediaField(props) {
|
|
121
150
|
const intl = usePanneauIntl();
|
|
122
151
|
const contextAdapter = useOptionalDataAdapter();
|
|
123
152
|
const context = useFieldsContext();
|
|
124
|
-
const { adapter = contextAdapter, uploadSource = context.uploadSource, accept = null, maxSize = null, refreshInterval = 3e3, resource, itemTitle = "name", label, description, errors = [], disabled, readOnly, required, className, labelClassName, descriptionClassName, errorsClassName } = props;
|
|
153
|
+
const { adapter = contextAdapter, uploadSource = context.uploadSource, accept = null, maxSize = null, refreshInterval = 3e3, resource, itemTitle = "name", sources = [], stateKey, label, description, errors = [], disabled, readOnly, required, className, labelClassName, descriptionClassName, errorsClassName } = props;
|
|
125
154
|
const multiple = props.multiple === true;
|
|
126
155
|
const media = toMediaReferences(props.value, itemTitle);
|
|
127
|
-
const
|
|
156
|
+
const enabled = disabled !== true && readOnly !== true;
|
|
157
|
+
const canUpload = adapter !== null && enabled;
|
|
158
|
+
const canPick = sources.length > 0 && enabled;
|
|
159
|
+
const [picking, setPicking] = useState(false);
|
|
160
|
+
const imageOptions = props.image ?? null;
|
|
161
|
+
const [editing, setEditing] = useState(null);
|
|
162
|
+
const editorTools = useImageEditorTools();
|
|
128
163
|
const [problems, setProblems] = useState([]);
|
|
129
164
|
const [creating, setCreating] = useState([]);
|
|
130
165
|
const rejectionMessage = useRejectionMessage(maxSize);
|
|
@@ -137,8 +172,13 @@ function MediaField(props) {
|
|
|
137
172
|
if (props.multiple === true) props.onChange?.(next.length > 0 ? next : null);
|
|
138
173
|
else props.onChange?.(next[0] ?? null);
|
|
139
174
|
};
|
|
175
|
+
/** Adds picked media: a single pick replaces the media, several are added to the list, once. */
|
|
176
|
+
const add = (picked) => {
|
|
177
|
+
const known = new Set(latest.current.map((entry) => keyOf(entry, resource)));
|
|
178
|
+
set(multiple ? [...latest.current, ...picked.filter((entry_0) => !known.has(keyOf(entry_0, resource)))] : picked.slice(0, 1));
|
|
179
|
+
};
|
|
140
180
|
useMediaRefresh(media, resource, adapter, refreshInterval, (refreshed) => set(latest.current.map((reference) => {
|
|
141
|
-
const item = refreshed.find((candidate) => candidate.id === reference.id);
|
|
181
|
+
const item = refreshed.find((candidate) => candidate.item.id === reference.id && candidate.resource === (reference.resource ?? resource))?.item;
|
|
142
182
|
const fresh = item !== void 0 ? toMediaReference(item, itemTitle) : null;
|
|
143
183
|
return fresh !== null ? {
|
|
144
184
|
...reference,
|
|
@@ -155,7 +195,10 @@ function MediaField(props) {
|
|
|
155
195
|
const name = file.name;
|
|
156
196
|
setCreating((current) => [...current, name]);
|
|
157
197
|
adapter.create(resource, { file: file instanceof File ? file : { upload: file.upload } }).then((item_0) => {
|
|
158
|
-
const created =
|
|
198
|
+
const created = {
|
|
199
|
+
...toMediaReference(item_0, itemTitle) ?? { id: item_0.id },
|
|
200
|
+
resource
|
|
201
|
+
};
|
|
159
202
|
set(multiple ? [...latest.current, created] : [created]);
|
|
160
203
|
}).catch((error) => setProblems((current_0) => [...current_0, intl.formatMessage(fieldsMessages.uploadFailed, { error: `${name}: ${error instanceof Error ? error.message : String(error)}` })])).finally(() => setCreating((current_1) => {
|
|
161
204
|
const at = current_1.indexOf(name);
|
|
@@ -165,23 +208,55 @@ function MediaField(props) {
|
|
|
165
208
|
onRejected: (file_0, reason) => setProblems((current_2) => [...current_2, rejectionMessage(file_0, reason)])
|
|
166
209
|
});
|
|
167
210
|
const statusBadge = (item_1) => {
|
|
168
|
-
if (item_1.media?.status === "failed")
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
211
|
+
if (item_1.media?.status === "failed") {
|
|
212
|
+
const reason_0 = item_1.media.error?.message;
|
|
213
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
214
|
+
className: MediaField_module_default.failed,
|
|
215
|
+
children: [/* @__PURE__ */ jsx(Badge, {
|
|
216
|
+
tone: "danger",
|
|
217
|
+
children: intl.formatMessage(mediaMessages.statusFailed)
|
|
218
|
+
}), typeof reason_0 === "string" && reason_0 !== "" ? /* @__PURE__ */ jsx("span", {
|
|
219
|
+
className: MediaField_module_default.failure,
|
|
220
|
+
title: reason_0,
|
|
221
|
+
children: reason_0
|
|
222
|
+
}) : null]
|
|
223
|
+
});
|
|
224
|
+
}
|
|
172
225
|
if (isProcessing(item_1)) return /* @__PURE__ */ jsx(Badge, {
|
|
173
226
|
tone: "warning",
|
|
174
227
|
children: intl.formatMessage(item_1.media?.status === "pending" ? mediaMessages.statusPending : mediaMessages.statusProcessing)
|
|
175
228
|
});
|
|
176
229
|
return null;
|
|
177
230
|
};
|
|
178
|
-
|
|
231
|
+
const imageRatios = imageOptions?.ratios ?? [];
|
|
232
|
+
const constraints = imageOptions === null ? [] : [...imageRatios.length > 0 ? [intl.formatMessage(imageOptions.enforceRatios === true ? mediaMessages.imageRatios : mediaMessages.imageSuggestedRatios, { ratios: intl.formatList(imageRatios, { type: "disjunction" }) })] : [], ...imageOptions.cropRequired === true ? [intl.formatMessage(mediaMessages.imageCropRequired)] : []];
|
|
233
|
+
const edited = editing === null ? null : "key" in editing ? media.find((entry_1) => keyOf(entry_1, resource) === editing.key) ?? null : editing.picked;
|
|
234
|
+
const editedImage = edited !== null ? editedImageOf(edited) : null;
|
|
235
|
+
const tools = [
|
|
236
|
+
editorTools.crop,
|
|
237
|
+
...imageOptions?.rotate === false ? [] : [editorTools.rotate],
|
|
238
|
+
...imageOptions?.focalPoint === false ? [] : [editorTools.focalPoint]
|
|
239
|
+
];
|
|
240
|
+
const unavailable = adapter === null && disabled !== true && readOnly !== true;
|
|
241
|
+
const chooseLabel = imageOptions !== null ? multiple ? mediaMessages.addImages : mediaMessages.chooseImage : multiple ? mediaMessages.addMedia : mediaMessages.chooseMedia;
|
|
242
|
+
const cropStep = imageOptions?.cropRequired === true && !multiple;
|
|
243
|
+
const withTransform = (reference_0, transform) => {
|
|
244
|
+
const { transform: _previous, ...rest } = reference_0;
|
|
245
|
+
return transform === null || transform === void 0 ? rest : {
|
|
246
|
+
...rest,
|
|
247
|
+
transform
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
return /* @__PURE__ */ jsxs(Fieldset, {
|
|
179
251
|
legend: label,
|
|
180
|
-
description:
|
|
252
|
+
description: description !== void 0 || unavailable || constraints.length > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
181
253
|
description,
|
|
182
|
-
" ",
|
|
183
|
-
|
|
184
|
-
|
|
254
|
+
unavailable ? `${description !== void 0 ? " " : ""}${intl.formatMessage(mediaMessages.unavailable)}` : null,
|
|
255
|
+
constraints.length > 0 ? /* @__PURE__ */ jsx("span", {
|
|
256
|
+
className: MediaField_module_default.constraints,
|
|
257
|
+
children: constraints.map((constraint) => /* @__PURE__ */ jsx(Badge, { children: constraint }, constraint))
|
|
258
|
+
}) : null
|
|
259
|
+
] }) : void 0,
|
|
185
260
|
errors: [...errors, ...problems],
|
|
186
261
|
isRequired: required,
|
|
187
262
|
isDisabled: disabled,
|
|
@@ -189,117 +264,245 @@ function MediaField(props) {
|
|
|
189
264
|
legendClassName: labelClassName,
|
|
190
265
|
descriptionClassName,
|
|
191
266
|
errorsClassName,
|
|
192
|
-
children:
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
267
|
+
children: [
|
|
268
|
+
/* @__PURE__ */ jsxs("div", {
|
|
269
|
+
className: MediaField_module_default.field,
|
|
270
|
+
children: [
|
|
271
|
+
media.length > 0 && imageOptions !== null ? /* @__PURE__ */ jsx("ul", {
|
|
272
|
+
className: multiple ? MediaField_module_default.gallery : MediaField_module_default.media,
|
|
273
|
+
children: media.map((item_2) => /* @__PURE__ */ jsx(ImageItem, {
|
|
274
|
+
reference: item_2,
|
|
275
|
+
tile: multiple,
|
|
276
|
+
cropRequired: imageOptions.cropRequired === true,
|
|
277
|
+
status: statusBadge(item_2),
|
|
278
|
+
onEdit: enabled ? () => setEditing({ key: keyOf(item_2, resource) }) : null,
|
|
279
|
+
onRemove: canUpload || canPick ? () => set(latest.current.filter((entry_2) => keyOf(entry_2, resource) !== keyOf(item_2, resource))) : null
|
|
280
|
+
}, keyOf(item_2, resource)))
|
|
281
|
+
}) : media.length > 0 ? /* @__PURE__ */ jsx("ul", {
|
|
282
|
+
className: MediaField_module_default.media,
|
|
283
|
+
children: media.map((item_3) => {
|
|
284
|
+
const name_0 = item_3.label ?? item_3.id;
|
|
285
|
+
const size = item_3.media?.size;
|
|
286
|
+
return /* @__PURE__ */ jsxs("li", {
|
|
287
|
+
className: MediaField_module_default.item,
|
|
288
|
+
children: [
|
|
289
|
+
/* @__PURE__ */ jsx("span", {
|
|
290
|
+
className: MediaField_module_default.thumbnail,
|
|
291
|
+
children: typeof item_3.media?.thumbnailUrl === "string" ? /* @__PURE__ */ jsx("img", {
|
|
292
|
+
src: item_3.media.thumbnailUrl,
|
|
293
|
+
alt: ""
|
|
294
|
+
}) : /* @__PURE__ */ jsx(FileImage, { "aria-hidden": true })
|
|
295
|
+
}),
|
|
296
|
+
/* @__PURE__ */ jsxs("span", {
|
|
297
|
+
className: MediaField_module_default.details,
|
|
298
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
299
|
+
className: MediaField_module_default.name,
|
|
300
|
+
children: name_0
|
|
301
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
302
|
+
className: MediaField_module_default.meta,
|
|
303
|
+
children: [
|
|
304
|
+
item_3.media !== void 0 ? /* @__PURE__ */ jsx("span", { children: item_3.media.type }) : null,
|
|
305
|
+
typeof size === "number" ? /* @__PURE__ */ jsx("span", { children: formatSize(size, intl.locale) }) : null,
|
|
306
|
+
statusBadge(item_3)
|
|
307
|
+
]
|
|
308
|
+
})]
|
|
309
|
+
}),
|
|
310
|
+
canUpload || canPick ? /* @__PURE__ */ jsx(IconButton, {
|
|
311
|
+
icon: X,
|
|
312
|
+
size: "sm",
|
|
313
|
+
label: intl.formatMessage(fieldsMessages.removeFile, { name: name_0 }),
|
|
314
|
+
onPress: () => set(latest.current.filter((entry_3) => keyOf(entry_3, resource) !== keyOf(item_3, resource)))
|
|
315
|
+
}) : null
|
|
316
|
+
]
|
|
317
|
+
}, keyOf(item_3, resource));
|
|
318
|
+
})
|
|
319
|
+
}) : creating.length === 0 && upload.items.length === 0 ? /* @__PURE__ */ jsx("span", {
|
|
320
|
+
className: MediaField_module_default.none,
|
|
321
|
+
children: intl.formatMessage(imageOptions !== null ? mediaMessages.noImage : mediaMessages.noMedia)
|
|
322
|
+
}) : null,
|
|
323
|
+
/* @__PURE__ */ jsx(UploadList, {
|
|
324
|
+
items: upload.items,
|
|
325
|
+
onPause: upload.pause,
|
|
326
|
+
onResume: upload.resume,
|
|
327
|
+
onCancel: upload.cancel,
|
|
328
|
+
onRetry: upload.retry
|
|
329
|
+
}),
|
|
330
|
+
creating.length > 0 ? /* @__PURE__ */ jsxs("span", {
|
|
331
|
+
className: MediaField_module_default.meta,
|
|
332
|
+
role: "status",
|
|
333
|
+
children: [
|
|
334
|
+
/* @__PURE__ */ jsx(Spinner, { size: "sm" }),
|
|
335
|
+
intl.formatMessage(fieldsMessages.uploadCompleting),
|
|
336
|
+
" ",
|
|
337
|
+
creating.join(", ")
|
|
338
|
+
]
|
|
339
|
+
}) : null,
|
|
340
|
+
canPick || canUpload ? /* @__PURE__ */ jsxs("div", {
|
|
341
|
+
className: MediaField_module_default.actions,
|
|
342
|
+
children: [canPick ? /* @__PURE__ */ jsx(Button, {
|
|
343
|
+
size: "sm",
|
|
344
|
+
appearance: "outline",
|
|
345
|
+
icon: Images,
|
|
346
|
+
onPress: () => setPicking(true),
|
|
347
|
+
children: intl.formatMessage(chooseLabel)
|
|
348
|
+
}) : null, canUpload ? /* @__PURE__ */ jsx(FilePicker, {
|
|
349
|
+
accept,
|
|
350
|
+
multiple,
|
|
351
|
+
className: MediaField_module_default.upload,
|
|
352
|
+
...canPick ? { label: intl.formatMessage(imageOptions !== null ? multiple ? mediaMessages.uploadImages : mediaMessages.uploadImage : multiple ? mediaMessages.uploadFiles : mediaMessages.uploadFile) } : {},
|
|
353
|
+
onSelect: (selected) => {
|
|
354
|
+
setProblems([]);
|
|
355
|
+
upload.add(selected);
|
|
356
|
+
}
|
|
357
|
+
}) : null]
|
|
358
|
+
}) : null
|
|
359
|
+
]
|
|
360
|
+
}),
|
|
361
|
+
canPick ? /* @__PURE__ */ jsx(MediaPicker, {
|
|
362
|
+
isOpen: picking,
|
|
363
|
+
onOpenChange: setPicking,
|
|
364
|
+
sources,
|
|
365
|
+
multiple,
|
|
366
|
+
accept,
|
|
367
|
+
maxSize,
|
|
368
|
+
value: media,
|
|
369
|
+
title: intl.formatMessage(chooseLabel),
|
|
370
|
+
...cropStep ? { confirmLabel: intl.formatMessage(mediaMessages.imageNextCrop) } : {},
|
|
371
|
+
uploadSource,
|
|
372
|
+
refreshInterval,
|
|
373
|
+
...stateKey !== void 0 ? { stateKey } : {},
|
|
374
|
+
onSelect: (picked_0) => {
|
|
375
|
+
setProblems([]);
|
|
376
|
+
const [first] = picked_0;
|
|
377
|
+
if (cropStep && picked_0.length === 1 && first !== void 0 && editedImageOf(first) !== null) {
|
|
378
|
+
setEditing({ picked: first });
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
add(picked_0);
|
|
382
|
+
}
|
|
383
|
+
}) : null,
|
|
384
|
+
imageOptions !== null && edited !== null && editedImage !== null && editing !== null ? /* @__PURE__ */ jsx(ImageEditorDialog, {
|
|
385
|
+
isOpen: true,
|
|
386
|
+
onOpenChange: (isOpen) => {
|
|
387
|
+
if (!isOpen) setEditing(null);
|
|
388
|
+
},
|
|
389
|
+
image: editedImage,
|
|
390
|
+
value: "key" in editing ? edited.transform ?? null : null,
|
|
391
|
+
base: imageOptions.transformBase === "original" ? "original" : "media",
|
|
392
|
+
canChooseBase: imageOptions.transformBase === "choose",
|
|
393
|
+
tools,
|
|
394
|
+
ratios: imageRatios,
|
|
395
|
+
enforceRatios: imageOptions.enforceRatios === true,
|
|
396
|
+
cropRequired: imageOptions.cropRequired === true,
|
|
397
|
+
..."picked" in editing ? {
|
|
398
|
+
title: intl.formatMessage(mediaMessages.imageCropForUse),
|
|
399
|
+
applyLabel: intl.formatMessage(mediaMessages.imageUseCrop),
|
|
400
|
+
onBack: () => {
|
|
401
|
+
setEditing(null);
|
|
402
|
+
setPicking(true);
|
|
260
403
|
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
|
|
404
|
+
} : {},
|
|
405
|
+
onApply: (transform_0) => {
|
|
406
|
+
const next_0 = withTransform(edited, transform_0);
|
|
407
|
+
if ("picked" in editing) add([next_0]);
|
|
408
|
+
else set(latest.current.map((entry_4) => keyOf(entry_4, resource) === editing.key ? next_0 : entry_4));
|
|
409
|
+
}
|
|
410
|
+
}) : null
|
|
411
|
+
]
|
|
264
412
|
});
|
|
265
413
|
}
|
|
266
414
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
415
|
+
* An `image` field (decision 0031): a `media` field for images, with the image editor. It accepts
|
|
416
|
+
* `image/*` by default. Each image shows as it is rendered for this use, with Edit, which opens
|
|
417
|
+
* the editor on its transform. When a crop is required, the editor is the step right after the
|
|
418
|
+
* pick of one image.
|
|
419
|
+
*/
|
|
420
|
+
function ImageField(props) {
|
|
421
|
+
const { ratios, enforceRatios, cropRequired, rotate, focalPoint, transformBase, accept = ["image/*"], ...rest } = props;
|
|
422
|
+
const image = {
|
|
423
|
+
...ratios !== void 0 ? { ratios } : {},
|
|
424
|
+
...enforceRatios !== void 0 ? { enforceRatios } : {},
|
|
425
|
+
...cropRequired !== void 0 ? { cropRequired } : {},
|
|
426
|
+
...rotate !== void 0 ? { rotate } : {},
|
|
427
|
+
...focalPoint !== void 0 ? { focalPoint } : {},
|
|
428
|
+
...transformBase !== void 0 ? { transformBase } : {}
|
|
429
|
+
};
|
|
430
|
+
return /* @__PURE__ */ jsx(MediaField, {
|
|
431
|
+
...rest,
|
|
432
|
+
accept,
|
|
433
|
+
image
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* The options of an `image` field of a definition: its `crop` rule (enforced ratios, a required
|
|
438
|
+
* crop), its suggested `ratios`, and the tools and base of its editor.
|
|
439
|
+
*/
|
|
440
|
+
function getImageOptions(field) {
|
|
441
|
+
const rule = field.rules?.find((candidate) => candidate.rule === "crop");
|
|
442
|
+
const value = rule !== void 0 && "value" in rule && typeof rule.value === "object" && rule.value !== null ? rule.value : {};
|
|
443
|
+
const enforced = Array.isArray(value.ratios) ? value.ratios.filter((ratio) => typeof ratio === "string") : [];
|
|
444
|
+
return {
|
|
445
|
+
ratios: enforced.length > 0 ? enforced : field.ratios ?? [],
|
|
446
|
+
enforceRatios: enforced.length > 0,
|
|
447
|
+
cropRequired: value.required === true,
|
|
448
|
+
rotate: field.rotate !== false,
|
|
449
|
+
focalPoint: field.focalPoint !== false,
|
|
450
|
+
transformBase: field.transformBase ?? "media"
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* The registry entry of the `media` and `image` types: the picker on the libraries the field allows
|
|
455
|
+
* (`getMediaLibraries()`), uploads to the first one (`getLibrary()`), `multiple`, `accept` (or
|
|
456
|
+
* its `accept` rule) and `maxSize` rule, with the adapter of `@panneau/data` and the upload source
|
|
457
|
+
* of the fields context. The picker opens again where the user left it in the same libraries.
|
|
458
|
+
* Without a library, the field shows its media and can't add any. An `image` field also gets its
|
|
459
|
+
* options (`getImageOptions()`), and accepts `image/*` by default.
|
|
271
460
|
*/
|
|
272
461
|
function MediaFieldEntry(props) {
|
|
273
|
-
const $ = c(
|
|
462
|
+
const $ = c(14);
|
|
274
463
|
const { field, value, onChange } = props;
|
|
275
464
|
const { definition } = usePanneau();
|
|
465
|
+
const adapter = useOptionalDataAdapter();
|
|
276
466
|
let common;
|
|
277
467
|
let t0;
|
|
278
|
-
if ($[0] !==
|
|
468
|
+
if ($[0] !== adapter || $[1] !== definition || $[2] !== field || $[3] !== props || $[4] !== value) {
|
|
279
469
|
const library = getLibrary(definition, field.resource);
|
|
280
470
|
const itemTitle = library?.itemTitle ?? "name";
|
|
471
|
+
let t1;
|
|
472
|
+
if ($[7] !== definition?.resources) {
|
|
473
|
+
t1 = (id) => definition?.resources.find((candidate) => candidate.id === id) ?? [];
|
|
474
|
+
$[7] = definition?.resources;
|
|
475
|
+
$[8] = t1;
|
|
476
|
+
} else t1 = $[8];
|
|
477
|
+
const libraries = getMediaLibraries(definition, field).flatMap(t1);
|
|
478
|
+
const sources = adapter !== null ? libraries.map((candidate_0) => createResourceMediaSource(adapter, candidate_0)) : [];
|
|
479
|
+
const image = field.type === "image" ? getImageOptions(field) : null;
|
|
281
480
|
common = {
|
|
282
481
|
...getFieldProps(props),
|
|
283
|
-
resource: library?.id ?? field.resource
|
|
482
|
+
resource: library?.id ?? (typeof field.resource === "string" ? field.resource : ""),
|
|
284
483
|
itemTitle,
|
|
285
|
-
accept: getAccept(field),
|
|
484
|
+
accept: getAccept(field) ?? (image !== null ? ["image/*"] : null),
|
|
286
485
|
maxSize: getMaxSize(field),
|
|
486
|
+
sources,
|
|
487
|
+
image,
|
|
488
|
+
stateKey: `panneau.media.${libraries.map(_temp3).join(",")}`,
|
|
287
489
|
...library === null ? { adapter: null } : {}
|
|
288
490
|
};
|
|
289
491
|
t0 = toMediaReferences(value, itemTitle);
|
|
290
|
-
$[0] =
|
|
291
|
-
$[1] =
|
|
292
|
-
$[2] =
|
|
293
|
-
$[3] =
|
|
294
|
-
$[4] =
|
|
295
|
-
$[5] =
|
|
492
|
+
$[0] = adapter;
|
|
493
|
+
$[1] = definition;
|
|
494
|
+
$[2] = field;
|
|
495
|
+
$[3] = props;
|
|
496
|
+
$[4] = value;
|
|
497
|
+
$[5] = common;
|
|
498
|
+
$[6] = t0;
|
|
296
499
|
} else {
|
|
297
|
-
common = $[
|
|
298
|
-
t0 = $[
|
|
500
|
+
common = $[5];
|
|
501
|
+
t0 = $[6];
|
|
299
502
|
}
|
|
300
503
|
const references = t0;
|
|
301
504
|
let t1;
|
|
302
|
-
if ($[
|
|
505
|
+
if ($[9] !== common || $[10] !== field.multiple || $[11] !== onChange || $[12] !== references) {
|
|
303
506
|
t1 = field.multiple === true ? /* @__PURE__ */ jsx(MediaField, {
|
|
304
507
|
...common,
|
|
305
508
|
multiple: true,
|
|
@@ -310,15 +513,18 @@ function MediaFieldEntry(props) {
|
|
|
310
513
|
value: references[0] ?? null,
|
|
311
514
|
onChange
|
|
312
515
|
});
|
|
313
|
-
$[
|
|
314
|
-
$[
|
|
315
|
-
$[
|
|
316
|
-
$[
|
|
317
|
-
$[
|
|
318
|
-
} else t1 = $[
|
|
516
|
+
$[9] = common;
|
|
517
|
+
$[10] = field.multiple;
|
|
518
|
+
$[11] = onChange;
|
|
519
|
+
$[12] = references;
|
|
520
|
+
$[13] = t1;
|
|
521
|
+
} else t1 = $[13];
|
|
319
522
|
return t1;
|
|
320
523
|
}
|
|
524
|
+
function _temp3(candidate_1) {
|
|
525
|
+
return candidate_1.id;
|
|
526
|
+
}
|
|
321
527
|
//#endregion
|
|
322
|
-
export { MediaField, MediaFieldEntry, getLibrary, isProcessing, toMediaReference, toMediaReferences };
|
|
528
|
+
export { ImageField, MediaField, MediaFieldEntry, getImageOptions, getLibrary, isProcessing, toMediaReference, toMediaReferences };
|
|
323
529
|
|
|
324
530
|
//# sourceMappingURL=MediaField.js.map
|