Simplify the selection extents function

This commit is contained in:
Kovid Goyal 2020-08-08 22:11:13 +05:30
parent 72fcbbc93a
commit 1e24b66dad
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 18 deletions

View File

@ -534,7 +534,7 @@ class IframeBoss:
self.send_message(
'selectionchange', text=text, empty=v'!!collapsed', annot_id=annot_id,
drag_mouse_position=drag_mouse_position, selection_change_caused_by_search=by_search,
selection_extents=selection_extents(current_layout_mode() is 'flow', True))
selection_extents=selection_extents(current_layout_mode() is 'flow'))
def onresize_stage2(self):
if scroll_viewport.width() is self.last_window_width and scroll_viewport.height() is self.last_window_height:
@ -556,7 +556,7 @@ class IframeBoss:
if sel and not sel.isCollapsed:
# update_selection_position has probably already been called by
# no_latency_onscroll but make sure
self.send_message('update_selection_position', selection_extents=selection_extents(current_layout_mode() is 'flow', True))
self.send_message('update_selection_position', selection_extents=selection_extents(current_layout_mode() is 'flow'))
def received_window_size(self, data):
scroll_viewport.update_window_size(data.width, data.height)

View File

@ -51,12 +51,12 @@ def empty_range_extents():
}
def range_extents(start, end, in_flow_mode):
def range_extents(q, in_flow_mode):
ans = empty_range_extents()
if not start or not end:
if not q:
return ans
start = start.cloneRange()
end = end.cloneRange()
start = q.cloneRange()
end = q.cloneRange()
start.collapse(True)
end.collapse(False)
@ -94,21 +94,11 @@ def range_extents(start, end, in_flow_mode):
def selection_extents(in_flow_mode, end_must_be_focus):
def selection_extents(in_flow_mode):
sel = window.getSelection()
if not sel or not sel.rangeCount or sel.isCollapsed:
return empty_range_extents()
if end_must_be_focus:
start = document.createRange()
start.setStart(sel.anchorNode, sel.anchorOffset)
start.setEnd(sel.anchorNode, sel.anchorOffset)
end = document.createRange()
end.setStart(sel.focusNode, sel.focusOffset)
end.setEnd(sel.focusNode, sel.focusOffset)
else:
start = sel.getRangeAt(0)
end = sel.getRangeAt(sel.rangeCount - 1)
return range_extents(start, end, in_flow_mode)
return range_extents(sel.getRangeAt(0), in_flow_mode)
def range_at_limit(invert_x, invert_y):