@@ -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 }
0 commit comments