function getStartPositionInLine() { let selection = window.getSelection(); if (selection.rangeCount == 0) { return [null, 0]; } let range = selection.getRangeAt(0).cloneRange(); let obj = range.startContainer; let position = range.startOffset; if (obj.nodeName == 'DIV' && obj.classList.contains('mdnotes_line')) { return [obj, position]; } let line = null; while (obj != null) { if (obj.previousSibling != null) { obj = obj.previousSibling; if (obj.innerText != null) { position += obj.innerText.length; } else if (obj.length != null) { position += obj.length; } } else if (obj.parentNode != null && obj.parentNode.classList != null && !obj.parentNode.classList.contains('mdnotes_line')) { obj = obj.parentNode; } else if (obj.parentNode != null && obj.parentNode.classList != null && obj.parentNode.classList.contains('mdnotes_line')) { line = obj.parentNode; obj = null; } else { obj = null; } } return [line, position]; } function setStartPositionInLine(line, position) { setStartPositionInDiv(line, position) } function setStartPositionInDiv(obj, position) { let temppos = position; if (temppos > obj.innerText.length) { return temppos - obj.innerText.length; } let children = obj.childNodes; for (let i = 0; i < children.length; i++) { let child = children[i]; if (child.nodeType == 3) { temppos = setStartPositionInText(child, temppos); } else { temppos = setStartPositionInDiv(child, temppos); } if (temppos == 0) { return temppos; } } return temppos; } function setStartPositionInText(obj, position) { if (position > obj.length) { return position - obj.length; } let selection = window.getSelection(); selection.removeAllRanges(); let range = document.createRange(); range.setStart(obj, position); selection.addRange(range); return 0; } export { getStartPositionInLine, setStartPositionInLine };