import React, { useEffect, useState } from "react"; import copyToClipboard from "../../lib/copyToClipboard"; import EditableImage from "../EditableImage"; import useToastStore from "../../stores/useToastStore"; import { ImageResource, Locale } from "../../types"; import Placeholder from "./Placeholder"; import { useDraggable } from "../drag"; interface Record { id: number | null; image: ImageResource; src: string | null; file: File | null; } interface GridImageProps { draggable: { handle: string; record: Record }; attributeName: string; locale: string; locales: { [index: string]: Locale }; placeholder: boolean; enablePrimary: boolean; showEmbed: boolean; primary: boolean; position: number; deleteImage: () => void; startDrag: (evt: Event, draggable: Draggable) => void; onUpdate: (newImage: ImageResource, src: string) => void; } export default function GridImage(props: GridImageProps) { const { attributeName, draggable } = props; const record = draggable.record; const image = record.image; const notice = useToastStore((state) => state.notice); const [src, setSrc] = useState(record.src || null); const dragAttrs = useDraggable(draggable, props.startDrag); useEffect(() => { if (record.file) { const reader = new FileReader(); reader.onload = () => setSrc(reader.result); reader.readAsDataURL(record.file); } }, []); const copyEmbed = (evt: Event) => { evt.preventDefault(); copyToClipboard(`[image:${image.id}]`); notice("Embed code copied to clipboard"); }; const deleteImage = (evt: Event) => { evt.preventDefault(); if (props.deleteImage) { props.deleteImage(); } }; const classes = ["grid-image"]; if (props.placeholder) { classes.push("placeholder"); } if (record.file) { classes.push("uploading"); } return (
{props.enablePrimary && ( )} {!image && } {image && ( <>
{props.showEmbed && } {props.deleteImage && }
)}
); }