Skip to content

Commit

Permalink
Get the selection from the prosemirror root insteadd of the window
Browse files Browse the repository at this point in the history
  • Loading branch information
boschDev committed May 19, 2020
1 parent 8ac0edc commit 42993e7
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,6 @@ const getElementFromTextNode = node => {
return node.parentElement
}

const isDomSelectionInView = () => {
const selection = window.getSelection()
const anchorElement = getElementFromTextNode(selection.anchorNode)
if (selection && isInViewport(anchorElement)) {
const focusElement = getElementFromTextNode(selection.focusNode)
if (focusElement === anchorElement ||
focusElement === selection.anchorNode ||
selection.focusNode === focusElement ||
selection.focusNode === selection.anchorNode ||
isInViewport(focusElement)
) {
return true
}
}

return false
}

const isInViewport = element => {
const bounding = element.getBoundingClientRect()
const documentElement = dom.doc.documentElement
Expand Down Expand Up @@ -261,11 +243,29 @@ export class ProsemirrorBinding {
setTimeout(() => {
this._domSelectionInView = null
}, 0)
this._domSelectionInView = isDomSelectionInView()
this._domSelectionInView = this._isDomSelectionInView()
}
return this._domSelectionInView
}

_isDomSelectionInView () {
const selection = this.prosemirrorView._root.getSelection()
const anchorElement = getElementFromTextNode(selection.anchorNode)
if (selection && isInViewport(anchorElement)) {
const focusElement = getElementFromTextNode(selection.focusNode)
if (focusElement === anchorElement ||
focusElement === selection.anchorNode ||
selection.focusNode === focusElement ||
selection.focusNode === selection.anchorNode ||
isInViewport(focusElement)
) {
return true
}
}

return false
}

renderSnapshot (snapshot, prevSnapshot) {
if (!prevSnapshot) {
prevSnapshot = Y.createSnapshot(Y.createDeleteSet(), new Map())
Expand Down

0 comments on commit 42993e7

Please sign in to comment.