mirror of
https://github.com/owncast/owncast.git
synced 2024-11-22 12:49:37 +03:00
Fix and refactor getCaretPosition
(#1782)
This commit is contained in:
parent
c960f6488f
commit
6e05c5cd7d
1 changed files with 12 additions and 7 deletions
|
@ -6,16 +6,21 @@ import {
|
|||
|
||||
// Taken from https://stackoverflow.com/a/46902361
|
||||
export function getCaretPosition(node) {
|
||||
var range = window.getSelection().getRangeAt(0),
|
||||
preCaretRange = range.cloneRange(),
|
||||
caretPosition,
|
||||
tmp = document.createElement('div');
|
||||
const selection = window.getSelection();
|
||||
|
||||
if (selection.rangeCount === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const range = selection.getRangeAt(0);
|
||||
const preCaretRange = range.cloneRange();
|
||||
const tempElement = document.createElement('div');
|
||||
|
||||
preCaretRange.selectNodeContents(node);
|
||||
preCaretRange.setEnd(range.endContainer, range.endOffset);
|
||||
tmp.appendChild(preCaretRange.cloneContents());
|
||||
caretPosition = tmp.innerHTML.length;
|
||||
return caretPosition;
|
||||
tempElement.appendChild(preCaretRange.cloneContents());
|
||||
|
||||
return tempElement.innerHTML.length;
|
||||
}
|
||||
|
||||
// Might not need this anymore
|
||||
|
|
Loading…
Reference in a new issue