@haklex/rich-ext-gallery 0.0.82 → 0.0.83

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.
@@ -0,0 +1,349 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { decodeThumbHash, createRendererDecoration } from "@haklex/rich-editor/renderers";
5
+ import { DecoratorNode } from "lexical";
6
+ import { jsx, jsxs } from "react/jsx-runtime";
7
+ import "react-photo-view/dist/react-photo-view.css";
8
+ import { ChevronLeft, ChevronRight } from "lucide-react";
9
+ import { memo, useMemo, useState, useRef, useEffect, useCallback } from "react";
10
+ import { useInView } from "react-intersection-observer";
11
+ import { PhotoView, PhotoProvider } from "react-photo-view";
12
+ var gallery = "_10ydd1n0";
13
+ var galleryGrid = "_10ydd1n1";
14
+ var galleryMasonry = "_10ydd1n2";
15
+ var galleryCarousel = "_10ydd1n3";
16
+ var galleryContainer = "_10ydd1n4";
17
+ var galleryItem = "_10ydd1n5";
18
+ var galleryNav = "_10ydd1n6";
19
+ var galleryNavPrev = "_10ydd1n7";
20
+ var galleryNavNext = "_10ydd1n8";
21
+ var galleryIndicators = "_10ydd1n9";
22
+ var galleryIndicator = "_10ydd1na";
23
+ var galleryIndicatorActive = "_10ydd1nb";
24
+ var galleryEditContainer = "_10ydd1nc";
25
+ var galleryEditOverlay = "_10ydd1nd";
26
+ var galleryEditLabel = "_10ydd1ne";
27
+ var galleryEmptyState = "_10ydd1nf";
28
+ var galleryDialogPopup = "_10ydd1ng";
29
+ var galleryDialogHeader = "_10ydd1nh";
30
+ var galleryDialogTitle = "_10ydd1ni";
31
+ var galleryHeaderActions = "_10ydd1nj";
32
+ var galleryHeaderClose = "_10ydd1nk";
33
+ var galleryDialogBody = "_10ydd1nl";
34
+ var galleryImageList = "_10ydd1nm";
35
+ var galleryImageCard = "_10ydd1nn";
36
+ var galleryImageDragHandle = "_10ydd1no";
37
+ var galleryImageThumb = "_10ydd1np";
38
+ var galleryImageThumbPlaceholder = "_10ydd1nq";
39
+ var galleryImageFields = "_10ydd1nr";
40
+ var galleryImageInput = "_10ydd1ns";
41
+ var galleryImageActions = "_10ydd1nt";
42
+ var galleryImageDeleteBtn = "_10ydd1nu";
43
+ var galleryAddBtn = "_10ydd1nv";
44
+ var galleryDialogFooter = "_10ydd1nw";
45
+ var galleryFooterInfo = "_10ydd1nx";
46
+ var galleryFooterActions = "_10ydd1ny";
47
+ var galleryFooterBtnCancel = "_10ydd1n10 _10ydd1nz";
48
+ var galleryFooterBtnSave = "_10ydd1n11 _10ydd1nz";
49
+ var galleryDialogEmpty = "_10ydd1n12";
50
+ const IMAGE_CONTAINER_MARGIN_INSET = 60;
51
+ const CHILD_GAP = 15;
52
+ const AUTOPLAY_DURATION = 5e3;
53
+ function throttle(func, wait) {
54
+ let timeout = null;
55
+ return ((...args) => {
56
+ if (!timeout) {
57
+ timeout = setTimeout(() => {
58
+ func(...args);
59
+ timeout = null;
60
+ }, wait);
61
+ }
62
+ });
63
+ }
64
+ function clsx(...classes) {
65
+ return classes.filter(Boolean).join(" ");
66
+ }
67
+ function useThumbhashStyle(image) {
68
+ return useMemo(() => {
69
+ if (!image.thumbhash) return;
70
+ const url = decodeThumbHash(image.thumbhash);
71
+ if (!url) return;
72
+ return { backgroundImage: `url(${url})`, backgroundSize: "cover" };
73
+ }, [image.thumbhash]);
74
+ }
75
+ const GalleryRenderer = ({ images, layout }) => {
76
+ const [containerRef, setContainerRef] = useState(null);
77
+ const [, setUpdated] = useState({});
78
+ const memoedChildContainerWidthRef = useRef(0);
79
+ useEffect(() => {
80
+ if (!containerRef) return;
81
+ const ob = new ResizeObserver(() => {
82
+ setUpdated({});
83
+ calChild(containerRef);
84
+ });
85
+ function calChild(containerRef2) {
86
+ const $child = containerRef2.children.item(0);
87
+ if ($child) {
88
+ memoedChildContainerWidthRef.current = $child.clientWidth;
89
+ }
90
+ }
91
+ calChild(containerRef);
92
+ ob.observe(containerRef);
93
+ return () => {
94
+ ob.disconnect();
95
+ };
96
+ }, [containerRef]);
97
+ const [currentIndex, setCurrentIndex] = useState(0);
98
+ const handleOnScroll = useCallback(
99
+ throttle((e) => {
100
+ const $ = e.target;
101
+ const index = Math.floor(
102
+ ($.scrollLeft + IMAGE_CONTAINER_MARGIN_INSET + 15) / memoedChildContainerWidthRef.current
103
+ );
104
+ setCurrentIndex(index);
105
+ }, 60),
106
+ []
107
+ );
108
+ const handleScrollTo = useCallback(
109
+ (i) => {
110
+ if (!containerRef) return;
111
+ containerRef.scrollTo({
112
+ left: memoedChildContainerWidthRef.current * i,
113
+ behavior: "smooth"
114
+ });
115
+ },
116
+ [containerRef]
117
+ );
118
+ const autoplayTimerRef = useRef(null);
119
+ const isForward = useRef(true);
120
+ const autoplayRef = useRef(true);
121
+ const handleCancelAutoplay = useCallback(() => {
122
+ if (!autoplayRef.current) return;
123
+ autoplayRef.current = false;
124
+ if (autoplayTimerRef.current) {
125
+ clearInterval(autoplayTimerRef.current);
126
+ }
127
+ }, []);
128
+ const { ref } = useInView({
129
+ initialInView: false,
130
+ triggerOnce: images.length < 2,
131
+ onChange(inView) {
132
+ if (images.length < 2 || !autoplayRef.current) return;
133
+ if (inView) {
134
+ autoplayTimerRef.current = setInterval(() => {
135
+ setCurrentIndex((prev) => {
136
+ if (prev + 1 > images.length - 1 && isForward.current) {
137
+ isForward.current = false;
138
+ }
139
+ if (prev - 1 < 0 && !isForward.current) {
140
+ isForward.current = true;
141
+ }
142
+ const index = prev + (isForward.current ? 1 : -1);
143
+ handleScrollTo(index);
144
+ return index;
145
+ });
146
+ }, AUTOPLAY_DURATION);
147
+ } else {
148
+ if (autoplayTimerRef.current) {
149
+ clearInterval(autoplayTimerRef.current);
150
+ }
151
+ }
152
+ }
153
+ });
154
+ useEffect(
155
+ () => () => {
156
+ if (autoplayTimerRef.current) {
157
+ clearInterval(autoplayTimerRef.current);
158
+ }
159
+ },
160
+ []
161
+ );
162
+ if (images.length === 0) return null;
163
+ const layoutClass = layout === "masonry" ? galleryMasonry : layout === "carousel" ? galleryCarousel : galleryGrid;
164
+ if (images.length === 1 || layout === "grid" || layout === "masonry") {
165
+ return /* @__PURE__ */ jsx(PhotoProvider, { children: /* @__PURE__ */ jsx("div", { className: clsx(gallery, layoutClass), children: images.map((image, index) => /* @__PURE__ */ jsx(GalleryFigure, { image }, index)) }) });
166
+ }
167
+ return /* @__PURE__ */ jsx(PhotoProvider, { children: /* @__PURE__ */ jsxs(
168
+ "div",
169
+ {
170
+ className: clsx(gallery, galleryCarousel),
171
+ ref,
172
+ onTouchMove: handleCancelAutoplay,
173
+ onWheel: handleCancelAutoplay,
174
+ children: [
175
+ /* @__PURE__ */ jsx(
176
+ "div",
177
+ {
178
+ className: galleryContainer,
179
+ ref: setContainerRef,
180
+ onScroll: handleOnScroll,
181
+ onTouchStart: handleCancelAutoplay,
182
+ children: images.map((image, index) => /* @__PURE__ */ jsx(
183
+ GalleryFigure,
184
+ {
185
+ image,
186
+ style: {
187
+ width: `calc(100% - ${IMAGE_CONTAINER_MARGIN_INSET}px)`,
188
+ marginRight: `${CHILD_GAP}px`
189
+ }
190
+ },
191
+ index
192
+ ))
193
+ }
194
+ ),
195
+ currentIndex > 0 && /* @__PURE__ */ jsx(
196
+ "button",
197
+ {
198
+ className: clsx(galleryNav, galleryNavPrev),
199
+ onClick: () => handleScrollTo(currentIndex - 1),
200
+ children: /* @__PURE__ */ jsx(ChevronLeft, { size: 18 })
201
+ }
202
+ ),
203
+ currentIndex < images.length - 1 && /* @__PURE__ */ jsx(
204
+ "button",
205
+ {
206
+ className: clsx(galleryNav, galleryNavNext),
207
+ onClick: () => handleScrollTo(currentIndex + 1),
208
+ children: /* @__PURE__ */ jsx(ChevronRight, { size: 18 })
209
+ }
210
+ ),
211
+ /* @__PURE__ */ jsx("div", { className: galleryIndicators, children: images.map((_, i) => /* @__PURE__ */ jsx(
212
+ "div",
213
+ {
214
+ className: clsx(
215
+ galleryIndicator,
216
+ currentIndex === i && galleryIndicatorActive
217
+ ),
218
+ onClick: () => handleScrollTo(i)
219
+ },
220
+ i
221
+ )) })
222
+ ]
223
+ }
224
+ ) });
225
+ };
226
+ const GalleryFigure = memo(
227
+ ({ image, style }) => {
228
+ const thumbStyle = useThumbhashStyle(image);
229
+ return /* @__PURE__ */ jsx(PhotoView, { src: image.src, children: /* @__PURE__ */ jsx("figure", { className: galleryItem, style: { ...thumbStyle, ...style }, children: /* @__PURE__ */ jsx(
230
+ "img",
231
+ {
232
+ alt: image.alt || "",
233
+ height: image.height,
234
+ loading: "lazy",
235
+ src: image.src,
236
+ style: { maxWidth: "100%", height: "auto" },
237
+ width: image.width
238
+ }
239
+ ) }) });
240
+ }
241
+ );
242
+ const GalleryRenderer_default = memo(GalleryRenderer);
243
+ class GalleryNode extends DecoratorNode {
244
+ constructor(payload, key) {
245
+ super(key);
246
+ __publicField(this, "__images");
247
+ __publicField(this, "__layout");
248
+ this.__images = payload.images;
249
+ this.__layout = payload.layout || "grid";
250
+ }
251
+ static getType() {
252
+ return "gallery";
253
+ }
254
+ static clone(node) {
255
+ return new GalleryNode(
256
+ {
257
+ images: node.__images.map((img) => ({ ...img })),
258
+ layout: node.__layout
259
+ },
260
+ node.__key
261
+ );
262
+ }
263
+ createDOM(_config) {
264
+ const div = document.createElement("div");
265
+ div.className = `rich-gallery rich-gallery-${this.__layout}`;
266
+ return div;
267
+ }
268
+ updateDOM() {
269
+ return false;
270
+ }
271
+ isInline() {
272
+ return false;
273
+ }
274
+ static importJSON(serializedNode) {
275
+ return $createGalleryNode({
276
+ images: serializedNode.images,
277
+ layout: serializedNode.layout
278
+ });
279
+ }
280
+ exportJSON() {
281
+ return {
282
+ ...super.exportJSON(),
283
+ type: "gallery",
284
+ images: this.__images,
285
+ layout: this.__layout,
286
+ version: 1
287
+ };
288
+ }
289
+ getImages() {
290
+ return this.getLatest().__images;
291
+ }
292
+ setImages(images) {
293
+ const writable = this.getWritable();
294
+ writable.__images = images;
295
+ }
296
+ getLayout() {
297
+ return this.getLatest().__layout;
298
+ }
299
+ setLayout(layout) {
300
+ const writable = this.getWritable();
301
+ writable.__layout = layout;
302
+ }
303
+ decorate(_editor, _config) {
304
+ const props = {
305
+ images: this.__images,
306
+ layout: this.__layout
307
+ };
308
+ return createRendererDecoration("Gallery", GalleryRenderer, props);
309
+ }
310
+ }
311
+ function $createGalleryNode(payload) {
312
+ return new GalleryNode(payload);
313
+ }
314
+ function $isGalleryNode(node) {
315
+ return node instanceof GalleryNode;
316
+ }
317
+ export {
318
+ $createGalleryNode as $,
319
+ galleryImageDeleteBtn as A,
320
+ $isGalleryNode as B,
321
+ GalleryRenderer_default as C,
322
+ GalleryNode as G,
323
+ GalleryRenderer as a,
324
+ galleryEditContainer as b,
325
+ galleryEmptyState as c,
326
+ galleryEditOverlay as d,
327
+ galleryEditLabel as e,
328
+ galleryDialogHeader as f,
329
+ galleryDialogPopup as g,
330
+ galleryDialogTitle as h,
331
+ galleryHeaderActions as i,
332
+ galleryHeaderClose as j,
333
+ galleryDialogBody as k,
334
+ galleryDialogEmpty as l,
335
+ galleryAddBtn as m,
336
+ galleryImageList as n,
337
+ galleryDialogFooter as o,
338
+ galleryFooterInfo as p,
339
+ galleryFooterActions as q,
340
+ galleryFooterBtnCancel as r,
341
+ galleryFooterBtnSave as s,
342
+ galleryImageCard as t,
343
+ galleryImageDragHandle as u,
344
+ galleryImageThumb as v,
345
+ galleryImageThumbPlaceholder as w,
346
+ galleryImageFields as x,
347
+ galleryImageInput as y,
348
+ galleryImageActions as z
349
+ };