Skip to content

Commit 6698d15

Browse files
committed
auto scrolling for markdown preview
1 parent ef35fd0 commit 6698d15

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

browser/main/HomePage/ArticleDetail/ArticleEditor.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,50 @@ export default class ArticleEditor extends React.Component {
1111
super(props)
1212
this.isMouseDown = false
1313
this.state = {
14-
status: PREVIEW_MODE
14+
status: PREVIEW_MODE,
15+
cursorPosition: null,
16+
firstVisibleRow: null
1517
}
1618
}
1719

18-
componentWillReceiveProps (nextProps) {
20+
resetCursorPosition () {
21+
this.setState({
22+
cursorPosition: null,
23+
firstVisibleRow: null
24+
}, function () {
25+
let previewEl = ReactDOM.findDOMNode(this.refs.preview)
26+
previewEl.scrollTop = 0
27+
})
1928
}
2029

2130
switchPreviewMode () {
31+
let cursorPosition = this.refs.editor.getCursorPosition()
32+
let firstVisibleRow = this.refs.editor.getFirstVisibleRow()
2233
this.setState({
23-
status: PREVIEW_MODE
34+
status: PREVIEW_MODE,
35+
cursorPosition,
36+
firstVisibleRow
37+
}, function () {
38+
let previewEl = ReactDOM.findDOMNode(this.refs.preview)
39+
let anchors = previewEl.querySelectorAll('.lineAnchor')
40+
for (let i = 0; i < anchors.length; i++) {
41+
if (parseInt(anchors[i].dataset.key, 10) > cursorPosition.row || i === anchors.length - 1) {
42+
var targetAnchor = anchors[i > 0 ? i - 1 : 0]
43+
previewEl.scrollTop = targetAnchor.offsetTop - 100
44+
break
45+
}
46+
}
2447
})
2548
}
2649

2750
switchEditMode () {
2851
this.setState({
2952
status: EDIT_MODE
3053
}, function () {
54+
if (this.state.cursorPosition != null) {
55+
this.refs.editor.moveCursorTo(this.state.cursorPosition.row, this.state.cursorPosition.column)
56+
this.refs.editor.scrollToLine(this.state.firstVisibleRow)
57+
}
3158
this.refs.editor.editor.focus()
3259
})
3360
}
@@ -61,6 +88,7 @@ export default class ArticleEditor extends React.Component {
6188
return (
6289
<div className='ArticleEditor'>
6390
<MarkdownPreview
91+
ref='preview'
6492
onMouseUp={e => this.handlePreviewMouseUp(e)}
6593
onMouseDown={e => this.handlePreviewMouseDown(e)}
6694
onMouseMove={e => this.handlePreviewMouseMove(e)}
@@ -73,7 +101,8 @@ export default class ArticleEditor extends React.Component {
73101

74102
return (
75103
<div className='ArticleEditor'>
76-
<CodeEditor ref='editor'
104+
<CodeEditor
105+
ref='editor'
77106
onBlur={e => this.handleBlurCodeEditor(e)}
78107
onChange={this.props.onChange}
79108
mode={this.props.mode}

browser/main/HomePage/ArticleDetail/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ export default class ArticleDetail extends React.Component {
164164
this.setState(nextState)
165165
}
166166

167+
componentDidUpdate (prevProps, prevState) {
168+
if (this.props.activeArticle == null || prevProps.activeArticle == null || this.props.activeArticle.key !== prevProps.activeArticle.key) {
169+
this.refs.editor.resetCursorPosition()
170+
}
171+
}
172+
167173
editArticle () {
168174
ReactDOM.findDOMNode(this.refs.title).focus()
169175
ReactDOM.findDOMNode(this.refs.title).select()

0 commit comments

Comments
 (0)