@alexgorbatchev/shadcn 0.0.70 → 0.0.71
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 +56 -1
- package/dist/components/image-viewer/ImageComparison.d.ts +41 -0
- package/dist/components/image-viewer/ImageComparisonToolbar.d.ts +22 -0
- package/dist/components/image-viewer/ImageDifferenceOverlay.d.ts +8 -0
- package/dist/components/image-viewer/ImageSwipeDivider.d.ts +15 -0
- package/dist/components/image-viewer/ImageViewer.d.ts +34 -0
- package/dist/components/image-viewer/ImageViewport.d.ts +23 -0
- package/dist/components/image-viewer/ImageZoomToggle.d.ts +19 -0
- package/dist/components/image-viewer/computeImageDiff.d.ts +11 -0
- package/dist/components/image-viewer/getContainedRect.d.ts +3 -0
- package/dist/components/image-viewer/getImageStageSize.d.ts +6 -0
- package/dist/components/image-viewer/getNormalizedImagePoint.d.ts +7 -0
- package/dist/components/image-viewer/hooks/useElementSize.d.ts +4 -0
- package/dist/components/image-viewer/hooks/useImageDifferences.d.ts +20 -0
- package/dist/components/image-viewer/hooks/useImageLoad.d.ts +9 -0
- package/dist/components/image-viewer/index.d.ts +9 -0
- package/dist/components/image-viewer/index.js +1114 -0
- package/dist/components/image-viewer/measureImageDifferences.d.ts +19 -0
- package/dist/components/image-viewer/types.d.ts +84 -0
- package/dist/index.js +23 -23
- package/dist/prototype.js +5 -1
- package/llms/components/data-display/ImageComparison.md +153 -0
- package/llms/components/data-display/ImageViewer.md +117 -0
- package/llms/components/data-display/ImageZoomToggle.md +82 -0
- package/llms/setup.md +56 -1
- package/llms.txt +3 -0
- package/package.json +5 -1
|
@@ -0,0 +1,1114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cn2
|
|
3
|
+
} from "../../chunk-en44w1pn.js";
|
|
4
|
+
import"../../chunk-7ny0vyek.js";
|
|
5
|
+
import"../../chunk-8ssktt4b.js";
|
|
6
|
+
import {
|
|
7
|
+
ToggleGroup2,
|
|
8
|
+
ToggleGroupItem2
|
|
9
|
+
} from "../../chunk-9n617bte.js";
|
|
10
|
+
import {
|
|
11
|
+
Separator2
|
|
12
|
+
} from "../../chunk-52aadmkc.js";
|
|
13
|
+
import"../../chunk-e6hk39ev.js";
|
|
14
|
+
import"../../chunk-x90xmrba.js";
|
|
15
|
+
import"../../chunk-jpvw9xqy.js";
|
|
16
|
+
import {
|
|
17
|
+
Button2
|
|
18
|
+
} from "../../chunk-bd09gygt.js";
|
|
19
|
+
import"../../chunk-m32ptxf3.js";
|
|
20
|
+
import {
|
|
21
|
+
Field2,
|
|
22
|
+
FieldContent2,
|
|
23
|
+
FieldLabel2,
|
|
24
|
+
FieldDescription2
|
|
25
|
+
} from "../../chunk-9n0axzd5.js";
|
|
26
|
+
import {
|
|
27
|
+
Switch2
|
|
28
|
+
} from "../../chunk-m98d89xc.js";
|
|
29
|
+
import"../../chunk-5mydsjp3.js";
|
|
30
|
+
|
|
31
|
+
// src/components/image-viewer/ImageViewer.tsx
|
|
32
|
+
import { useControlled } from "@base-ui/utils/useControlled";
|
|
33
|
+
import { useRef as useRef2 } from "react";
|
|
34
|
+
|
|
35
|
+
// src/components/image-viewer/ImageViewport.tsx
|
|
36
|
+
import { useIsoLayoutEffect as useIsoLayoutEffect2 } from "@base-ui/utils/useIsoLayoutEffect";
|
|
37
|
+
import { useMergedRefs } from "@base-ui/utils/useMergedRefs";
|
|
38
|
+
import { useRef } from "react";
|
|
39
|
+
|
|
40
|
+
// src/components/image-viewer/getImageStageSize.ts
|
|
41
|
+
function getImageStageSize(natural, viewport, zoom) {
|
|
42
|
+
const width = Math.max(0, natural.width);
|
|
43
|
+
const height = Math.max(0, natural.height);
|
|
44
|
+
const scale = zoom === "original" || width === 0 || height === 0 ? 1 : Math.max(0, Math.min(1, viewport.width / width, viewport.height / height));
|
|
45
|
+
return { width: width * scale, height: height * scale, scale };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/components/image-viewer/hooks/useElementSize.ts
|
|
49
|
+
import { useIsoLayoutEffect } from "@base-ui/utils/useIsoLayoutEffect";
|
|
50
|
+
import { useState } from "react";
|
|
51
|
+
function useElementSize(ref) {
|
|
52
|
+
const [size, setSize] = useState();
|
|
53
|
+
useIsoLayoutEffect(() => {
|
|
54
|
+
const element = ref.current;
|
|
55
|
+
if (!element)
|
|
56
|
+
return;
|
|
57
|
+
const measure = () => {
|
|
58
|
+
const width = element.clientWidth;
|
|
59
|
+
const height = element.clientHeight;
|
|
60
|
+
setSize((current) => current?.width === width && current.height === height ? current : { width, height });
|
|
61
|
+
};
|
|
62
|
+
measure();
|
|
63
|
+
const observer = new ResizeObserver(measure);
|
|
64
|
+
observer.observe(element);
|
|
65
|
+
return () => observer.disconnect();
|
|
66
|
+
}, [ref]);
|
|
67
|
+
return size;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/components/image-viewer/ImageViewport.tsx
|
|
71
|
+
import { jsx } from "react/jsx-runtime";
|
|
72
|
+
function ImageViewport({
|
|
73
|
+
label,
|
|
74
|
+
zoom,
|
|
75
|
+
naturalSize,
|
|
76
|
+
center,
|
|
77
|
+
children,
|
|
78
|
+
fallback,
|
|
79
|
+
viewportRef,
|
|
80
|
+
onViewportScroll
|
|
81
|
+
}) {
|
|
82
|
+
const elementRef = useRef(null);
|
|
83
|
+
const ref = useMergedRefs(elementRef, viewportRef);
|
|
84
|
+
const viewportSize = useElementSize(elementRef);
|
|
85
|
+
const stage = naturalSize && viewportSize ? getImageStageSize(naturalSize, viewportSize, zoom) : undefined;
|
|
86
|
+
const stageWidth = stage?.width ?? 0;
|
|
87
|
+
const stageHeight = stage?.height ?? 0;
|
|
88
|
+
useIsoLayoutEffect2(() => {
|
|
89
|
+
const viewport = elementRef.current;
|
|
90
|
+
if (!viewport || stageWidth === 0 || stageHeight === 0)
|
|
91
|
+
return;
|
|
92
|
+
if (zoom === "fit") {
|
|
93
|
+
center.current = { x: 0.5, y: 0.5 };
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
viewport.scrollLeft = center.current.x * stageWidth - viewport.clientWidth / 2;
|
|
97
|
+
viewport.scrollTop = center.current.y * stageHeight - viewport.clientHeight / 2;
|
|
98
|
+
}, [center, stageHeight, stageWidth, zoom]);
|
|
99
|
+
function handleScroll(event) {
|
|
100
|
+
const viewport = event.currentTarget;
|
|
101
|
+
if (zoom === "original" && stageWidth > 0 && stageHeight > 0) {
|
|
102
|
+
center.current = {
|
|
103
|
+
x: stageWidth > viewport.clientWidth ? (viewport.scrollLeft + viewport.clientWidth / 2) / stageWidth : 0.5,
|
|
104
|
+
y: stageHeight > viewport.clientHeight ? (viewport.scrollTop + viewport.clientHeight / 2) / stageHeight : 0.5
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
onViewportScroll?.(viewport);
|
|
108
|
+
}
|
|
109
|
+
return /* @__PURE__ */ jsx("div", {
|
|
110
|
+
ref,
|
|
111
|
+
role: "region",
|
|
112
|
+
"aria-label": label,
|
|
113
|
+
tabIndex: 0,
|
|
114
|
+
"data-slot": "image-viewport",
|
|
115
|
+
"data-zoom": zoom,
|
|
116
|
+
onScroll: handleScroll,
|
|
117
|
+
className: cn2("flex min-h-0 min-w-0 flex-1 overscroll-contain outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-inset", zoom === "fit" ? "overflow-hidden" : "overflow-auto"),
|
|
118
|
+
children: fallback ?? /* @__PURE__ */ jsx("div", {
|
|
119
|
+
"data-slot": "image-stage",
|
|
120
|
+
className: "relative m-auto shrink-0",
|
|
121
|
+
style: { width: stageWidth, height: stageHeight },
|
|
122
|
+
children
|
|
123
|
+
})
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/components/image-viewer/ImageZoomToggle.tsx
|
|
128
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
129
|
+
function ImageZoomToggle({
|
|
130
|
+
zoom,
|
|
131
|
+
onZoomChange,
|
|
132
|
+
label = "Zoom",
|
|
133
|
+
size = "sm",
|
|
134
|
+
orientation = "horizontal",
|
|
135
|
+
layout = "content",
|
|
136
|
+
disabled = false,
|
|
137
|
+
density
|
|
138
|
+
}) {
|
|
139
|
+
return /* @__PURE__ */ jsxs(ToggleGroup2, {
|
|
140
|
+
"aria-label": label,
|
|
141
|
+
required: true,
|
|
142
|
+
value: [zoom],
|
|
143
|
+
onValueChange: (value) => onZoomChange(value[0] === "original" ? "original" : "fit"),
|
|
144
|
+
variant: "outline",
|
|
145
|
+
size,
|
|
146
|
+
spacing: 0,
|
|
147
|
+
orientation,
|
|
148
|
+
layout,
|
|
149
|
+
disabled,
|
|
150
|
+
density,
|
|
151
|
+
children: [
|
|
152
|
+
/* @__PURE__ */ jsx2(ToggleGroupItem2, {
|
|
153
|
+
value: "fit",
|
|
154
|
+
children: "Fit to screen"
|
|
155
|
+
}),
|
|
156
|
+
/* @__PURE__ */ jsx2(ToggleGroupItem2, {
|
|
157
|
+
value: "original",
|
|
158
|
+
children: "Original size"
|
|
159
|
+
})
|
|
160
|
+
]
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/components/image-viewer/hooks/useImageLoad.ts
|
|
165
|
+
import { useState as useState2 } from "react";
|
|
166
|
+
function useImageLoad(src) {
|
|
167
|
+
const [state, setState] = useState2({ src, status: "loading" });
|
|
168
|
+
const handlers = {
|
|
169
|
+
onLoad: (event) => {
|
|
170
|
+
const image = event.currentTarget;
|
|
171
|
+
setState({ src, status: "loaded", size: { width: image.naturalWidth, height: image.naturalHeight } });
|
|
172
|
+
},
|
|
173
|
+
onError: () => setState({ src, status: "error" })
|
|
174
|
+
};
|
|
175
|
+
return [state.src === src ? state : { status: "loading" }, handlers];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/components/image-viewer/ImageViewer.tsx
|
|
179
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
180
|
+
function ImageViewer({
|
|
181
|
+
src,
|
|
182
|
+
alt,
|
|
183
|
+
crossOrigin,
|
|
184
|
+
zoom: zoomProp,
|
|
185
|
+
defaultZoom = "fit",
|
|
186
|
+
onZoomChange,
|
|
187
|
+
showZoomControl = false,
|
|
188
|
+
overlay,
|
|
189
|
+
imageRef,
|
|
190
|
+
overlayRef,
|
|
191
|
+
label,
|
|
192
|
+
onLoad,
|
|
193
|
+
onError,
|
|
194
|
+
density
|
|
195
|
+
}) {
|
|
196
|
+
const [zoom, setZoomState] = useControlled({
|
|
197
|
+
controlled: zoomProp,
|
|
198
|
+
default: defaultZoom,
|
|
199
|
+
name: "ImageViewer",
|
|
200
|
+
state: "zoom"
|
|
201
|
+
});
|
|
202
|
+
const center = useRef2({ x: 0.5, y: 0.5 });
|
|
203
|
+
const [image, imageHandlers] = useImageLoad(src);
|
|
204
|
+
function setZoom(next) {
|
|
205
|
+
setZoomState(next);
|
|
206
|
+
onZoomChange?.(next);
|
|
207
|
+
}
|
|
208
|
+
return /* @__PURE__ */ jsxs2("div", {
|
|
209
|
+
"data-slot": "image-viewer",
|
|
210
|
+
"data-scroll": "none",
|
|
211
|
+
"data-density": density,
|
|
212
|
+
"data-zoom": zoom,
|
|
213
|
+
"data-status": image.status,
|
|
214
|
+
className: "flex size-full min-h-0 min-w-0 flex-1 flex-col gap-2",
|
|
215
|
+
children: [
|
|
216
|
+
showZoomControl && /* @__PURE__ */ jsx3("div", {
|
|
217
|
+
"data-slot": "image-viewer-toolbar",
|
|
218
|
+
className: "flex shrink-0 justify-end",
|
|
219
|
+
children: /* @__PURE__ */ jsx3(ImageZoomToggle, {
|
|
220
|
+
zoom,
|
|
221
|
+
onZoomChange: setZoom,
|
|
222
|
+
density
|
|
223
|
+
})
|
|
224
|
+
}),
|
|
225
|
+
/* @__PURE__ */ jsxs2(ImageViewport, {
|
|
226
|
+
label: label ?? alt,
|
|
227
|
+
zoom,
|
|
228
|
+
naturalSize: image.size,
|
|
229
|
+
center,
|
|
230
|
+
fallback: image.status === "error" ? /* @__PURE__ */ jsx3("p", {
|
|
231
|
+
"data-slot": "image-viewer-error",
|
|
232
|
+
className: "m-auto p-panel text-center text-sm text-muted-foreground",
|
|
233
|
+
children: "Image could not be loaded."
|
|
234
|
+
}) : undefined,
|
|
235
|
+
children: [
|
|
236
|
+
/* @__PURE__ */ jsx3("img", {
|
|
237
|
+
ref: imageRef,
|
|
238
|
+
src,
|
|
239
|
+
alt,
|
|
240
|
+
crossOrigin,
|
|
241
|
+
draggable: false,
|
|
242
|
+
"data-slot": "image-viewer-image",
|
|
243
|
+
className: "block size-full select-none",
|
|
244
|
+
onLoad: (event) => {
|
|
245
|
+
imageHandlers.onLoad(event);
|
|
246
|
+
onLoad?.(event);
|
|
247
|
+
},
|
|
248
|
+
onError: (event) => {
|
|
249
|
+
imageHandlers.onError();
|
|
250
|
+
onError?.(event);
|
|
251
|
+
}
|
|
252
|
+
}),
|
|
253
|
+
overlay !== undefined && /* @__PURE__ */ jsx3("div", {
|
|
254
|
+
ref: overlayRef,
|
|
255
|
+
"data-slot": "image-viewer-overlay",
|
|
256
|
+
className: "absolute inset-0",
|
|
257
|
+
children: overlay
|
|
258
|
+
})
|
|
259
|
+
]
|
|
260
|
+
})
|
|
261
|
+
]
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
// src/components/image-viewer/ImageComparison.tsx
|
|
265
|
+
import { useControlled as useControlled2 } from "@base-ui/utils/useControlled";
|
|
266
|
+
import { useRef as useRef4, useState as useState4 } from "react";
|
|
267
|
+
|
|
268
|
+
// src/components/image-viewer/ImageComparisonToolbar.tsx
|
|
269
|
+
import { Columns2Icon, Rows2Icon } from "lucide-react";
|
|
270
|
+
import { useId } from "react";
|
|
271
|
+
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
272
|
+
function ToolbarSection({ title, titleId, children }) {
|
|
273
|
+
return /* @__PURE__ */ jsxs3("div", {
|
|
274
|
+
"data-slot": "image-comparison-toolbar-section",
|
|
275
|
+
className: "flex min-w-0 flex-col gap-1.5",
|
|
276
|
+
children: [
|
|
277
|
+
/* @__PURE__ */ jsx4("span", {
|
|
278
|
+
id: titleId,
|
|
279
|
+
className: "sr-only text-xs font-medium text-muted-foreground @3xl:not-sr-only",
|
|
280
|
+
children: title
|
|
281
|
+
}),
|
|
282
|
+
children
|
|
283
|
+
]
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
function toMode(value) {
|
|
287
|
+
return value === "swipe" || value === "side-by-side" ? value : "toggle";
|
|
288
|
+
}
|
|
289
|
+
function isPeekKey(event) {
|
|
290
|
+
return event.key === " " || event.key === "Enter";
|
|
291
|
+
}
|
|
292
|
+
function describeDifferences(differences, showDifferences) {
|
|
293
|
+
if (differences.status === "unavailable")
|
|
294
|
+
return differences.reason;
|
|
295
|
+
if (!showDifferences)
|
|
296
|
+
return "";
|
|
297
|
+
if (differences.status !== "ready")
|
|
298
|
+
return "Comparing pixels…";
|
|
299
|
+
const count = differences.diff.regions.length;
|
|
300
|
+
if (count === 0)
|
|
301
|
+
return "No visible differences";
|
|
302
|
+
return `${count} changed ${count === 1 ? "region" : "regions"}`;
|
|
303
|
+
}
|
|
304
|
+
function formatSize(size) {
|
|
305
|
+
return `${size.width} × ${size.height}`;
|
|
306
|
+
}
|
|
307
|
+
function ImageComparisonToolbar({
|
|
308
|
+
mode,
|
|
309
|
+
onModeChange,
|
|
310
|
+
orientation,
|
|
311
|
+
onOrientationChange,
|
|
312
|
+
zoom,
|
|
313
|
+
onZoomChange,
|
|
314
|
+
showDifferences,
|
|
315
|
+
onShowDifferencesChange,
|
|
316
|
+
differences,
|
|
317
|
+
onPeekChange,
|
|
318
|
+
beforeLabel,
|
|
319
|
+
afterLabel,
|
|
320
|
+
beforeSize,
|
|
321
|
+
afterSize,
|
|
322
|
+
density
|
|
323
|
+
}) {
|
|
324
|
+
const id = useId();
|
|
325
|
+
const isUnavailable = differences.status === "unavailable";
|
|
326
|
+
const releasePeek = () => onPeekChange(false);
|
|
327
|
+
const sizeMismatch = beforeSize && afterSize && (beforeSize.width !== afterSize.width || beforeSize.height !== afterSize.height) ? `Sizes differ: ${beforeLabel} ${formatSize(beforeSize)}, ${afterLabel} ${formatSize(afterSize)}. ` + `${beforeLabel} is fitted inside the ${afterLabel} image box.` : undefined;
|
|
328
|
+
return /* @__PURE__ */ jsxs3("div", {
|
|
329
|
+
role: "group",
|
|
330
|
+
"aria-label": "Comparison controls",
|
|
331
|
+
"data-slot": "image-comparison-toolbar",
|
|
332
|
+
"data-density": density,
|
|
333
|
+
className: "flex shrink-0 flex-wrap items-end gap-x-panel gap-y-3 border-t border-border p-panel-sm @3xl:w-72 @3xl:flex-col @3xl:flex-nowrap @3xl:items-stretch @3xl:overflow-y-auto @3xl:border-t-0 @3xl:border-s",
|
|
334
|
+
children: [
|
|
335
|
+
/* @__PURE__ */ jsx4(ToolbarSection, {
|
|
336
|
+
title: "Mode",
|
|
337
|
+
titleId: `${id}-mode`,
|
|
338
|
+
children: /* @__PURE__ */ jsxs3(ToggleGroup2, {
|
|
339
|
+
"aria-labelledby": `${id}-mode`,
|
|
340
|
+
required: true,
|
|
341
|
+
value: [mode],
|
|
342
|
+
onValueChange: (value) => onModeChange(toMode(value[0])),
|
|
343
|
+
variant: "outline",
|
|
344
|
+
size: "sm",
|
|
345
|
+
spacing: 0,
|
|
346
|
+
children: [
|
|
347
|
+
/* @__PURE__ */ jsx4(ToggleGroupItem2, {
|
|
348
|
+
value: "toggle",
|
|
349
|
+
children: "Toggle"
|
|
350
|
+
}),
|
|
351
|
+
/* @__PURE__ */ jsx4(ToggleGroupItem2, {
|
|
352
|
+
value: "swipe",
|
|
353
|
+
children: "Swipe"
|
|
354
|
+
}),
|
|
355
|
+
/* @__PURE__ */ jsx4(ToggleGroupItem2, {
|
|
356
|
+
value: "side-by-side",
|
|
357
|
+
children: "Side by side"
|
|
358
|
+
})
|
|
359
|
+
]
|
|
360
|
+
})
|
|
361
|
+
}),
|
|
362
|
+
/* @__PURE__ */ jsx4(ToolbarSection, {
|
|
363
|
+
title: "Orientation",
|
|
364
|
+
titleId: `${id}-orientation`,
|
|
365
|
+
children: /* @__PURE__ */ jsxs3(ToggleGroup2, {
|
|
366
|
+
"aria-labelledby": `${id}-orientation`,
|
|
367
|
+
required: true,
|
|
368
|
+
value: [orientation],
|
|
369
|
+
onValueChange: (value) => onOrientationChange(value[0] === "vertical" ? "vertical" : "horizontal"),
|
|
370
|
+
variant: "outline",
|
|
371
|
+
size: "sm",
|
|
372
|
+
spacing: 0,
|
|
373
|
+
disabled: mode !== "side-by-side",
|
|
374
|
+
children: [
|
|
375
|
+
/* @__PURE__ */ jsxs3(ToggleGroupItem2, {
|
|
376
|
+
value: "horizontal",
|
|
377
|
+
children: [
|
|
378
|
+
/* @__PURE__ */ jsx4(Columns2Icon, {
|
|
379
|
+
"data-icon": "inline-start"
|
|
380
|
+
}),
|
|
381
|
+
"Horizontal"
|
|
382
|
+
]
|
|
383
|
+
}),
|
|
384
|
+
/* @__PURE__ */ jsxs3(ToggleGroupItem2, {
|
|
385
|
+
value: "vertical",
|
|
386
|
+
children: [
|
|
387
|
+
/* @__PURE__ */ jsx4(Rows2Icon, {
|
|
388
|
+
"data-icon": "inline-start"
|
|
389
|
+
}),
|
|
390
|
+
"Vertical"
|
|
391
|
+
]
|
|
392
|
+
})
|
|
393
|
+
]
|
|
394
|
+
})
|
|
395
|
+
}),
|
|
396
|
+
/* @__PURE__ */ jsx4(ToolbarSection, {
|
|
397
|
+
title: "Zoom",
|
|
398
|
+
titleId: `${id}-zoom`,
|
|
399
|
+
children: /* @__PURE__ */ jsx4(ImageZoomToggle, {
|
|
400
|
+
zoom,
|
|
401
|
+
onZoomChange
|
|
402
|
+
})
|
|
403
|
+
}),
|
|
404
|
+
/* @__PURE__ */ jsx4(Button2, {
|
|
405
|
+
variant: "outline",
|
|
406
|
+
size: "sm",
|
|
407
|
+
disabled: mode !== "toggle",
|
|
408
|
+
className: "touch-none",
|
|
409
|
+
onPointerDown: (event) => {
|
|
410
|
+
if (event.button === 0)
|
|
411
|
+
onPeekChange(true);
|
|
412
|
+
},
|
|
413
|
+
onPointerUp: releasePeek,
|
|
414
|
+
onPointerLeave: releasePeek,
|
|
415
|
+
onPointerCancel: releasePeek,
|
|
416
|
+
onKeyDown: (event) => {
|
|
417
|
+
if (isPeekKey(event))
|
|
418
|
+
onPeekChange(true);
|
|
419
|
+
},
|
|
420
|
+
onKeyUp: (event) => {
|
|
421
|
+
if (isPeekKey(event))
|
|
422
|
+
releasePeek();
|
|
423
|
+
},
|
|
424
|
+
onBlur: releasePeek,
|
|
425
|
+
onContextMenu: (event) => event.preventDefault(),
|
|
426
|
+
children: "Hold to show before"
|
|
427
|
+
}),
|
|
428
|
+
/* @__PURE__ */ jsxs3(Field2, {
|
|
429
|
+
orientation: "horizontal",
|
|
430
|
+
children: [
|
|
431
|
+
/* @__PURE__ */ jsx4(Switch2, {
|
|
432
|
+
id: `${id}-differences`,
|
|
433
|
+
"aria-describedby": `${id}-differences-status`,
|
|
434
|
+
checked: showDifferences && !isUnavailable,
|
|
435
|
+
disabled: isUnavailable,
|
|
436
|
+
onCheckedChange: onShowDifferencesChange
|
|
437
|
+
}),
|
|
438
|
+
/* @__PURE__ */ jsxs3(FieldContent2, {
|
|
439
|
+
children: [
|
|
440
|
+
/* @__PURE__ */ jsx4(FieldLabel2, {
|
|
441
|
+
htmlFor: `${id}-differences`,
|
|
442
|
+
children: "Highlight differences"
|
|
443
|
+
}),
|
|
444
|
+
showDifferences && differences.status === "ready" && /* @__PURE__ */ jsxs3(FieldDescription2, {
|
|
445
|
+
"data-slot": "image-comparison-legend",
|
|
446
|
+
className: "flex items-center gap-1.5",
|
|
447
|
+
children: [
|
|
448
|
+
/* @__PURE__ */ jsx4("span", {
|
|
449
|
+
"aria-hidden": "true",
|
|
450
|
+
className: "mx-[3px] size-2.5 shrink-0 rounded-xs bg-warning/10 ring-1 ring-background outline-2 outline-offset-1 outline-warning"
|
|
451
|
+
}),
|
|
452
|
+
"Changed region"
|
|
453
|
+
]
|
|
454
|
+
})
|
|
455
|
+
]
|
|
456
|
+
})
|
|
457
|
+
]
|
|
458
|
+
}),
|
|
459
|
+
/* @__PURE__ */ jsxs3("div", {
|
|
460
|
+
"data-slot": "image-comparison-status",
|
|
461
|
+
className: "flex min-w-0 flex-col gap-1 text-xs text-muted-foreground",
|
|
462
|
+
children: [
|
|
463
|
+
/* @__PURE__ */ jsx4("p", {
|
|
464
|
+
id: `${id}-differences-status`,
|
|
465
|
+
role: "status",
|
|
466
|
+
children: describeDifferences(differences, showDifferences)
|
|
467
|
+
}),
|
|
468
|
+
sizeMismatch && /* @__PURE__ */ jsx4("p", {
|
|
469
|
+
"data-slot": "image-comparison-size-mismatch",
|
|
470
|
+
children: sizeMismatch
|
|
471
|
+
})
|
|
472
|
+
]
|
|
473
|
+
})
|
|
474
|
+
]
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// src/components/image-viewer/ImageDifferenceOverlay.tsx
|
|
479
|
+
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
480
|
+
function toPercent(value, total) {
|
|
481
|
+
return `${value / total * 100}%`;
|
|
482
|
+
}
|
|
483
|
+
function ImageDifferenceOverlay({ diff }) {
|
|
484
|
+
return /* @__PURE__ */ jsx5("div", {
|
|
485
|
+
"data-slot": "image-difference-overlay",
|
|
486
|
+
"aria-hidden": "true",
|
|
487
|
+
className: "pointer-events-none absolute inset-0",
|
|
488
|
+
children: diff.regions.map((region) => /* @__PURE__ */ jsx5("div", {
|
|
489
|
+
"data-slot": "image-difference-region",
|
|
490
|
+
className: "absolute rounded-xs bg-warning/10 shadow-[0_0_0_4px_var(--background)] ring-1 ring-background outline-2 outline-offset-1 outline-warning",
|
|
491
|
+
style: {
|
|
492
|
+
left: toPercent(region.x, diff.width),
|
|
493
|
+
top: toPercent(region.y, diff.height),
|
|
494
|
+
width: toPercent(region.width, diff.width),
|
|
495
|
+
height: toPercent(region.height, diff.height)
|
|
496
|
+
}
|
|
497
|
+
}, `${region.x}:${region.y}:${region.width}:${region.height}`))
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// src/components/image-viewer/ImageSwipeDivider.tsx
|
|
502
|
+
import { DirectionProvider } from "@base-ui/react/direction-provider";
|
|
503
|
+
import { Slider as SliderPrimitive } from "@base-ui/react/slider";
|
|
504
|
+
import { ChevronsLeftRightIcon } from "lucide-react";
|
|
505
|
+
import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
506
|
+
function ImageSwipeDivider({
|
|
507
|
+
value,
|
|
508
|
+
onValueChange,
|
|
509
|
+
beforeLabel,
|
|
510
|
+
afterLabel
|
|
511
|
+
}) {
|
|
512
|
+
return /* @__PURE__ */ jsx6(DirectionProvider, {
|
|
513
|
+
direction: "ltr",
|
|
514
|
+
children: /* @__PURE__ */ jsx6(SliderPrimitive.Root, {
|
|
515
|
+
dir: "ltr",
|
|
516
|
+
"data-slot": "image-swipe-divider",
|
|
517
|
+
value,
|
|
518
|
+
onValueChange,
|
|
519
|
+
min: 0,
|
|
520
|
+
max: 100,
|
|
521
|
+
step: 1,
|
|
522
|
+
largeStep: 10,
|
|
523
|
+
className: "pointer-events-none absolute inset-0",
|
|
524
|
+
children: /* @__PURE__ */ jsx6(SliderPrimitive.Control, {
|
|
525
|
+
className: "pointer-events-none absolute inset-0",
|
|
526
|
+
children: /* @__PURE__ */ jsxs4(SliderPrimitive.Thumb, {
|
|
527
|
+
"data-slot": "image-swipe-divider-handle",
|
|
528
|
+
getAriaLabel: () => "Swipe divider",
|
|
529
|
+
getAriaValueText: (_formatted, current) => `${beforeLabel} ${current}%, ${afterLabel} ${100 - current}%`,
|
|
530
|
+
className: "group/divider pointer-events-auto flex h-full w-6 cursor-ew-resize touch-none flex-col items-center justify-center select-none",
|
|
531
|
+
children: [
|
|
532
|
+
/* @__PURE__ */ jsx6("span", {
|
|
533
|
+
"aria-hidden": "true",
|
|
534
|
+
className: "absolute inset-y-0 start-1/2 w-0.5 -translate-x-1/2 bg-foreground ring-1 ring-background"
|
|
535
|
+
}),
|
|
536
|
+
/* @__PURE__ */ jsx6("span", {
|
|
537
|
+
"aria-hidden": "true",
|
|
538
|
+
className: "relative flex h-8 w-5 items-center justify-center rounded-sm border border-border bg-background text-foreground shadow-frame group-has-[:focus-visible]/divider:border-ring group-has-[:focus-visible]/divider:ring-1 group-has-[:focus-visible]/divider:ring-ring group-data-dragging/divider:border-ring",
|
|
539
|
+
children: /* @__PURE__ */ jsx6(ChevronsLeftRightIcon, {
|
|
540
|
+
className: "size-3.5"
|
|
541
|
+
})
|
|
542
|
+
})
|
|
543
|
+
]
|
|
544
|
+
})
|
|
545
|
+
})
|
|
546
|
+
})
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// src/components/image-viewer/hooks/useImageDifferences.ts
|
|
551
|
+
import { useEffect, useRef as useRef3, useState as useState3 } from "react";
|
|
552
|
+
|
|
553
|
+
// src/components/image-viewer/computeImageDiff.ts
|
|
554
|
+
var DEFAULT_THRESHOLD = 32;
|
|
555
|
+
var DEFAULT_CELL_SIZE = 16;
|
|
556
|
+
var DEFAULT_MIN_CHANGED_PIXELS = 2;
|
|
557
|
+
function getPixelDelta(before, after, offset) {
|
|
558
|
+
const beforeAlpha = before[offset + 3];
|
|
559
|
+
const afterAlpha = after[offset + 3];
|
|
560
|
+
return Math.max(Math.abs(beforeAlpha - afterAlpha), Math.abs(before[offset] * beforeAlpha - after[offset] * afterAlpha) / 255, Math.abs(before[offset + 1] * beforeAlpha - after[offset + 1] * afterAlpha) / 255, Math.abs(before[offset + 2] * beforeAlpha - after[offset + 2] * afterAlpha) / 255);
|
|
561
|
+
}
|
|
562
|
+
function collectRegions(cells, counts, bounds, columns, rows) {
|
|
563
|
+
const visited = new Uint8Array(cells.length);
|
|
564
|
+
const regions = [];
|
|
565
|
+
const stack = [];
|
|
566
|
+
for (let start = 0;start < cells.length; start++) {
|
|
567
|
+
if (!cells[start] || visited[start])
|
|
568
|
+
continue;
|
|
569
|
+
visited[start] = 1;
|
|
570
|
+
stack.push(start);
|
|
571
|
+
let left = Number.POSITIVE_INFINITY;
|
|
572
|
+
let top = Number.POSITIVE_INFINITY;
|
|
573
|
+
let right = Number.NEGATIVE_INFINITY;
|
|
574
|
+
let bottom = Number.NEGATIVE_INFINITY;
|
|
575
|
+
let changedPixels = 0;
|
|
576
|
+
for (let cell = stack.pop();cell !== undefined; cell = stack.pop()) {
|
|
577
|
+
left = Math.min(left, bounds[cell * 4]);
|
|
578
|
+
top = Math.min(top, bounds[cell * 4 + 1]);
|
|
579
|
+
right = Math.max(right, bounds[cell * 4 + 2]);
|
|
580
|
+
bottom = Math.max(bottom, bounds[cell * 4 + 3]);
|
|
581
|
+
changedPixels += counts[cell];
|
|
582
|
+
const column = cell % columns;
|
|
583
|
+
const row = (cell - column) / columns;
|
|
584
|
+
for (let neighborRow = Math.max(0, row - 1);neighborRow <= Math.min(rows - 1, row + 1); neighborRow++) {
|
|
585
|
+
for (let neighborColumn = Math.max(0, column - 1);neighborColumn <= Math.min(columns - 1, column + 1); neighborColumn++) {
|
|
586
|
+
const neighbor = neighborRow * columns + neighborColumn;
|
|
587
|
+
if (cells[neighbor] && !visited[neighbor]) {
|
|
588
|
+
visited[neighbor] = 1;
|
|
589
|
+
stack.push(neighbor);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
regions.push({ x: left, y: top, width: right - left + 1, height: bottom - top + 1, changedPixels });
|
|
595
|
+
}
|
|
596
|
+
return regions.sort((first, second) => first.y - second.y || first.x - second.x);
|
|
597
|
+
}
|
|
598
|
+
function createImageDiffAccumulator(width, height, {
|
|
599
|
+
threshold = DEFAULT_THRESHOLD,
|
|
600
|
+
cellSize = DEFAULT_CELL_SIZE,
|
|
601
|
+
minChangedPixels = DEFAULT_MIN_CHANGED_PIXELS
|
|
602
|
+
} = {}) {
|
|
603
|
+
const size = Math.max(1, Math.floor(cellSize));
|
|
604
|
+
const columns = Math.ceil(width / size);
|
|
605
|
+
const rows = Math.ceil(height / size);
|
|
606
|
+
const counts = new Uint32Array(columns * rows);
|
|
607
|
+
const bounds = new Int32Array(columns * rows * 4);
|
|
608
|
+
for (let cell = 0;cell < counts.length; cell++) {
|
|
609
|
+
bounds[cell * 4] = width;
|
|
610
|
+
bounds[cell * 4 + 1] = height;
|
|
611
|
+
bounds[cell * 4 + 2] = -1;
|
|
612
|
+
bounds[cell * 4 + 3] = -1;
|
|
613
|
+
}
|
|
614
|
+
return {
|
|
615
|
+
addRows(before, after, startY, rowCount) {
|
|
616
|
+
for (let row = 0;row < rowCount; row++) {
|
|
617
|
+
const y = startY + row;
|
|
618
|
+
const cellRowStart = Math.floor(y / size) * columns;
|
|
619
|
+
let offset = row * width * 4;
|
|
620
|
+
for (let x = 0;x < width; x++, offset += 4) {
|
|
621
|
+
if (getPixelDelta(before, after, offset) <= threshold)
|
|
622
|
+
continue;
|
|
623
|
+
const cell = cellRowStart + Math.floor(x / size);
|
|
624
|
+
counts[cell]++;
|
|
625
|
+
const boundsOffset = cell * 4;
|
|
626
|
+
if (x < bounds[boundsOffset])
|
|
627
|
+
bounds[boundsOffset] = x;
|
|
628
|
+
if (y < bounds[boundsOffset + 1])
|
|
629
|
+
bounds[boundsOffset + 1] = y;
|
|
630
|
+
if (x > bounds[boundsOffset + 2])
|
|
631
|
+
bounds[boundsOffset + 2] = x;
|
|
632
|
+
if (y > bounds[boundsOffset + 3])
|
|
633
|
+
bounds[boundsOffset + 3] = y;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
},
|
|
637
|
+
finish() {
|
|
638
|
+
const cells = new Uint8Array(counts.length);
|
|
639
|
+
for (let cell = 0;cell < counts.length; cell++) {
|
|
640
|
+
if (counts[cell] >= Math.max(1, minChangedPixels))
|
|
641
|
+
cells[cell] = 1;
|
|
642
|
+
}
|
|
643
|
+
const regions = collectRegions(cells, counts, bounds, columns, rows);
|
|
644
|
+
const changedPixels = regions.reduce((total, region) => total + region.changedPixels, 0);
|
|
645
|
+
return { width, height, cellSize: size, columns, rows, cells, regions, changedPixels };
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
function computeImageDiff(before, after, options) {
|
|
650
|
+
if (before.width !== after.width || before.height !== after.height) {
|
|
651
|
+
throw new RangeError(`Cannot compare ${before.width}×${before.height} pixels with ${after.width}×${after.height} pixels.`);
|
|
652
|
+
}
|
|
653
|
+
const length = before.width * before.height * 4;
|
|
654
|
+
if (before.data.length < length || after.data.length < length) {
|
|
655
|
+
throw new RangeError(`Expected ${length} RGBA values for a ${before.width}×${before.height} image.`);
|
|
656
|
+
}
|
|
657
|
+
const accumulator = createImageDiffAccumulator(after.width, after.height, options);
|
|
658
|
+
accumulator.addRows(before.data, after.data, 0, after.height);
|
|
659
|
+
return accumulator.finish();
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// src/components/image-viewer/getContainedRect.ts
|
|
663
|
+
function getContainedRect(source, frame) {
|
|
664
|
+
if (source.width <= 0 || source.height <= 0)
|
|
665
|
+
return { x: frame.width / 2, y: frame.height / 2, width: 0, height: 0 };
|
|
666
|
+
const scale = Math.min(frame.width / source.width, frame.height / source.height);
|
|
667
|
+
const width = source.width * scale;
|
|
668
|
+
const height = source.height * scale;
|
|
669
|
+
return { x: (frame.width - width) / 2, y: (frame.height - height) / 2, width, height };
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// src/components/image-viewer/measureImageDifferences.ts
|
|
673
|
+
var DEFAULT_SLICE_BUDGET = 8;
|
|
674
|
+
var BAND_HEIGHT = 32;
|
|
675
|
+
function yieldToMain() {
|
|
676
|
+
if (typeof scheduler !== "undefined" && typeof scheduler.yield === "function")
|
|
677
|
+
return scheduler.yield();
|
|
678
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
679
|
+
}
|
|
680
|
+
function createReadableContext(image, width, height) {
|
|
681
|
+
const canvas = image.ownerDocument.createElement("canvas");
|
|
682
|
+
canvas.width = width;
|
|
683
|
+
canvas.height = height;
|
|
684
|
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
685
|
+
if (!context)
|
|
686
|
+
throw new Error("Canvas 2D drawing is unavailable.");
|
|
687
|
+
return context;
|
|
688
|
+
}
|
|
689
|
+
function isDOMException(error, name) {
|
|
690
|
+
return error instanceof DOMException && error.name === name;
|
|
691
|
+
}
|
|
692
|
+
function describeImageReadError(error) {
|
|
693
|
+
if (isDOMException(error, "AbortError"))
|
|
694
|
+
return;
|
|
695
|
+
if (isDOMException(error, "SecurityError")) {
|
|
696
|
+
return "Pixels cannot be read: an image comes from another origin without CORS access.";
|
|
697
|
+
}
|
|
698
|
+
if (isDOMException(error, "EncodingError"))
|
|
699
|
+
return "Pixels cannot be read: an image could not be decoded.";
|
|
700
|
+
return "Pixels cannot be read in this browser.";
|
|
701
|
+
}
|
|
702
|
+
async function getImageReadFailure(image) {
|
|
703
|
+
try {
|
|
704
|
+
await image.decode();
|
|
705
|
+
const context = createReadableContext(image, 1, 1);
|
|
706
|
+
context.drawImage(image, 0, 0, 1, 1);
|
|
707
|
+
context.getImageData(0, 0, 1, 1);
|
|
708
|
+
return;
|
|
709
|
+
} catch (error) {
|
|
710
|
+
return describeImageReadError(error);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
async function measureImageDifferences(before, after, {
|
|
714
|
+
signal,
|
|
715
|
+
sliceBudget = DEFAULT_SLICE_BUDGET,
|
|
716
|
+
yieldToMain: yieldTask = yieldToMain,
|
|
717
|
+
now = () => performance.now(),
|
|
718
|
+
...options
|
|
719
|
+
} = {}) {
|
|
720
|
+
await Promise.all([before.decode(), after.decode()]);
|
|
721
|
+
signal?.throwIfAborted();
|
|
722
|
+
const width = after.naturalWidth;
|
|
723
|
+
const height = after.naturalHeight;
|
|
724
|
+
const beforeContext = createReadableContext(after, width, height);
|
|
725
|
+
const afterContext = createReadableContext(after, width, height);
|
|
726
|
+
const placement = getContainedRect({ width: before.naturalWidth, height: before.naturalHeight }, { width, height });
|
|
727
|
+
beforeContext.drawImage(before, placement.x, placement.y, placement.width, placement.height);
|
|
728
|
+
await yieldTask();
|
|
729
|
+
signal?.throwIfAborted();
|
|
730
|
+
afterContext.drawImage(after, 0, 0, width, height);
|
|
731
|
+
await yieldTask();
|
|
732
|
+
signal?.throwIfAborted();
|
|
733
|
+
const accumulator = createImageDiffAccumulator(width, height, options);
|
|
734
|
+
let sliceStart = now();
|
|
735
|
+
for (let y = 0;y < height; y += BAND_HEIGHT) {
|
|
736
|
+
const rowCount = Math.min(BAND_HEIGHT, height - y);
|
|
737
|
+
accumulator.addRows(beforeContext.getImageData(0, y, width, rowCount).data, afterContext.getImageData(0, y, width, rowCount).data, y, rowCount);
|
|
738
|
+
if (now() - sliceStart >= sliceBudget) {
|
|
739
|
+
await yieldTask();
|
|
740
|
+
signal?.throwIfAborted();
|
|
741
|
+
sliceStart = now();
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
return accumulator.finish();
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// src/components/image-viewer/hooks/useImageDifferences.ts
|
|
748
|
+
function withEntry(entries, key, state) {
|
|
749
|
+
const next = new Map(entries);
|
|
750
|
+
if (state)
|
|
751
|
+
next.set(key, state);
|
|
752
|
+
else
|
|
753
|
+
next.delete(key);
|
|
754
|
+
return next;
|
|
755
|
+
}
|
|
756
|
+
function useImageDifferences({
|
|
757
|
+
pairKey,
|
|
758
|
+
enabled,
|
|
759
|
+
loaded,
|
|
760
|
+
loadFailure,
|
|
761
|
+
beforeRef,
|
|
762
|
+
afterRef
|
|
763
|
+
}) {
|
|
764
|
+
const [entries, setEntries] = useState3(() => new Map);
|
|
765
|
+
const startedRef = useRef3(new Set);
|
|
766
|
+
const controllerRef = useRef3(undefined);
|
|
767
|
+
useEffect(() => () => controllerRef.current?.abort(), [pairKey]);
|
|
768
|
+
useEffect(() => {
|
|
769
|
+
const before = beforeRef.current;
|
|
770
|
+
const after = afterRef.current;
|
|
771
|
+
if (!loaded || !before || !after)
|
|
772
|
+
return;
|
|
773
|
+
let isCurrent = true;
|
|
774
|
+
Promise.all([getImageReadFailure(before), getImageReadFailure(after)]).then((failures) => {
|
|
775
|
+
const reason = failures.find((failure) => failure !== undefined);
|
|
776
|
+
if (isCurrent && reason)
|
|
777
|
+
setEntries((current) => withEntry(current, pairKey, { status: "unavailable", reason }));
|
|
778
|
+
});
|
|
779
|
+
return () => {
|
|
780
|
+
isCurrent = false;
|
|
781
|
+
};
|
|
782
|
+
}, [afterRef, beforeRef, loaded, pairKey]);
|
|
783
|
+
useEffect(() => {
|
|
784
|
+
const before = beforeRef.current;
|
|
785
|
+
const after = afterRef.current;
|
|
786
|
+
if (!enabled || !loaded || !before || !after || startedRef.current.has(pairKey))
|
|
787
|
+
return;
|
|
788
|
+
const controller = new AbortController;
|
|
789
|
+
controllerRef.current = controller;
|
|
790
|
+
startedRef.current.add(pairKey);
|
|
791
|
+
setEntries((current) => withEntry(current, pairKey, { status: "measuring" }));
|
|
792
|
+
measureImageDifferences(before, after, { signal: controller.signal }).then((diff) => setEntries((current) => withEntry(current, pairKey, { status: "ready", diff })), (error) => {
|
|
793
|
+
const reason = describeImageReadError(error);
|
|
794
|
+
if (!reason)
|
|
795
|
+
startedRef.current.delete(pairKey);
|
|
796
|
+
setEntries((current) => withEntry(current, pairKey, reason ? { status: "unavailable", reason } : undefined));
|
|
797
|
+
});
|
|
798
|
+
}, [afterRef, beforeRef, enabled, loaded, pairKey]);
|
|
799
|
+
if (loadFailure)
|
|
800
|
+
return { status: "unavailable", reason: loadFailure };
|
|
801
|
+
return entries.get(pairKey) ?? { status: "idle" };
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// src/components/image-viewer/ImageComparison.tsx
|
|
805
|
+
import { jsx as jsx7, jsxs as jsxs5, Fragment } from "react/jsx-runtime";
|
|
806
|
+
function ComparisonPane({ header, children }) {
|
|
807
|
+
return /* @__PURE__ */ jsxs5("div", {
|
|
808
|
+
"data-slot": "image-comparison-pane",
|
|
809
|
+
"data-scroll": "none",
|
|
810
|
+
className: "flex min-h-0 min-w-0 flex-1 flex-col",
|
|
811
|
+
children: [
|
|
812
|
+
/* @__PURE__ */ jsx7("div", {
|
|
813
|
+
"data-slot": "image-comparison-pane-header",
|
|
814
|
+
className: "flex h-control shrink-0 items-center justify-between gap-2 px-control-x text-xs font-medium text-muted-foreground",
|
|
815
|
+
children: header
|
|
816
|
+
}),
|
|
817
|
+
children
|
|
818
|
+
]
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
function ImageComparison({
|
|
822
|
+
before,
|
|
823
|
+
after,
|
|
824
|
+
beforeLabel = "Before",
|
|
825
|
+
afterLabel = "After",
|
|
826
|
+
mode: modeProp,
|
|
827
|
+
defaultMode = "toggle",
|
|
828
|
+
onModeChange,
|
|
829
|
+
orientation: orientationProp,
|
|
830
|
+
defaultOrientation = "horizontal",
|
|
831
|
+
onOrientationChange,
|
|
832
|
+
zoom: zoomProp,
|
|
833
|
+
defaultZoom = "fit",
|
|
834
|
+
onZoomChange,
|
|
835
|
+
showDifferences: showDifferencesProp,
|
|
836
|
+
defaultShowDifferences = false,
|
|
837
|
+
onShowDifferencesChange,
|
|
838
|
+
showToolbar = true,
|
|
839
|
+
label = "Image comparison",
|
|
840
|
+
density
|
|
841
|
+
}) {
|
|
842
|
+
const [mode, setModeState] = useControlled2({
|
|
843
|
+
controlled: modeProp,
|
|
844
|
+
default: defaultMode,
|
|
845
|
+
name: "ImageComparison",
|
|
846
|
+
state: "mode"
|
|
847
|
+
});
|
|
848
|
+
const [orientation, setOrientationState] = useControlled2({
|
|
849
|
+
controlled: orientationProp,
|
|
850
|
+
default: defaultOrientation,
|
|
851
|
+
name: "ImageComparison",
|
|
852
|
+
state: "orientation"
|
|
853
|
+
});
|
|
854
|
+
const [zoom, setZoomState] = useControlled2({
|
|
855
|
+
controlled: zoomProp,
|
|
856
|
+
default: defaultZoom,
|
|
857
|
+
name: "ImageComparison",
|
|
858
|
+
state: "zoom"
|
|
859
|
+
});
|
|
860
|
+
const [showDifferences, setShowDifferencesState] = useControlled2({
|
|
861
|
+
controlled: showDifferencesProp,
|
|
862
|
+
default: defaultShowDifferences,
|
|
863
|
+
name: "ImageComparison",
|
|
864
|
+
state: "showDifferences"
|
|
865
|
+
});
|
|
866
|
+
const [isPeeking, setIsPeeking] = useState4(false);
|
|
867
|
+
const [position, setPosition] = useState4(50);
|
|
868
|
+
const [beforeImage, beforeHandlers] = useImageLoad(before.src);
|
|
869
|
+
const [afterImage, afterHandlers] = useImageLoad(after.src);
|
|
870
|
+
const beforeRef = useRef4(null);
|
|
871
|
+
const afterRef = useRef4(null);
|
|
872
|
+
const beforeViewportRef = useRef4(null);
|
|
873
|
+
const afterViewportRef = useRef4(null);
|
|
874
|
+
const expectedScrollRef = useRef4(new WeakMap);
|
|
875
|
+
const center = useRef4({ x: 0.5, y: 0.5 });
|
|
876
|
+
const failedLabel = afterImage.status === "error" ? afterLabel : beforeImage.status === "error" ? beforeLabel : undefined;
|
|
877
|
+
const differences = useImageDifferences({
|
|
878
|
+
pairKey: `${before.src}
|
|
879
|
+
${after.src}`,
|
|
880
|
+
enabled: showDifferences,
|
|
881
|
+
loaded: beforeImage.status === "loaded" && afterImage.status === "loaded",
|
|
882
|
+
loadFailure: failedLabel ? `Pixels cannot be read: the ${failedLabel} image could not be loaded.` : undefined,
|
|
883
|
+
beforeRef,
|
|
884
|
+
afterRef
|
|
885
|
+
});
|
|
886
|
+
const isShowingBefore = mode === "toggle" && isPeeking;
|
|
887
|
+
const overlay = showDifferences && differences.status === "ready" ? /* @__PURE__ */ jsx7(ImageDifferenceOverlay, {
|
|
888
|
+
diff: differences.diff
|
|
889
|
+
}) : null;
|
|
890
|
+
const fallback = failedLabel ? /* @__PURE__ */ jsxs5("p", {
|
|
891
|
+
"data-slot": "image-comparison-error",
|
|
892
|
+
className: "m-auto p-panel text-center text-sm text-muted-foreground",
|
|
893
|
+
children: [
|
|
894
|
+
"The ",
|
|
895
|
+
failedLabel,
|
|
896
|
+
" image could not be loaded."
|
|
897
|
+
]
|
|
898
|
+
}) : undefined;
|
|
899
|
+
function setMode(next) {
|
|
900
|
+
setModeState(next);
|
|
901
|
+
onModeChange?.(next);
|
|
902
|
+
}
|
|
903
|
+
function setOrientation(next) {
|
|
904
|
+
setOrientationState(next);
|
|
905
|
+
onOrientationChange?.(next);
|
|
906
|
+
}
|
|
907
|
+
function setZoom(next) {
|
|
908
|
+
setZoomState(next);
|
|
909
|
+
onZoomChange?.(next);
|
|
910
|
+
}
|
|
911
|
+
function setShowDifferences(next) {
|
|
912
|
+
setShowDifferencesState(next);
|
|
913
|
+
onShowDifferencesChange?.(next);
|
|
914
|
+
}
|
|
915
|
+
function syncScroll(source, target) {
|
|
916
|
+
const expected = expectedScrollRef.current.get(source);
|
|
917
|
+
expectedScrollRef.current.delete(source);
|
|
918
|
+
if (!target || expected?.left === source.scrollLeft && expected.top === source.scrollTop)
|
|
919
|
+
return;
|
|
920
|
+
const previous = { left: target.scrollLeft, top: target.scrollTop };
|
|
921
|
+
target.scrollLeft = source.scrollLeft;
|
|
922
|
+
target.scrollTop = source.scrollTop;
|
|
923
|
+
if (target.scrollLeft !== previous.left || target.scrollTop !== previous.top) {
|
|
924
|
+
expectedScrollRef.current.set(target, { left: target.scrollLeft, top: target.scrollTop });
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
function renderAfterImage(className) {
|
|
928
|
+
return /* @__PURE__ */ jsx7("img", {
|
|
929
|
+
ref: afterRef,
|
|
930
|
+
src: after.src,
|
|
931
|
+
alt: after.alt,
|
|
932
|
+
crossOrigin: after.crossOrigin,
|
|
933
|
+
draggable: false,
|
|
934
|
+
"data-slot": "image-comparison-after",
|
|
935
|
+
className: cn2("absolute inset-0 size-full bg-background select-none", className),
|
|
936
|
+
onLoad: afterHandlers.onLoad,
|
|
937
|
+
onError: afterHandlers.onError
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
function renderBeforeImage(className, style) {
|
|
941
|
+
return /* @__PURE__ */ jsx7("img", {
|
|
942
|
+
ref: beforeRef,
|
|
943
|
+
src: before.src,
|
|
944
|
+
alt: before.alt,
|
|
945
|
+
crossOrigin: before.crossOrigin,
|
|
946
|
+
draggable: false,
|
|
947
|
+
"data-slot": "image-comparison-before",
|
|
948
|
+
className: cn2("absolute inset-0 size-full bg-background object-contain select-none", className),
|
|
949
|
+
style,
|
|
950
|
+
onLoad: beforeHandlers.onLoad,
|
|
951
|
+
onError: beforeHandlers.onError
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
let content;
|
|
955
|
+
if (mode === "side-by-side") {
|
|
956
|
+
content = /* @__PURE__ */ jsxs5("div", {
|
|
957
|
+
"data-slot": "image-comparison-panes",
|
|
958
|
+
className: cn2("flex min-h-0 min-w-0 flex-1", orientation === "vertical" ? "flex-col" : "flex-row"),
|
|
959
|
+
children: [
|
|
960
|
+
/* @__PURE__ */ jsx7(ComparisonPane, {
|
|
961
|
+
header: /* @__PURE__ */ jsx7("span", {
|
|
962
|
+
children: beforeLabel
|
|
963
|
+
}),
|
|
964
|
+
children: /* @__PURE__ */ jsxs5(ImageViewport, {
|
|
965
|
+
label: beforeLabel,
|
|
966
|
+
zoom,
|
|
967
|
+
naturalSize: afterImage.size,
|
|
968
|
+
center,
|
|
969
|
+
fallback,
|
|
970
|
+
viewportRef: beforeViewportRef,
|
|
971
|
+
onViewportScroll: (viewport) => syncScroll(viewport, afterViewportRef.current),
|
|
972
|
+
children: [
|
|
973
|
+
renderBeforeImage(),
|
|
974
|
+
overlay
|
|
975
|
+
]
|
|
976
|
+
})
|
|
977
|
+
}),
|
|
978
|
+
/* @__PURE__ */ jsx7(Separator2, {
|
|
979
|
+
orientation: orientation === "vertical" ? "horizontal" : "vertical"
|
|
980
|
+
}),
|
|
981
|
+
/* @__PURE__ */ jsx7(ComparisonPane, {
|
|
982
|
+
header: /* @__PURE__ */ jsx7("span", {
|
|
983
|
+
children: afterLabel
|
|
984
|
+
}),
|
|
985
|
+
children: /* @__PURE__ */ jsxs5(ImageViewport, {
|
|
986
|
+
label: afterLabel,
|
|
987
|
+
zoom,
|
|
988
|
+
naturalSize: afterImage.size,
|
|
989
|
+
center,
|
|
990
|
+
fallback,
|
|
991
|
+
viewportRef: afterViewportRef,
|
|
992
|
+
onViewportScroll: (viewport) => syncScroll(viewport, beforeViewportRef.current),
|
|
993
|
+
children: [
|
|
994
|
+
renderAfterImage(),
|
|
995
|
+
overlay
|
|
996
|
+
]
|
|
997
|
+
})
|
|
998
|
+
})
|
|
999
|
+
]
|
|
1000
|
+
});
|
|
1001
|
+
} else if (mode === "swipe") {
|
|
1002
|
+
content = /* @__PURE__ */ jsx7(ComparisonPane, {
|
|
1003
|
+
header: /* @__PURE__ */ jsxs5(Fragment, {
|
|
1004
|
+
children: [
|
|
1005
|
+
/* @__PURE__ */ jsx7("span", {
|
|
1006
|
+
children: beforeLabel
|
|
1007
|
+
}),
|
|
1008
|
+
/* @__PURE__ */ jsx7("span", {
|
|
1009
|
+
children: afterLabel
|
|
1010
|
+
})
|
|
1011
|
+
]
|
|
1012
|
+
}),
|
|
1013
|
+
children: /* @__PURE__ */ jsxs5(ImageViewport, {
|
|
1014
|
+
label: `${beforeLabel} and ${afterLabel}`,
|
|
1015
|
+
zoom,
|
|
1016
|
+
naturalSize: afterImage.size,
|
|
1017
|
+
center,
|
|
1018
|
+
fallback,
|
|
1019
|
+
children: [
|
|
1020
|
+
renderAfterImage(),
|
|
1021
|
+
renderBeforeImage(undefined, { clipPath: `inset(0 ${100 - position}% 0 0)` }),
|
|
1022
|
+
overlay,
|
|
1023
|
+
/* @__PURE__ */ jsx7(ImageSwipeDivider, {
|
|
1024
|
+
value: position,
|
|
1025
|
+
onValueChange: setPosition,
|
|
1026
|
+
beforeLabel,
|
|
1027
|
+
afterLabel
|
|
1028
|
+
})
|
|
1029
|
+
]
|
|
1030
|
+
})
|
|
1031
|
+
});
|
|
1032
|
+
} else {
|
|
1033
|
+
const showingLabel = isShowingBefore ? beforeLabel : afterLabel;
|
|
1034
|
+
content = /* @__PURE__ */ jsx7(ComparisonPane, {
|
|
1035
|
+
header: /* @__PURE__ */ jsxs5("span", {
|
|
1036
|
+
"data-slot": "image-comparison-showing",
|
|
1037
|
+
"aria-live": "polite",
|
|
1038
|
+
children: [
|
|
1039
|
+
"Showing ",
|
|
1040
|
+
showingLabel
|
|
1041
|
+
]
|
|
1042
|
+
}),
|
|
1043
|
+
children: /* @__PURE__ */ jsxs5(ImageViewport, {
|
|
1044
|
+
label: showingLabel,
|
|
1045
|
+
zoom,
|
|
1046
|
+
naturalSize: afterImage.size,
|
|
1047
|
+
center,
|
|
1048
|
+
fallback,
|
|
1049
|
+
children: [
|
|
1050
|
+
renderAfterImage(isShowingBefore ? "invisible" : undefined),
|
|
1051
|
+
renderBeforeImage(isShowingBefore ? undefined : "invisible"),
|
|
1052
|
+
overlay
|
|
1053
|
+
]
|
|
1054
|
+
})
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
return /* @__PURE__ */ jsx7("div", {
|
|
1058
|
+
role: "group",
|
|
1059
|
+
"aria-label": label,
|
|
1060
|
+
"data-slot": "image-comparison",
|
|
1061
|
+
"data-scroll": "none",
|
|
1062
|
+
"data-density": density,
|
|
1063
|
+
"data-mode": mode,
|
|
1064
|
+
"data-orientation": orientation,
|
|
1065
|
+
"data-zoom": zoom,
|
|
1066
|
+
"data-showing": isShowingBefore ? "before" : "after",
|
|
1067
|
+
className: "@container flex size-full min-h-0 min-w-0 flex-1",
|
|
1068
|
+
children: /* @__PURE__ */ jsxs5("div", {
|
|
1069
|
+
className: "flex min-h-0 min-w-0 flex-1 flex-col @3xl:flex-row",
|
|
1070
|
+
children: [
|
|
1071
|
+
/* @__PURE__ */ jsx7("div", {
|
|
1072
|
+
"data-slot": "image-comparison-stage",
|
|
1073
|
+
"data-scroll": "none",
|
|
1074
|
+
className: "flex min-h-0 min-w-0 flex-1",
|
|
1075
|
+
children: content
|
|
1076
|
+
}),
|
|
1077
|
+
showToolbar && /* @__PURE__ */ jsx7(ImageComparisonToolbar, {
|
|
1078
|
+
mode,
|
|
1079
|
+
onModeChange: setMode,
|
|
1080
|
+
orientation,
|
|
1081
|
+
onOrientationChange: setOrientation,
|
|
1082
|
+
zoom,
|
|
1083
|
+
onZoomChange: setZoom,
|
|
1084
|
+
showDifferences,
|
|
1085
|
+
onShowDifferencesChange: setShowDifferences,
|
|
1086
|
+
differences,
|
|
1087
|
+
onPeekChange: setIsPeeking,
|
|
1088
|
+
beforeLabel,
|
|
1089
|
+
afterLabel,
|
|
1090
|
+
beforeSize: beforeImage.size,
|
|
1091
|
+
afterSize: afterImage.size,
|
|
1092
|
+
density
|
|
1093
|
+
})
|
|
1094
|
+
]
|
|
1095
|
+
})
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
// src/components/image-viewer/getNormalizedImagePoint.ts
|
|
1099
|
+
function clampUnit(value) {
|
|
1100
|
+
return Math.min(1, Math.max(0, value));
|
|
1101
|
+
}
|
|
1102
|
+
function getNormalizedImagePoint(point, box) {
|
|
1103
|
+
return {
|
|
1104
|
+
x: box.width > 0 ? clampUnit((point.clientX - box.left) / box.width) : 0,
|
|
1105
|
+
y: box.height > 0 ? clampUnit((point.clientY - box.top) / box.height) : 0
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
export {
|
|
1109
|
+
ImageComparison,
|
|
1110
|
+
ImageViewer,
|
|
1111
|
+
ImageZoomToggle,
|
|
1112
|
+
computeImageDiff,
|
|
1113
|
+
getNormalizedImagePoint
|
|
1114
|
+
};
|