|
1 | | -import { onMount } from "solid-js"; |
| 1 | +import { onMount, createSignal, onCleanup } from "solid-js"; |
2 | 2 | import LegacyModal from "./legacy-modal/LegacyModal"; |
3 | 3 | import Croppie, { CropData } from "croppie"; |
4 | 4 | import "croppie/croppie.css"; |
5 | 5 | import Button from "./Button"; |
6 | 6 | import { t } from "@nerimity/i18lite"; |
| 7 | +import { debounce } from "@/common/debounce"; |
7 | 8 |
|
8 | 9 | export default function ImageCropModal(props: { |
9 | 10 | close(): void; |
10 | 11 | image: string; |
11 | 12 | onCropped(points: number[]): void; |
12 | 13 | type?: "avatar" | "banner"; |
13 | 14 | }) { |
14 | | - let imageEl: undefined | HTMLImageElement; |
| 15 | + let imageEl: HTMLDivElement | undefined; |
| 16 | + let containerRef: HTMLDivElement | undefined; |
15 | 17 | let croppie: Croppie | undefined; |
16 | 18 |
|
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) => { |
18 | 23 | if (!imageEl) return; |
19 | 24 |
|
| 25 | + // Cleanup old instance if it exists |
| 26 | + if (croppie) { |
| 27 | + croppie.destroy(); |
| 28 | + } |
| 29 | + |
20 | 30 | 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; |
21 | 41 |
|
22 | 42 | croppie = new Croppie(imageEl, { |
23 | 43 | viewport: { |
24 | 44 | type: isBanner ? "square" : "circle", |
25 | | - width: isBanner ? 400 : 300, |
26 | | - height: isBanner ? 150 : 300, |
| 45 | + width: vw, |
| 46 | + height: vh, |
27 | 47 | }, |
28 | 48 | boundary: { |
29 | 49 | width: "100%", |
30 | 50 | height: 400, |
31 | 51 | }, |
| 52 | + showZoomer: true, |
| 53 | + enableOrientation: true, |
| 54 | + enforceBoundary: true, // This prevents the "escaping into void" |
32 | 55 | }); |
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 | + } |
35 | 82 | }); |
36 | 83 |
|
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); |
44 | 86 |
|
45 | | - props.onCropped(pointsToInt); |
| 87 | + onCleanup(() => { |
| 88 | + ro.disconnect(); |
| 89 | + croppie?.destroy(); |
46 | 90 | }); |
47 | 91 | }); |
48 | 92 |
|
49 | 93 | const onClick = () => { |
50 | 94 | 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 | + } |
57 | 98 | props.close(); |
58 | 99 | }; |
59 | 100 |
|
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 | | - |
71 | 101 | return ( |
72 | 102 | <LegacyModal |
73 | 103 | title={t("imageCropModal.title")} |
74 | 104 | 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 | + } |
77 | 116 | > |
78 | 117 | <div |
| 118 | + ref={containerRef} |
79 | 119 | style={{ |
80 | 120 | "user-select": "none", |
81 | 121 | width: "100%", |
82 | | - height: "400px", |
83 | | - "margin-bottom": "50px", |
| 122 | + "min-height": "450px", |
| 123 | + "margin-bottom": "20px", |
84 | 124 | }} |
85 | 125 | > |
86 | 126 | <div ref={imageEl!} /> |
|
0 commit comments