forked from dburrows/draft-js-basic-html-editor
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasicHtmlEditor.js
More file actions
231 lines (205 loc) · 6.47 KB
/
Copy pathBasicHtmlEditor.js
File metadata and controls
231 lines (205 loc) · 6.47 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React from 'react';
import ReactDOM from 'react-dom';
import debounce from 'lodash/debounce';
import {
Editor,
EditorState,
ContentState,
Entity,
RichUtils,
convertToRaw,
CompositeDecorator,
Modifier
} from 'draft-js';
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';
export default class BasicHtmlEditor extends React.Component {
constructor(props) {
super(props);
let { value } = props;
const decorator = new CompositeDecorator([
{
strategy: findEntities.bind(null, 'link'),
component: Link
}
]);
this.ENTITY_CONTROLS = [
{label: 'Add Link', action: this._addLink.bind(this) },
{label: 'Remove Link', action: this._removeLink.bind(this) }
];
this.INLINE_STYLES = [
{label: 'Bold', style: 'BOLD'},
{label: 'Italic', style: 'ITALIC'},
{label: 'Underline', style: 'UNDERLINE'},
{label: 'Monospace', style: 'CODE'},
{label: 'Strikethrough', style: 'STRIKETHROUGH'}
];
this.BLOCK_TYPES = [
{label: 'P', style: 'unstyled'},
{label: 'H1', style: 'header-one'},
{label: 'H2', style: 'header-two'},
{label: 'Blockquote', style: 'blockquote'},
{label: 'UL', style: 'unordered-list-item'},
{label: 'OL', style: 'ordered-list-item'},
{label: 'Code Block', style: 'code-block'}
];
this.state = {
editorState: value ?
EditorState.createWithContent(
ContentState.createFromBlockArray(htmlToContent(value)),
decorator
) :
EditorState.createEmpty(decorator)
};
// this.focus = () => this.refs.editor.focus();
this.onChange = (editorState) => {
let previousContent = this.state.editorState.getCurrentContent();
this.setState({editorState});
// only emit html when content changes
if( previousContent !== editorState.getCurrentContent() ) {
this.emitHTML(editorState);
}
};
function emitHTML(editorState) {
let raw = convertToRaw( editorState.getCurrentContent() );
let html = draftRawToHtml(raw);
this.props.onChange(html);
}
this.emitHTML = debounce(emitHTML, this.props.debounce);
this.handleKeyCommand = (command) => this._handleKeyCommand(command);
this.toggleBlockType = (type) => this._toggleBlockType(type);
this.toggleInlineStyle = (style) => this._toggleInlineStyle(style);
this.handleReturn = (e) => this._handleReturn(e);
this.addLink = this._addLink.bind(this);
this.removeLink = this._removeLink.bind(this);
}
_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
)
);
}
_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()) {
return;
}
const href = window.prompt('Enter a URL');
const entityKey = Entity.create('link', 'MUTABLE', {href});
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>
);
}
}
// 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;
}
}