diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 30c574ac84..c525133993 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -402,7 +402,7 @@ class IframeBoss: if cfi: self.jump_to_cfi('/' + cfi) self.update_cfi() - self.update_toc_position() + self.update_toc_position(True) def number_of_columns_changed(self, data): opts.columns_per_screen = data.columns_per_screen @@ -548,8 +548,8 @@ class IframeBoss: self.send_message( 'update_progress_frac', progress_frac=pf, file_progress_frac=fpf, page_counts=page_counts()) - def update_toc_position(self): - visible_anchors = update_visible_toc_anchors(self.book.manifest.toc_anchor_map, self.anchor_funcs) + def update_toc_position(self, recalculate_toc_anchor_positions): + visible_anchors = update_visible_toc_anchors(self.book.manifest.toc_anchor_map, self.anchor_funcs, bool(recalculate_toc_anchor_positions)) self.send_message('update_toc_position', visible_anchors=visible_anchors) def onscroll(self): diff --git a/src/pyj/read_book/toc.pyj b/src/pyj/read_book/toc.pyj index 4ea1b7aee0..1ab556e161 100644 --- a/src/pyj/read_book/toc.pyj +++ b/src/pyj/read_book/toc.pyj @@ -198,32 +198,38 @@ def create_toc_panel(book, container, onclick): toc_panel.style.flexShrink = '1' toc_panel.style.overflow = 'auto' +def recalculate_toc_anchor_positions(tam, anchor_funcs): + name = current_spine_item().name + am = {} + anchors = v'[]' + pos_map = {} + for i, anchor in enumerate(tam[name] or v'[]'): + val = anchor_funcs.pos_for_elem() + if anchor.frag: + elem = document.getElementById(anchor.frag) + if elem: + val = anchor_funcs.pos_for_elem(elem) + am[anchor.id] = val + anchors.push(anchor.id) + pos_map[anchor.id] = i + # stable sort by position in document + anchors.sort(def (a, b): return anchor_funcs.cmp(am[a], am[b]) or (pos_map[a] - pos_map[b]);) + + current_map = {'layout_mode': current_layout_mode(), 'width': scroll_viewport.width(), 'height': scroll_viewport.height(), 'pos_map': am, 'sorted_anchors':anchors} + set_toc_anchor_map(current_map) + return current_map + def current_toc_anchor_map(tam, anchor_funcs): current_map = toc_anchor_map() if not (current_map and current_map.layout_mode is current_layout_mode() and current_map.width is scroll_viewport.width() and current_map.height is scroll_viewport.height()): - name = current_spine_item().name - am = {} - anchors = v'[]' - pos_map = {} - for i, anchor in enumerate(tam[name] or v'[]'): - val = anchor_funcs.pos_for_elem() - if anchor.frag: - elem = document.getElementById(anchor.frag) - if elem: - val = anchor_funcs.pos_for_elem(elem) - am[anchor.id] = val - anchors.push(anchor.id) - pos_map[anchor.id] = i - # stable sort by position in document - anchors.sort(def (a, b): return anchor_funcs.cmp(am[a], am[b]) or (pos_map[a] - pos_map[b]);) - - current_map = {'layout_mode': current_layout_mode(), 'width': scroll_viewport.width(), 'height': scroll_viewport.height(), 'pos_map': am, 'sorted_anchors':anchors} - set_toc_anchor_map(current_map) + current_map = recalculate_toc_anchor_positions(tam, anchor_funcs) return current_map -def update_visible_toc_anchors(toc_anchor_map, anchor_funcs): +def update_visible_toc_anchors(toc_anchor_map, anchor_funcs, recalculate): + if recalculate: + recalculate_toc_anchor_positions(toc_anchor_map, anchor_funcs) tam = current_toc_anchor_map(toc_anchor_map, anchor_funcs) before = after = None visible_anchors = {}