/
ContentEditor.js
145 lines (131 loc) Β· 3.56 KB
/
ContentEditor.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { Component } from "react";
import { RichUtils } from "draft-js";
import injectSheet from "react-jss";
import Toolbar from "./Toolbar";
import Editor, { composeDecorators } from "draft-js-plugins-editor";
import ImageMenu from "./ImageMenu";
import createEntityPropsPlugin from "draft-js-entity-props-plugin";
import createAutoListPlugin from "draft-js-autolist-plugin";
import createFocusPlugin from "draft-js-focus-plugin";
import createLinkifyPlugin from "draft-js-linkify-plugin";
import createImagePlugin from "draft-js-image-plugin";
import createUndoPlugin from "draft-js-undo-plugin";
import { stateToHTML } from "draft-js-export-html";
const autoListPlugin = createAutoListPlugin();
const entityPlugin = createEntityPropsPlugin();
const focusPlugin = createFocusPlugin();
const linkifyPlugin = createLinkifyPlugin();
const undoPlugin = createUndoPlugin();
const decorator = composeDecorators(focusPlugin.decorator);
const imagePlugin = createImagePlugin({ decorator });
const { UndoButton, RedoButton } = undoPlugin;
const plugins = [
autoListPlugin,
entityPlugin,
focusPlugin,
imagePlugin,
linkifyPlugin,
undoPlugin
];
const styles = {
contentEditor: {
display: "flex",
flexDirection: "column",
borderCollapse: "collapse",
border: "1px solid lightgray",
maxWidth: "600px"
},
toolbar: {
flex: "1"
},
editor: {
display: "block",
border: "1px solid lightgray",
fontFamily: "Minion Pro",
padding: "20px",
maxHeight: "600px",
overflow: "auto"
},
submit: {
background: "transparent",
margin: "10px",
padding: "5px",
maxWidth: "80px",
"&:hover": {
backgroundColor: "lightgray"
}
}
};
class ContentEditor extends Component {
constructor(props) {
super(props);
this.state = {
isImageMenuOpen: false,
};
}
toggleImageMenu = () => {
this.setState({ isImageMenuOpen: !this.state.isImageMenuOpen });
};
addImageToContent = imageSrc => {
const { onChange, editorState } = this.props;
onChange(imagePlugin.addImage(editorState, imageSrc));
};
handleKeyCommand = (command, editorState) => {
const newState = RichUtils.handleKeyCommand(editorState, command);
if (newState) {
// Update state
this.props.onChange(newState);
return "handled";
}
return "not-handled";
};
focus = () => {
this.editor.focus();
};
render() {
const {
editorState,
classes,
onChange,
uploadData,
images,
addImage,
endpoint
} = this.props;
const { isImageMenuOpen } = this.state;
return (
<div className={classes.contentEditor}>
<div className={classes.toolbar}>
<Toolbar
onChange={onChange}
editorState={editorState}
toggleImageMenu={this.toggleImageMenu}
UndoButton={UndoButton}
RedoButton={RedoButton}
/>
</div>
{isImageMenuOpen && (
<ImageMenu
endpoint={endpoint}
uploadData={uploadData}
addImageToState={addImage}
addImageToContent={this.addImageToContent}
images={images}
/>
)}
<div className={classes.editor} onClick={this.focus}>
<Editor
editorState={editorState}
onChange={onChange}
plugins={plugins}
ref={element => {
this.editor = element;
}}
handleKeyCommand={this.handleKeyCommand}
/>
</div>
</div>
);
}
}
export default injectSheet(styles)(ContentEditor);