Skip to content

Commit 83eb19c

Browse files
committed
fix(pasting): preserve scroll position
1 parent 14bc668 commit 83eb19c

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/cursor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'jquery'
22
import rangy from 'rangy'
3+
import viewport from './util/viewport'
34

45
import * as content from './content'
56
import * as parser from './parser'
@@ -92,7 +93,9 @@ export default class Cursor {
9293
// Without setting focus() Firefox is not happy (seems setting a selection is not enough.
9394
// Probably because Firefox can handle multiple selections).
9495
if (this.win.document.activeElement !== this.host) {
96+
const {x, y} = viewport.getScrollPosition(this.win)
9597
$(this.host).focus()
98+
this.win.scrollTo(x, y)
9699
}
97100
rangy.getSelection(this.win).setSingleRange(this.range)
98101
}
@@ -140,12 +143,9 @@ export default class Cursor {
140143
const coords = this.range.nativeRange.getBoundingClientRect()
141144
if (positioning === 'fixed') return coords
142145

143-
// code from mdn: https://developer.mozilla.org/en-US/docs/Web/API/window.scrollX
144-
const win = this.win
145-
const x = (win.pageXOffset !== undefined) ? win.pageXOffset : (win.document.documentElement || win.document.body.parentNode || win.document.body).scrollLeft
146-
const y = (win.pageYOffset !== undefined) ? win.pageYOffset : (win.document.documentElement || win.document.body.parentNode || win.document.body).scrollTop
147146

148147
// translate into absolute positions
148+
const {x, y} = viewport.getScrollPosition(this.win)
149149
return {
150150
top: coords.top + y,
151151
bottom: coords.bottom + y,

src/util/viewport.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// code from mdn: https://developer.mozilla.org/en-US/docs/Web/API/window.scrollX
2+
export function getScrollPosition (win) {
3+
const x = (win.pageXOffset !== undefined) ? win.pageXOffset : (win.document.documentElement || win.document.body.parentNode || win.document.body).scrollLeft
4+
const y = (win.pageYOffset !== undefined) ? win.pageYOffset : (win.document.documentElement || win.document.body.parentNode || win.document.body).scrollTop
5+
return {x, y}
6+
}

0 commit comments

Comments
 (0)