diff --git a/src/lib/media/picker.web.tsx b/src/lib/media/picker.web.tsx index 95c79cc879..a53ffc9614 100644 --- a/src/lib/media/picker.web.tsx +++ b/src/lib/media/picker.web.tsx @@ -21,6 +21,8 @@ export async function openCropper(opts: CropperOptions): Promise { opts.width && opts.height ? {width: opts.width, height: opts.height} : undefined, + aspect: opts.webAspectRatio, + circular: opts.webCircularCrop, onSelect: (img?: RNImage) => { if (img) { resolve(img) diff --git a/src/lib/media/types.ts b/src/lib/media/types.ts index e6f442759f..ec94256ea1 100644 --- a/src/lib/media/types.ts +++ b/src/lib/media/types.ts @@ -18,4 +18,7 @@ export interface CameraOpts { cropperCircleOverlay?: boolean } -export type CropperOptions = Parameters[0] +export type CropperOptions = Parameters[0] & { + webAspectRatio?: number + webCircularCrop?: boolean +} diff --git a/src/state/modals/index.tsx b/src/state/modals/index.tsx index 86eaac3478..91ab7c0a3b 100644 --- a/src/state/modals/index.tsx +++ b/src/state/modals/index.tsx @@ -40,6 +40,8 @@ export interface CropImageModal { name: 'crop-image' uri: string dimensions?: {width: number; height: number} + aspect?: number + circular?: boolean onSelect: (img?: RNImage) => void } diff --git a/src/view/com/composer/photos/EditImageDialog.web.tsx b/src/view/com/composer/photos/EditImageDialog.web.tsx index 719cbce0b7..8492f58ab8 100644 --- a/src/view/com/composer/photos/EditImageDialog.web.tsx +++ b/src/view/com/composer/photos/EditImageDialog.web.tsx @@ -29,7 +29,6 @@ const EditImageInner = ({control, image, onChange}: EditImageDialogProps) => { const {initialCrop} = React.useMemo(() => { const initialArea = manips?.crop - let crop: PercentCrop | undefined if (initialArea) { diff --git a/src/view/com/modals/CropImage.web.tsx b/src/view/com/modals/CropImage.web.tsx index 3d8f094b17..c7dd98e2cb 100644 --- a/src/view/com/modals/CropImage.web.tsx +++ b/src/view/com/modals/CropImage.web.tsx @@ -7,6 +7,7 @@ import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import ReactCrop, {PercentCrop} from 'react-image-crop' +import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries' import {useModalControls} from '#/state/modals' import {usePalette} from 'lib/hooks/usePalette' import {getDataUriSize} from 'lib/media/util' @@ -17,60 +18,60 @@ export const snapPoints = ['0%'] export function Component({ uri, - dimensions, + aspect, + circular, onSelect, }: { uri: string - dimensions?: {width: number; height: number} + aspect?: number + circular?: boolean onSelect: (img?: RNImage) => void }) { const pal = usePalette('default') const {_} = useLingui() const {closeModal} = useModalControls() + const {isMobile} = useWebMediaQueries() const imageRef = React.useRef(null) const [crop, setCrop] = React.useState() const isEmpty = !crop || (crop.width || crop.height) === 0 - const aspect = dimensions ? dimensions.width / dimensions.height : undefined const onPressCancel = () => { onSelect(undefined) closeModal() } const onPressDone = async () => { - if (!isEmpty) { - const img = imageRef.current! + const img = imageRef.current! - const result = await manipulateAsync( - uri, - [ - { - crop: { - originX: (crop.x * img.width) / 100, - originY: (crop.y * img.height) / 100, - width: (crop.width * img.width) / 100, - height: (crop.height * img.height) / 100, + const result = await manipulateAsync( + uri, + isEmpty + ? [] + : [ + { + crop: { + originX: (crop.x * img.naturalWidth) / 100, + originY: (crop.y * img.naturalHeight) / 100, + width: (crop.width * img.naturalWidth) / 100, + height: (crop.height * img.naturalHeight) / 100, + }, }, - }, - ], - { - base64: true, - format: SaveFormat.JPEG, - }, - ) + ], + { + base64: true, + format: SaveFormat.JPEG, + }, + ) - onSelect({ - path: result.uri, - mime: 'image/jpeg', - size: result.base64 !== undefined ? getDataUriSize(result.base64) : 0, - width: result.width, - height: result.height, - }) - } else { - onSelect(undefined) - } + onSelect({ + path: result.uri, + mime: 'image/jpeg', + size: result.base64 !== undefined ? getDataUriSize(result.base64) : 0, + width: result.width, + height: result.height, + }) closeModal() } @@ -81,11 +82,12 @@ export function Component({ setCrop(next)}> + onChange={(_, next) => setCrop(next)} + circularCrop={circular}> - +