getCaretOffsets (element, range) {
    let preCaretRange
    let postCaretRange

    if (!range) {
      range = window.getSelection().getRangeAt(0)
    }

    preCaretRange = range.cloneRange()
    postCaretRange = range.cloneRange()

    preCaretRange.selectNodeContents(element)
    preCaretRange.setEnd(range.endContainer, range.endOffset)

    postCaretRange.selectNodeContents(element)
    postCaretRange.setStart(range.endContainer, range.endOffset)

    return {
      left: preCaretRange.toString().length,
      right: postCaretRange.toString().length
    }
  }
Find the caret position within an element irrespective of any inline tags it may contain.

 @param {DOMElement} An element containing the cursor to find offsets relative to.
 @param {Range} A Range representing cursor position. Will window.getSelection if none is passed.
 @return {Object} 'left' and 'right' attributes contain offsets from beginning and end of Element