File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -48,10 +48,12 @@ class MarkdownEditor extends React.Component {
4848 handleBlur ( e ) {
4949 let { config } = this . props
5050 if ( config . editor . switchPreview === 'BLUR' ) {
51+ let cursorPosition = this . refs . code . getCursorPosition ( )
5152 this . setState ( {
5253 status : 'PREVIEW'
5354 } , ( ) => {
5455 this . refs . preview . focus ( )
56+ this . refs . preview . scrollTo ( cursorPosition . row )
5557 } )
5658 }
5759 }
@@ -62,7 +64,7 @@ class MarkdownEditor extends React.Component {
6264
6365 handlePreviewMouseUp ( e ) {
6466 let { config } = this . props
65- if ( config . editor . switchPreview === 'BLUR' && new Date ( ) - this . previewMouseDownedAt < 150 ) {
67+ if ( config . editor . switchPreview === 'BLUR' && new Date ( ) - this . previewMouseDownedAt < 200 ) {
6668 this . setState ( {
6769 status : 'CODE'
6870 } , ( ) => {
Original file line number Diff line number Diff line change @@ -120,6 +120,24 @@ export default class MarkdownPreview extends React.Component {
120120 this . refs . root . focus ( )
121121 }
122122
123+ getWindow ( ) {
124+ return this . refs . root . contentWindow
125+ }
126+
127+ scrollTo ( targetRow ) {
128+ let lineAnchors = this . getWindow ( ) . document . querySelectorAll ( 'a.lineAnchor' )
129+
130+ for ( let index = 0 ; index < lineAnchors . length ; index ++ ) {
131+ let lineAnchor = lineAnchors [ index ]
132+ let row = parseInt ( lineAnchor . getAttribute ( 'data-key' ) )
133+ if ( row > targetRow ) {
134+ let targetAnchor = lineAnchors [ index - 1 ]
135+ this . getWindow ( ) . scrollTo ( 0 , targetAnchor . offsetTop )
136+ break
137+ }
138+ }
139+ }
140+
123141 render ( ) {
124142 let { className, style, tabIndex } = this . props
125143 return (
You can’t perform that action at this time.
0 commit comments