Skip to content

Commit

Permalink
Merge pull request #486 from zTree/master
Browse files Browse the repository at this point in the history
getCursorPosition 针对空行会得到错误数据
  • Loading branch information
Vanessa219 authored Jun 13, 2020
2 parents 3b17581 + c0d8f3a commit 68179b8
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/ts/util/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,40 @@ export const getCursorPosition = (editor: HTMLElement) => {
let cursorRect;
if (range.getClientRects().length === 0) {
if (range.startContainer.nodeType === 3) {
return {
left: 0,
top: 0,
};
}
const children = (range.startContainer as Element).children;
if (children[range.startOffset] &&
children[range.startOffset].getClientRects().length > 0) {
// markdown 模式回车
cursorRect = children[range.startOffset].getClientRects()[0];
} else if (range.startContainer.childNodes.length > 0) {
// in table or code block
range.selectNode(range.startContainer.childNodes[Math.max(0, range.startOffset - 1)]);
cursorRect = range.getClientRects()[0];
range.collapse(false);
// 空行时,会出现没有 br 的情况,需要根据父元素 <p> 获取位置信息
let parent = range.startContainer.parentElement;
if (parent && parent.getClientRects().length > 0) {
cursorRect = parent.getClientRects()[0];
} else {
return {
left: 0,
top: 0,
};
}
} else {
cursorRect = (range.startContainer as HTMLElement).getClientRects()[0];
}
if (!cursorRect) {
let parentElement = range.startContainer.childNodes[range.startOffset] as HTMLElement;
while (!parentElement.getClientRects ||
(parentElement.getClientRects && parentElement.getClientRects().length === 0)) {
parentElement = parentElement.parentElement;
const children = (range.startContainer as Element).children;
if (children[range.startOffset] &&
children[range.startOffset].getClientRects().length > 0) {
// markdown 模式回车
cursorRect = children[range.startOffset].getClientRects()[0];
} else if (range.startContainer.childNodes.length > 0) {
// in table or code block
range.selectNode(range.startContainer.childNodes[Math.max(0, range.startOffset - 1)]);
cursorRect = range.getClientRects()[0];
range.collapse(false);
} else {
cursorRect = (range.startContainer as HTMLElement).getClientRects()[0];
}
if (!cursorRect) {
let parentElement = range.startContainer.childNodes[range.startOffset] as HTMLElement;
while (!parentElement.getClientRects ||
(parentElement.getClientRects && parentElement.getClientRects().length === 0)) {
parentElement = parentElement.parentElement;
}
cursorRect = parentElement.getClientRects()[0];
}
cursorRect = parentElement.getClientRects()[0];
}

} else {
cursorRect = range.getClientRects()[0];
}
Expand Down

0 comments on commit 68179b8

Please sign in to comment.