Skip to content

Commit

Permalink
fixed sanitization - cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
novikov82 committed Feb 15, 2024
1 parent 154f5b7 commit 71b571f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,21 @@ export function sanitizeEditableContent(element: any, cleanLineBreaks: boolean =

element.innerText = innerText;
range = document.createRange();
range.setStart(element.childNodes[0], innerText.length - tail_len);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);

const newPosition = innerText.length - tail_len;
let sumLen = 0;
for (let i = 0; i < element.childNodes.length; i++) {
const elementLen = element.childNodes[i].textContent.length;
if (elementLen == 0) continue;
if (sumLen + elementLen >= newPosition) {
range.setStart(element.childNodes[i], newPosition - sumLen);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
return;
}
sumLen += elementLen;
}
}
}
function mergeValues(src: any, dest: any) {
Expand Down

0 comments on commit 71b571f

Please sign in to comment.