allow for custom cropper aspect ration based on image
parent
ebd333b331
commit
bc956803b8
|
@ -17,6 +17,10 @@ export async function openCropper(opts: CropperOptions): Promise<RNImage> {
|
||||||
unstable__openModal({
|
unstable__openModal({
|
||||||
name: 'crop-image',
|
name: 'crop-image',
|
||||||
uri: opts.path,
|
uri: opts.path,
|
||||||
|
dimensions:
|
||||||
|
opts.height && opts.width
|
||||||
|
? {width: opts.width, height: opts.height}
|
||||||
|
: undefined,
|
||||||
onSelect: (img?: RNImage) => {
|
onSelect: (img?: RNImage) => {
|
||||||
if (img) {
|
if (img) {
|
||||||
resolve(img)
|
resolve(img)
|
||||||
|
|
|
@ -47,6 +47,7 @@ export interface EditImageModal {
|
||||||
export interface CropImageModal {
|
export interface CropImageModal {
|
||||||
name: 'crop-image'
|
name: 'crop-image'
|
||||||
uri: string
|
uri: string
|
||||||
|
dimensions?: {width: number; height: number}
|
||||||
onSelect: (img?: RNImage) => void
|
onSelect: (img?: RNImage) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,13 @@ import {Dimensions} from 'lib/media/types'
|
||||||
import {getDataUriSize} from 'lib/media/util'
|
import {getDataUriSize} from 'lib/media/util'
|
||||||
import {gradients, s} from 'lib/styles'
|
import {gradients, s} from 'lib/styles'
|
||||||
import {Text} from 'view/com/util/text/Text'
|
import {Text} from 'view/com/util/text/Text'
|
||||||
|
import {calculateDimensions} from './cropImageUtil'
|
||||||
|
|
||||||
enum AspectRatio {
|
enum AspectRatio {
|
||||||
Square = 'square',
|
Square = 'square',
|
||||||
Wide = 'wide',
|
Wide = 'wide',
|
||||||
Tall = 'tall',
|
Tall = 'tall',
|
||||||
|
Custom = 'custom',
|
||||||
}
|
}
|
||||||
|
|
||||||
const DIMS: Record<string, Dimensions> = {
|
const DIMS: Record<string, Dimensions> = {
|
||||||
|
@ -31,17 +33,24 @@ export const snapPoints = ['0%']
|
||||||
|
|
||||||
export function Component({
|
export function Component({
|
||||||
uri,
|
uri,
|
||||||
|
dimensions,
|
||||||
onSelect,
|
onSelect,
|
||||||
}: {
|
}: {
|
||||||
uri: string
|
uri: string
|
||||||
|
dimensions?: Dimensions
|
||||||
onSelect: (img?: RNImage) => void
|
onSelect: (img?: RNImage) => void
|
||||||
}) {
|
}) {
|
||||||
const {closeModal} = useModalControls()
|
const {closeModal} = useModalControls()
|
||||||
const pal = usePalette('default')
|
const pal = usePalette('default')
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const [as, setAs] = React.useState<AspectRatio>(AspectRatio.Square)
|
const defaultAspectStyle = dimensions
|
||||||
|
? AspectRatio.Custom
|
||||||
|
: AspectRatio.Square
|
||||||
|
const [as, setAs] = React.useState<AspectRatio>(defaultAspectStyle)
|
||||||
const [scale, setScale] = React.useState<number>(1)
|
const [scale, setScale] = React.useState<number>(1)
|
||||||
const editorRef = React.useRef<ImageEditor>(null)
|
const editorRef = React.useRef<ImageEditor>(null)
|
||||||
|
const imageEditorWidth = dimensions ? dimensions.width : DIMS[as].width
|
||||||
|
const imageEditorHeight = dimensions ? dimensions.height : DIMS[as].height
|
||||||
|
|
||||||
const doSetAs = (v: AspectRatio) => () => setAs(v)
|
const doSetAs = (v: AspectRatio) => () => setAs(v)
|
||||||
|
|
||||||
|
@ -57,8 +66,8 @@ export function Component({
|
||||||
path: dataUri,
|
path: dataUri,
|
||||||
mime: 'image/jpeg',
|
mime: 'image/jpeg',
|
||||||
size: getDataUriSize(dataUri),
|
size: getDataUriSize(dataUri),
|
||||||
width: DIMS[as].width,
|
width: imageEditorWidth,
|
||||||
height: DIMS[as].height,
|
height: imageEditorHeight,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
onSelect(undefined)
|
onSelect(undefined)
|
||||||
|
@ -73,7 +82,18 @@ export function Component({
|
||||||
cropperStyle = styles.cropperWide
|
cropperStyle = styles.cropperWide
|
||||||
} else if (as === AspectRatio.Tall) {
|
} else if (as === AspectRatio.Tall) {
|
||||||
cropperStyle = styles.cropperTall
|
cropperStyle = styles.cropperTall
|
||||||
|
} else if (as === AspectRatio.Custom) {
|
||||||
|
const cropperDimensions = calculateDimensions(
|
||||||
|
550,
|
||||||
|
imageEditorHeight,
|
||||||
|
imageEditorWidth,
|
||||||
|
)
|
||||||
|
cropperStyle = {
|
||||||
|
width: cropperDimensions.width,
|
||||||
|
height: cropperDimensions.height,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View>
|
<View>
|
||||||
<View style={[styles.cropper, pal.borderDark, cropperStyle]}>
|
<View style={[styles.cropper, pal.borderDark, cropperStyle]}>
|
||||||
|
@ -81,8 +101,8 @@ export function Component({
|
||||||
ref={editorRef}
|
ref={editorRef}
|
||||||
style={styles.imageEditor}
|
style={styles.imageEditor}
|
||||||
image={uri}
|
image={uri}
|
||||||
width={DIMS[as].width}
|
width={imageEditorWidth}
|
||||||
height={DIMS[as].height}
|
height={imageEditorHeight}
|
||||||
scale={scale}
|
scale={scale}
|
||||||
border={0}
|
border={0}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in New Issue