import _ from "lodash" import React from "react" import RotationAwareImageComponent, { Image } from "../RotationAwareImageComponent" import ImagePopupComponent from "../ImagePopupComponent" import { LocalizeString } from "ez-localize" import { ImageManager, ImageAcquirer } from "../formContext" export interface ImagesAnswerComponentProps { /** array of { id: someid, caption: caption, cover: true/false } */ imagelist?: Image[] | null /** Called when image list changed */ onImagelistChange?: (imagelist: Image[]) => void consentPrompt?: string /** Maximum dimension (width or height) for the images in pixels */ maxImageDimension?: number | null imageManager: ImageManager imageAcquirer?: ImageAcquirer T: LocalizeString } interface ImagesAnswerComponentState { modalImageId: string | null } // Edit an image export default class ImagesAnswerComponent extends React.Component< ImagesAnswerComponentProps, ImagesAnswerComponentState > { constructor(props: ImagesAnswerComponentProps) { super(props) this.state = { modalImageId: null } // Image id of modal. null if not open } focus() { // Nothing to focus return null } handleAdd = () => { // Check consent if (this.props.consentPrompt && !confirm(this.props.consentPrompt)) { return } // Call imageAcquirer this.props.imageAcquirer?.acquireMultiple( { maxImageDimension: this.props.maxImageDimension }, (images: Image[]) => { // Add to image list const imagelist = [...(this.props.imagelist || []), ...images.map((image, index) => ({ ...image, cover: index === 0 && this.props.imagelist?.length === 0 }))] this.props.onImagelistChange?.(imagelist) }, (err: any) => { console.log("Error acquiring image", err.message || err.code || err) alert(`${this.props.T("Error getting image")}: ${err.message || err.code || err}`) } ) } handleClickImage = (id: string) => { this.setState({ modalImageId: id }) } renderModal() { const { modalImageId } = this.state if (!modalImageId) { return null } const { onImagelistChange, imagelist, imageManager, T } = this.props const onRemove = onImagelistChange ? () => { this.setState({ modalImageId: null }) const updatedImagelist = imagelist?.filter(image => image.id !== modalImageId) || [] onImagelistChange(updatedImagelist) } : undefined const onSetCover = onImagelistChange ? () => { this.setState({ modalImageId: null }) const updatedImagelist = imagelist?.map(image => ({ ...image, cover: image.id === modalImageId })) || [] onImagelistChange(updatedImagelist) } : undefined const onRotate = onImagelistChange ? () => { const updatedImagelist = imagelist?.map(image => image.id === modalImageId ? { ...image, rotation: ((image.rotation || 0) + 90) % 360 } : image ) || [] onImagelistChange(updatedImagelist) } : undefined return ( image.id === modalImageId)!} T={T} onRemove={onRemove} onSetCover={onSetCover} onRotate={onRotate} onClose={() => this.setState({ modalImageId: null })} /> ) } render() { const { imagelist, imageManager, onImagelistChange, imageAcquirer } = this.props return (
{this.renderModal()} {imagelist?.map((image) => ( this.handleClickImage(image.id)} /> ))} {onImagelistChange && imageAcquirer && ( ( ) )}
) } }