Better calculation of selection extents

This commit is contained in:
Kovid Goyal 2020-07-24 16:20:52 +05:30
parent 0407959ce4
commit 8aad5bd72f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -63,13 +63,19 @@ def range_extents(start, end, in_flow_mode):
def for_boundary(r, ans): def for_boundary(r, ans):
rect = r.getBoundingClientRect() rect = r.getBoundingClientRect()
if rect.height is 0: if rect.height is 0:
# this tends to happen when moving the mouse downwards
# at the boundary between paragraphs
if r.startContainer?.nodeType is Node.ELEMENT_NODE: if r.startContainer?.nodeType is Node.ELEMENT_NODE:
node = r.startContainer node = r.startContainer
if r.startOffset and node.childNodes.length > r.startOffset: if r.startOffset and node.childNodes.length > r.startOffset:
node = node.childNodes[r.startOffset] node = node.childNodes[r.startOffset]
if node.getBoundingClientRect: # we cant use getBoundingClientRect as the node might be split
erect = node.getBoundingClientRect() # among multiple columns
rect = {'left': erect.left, 'top': erect.top, 'right': erect.right, 'bottom': erect.bottom, 'width': erect.width, 'height': 2} if node.getClientRects:
rects = node.getClientRects()
if rects.length:
erect = rects[0]
rect = {'left': erect.left, 'top': erect.top, 'right': erect.left + 2, 'bottom': erect.top + 2, 'width': 2, 'height': 2}
ans.x = Math.round(rect.left) ans.x = Math.round(rect.left)
ans.y = Math.round(rect.top) ans.y = Math.round(rect.top)
ans.height = rect.height ans.height = rect.height