-
Notifications
You must be signed in to change notification settings - Fork 2
/
DescriptionHTMLEditor.js
159 lines (141 loc) · 4.82 KB
/
DescriptionHTMLEditor.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Copyright (c) 2016-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { Editor, EditorState, RichUtils } from 'draft-js';
import classNames from 'classnames';
import { convertFromHTML } from 'draft-convert';
import Icon from '../Icon';
import polyglot from '../../i18n';
import StyleControls from './StyleControls';
import { convertDraftJsToHtml } from '../../util/convertDraftJsStateToHtml';
export default class DescriptionHTMLEditor extends React.Component {
constructor(props) {
super(props);
const htmlStr = props.input.value;
const editorState =
htmlStr && htmlStr.length > 0
? EditorState.createWithContent(convertFromHTML(htmlStr))
: EditorState.createEmpty();
this.state = { editorState, previousHasText: htmlStr.length > 0 };
this.focus = () => this.editor.focus();
this.blur = () => this.editor.blur();
this.toggleBlockType = this.toggleBlockType.bind(this);
this.toggleInlineStyle = this.toggleInlineStyle.bind(this);
this.handleDescriptionChange = this.handleDescriptionChange.bind(this);
this.handleDescriptionBlur = this.handleDescriptionBlur.bind(this);
}
componentWillMount() {
this.setEditorContentStateFromHTML(this.props.input.value);
}
shouldComponentUpdate(nextProps) {
if (nextProps.input.value !== this.props.input.value) {
return false;
}
return true;
}
setEditorContentStateFromHTML(htmlStr) {
if (htmlStr !== undefined) {
const contentState = convertFromHTML(htmlStr);
const editorState = EditorState.createWithContent(contentState);
this.handleDescriptionChange(editorState);
}
}
handleDescriptionChange(editorState) {
this.setState(
prevState => ({
editorState,
previousHasText: prevState.editorState.getCurrentContent().hasText(),
}),
() => {
const contentState = editorState.getCurrentContent();
if (!this.state.previousHasText || !contentState.hasText()) {
const descriptionHTML = convertDraftJsToHtml(contentState);
this.props.input.onChange(descriptionHTML);
}
},
);
}
handleDescriptionBlur() {
const contentState = this.state.editorState.getCurrentContent();
const descriptionHTML = convertDraftJsToHtml(contentState);
this.props.onBlur(descriptionHTML);
}
toggleBlockType(blockType) {
this.handleDescriptionChange(
RichUtils.toggleBlockType(this.state.editorState, blockType),
);
}
toggleInlineStyle(inlineStyle) {
this.handleDescriptionChange(
RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle),
);
}
render() {
const { editorState } = this.state;
const contentState = editorState.getCurrentContent();
// 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.
const commentAboveApplies =
!contentState.hasText() &&
contentState
.getBlockMap()
.first()
.getType() !== 'unstyled';
const className = classNames({
'RichEditor-editor learning-step-form_input learning-step-form_paragraph': true,
'RichEditor-hidePlaceholder': commentAboveApplies,
});
return (
<div className="RichEditor-root">
<div className="learning-step-form_group">
<StyleControls
editorState={editorState}
onToggleInline={this.toggleInlineStyle}
onToggleBlock={this.toggleBlockType}
/>
</div>
<div className="learning-step-form_group">
<div className="learning-step-form_left">
<span className="learning-step-form_icon-bg">
<Icon.Create />
</span>
</div>
<div className="learning-step-form_right">
{/*eslint-disable*/}
<div className={className} onClick={this.focus}>
{/* eslint-enable */}
<Editor
editorState={editorState}
onChange={this.handleDescriptionChange}
onBlur={this.handleDescriptionBlur}
placeholder={this.props.placeholder}
ref={editor => {
this.editor = editor;
}}
spellCheck
/>
</div>
</div>
</div>
</div>
);
}
}
DescriptionHTMLEditor.propTypes = {
input: PropTypes.shape({
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func.isRequired,
value: PropTypes.string.isRequired,
}),
onBlur: PropTypes.func,
placeholder: PropTypes.string,
};
DescriptionHTMLEditor.defaultProps = {
placeholder: polyglot.t('editPathStep.stepDescriptionPlaceholder'),
};