Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix bug causing editor to lose focus

dominik-zeglenc2a103ee740b
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx b/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx
index 9d6ba68fa6..f791be18c2 100644
--- a/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx
+++ b/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx
@@ -10,7 +10,6 @@ import {
withStyles,
WithStyles
} from "@material-ui/core/styles";
-import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import DeleteIcon from "@material-ui/icons/Delete";
import { ContentState } from "draft-js";
@@ -18,7 +17,6 @@ import * as React from "react";
import i18n from "../../i18n";
import Anchor from "../Anchor";
-import Form from "../Form";
import Link from "../Link";
import Toggle from "../Toggle";
@@ -93,72 +91,28 @@ const LinkEntity = withStyles(styles, {
onClickAway={disable}
mouseEvent="onClick"
>
- <Form
- initial={{
- value: contentState.getEntity(entityKey).getData()
- .href
- }}
- onSubmit={formData => onEdit(formData.value)}
- >
- {({ change, data }) => (
- <Toggle>
- {(editMode, { toggle: toggleEditMode }) => (
- <div className={classes.container}>
- {editMode ? (
- <TextField
- value={data.value}
- onChange={change}
- />
- ) : (
- <Typography
- className={classes.inline}
- variant="body1"
- >
- {
- contentState
- .getEntity(entityKey)
- .getData().href
- }
- </Typography>
- )}
- <span className={classes.separator} />
- {editMode ? (
- <>
- <Button
- onClick={toggleEditMode}
- variant="flat"
- >
- {i18n.t("Cancel")}
- </Button>
- <Button
- onClick={() => onEdit(entityKey)}
- color="secondary"
- variant="flat"
- >
- {i18n.t("Save")}
- </Button>
- </>
- ) : (
- <>
- <Button
- onClick={toggleEditMode}
- color="secondary"
- variant="flat"
- >
- {i18n.t("Edit")}
- </Button>
- <IconButton
- onClick={() => onRemove(entityKey)}
- >
- <DeleteIcon color="secondary" />
- </IconButton>
- </>
- )}
- </div>
- )}
- </Toggle>
- )}
- </Form>
+ <div className={classes.container}>
+ <Typography
+ className={classes.inline}
+ variant="body1"
+ >
+ {contentState.getEntity(entityKey).getData().href}
+ </Typography>
+ <span className={classes.separator} />
+ <Button
+ onClick={() => {
+ disable();
+ onEdit(entityKey);
+ }}
+ color="secondary"
+ variant="flat"
+ >
+ {i18n.t("Edit")}
+ </Button>
+ <IconButton onClick={() => onRemove(entityKey)}>
+ <DeleteIcon color="secondary" />
+ </IconButton>
+ </div>
</ClickAwayListener>
</Paper>
</Grow>
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx b/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
index abe853cbc3..f3339f75e2 100644
--- a/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
+++ b/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
@@ -1,8 +1,9 @@
-import { EditorState, RichUtils } from "draft-js";
+import { EditorState, EntityInstance, RichUtils } from "draft-js";
import * as React from "react";
interface LinkSourceProps {
editorState: EditorState;
+ entity?: EntityInstance;
entityType: {
type: string;
};
@@ -11,9 +12,9 @@ interface LinkSourceProps {
class LinkSource extends React.Component<LinkSourceProps> {
componentDidMount() {
- const { editorState, entityType, onComplete } = this.props;
+ const { editorState, entity, entityType, onComplete } = this.props;
- const href = window.prompt("Link URL");
+ const href = window.prompt("Link URL", entity ? entity.getData().href : "");
if (href) {
const content = editorState.getCurrentContent();
diff --git a/saleor/static/dashboard-next/pages/components/PageDetailsPage/PageDetailsPage.tsx b/saleor/static/dashboard-next/pages/components/PageDetailsPage/PageDetailsPage.tsx
index 4dcda7c66d..e1403c448b 100644
--- a/saleor/static/dashboard-next/pages/components/PageDetailsPage/PageDetailsPage.tsx
+++ b/saleor/static/dashboard-next/pages/components/PageDetailsPage/PageDetailsPage.tsx
@@ -62,6 +62,7 @@ const PageDetailsPage: React.StatelessComponent<PageDetailsPageProps> = ({
data={data}
disabled={disabled}
errors={formErrors}
+ page={page}
onChange={change}
/>
<CardSpacer />
diff --git a/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx b/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx
index 6f2c790254..e055479649 100644
--- a/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx
+++ b/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx
@@ -8,12 +8,16 @@ import CardTitle from "../../../components/CardTitle";
import FormSpacer from "../../../components/FormSpacer";
import RichTextEditor from "../../../components/RichTextEditor";
import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
+import { FormErrors } from "../../../types";
+import { PageDetails_page } from "../../types/PageDetails";
import { FormData } from "../PageDetailsPage";
export interface PageInfoProps {
data: FormData;
disabled: boolean;
- errors: Partial<Record<"title", string>>;
+ errors: FormErrors<"title">;
+ page: PageDetails_page;
onChange: (event: React.ChangeEvent<any>) => void;
}
@@ -31,6 +35,7 @@ const PageInfo = withStyles(styles, {
data,
disabled,
errors,
+ page,
onChange
}: PageInfoProps & WithStyles<typeof styles>) => (
<Card className={classes.root}>
@@ -49,7 +54,7 @@ const PageInfo = withStyles(styles, {
<FormSpacer />
<RichTextEditor
disabled={disabled}
- initial={data.content}
+ initial={maybe(() => JSON.parse(page.content))}
label={i18n.t("Content")}
name={"content" as keyof FormData}
onChange={onChange}