From 890207daee94b8d7d72ae92b2610b6789dec7f95 Mon Sep 17 00:00:00 2001 From: Dick Choi Date: Tue, 19 Jul 2016 08:59:47 +0900 Subject: [PATCH] auto scroll --- browser/components/MarkdownEditor.js | 4 +++- browser/components/MarkdownPreview.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/browser/components/MarkdownEditor.js b/browser/components/MarkdownEditor.js index f99f6f77..2e07bc39 100644 --- a/browser/components/MarkdownEditor.js +++ b/browser/components/MarkdownEditor.js @@ -48,10 +48,12 @@ class MarkdownEditor extends React.Component { handleBlur (e) { let { config } = this.props if (config.editor.switchPreview === 'BLUR') { + let cursorPosition = this.refs.code.getCursorPosition() this.setState({ status: 'PREVIEW' }, () => { this.refs.preview.focus() + this.refs.preview.scrollTo(cursorPosition.row) }) } } @@ -62,7 +64,7 @@ class MarkdownEditor extends React.Component { handlePreviewMouseUp (e) { let { config } = this.props - if (config.editor.switchPreview === 'BLUR' && new Date() - this.previewMouseDownedAt < 150) { + if (config.editor.switchPreview === 'BLUR' && new Date() - this.previewMouseDownedAt < 200) { this.setState({ status: 'CODE' }, () => { diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 97108c3f..45304c11 100644 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -120,6 +120,24 @@ export default class MarkdownPreview extends React.Component { this.refs.root.focus() } + getWindow () { + return this.refs.root.contentWindow + } + + scrollTo (targetRow) { + let lineAnchors = this.getWindow().document.querySelectorAll('a.lineAnchor') + + for (let index = 0; index < lineAnchors.length; index++) { + let lineAnchor = lineAnchors[index] + let row = parseInt(lineAnchor.getAttribute('data-key')) + if (row > targetRow) { + let targetAnchor = lineAnchors[index - 1] + this.getWindow().scrollTo(0, targetAnchor.offsetTop) + break + } + } + } + render () { let { className, style, tabIndex } = this.props return (