about summary refs log tree commit diff
path: root/src/view/com/composer/PhotoCarouselPicker.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/view/com/composer/PhotoCarouselPicker.tsx')
-rw-r--r--src/view/com/composer/PhotoCarouselPicker.tsx41
1 files changed, 27 insertions, 14 deletions
diff --git a/src/view/com/composer/PhotoCarouselPicker.tsx b/src/view/com/composer/PhotoCarouselPicker.tsx
index 440e7d38f..6b537e9c8 100644
--- a/src/view/com/composer/PhotoCarouselPicker.tsx
+++ b/src/view/com/composer/PhotoCarouselPicker.tsx
@@ -26,6 +26,28 @@ const IMAGE_PARAMS = {
   compressImageQuality: 1.0,
 }
 
+export async function cropPhoto(
+  path: string,
+  imgWidth = MAX_WIDTH,
+  imgHeight = MAX_HEIGHT,
+) {
+  // choose target dimensions based on the original
+  // this causes the photo cropper to start with the full image "selected"
+  const {width, height} = scaleDownDimensions(
+    {width: imgWidth, height: imgHeight},
+    {width: MAX_WIDTH, height: MAX_HEIGHT},
+  )
+  const cropperRes = await openCropper({
+    mediaType: 'photo',
+    path,
+    ...IMAGE_PARAMS,
+    width,
+    height,
+  })
+  const img = await compressIfNeeded(cropperRes, MAX_SIZE)
+  return img.path
+}
+
 export const PhotoCarouselPicker = ({
   selectedPhotos,
   onSelectPhotos,
@@ -55,21 +77,12 @@ export const PhotoCarouselPicker = ({
   const handleSelectPhoto = useCallback(
     async (item: PhotoIdentifier) => {
       try {
-        // choose target dimensions based on the original
-        // this causes the photo cropper to start with the full image "selected"
-        const {width, height} = scaleDownDimensions(
-          {width: item.node.image.width, height: item.node.image.height},
-          {width: MAX_WIDTH, height: MAX_HEIGHT},
+        const imgPath = await cropPhoto(
+          item.node.image.uri,
+          item.node.image.width,
+          item.node.image.height,
         )
-        const cropperRes = await openCropper({
-          mediaType: 'photo',
-          path: item.node.image.uri,
-          ...IMAGE_PARAMS,
-          width,
-          height,
-        })
-        const img = await compressIfNeeded(cropperRes, MAX_SIZE)
-        onSelectPhotos([...selectedPhotos, img.path])
+        onSelectPhotos([...selectedPhotos, imgPath])
       } catch (err: any) {
         // ignore
         store.log.warn('Error selecting photo', err)