Debian Codegraph @

src/pyj/read_book/bookmarks.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from book_list.item_list import build_list, create_item, create_side_action
9 from dom import ensure_id, set_css
10 from modals import question_dialog
11 from read_book.toc import get_book_mark_title
12 from widgets import create_button
13
14
15 def goto_cfi(cfi, view):
16 view.goto_cfi(cfi, True)
17
18
19 def remove_bookmark(annotations_manager, title, list_dom_node):
20 question_dialog(_('Are you sure?'), _('Do you want to permanently delete this bookmark?'), def (yes):
21 if yes:
22 annotations_manager.remove_bookmark(title)
23 list_dom_node.style.display = 'none'
24 )
25
26
27 def edit_bookmark(annotations_manager, title, list_dom_node):
28 new_title = window.prompt(_('Enter new title for bookmark:'), title)
29 if new_title:
30 if annotations_manager.edit_bookmark(title, new_title):
31 console.log(list_dom_node)
32 list_dom_node.querySelector('.item-title').textContent = new_title
33
34
35 def create_bookmarks_list(annotations_manager, onclick):
36 bookmarks = sorted(annotations_manager.all_bookmarks(), key=def (x): return x.title.toLowerCase();)
37 items = []
38 for bookmark in bookmarks:
39 if not bookmark.removed:
40 sa = create_side_action('trash', remove_bookmark.bind(None, annotations_manager, bookmark.title), _('Remove this bookmark'))
41 ea = create_side_action('edit', edit_bookmark.bind(None, annotations_manager, bookmark.title), _('Edit this bookmark'))
42 items.push(create_item(
43 bookmark.title, data=bookmark.pos,
44 action=onclick.bind(None, goto_cfi.bind(None, bookmark.pos)),
45 side_actions=[sa, ea]
46 ))
47 c = E.div(style='margin-top: 1ex')
48 build_list(c, items)
49 return c
50
51
52 def create_new_bookmark(annotations_manager, data):
53 base_default_title = get_book_mark_title() or _('Bookmark')
54 title = window.prompt(_('Enter title for bookmark:'), data.selected_text or annotations_manager.default_bookmark_title(base_default_title))
55 if not title:
56 return False
57 cfi = data.cfi
58 if data.selection_bounds?.start:
59 cfi = data.selection_bounds.start
60 annotations_manager.add_bookmark(title, cfi)
61 return True
62
63
64 def new_bookmark(container_id, annotations_manager, data, onclick, ev):
65 if create_new_bookmark(annotations_manager, data):
66 onclick(def (): pass;)
67
68
69 def create_bookmarks_panel(annotations_manager, data, book, container, onclick):
70 set_css(container, display='flex', flex_direction='column')
71 container.appendChild(E.div(style='padding: 1rem'))
72 container = container.lastChild
73 container_id = ensure_id(container)
74 button = create_button(_('New bookmark'), 'plus', new_bookmark.bind(None, container_id, annotations_manager, data, onclick))
75 container.appendChild(E.div(button))
76 container.appendChild(E.div(create_bookmarks_list(annotations_manager, onclick)))