Use an <iframe> rather than a new window for editing metadata from the viewer

This commit is contained in:
Kovid Goyal 2020-08-12 20:12:42 +05:30
parent 4154c3be14
commit 74644b31ae
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 25 deletions

View File

@ -939,8 +939,8 @@ def changes_submitted(container_id, book_id, end_type, xhr, ev):
return
cq = get_current_query()
if cq.from_read_book and window.opener:
window.opener.postMessage(
if cq.from_read_book:
window.parent.postMessage(
{'type': 'update_cached_book_metadata', 'library_id': cq.library_id, 'book_id': cq.book_id, 'metadata': dirtied[book_id], 'from_read_book': cq.from_read_book},
document.location.protocol + '//' + document.location.host
)
@ -1002,8 +1002,12 @@ def on_close(container_id):
q = parse_url_params()
show_book(container_id, int(q.book_id))
return
if get_current_query().from_read_book:
window.close()
cq = get_current_query()
if cq.from_read_book:
window.parent.postMessage(
{'type': 'edit_metadata_closed', 'from_read_book': cq.from_read_book},
document.location.protocol + '//' + document.location.host
)
else:
back()
@ -1015,6 +1019,9 @@ def proceed_after_succesful_fetch_metadata(container_id, book_id):
container = document.getElementById(container_id)
mi = book_metadata(book_id)
if not mi or not container:
if get_current_query().from_read_book:
container.textContent = _('Failed to read metadata for book')
return
show_panel('book_details', query=parse_url_params(), replace=True)
return
set_title(container, _('Edit metadata for {}').format(mi.title))
@ -1065,18 +1072,6 @@ def init(container_id):
container.lastChild.focus()
container.lastChild.appendChild(E.div(_('Loading books from the calibre library, please wait...'), style='margin: 1ex 1em'))
conditional_timeout(container_id, 5, check_for_books_loaded)
cq = get_current_query()
if cq.from_read_book:
window.opener.postMessage(
{'type': 'edit_metadata_opened', 'library_id': cq.library_id, 'book_id': cq.book_id, 'from_read_book': cq.from_read_book},
document.location.protocol + '//' + document.location.host
)
window.addEventListener('unload', def (ev):
window.opener.postMessage(
{'type': 'edit_metadata_closed', 'from_read_book': cq.from_read_book},
document.location.protocol + '//' + document.location.host
)
)
set_panel_handler('edit_metadata', init)

View File

@ -14,7 +14,9 @@ from book_list.library_data import (
from book_list.router import home
from book_list.theme import get_color
from book_list.ui import query_as_href, show_panel
from dom import add_extra_css, build_rule, clear, set_css, svgicon, unique_id
from dom import (
add_extra_css, build_rule, clear, ensure_id, set_css, svgicon, unique_id
)
from modals import error_dialog
from read_book.globals import runtime, ui_operations
from read_book.goto import create_goto_panel, create_location_overlay
@ -674,19 +676,17 @@ class Overlay:
from_read_book = short_uuid()
def show_metadata_overlay(mi, pathtoebook, lname, book_id, overlay, container):
container.appendChild(E.div(class_=BD_CLASS_NAME, style='padding: 1ex 1em', id=from_read_book))
container_id = ensure_id(container)
container.appendChild(E.div(class_=BD_CLASS_NAME, style='padding: 1ex 1em'))
table = E.table(class_='metadata')
def handle_message(msg):
data = msg.data
if data.from_read_book is not from_read_book:
return
if data.type is 'edit_metadata_opened':
if document.getElementById(from_read_book) and self.panels.length and self.panels[-1].from_read_book is from_read_book:
self.hide_current_panel()
return
if data.type is 'edit_metadata_closed':
ui_operations.stop_waiting_for_messages_from(this)
self.hide_current_panel()
return
if data.type is 'update_cached_book_metadata' and data.metadata:
if current_library_id() is data.library_id:
@ -702,10 +702,14 @@ class Overlay:
container.lastChild.appendChild(E.div(
style='text-align: right',
E.a(_('Edit metadata of book in library'), class_='blue-link', onclick=def():
w = window.open(query_as_href({
container = document.getElementById(container_id)
clear(container)
container.appendChild(E.iframe(
style='position: absolute; left: 0; top: 0; width: 100vw; height: 100vh; border-width: 0',
src=query_as_href({
'from_read_book': from_read_book, 'library_id': lname, 'book_id': book_id + ''}, 'edit_metadata')
, '_blank'
)
))
w = container.lastChild.contentWindow
ui_operations.wait_for_messages_from(w, handle_message.bind(w))
)
))