Skip to content

Commit 77833ff

Browse files
hikerpigRokt33r
authored andcommitted
Fix preview-window's scroll behavior, #3289
1 parent d010c55 commit 77833ff

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

browser/components/MarkdownEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class MarkdownEditor extends React.Component {
119119
status: 'PREVIEW'
120120
}, () => {
121121
this.refs.preview.focus()
122-
this.refs.preview.scrollTo(cursorPosition.line)
122+
this.refs.preview.scrollToRow(cursorPosition.line)
123123
})
124124
eventEmitter.emit('topbar:togglelockbutton', this.state.status)
125125
}

browser/components/MarkdownPreview.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ ${markdownStyle}
102102
body {
103103
font-family: '${fontFamily.join("','")}';
104104
font-size: ${fontSize}px;
105-
${scrollPastEnd ? 'padding-bottom: 90vh;' : ''}
105+
${scrollPastEnd ? `
106+
padding-bottom: 90vh;
107+
box-sizing: border-box;
108+
`
109+
: ''}
106110
${optimizeOverflowScroll ? 'height: 100%;' : ''}
107111
}
108112
@media print {
@@ -611,7 +615,7 @@ export default class MarkdownPreview extends React.Component {
611615

612616
// Should scroll to top after selecting another note
613617
if (prevProps.noteKey !== this.props.noteKey) {
614-
this.getWindow().scrollTo(0, 0)
618+
this.scrollTo(0, 0)
615619
}
616620
}
617621

@@ -996,7 +1000,11 @@ export default class MarkdownPreview extends React.Component {
9961000
return this.refs.root.contentWindow
9971001
}
9981002

999-
scrollTo (targetRow) {
1003+
/**
1004+
* @public
1005+
* @param {Number} targetRow
1006+
*/
1007+
scrollToRow (targetRow) {
10001008
const blocks = this.getWindow().document.querySelectorAll(
10011009
'body>[data-line]'
10021010
)
@@ -1006,12 +1014,21 @@ export default class MarkdownPreview extends React.Component {
10061014
const row = parseInt(block.getAttribute('data-line'))
10071015
if (row > targetRow || index === blocks.length - 1) {
10081016
block = blocks[index - 1]
1009-
block != null && this.getWindow().scrollTo(0, block.offsetTop)
1017+
block != null && this.scrollTo(0, block.offsetTop)
10101018
break
10111019
}
10121020
}
10131021
}
10141022

1023+
/**
1024+
* `document.body.scrollTo`
1025+
* @param {Number} x
1026+
* @param {Number} y
1027+
*/
1028+
scrollTo (x, y) {
1029+
this.getWindow().document.body.scrollTo(x, y)
1030+
}
1031+
10151032
preventImageDroppedHandler (e) {
10161033
e.preventDefault()
10171034
e.stopPropagation()
@@ -1054,7 +1071,7 @@ export default class MarkdownPreview extends React.Component {
10541071
)
10551072

10561073
if (targetElement != null) {
1057-
this.getWindow().scrollTo(0, targetElement.offsetTop)
1074+
this.scrollTo(0, targetElement.offsetTop)
10581075
}
10591076
return
10601077
}

0 commit comments

Comments
 (0)