Fix measuring caret position
This commit is contained in:
parent
ad66af68e7
commit
4e5c61e31f
@ -105,8 +105,8 @@ export default function getRichElementValue(node: HTMLElement, lines: string[],
|
||||
if(isBlock && line.length || node.tagName === 'BR') {
|
||||
lines.push(line.join(''));
|
||||
line.splice(0, line.length);
|
||||
} else if(node.tagName === 'IMG') {
|
||||
const alt = (node as HTMLImageElement).alt;
|
||||
} else if(node instanceof HTMLImageElement) {
|
||||
const alt = node.alt;
|
||||
if(alt) {
|
||||
line.push(alt);
|
||||
offset.offset += alt.length;
|
||||
|
@ -28,11 +28,21 @@ export default function getRichValueWithCaret(field: HTMLElement, withEntities =
|
||||
range.startContainer == range.endContainer &&
|
||||
startOffset == range.endOffset
|
||||
) {
|
||||
// * if focused on img
|
||||
// * if focused on img, or caret has been set via placeCaretAtEnd
|
||||
const possibleChildrenFocusOffset = startOffset - 1;
|
||||
if(range.startContainer === field && field.childNodes[possibleChildrenFocusOffset]) {
|
||||
selNode = field.childNodes[possibleChildrenFocusOffset];
|
||||
selOffset = 1;
|
||||
const childNodes = field.childNodes;
|
||||
if(range.startContainer === field && childNodes[possibleChildrenFocusOffset]) {
|
||||
selNode = childNodes[possibleChildrenFocusOffset];
|
||||
selOffset = 0;
|
||||
|
||||
for(let i = 0; i < range.endOffset; ++i) {
|
||||
const node = childNodes[i];
|
||||
const value = node.nodeValue || (node as HTMLImageElement).alt;
|
||||
|
||||
if(value) {
|
||||
selOffset += value.length;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
selNode = range.startContainer;
|
||||
selOffset = startOffset;
|
||||
|
Loading…
Reference in New Issue
Block a user