all files / util/ windowUtils.js

10% Statements 5/50
0% Branches 0/28
0% Functions 0/5
10.42% Lines 5/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95                                                                                                                                                                                   
import isNil from './isNil'
import platform from './platform'
 
export function getDOMRangeFromEvent(evt) {
  let range, x = evt.clientX, y = evt.clientY
  // Try the simple IE way first
  if (document.body.createTextRange) {
    range = document.body.createTextRange()
    range.moveToPoint(x, y)
  }
  else if (!isNil(document.createRange)) {
    // Try Mozilla's rangeOffset and rangeParent properties,
    // which are exactly what we want
    if (!isNil(evt.rangeParent)) {
      range = document.createRange()
      range.setStart(evt.rangeParent, evt.rangeOffset)
      range.collapse(true)
    }
    // Try the standards-based way next
    else if (document.caretPositionFromPoint) {
      let pos = document.caretPositionFromPoint(x, y)
      range = document.createRange()
      range.setStart(pos.offsetNode, pos.offset)
      range.collapse(true)
    }
    // Next, the WebKit way
    else if (document.caretRangeFromPoint) {
      range = document.caretRangeFromPoint(x, y)
    }
  }
  return range
}
 
/*
  Get selection rectangle relative to panel content element
*/
export function getSelectionRect(parentRect) {
  if (platform.inBrowser) {
    const wsel = window.getSelection()
    if (wsel.rangeCount === 0) return
    const wrange = wsel.getRangeAt(0)
    let contentRect = parentRect
    let selectionRect = wrange.getBoundingClientRect()
    if (selectionRect.top === 0 && selectionRect.bottom === 0) {
      selectionRect = _fixForCursorRectBug()
    }
    return getRelativeRect(contentRect, selectionRect)
  }
}
 
function _fixForCursorRectBug() {
  let wsel = window.getSelection()
  let el = wsel.anchorNode
  if (!el) return
  while (el && el.nodeType !== 1) {
    el = el.parentNode
  }
  let rects = el.getClientRects()
  let rect = rects[0]
  return {
    left: rect.left,
    top: rect.top,
    width: 0,
    height: rect.height,
    right: rect.width,
    bottom: rect.bottom
  }
}
 
export function getRelativeRect(parentRect, childRect) {
  var left = childRect.left - parentRect.left
  var top = childRect.top - parentRect.top
  return {
    left: left,
    top: top,
    right: parentRect.width - left - childRect.width,
    bottom: parentRect.height - top - childRect.height,
    width: childRect.width,
    height: childRect.height
  }
}
 
export function isMouseInsideDOMSelection(e) {
  let wsel = window.getSelection()
  if (wsel.rangeCount === 0) {
    return false
  }
  let wrange = wsel.getRangeAt(0)
  let selectionRect = wrange.getBoundingClientRect()
  return e.clientX >= selectionRect.left &&
         e.clientX <= selectionRect.right &&
         e.clientY >= selectionRect.top &&
         e.clientY <= selectionRect.bottom
}