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

Commit 9348a05

Browse files
committed
init
1 parent 65e6779 commit 9348a05

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

demo/client/components/DemoEditor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class DemoEditor extends Component {
5252
const { editorState } = this.state;
5353
return (
5454
<div className={styles.root}>
55-
<div className={styles.editor} onClick={this.focus}>
55+
<div className={styles.editor}>
5656
<Editor
5757
editorState={editorState}
5858
onChange={this.onChange}

src/components/Code/index.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React, { PureComponent } from "react";
2+
import { Map } from "immutable";
3+
import { EditorState, EditorBlock, Modifier } from "draft-js";
4+
5+
const languages = {
6+
bash: "Bash",
7+
c: "C",
8+
cpp: "C++",
9+
css: "CSS",
10+
go: "Go",
11+
html: "HTML",
12+
java: "Java",
13+
javascript: "JavaScript",
14+
js: "JavaScript",
15+
kotlin: "Kotlin",
16+
mathml: "MathML",
17+
perl: "Perl",
18+
ruby: "Ruby",
19+
scala: "Scala",
20+
sql: "SQL",
21+
svg: "SVG",
22+
swift: "Swift",
23+
};
24+
25+
class CodeBlock extends PureComponent {
26+
onChange = ev => {
27+
ev.preventDefault();
28+
const blockKey = this.props.block.getKey();
29+
const { getEditorState, setEditorState } = this.props.blockProps;
30+
const editorState = getEditorState();
31+
const selection = editorState.getSelection();
32+
const language = ev.currentTarget.value;
33+
34+
let content = editorState.getCurrentContent();
35+
content = Modifier.mergeBlockData(content, selection, Map({ language }));
36+
37+
const newEditorState = EditorState.push(
38+
editorState,
39+
content,
40+
"change-block-data"
41+
);
42+
43+
// setTimeout(() => {
44+
// setEditorState(EditorState.forceSelection(
45+
// newEditorState,
46+
// content.getSelectionAfter()
47+
// ))
48+
// }, 3000)
49+
};
50+
51+
render() {
52+
const { language } = this.props.blockProps;
53+
54+
return (
55+
<div>
56+
<EditorBlock {...this.props} />
57+
<div contentEditable={false}>
58+
<select value={language} onChange={this.onChange}>
59+
{Object.keys(this.props.languages).map(lang => (
60+
<option key={lang} value={lang}>
61+
{this.props.languages[lang]}
62+
</option>
63+
))}
64+
</select>
65+
</div>
66+
</div>
67+
);
68+
}
69+
}
70+
71+
CodeBlock.defaultProps = {
72+
languages,
73+
};
74+
75+
export default CodeBlock;

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export const inlineMatchers = {
66
CODE: [/`([^`]+)`/g],
77
STRIKETHROUGH: [/~~([^(?:~~)]+)~~/g],
88
};
9+
10+
export const CODE_BLOCK_TYPE = "code-block";

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88

99
import { Map } from "immutable";
1010

11+
import CodeBlock from "./components/Code";
1112
import adjustBlockDepth from "./modifiers/adjustBlockDepth";
1213
import handleBlockType from "./modifiers/handleBlockType";
1314
import handleInlineStyle from "./modifiers/handleInlineStyle";
@@ -21,7 +22,7 @@ import changeCurrentBlockType from "./modifiers/changeCurrentBlockType";
2122
import createLinkDecorator from "./decorators/link";
2223
import createImageDecorator from "./decorators/image";
2324
import { replaceText } from "./utils";
24-
import { CODE_BLOCK_REGEX } from "./constants";
25+
import { CODE_BLOCK_REGEX, CODE_BLOCK_TYPE } from "./constants";
2526

2627
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
2728

@@ -104,7 +105,7 @@ const createMarkdownPlugin = (config = {}) => {
104105
blockRenderMap: Map({
105106
"code-block": {
106107
element: "code",
107-
wrapper: <pre spellCheck={"false"} />,
108+
wrapper: <pre spellCheck="false" />,
108109
},
109110
}).merge(checkboxBlockRenderMap),
110111
decorators: [
@@ -139,6 +140,17 @@ const createMarkdownPlugin = (config = {}) => {
139140
},
140141
};
141142
}
143+
case CODE_BLOCK_TYPE: {
144+
return {
145+
component: CodeBlock,
146+
props: {
147+
setEditorState,
148+
language: block.getData().get("language"),
149+
getEditorState,
150+
},
151+
};
152+
}
153+
142154
default:
143155
return null;
144156
}

0 commit comments

Comments
 (0)