Skip to content

Commit f777a3e

Browse files
committed
fix bugs with cropping
1 parent 3fb475f commit f777a3e

2 files changed

Lines changed: 89 additions & 43 deletions

File tree

‎src/common/debounce.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
export function debounce(func: Function, timeout = 300) {
2-
let timer;
3-
return (...args) => {
4-
clearTimeout(timer);
5-
timer = setTimeout(() => {
6-
func.apply(this, args);
7-
}, timeout);
1+
export function debounce<T extends (...args: any[]) => void>(
2+
func: T,
3+
wait: number = 300
4+
): (...args: Parameters<T>) => void {
5+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
6+
7+
return (...args: Parameters<T>) => {
8+
if (timeoutId !== undefined) {
9+
clearTimeout(timeoutId);
10+
}
11+
timeoutId = setTimeout(() => {
12+
func(...args);
13+
}, wait);
814
};
915
}

‎src/components/ui/ImageCropModal.tsx‎

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,126 @@
1-
import { onMount } from "solid-js";
1+
import { onMount, createSignal, onCleanup } from "solid-js";
22
import LegacyModal from "./legacy-modal/LegacyModal";
33
import Croppie, { CropData } from "croppie";
44
import "croppie/croppie.css";
55
import Button from "./Button";
66
import { t } from "@nerimity/i18lite";
7+
import { debounce } from "@/common/debounce";
78

89
export default function ImageCropModal(props: {
910
close(): void;
1011
image: string;
1112
onCropped(points: number[]): void;
1213
type?: "avatar" | "banner";
1314
}) {
14-
let imageEl: undefined | HTMLImageElement;
15+
let imageEl: HTMLDivElement | undefined;
16+
let containerRef: HTMLDivElement | undefined;
1517
let croppie: Croppie | undefined;
1618

17-
onMount(() => {
19+
// We use a signal to track the available width for responsive calc
20+
const [containerWidth, setContainerWidth] = createSignal(500);
21+
22+
const initCroppie = (width: number) => {
1823
if (!imageEl) return;
1924

25+
// Cleanup old instance if it exists
26+
if (croppie) {
27+
croppie.destroy();
28+
}
29+
2030
const isBanner = props.type === "banner";
31+
const padding = 40;
32+
const availableWidth = width - padding;
33+
34+
// Calculate viewport size to fit the screen
35+
// If it's a banner (400x150), we maintain a 2.66 aspect ratio
36+
// If it's an avatar (300x300), we keep it square
37+
const vw = isBanner
38+
? Math.min(400, availableWidth)
39+
: Math.min(300, availableWidth);
40+
const vh = isBanner ? vw / 2.66 : vw;
2141

2242
croppie = new Croppie(imageEl, {
2343
viewport: {
2444
type: isBanner ? "square" : "circle",
25-
width: isBanner ? 400 : 300,
26-
height: isBanner ? 150 : 300,
45+
width: vw,
46+
height: vh,
2747
},
2848
boundary: {
2949
width: "100%",
3050
height: 400,
3151
},
52+
showZoomer: true,
53+
enableOrientation: true,
54+
enforceBoundary: true, // This prevents the "escaping into void"
3255
});
33-
croppie.bind({
34-
url: props.image,
56+
57+
croppie.bind({ url: props.image });
58+
59+
imageEl.addEventListener(
60+
"update",
61+
debounce((ev) => {
62+
const result = (ev as any).detail as CropData;
63+
if (result?.points) {
64+
props.onCropped(result.points.map((v) => parseInt(v as any)));
65+
}
66+
}, 50)
67+
);
68+
};
69+
70+
onMount(() => {
71+
if (!containerRef) return;
72+
73+
// Observe size changes to re-init if the window resizes significantly
74+
const ro = new ResizeObserver((entries) => {
75+
for (const entry of entries) {
76+
const newWidth = entry.contentRect.width;
77+
if (Math.abs(newWidth - containerWidth()) > 50) {
78+
setContainerWidth(newWidth);
79+
initCroppie(newWidth);
80+
}
81+
}
3582
});
3683

37-
imageEl.addEventListener("update", (ev) => {
38-
if (!("detail" in ev)) return;
39-
const result = ev.detail as CropData;
40-
if (!result || !result.points) return;
41-
const pointsToInt = result.points.map((v) =>
42-
parseInt(v as unknown as string)
43-
);
84+
ro.observe(containerRef);
85+
initCroppie(containerRef.clientWidth);
4486

45-
props.onCropped(pointsToInt);
87+
onCleanup(() => {
88+
ro.disconnect();
89+
croppie?.destroy();
4690
});
4791
});
4892

4993
const onClick = () => {
5094
const result = croppie?.get();
51-
if (!result || !result.points) return;
52-
53-
const pointsToInt = result.points.map((v) =>
54-
parseInt(v as unknown as string)
55-
);
56-
props.onCropped(pointsToInt);
95+
if (result?.points) {
96+
props.onCropped(result.points.map((v) => parseInt(v as any)));
97+
}
5798
props.close();
5899
};
59100

60-
const ActionButtons = (
61-
<Button
62-
iconName="check"
63-
label={t("imageCropModal.done")}
64-
onClick={onClick}
65-
styles={{ flex: 1 }}
66-
primary
67-
padding={10}
68-
/>
69-
);
70-
71101
return (
72102
<LegacyModal
73103
title={t("imageCropModal.title")}
74104
close={props.close}
75-
maxWidth={500}
76-
actionButtons={ActionButtons}
105+
maxWidth={550}
106+
actionButtons={
107+
<Button
108+
iconName="check"
109+
label={t("imageCropModal.done")}
110+
onClick={onClick}
111+
styles={{ flex: 1 }}
112+
primary
113+
padding={10}
114+
/>
115+
}
77116
>
78117
<div
118+
ref={containerRef}
79119
style={{
80120
"user-select": "none",
81121
width: "100%",
82-
height: "400px",
83-
"margin-bottom": "50px",
122+
"min-height": "450px",
123+
"margin-bottom": "20px",
84124
}}
85125
>
86126
<div ref={imageEl!} />

0 commit comments

Comments
 (0)