mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Comments editor now works on EM page
This commit is contained in:
parent
4ed67af5f2
commit
3b464dc99b
@ -52,6 +52,7 @@ registry = {}
|
|||||||
def add_editor(editor):
|
def add_editor(editor):
|
||||||
for k in Object.keys(registry):
|
for k in Object.keys(registry):
|
||||||
if not document.getElementById(k):
|
if not document.getElementById(k):
|
||||||
|
registry[k].destroy()
|
||||||
v'delete registry[k]'
|
v'delete registry[k]'
|
||||||
registry[editor.id] = editor
|
registry[editor.id] = editor
|
||||||
|
|
||||||
@ -72,6 +73,10 @@ class Editor:
|
|||||||
def init(self):
|
def init(self):
|
||||||
self.iframe_wrapper.init()
|
self.iframe_wrapper.init()
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
self.iframe_wrapper.destroy()
|
||||||
|
self.get_html_callbacks = v'[]'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def iframe(self):
|
def iframe(self):
|
||||||
return self.iframe_wrapper.iframe
|
return self.iframe_wrapper.iframe
|
||||||
@ -115,7 +120,7 @@ def create_editor():
|
|||||||
|
|
||||||
def create_comments_editor(container):
|
def create_comments_editor(container):
|
||||||
iframe, editor = create_editor()
|
iframe, editor = create_editor()
|
||||||
toolbar1 = E.div(style='flex-grow: 0')
|
toolbar1 = E.div('TODO: add toolbar', style='flex-grow: 0')
|
||||||
container.setAttribute('style', (container.getAttribute('style') or '') + ';display: flex; flex-direction: column; align-items: stretch')
|
container.setAttribute('style', (container.getAttribute('style') or '') + ';display: flex; flex-direction: column; align-items: stretch')
|
||||||
container.appendChild(toolbar1)
|
container.appendChild(toolbar1)
|
||||||
container.appendChild(iframe)
|
container.appendChild(iframe)
|
||||||
|
@ -76,17 +76,14 @@ def truncated_html(val):
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
def onsubmit_field2(container_id, book_id, field, value):
|
||||||
def onsubmit_field(get_value, container_id, book_id, field):
|
nonlocal has_changes
|
||||||
c = document.getElementById(container_id)
|
c = document.getElementById(container_id)
|
||||||
if not c:
|
if not c:
|
||||||
return
|
return
|
||||||
d = c.querySelector('div[data-ctype="edit"]')
|
d = c.querySelector('div[data-ctype="edit"]')
|
||||||
if not d:
|
if not d:
|
||||||
return
|
return
|
||||||
ok, value = get_value(d)
|
|
||||||
if not ok:
|
|
||||||
return
|
|
||||||
is_series = value.series_index is not undefined
|
is_series = value.series_index is not undefined
|
||||||
if is_series:
|
if is_series:
|
||||||
unchanged = value.series_name is book_metadata(book_id)[field] and value.series_index is book_metadata(book_id)[field + '_index']
|
unchanged = value.series_name is book_metadata(book_id)[field] and value.series_index is book_metadata(book_id)[field + '_index']
|
||||||
@ -96,8 +93,6 @@ def onsubmit_field(get_value, container_id, book_id, field):
|
|||||||
on_close(container_id)
|
on_close(container_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
def proceed():
|
|
||||||
nonlocal has_changes
|
|
||||||
clear(d)
|
clear(d)
|
||||||
d.appendChild(E.div(style='margin: 1ex 1rem', _('Contacting server, please wait') + '…'))
|
d.appendChild(E.div(style='margin: 1ex 1rem', _('Contacting server, please wait') + '…'))
|
||||||
if is_series:
|
if is_series:
|
||||||
@ -110,7 +105,21 @@ def onsubmit_field(get_value, container_id, book_id, field):
|
|||||||
on_close(container_id)
|
on_close(container_id)
|
||||||
|
|
||||||
|
|
||||||
window.setTimeout(proceed, 0) # needed to avoid console error about form submission failing because form is removed from DOM in onsubmit handler
|
def onsubmit_field(get_value, container_id, book_id, field):
|
||||||
|
c = document.getElementById(container_id)
|
||||||
|
if not c:
|
||||||
|
return
|
||||||
|
d = c.querySelector('div[data-ctype="edit"]')
|
||||||
|
if not d:
|
||||||
|
return
|
||||||
|
if get_value is html_edit_get_value:
|
||||||
|
html_edit_get_value(d, onsubmit_field2.bind(None, container_id, book_id, field))
|
||||||
|
else:
|
||||||
|
ok, value = get_value(d)
|
||||||
|
if not ok:
|
||||||
|
return
|
||||||
|
# needed to avoid console error about form submission failing because form is removed from DOM in onsubmit handler
|
||||||
|
window.setTimeout(onsubmit_field2, 0, container_id, book_id, field, value)
|
||||||
|
|
||||||
|
|
||||||
def create_form(widget, get_value, container_id, book_id, field):
|
def create_form(widget, get_value, container_id, book_id, field):
|
||||||
@ -160,8 +169,8 @@ def text_edit(container_id, book_id, field, fm, div, mi, get_name):
|
|||||||
le.focus()
|
le.focus()
|
||||||
|
|
||||||
|
|
||||||
def html_edit_get_value(container):
|
def html_edit_get_value(container, proceed):
|
||||||
return True, get_comments_html(container)
|
get_comments_html(container, proceed)
|
||||||
|
|
||||||
|
|
||||||
def html_edit(container_id, book_id, field, fm, div, mi):
|
def html_edit(container_id, book_id, field, fm, div, mi):
|
||||||
@ -171,12 +180,13 @@ def html_edit(container_id, book_id, field, fm, div, mi):
|
|||||||
name = fm.name or field
|
name = fm.name or field
|
||||||
c = E.div(style='width: 100%; min-height: 75vh')
|
c = E.div(style='width: 100%; min-height: 75vh')
|
||||||
form = create_form(c, html_edit_get_value, container_id, book_id, field)
|
form = create_form(c, html_edit_get_value, container_id, book_id, field)
|
||||||
create_comments_editor(c)
|
editor = create_comments_editor(c)
|
||||||
set_comments_html(c, val)
|
set_comments_html(c, val)
|
||||||
div.appendChild(E.div(style='margin: 0.5ex 1rem', _('Edit the "{}" below').format(name)))
|
div.appendChild(E.div(style='margin: 0.5ex 1rem', _('Edit the "{}" below').format(name)))
|
||||||
div.appendChild(E.div(style='margin: 0.5ex 1rem', form))
|
div.appendChild(E.div(style='margin: 0.5ex 1rem', form))
|
||||||
focus_comments_editor(c)
|
focus_comments_editor(c)
|
||||||
value_to_json = identity
|
value_to_json = identity
|
||||||
|
editor.init()
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
# Number edit {{{
|
# Number edit {{{
|
||||||
@ -573,7 +583,7 @@ def edit_field(container_id, book_id, field):
|
|||||||
number_edit(container_id, book_id, field, fm, d, mi)
|
number_edit(container_id, book_id, field, fm, d, mi)
|
||||||
elif field is 'identifiers':
|
elif field is 'identifiers':
|
||||||
identifiers_edit(container_id, book_id, field, fm, d, mi)
|
identifiers_edit(container_id, book_id, field, fm, d, mi)
|
||||||
elif fm.datatype is 'comments':
|
elif fm.datatype is 'comments' or field is 'comments':
|
||||||
ias = fm.display?.interpret_as
|
ias = fm.display?.interpret_as
|
||||||
if ias is 'short-text':
|
if ias is 'short-text':
|
||||||
simple_line_edit(container_id, book_id, field, fm, d, mi)
|
simple_line_edit(container_id, book_id, field, fm, d, mi)
|
||||||
|
@ -72,6 +72,9 @@ class IframeWrapper:
|
|||||||
self.handlers.ready = self.on_iframe_ready
|
self.handlers.ready = self.on_iframe_ready
|
||||||
window.addEventListener('message', self.handle_message, False)
|
window.addEventListener('message', self.handle_message, False)
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
window.removeEventListener('message', self.handle_message, False)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def iframe(self):
|
def iframe(self):
|
||||||
return document.getElementById(self.iframe_id)
|
return document.getElementById(self.iframe_id)
|
||||||
@ -118,7 +121,7 @@ class IframeWrapper:
|
|||||||
self._send_message(action, False, data)
|
self._send_message(action, False, data)
|
||||||
|
|
||||||
def handle_message(self, event):
|
def handle_message(self, event):
|
||||||
if event.source is not self.iframe.contentWindow:
|
if event.source is not self.iframe?.contentWindow:
|
||||||
return
|
return
|
||||||
data = event.data
|
data = event.data
|
||||||
if self.encrypted_communications:
|
if self.encrypted_communications:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user