-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBasicHtmlEditor.js
More file actions
212 lines (189 loc) · 5.95 KB
/
BasicHtmlEditor.js
File metadata and controls
212 lines (189 loc) · 5.95 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, { Component } from 'react';
import {
Editor,
EditorState,
RichUtils,
convertToRaw,
CompositeDecorator,
Modifier
} from 'draft-js/lib/Draft';
import htmlToContent from './utils/htmlToContent';
import draftRawToHtml from './utils/draftRawToHtml';
import Link from './components/Link';
import EntityControls from './components/EntityControls';
import InlineStyleControls from './components/InlineStyleControls';
import BlockStyleControls from './components/BlockStyleControls';
import findEntities from './utils/findEntities';
import { INLINE_STYLES, BLOCK_TYPES, ENTITY_CONTROLS } from './config/constants';
// Custom overrides for "code" style.
const styleMap = {
CODE: {
backgroundColor: 'rgba(0, 0, 0, 0.05)',
fontFamily: '"Inconsolata", "Menlo", "Consolas", monospace',
fontSize: 16,
padding: 2
}
};
const getBlockStyle = block => {
switch (block.getType()) {
case 'blockquote': return 'RichEditor-blockquote';
default: return null;
}
};
const decorator = new CompositeDecorator([
{
strategy: findEntities,
component: Link
}
]);
export default class BasicHtmlEditor extends Component {
constructor(props) {
super(props);
const { value } = props;
this.focus = () => this.refs.editor.focus();
this.ENTITY_CONTROLS = ENTITY_CONTROLS.map(control => {
control.action = this[control.actionName];
return control;
});
this.INLINE_STYLES = INLINE_STYLES;
this.BLOCK_TYPES = BLOCK_TYPES;
const contentState = htmlToContent(value);
this.state = {
editorState: EditorState.createWithContent(contentState, decorator)
};
this.handleKeyCommand = (command) => this._handleKeyCommand(command);
this.toggleBlockType = (type) => this._toggleBlockType(type);
this.toggleInlineStyle = (style) => this._toggleInlineStyle(style);
this.handleReturn = (e) => this._handleReturn(e);
}
componentWillUpdate(nextProps, nextState) {
// only emit html when content changes
const previousContent = this.state.editorState.getCurrentContent();
if( previousContent !== nextState.editorState.getCurrentContent() ) {
clearTimeout(this.timer);
this.timer = setTimeout(this.emitHTML(nextState.editorState), nextProps.debounce);
}
}
emitHTML = (editorState) => () => {
const raw = convertToRaw( editorState.getCurrentContent() );
const html = draftRawToHtml(raw);
this.props.onChange(html);
}
_handleKeyCommand(command) {
const {editorState} = this.state;
const newState = RichUtils.handleKeyCommand(editorState, command);
if (newState) {
this.onChange(newState);
return true;
}
return false;
}
_handleReturn(e) {
if (e.metaKey === true) {
return this._addLineBreak();
} else {
return false;
}
}
_toggleBlockType(blockType) {
this.onChange(
RichUtils.toggleBlockType(
this.state.editorState,
blockType
)
);
}
_toggleInlineStyle(inlineStyle) {
this.onChange(
RichUtils.toggleInlineStyle(
this.state.editorState,
inlineStyle
)
);
}
onChange = (editorState) => {
this.setState({editorState});
};
_addLineBreak(/* e */) {
let newContent, newEditorState;
const {editorState} = this.state;
const content = editorState.getCurrentContent();
const selection = editorState.getSelection();
const block = content.getBlockForKey(selection.getStartKey());
// console.log(content.toJS(), selection.toJS(), block.toJS());
if (block.type === 'code-block') {
newContent = Modifier.insertText(content, selection, '\n');
newEditorState = EditorState.push(editorState, newContent, 'add-new-line');
this.onChange(newEditorState);
return true;
} else {
return false;
}
}
addLink = (/* e */) => {
const {editorState} = this.state;
const selection = editorState.getSelection();
if (!selection.isCollapsed()) {
const url = window.prompt('Enter a URL');
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity(
'LINK',
'MUTABLE',
{url}
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
this.onChange(RichUtils.toggleLink(editorState, selection, entityKey));
}
}
removeLink = (/* e */) => {
const {editorState} = this.state;
const selection = editorState.getSelection();
if (selection.isCollapsed()) {
return;
}
this.onChange( RichUtils.toggleLink(editorState, selection, null));
}
render() {
const {editorState} = this.state;
// If the user changes block type before entering any text, we can
// either style the placeholder or hide it. Let's just hide it now.
let className = 'RichEditor-editor';
var contentState = editorState.getCurrentContent();
if (!contentState.hasText()) {
if (contentState.getBlockMap().first().getType() !== 'unstyled') {
className += ' RichEditor-hidePlaceholder';
}
}
return (
<div className="RichEditor-root draftjs-bhe">
<BlockStyleControls
editorState={editorState}
blockTypes={this.BLOCK_TYPES}
onToggle={this.toggleBlockType}
/>
<InlineStyleControls
editorState={editorState}
onToggle={this.toggleInlineStyle}
inlineStyles={this.INLINE_STYLES}
/>
<EntityControls
editorState={editorState}
entityControls={this.ENTITY_CONTROLS}
/>
<div className={className} onClick={this.focus} >
<Editor
blockStyleFn={getBlockStyle}
customStyleMap={styleMap}
editorState={editorState}
handleKeyCommand={this.handleKeyCommand}
handleReturn={this.handleReturn}
onChange={this.onChange}
placeholder="Tell a story..."
ref="editor"
spellCheck={true}
/>
</div>
</div>
);
}
}