import React, { useEffect, useState } from "react"; import PropTypes from "prop-types"; import copyToClipboard from "../../lib/copyToClipboard"; import EditableImage from "../EditableImage"; import ToastStore from "../ToastStore"; import Placeholder from "./Placeholder"; import { useDraggable } from "../drag"; export default function GridImage(props) { const { attributeName, draggable } = props; const record = draggable.record; const image = record.image; 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) => { evt.preventDefault(); copyToClipboard(`[image:${image.id}]`); ToastStore.dispatch({ type: "NOTICE", message: "Embed code copied to clipboard" }); }; const deleteImage = (evt) => { evt.preventDefault(); if (props.deleteImage) { props.deleteImage(); } }; let classes = ["grid-image"]; if (props.placeholder) { classes.push("placeholder"); } if (record.file) { classes.push("uploading"); } return (
{props.enablePrimary && ( )} {!image && } {image && <>
{props.showEmbed && ( )} {props.deleteImage && ( )}
}
); } GridImage.propTypes = { draggable: PropTypes.object, deleteImage: PropTypes.func, startDrag: PropTypes.func, locale: PropTypes.string, locales: PropTypes.object, onUpdate: PropTypes.func, attributeName: PropTypes.string, placeholder: PropTypes.bool, enablePrimary: PropTypes.bool, showEmbed: PropTypes.bool, primary: PropTypes.bool, position: PropTypes.number, };