You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.0 KiB
74 lines
2.0 KiB
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('cheezenotes_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('cheezenotes_line')) {
|
|
obj = obj.parentNode;
|
|
} else if (obj.parentNode != null && obj.parentNode.classList != null && obj.parentNode.classList.contains('cheezenotes_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 };
|