Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add image entity
dominik-zeglend9c07ffaa8c9
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/ImageEntity.tsx b/saleor/static/dashboard-next/components/RichTextEditor/ImageEntity.tsxnew file mode 100644index 0000000000..f6268add4b--- /dev/null+++ b/saleor/static/dashboard-next/components/RichTextEditor/ImageEntity.tsx@@ -0,0 +1,119 @@+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";+import {+ createStyles,+ Theme,+ withStyles,+ WithStyles+} from "@material-ui/core/styles";+import Typography from "@material-ui/core/Typography";+import DeleteIcon from "@material-ui/icons/Delete";+import { ContentState } from "draft-js";+import * as React from "react";++import i18n from "../../i18n";+import Anchor from "../Anchor";+import Toggle from "../Toggle";++interface ImageEntityProps {+ children: React.ReactNode;+ contentState: ContentState;+ entityKey: string;+ onEdit: (entityKey: string) => void;+ onRemove: (entityKey: string) => void;+}++const styles = (theme: Theme) =>+ createStyles({+ 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.unit * 1.5+ }+ });++const ImageEntity = withStyles(styles, {+ name: "ImageEntity"+})(+ ({+ classes,+ contentState,+ entityKey,+ onEdit,+ onRemove+ }: ImageEntityProps & WithStyles<typeof styles>) => (+ <Toggle>+ {(isOpened, { disable, toggle }) => (+ <>+ <Anchor>+ {anchor => (+ <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="secondary"+ variant="flat"+ >+ {i18n.t("Replace")}+ </Button>+ <IconButton onClick={() => onRemove(entityKey)}>+ <DeleteIcon color="secondary" />+ </IconButton>+ </div>+ </ClickAwayListener>+ </Paper>+ </Grow>+ )}+ </Popper>+ </div>+ )}+ </Anchor>+ <img+ className={classes.image}+ src={contentState.getEntity(entityKey).getData().href}+ onClick={toggle}+ />+ </>+ )}+ </Toggle>+ )+);+export default ImageEntity;diff --git a/saleor/static/dashboard-next/components/RichTextEditor/ImageSource.tsx b/saleor/static/dashboard-next/components/RichTextEditor/ImageSource.tsxnew file mode 100644index 0000000000..4bfbd68172--- /dev/null+++ b/saleor/static/dashboard-next/components/RichTextEditor/ImageSource.tsx@@ -0,0 +1,101 @@+import Button from "@material-ui/core/Button";+import Dialog from "@material-ui/core/Dialog";+import DialogActions from "@material-ui/core/DialogActions";+import DialogContent from "@material-ui/core/DialogContent";+import DialogTitle from "@material-ui/core/DialogTitle";+import TextField from "@material-ui/core/TextField";+import { AtomicBlockUtils, EditorState, EntityInstance } from "draft-js";+import * as React from "react";++import i18n from "../../i18n";+import Form from "../Form";++interface ImageSourceProps {+ editorState: EditorState;+ entity?: EntityInstance;+ entityKey?: string;+ entityType: {+ type: string;+ };+ onComplete: (updateState: EditorState) => void;+ onClose: () => void;+}++class ImageSource extends React.Component<ImageSourceProps> {+ submit = (href: string) => {+ const {+ editorState,+ entity,+ entityKey,+ entityType,+ onComplete+ } = this.props;++ if (href) {+ const content = editorState.getCurrentContent();+ if (entity) {+ const nextContent = content.mergeEntityData(entityKey, { href });+ const nextState = EditorState.push(+ editorState,+ nextContent,+ "apply-entity"+ );+ EditorState.forceSelection(editorState, editorState.getSelection());+ onComplete(nextState);+ } else {+ const contentWithEntity = content.createEntity(+ entityType.type,+ "IMMUTABLE",+ { href }+ );+ const nextState = AtomicBlockUtils.insertAtomicBlock(+ editorState,+ contentWithEntity.getLastCreatedEntityKey(),+ ""+ );++ onComplete(nextState);+ }+ } else {+ onComplete(editorState);+ }+ };++ render() {+ const { entity, onClose } = this.props;+ const initial = entity ? entity.getData().href : "";++ return (+ <Dialog open={true} fullWidth maxWidth="sm">+ <Form+ initial={{ href: initial }}+ onSubmit={({ href }) => this.submit(href)}+ >+ {({ data, change, submit }) => (+ <>+ <DialogTitle>{i18n.t("Add Image Link")}</DialogTitle>+ <DialogContent>+ <TextField+ name="href"+ fullWidth+ label={i18n.t("Image URL")}+ value={data.href}+ onChange={change}+ />+ </DialogContent>+ <DialogActions>+ <Button onClick={onClose}>+ {i18n.t("Cancel", { context: "button" })}+ </Button>+ <Button onClick={submit} color="secondary" variant="contained">+ {i18n.t("Save", { context: "button" })}+ </Button>+ </DialogActions>+ </>+ )}+ </Form>+ </Dialog>+ );+ }+}+export default ImageSource;diff --git a/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx b/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsxindex 13ab72e5b6..331c155958 100644--- a/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx+++ b/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx@@ -78,6 +78,7 @@ const LinkEntity = withStyles(styles, {open={isOpened}anchorEl={anchor.current}transition+ disablePortalplacement="bottom">{({ TransitionProps, placement }) => (diff --git a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsxindex 1661df4276..8d46fc13ef 100644--- a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx+++ b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx@@ -11,6 +11,7 @@ import ItalicIcon from "@material-ui/icons/FormatItalic";import UnorderedListIcon from "@material-ui/icons/FormatListBulleted";import OrderedListIcon from "@material-ui/icons/FormatListNumbered";import QuotationIcon from "@material-ui/icons/FormatQuote";+import ImageIcon from "@material-ui/icons/Image";import LinkIcon from "@material-ui/icons/Link";import * as classNames from "classnames";import { RawDraftContentState } from "draft-js";@@ -24,6 +25,8 @@ import * as React from "react";import HeaderThree from "../../icons/HeaderThree";import HeaderTwo from "../../icons/HeaderTwo";+import ImageEntity from "./ImageEntity";+import ImageSource from "./ImageSource";import LinkEntity from "./LinkEntity";import LinkSource from "./LinkSource";@@ -67,6 +70,12 @@ const styles = (theme: Theme) =>root: {"& .DraftEditor": {"&-editorContainer": {+ "& .public-DraftEditor-content": {+ lineHeight: 1.62,+ maxHeight: 300,+ minHeight: 100,+ overflowY: "scroll"+ },"& a": {color: theme.palette.secondary.light},@@ -247,6 +256,13 @@ const RichTextEditor = withStyles(styles, { name: "RichTextEditor" })(icon: <LinkIcon />,source: LinkSource,type: ENTITY_TYPE.LINK+ },+ {+ attributes: ["href"],+ decorator: ImageEntity,+ icon: <ImageIcon />,+ source: ImageSource,+ type: ENTITY_TYPE.IMAGE}]}/>