Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 8453b44

Browse files
committed
- add readme, remove config argument where it isn't used, made languages configurable
1 parent b20468a commit 8453b44

File tree

6 files changed

+80
-34
lines changed

6 files changed

+80
-34
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,48 @@ A [DraftJS] plugin for supporting Markdown syntax shortcuts in DraftJS. This plu
1717
npm i --save draft-js-markdown-plugin
1818
```
1919

20+
## Options
21+
The `draft-js-markdown-plugin` is configurable. Just pass a config object. Here are the available options:
22+
23+
24+
### `renderLanguageSelect`
25+
26+
```js
27+
renderLanguageSelect = ({
28+
// Array of language options
29+
options: Array<{ label, value }>,
30+
// Callback to select an option
31+
onChange: (selectedValue: string) => void,
32+
// Value of selected option
33+
selectedValue: string,
34+
// Label of selected option
35+
selectedLabel: string
36+
}) => React.Node
37+
```
38+
39+
Code blocks render a select to switch syntax highlighting - `renderLanguageSelect` is a render function that lets you override how this is rendered.
40+
41+
Here's an example:
42+
43+
```
44+
import createMarkdownPlugin from 'draft-js-markdown-plugin';
45+
46+
const renderLanguageSelect = ({ options, onChange, selectedValue }) => (
47+
<select value={selectedValue} onChange={onChange}>
48+
{options.map(({ label, value }) => (
49+
<option key={value} value={value}>
50+
{label}
51+
</option>
52+
))}
53+
</select>
54+
);
55+
56+
const markdownPlugin = createMarkdownPlugin({ renderLanguageSelect })
57+
```
58+
59+
### `languages`
60+
Dictionary for languages available to code block switcher
61+
2062
## Usage
2163

2264
```js

demo/client/components/DemoEditor/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ const renderLanguageSelect = ({
5353
</div>
5454
);
5555

56+
const languages = {
57+
js: "JavaScript",
58+
kotlin: "Kotlin",
59+
mathml: "MathML",
60+
};
61+
5662
const plugins = [
5763
prismPlugin,
5864
createMarkdownShortcutsPlugin({ renderLanguageSelect }),

src/components/Code/index.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ const alias = {
88
jsx: "js",
99
};
1010

11-
const languages = {
12-
bash: "Bash",
13-
c: "C",
14-
cpp: "C++",
15-
css: "CSS",
16-
go: "Go",
17-
html: "HTML",
18-
java: "Java",
19-
js: "JavaScript",
20-
kotlin: "Kotlin",
21-
mathml: "MathML",
22-
perl: "Perl",
23-
ruby: "Ruby",
24-
scala: "Scala",
25-
sql: "SQL",
26-
svg: "SVG",
27-
swift: "Swift",
28-
};
29-
3011
const CodeSwitchContainer = enhanceWithClickOutside(
3112
class SwitchContainer extends PureComponent {
3213
handleClickOutside() {
@@ -103,12 +84,15 @@ class CodeBlock extends PureComponent {
10384
const editorState = getEditorState();
10485
const selection = editorState.getSelection();
10586

106-
setEditorState(EditorState.forceSelection(newEditorState, selection));
87+
setEditorState(EditorState.forceSelection(editorState, selection));
10788
};
10889

10990
render() {
110-
const { languages, blockProps } = this.props;
111-
const { renderLanguageSelect, language: _language } = blockProps;
91+
const {
92+
languages,
93+
renderLanguageSelect,
94+
language: _language,
95+
} = this.props.blockProps;
11296

11397
const language = alias[_language] || _language;
11498
const selectedLabel = languages[language];
@@ -144,8 +128,4 @@ class CodeBlock extends PureComponent {
144128
}
145129
}
146130

147-
CodeBlock.defaultProps = {
148-
languages,
149-
};
150-
151131
export default CodeBlock;

src/decorators/image/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import createImageStrategy from "./imageStrategy";
22
import Image from "../../components/Image";
33

4-
const createImageDecorator = (config, store) => ({
5-
strategy: createImageStrategy(config, store),
4+
const createImageDecorator = () => ({
5+
strategy: createImageStrategy(),
66
component: Image,
77
});
88

src/decorators/link/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import createLinkStrategy from "./linkStrategy";
22
import Link from "../../components/Link";
33

4-
const createLinkDecorator = (config, store) => ({
5-
strategy: createLinkStrategy(config, store),
4+
const createLinkDecorator = () => ({
5+
strategy: createLinkStrategy(),
66
component: Link,
77
});
88

src/index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ import createImageDecorator from "./decorators/image";
2424
import { replaceText } from "./utils";
2525
import { CODE_BLOCK_REGEX, CODE_BLOCK_TYPE } from "./constants";
2626

27+
const defaultLanguages = {
28+
bash: "Bash",
29+
c: "C",
30+
cpp: "C++",
31+
css: "CSS",
32+
go: "Go",
33+
html: "HTML",
34+
java: "Java",
35+
js: "JavaScript",
36+
kotlin: "Kotlin",
37+
mathml: "MathML",
38+
perl: "Perl",
39+
ruby: "Ruby",
40+
scala: "Scala",
41+
sql: "SQL",
42+
svg: "SVG",
43+
swift: "Swift",
44+
};
45+
2746
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
2847

2948
const defaultRenderSelect = ({ options, onChange, selectedValue }) => (
@@ -110,6 +129,7 @@ function checkReturnForState(editorState, ev) {
110129

111130
const defaultConfig = {
112131
renderLanguageSelect: defaultRenderSelect,
132+
languages: defaultLanguages,
113133
};
114134

115135
const createMarkdownPlugin = (_config = {}) => {
@@ -128,10 +148,7 @@ const createMarkdownPlugin = (_config = {}) => {
128148
wrapper: <pre spellCheck="false" />,
129149
},
130150
}).merge(checkboxBlockRenderMap),
131-
decorators: [
132-
createLinkDecorator(config, store),
133-
createImageDecorator(config, store),
134-
],
151+
decorators: [createLinkDecorator(), createImageDecorator()],
135152
initialize({ setEditorState, getEditorState }) {
136153
store.setEditorState = setEditorState;
137154
store.getEditorState = getEditorState;
@@ -169,6 +186,7 @@ const createMarkdownPlugin = (_config = {}) => {
169186
props: {
170187
setEditorState,
171188
renderLanguageSelect: config.renderLanguageSelect,
189+
languages: config.languages,
172190
setReadOnly,
173191
language: block.getData().get("language"),
174192
getEditorState,

0 commit comments

Comments
 (0)