Skip to content

Commit

Permalink
Use of the dom lib0
Browse files Browse the repository at this point in the history
  • Loading branch information
boschDev authored May 19, 2020
1 parent d504146 commit 5ce6af2
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Y from 'yjs'
import { absolutePositionToRelativePosition, relativePositionToAbsolutePosition } from '../lib.js'
import * as random from 'lib0/random.js'
import * as environment from 'lib0/environment.js'
import * as dom from 'lib0/dom.js'

/**
* @param {Y.Item} item
Expand Down Expand Up @@ -179,18 +180,18 @@ export const getRelativeSelection = (pmbinding, state) => ({
head: absolutePositionToRelativePosition(state.selection.head, pmbinding.type, pmbinding.mapping)
})

const getElementFromNode = node => {
if (node instanceof Element) {
const getElementFromTextNode = node => {
if (dom.checkNodeType(node, dom.ELEMENT_NODE)) {
return node
}
return node.parentElement
}

const isDomSelectionInView = () => {
const selection = window.getSelection()
const anchorElement = getElementFromNode(selection.anchorNode)
const anchorElement = getElementFromTextNode(selection.anchorNode)
if (selection && isInViewport(anchorElement)) {
const focusElement = getElementFromNode(selection.focusNode)
const focusElement = getElementFromTextNode(selection.focusNode)
if (focusElement === anchorElement
|| focusElement === selection.anchorNode
|| selection.focusNode === focusElement
Expand All @@ -206,7 +207,7 @@ const isDomSelectionInView = () => {

const isInViewport = element => {
const bounding = element.getBoundingClientRect()
const documentElement = document.documentElement
const documentElement = dom.doc.documentElement
return bounding.top >= 0 && bounding.left >= 0
&& bounding.bottom <= (window.innerHeight || documentElement.clientHeight)
&& bounding.right <= (window.innerWidth || documentElement.clientWidth)
Expand Down

0 comments on commit 5ce6af2

Please sign in to comment.