Allow message type edition

This commit is contained in:
2021-09-17 23:45:28 +02:00
parent 0968a8481a
commit 07200af13e
6 changed files with 241 additions and 112 deletions

52
jquery/common.js vendored
View File

@@ -753,4 +753,56 @@ function utf8_encode(argString)
}
return utftext;
}
function getCaretPosition(editableDiv) {
var caretPos = 0, sel, range;
if(window.getSelection) {
sel = window.getSelection();
if(sel.rangeCount) {
range = sel.getRangeAt(0);
if(range.commonAncestorContainer.parentNode == editableDiv) {
caretPos = range.endOffset;
}
}
}
else if(document.selection && document.selection.createRange) {
range = document.selection.createRange();
if (range.parentElement() == editableDiv) {
var tempEl = document.createElement("span");
editableDiv.insertBefore(tempEl, editableDiv.firstChild);
var tempRange = range.duplicate();
tempRange.moveToElementText(tempEl);
tempRange.setEndPoint("EndToEnd", range);
caretPos = tempRange.text.length;
}
}
return caretPos;
}
function setCaretPosition(el, pos) {
// Loop through all child nodes
for(var node of el.childNodes) {
if(node.nodeType == 3){ // we have a text node
if(node.length >= pos) {
// finally add our range
var range = document.createRange(), sel = window.getSelection();
range.setStart(node,pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
return -1; // we are done
}
else {
pos -= node.length;
}
}
else {
pos = setCaretPosition(node,pos);
if(pos == -1) {
return -1; // no need to finish the for loop
}
}
}
return pos; // needed because of recursion stuff
}