Skip to content

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

Add basic link handler

dominik-zeglen961bf313a660
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/Link.tsx b/saleor/static/dashboard-next/components/RichTextEditor/Link.tsx
new file mode 100644
index 0000000000..5ff258481f
--- /dev/null
+++ b/saleor/static/dashboard-next/components/RichTextEditor/Link.tsx
@@ -0,0 +1,10 @@
+import * as React from "react";
+
+interface LinkProps {
+ children: React.ReactNode;
+}
+
+const Link: React.StatelessComponent<LinkProps> = ({ children }) => (
+ <a>{children}</a>
+);
+export default Link;
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx b/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
new file mode 100644
index 0000000000..9eb5887e58
--- /dev/null
+++ b/saleor/static/dashboard-next/components/RichTextEditor/LinkSource.tsx
@@ -0,0 +1,42 @@
+import { AtomicBlockUtils } from "draft-js";
+import * as React from "react";
+
+interface LinkSourceProps {
+ editorState: any;
+ entityType: {
+ type: string;
+ };
+ onComplete: (updateState: any) => void;
+}
+
+class LinkSource extends React.Component<LinkSourceProps> {
+ componentDidMount() {
+ const { editorState, entityType, onComplete } = this.props;
+
+ const src = window.prompt("Link URL");
+
+ if (src) {
+ const content = editorState.getCurrentContent();
+ const contentWithEntity = content.createEntity(
+ entityType.type,
+ "IMMUTABLE",
+ { src }
+ );
+ const entityKey = contentWithEntity.getLastCreatedEntityKey();
+ const nextState = AtomicBlockUtils.insertAtomicBlock(
+ editorState,
+ entityKey,
+ " "
+ );
+
+ onComplete(nextState);
+ } else {
+ onComplete(editorState);
+ }
+ }
+
+ render() {
+ return null;
+ }
+}
+export default LinkSource;
diff --git a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
index 0cf1b86c0c..90fbaae188 100644
--- a/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
+++ b/saleor/static/dashboard-next/components/RichTextEditor/RichTextEditor.tsx
@@ -11,6 +11,8 @@ import {
INLINE_STYLE
} from "draftail";
import * as React from "react";
+import Link from "./Link";
+import LinkSource from "./LinkSource";
export interface RichTextEditorProps {
disabled: boolean;
@@ -57,7 +59,12 @@ const RichTextEditor = withStyles(styles, { name: "RichTextEditor" })(
{ type: INLINE_STYLE.ITALIC }
]}
entityTypes={[
- { type: ENTITY_TYPE.LINK },
+ {
+ attributes: ["url"],
+ decorator: Link,
+ source: LinkSource,
+ type: ENTITY_TYPE.LINK
+ },
{ type: ENTITY_TYPE.HORIZONTAL_RULE }
]}
/>