Skip to content

Commit 5a2ebe8

Browse files
authored
Merge pull request #151 from upfrontIO/preserve-scroll-position
Preserve scroll position
2 parents 14bc668 + b14380e commit 5a2ebe8

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

src/cursor.js

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

45
import * as content from './content'
56
import * as parser from './parser'
67
import * as string from './util/string'
78
import * as nodeType from './node-type'
8-
import * as error from './util/error'
9+
import error from './util/error'
910
import * as rangeSaveRestore from './range-save-restore'
1011

1112
/**
@@ -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
}
@@ -101,7 +104,7 @@ export default class Cursor {
101104
// (The character '|' represents the cursor position)
102105
//
103106
// <div contenteditable="true">fo|o</div>
104-
// before() will return a document frament containing a text node 'fo'.
107+
// before() will return a document fragment containing a text node 'fo'.
105108
//
106109
// @returns {Document Fragment} content before the cursor or selection.
107110
before () {
@@ -119,7 +122,7 @@ export default class Cursor {
119122
// (The character '|' represents the cursor position)
120123
//
121124
// <div contenteditable="true">fo|o</div>
122-
// after() will return a document frament containing a text node 'o'.
125+
// after() will return a document fragment containing a text node 'o'.
123126
//
124127
// @returns {Document Fragment} content after the cursor or selection.
125128
after () {
@@ -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/range-save-restore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import rangy from 'rangy'
2-
import * as error from './util/error'
2+
import error from './util/error'
33
import * as nodeType from './node-type'
44

55
/**

src/util/error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import config from '../config'
1+
import * as config from '../config'
22

33
// Allows for safe error logging
44
// Falls back to console.log if console.error is not available

src/util/viewport.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// code from mdn: https://developer.mozilla.org/en-US/docs/Web/API/window.scrollX
2+
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+
}
7+
8+
export {getScrollPosition}

0 commit comments

Comments
 (0)