-
Notifications
You must be signed in to change notification settings - Fork 2
/
RichEditorExample.js
130 lines (114 loc) · 3.65 KB
/
RichEditorExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import InlineStyleControls from "./InlineStyleControls";
import BlockStyleControls from "./BlockStyleControls";
import React from "react";
import Draft from "draft-js";
import { stateToHTML } from "draft-js-export-html";
import "./RichEditorExample.css";
const { Editor, EditorState, RichUtils, ContentState } = Draft;
// Custom overrides for "code" style.
const styleMap = {
CODE: {
backgroundColor: "rgba(0, 0, 0, 0.05)",
fontFamily: "'Inconsolata', 'Menlo', 'Consolas', monospace",
fontSize: 16,
padding: 2
}
};
function getBlockStyle(block) {
switch (block.getType()) {
case "blockquote":
return "RichEditor-blockquote";
default:
return null;
}
}
class RichEditorExample extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty()
};
this.focus = () => this.refs.editor.focus();
this.onChange = (editorState) => this.setState({ editorState });
this.handleKeyCommand = (command) => this._handleKeyCommand(command);
this.onTab = (e) => this._onTab(e);
this.toggleBlockType = (type) => this._toggleBlockType(type);
this.toggleInlineStyle = (style) => this._toggleInlineStyle(style);
this.handleSubmit = () => this._handleSubmit();
this.resetContent = this.resetContent.bind(this);
}
_handleKeyCommand(command) {
const { editorState } = this.state;
const newState = RichUtils.handleKeyCommand(editorState, command);
if (newState) {
this.onChange(newState);
return true;
}
return false;
}
_onTab(e) {
const maxDepth = 4;
this.onChange(RichUtils.onTab(e, this.state.editorState, maxDepth));
}
_toggleBlockType(blockType) {
this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType));
}
_toggleInlineStyle(inlineStyle) {
this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle));
}
_handleSubmit() {
if (this.state.editorState.getCurrentContent().hasText()) {
const htmlContent = stateToHTML(this.state.editorState.getCurrentContent());
this.props.publishContent(htmlContent);
}
}
resetContent() {
const editorState = EditorState.push(this.state.editorState, ContentState.createFromText(""));
this.setState({ editorState });
}
render() {
const { editorState } = this.state;
let className = "RichEditor-editor";
const contentState = editorState.getCurrentContent();
if (!contentState.hasText()) {
if (contentState.getBlockMap().first().getType() !== "unstyled") {
className += " RichEditor-hidePlaceholder";
}
}
return (
<div className="RichEditor-root">
<BlockStyleControls editorState={ editorState } onToggle={this.toggleBlockType}/>
<InlineStyleControls
editorState={editorState}
onToggle={this.toggleInlineStyle}
/>
<div className={className} onClick={this.focus}>
<Editor
blockStyleFn={getBlockStyle}
customStyleMap={styleMap}
editorState={editorState}
handleKeyCommand={this.handleKeyCommand}
onChange={this.onChange}
onTab={this.onTab}
placeholder="Write a comment"
ref="editor"
spellCheck={true}
/>
</div>
<input
type="button"
value="Submit"
onClick={this.handleSubmit}
disabled={!this.props.submitEnabled}
/>
<input
type="button"
value="Reset"
onClick={this.resetContent}
disabled={!contentState.hasText()}
/>
</div>
);
}
}
export default RichEditorExample;