Debian Codegraph @

src/pyj/read_book/toc.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from complete import create_search_bar
9 from dom import ensure_id, set_css, svgicon
10 from modals import error_dialog
11 from read_book.anchor_visibility import position_for_anchor
12 from read_book.globals import (
13 current_book, current_layout_mode, current_spine_item, get_boss, set_toc_anchor_map,
14 toc_anchor_map
15 )
16 from read_book.viewport import scroll_viewport
17 from widgets import create_tree, find_text_in_tree, scroll_tree_item_into_view
18
19
20 def update_visible_toc_nodes(visible_anchors):
21 update_visible_toc_nodes.data = visible_anchors
22 update_visible_toc_nodes.data = {}
23
24
25 def iter_toc_descendants(node, callback):
26 for child in node.children:
27 if callback(child):
28 return
29 iter_toc_descendants(child, callback)
30
31
32 def get_toc_nodes_bordering_spine_item(toc, csi):
33 # Find the ToC entries that point to the closest files on either side of the
34 # spine item
35 toc = toc or current_book().manifest.toc
36 csi = csi or current_spine_item()
37 spine = current_book().manifest.spine
38 spine_before, spine_after = {}, {}
39 which = spine_before
40 before = after = prev = None
41 for name in spine:
42 if name is csi:
43 which = spine_after
44 else:
45 which[name] = True
46 iter_toc_descendants(toc, def (node):
47 nonlocal prev, before, after
48 if node.dest:
49 if spine_before[node.dest]:
50 prev = node
51 elif spine_after[node.dest]:
52 if not before:
53 before = prev
54 if not after:
55 after = node
56 return True
57 )
58 if not before and prev is not None:
59 before = prev
60 return before, after
61
62
63 def get_border_nodes(toc, id_map):
64 data = update_visible_toc_nodes.data
65 before, after = data.before, data.after
66 if before:
67 before = id_map[before]
68 if after:
69 after = id_map[after]
70 if before and after:
71 # Both border nodes are in the current spine item
72 return before, after
73 sb, sa = get_toc_nodes_bordering_spine_item(toc)
74 before = before or sb
75 after = after or sa
76 return before, after
77
78
79 def family_for_toc_node(toc_node_id, parent_map, id_map):
80 if not id_map or not parent_map:
81 toc = current_book().manifest.toc
82 parent_map, id_map = get_toc_maps(toc)
83 family = []
84 node = id_map[toc_node_id]
85 while node and node.title:
86 family.unshift(node)
87 parent = parent_map[node.id]
88 node = None
89 if parent:
90 node = id_map[parent.id]
91 return family
92
93
94 def get_page_list_id_map(page_list):
95 return {x.id: x for x in page_list}
96
97
98 def get_page_list_before(page_list, id_map):
99 # Find the page list entries that point to the closest files on either side of the spine item
100 csi = csi or current_spine_item()
101 spine = current_book().manifest.spine
102 spine_before, spine_after = {}, {}
103 which = spine_before
104 before = prev = None
105 for name in spine:
106 if name is csi:
107 which = spine_after
108 else:
109 which[name] = True
110 for item in page_list:
111 if item.dest:
112 if spine_before[item.dest]:
113 prev = item
114 elif spine_after[item.dest]:
115 if not before:
116 before = prev
117 break
118 return before
119
120
121 def get_current_pagelist_items():
122 ans = {}
123 page_list = current_book().manifest.page_list
124 data = update_visible_toc_nodes.data?.page_list
125 if not data or not page_list:
126 return ans
127 id_map = get_page_list_id_map(page_list)
128 if data.has_visible:
129 ans = data.visible_anchors
130 else:
131 if data.before?:
132 ans[data.before] = True
133 else:
134 before = get_page_list_before(page_list, id_map)
135 if before?:
136 ans[before.id] = True
137 return [id_map[x] for x in Object.keys(ans)]
138
139
140 def get_current_toc_nodes():
141 toc = current_book().manifest.toc
142 parent_map, id_map = get_toc_maps(toc)
143 data = update_visible_toc_nodes.data
144 ans = {}
145 if data.has_visible:
146 ans = data.visible_anchors
147 else:
148 if data.before:
149 ans[data.before] = True
150 else:
151 before = get_border_nodes(toc, id_map)[0]
152 if before:
153 ans[before.id] = True
154 r = []
155 for x in Object.keys(ans):
156 fam = family_for_toc_node(x, parent_map, id_map)
157 if fam?.length:
158 r.push(fam)
159 return r
160
161
162 def get_highlighted_toc_nodes(toc, parent_map, id_map, skip_parents):
163 data = update_visible_toc_nodes.data
164 ans = {}
165 if data.has_visible:
166 ans = data.visible_anchors
167 else:
168 if data.before:
169 ans[data.before] = True
170 else:
171 before = get_border_nodes(toc, id_map)[0]
172 if before:
173 ans[before.id] = True
174 if not skip_parents:
175 for node_id in Object.keys(ans):
176 p = parent_map[node_id]
177 while p and p.title:
178 ans[p.id] = True
179 p = parent_map[p.id]
180 return ans
181
182
183 def get_toc_maps(toc):
184 if not toc:
185 toc = current_book().manifest.toc
186 parent_map, id_map = {}, {}
187
188 def process_node(node, parent):
189 id_map[node.id] = node
190 parent_map[node.id] = parent
191 for c in node.children:
192 process_node(c, node)
193
194 process_node(toc)
195 return parent_map, id_map
196
197
198 def get_book_mark_title():
199 toc = current_book().manifest.toc
200 parent_map, id_map = get_toc_maps(toc)
201 highlighted_toc_nodes = get_highlighted_toc_nodes(toc, parent_map, id_map, True)
202 for node_id in Object.keys(highlighted_toc_nodes):
203 node = id_map[node_id]
204 if node.title:
205 return node.title
206 return ''
207
208
209 def create_toc_tree(toc, onclick):
210 parent_map, id_map = get_toc_maps(toc)
211 highlighted_toc_nodes = get_highlighted_toc_nodes(toc, parent_map, id_map)
212
213 def populate_data(node, li, a):
214 li.dataset.tocDest = node.dest or ''
215 li.dataset.tocFrag = node.frag or ''
216 title = node.title or ''
217 if highlighted_toc_nodes[node.id]:
218 a.appendChild(E.b(E.i(title)))
219 else:
220 a.textContent = title
221
222 return create_tree(toc, populate_data, onclick)
223
224
225 def do_search(text):
226 container = document.getElementById(this)
227 a = find_text_in_tree(container, text)
228 if not text:
229 return
230 if not a:
231 return error_dialog(_('No matches found'), _(
232 'The text "{}" was not found in the Table of Contents').format(text))
233 scroll_tree_item_into_view(a)
234
235
236 def create_toc_panel(book, container, onclick):
237 def handle_click(event, li):
238 if event.button is 0:
239 onclick(li.dataset.tocDest, li.dataset.tocFrag)
240 toc_panel = create_toc_tree(book.manifest.toc, handle_click)
241 toc_panel_id = ensure_id(toc_panel)
242 set_css(container, display='flex', flex_direction='column', height='100%', min_height='100%', overflow='hidden', max_height='100vh', max_width='100vw')
243 set_css(toc_panel, flex_grow='10')
244 container.appendChild(toc_panel)
245 search_button = E.a(class_='calibre-icon-button', svgicon('search'), href='javascript:void(0)', title=_('Search'),
246 onmousedown=def (e): e.preventDefault();)
247 t = _('Search Table of Contents')
248 search_bar = create_search_bar(do_search.bind(toc_panel_id), 'search-book-toc', button=search_button, placeholder=t)
249 set_css(search_bar, flex_grow='10', margin_right='1em')
250 container.appendChild(E.div(style='margin: 1ex 1em; display: flex; align-items: center', search_bar, search_button))
251 for child in container.childNodes:
252 child.style.flexShrink = '0'
253 toc_panel.style.flexGrow = '100'
254 toc_panel.style.flexShrink = '1'
255 toc_panel.style.overflow = 'auto'
256
257
258 def recalculate_toc_anchor_positions(tam, plam):
259 name = current_spine_item().name
260 am = {}
261 anchors = []
262 pos_map = {}
263 for i, anchor in enumerate(tam[name] or []):
264 am[anchor.id] = position_for_anchor(anchor.frag)
265 anchors.push(anchor.id)
266 pos_map[anchor.id] = i
267 anchor_funcs = get_boss().anchor_funcs
268 # stable sort by position in document
269 anchors.sort(def (a, b): return anchor_funcs.cmp(am[a], am[b]) or (pos_map[a] - pos_map[b]);)
270
271 current_map = {
272 'layout_mode': current_layout_mode(),
273 'width': scroll_viewport.width(),
274 'height': scroll_viewport.height(),
275 'pos_map': am,
276 'sorted_anchors': anchors,
277 }
278 am = {}
279 anchors = []
280 pos_map = {}
281 if plam:
282 for i, anchor in enumerate(plam[name] or []):
283 am[anchor.id] = position_for_anchor(anchor.frag, True)
284 anchors.push(anchor.id)
285 pos_map[anchor.id] = i
286 # stable sort by position in document
287 anchors.sort(def (a, b): return anchor_funcs.cmp(am[a], am[b]) or (pos_map[a] - pos_map[b]);)
288 current_map.page_list_pos_map = am
289 current_map.page_list_sorted_anchors = anchors
290
291 set_toc_anchor_map(current_map)
292 return current_map
293
294
295 def current_toc_anchor_map(tam, plam):
296 current_map = toc_anchor_map()
297 if not (current_map and current_map.layout_mode is current_layout_mode()
298 and current_map.width is scroll_viewport.width() and current_map.height is scroll_viewport.height()):
299 current_map = recalculate_toc_anchor_positions(tam, plam)
300 return current_map
301
302
303 def update_visible_toc_anchors(toc_anchor_map, page_list_anchor_map, recalculate):
304 if recalculate:
305 recalculate_toc_anchor_positions(toc_anchor_map, page_list_anchor_map)
306 anchor_funcs = get_boss().anchor_funcs
307
308 tam = current_toc_anchor_map(toc_anchor_map, page_list_anchor_map)
309 before = after = None
310 visible_anchors = {}
311 has_visible = False
312
313 for anchor_id in tam.sorted_anchors:
314 pos = tam.pos_map[anchor_id]
315 visibility = anchor_funcs.visibility(pos)
316 if visibility < 0:
317 before = anchor_id
318 elif visibility is 0:
319 has_visible = True
320 visible_anchors[anchor_id] = True
321 elif visibility > 0:
322 after = anchor_id
323 break
324 ans = {'visible_anchors': visible_anchors, 'has_visible': has_visible, 'before': before, 'after': after, 'sorted_anchors': tam.sorted_anchors}
325
326 before = after = None
327 visible_anchors = {}
328 has_visible = False
329 for anchor_id in tam.page_list_sorted_anchors:
330 pos = tam.page_list_pos_map[anchor_id]
331 visibility = anchor_funcs.visibility(pos)
332 if visibility < 0:
333 before = anchor_id
334 elif visibility is 0:
335 has_visible = True
336 visible_anchors[anchor_id] = True
337 elif visibility > 0:
338 after = anchor_id
339 break
340 ans.page_list = {
341 'visible_anchors': visible_anchors,
342 'has_visible': has_visible,
343 'before': before,
344 'after': after,
345 'sorted_anchors': tam.page_list_sorted_anchors,
346 }
347 return ans
348
349
350 def find_anchor_before_range(r, toc_anchor_map, page_list_anchor_map):
351 name = current_spine_item().name
352 prev_anchor = None
353 tam = current_toc_anchor_map(toc_anchor_map, page_list_anchor_map)
354 anchors = toc_anchor_map[name]
355 if anchors:
356 amap = {x.id: x for x in anchors}
357 for anchor_id in tam.sorted_anchors:
358 anchor = amap[anchor_id]
359 is_before = True
360 if anchor.frag:
361 elem = document.getElementById(anchor.frag)
362 if elem:
363 q = document.createRange()
364 q.selectNode(elem)
365 if q.compareBoundaryPoints(window.Range.START_TO_START, r) > 0:
366 is_before = False
367 if is_before:
368 prev_anchor = anchor
369 else:
370 break
371 return prev_anchor