diff --git a/src/pyj/read_book/search.pyj b/src/pyj/read_book/search.pyj index 783efc7f3a..519a028356 100644 --- a/src/pyj/read_book/search.pyj +++ b/src/pyj/read_book/search.pyj @@ -28,6 +28,33 @@ add_extra_css(def(): ) +def get_toc_data(book): + spine = book.manifest.spine + spine_toc_map = {name: v'[]' for name in spine} + parent_map = {} + toc_id_map = {} + + def process_node(node): + toc_id_map[node.id] = node + items = spine_toc_map[node.dest] + if items: + items.push(node) + children = node.children + if children: + for child in children: + parent_map[child.id] = node + process_node(child) + + toc = book.manifest.toc + if toc: + process_node(toc) + return { + 'spine': spine, 'spine_toc_map': spine_toc_map, + 'spine_idx_map': {name: idx for idx, name in enumerate(spine)}, + 'parent_map': parent_map, 'toc_id_map': toc_id_map + } + + class SearchOverlay: def __init__(self, view): @@ -90,7 +117,11 @@ class SearchOverlay: def clear_caches(self, book): if self._worker: book = book or current_book() - data = {'book_hash': book.book_hash, 'stored_files': book.stored_files, 'spine': book.manifest.spine} + self.toc_data = get_toc_data(book) + data = { + 'book_hash': book.book_hash, 'stored_files': book.stored_files, 'spine': book.manifest.spine, + 'toc_data': self.toc_data + } self.worker.postMessage({'type': 'clear_caches', 'book': data}) def onkeydown(self, event):