refactor: keyed gallery items

This commit is contained in:
Mary
2024-05-08 08:07:40 +07:00
parent 1a8239a0c3
commit 70030b32d8
2 changed files with 13 additions and 5 deletions
+12 -4
View File
@@ -15,6 +15,7 @@ import {
manipulateAsync, manipulateAsync,
SaveFormat, SaveFormat,
} from 'expo-image-manipulator' } from 'expo-image-manipulator'
import {nanoid} from 'nanoid/non-secure'
import {POST_IMG_MAX} from '#/lib/constants' import {POST_IMG_MAX} from '#/lib/constants'
import {getImageDim} from '#/lib/media/manip' import {getImageDim} from '#/lib/media/manip'
@@ -26,13 +27,17 @@ export type ImageTransformation = Partial<
ActionCrop & ActionExtent & ActionFlip & ActionResize & ActionRotate ActionCrop & ActionExtent & ActionFlip & ActionResize & ActionRotate
> >
export type ImageSource = { export type ImageMeta = {
path: string path: string
width: number width: number
height: number height: number
mime: string mime: string
} }
export type ImageSource = ImageMeta & {
id: string
}
type ComposerImageBase = { type ComposerImageBase = {
alt: string alt: string
source: ImageSource source: ImageSource
@@ -41,7 +46,7 @@ type ComposerImageWithoutTransformation = ComposerImageBase & {
transformed?: undefined transformed?: undefined
} }
type ComposerImageWithTransformation = ComposerImageBase & { type ComposerImageWithTransformation = ComposerImageBase & {
transformed: ImageSource transformed: ImageMeta
transformations?: ImageTransformation transformations?: ImageTransformation
} }
@@ -54,11 +59,12 @@ const imageCacheDirectory = isNative
: null : null
export async function createComposerImage( export async function createComposerImage(
raw: ImageSource, raw: ImageMeta,
): Promise<ComposerImageWithoutTransformation> { ): Promise<ComposerImageWithoutTransformation> {
return { return {
alt: '', alt: '',
source: { source: {
id: nanoid(),
path: await moveIfNecessary(raw.path), path: await moveIfNecessary(raw.path),
width: raw.width, width: raw.width,
height: raw.height, height: raw.height,
@@ -78,6 +84,7 @@ export function createInitialImages(
return { return {
alt: '', alt: '',
source: { source: {
id: nanoid(),
path: uri, path: uri,
width: width, width: width,
height: height, height: height,
@@ -96,6 +103,7 @@ export async function pasteImage(
return { return {
alt: '', alt: '',
source: { source: {
id: nanoid(),
path: uri, path: uri,
width: width, width: width,
height: height, height: height,
@@ -199,7 +207,7 @@ export function resetImageManipulation(
return img return img
} }
export async function compressImage(img: ComposerImage): Promise<ImageSource> { export async function compressImage(img: ComposerImage): Promise<ImageMeta> {
const source = img.transformed || img.source const source = img.transformed || img.source
const [w, h] = containImageRes(source.width, source.height, POST_IMG_MAX) const [w, h] = containImageRes(source.width, source.height, POST_IMG_MAX)
+1 -1
View File
@@ -91,7 +91,7 @@ const GalleryInner = ({images, containerInfo, onChange}: GalleryInnerProps) => {
{images.map((image, index) => { {images.map((image, index) => {
return ( return (
<GalleryItem <GalleryItem
key={index} key={image.source.id}
image={image} image={image}
altTextControlStyle={altTextControlStyle} altTextControlStyle={altTextControlStyle}
imageControlsStyle={imageControlsStyle} imageControlsStyle={imageControlsStyle}