Debian Codegraph @

src/pyj/book_list/show_note.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2023, 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 ajax import ajax
9 from book_list.comments_editor import (
10 create_comments_editor, destroy_editor, focus_comments_editor, get_comments_html,
11 set_comments_html
12 )
13 from book_list.details_list import sandbox_css
14 from book_list.globals import book_whose_metadata_is_being_edited
15 from book_list.library_data import book_metadata
16 from book_list.router import back, home, show_note
17 from book_list.top_bar import add_button, create_top_bar
18 from book_list.ui import set_panel_handler
19 from modals import ajax_send_progress_dialog, error_dialog
20 from utils import parse_url_params, safe_set_inner_html, sandboxed_html
21
22
23 def message_from_iframe(ev):
24 c = document.getElementById(current_container_id)
25 if c and c.lastChild:
26 iframe = c.querySelector('iframe')
27 if iframe and ev.source is iframe.contentWindow:
28 if ev.data.key:
29 ev.data.from_iframe = True
30 close_action = get_close_action()[0]
31 onkeydown(current_container_id, close_action, ev.data)
32
33
34 def make_iframe(html):
35 html += '''
36 <script>
37 function onkeydown(ev) {
38 ev.preventDefault()
39 ev.stopPropagation()
40 msg = {
41 key: ev.key, altKey: ev.altKey, ctrlKey: ev.ctrlKey, code: ev.code, isComposing: ev.isComposing,
42 repeat: ev.repeat, shiftKey: ev.shiftKey, metaKey: ev.metaKey,
43 }
44 window.parent.postMessage(msg, '*')
45 }
46 document.onkeydown = onkeydown
47 ''' + '<' + '/script>'
48 iframe = sandboxed_html(html, sandbox_css() + '\n\nhtml { overflow: visible; margin: 0.5rem; }', 'allow-scripts')
49 iframe.tabIndex = -1 # prevent tab from focusing this iframe
50 iframe.style.width = '100%'
51 iframe.style.flexGrow = '10'
52 if not message_from_iframe.added:
53 message_from_iframe.added = True
54 window.addEventListener('message', message_from_iframe, False)
55 return iframe
56
57
58 current_note_markup = None
59 current_container_id = ''
60
61
62 def on_item_val_based_notes_fetched(load_type, xhr, ev):
63 nonlocal current_note_markup
64 container = document.getElementById(this)
65 if not container:
66 return
67 q = parse_url_params()
68 current_note_markup = None
69 if load_type is 'load':
70 data = JSON.parse(xhr.responseText)
71 q.html = data.html
72 q.item_id = str(data.item_id)
73 current_note_markup = q
74 show_note(q.field, q.item_id, q.item, replace=True, library_id=q.library_id or None, close_action=q.close_action, panel=q.panel)
75 return
76 old = container.querySelector('div.loading')
77 p = old.parentNode
78 p.removeChild(old)
79 container.appendChild(E.div())
80 safe_set_inner_html(container.lastChild, xhr.error_html)
81
82
83 def on_notes_fetched(load_type, xhr, ev):
84 nonlocal current_note_markup
85 q = parse_url_params()
86 if load_type is 'load':
87 html = xhr.responseText
88 q.html = xhr.responseText
89 current_note_markup = q
90 else:
91 current_note_markup = None
92 html = xhr.error_html
93 container = document.getElementById(this)
94 if not container:
95 return
96 old = container.querySelector('div.loading')
97 p = old.parentNode
98 p.removeChild(old)
99 if q.panel is 'show_note':
100 create_note_display(container)
101 elif q.panel is 'edit_note':
102 if current_note_markup:
103 create_editor(container.lastChild, current_note_markup.html)
104 else:
105 container.appendChild(E.div())
106 safe_set_inner_html(container.lastChild, html)
107
108
109 def edit_note():
110 q = parse_url_params()
111 show_note(q.field, q.item_id, q.item, library_id=q.library_id, panel='edit_note')
112
113
114 def onkeydown(container_id, close_action, ev):
115 if ev.key is 'Escape':
116 if not ev.from_iframe:
117 ev.preventDefault(), ev.stopPropagation()
118 close_action()
119 elif ev.key is 'e' or ev.key is 'E':
120 q = parse_url_params()
121 if q.panel is 'show_note':
122 if not ev.from_iframe:
123 ev.preventDefault(), ev.stopPropagation()
124 edit_note()
125
126
127 def setup_container(container, close_action):
128 container.style.height = '100vh'
129 container.style.display = 'flex'
130 container.style.flexDirection = 'column'
131 container.appendChild(E.div(tabindex='0', style='flex-grow: 10; display: flex; align-items: stretch'))
132 container.lastChild.addEventListener('keydown', onkeydown.bind(None, container.id, close_action), {'passive': False, 'capture': True})
133 container.lastChild.focus()
134
135
136 def get_close_action():
137 q = parse_url_params()
138 if q.panel is 'show_note':
139 close_action, close_icon = back, 'close'
140 q = parse_url_params()
141 ca = q.close_action
142 if ca is 'home':
143 close_action, close_icon = def (): home();, 'home'
144 else:
145 close_action, close_icon = back, 'close'
146 return close_action, close_icon
147
148
149 def create_note_display(container):
150 add_button(container, 'edit', action=edit_note, tooltip=_('Edit this note [E]'))
151 html = current_note_markup.html
152 container.lastChild.appendChild(make_iframe(html))
153
154
155 def reroute_with_item_id(container, q):
156 container.lastChild.appendChild(E.div(_('Loading') + '…', style='margin: 0.5rem', class_='loading'))
157 url = 'get-note-from-item-val/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item)
158 if q.library_id:
159 url += '/' + encodeURIComponent(q.library_id)
160 ajax(url, on_item_val_based_notes_fetched.bind(container.id), bypass_cache=False).send()
161 return
162
163
164 def init_display_note(container_id):
165 nonlocal current_container_id
166 current_container_id = container_id
167 container = document.getElementById(container_id)
168 close_action, close_icon = get_close_action()
169 q = parse_url_params()
170 create_top_bar(container, title=q.item, action=close_action, icon=close_icon)
171 setup_container(container, close_action)
172 if q.item_id is '0' or q.item_id is 0 or not q.item_id:
173 return reroute_with_item_id(container, q)
174 if (current_note_markup and current_note_markup.library_id is q.library_id
175 and current_note_markup.field is q.field and current_note_markup.item_id is q.item_id):
176 create_note_display(container)
177 else:
178 container.lastChild.appendChild(E.div(_('Loading') + '…', style='margin: 0.5rem', class_='loading'))
179 url = 'get-note/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item_id)
180 if q.library_id:
181 url += '/' + encodeURIComponent(q.library_id)
182 ajax(url, on_notes_fetched.bind(container_id), bypass_cache=False).send()
183
184
185 def save(container_id, book_id):
186 c = document.getElementById(container_id)
187 if not c:
188 return
189 q = parse_url_params()
190 url = 'set-note/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item_id)
191 if q.library_id:
192 url += '/' + encodeURIComponent(q.library_id)
193 close_action = get_close_action()[0]
194
195 def on_complete(end_type, xhr, evt):
196 nonlocal current_note_markup
197 if end_type is 'abort':
198 close_action()
199 return
200 if end_type is not 'load':
201 error_dialog(_('Failed to send notes to server'), _(
202 'Updating the notes for: {} failed.').format(q.item), xhr.error_html)
203 return
204 q.html = xhr.responseText
205 current_note_markup = q
206 destroy_editor(c)
207 if book_id:
208 mi = book_metadata(book_id)
209 if mi:
210 mi.items_with_notes = mi.items_with_notes or {}
211 mi.items_with_notes[q.field] = mi.items_with_notes[q.field] or {}
212 if q.html:
213 mi.items_with_notes[q.field][q.item] = int(q.item_id)
214 else:
215 v'delete mi.items_with_notes[q.field][q.item]'
216 close_action()
217
218 def on_got_html(html, extra_data):
219 ajax_send_progress_dialog(
220 url,
221 {'html': html, 'searchable_text': extra_data.text, 'images': extra_data.images},
222 on_complete,
223 _('Sending note data to calibre server...')
224 )
225
226 get_comments_html(c, on_got_html)
227
228
229 def init_edit(container_id):
230 nonlocal current_container_id
231 current_container_id = container_id
232 container = document.getElementById(container_id)
233 q = parse_url_params()
234 close_action, close_icon = get_close_action()
235 create_top_bar(container, title=_('Edit {}').format(q.item), action=close_action, icon=close_icon, tooltip=_('Discard any changes'))
236 setup_container(container, close_action)
237 if q.item_id is '0' or q.item_id is 0 or not q.item_id:
238 return reroute_with_item_id(container, q)
239 if (current_note_markup and current_note_markup.library_id is q.library_id
240 and current_note_markup.field is q.field and current_note_markup.item_id is q.item_id):
241 create_editor(container.lastChild, current_note_markup.html)
242 else:
243 container.lastChild.appendChild(E.div(_('Loading') + '…', style='margin: 0.5rem', class_='loading'))
244 url = 'get-note/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item_id)
245 if q.library_id:
246 url += '/' + encodeURIComponent(q.library_id)
247 ajax(url, on_notes_fetched.bind(container_id), bypass_cache=False).send()
248
249
250 def create_editor(container, html):
251 book_id = book_whose_metadata_is_being_edited()
252 book_whose_metadata_is_being_edited(None)
253 bar_container = document.getElementById(current_container_id)
254 if bar_container:
255 add_button(bar_container, 'check', action=save.bind(None, bar_container.id, book_id), tooltip=_('Save the changes'))
256 c = container.appendChild(E.div(style='flex-grow:10'))
257 editor = create_comments_editor(c, {'insert_image_files': True})
258 set_comments_html(c, html)
259 focus_comments_editor(c)
260 editor.init()
261
262
263 def set_panel_handlers():
264 set_panel_handler('show_note', init_display_note)
265 set_panel_handler('edit_note', init_edit)