Skip to content

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

Use link component in link entity

dominik-zeglen582c97bdcfc4
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/Link.tsx b/saleor/static/dashboard-next/components/RichTextEditor/Link.tsx
deleted file mode 100644
index e5ae59ea3d..0000000000
--- a/saleor/static/dashboard-next/components/RichTextEditor/Link.tsx
+++ /dev/null
@@ -1,15 +0,0 @@
-import { ContentState } from "draft-js";
-import * as React from "react";
-
-interface LinkProps {
- children: React.ReactNode;
- contentState: ContentState;
- entityKey: string;
-}
-
-const Link: React.StatelessComponent<LinkProps> = ({
- children,
- contentState,
- entityKey
-}) => <a href={contentState.getEntity(entityKey).getData().href}>{children}</a>;
-export default Link;
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx b/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx
new file mode 100644
index 0000000000..47e2aef8a7
--- /dev/null
+++ b/saleor/static/dashboard-next/components/RichTextEditor/LinkEntity.tsx
@@ -0,0 +1,20 @@
+import { ContentState } from "draft-js";
+import * as React from "react";
+import Link from "../Link";
+
+interface LinkEntityProps {
+ children: React.ReactNode;
+ contentState: ContentState;
+ entityKey: string;
+}
+
+const LinkEntity: React.StatelessComponent<LinkEntityProps> = ({
+ children,
+ contentState,
+ entityKey
+}) => (
+ <Link href={contentState.getEntity(entityKey).getData().href}>
+ {children}
+ </Link>
+);
+export default LinkEntity;
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx b/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
index 70b0a9f3cc..abe853cbc3 100644
--- a/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
+++ b/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
@@ -31,11 +31,6 @@ class LinkSource extends React.Component<LinkSourceProps> {
newEditorState.getSelection(),
entityKey
);
- // const nextState = AtomicBlockUtils.insertAtomicBlock(
- // editorState,
- // entityKey,
- // " "
- // );
onComplete(nextState);
} else {
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
index 66530f7b28..4b2b397fce 100644
--- a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
+++ b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
@@ -6,6 +6,7 @@ import {
} from "@material-ui/core/styles";
import { fade } from "@material-ui/core/styles/colorManipulator";
import Typography from "@material-ui/core/Typography";
+import { RawDraftContentState } from "draft-js";
import {
BLOCK_TYPE,
DraftailEditor,
@@ -14,14 +15,14 @@ import {
} from "draftail";
import * as React from "react";
-import Link from "./Link";
+import LinkEntity from "./LinkEntity";
import LinkSource from "./LinkSource";
export interface RichTextEditorProps {
disabled: boolean;
label: string;
name: string;
- initial: string;
+ initial: RawDraftContentState;
onChange: (event: React.ChangeEvent<any>) => void;
}
@@ -33,6 +34,9 @@ const styles = (theme: Theme) =>
root: {
"& .DraftEditor": {
"&-editorContainer": {
+ "& a": {
+ color: theme.palette.secondary.light
+ },
"&:after": {
background: theme.palette.primary.main,
bottom: -1,
@@ -142,7 +146,7 @@ const RichTextEditor = withStyles(styles, { name: "RichTextEditor" })(
entityTypes={[
{
attributes: ["href"],
- decorator: Link,
+ decorator: LinkEntity,
source: LinkSource,
type: ENTITY_TYPE.LINK
},
diff --git a/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx b/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx
index 020249915e..44b6bce1bf 100644
--- a/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx
+++ b/saleor/static/dashboard-next/pages/components/PageInfo/PageInfo.tsx
@@ -38,7 +38,7 @@ const PageInfo: React.StatelessComponent<PageInfoProps> = ({
<FormSpacer />
<RichTextEditor
disabled={disabled}
- initial={data.content}
+ initial={JSON.parse(data.content)}
label={i18n.t("Content")}
name={"content" as keyof FormData}
onChange={onChange}
diff --git a/saleor/static/dashboard-next/storybook/stories/components/RichTextEditor.tsx b/saleor/static/dashboard-next/storybook/stories/components/RichTextEditor.tsx
index 53b882ea52..c02702f47a 100644
--- a/saleor/static/dashboard-next/storybook/stories/components/RichTextEditor.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/components/RichTextEditor.tsx
@@ -1,26 +1,95 @@
import { storiesOf } from "@storybook/react";
import * as React from "react";
-import Form from "../../../components/Form";
import RichTextEditor from "../../../components/RichTextEditor";
import Decorator from "../../Decorator";
storiesOf("Generics / Rich text editor", module)
.addDecorator(Decorator)
.add("default", () => (
- <Form
+ <RichTextEditor
+ disabled={false}
initial={{
- content: null
+ blocks: [
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [{ offset: 0, length: 4, style: "BOLD" }],
+ key: "rosn",
+ text: "bold",
+ type: "unstyled"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [{ offset: 0, length: 6, style: "ITALIC" }],
+ key: "6tbch",
+ text: "italic",
+ type: "unstyled"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [],
+ key: "9rabl",
+ text: "h2",
+ type: "header-two"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [],
+ key: "bv0ac",
+ text: "h3",
+ type: "header-three"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [],
+ key: "2ip7q",
+ text: "blockquote",
+ type: "blockquote"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [],
+ key: "8r8ss",
+ text: "ul",
+ type: "unordered-list-item"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [],
+ inlineStyleRanges: [],
+ key: "911hc",
+ text: "ol",
+ type: "ordered-list-item"
+ },
+ {
+ data: {},
+ depth: 0,
+ entityRanges: [{ offset: 0, length: 4, key: 0 }],
+ inlineStyleRanges: [],
+ key: "5aejo",
+ text: "link",
+ type: "unstyled"
+ }
+ ],
+ entityMap: {
+ "0": { type: "LINK", mutability: "MUTABLE", data: { href: "#" } }
+ }
}}
- >
- {({ change, data }) => (
- <RichTextEditor
- disabled={false}
- initial={data.content}
- label="Content"
- name="content"
- onChange={change}
- />
- )}
- </Form>
+ label="Content"
+ name="content"
+ onChange={() => undefined}
+ />
));