@haklex/rich-ext-gallery 0.57.0 → 0.59.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.
@@ -1,17 +1,51 @@
1
- import { A as galleryImageThumb, C as galleryImageActions, D as galleryImageFields, E as galleryImageDragHandle, M as galleryMaxHeightInput, O as galleryImageInput, S as galleryHeaderClose, T as galleryImageDeleteBtn, _ as galleryFooterActions, a as galleryDialogEmpty, b as galleryFooterInfo, c as galleryDialogPopup, d as galleryDialogSubHeaderLabel, f as galleryDialogTitle, g as galleryEmptyState, h as galleryEditOverlay, i as galleryDialogBody, j as galleryImageThumbPlaceholder, k as galleryImageList, l as galleryDialogSubHeader, m as galleryEditLabel, o as galleryDialogFooter, p as galleryEditContainer, r as galleryAddBtn, s as galleryDialogHeader, t as GalleryRenderer, u as galleryDialogSubHeaderGroup, v as galleryFooterBtnCancel, w as galleryImageCard, x as galleryHeaderActions, y as galleryFooterBtnSave } from "./GalleryRenderer-CHoZMZOz.js";
1
+ import { C as galleryMaxHeightInput, S as galleryImageThumbPlaceholder, _ as galleryImageCard, a as galleryDialogEmpty, b as galleryImageList, c as galleryDialogSubHeaderGroup, d as galleryDragOverlayText, f as galleryDropZone, g as galleryEmptyState, h as galleryEditOverlay, i as galleryDialogBody, l as galleryDialogSubHeaderLabel, m as galleryEditLabel, o as galleryDialogPopup, p as galleryEditContainer, r as galleryAddActions, s as galleryDialogSubHeader, t as GalleryRenderer, u as galleryDragOverlay, v as galleryImageDragHandle, x as galleryImageThumb, y as galleryImageFields } from "./GalleryRenderer-BQwH6RKf.js";
2
2
  import { i as _defineProperty, r as GalleryNode } from "./GalleryNode-CGOSFC43.js";
3
3
  import { $getNodeByKey, $insertNodes } from "lexical";
4
- import { GripVertical, ImageIcon, Images, Pencil, Plus, Trash2, X } from "lucide-react";
5
- import { createElement, useCallback, useRef, useState } from "react";
6
- import { DndContext, DragOverlay, PointerSensor, closestCenter, useSensor, useSensors } from "@dnd-kit/core";
7
- import { SortableContext, arrayMove, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable";
8
- import { CSS } from "@dnd-kit/utilities";
4
+ import { GripVertical, ImageIcon, Images, Link, Pencil, Plus, Trash2 } from "lucide-react";
5
+ import { createElement, useCallback, useEffect, useMemo, useRef, useState } from "react";
9
6
  import { resolvePreprocessTargets, useImagePreprocess, useImageUpload } from "@haklex/rich-editor/plugins";
10
7
  import { useColorScheme } from "@haklex/rich-editor/static";
11
- import { SegmentedControl, presentDialog } from "@haklex/rich-editor-ui";
8
+ import { ActionButton, ModalShell, ModalShellBody, ModalShellFooter, ModalShellHeader, SegmentedControl, TextInput, confirmDiscardChanges, presentDialog, toast } from "@haklex/rich-editor-ui";
12
9
  import { usePortalTheme, vars } from "@haklex/rich-style-token";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ import { DndContext, DragOverlay, PointerSensor, closestCenter, useSensor, useSensors } from "@dnd-kit/core";
12
+ import { SortableContext, arrayMove, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable";
13
13
  import { createPortal } from "react-dom";
14
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
14
+ import { CSS } from "@dnd-kit/utilities";
15
+ //#region src/gallery-files.ts
16
+ function readAsDataUrl(file) {
17
+ return new Promise((resolve, reject) => {
18
+ const reader = new FileReader();
19
+ reader.onload = () => resolve(String(reader.result));
20
+ reader.onerror = () => reject(reader.error ?? /* @__PURE__ */ new Error("File read failed"));
21
+ reader.readAsDataURL(file);
22
+ });
23
+ }
24
+ async function fileToGalleryImage(file, uploader) {
25
+ if (uploader) {
26
+ const result = await uploader(file);
27
+ return {
28
+ src: result.src,
29
+ alt: result.altText ?? file.name
30
+ };
31
+ }
32
+ return {
33
+ src: await readAsDataUrl(file),
34
+ alt: file.name
35
+ };
36
+ }
37
+ function extractImageFiles(list) {
38
+ if (!list) return [];
39
+ return Array.from(list).filter((file) => file?.type.startsWith("image/"));
40
+ }
41
+ function imageCountLabel(count) {
42
+ return `${count} ${count === 1 ? "image" : "images"}`;
43
+ }
44
+ var nextId = 0;
45
+ function genId() {
46
+ return `img-${++nextId}`;
47
+ }
48
+ //#endregion
15
49
  //#region src/GalleryDialogSubHeader.tsx
16
50
  var aspectItems = [
17
51
  {
@@ -43,26 +77,29 @@ var fitBaseItems = [{
43
77
  label: "Contain"
44
78
  }];
45
79
  var GalleryDialogSubHeader = ({ aspect, fit, layout, maxItemHeightInput, onAspectChange, onFitChange, onMaxItemHeightInputChange }) => {
46
- if (layout === "masonry") return /* @__PURE__ */ jsxs("div", {
80
+ if (layout === "masonry") return /* @__PURE__ */ jsx("div", {
47
81
  className: galleryDialogSubHeader,
48
- children: [
49
- /* @__PURE__ */ jsx("span", {
50
- className: galleryDialogSubHeaderLabel,
51
- children: "Max height"
52
- }),
53
- /* @__PURE__ */ jsx("input", {
54
- className: galleryMaxHeightInput,
55
- min: 1,
56
- placeholder: "Auto",
57
- type: "number",
58
- value: maxItemHeightInput,
59
- onChange: (e) => onMaxItemHeightInputChange(e.target.value)
60
- }),
61
- /* @__PURE__ */ jsx("span", {
62
- className: galleryDialogSubHeaderLabel,
63
- children: "px"
64
- })
65
- ]
82
+ children: /* @__PURE__ */ jsxs("label", {
83
+ className: galleryDialogSubHeaderGroup,
84
+ children: [
85
+ /* @__PURE__ */ jsx("span", {
86
+ className: galleryDialogSubHeaderLabel,
87
+ children: "Max height"
88
+ }),
89
+ /* @__PURE__ */ jsx(TextInput, {
90
+ className: galleryMaxHeightInput,
91
+ min: 1,
92
+ placeholder: "Auto",
93
+ type: "number",
94
+ value: maxItemHeightInput,
95
+ onChange: (e) => onMaxItemHeightInputChange(e.target.value)
96
+ }),
97
+ /* @__PURE__ */ jsx("span", {
98
+ className: galleryDialogSubHeaderLabel,
99
+ children: "px"
100
+ })
101
+ ]
102
+ })
66
103
  });
67
104
  return /* @__PURE__ */ jsxs("div", {
68
105
  className: galleryDialogSubHeader,
@@ -93,64 +130,18 @@ var GalleryDialogSubHeader = ({ aspect, fit, layout, maxItemHeightInput, onAspec
93
130
  });
94
131
  };
95
132
  //#endregion
96
- //#region src/maxItemHeight.ts
97
- function parseMaxItemHeightInput(value) {
98
- const trimmed = value.trim();
99
- if (!trimmed) return void 0;
100
- const parsed = Number(trimmed);
101
- if (!Number.isFinite(parsed) || parsed <= 0) return void 0;
102
- return parsed;
103
- }
104
- //#endregion
105
- //#region src/GalleryEditRenderer.tsx
106
- var layoutItems = [
107
- {
108
- value: "grid",
109
- label: "Grid"
110
- },
111
- {
112
- value: "masonry",
113
- label: "Masonry"
114
- },
115
- {
116
- value: "carousel",
117
- label: "Carousel"
118
- }
119
- ];
120
- var nextId = 0;
121
- function genId() {
122
- return `img-${++nextId}`;
123
- }
124
- function readAsDataUrl(file) {
125
- return new Promise((resolve, reject) => {
126
- const reader = new FileReader();
127
- reader.onload = () => resolve(String(reader.result));
128
- reader.onerror = () => reject(reader.error ?? /* @__PURE__ */ new Error("File read failed"));
129
- reader.readAsDataURL(file);
130
- });
131
- }
132
- async function fileToGalleryImage(file, uploader) {
133
- if (uploader) {
134
- const result = await uploader(file);
135
- return {
136
- src: result.src,
137
- alt: result.altText ?? file.name
138
- };
139
- }
140
- return {
141
- src: await readAsDataUrl(file),
142
- alt: file.name
143
- };
144
- }
145
- function extractImageFiles(list) {
146
- if (!list) return [];
147
- const files = [];
148
- for (let i = 0; i < list.length; i++) {
149
- const f = list[i];
150
- if (f && f.type.startsWith("image/")) files.push(f);
151
- }
152
- return files;
153
- }
133
+ //#region src/GalleryImageCard.tsx
134
+ var Thumb = ({ image }) => /* @__PURE__ */ jsx("div", {
135
+ className: galleryImageThumb,
136
+ title: "Drop an image here to replace",
137
+ children: image.src ? /* @__PURE__ */ jsx("img", {
138
+ alt: image.alt || "",
139
+ src: image.src
140
+ }) : /* @__PURE__ */ jsx("span", {
141
+ className: galleryImageThumbPlaceholder,
142
+ children: /* @__PURE__ */ jsx(ImageIcon, { size: 20 })
143
+ })
144
+ });
154
145
  var SortableImageCard = ({ id, image, index, isLast, lastInputRef, onUpdate, onRemove, onDropFile }) => {
155
146
  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id });
156
147
  const [dropActive, setDropActive] = useState(false);
@@ -188,130 +179,164 @@ var SortableImageCard = ({ id, image, index, isLast, lastInputRef, onUpdate, onR
188
179
  onDrop: handleDrop,
189
180
  children: [
190
181
  /* @__PURE__ */ jsx("div", {
182
+ "aria-label": "Reorder image",
191
183
  className: galleryImageDragHandle,
192
184
  ...attributes,
193
185
  ...listeners,
194
186
  children: /* @__PURE__ */ jsx(GripVertical, { size: 14 })
195
187
  }),
196
- /* @__PURE__ */ jsx("div", {
197
- className: galleryImageThumb,
198
- title: "Drop an image here to replace",
199
- children: image.src ? /* @__PURE__ */ jsx("img", {
200
- alt: image.alt || "",
201
- src: image.src
202
- }) : /* @__PURE__ */ jsx("span", {
203
- className: galleryImageThumbPlaceholder,
204
- children: /* @__PURE__ */ jsx(ImageIcon, { size: 20 })
205
- })
206
- }),
188
+ /* @__PURE__ */ jsx(Thumb, { image }),
207
189
  /* @__PURE__ */ jsxs("div", {
208
190
  className: galleryImageFields,
209
- children: [/* @__PURE__ */ jsx("input", {
210
- className: galleryImageInput,
191
+ children: [/* @__PURE__ */ jsx(TextInput, {
192
+ "aria-label": "Image URL",
211
193
  placeholder: "Image URL",
212
194
  ref: isLast ? lastInputRef : void 0,
213
- type: "text",
214
195
  value: image.src,
215
196
  onChange: (e) => onUpdate(index, "src", e.target.value)
216
- }), /* @__PURE__ */ jsx("input", {
217
- className: galleryImageInput,
197
+ }), /* @__PURE__ */ jsx(TextInput, {
198
+ "aria-label": "Alt text",
218
199
  placeholder: "Alt text (optional)",
219
- type: "text",
220
200
  value: image.alt || "",
221
201
  onChange: (e) => onUpdate(index, "alt", e.target.value)
222
202
  })]
223
203
  }),
224
- /* @__PURE__ */ jsx("div", {
225
- className: galleryImageActions,
226
- children: /* @__PURE__ */ jsx("button", {
227
- className: galleryImageDeleteBtn,
228
- title: "Remove",
229
- type: "button",
230
- onClick: () => onRemove(index),
231
- children: /* @__PURE__ */ jsx(Trash2, { size: 14 })
232
- })
204
+ /* @__PURE__ */ jsx(ActionButton, {
205
+ danger: true,
206
+ icon: true,
207
+ "aria-label": "Remove image",
208
+ size: "md",
209
+ title: "Remove image",
210
+ onClick: () => onRemove(index),
211
+ children: /* @__PURE__ */ jsx(Trash2, {})
233
212
  })
234
213
  ]
235
214
  });
236
215
  };
237
216
  var DragOverlayCard = ({ image }) => /* @__PURE__ */ jsxs("div", {
238
- className: galleryImageCard,
239
- style: { opacity: .9 },
217
+ className: `${galleryImageCard} ${galleryDragOverlay}`,
240
218
  children: [
241
219
  /* @__PURE__ */ jsx("div", {
242
220
  className: galleryImageDragHandle,
243
221
  children: /* @__PURE__ */ jsx(GripVertical, { size: 14 })
244
222
  }),
245
- /* @__PURE__ */ jsx("div", {
246
- className: galleryImageThumb,
247
- children: image.src ? /* @__PURE__ */ jsx("img", {
248
- alt: image.alt || "",
249
- src: image.src
250
- }) : /* @__PURE__ */ jsx("span", {
251
- className: galleryImageThumbPlaceholder,
252
- children: /* @__PURE__ */ jsx(ImageIcon, { size: 20 })
253
- })
254
- }),
223
+ /* @__PURE__ */ jsx(Thumb, { image }),
255
224
  /* @__PURE__ */ jsx("div", {
256
225
  className: galleryImageFields,
257
226
  children: /* @__PURE__ */ jsx("span", {
258
- style: {
259
- fontSize: vars.typography.fontSizeSm,
260
- overflow: "hidden",
261
- textOverflow: "ellipsis",
262
- whiteSpace: "nowrap"
263
- },
227
+ className: galleryDragOverlayText,
264
228
  children: image.src || "No URL"
265
229
  })
266
230
  })
267
231
  ]
268
232
  });
269
- var GalleryEditorDialogContent = ({ dismiss, initialAspect, initialFit, initialImages, initialLayout, initialMaxItemHeight, preprocess, uploader, onAspectChange, onFitChange, onImagesChange, onLayoutChange, onMaxItemHeightChange }) => {
233
+ //#endregion
234
+ //#region src/GalleryImageList.tsx
235
+ var GalleryImageList = ({ children, entries, lastInputRef, setEntries, onDropFile }) => {
270
236
  const { className: portalClassName } = usePortalTheme();
271
- const [entries, setEntries] = useState(() => initialImages.map((img) => ({
272
- id: genId(),
273
- image: { ...img }
274
- })));
275
- const [layout, setLayout] = useState(initialLayout);
276
- const [aspect, setAspect] = useState(initialAspect);
277
- const [fit, setFit] = useState(initialFit);
278
- const [maxItemHeightInput, setMaxItemHeightInput] = useState(initialMaxItemHeight === void 0 ? "" : String(initialMaxItemHeight));
279
- const newInputRef = useRef(null);
280
237
  const [dragActiveId, setDragActiveId] = useState(null);
281
- const [bodyDropActive, setBodyDropActive] = useState(false);
282
- const dragDepthRef = useRef(0);
283
- const itemIds = entries.map((e) => e.id);
284
238
  const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 5 } }));
285
- const handleAddImage = useCallback(() => {
286
- setEntries((prev) => [...prev, {
287
- id: genId(),
239
+ const handleRemove = useCallback((index) => {
240
+ setEntries((prev) => prev.filter((_, i) => i !== index));
241
+ }, [setEntries]);
242
+ const handleUpdate = useCallback((index, field, value) => {
243
+ setEntries((prev) => prev.map((entry, i) => i === index ? {
244
+ ...entry,
288
245
  image: {
289
- src: "",
290
- alt: ""
246
+ ...entry.image,
247
+ [field]: value
291
248
  }
292
- }]);
293
- requestAnimationFrame(() => {
294
- newInputRef.current?.focus();
249
+ } : entry));
250
+ }, [setEntries]);
251
+ const handleDragEnd = useCallback((event) => {
252
+ const { active, over } = event;
253
+ setDragActiveId(null);
254
+ if (!over || active.id === over.id) return;
255
+ setEntries((prev) => {
256
+ const oldIndex = prev.findIndex((e) => e.id === String(active.id));
257
+ const newIndex = prev.findIndex((e) => e.id === String(over.id));
258
+ if (oldIndex === -1 || newIndex === -1) return prev;
259
+ return arrayMove(prev, oldIndex, newIndex);
295
260
  });
296
- }, []);
297
- const handleRemoveImage = useCallback((index) => {
298
- setEntries((prev) => prev.filter((_, i) => i !== index));
299
- }, []);
261
+ }, [setEntries]);
262
+ const dragActiveEntry = dragActiveId ? entries.find((e) => e.id === dragActiveId) : null;
263
+ return /* @__PURE__ */ jsxs("div", {
264
+ className: galleryImageList,
265
+ children: [/* @__PURE__ */ jsxs(DndContext, {
266
+ collisionDetection: closestCenter,
267
+ sensors,
268
+ onDragCancel: () => setDragActiveId(null),
269
+ onDragEnd: handleDragEnd,
270
+ onDragStart: (event) => {
271
+ setDragActiveId(String(event.active.id));
272
+ },
273
+ children: [/* @__PURE__ */ jsx(SortableContext, {
274
+ items: entries.map((e) => e.id),
275
+ strategy: verticalListSortingStrategy,
276
+ children: entries.map((entry, index) => /* @__PURE__ */ jsx(SortableImageCard, {
277
+ id: entry.id,
278
+ image: entry.image,
279
+ index,
280
+ isLast: index === entries.length - 1,
281
+ lastInputRef,
282
+ onDropFile,
283
+ onRemove: handleRemove,
284
+ onUpdate: handleUpdate
285
+ }, entry.id))
286
+ }), typeof document !== "undefined" ? createPortal(/* @__PURE__ */ jsx("div", {
287
+ className: portalClassName,
288
+ children: /* @__PURE__ */ jsx(DragOverlay, {
289
+ dropAnimation: null,
290
+ children: dragActiveEntry ? /* @__PURE__ */ jsx(DragOverlayCard, { image: dragActiveEntry.image }) : null
291
+ })
292
+ }), document.body) : null]
293
+ }), children]
294
+ });
295
+ };
296
+ //#endregion
297
+ //#region src/maxItemHeight.ts
298
+ function parseMaxItemHeightInput(value) {
299
+ const trimmed = value.trim();
300
+ if (!trimmed) return void 0;
301
+ const parsed = Number(trimmed);
302
+ if (!Number.isFinite(parsed) || parsed <= 0) return void 0;
303
+ return parsed;
304
+ }
305
+ //#endregion
306
+ //#region src/useGalleryUpload.ts
307
+ function uploadFailedTitle(count) {
308
+ return count === 1 ? "Couldn’t upload image" : `Couldn’t upload ${count} images`;
309
+ }
310
+ var hasFiles = (e) => !!e.dataTransfer?.types.includes("Files");
311
+ function useGalleryUpload({ preprocess, uploader, setEntries, onAddUrl }) {
312
+ const fileInputRef = useRef(null);
313
+ const [dropActive, setDropActive] = useState(false);
314
+ const dragDepthRef = useRef(0);
300
315
  const appendFiles = useCallback(async (picked) => {
301
316
  const files = await resolvePreprocessTargets(picked, "gallery", preprocess?.ref.current ?? null);
302
317
  if (!files.length) return;
303
- const fresh = (await Promise.all(files.map(async (file) => {
318
+ const results = await Promise.all(files.map(async (file) => {
304
319
  try {
305
320
  return await fileToGalleryImage(file, uploader);
306
321
  } catch {
307
322
  return null;
308
323
  }
309
- }))).filter((r) => !!r && !!r.src).map((image) => ({
324
+ }));
325
+ const failed = results.filter((r) => !r?.src).length;
326
+ if (failed) toast({
327
+ title: uploadFailedTitle(failed),
328
+ tone: "error"
329
+ });
330
+ const fresh = results.filter((r) => !!r && !!r.src).map((image) => ({
310
331
  id: genId(),
311
332
  image
312
333
  }));
313
334
  if (fresh.length) setEntries((prev) => [...prev, ...fresh]);
314
- }, [preprocess, uploader]);
335
+ }, [
336
+ preprocess,
337
+ uploader,
338
+ setEntries
339
+ ]);
315
340
  const replaceEntryFile = useCallback(async (index, picked) => {
316
341
  const [file] = await resolvePreprocessTargets([picked], "gallery", preprocess?.ref.current ?? null);
317
342
  if (!file) return;
@@ -324,28 +349,120 @@ var GalleryEditorDialogContent = ({ dismiss, initialAspect, initialFit, initialI
324
349
  alt: next.alt || entry.image.alt || ""
325
350
  }
326
351
  } : entry));
327
- } catch {}
328
- }, [preprocess, uploader]);
329
- const handleUpdateImage = useCallback((index, field, value) => {
330
- setEntries((prev) => prev.map((entry, i) => i === index ? {
331
- ...entry,
352
+ } catch {
353
+ toast({
354
+ title: uploadFailedTitle(1),
355
+ tone: "error"
356
+ });
357
+ }
358
+ }, [
359
+ preprocess,
360
+ uploader,
361
+ setEntries
362
+ ]);
363
+ const handleAddImages = useCallback(() => {
364
+ if (uploader) fileInputRef.current?.click();
365
+ else onAddUrl();
366
+ }, [uploader, onAddUrl]);
367
+ const handleFileInputChange = (e) => {
368
+ const files = extractImageFiles(e.target.files);
369
+ e.target.value = "";
370
+ if (files.length) appendFiles(files);
371
+ };
372
+ return {
373
+ dropActive,
374
+ dropHandlers: {
375
+ onDragEnter: (e) => {
376
+ if (!hasFiles(e)) return;
377
+ dragDepthRef.current += 1;
378
+ setDropActive(true);
379
+ },
380
+ onDragOver: (e) => {
381
+ if (!hasFiles(e)) return;
382
+ e.preventDefault();
383
+ e.dataTransfer.dropEffect = "copy";
384
+ },
385
+ onDragLeave: (e) => {
386
+ if (!hasFiles(e)) return;
387
+ dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
388
+ if (dragDepthRef.current === 0) setDropActive(false);
389
+ },
390
+ onDrop: (e) => {
391
+ const files = extractImageFiles(e.dataTransfer?.files);
392
+ dragDepthRef.current = 0;
393
+ setDropActive(false);
394
+ if (!files.length) return;
395
+ e.preventDefault();
396
+ appendFiles(files);
397
+ }
398
+ },
399
+ fileInputRef,
400
+ handleAddImages,
401
+ handleFileInputChange,
402
+ replaceEntryFile
403
+ };
404
+ }
405
+ //#endregion
406
+ //#region src/GalleryEditorDialog.tsx
407
+ var layoutItems = [
408
+ {
409
+ value: "grid",
410
+ label: "Grid"
411
+ },
412
+ {
413
+ value: "masonry",
414
+ label: "Masonry"
415
+ },
416
+ {
417
+ value: "carousel",
418
+ label: "Carousel"
419
+ }
420
+ ];
421
+ var GalleryEditorDialog = ({ dismiss, requestDismiss, initialAspect, initialFit, initialImages, initialLayout, initialMaxItemHeight, preprocess, uploader, onAspectChange, onDirtyChange, onFitChange, onImagesChange, onLayoutChange, onMaxItemHeightChange }) => {
422
+ const [entries, setEntries] = useState(() => initialImages.map((img) => ({
423
+ id: genId(),
424
+ image: { ...img }
425
+ })));
426
+ const [layout, setLayout] = useState(initialLayout);
427
+ const [aspect, setAspect] = useState(initialAspect);
428
+ const [fit, setFit] = useState(initialFit);
429
+ const initialMaxItemHeightInput = initialMaxItemHeight === void 0 ? "" : String(initialMaxItemHeight);
430
+ const [maxItemHeightInput, setMaxItemHeightInput] = useState(initialMaxItemHeightInput);
431
+ const newInputRef = useRef(null);
432
+ const dirty = useMemo(() => layout !== initialLayout || aspect !== initialAspect || fit !== initialFit || maxItemHeightInput !== initialMaxItemHeightInput || JSON.stringify(entries.map((e) => e.image)) !== JSON.stringify(initialImages), [
433
+ entries,
434
+ layout,
435
+ aspect,
436
+ fit,
437
+ maxItemHeightInput,
438
+ initialImages,
439
+ initialLayout,
440
+ initialAspect,
441
+ initialFit,
442
+ initialMaxItemHeightInput
443
+ ]);
444
+ useEffect(() => {
445
+ onDirtyChange?.(dirty);
446
+ }, [dirty, onDirtyChange]);
447
+ const imageCount = entries.filter((e) => e.image.src.trim()).length;
448
+ const handleAddUrl = useCallback(() => {
449
+ setEntries((prev) => [...prev, {
450
+ id: genId(),
332
451
  image: {
333
- ...entry.image,
334
- [field]: value
452
+ src: "",
453
+ alt: ""
335
454
  }
336
- } : entry));
337
- }, []);
338
- const handleDragEnd = useCallback((event) => {
339
- const { active, over } = event;
340
- setDragActiveId(null);
341
- if (!over || active.id === over.id) return;
342
- setEntries((prev) => {
343
- const oldIndex = prev.findIndex((e) => e.id === String(active.id));
344
- const newIndex = prev.findIndex((e) => e.id === String(over.id));
345
- if (oldIndex === -1 || newIndex === -1) return prev;
346
- return arrayMove(prev, oldIndex, newIndex);
455
+ }]);
456
+ requestAnimationFrame(() => {
457
+ newInputRef.current?.focus();
347
458
  });
348
459
  }, []);
460
+ const { dropActive, dropHandlers, fileInputRef, handleAddImages, handleFileInputChange, replaceEntryFile } = useGalleryUpload({
461
+ preprocess,
462
+ uploader,
463
+ setEntries,
464
+ onAddUrl: handleAddUrl
465
+ });
349
466
  const handleSave = useCallback(() => {
350
467
  onImagesChange(entries.map((e) => e.image).filter((img) => img.src.trim()));
351
468
  onLayoutChange(layout);
@@ -366,147 +483,83 @@ var GalleryEditorDialogContent = ({ dismiss, initialAspect, initialFit, initialI
366
483
  onMaxItemHeightChange,
367
484
  dismiss
368
485
  ]);
369
- const dragActiveEntry = dragActiveId ? entries.find((e) => e.id === dragActiveId) : null;
370
- const handleBodyDragEnter = (e) => {
371
- if (!e.dataTransfer?.types.includes("Files")) return;
372
- dragDepthRef.current += 1;
373
- setBodyDropActive(true);
374
- };
375
- const handleBodyDragOver = (e) => {
376
- if (!e.dataTransfer?.types.includes("Files")) return;
377
- e.preventDefault();
378
- e.dataTransfer.dropEffect = "copy";
379
- };
380
- const handleBodyDragLeave = (e) => {
381
- if (!e.dataTransfer?.types.includes("Files")) return;
382
- dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
383
- if (dragDepthRef.current === 0) setBodyDropActive(false);
384
- };
385
- const handleBodyDrop = (e) => {
386
- const files = extractImageFiles(e.dataTransfer?.files);
387
- dragDepthRef.current = 0;
388
- setBodyDropActive(false);
389
- if (!files.length) return;
390
- e.preventDefault();
391
- appendFiles(files);
392
- };
393
- return /* @__PURE__ */ jsxs(Fragment, { children: [
394
- /* @__PURE__ */ jsxs("div", {
395
- className: galleryDialogHeader,
396
- children: [
397
- /* @__PURE__ */ jsxs("div", {
398
- className: galleryDialogTitle,
399
- children: [/* @__PURE__ */ jsx(Images, { size: 18 }), /* @__PURE__ */ jsx("span", { children: "Gallery Editor" })]
400
- }),
401
- /* @__PURE__ */ jsx("div", {
402
- className: galleryHeaderActions,
403
- children: /* @__PURE__ */ jsx(SegmentedControl, {
404
- items: layoutItems,
405
- value: layout,
406
- onChange: setLayout
407
- })
486
+ const handleCancel = requestDismiss ?? dismiss;
487
+ const addActions = /* @__PURE__ */ jsxs("div", {
488
+ className: galleryAddActions,
489
+ children: [/* @__PURE__ */ jsxs(ActionButton, {
490
+ size: "md",
491
+ variant: "outline",
492
+ onClick: handleAddImages,
493
+ children: [/* @__PURE__ */ jsx(Plus, {}), "Add images"]
494
+ }), uploader && /* @__PURE__ */ jsxs(ActionButton, {
495
+ size: "md",
496
+ onClick: handleAddUrl,
497
+ children: [/* @__PURE__ */ jsx(Link, {}), "Add URL"]
498
+ })]
499
+ });
500
+ return /* @__PURE__ */ jsxs(ModalShell, {
501
+ onSave: handleSave,
502
+ children: [
503
+ /* @__PURE__ */ jsx(ModalShellHeader, {
504
+ center: /* @__PURE__ */ jsx(SegmentedControl, {
505
+ items: layoutItems,
506
+ value: layout,
507
+ onChange: setLayout
408
508
  }),
409
- /* @__PURE__ */ jsx("button", {
410
- className: galleryHeaderClose,
411
- type: "button",
412
- onClick: dismiss,
413
- children: /* @__PURE__ */ jsx(X, { size: 18 })
509
+ icon: /* @__PURE__ */ jsx(Images, {}),
510
+ title: "Gallery",
511
+ onClose: handleCancel
512
+ }),
513
+ /* @__PURE__ */ jsx(GalleryDialogSubHeader, {
514
+ aspect,
515
+ fit,
516
+ layout,
517
+ maxItemHeightInput,
518
+ onAspectChange: setAspect,
519
+ onFitChange: setFit,
520
+ onMaxItemHeightInputChange: setMaxItemHeightInput
521
+ }),
522
+ /* @__PURE__ */ jsx(ModalShellBody, {
523
+ className: galleryDialogBody,
524
+ children: /* @__PURE__ */ jsxs("div", {
525
+ className: galleryDropZone,
526
+ "data-drop-active": dropActive || void 0,
527
+ ...dropHandlers,
528
+ children: [/* @__PURE__ */ jsx("input", {
529
+ hidden: true,
530
+ multiple: true,
531
+ accept: "image/*",
532
+ ref: fileInputRef,
533
+ tabIndex: -1,
534
+ type: "file",
535
+ onChange: handleFileInputChange
536
+ }), entries.length === 0 ? /* @__PURE__ */ jsxs("div", {
537
+ className: galleryDialogEmpty,
538
+ children: [
539
+ /* @__PURE__ */ jsx(ImageIcon, { size: 32 }),
540
+ /* @__PURE__ */ jsx("span", { children: "Add images or drop files here" }),
541
+ addActions
542
+ ]
543
+ }) : /* @__PURE__ */ jsx(GalleryImageList, {
544
+ entries,
545
+ lastInputRef: newInputRef,
546
+ setEntries,
547
+ onDropFile: replaceEntryFile,
548
+ children: addActions
549
+ })]
414
550
  })
415
- ]
416
- }),
417
- /* @__PURE__ */ jsx(GalleryDialogSubHeader, {
418
- aspect,
419
- fit,
420
- layout,
421
- maxItemHeightInput,
422
- onAspectChange: setAspect,
423
- onFitChange: setFit,
424
- onMaxItemHeightInputChange: setMaxItemHeightInput
425
- }),
426
- /* @__PURE__ */ jsx("div", {
427
- className: galleryDialogBody,
428
- style: bodyDropActive ? {
429
- outline: `2px dashed ${vars.color.accent}`,
430
- outlineOffset: -8,
431
- borderRadius: 8
432
- } : void 0,
433
- onDragEnter: handleBodyDragEnter,
434
- onDragLeave: handleBodyDragLeave,
435
- onDragOver: handleBodyDragOver,
436
- onDrop: handleBodyDrop,
437
- children: entries.length === 0 ? /* @__PURE__ */ jsxs("div", {
438
- className: galleryDialogEmpty,
439
- children: [
440
- /* @__PURE__ */ jsx(ImageIcon, { size: 32 }),
441
- /* @__PURE__ */ jsx("span", { children: "Add your first image or drop files here" }),
442
- /* @__PURE__ */ jsxs("button", {
443
- className: galleryAddBtn,
444
- style: { maxWidth: 200 },
445
- type: "button",
446
- onClick: handleAddImage,
447
- children: [/* @__PURE__ */ jsx(Plus, { size: 14 }), "Add Image"]
448
- })
449
- ]
450
- }) : /* @__PURE__ */ jsxs("div", {
451
- className: galleryImageList,
452
- children: [/* @__PURE__ */ jsxs(DndContext, {
453
- collisionDetection: closestCenter,
454
- sensors,
455
- onDragCancel: () => setDragActiveId(null),
456
- onDragEnd: handleDragEnd,
457
- onDragStart: (event) => {
458
- setDragActiveId(String(event.active.id));
459
- },
460
- children: [/* @__PURE__ */ jsx(SortableContext, {
461
- items: itemIds,
462
- strategy: verticalListSortingStrategy,
463
- children: entries.map((entry, index) => /* @__PURE__ */ jsx(SortableImageCard, {
464
- id: entry.id,
465
- image: entry.image,
466
- index,
467
- isLast: index === entries.length - 1,
468
- lastInputRef: newInputRef,
469
- onDropFile: replaceEntryFile,
470
- onRemove: handleRemoveImage,
471
- onUpdate: handleUpdateImage
472
- }, entry.id))
473
- }), typeof document !== "undefined" ? createPortal(/* @__PURE__ */ jsx("div", {
474
- className: portalClassName,
475
- children: /* @__PURE__ */ jsx(DragOverlay, {
476
- dropAnimation: null,
477
- children: dragActiveEntry ? /* @__PURE__ */ jsx(DragOverlayCard, { image: dragActiveEntry.image }) : null
478
- })
479
- }), document.body) : null]
480
- }), /* @__PURE__ */ jsxs("button", {
481
- className: galleryAddBtn,
482
- type: "button",
483
- onClick: handleAddImage,
484
- children: [/* @__PURE__ */ jsx(Plus, { size: 14 }), "Add Image"]
485
- })]
551
+ }),
552
+ /* @__PURE__ */ jsx(ModalShellFooter, {
553
+ dirty,
554
+ status: imageCountLabel(imageCount),
555
+ onCancel: handleCancel,
556
+ onSave: handleSave
486
557
  })
487
- }),
488
- /* @__PURE__ */ jsxs("div", {
489
- className: galleryDialogFooter,
490
- children: [/* @__PURE__ */ jsx("span", {
491
- className: galleryFooterInfo,
492
- children: `${entries.filter((e) => e.image.src.trim()).length} ${entries.filter((e) => e.image.src.trim()).length !== 1 ? "images" : "image"}`
493
- }), /* @__PURE__ */ jsxs("div", {
494
- className: galleryFooterActions,
495
- children: [/* @__PURE__ */ jsx("button", {
496
- className: galleryFooterBtnCancel,
497
- type: "button",
498
- onClick: dismiss,
499
- children: "Cancel"
500
- }), /* @__PURE__ */ jsx("button", {
501
- className: galleryFooterBtnSave,
502
- type: "button",
503
- onClick: handleSave,
504
- children: "Save"
505
- })]
506
- })]
507
- })
508
- ] });
558
+ ]
559
+ });
509
560
  };
561
+ //#endregion
562
+ //#region src/GalleryEditRenderer.tsx
510
563
  var GalleryEditRenderer = ({ aspect, fit, images, layout, maxItemHeight, onAspectChange, onFitChange, onImagesChange, onLayoutChange, onMaxItemHeightChange }) => {
511
564
  const { className: portalClassName } = usePortalTheme();
512
565
  const theme = useColorScheme();
@@ -514,8 +567,12 @@ var GalleryEditRenderer = ({ aspect, fit, images, layout, maxItemHeight, onAspec
514
567
  const preprocess = useImagePreprocess();
515
568
  const handleOpenEditor = useCallback(() => {
516
569
  if (!onImagesChange || !onLayoutChange) return;
570
+ let dirty = false;
571
+ const setDirty = (next) => {
572
+ dirty = next;
573
+ };
517
574
  presentDialog({
518
- content: ({ dismiss }) => /* @__PURE__ */ jsx(GalleryEditorDialogContent, {
575
+ content: ({ dismiss, requestDismiss }) => /* @__PURE__ */ jsx(GalleryEditorDialog, {
519
576
  dismiss,
520
577
  initialAspect: aspect ?? "auto",
521
578
  initialFit: fit ?? "cover",
@@ -523,8 +580,10 @@ var GalleryEditRenderer = ({ aspect, fit, images, layout, maxItemHeight, onAspec
523
580
  initialLayout: layout,
524
581
  initialMaxItemHeight: maxItemHeight,
525
582
  preprocess,
583
+ requestDismiss,
526
584
  uploader,
527
585
  onAspectChange,
586
+ onDirtyChange: setDirty,
528
587
  onFitChange,
529
588
  onImagesChange,
530
589
  onLayoutChange,
@@ -533,8 +592,10 @@ var GalleryEditRenderer = ({ aspect, fit, images, layout, maxItemHeight, onAspec
533
592
  className: galleryDialogPopup,
534
593
  portalClassName,
535
594
  theme,
595
+ width: 896,
596
+ sheet: "auto",
536
597
  showCloseButton: false,
537
- clickOutsideToDismiss: false
598
+ confirmDismiss: () => !dirty || confirmDiscardChanges()
538
599
  });
539
600
  }, [
540
601
  aspect,
@@ -559,36 +620,25 @@ var GalleryEditRenderer = ({ aspect, fit, images, layout, maxItemHeight, onAspec
559
620
  layout,
560
621
  maxItemHeight
561
622
  });
562
- if (images.length === 0) return /* @__PURE__ */ jsxs("div", {
623
+ return /* @__PURE__ */ jsxs("div", {
563
624
  className: galleryEditContainer,
564
- children: [/* @__PURE__ */ jsxs("div", {
625
+ children: [images.length === 0 ? /* @__PURE__ */ jsxs("div", {
565
626
  className: galleryEmptyState,
566
627
  children: [/* @__PURE__ */ jsx(ImageIcon, { size: 32 }), /* @__PURE__ */ jsx("span", { children: "Empty gallery" })]
567
- }), /* @__PURE__ */ jsx("button", {
568
- className: galleryEditOverlay,
569
- type: "button",
570
- onClick: handleOpenEditor,
571
- children: /* @__PURE__ */ jsxs("span", {
572
- className: galleryEditLabel,
573
- children: [/* @__PURE__ */ jsx(Pencil, { size: 16 }), "Edit Gallery"]
574
- })
575
- })]
576
- });
577
- return /* @__PURE__ */ jsxs("div", {
578
- className: galleryEditContainer,
579
- children: [/* @__PURE__ */ jsx(GalleryRenderer, {
628
+ }) : /* @__PURE__ */ jsx(GalleryRenderer, {
580
629
  aspect,
581
630
  fit,
582
631
  images,
583
632
  layout,
584
633
  maxItemHeight
585
634
  }), /* @__PURE__ */ jsx("button", {
635
+ "aria-label": "Edit gallery",
586
636
  className: galleryEditOverlay,
587
637
  type: "button",
588
638
  onClick: handleOpenEditor,
589
639
  children: /* @__PURE__ */ jsxs("span", {
590
640
  className: galleryEditLabel,
591
- children: [/* @__PURE__ */ jsx(Pencil, { size: 16 }), "Edit Gallery"]
641
+ children: [/* @__PURE__ */ jsx(Pencil, { size: 16 }), "Edit gallery"]
592
642
  })
593
643
  })]
594
644
  });