Skip to content

Commit ca79857

Browse files
committed
編集状態でのMarkdown preview追加
1 parent 60e551e commit ca79857

5 files changed

Lines changed: 46 additions & 15 deletions

File tree

browser/main/HomePage.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const TAG_FILTER = 'TAG_FILTER'
1616

1717
class HomePage extends React.Component {
1818
componentDidMount () {
19+
// React自体のKey入力はfocusされていないElementからは動かないため、
20+
// `window`に直接かける
1921
this.listener = (e) => this.handleKeyDown(e)
2022
window.addEventListener('keydown', this.listener)
2123
}
@@ -33,6 +35,7 @@ class HomePage extends React.Component {
3335
let { status } = this.props
3436
let { nav, top, list, detail } = this.refs
3537

38+
// Search inputがfocusされていたら大体のキー入力は無視される。
3639
if (top.isInputFocused() && !e.metaKey) {
3740
if (e.keyCode === 13 || e.keyCode === 27) top.escape()
3841
return

browser/main/HomePage/ArticleDetail.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from 'lodash'
44
import ModeIcon from 'boost/components/ModeIcon'
55
import MarkdownPreview from 'boost/components/MarkdownPreview'
66
import 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'
88
import aceModes from 'boost/ace-modes'
99
import Select from 'react-select'
1010
import 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>

browser/main/MainPage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export default class MainContainer extends React.Component {
2929
{this.state.updateAvailable ? (
3030
<button onClick={this.updateApp} className='appUpdateButton'><i className='fa fa-cloud-download'/> Update available!</button>
3131
) : null}
32-
<button onClick={this.openContactModal} className='contactButton'>
32+
{/* <button onClick={this.openContactModal} className='contactButton'>
3333
<i className='fa fa-paper-plane-o'/>
3434
<div className='tooltip'>Contact us</div>
35-
</button>
35+
</button> */}
3636
{this.props.children}
3737
</div>
3838
)

browser/styles/main/HomeContainer/components/ArticleDetail.styl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ iptFocusBorderColor = #369DCD
138138
background-color darken(white, 5%)
139139
border solid 1px borderColor
140140
border-radius 5px
141+
&.preview
142+
width inherit
141143
&:hover
142144
background-color white
143145
&.primary

lib/reducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function articles (state = initialArticles, action) {
109109
}
110110

111111
function status (state = initialStatus, action) {
112+
state = Object.assign({}, state)
112113
switch (action.type) {
113114
case SWITCH_FOLDER:
114115
state.mode = IDLE_MODE

0 commit comments

Comments
 (0)