All files / packages/components/src image-cropper.web.tsx

93.02% Statements 40/43
50% Branches 2/4
90% Functions 9/10
92.85% Lines 39/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                23x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x       23x 1x 1x 1x 1x       1x 1x   1x   1x 1x 1x       1x 1x 1x 1x   1x                     1x     23x 1x 1x 1x 1x 1x     1x       1x 1x     745x                 739x                              
import type { Area, Point } from "react-easy-crop";
import Cropper from "react-easy-crop";
 
import { MAX_AVATAR_SIDE_SIZE } from "~utils/images";
import { promisifyEvent } from "~utils/promise";
 
export type Props = {
	image: string;
	crop: Point;
	zoom: number;
	maxZoom: number;
	onCropChange: (location: Point) => void;
	onCropComplete: (area: Area, areaPixels: Area) => void;
	onZoomChange: (zoom: number) => void;
};
 
const convertDataUrlToImageElement = async (url: string) =>
	promisifyEvent<HTMLImageElement>((listener, errorListener) => {
		const image = new Image();
		const localListener = () => listener(image);
		const localErrorListener = (error: ErrorEvent) =>
			errorListener(new Error(error.message));
		image.addEventListener("load", localListener);
		image.addEventListener("error", localErrorListener);
		image.src = url;
		return () => {
			image.removeEventListener("load", localListener);
			image.removeEventListener("error", localErrorListener);
		};
	});
 
const getCroppedCanvas = async (imageSrc: string, pixelCrop: Area) => {
	const image = await convertDataUrlToImageElement(imageSrc);
	const canvas = document.createElement("canvas");
	const ctx = canvas.getContext("2d");
	Iif (!ctx) {
		throw new Error("Expected to get 2d context for canvas");
	}
 
	canvas.width = image.width;
	canvas.height = image.height;
 
	ctx.drawImage(image, 0, 0);
 
	const croppedCanvas = document.createElement("canvas");
	const croppedCtx = croppedCanvas.getContext("2d");
	Iif (!croppedCtx) {
		throw new Error("Expected to get 2d context for cropped canvas");
	}
 
	const constrainedWidth = Math.min(pixelCrop.width, MAX_AVATAR_SIDE_SIZE);
	const constrainedHeight = Math.min(pixelCrop.height, MAX_AVATAR_SIDE_SIZE);
	croppedCanvas.width = constrainedWidth;
	croppedCanvas.height = constrainedHeight;
 
	croppedCtx.drawImage(
		canvas,
		pixelCrop.x,
		pixelCrop.y,
		pixelCrop.width,
		pixelCrop.height,
		0,
		0,
		constrainedWidth,
		constrainedHeight,
	);
	return croppedCanvas;
};
 
export const getFormData = async (imageSrc: string, pixelCrop: Area) => {
	const formData = new FormData();
	const croppedCanvas = await getCroppedCanvas(imageSrc, pixelCrop);
	const type = "image/png";
	const croppedBlob = await promisifyEvent<File>((listener) => {
		croppedCanvas.toBlob(
			// It is not clear in which case `file` in `null`
			// oxlint-disable-next-line typescript/no-non-null-assertion
			(file) => listener(new File([file!], "avatar.png", { type })),
			type,
		);
	});
	formData.append("avatar", croppedBlob);
	return formData;
};
 
export const ImageCropper = ({
	image,
	crop,
	zoom,
	maxZoom,
	onCropChange,
	onCropComplete,
	onZoomChange,
}: Props) => (
	<Cropper
		image={image}
		crop={crop}
		zoom={zoom}
		maxZoom={maxZoom}
		aspect={1}
		onCropChange={onCropChange}
		onCropComplete={onCropComplete}
		onZoomChange={onZoomChange}
		classes={{
			cropAreaClassName: "rounded-full !text-background !text-opacity-50",
			mediaClassName: "rounded-lg",
		}}
	/>
);