From f4b68062028a1fee57dc81a15d55e553ce0ee460 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 11 Dec 2020 07:34:17 +0530 Subject: [PATCH] TTS should start reading from the current page --- src/pyj/read_book/iframe.pyj | 9 +++++---- src/pyj/select.pyj | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index f1591b2ebb..8da7b0b64c 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -64,7 +64,7 @@ from read_book.touch import ( ) from read_book.viewport import scroll_viewport from select import ( - move_end_of_selection, selection_extents, word_at_point + move_end_of_selection, selection_extents, word_at_point, first_visible_word ) from utils import debounce, is_ios @@ -903,9 +903,10 @@ class IframeBoss: text_node, offset = None, 0 if data.pos: r = word_at_point(data.pos.x, data.pos.y) - if r: - if r.startContainer?.nodeType is Node.TEXT_NODE: - text_node, offset = r.startContainer, r.startOffset + else: + r = first_visible_word() + if r and r.startContainer?.nodeType is Node.TEXT_NODE: + text_node, offset = r.startContainer, r.startOffset marked_text = tts_data(text_node, offset) sel = window.getSelection() sel.removeAllRanges() diff --git a/src/pyj/select.pyj b/src/pyj/select.pyj index 35102bf199..0a8bc1b418 100644 --- a/src/pyj/select.pyj +++ b/src/pyj/select.pyj @@ -42,6 +42,18 @@ def word_at_point(x, y): return r +def first_visible_word(): + width = window.innerWidth + height = window.innerHeight + xdelta = width // 10 + ydelta = height // 10 + for y in range(0, height, ydelta): + for x in range(0, width, xdelta): + r = word_at_point(x, y) + if r?: + return r + + def empty_range_extents(): return { 'start': {'x': 0, 'y': 0, 'height': 0, 'width': 0, 'onscreen': False, 'selected_prev': False},