2019-06-19 14:40:52 +00:00
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import ClickAwayListener from "@material-ui/core/ClickAwayListener";
|
|
|
|
import Grow from "@material-ui/core/Grow";
|
|
|
|
import IconButton from "@material-ui/core/IconButton";
|
|
|
|
import Paper from "@material-ui/core/Paper";
|
|
|
|
import Popper from "@material-ui/core/Popper";
|
2019-10-30 14:34:24 +00:00
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2019-06-19 14:40:52 +00:00
|
|
|
import DeleteIcon from "@material-ui/icons/Delete";
|
|
|
|
import { ContentState } from "draft-js";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:54:03 +00:00
|
|
|
import { FormattedMessage } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
interface ImageEntityProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
contentState: ContentState;
|
|
|
|
entityKey: string;
|
|
|
|
onEdit: (entityKey: string) => void;
|
|
|
|
onRemove: (entityKey: string) => void;
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const useStyles = makeStyles(theme => ({
|
|
|
|
anchor: {
|
|
|
|
display: "inline-block"
|
|
|
|
},
|
|
|
|
container: {
|
|
|
|
alignItems: "center",
|
|
|
|
display: "flex"
|
|
|
|
},
|
|
|
|
image: { maxWidth: "100%" },
|
|
|
|
inline: {
|
|
|
|
display: "inline-block"
|
|
|
|
},
|
|
|
|
root: {
|
|
|
|
alignItems: "center",
|
|
|
|
display: "flex",
|
|
|
|
minHeight: 72,
|
|
|
|
padding: theme.spacing(1.5)
|
|
|
|
}
|
|
|
|
}));
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const ImageEntity: React.FC<ImageEntityProps> = props => {
|
|
|
|
const { contentState, entityKey, onEdit, onRemove } = props;
|
|
|
|
const classes = useStyles(props);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const [isOpened, setOpenStatus] = React.useState(false);
|
|
|
|
const anchor = React.useRef<HTMLDivElement>();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const disable = () => setOpenStatus(false);
|
|
|
|
const toggle = () => setOpenStatus(!isOpened);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className={classes.anchor} ref={anchor}>
|
|
|
|
<Popper
|
|
|
|
open={isOpened}
|
|
|
|
anchorEl={anchor.current}
|
|
|
|
transition
|
|
|
|
disablePortal
|
|
|
|
placement="bottom"
|
|
|
|
>
|
|
|
|
{({ TransitionProps, placement }) => (
|
|
|
|
<Grow
|
|
|
|
{...TransitionProps}
|
|
|
|
style={{
|
|
|
|
transformOrigin: placement
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Paper className={classes.root}>
|
|
|
|
<ClickAwayListener onClickAway={disable} mouseEvent="onClick">
|
|
|
|
<div className={classes.container}>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
disable();
|
|
|
|
onEdit(entityKey);
|
|
|
|
}}
|
|
|
|
color="primary"
|
|
|
|
>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Replace"
|
|
|
|
description="replace image, button"
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
<IconButton onClick={() => onRemove(entityKey)}>
|
|
|
|
<DeleteIcon color="primary" />
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
</ClickAwayListener>
|
|
|
|
</Paper>
|
|
|
|
</Grow>
|
|
|
|
)}
|
|
|
|
</Popper>
|
|
|
|
</div>
|
|
|
|
<img
|
|
|
|
className={classes.image}
|
|
|
|
src={contentState.getEntity(entityKey).getData().href}
|
|
|
|
onClick={toggle}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
export default ImageEntity;
|