-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRichTextarea.js
More file actions
108 lines (90 loc) · 2.43 KB
/
RichTextarea.js
File metadata and controls
108 lines (90 loc) · 2.43 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
import React, { Component } from 'react'
import styled from 'styled-components'
import { EditorState, convertToRaw, ContentState } from 'draft-js'
import { Editor } from 'react-draft-wysiwyg'
import draftToHtml from 'draftjs-to-html'
import htmlToDraft from 'html-to-draftjs'
const DEFAULT_TOOLBAR = {
options: [
'inline',
'blockType',
'fontSize',
'fontFamily',
'list',
'textAlign',
'colorPicker',
'link'
]
}
export default class RichTextarea extends Component {
defaultProps = {
markdown: false
}
state = {
editorState: EditorState.createEmpty(),
value: this.props.input.value
}
encode = rawObject => {
return draftToHtml(rawObject)
}
decode = val => {
return htmlToDraft(val)
}
onChange = editorState => {
this.setState({ editorState })
const value = this.encode(convertToRaw(editorState.getCurrentContent()))
this.setState({
value
})
}
onBlur = () => {
let { value, editorState } = this.state
value = editorState.getCurrentContent().hasText() ? value : null
this.props.input.onChange(value)
}
componentDidMount() {
const { input: { value } } = this.props
const editorState = EditorState.createWithContent(
ContentState.createFromBlockArray(this.decode(value))
)
this.setState({ value, editorState })
}
componentWillReceiveProps(nextProps) {
if (this.props.input.value !== nextProps.input.value) {
const editorState = EditorState.createWithContent(
ContentState.createFromBlockArray(this.decode(nextProps.input.value))
)
this.setState({ value: nextProps.input.value, editorState })
}
}
render() {
const { toolbar = DEFAULT_TOOLBAR, minHeight } = this.props
const { editorState } = this.state
return (
<Wrapper suppressContentEditableWarning={true} minHeight={minHeight}>
<Editor
editorState={editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="richEditor"
onEditorStateChange={this.onChange}
onBlur={this.onBlur}
suppressContentEditableWarning={true}
toolbar={toolbar}
minHeight={minHeight}
/>
</Wrapper>
)
}
}
const Wrapper = styled.div`
.richEditor {
padding: 6px 5px 0;
border-radius: 2px;
${props =>
props.minHeight &&
`
min-height: ${props.minHeight}px;
`};
}
`