Skip to content

Commit bec0528

Browse files
committed
implement click to edit
1 parent 670f2b1 commit bec0528

2 files changed

Lines changed: 74 additions & 71 deletions

File tree

browser/main/HomePage/ArticleDetail/index.js

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import DeleteArticleModal from '../../modal/DeleteArticleModal'
2020
const electron = require('electron')
2121
const clipboard = electron.clipboard
2222
const ipc = electron.ipcRenderer
23-
const remote = electron.remote
2423

2524
const OSX = global.process.platform === 'darwin'
2625
const BRAND_COLOR = '#18AF90'
@@ -109,7 +108,7 @@ export default class ArticleDetail extends React.Component {
109108

110109
this.state = {
111110
article: Object.assign({content: ''}, props.activeArticle),
112-
previewMode: false,
111+
previewMode: true,
113112
openShareDropdown: false
114113
}
115114

@@ -152,7 +151,6 @@ export default class ArticleDetail extends React.Component {
152151
editArticle () {
153152
ReactDOM.findDOMNode(this.refs.title).focus()
154153
ReactDOM.findDOMNode(this.refs.title).select()
155-
this.setState({previewMode: false})
156154
}
157155

158156
cacheArticle () {
@@ -223,12 +221,6 @@ export default class ArticleDetail extends React.Component {
223221
}, () => this.cacheArticle())
224222
}
225223

226-
handleModeSelectBlur () {
227-
if (this.refs.code != null) {
228-
this.refs.code.editor.focus()
229-
}
230-
}
231-
232224
handleContentChange (e, value) {
233225
let { article } = this.state
234226
article.content = value
@@ -238,37 +230,11 @@ export default class ArticleDetail extends React.Component {
238230
}, () => this.cacheArticle())
239231
}
240232

241-
handleTogglePreviewButtonClick (e) {
242-
if (this.state.article.mode === 'markdown') {
243-
if (!this.state.previewMode) {
244-
let cursorPosition = this.refs.code.getCursorPosition()
245-
let firstVisibleRow = this.refs.code.getFirstVisibleRow()
246-
this.setState({
247-
previewMode: true,
248-
cursorPosition,
249-
firstVisibleRow
250-
}, function () {
251-
let previewEl = ReactDOM.findDOMNode(this.refs.preview)
252-
let anchors = previewEl.querySelectorAll('.lineAnchor')
253-
for (let i = 0; i < anchors.length; i++) {
254-
if (parseInt(anchors[i].dataset.key, 10) > cursorPosition.row || i === anchors.length - 1) {
255-
var targetAnchor = anchors[i > 0 ? i - 1 : 0]
256-
previewEl.scrollTop = targetAnchor.offsetTop - 100
257-
break
258-
}
259-
}
260-
remote.getCurrentWebContents().send('list-focus')
261-
})
262-
} else {
263-
this.setState({
264-
previewMode: false
265-
}, function () {
266-
if (this.state.cursorPosition == null) return true
267-
this.refs.code.moveCursorTo(this.state.cursorPosition.row, this.state.cursorPosition.column)
268-
this.refs.code.scrollToLine(this.state.firstVisibleRow)
269-
this.refs.code.editor.focus()
270-
})
271-
}
233+
handleCodeEditorBlur (e) {
234+
if (this.state.article.mode === 'markdown' && !this.state.previewMode) {
235+
this.setState({
236+
previewMode: true
237+
})
272238
}
273239
}
274240

@@ -285,9 +251,24 @@ export default class ArticleDetail extends React.Component {
285251
}
286252
}
287253

288-
handlePreviewButtonDoubleClick (e) {
254+
handleModeSelectKeyDown (e) {
255+
if (e.keyCode === 9 && !e.shiftKey) {
256+
e.preventDefault()
257+
this.setState({previewMode: false}, function () {
258+
this.refs.code.editor.focus()
259+
})
260+
}
261+
if (e.keyCode === 9 && e.shiftKey) {
262+
e.preventDefault()
263+
ReactDOM.findDOMNode(this.refs.title).focus()
264+
}
265+
}
266+
267+
handlePreviewDoubleClick (e) {
289268
this.setState({
290269
previewMode: false
270+
}, function () {
271+
this.refs.code.editor.focus()
291272
})
292273
}
293274

@@ -373,23 +354,33 @@ export default class ArticleDetail extends React.Component {
373354
<ModeSelect
374355
ref='mode'
375356
onChange={e => this.handleModeChange(e)}
357+
onKeyDown={e => this.handleModeSelectKeyDown(e)}
376358
value={this.state.article.mode}
377359
className='ArticleDetail-panel-header-mode'
378-
onBlur={() => this.handleModeSelectBlur()}
379360
/>
380361
</div>
381362
{status.isTutorialOpen ? modeSelectTutorialElement : null}
382-
383-
{this.state.previewMode
384-
? <MarkdownPreview ref='preview' onDoubleClick={e => this.handlePreviewButtonDoubleClick(e)} content={this.state.article.content}/>
385-
: (<CodeEditor
386-
ref='code'
387-
onChange={(e, value) => this.handleContentChange(e, value)}
388-
readOnly={false}
389-
mode={this.state.article.mode}
390-
code={this.state.article.content}
391-
/>)
392-
}
363+
<div className='ArticleDetail-panel-content'>
364+
{this.state.article.mode === 'markdown' && this.state.previewMode
365+
? (<MarkdownPreview
366+
ref='preview'
367+
onDoubleClick={e => this.handlePreviewDoubleClick(e)}
368+
content={this.state.article.content}
369+
/>)
370+
: (<CodeEditor
371+
ref='code'
372+
onChange={(e, value) => this.handleContentChange(e, value)}
373+
onBlur={e => this.handleCodeEditorBlur(e)}
374+
readOnly={false}
375+
mode={this.state.article.mode}
376+
code={this.state.article.content}
377+
/>)}
378+
{
379+
this.state.article.mode === 'markdown' && this.state.previewMode
380+
? <div className='ArticleDetail-panel-content-tooltip'>Double click to edit post</div>
381+
: null
382+
}
383+
</div>
393384
</div>
394385
</div>
395386
)

browser/styles/main/ArticleDetail.styl

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,35 @@ infoButton()
258258
width 100%
259259
font-size 24px
260260
outline none
261-
.MarkdownPreview
261+
.ArticleDetail-panel-content
262262
absolute left right bottom
263263
top 60px
264-
marked()
265-
box-sizing border-box
266-
padding 5px 15px
267-
border-top solid 1px borderColor
268-
overflow-y auto
269-
user-select all
270-
&.empty
271-
color lighten(inactiveTextColor, 10%)
272-
user-select none
273-
font-size 14px
274-
.CodeEditor
275-
absolute left right bottom
276-
top 60px
277-
border-top solid 1px borderColor
278-
min-height 300px
279-
border-bottom-left-radius 5px
280-
border-bottom-right-radius 5px
264+
.ArticleDetail-panel-content-tooltip
265+
absolute bottom right
266+
height 24px
267+
background-color alpha(black, 0.5)
268+
line-height 24px
269+
color white
270+
padding 0 15px
271+
opacity 0
272+
transition 0.1s
273+
&:hover .ArticleDetail-panel-content-tooltip
274+
opacity 1
275+
.MarkdownPreview
276+
absolute top left right bottom
277+
marked()
278+
box-sizing border-box
279+
padding 5px 15px
280+
border-top solid 1px borderColor
281+
overflow-y auto
282+
user-select all
283+
&.empty
284+
color lighten(inactiveTextColor, 10%)
285+
user-select none
286+
font-size 14px
287+
.CodeEditor
288+
absolute top left right bottom
289+
border-top solid 1px borderColor
290+
min-height 300px
291+
border-bottom-left-radius 5px
292+
border-bottom-right-radius 5px

0 commit comments

Comments
 (0)