1
1
import $ from 'jquery'
2
2
import rangy from 'rangy'
3
+ import * as viewport from './util/viewport'
3
4
4
5
import * as content from './content'
5
6
import * as parser from './parser'
6
7
import * as string from './util/string'
7
8
import * as nodeType from './node-type'
8
- import * as error from './util/error'
9
+ import error from './util/error'
9
10
import * as rangeSaveRestore from './range-save-restore'
10
11
11
12
/**
@@ -92,7 +93,9 @@ export default class Cursor {
92
93
// Without setting focus() Firefox is not happy (seems setting a selection is not enough.
93
94
// Probably because Firefox can handle multiple selections).
94
95
if ( this . win . document . activeElement !== this . host ) {
96
+ const { x, y} = viewport . getScrollPosition ( this . win )
95
97
$ ( this . host ) . focus ( )
98
+ this . win . scrollTo ( x , y )
96
99
}
97
100
rangy . getSelection ( this . win ) . setSingleRange ( this . range )
98
101
}
@@ -101,7 +104,7 @@ export default class Cursor {
101
104
// (The character '|' represents the cursor position)
102
105
//
103
106
// <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'.
105
108
//
106
109
// @returns {Document Fragment } content before the cursor or selection.
107
110
before ( ) {
@@ -119,7 +122,7 @@ export default class Cursor {
119
122
// (The character '|' represents the cursor position)
120
123
//
121
124
// <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'.
123
126
//
124
127
// @returns {Document Fragment } content after the cursor or selection.
125
128
after ( ) {
@@ -140,12 +143,9 @@ export default class Cursor {
140
143
const coords = this . range . nativeRange . getBoundingClientRect ( )
141
144
if ( positioning === 'fixed' ) return coords
142
145
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
147
146
148
147
// translate into absolute positions
148
+ const { x, y} = viewport . getScrollPosition ( this . win )
149
149
return {
150
150
top : coords . top + y ,
151
151
bottom : coords . bottom + y ,
0 commit comments