11import React , { PropTypes } from 'react'
22import { Editor , EditorState , RichUtils } from 'draft-js'
3- import { stateToHTML } from 'draft-js-export-html '
3+ import { stateToMarkdown } from 'draft-js-export-markdown '
44import cn from 'classnames'
55import './draftjs.scss'
66import { Avatar } from 'appirio-tech-react-components'
7- // import { Icons } from 'appirio-tech-react-components'
87
98const styles = [
109 { className : 'bold' , style : 'BOLD' } ,
11- { className : 'italic' , style : 'ITALIC' } ,
12- { className : 'underline' , style : 'UNDERLINE' }
10+ { className : 'italic' , style : 'ITALIC' }
11+ // {className: 'underline', style: 'UNDERLINE'}
1312]
1413
1514const blocks = [
@@ -41,13 +40,13 @@ class NewPost extends React.Component {
4140
4241 constructor ( props ) {
4342 super ( props )
44- this . state = { editorState : EditorState . createEmpty ( ) , expandedEditor : false }
45- this . onChange = ( editorState ) => this . setState ( { editorState} )
46-
43+ this . state = { editorState : EditorState . createEmpty ( ) , expandedEditor : false , canSubmit : false }
44+ this . onEditorChange = this . onEditorChange . bind ( this )
4745 this . handleKeyCommand = this . handleKeyCommand . bind ( this )
4846 this . toggleBlockType = this . toggleBlockType . bind ( this )
4947 this . toggleInlineStyle = this . toggleInlineStyle . bind ( this )
5048 this . onClickOutside = this . onClickOutside . bind ( this )
49+ this . onNewPostChange = this . onNewPostChange . bind ( this )
5150 }
5251
5352 componentDidMount ( ) {
@@ -64,7 +63,7 @@ class NewPost extends React.Component {
6463 let isEditor = false
6564 let isCloseButton = false
6665 const title = this . refs . title . value
67- const content = stateToHTML ( this . state . editorState . getCurrentContent ( ) )
66+ const hasContent = this . state . editorState . getCurrentContent ( ) . hasText ( )
6867
6968 do {
7069 if ( currNode . className
@@ -87,7 +86,7 @@ class NewPost extends React.Component {
8786 // if any of title and content, is non empty, do not proceed
8887 if ( ! isEditor
8988 && ! isCloseButton
90- && ( ( title && title . length > 0 ) || ( content && content !== '<p><br></p>' ) ) ) {
89+ && ( ( title && title . trim ( ) . length > 0 ) || hasContent ) ) {
9190 return
9291 }
9392
@@ -102,23 +101,34 @@ class NewPost extends React.Component {
102101 const { editorState} = this . state
103102 const newState = RichUtils . handleKeyCommand ( editorState , command )
104103 if ( newState ) {
105- this . onChange ( newState )
104+ this . onEditorChange ( newState )
106105 return true
107106 }
108107 return false
109108 }
110109
111110 toggleBlockType ( blockType ) {
112- this . onChange ( RichUtils . toggleBlockType ( this . state . editorState , blockType ) )
111+ this . onEditorChange ( RichUtils . toggleBlockType ( this . state . editorState , blockType ) )
113112 }
114113
115114 toggleInlineStyle ( inlineStyle ) {
116- this . onChange ( RichUtils . toggleInlineStyle ( this . state . editorState , inlineStyle ) )
115+ this . onEditorChange ( RichUtils . toggleInlineStyle ( this . state . editorState , inlineStyle ) )
116+ }
117+
118+ onEditorChange ( editorState ) {
119+ this . setState ( { editorState} )
120+ this . onNewPostChange ( )
121+ }
122+
123+ onNewPostChange ( ) {
124+ this . setState ( {
125+ canSubmit : ! ! this . refs . title . value . trim ( ) . length && this . state . editorState . getCurrentContent ( ) . hasText ( )
126+ } )
117127 }
118128
119129 render ( ) {
120- const { currentUser, heading , titlePlaceholder } = this . props
121- const { editorState} = this . state
130+ const { currentUser, titlePlaceholder , isCreating } = this . props
131+ const { editorState, canSubmit } = this . state
122132 const currentStyle = editorState . getCurrentInlineStyle ( )
123133 const selection = editorState . getSelection ( )
124134 let authorName = currentUser . firstName
@@ -137,7 +147,7 @@ class NewPost extends React.Component {
137147 return
138148 }
139149 const title = this . refs . title . value
140- const content = stateToHTML ( editorState . getCurrentContent ( ) )
150+ const content = stateToMarkdown ( editorState . getCurrentContent ( ) )
141151 if ( title && content ) {
142152 this . props . onPost ( { title, content} )
143153 this . setState ( { editorState : EditorState . createEmpty ( ) } )
@@ -159,9 +169,11 @@ class NewPost extends React.Component {
159169 { /* No need to handle click event of the close button as its already
160170 handled in onClickOutside handler */ }
161171 < a href = "javascript:" className = "btn-close" />
172+ { /*
162173 <div className="modal-title title-muted">
163- { heading || 'NEW STATUS POST' }
174+ { heading || 'NEW STATUS POST' }
164175 </div>
176+ */ }
165177 < div className = "modal-row" >
166178 < div className = "portrait" >
167179 < Avatar avatarUrl = { currentUser . photoURL } userName = { authorName } />
@@ -171,13 +183,14 @@ class NewPost extends React.Component {
171183 ref = "title"
172184 className = "new-post-title"
173185 type = "text"
186+ onChange = { this . onNewPostChange }
174187 placeholder = { titlePlaceholder || 'Title of the post' }
175188 />
176189 < div className = "draftjs-editor tc-textarea" >
177190 < Editor
178191 ref = "editor"
179192 editorState = { editorState }
180- onChange = { this . onChange }
193+ onChange = { this . onEditorChange }
181194 handleKeyCommand = { this . handleKeyCommand }
182195 />
183196 < div className = "textarea-footer" >
@@ -206,7 +219,7 @@ class NewPost extends React.Component {
206219 { /*<div className="separator"/>
207220 <button className="attach"/>*/ }
208221 </ div >
209- < button className = "tc-btn tc-btn-primary tc-btn-sm" onClick = { onPost } disabled = { this . props . isCreating } >
222+ < button className = "tc-btn tc-btn-primary tc-btn-sm" onClick = { onPost } disabled = { isCreating || ! canSubmit } >
210223 { this . props . isCreating ? 'Posting...' : 'Post' }
211224 </ button >
212225 </ div >
0 commit comments