@@ -4,7 +4,7 @@ import _ from 'lodash'
44import ModeIcon from 'boost/components/ModeIcon'
55import MarkdownPreview from 'boost/components/MarkdownPreview'
66import CodeEditor from 'boost/components/CodeEditor'
7- import { IDLE_MODE , CREATE_MODE , EDIT_MODE , switchMode , switchArticle , switchFolder , updateArticle , destroyArticle } from 'boost/actions'
7+ import { IDLE_MODE , CREATE_MODE , EDIT_MODE , switchMode , switchArticle , switchFolder , clearSearch , updateArticle , destroyArticle } from 'boost/actions'
88import aceModes from 'boost/ace-modes'
99import Select from 'react-select'
1010import linkState from 'boost/linkState'
@@ -33,16 +33,23 @@ export default class ArticleDetail extends React.Component {
3333 }
3434
3535 componentWillReceiveProps ( nextProps ) {
36- if ( nextProps . activeArticle != null && ( nextProps . activeArticle . key !== this . state . article . key ) || ( nextProps . status . mode !== this . props . status . mode ) ) {
37- this . setState ( { article : makeInstantArticle ( nextProps . activeArticle ) } , function ( ) {
38- console . log ( 'receive props' )
36+ let nextState = { }
37+
38+ let isArticleChanged = nextProps . activeArticle != null && ( nextProps . activeArticle . key !== this . state . article . key )
39+ let isModeChanged = nextProps . status . mode !== this . props . status . mode
40+ if ( isArticleChanged || ( isModeChanged && nextProps . status . mode !== IDLE_MODE ) ) {
41+ Object . assign ( nextState , {
42+ article : makeInstantArticle ( nextProps . activeArticle )
3943 } )
4044 }
4145
42- let isEdit = nextProps . status . mode === EDIT_MODE || nextProps . status . mode === CREATE_MODE
43- if ( isEdit && this . state . openDeleteConfirmMenu ) {
44- this . setState ( { openDeleteConfirmMenu : false } )
46+ if ( isModeChanged ) {
47+ Object . assign ( nextState , {
48+ openDeleteConfirmMenu : false ,
49+ previewMode : false
50+ } )
4551 }
52+ this . setState ( nextState )
4653 }
4754
4855 renderEmpty ( ) {
@@ -152,7 +159,12 @@ export default class ArticleDetail extends React.Component {
152159
153160 dispatch ( updateArticle ( newArticle ) )
154161 dispatch ( switchMode ( IDLE_MODE ) )
162+ // Folder filterがかかっている時に、
163+ // Searchを初期化し、更新先のFolder filterをかける
164+ // かかれていない時に
165+ // Searchを初期化する
155166 if ( filters . folder . length !== 0 ) dispatch ( switchFolder ( folder . name ) )
167+ else dispatch ( clearSearch ( ) )
156168 dispatch ( switchArticle ( newArticle . key ) )
157169 }
158170
@@ -182,6 +194,10 @@ export default class ArticleDetail extends React.Component {
182194 this . setState ( { article : article } )
183195 }
184196
197+ handleTogglePreviewButtonClick ( e ) {
198+ this . setState ( { previewMode : ! this . state . previewMode } )
199+ }
200+
185201 renderEdit ( ) {
186202 let { folders } = this . props
187203
@@ -209,6 +225,11 @@ export default class ArticleDetail extends React.Component {
209225 />
210226 </ div >
211227 < div className = 'right' >
228+ {
229+ this . state . article . mode === 'markdown'
230+ ? ( < button className = 'preview' onClick = { e => this . handleTogglePreviewButtonClick ( e ) } > Toggle Preview</ button > )
231+ : null
232+ }
212233 < button onClick = { e => this . handleCancelButtonClick ( e ) } > Cancel</ button >
213234 < button onClick = { e => this . handleSaveButtonClick ( e ) } className = 'primary' > Save</ button >
214235 </ div >
@@ -229,12 +250,16 @@ export default class ArticleDetail extends React.Component {
229250 className = 'mode'
230251 />
231252 </ div >
232- < CodeEditor
233- onChange = { ( e , value ) => this . handleContentChange ( e , value ) }
234- readOnly = { false }
235- mode = { this . state . article . mode }
236- code = { this . state . article . content }
237- />
253+
254+ { this . state . previewMode
255+ ? < MarkdownPreview content = { this . state . article . content } />
256+ : ( < CodeEditor
257+ onChange = { ( e , value ) => this . handleContentChange ( e , value ) }
258+ readOnly = { false }
259+ mode = { this . state . article . mode }
260+ code = { this . state . article . content }
261+ /> )
262+ }
238263 </ div >
239264 </ div >
240265 </ div >
0 commit comments