From 2dafa013211cc91df78b710f75cc9d4e1b1e825e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 13 Oct 2023 10:51:29 +0530 Subject: [PATCH] Work on editor for notes in content server --- src/pyj/book_list/router.pyj | 4 +-- src/pyj/book_list/show_note.pyj | 58 ++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/pyj/book_list/router.pyj b/src/pyj/book_list/router.pyj index 2c6ce43761..57ca899305 100644 --- a/src/pyj/book_list/router.pyj +++ b/src/pyj/book_list/router.pyj @@ -96,9 +96,9 @@ def show_note_url(book_id, field, item_id, item_value, library_id=None, close_ac return ans + encode_query(q, '#') -def show_note(book_id, field, item_id, item_value, replace=False, library_id=None, close_action='back'): +def show_note(book_id, field, item_id, item_value, replace=False, library_id=None, close_action='back', panel='show_note'): lid = library_id or current_library_id() - q = {'book_id':book_id, 'field': field, 'item':item_value, 'item_id': item_id, 'panel': 'show_note', 'close_action': close_action} + q = {'book_id':book_id, 'field': field, 'item':item_value, 'item_id': item_id, 'panel': panel, 'close_action': close_action} if lid: q.library_id = lid push_state(q, replace=replace) diff --git a/src/pyj/book_list/show_note.pyj b/src/pyj/book_list/show_note.pyj index a821202521..f427e5626c 100644 --- a/src/pyj/book_list/show_note.pyj +++ b/src/pyj/book_list/show_note.pyj @@ -2,6 +2,7 @@ # License: GPL v3 Copyright: 2023, Kovid Goyal from __python__ import bound_methods, hash_literals +from elementmaker import E from ajax import ajax from book_list.details_list import sandbox_css from book_list.router import back, home @@ -18,16 +19,27 @@ def make_iframe(html): return iframe +current_note_markup = None + + def on_notes_fetched(load_type, xhr, ev): + nonlocal current_note_markup + q = parse_url_params() if load_type is 'load': html = xhr.responseText + q.html = xhr.responseText + current_note_markup = q else: html = xhr.error_html iframe = make_iframe(html) container = document.getElementById(this) - old = container.getElementsByTagName('iframe')[0] - old.parentNode.appendChild(iframe) - old.parentNode.removeChild(old) + old = container.querySelector('div.loading') + p = old.parentNode + p.removeChild(old) + if q.panel is 'show_note': + p.appendChild(iframe) + elif q.panel is 'edit_note': + create_editor(container, current_note_markup.html) def init(container_id): @@ -38,14 +50,44 @@ def init(container_id): if ca is 'home': close_action, close_icon = def(): home();, 'home' create_top_bar(container, title=q.item, action=close_action, icon=close_icon) - url = 'get-note/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item_id) - if q.library_id: - url += '/' + encodeURIComponent(q.library_id) container.style.height = '100vh' container.style.display = 'flex' container.style.flexDirection = 'column' - container.appendChild(make_iframe(_('Loading') + '…')) - ajax(url, on_notes_fetched.bind(container_id), bypass_cache=False).send() + if current_note_markup and current_note_markup.library_id is q.library_id and current_note_markup.field is q.field and current_note_markup.item_id is q.item_id: + html = current_note_markup.html + container.appendChild(make_iframe(html)) + else: + container.appendChild(E.div(_('Loading') + '…', style='margin: 0.5rem', class_='loading')) + url = 'get-note/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item_id) + if q.library_id: + url += '/' + encodeURIComponent(q.library_id) + ajax(url, on_notes_fetched.bind(container_id), bypass_cache=False).send() + + +def init_edit(container_id): + container = document.getElementById(container_id) + close_action, close_icon = back, 'close' + q = parse_url_params() + ca = q.close_action + if ca is 'home': + close_action, close_icon = def(): home();, 'home' + create_top_bar(container, title=_('Edit notes for:') + ' ' + q.item, action=close_action, icon=close_icon) + container.style.height = '100vh' + container.style.display = 'flex' + container.style.flexDirection = 'column' + if current_note_markup and current_note_markup.library_id is q.library_id and current_note_markup.field is q.field and current_note_markup.item_id is q.item_id: + create_editor(container, current_note_markup.html) + else: + container.appendChild(E.div(_('Loading') + '…', style='margin: 0.5rem', class_='loading')) + url = 'get-note/' + encodeURIComponent(q.field) + '/' + encodeURIComponent(q.item_id) + if q.library_id: + url += '/' + encodeURIComponent(q.library_id) + ajax(url, on_notes_fetched.bind(container_id), bypass_cache=False).send() + + +def create_editor(container, html): + pass set_panel_handler('show_note', init) +set_panel_handler('edit_note', init_edit)