findGlobalOffset = (node, position, root) => {
  const walker = (root.contentDocument ?? root.ownerDocument).createTreeWalker(root, NodeFilter.SHOW_ALL);

  let globalPosition = 0;
  let nodeReached = false;
  let currentNode = walker.nextNode();

  while (currentNode) {
    // Indicates that we at or below desired node
    nodeReached = nodeReached || node === currentNode;
    const atTargetNode = node === currentNode || currentNode.contains(node);
    const isText = currentNode.nodeType === Node.TEXT_NODE;
    const isBR = currentNode.nodeName === "BR";

    // Stop iteration
    // Break if we passed target node and current node
    // is not target, nor child of a target
    if (nodeReached && atTargetNode === false) {
      break;
    }

    if (isText || isBR) {
      let length = isDefined(currentNode.length) ? [...currentNode.textContent].length : 1;

      if (atTargetNode) {
        length = Math.min(position, length);
      }

      globalPosition += length;
    }

    currentNode = walker.nextNode();
  }

  return globalPosition;
}
Find text offset for given node and position relative to a root
@param {Node} node
@param {Number} position
@param {Node} root