@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.
Files changed (83) hide show
  1. package/README.md +94 -7
  2. package/dist/ImageItem.d.ts +23 -0
  3. package/dist/ImageItem.d.ts.map +1 -0
  4. package/dist/ImageItem.js +292 -0
  5. package/dist/ImageItem.js.map +1 -0
  6. package/dist/MediaField.css +127 -0
  7. package/dist/MediaField.d.ts +67 -15
  8. package/dist/MediaField.d.ts.map +1 -1
  9. package/dist/MediaField.js +354 -148
  10. package/dist/MediaField.js.map +1 -1
  11. package/dist/MediaField.module.js +28 -2
  12. package/dist/MediaField.module.js.map +1 -1
  13. package/dist/editor/ImageEditor.css +369 -0
  14. package/dist/editor/ImageEditor.d.ts +13 -0
  15. package/dist/editor/ImageEditor.d.ts.map +1 -0
  16. package/dist/editor/ImageEditor.js +461 -0
  17. package/dist/editor/ImageEditor.js.map +1 -0
  18. package/dist/editor/ImageEditor.module.js +86 -0
  19. package/dist/editor/ImageEditor.module.js.map +1 -0
  20. package/dist/editor/draw.d.ts +37 -0
  21. package/dist/editor/draw.d.ts.map +1 -0
  22. package/dist/editor/draw.js +93 -0
  23. package/dist/editor/draw.js.map +1 -0
  24. package/dist/editor/geometry.d.ts +54 -0
  25. package/dist/editor/geometry.d.ts.map +1 -0
  26. package/dist/editor/geometry.js +227 -0
  27. package/dist/editor/geometry.js.map +1 -0
  28. package/dist/editor/index.d.ts +5 -0
  29. package/dist/editor/index.d.ts.map +1 -0
  30. package/dist/editor/index.js +3 -0
  31. package/dist/editor/tools.d.ts +11 -0
  32. package/dist/editor/tools.d.ts.map +1 -0
  33. package/dist/editor/tools.js +1199 -0
  34. package/dist/editor/tools.js.map +1 -0
  35. package/dist/editor/types.d.ts +111 -0
  36. package/dist/editor/types.d.ts.map +1 -0
  37. package/dist/index.d.ts +3 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +3 -2
  40. package/dist/picker/MediaDetails.d.ts +22 -0
  41. package/dist/picker/MediaDetails.d.ts.map +1 -0
  42. package/dist/picker/MediaDetails.js +368 -0
  43. package/dist/picker/MediaDetails.js.map +1 -0
  44. package/dist/picker/MediaGrid.d.ts +30 -0
  45. package/dist/picker/MediaGrid.d.ts.map +1 -0
  46. package/dist/picker/MediaGrid.js +260 -0
  47. package/dist/picker/MediaGrid.js.map +1 -0
  48. package/dist/picker/MediaPicker.css +642 -0
  49. package/dist/picker/MediaPicker.d.ts +9 -0
  50. package/dist/picker/MediaPicker.d.ts.map +1 -0
  51. package/dist/picker/MediaPicker.js +601 -0
  52. package/dist/picker/MediaPicker.js.map +1 -0
  53. package/dist/picker/MediaPicker.module.js +132 -0
  54. package/dist/picker/MediaPicker.module.js.map +1 -0
  55. package/dist/picker/PickerToolbar.d.ts +19 -0
  56. package/dist/picker/PickerToolbar.d.ts.map +1 -0
  57. package/dist/picker/PickerToolbar.js +266 -0
  58. package/dist/picker/PickerToolbar.js.map +1 -0
  59. package/dist/picker/UploadPanel.d.ts +22 -0
  60. package/dist/picker/UploadPanel.d.ts.map +1 -0
  61. package/dist/picker/UploadPanel.js +186 -0
  62. package/dist/picker/UploadPanel.js.map +1 -0
  63. package/dist/picker/format.d.ts +18 -0
  64. package/dist/picker/format.d.ts.map +1 -0
  65. package/dist/picker/format.js +64 -0
  66. package/dist/picker/format.js.map +1 -0
  67. package/dist/picker/index.d.ts +3 -0
  68. package/dist/picker/index.d.ts.map +1 -0
  69. package/dist/picker/index.js +1 -0
  70. package/dist/picker/types.d.ts +119 -0
  71. package/dist/picker/types.d.ts.map +1 -0
  72. package/dist/picker/useMediaBrowser.d.ts +50 -0
  73. package/dist/picker/useMediaBrowser.d.ts.map +1 -0
  74. package/dist/picker/useMediaBrowser.js +314 -0
  75. package/dist/picker/useMediaBrowser.js.map +1 -0
  76. package/dist/plugin.d.ts +1 -1
  77. package/dist/plugin.js +5 -2
  78. package/dist/plugin.js.map +1 -1
  79. package/dist/resourceSource.d.ts +21 -0
  80. package/dist/resourceSource.d.ts.map +1 -0
  81. package/dist/resourceSource.js +99 -0
  82. package/dist/resourceSource.js.map +1 -0
  83. package/package.json +18 -7
@@ -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 { Badge, Fieldset, IconButton, Spinner, usePanneauIntl } from "@panneau/ui";
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 of a `media` field: its `resource`, or the only resource of kind `media` of the
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 libraries = (definition?.resources ?? []).filter((candidate) => candidate.kind === "media");
44
- if (resource !== void 0) return libraries.find((candidate) => candidate.id === resource) ?? null;
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(10);
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
- t1 = media.filter(isProcessing).map(_temp).join(",");
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] = t1;
68
- } else t1 = $[3];
86
+ $[3] = resource;
87
+ $[4] = t1;
88
+ } else t1 = $[4];
69
89
  const key = t1;
70
90
  let t2;
71
- let t3;
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 ids = key.split(",").map(_temp2);
94
+ const entries = key.split("\n").map(_temp);
76
95
  const controller = new AbortController();
77
96
  const timer = setTimeout(() => {
78
- Promise.all(ids.map((id) => adapter.get(resource, id, { signal: controller.signal }))).then((refreshed) => {
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(_temp3);
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
- $[4] = adapter;
94
- $[5] = interval;
95
- $[6] = key;
96
- $[7] = resource;
97
- $[8] = t2;
98
- $[9] = t3;
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 a library (the `media` type, decision 0026): a file chosen or dropped becomes a media
107
- * of the library, an item of its resource (`create(resource, { file })`), sent directly to storage
108
- * first when there is an upload source. Values are references to the media, with their `media`
109
- * and their `transform` for this use. The field shows each media with its thumbnail and its
110
- * status, and reads it again until its file is processed. Picking media already in the library
111
- * comes with the media picker.
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 _temp3() {}
114
- function _temp2(entry) {
115
- return entry.slice(0, entry.lastIndexOf(":"));
116
- }
117
- function _temp(item) {
118
- return `${item.id}:${item.media?.status ?? ""}`;
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 editable = adapter !== null && disabled !== true && readOnly !== true;
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 = toMediaReference(item_0, itemTitle) ?? { id: item_0.id };
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") return /* @__PURE__ */ jsx(Badge, {
169
- tone: "danger",
170
- children: intl.formatMessage(mediaMessages.statusFailed)
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
- return /* @__PURE__ */ jsx(Fieldset, {
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: adapter === null && disabled !== true && readOnly !== true ? /* @__PURE__ */ jsxs(Fragment, { children: [
252
+ description: description !== void 0 || unavailable || constraints.length > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [
181
253
  description,
182
- " ",
183
- intl.formatMessage(mediaMessages.unavailable)
184
- ] }) : description,
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: /* @__PURE__ */ jsxs("div", {
193
- className: MediaField_module_default.field,
194
- children: [
195
- media.length > 0 ? /* @__PURE__ */ jsx("ul", {
196
- className: MediaField_module_default.media,
197
- children: media.map((item_2) => {
198
- const name_0 = item_2.label ?? item_2.id;
199
- const size = item_2.media?.size;
200
- return /* @__PURE__ */ jsxs("li", {
201
- className: MediaField_module_default.item,
202
- children: [
203
- /* @__PURE__ */ jsx("span", {
204
- className: MediaField_module_default.thumbnail,
205
- children: typeof item_2.media?.thumbnailUrl === "string" ? /* @__PURE__ */ jsx("img", {
206
- src: item_2.media.thumbnailUrl,
207
- alt: ""
208
- }) : /* @__PURE__ */ jsx(FileImage, { "aria-hidden": true })
209
- }),
210
- /* @__PURE__ */ jsxs("span", {
211
- className: MediaField_module_default.details,
212
- children: [/* @__PURE__ */ jsx("span", {
213
- className: MediaField_module_default.name,
214
- children: name_0
215
- }), /* @__PURE__ */ jsxs("span", {
216
- className: MediaField_module_default.meta,
217
- children: [
218
- item_2.media !== void 0 ? /* @__PURE__ */ jsx("span", { children: item_2.media.type }) : null,
219
- typeof size === "number" ? /* @__PURE__ */ jsx("span", { children: formatSize(size, intl.locale) }) : null,
220
- statusBadge(item_2)
221
- ]
222
- })]
223
- }),
224
- editable ? /* @__PURE__ */ jsx(IconButton, {
225
- icon: X,
226
- size: "sm",
227
- label: intl.formatMessage(fieldsMessages.removeFile, { name: name_0 }),
228
- onPress: () => set(latest.current.filter((entry) => entry.id !== item_2.id))
229
- }) : null
230
- ]
231
- }, item_2.id);
232
- })
233
- }) : creating.length === 0 && upload.items.length === 0 ? /* @__PURE__ */ jsx("span", {
234
- className: MediaField_module_default.none,
235
- children: intl.formatMessage(mediaMessages.noMedia)
236
- }) : null,
237
- /* @__PURE__ */ jsx(UploadList, {
238
- items: upload.items,
239
- onPause: upload.pause,
240
- onResume: upload.resume,
241
- onCancel: upload.cancel,
242
- onRetry: upload.retry
243
- }),
244
- creating.length > 0 ? /* @__PURE__ */ jsxs("span", {
245
- className: MediaField_module_default.meta,
246
- role: "status",
247
- children: [
248
- /* @__PURE__ */ jsx(Spinner, { size: "sm" }),
249
- intl.formatMessage(fieldsMessages.uploadCompleting),
250
- " ",
251
- creating.join(", ")
252
- ]
253
- }) : null,
254
- editable ? /* @__PURE__ */ jsx(FilePicker, {
255
- accept,
256
- multiple,
257
- onSelect: (selected) => {
258
- setProblems([]);
259
- upload.add(selected);
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
- }) : null
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
- * The registry entry of the `media` type: the field's library (`resource`, or the only `media`
268
- * resource of the definition), `multiple`, `accept` (or its `accept` rule) and `maxSize` rule,
269
- * with the adapter of `@panneau/data` and the upload source of the fields context. Without a
270
- * library, the field shows its media and can't add any.
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(11);
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] !== definition || $[1] !== field || $[2] !== props || $[3] !== value) {
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] = definition;
291
- $[1] = field;
292
- $[2] = props;
293
- $[3] = value;
294
- $[4] = common;
295
- $[5] = t0;
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 = $[4];
298
- t0 = $[5];
500
+ common = $[5];
501
+ t0 = $[6];
299
502
  }
300
503
  const references = t0;
301
504
  let t1;
302
- if ($[6] !== common || $[7] !== field.multiple || $[8] !== onChange || $[9] !== references) {
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
- $[6] = common;
314
- $[7] = field.multiple;
315
- $[8] = onChange;
316
- $[9] = references;
317
- $[10] = t1;
318
- } else t1 = $[10];
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