changeset 1420:925a7c498cda

pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
author Goffi <goffi@goffi.org>
date Sat, 01 May 2021 18:50:04 +0200
parents 6fc41f000d24
children e065c8886b81
files libervia/pages/_browser/editor.py libervia/pages/blog/edit/_browser/__init__.py libervia/pages/blog/edit/page_meta.py
diffstat 3 files changed, 75 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/_browser/editor.py	Thu Apr 29 20:51:36 2021 +0200
+++ b/libervia/pages/_browser/editor.py	Sat May 01 18:50:04 2021 +0200
@@ -1,10 +1,14 @@
 """text edition management"""
 
-from browser import document, window, bind, timer
+from browser import document, window, aio, bind, timer
 from browser.local_storage import storage
 from browser.object_storage import ObjectStorage
+from javascript import JSON
+from aio_bridge import Bridge, BridgeException
 from template import Template
+import dialog
 
+bridge = Bridge()
 object_storage = ObjectStorage(storage)
 profile = window.profile
 
@@ -161,3 +165,70 @@
             evt.preventDefault()
             self.add_tag(self.tag_input_elt.value)
             self.tag_input_elt.value = ""
+
+
+class BlogEditor:
+    """Editor class, handling tabs, preview, and submit loading button
+
+    It's using and HTML form as source
+    The form must have:
+        - a "title" text input
+        - a "body" textarea
+        - an optional "tags" text input with comma separated tags (may be using Tags
+          Editor)
+        - a "tab_preview" tab element
+    """
+
+    def __init__(self, form_id="blog_post_edit"):
+        self.tab_select = window.tab_select
+        self.item_tpl = Template('blog/item.html')
+        self.form = document[form_id]
+        for elt in document.select(".click_to_edit"):
+            elt.bind("click", self.on_edit)
+        for elt in document.select('.click_to_preview'):
+            elt.bind("click", lambda evt: aio.run(self.on_preview(evt)))
+        self.form.bind("submit", self.on_submit)
+
+
+    def on_edit(self, evt):
+        self.tab_select(evt.target, "tab_edit", "is-active")
+
+    async def on_preview(self, evt):
+        """Generate a blog preview from HTML form
+
+        """
+        print("on preview OK")
+        elt = evt.target
+        tab_preview = document["tab_preview"]
+        tab_preview.clear()
+        data = {
+            "content_rich": self.form.select_one('textarea[name="body"]').value.strip()
+        }
+        title = self.form.select_one('input[name="title"]').value.strip()
+        if title:
+            data["title_rich"] = title
+        tags_input_elt = self.form.select_one('input[name="tags"]')
+        if tags_input_elt is not None:
+            tags = tags_input_elt.value.strip()
+            if tags:
+                data['tags'] = [t.strip() for t in tags.split(',') if t.strip()]
+        try:
+            preview_data = JSON.parse(
+                await bridge.mbPreview("", "", JSON.stringify(data))
+            )
+        except BridgeException as e:
+            dialog.notification.show(
+                f"Can't generate item preview: {e.message}",
+                level="error"
+            )
+        else:
+            self.tab_select(elt, "tab_preview", "is-active")
+            item_elt = self.item_tpl.get_elt({
+                "item": preview_data,
+                "dates_format": "short",
+            })
+            tab_preview <= item_elt
+
+    def on_submit(self, evt):
+        submit_btn = document.select_one("button[type='submit']")
+        submit_btn.classList.add("is-loading")
--- a/libervia/pages/blog/edit/_browser/__init__.py	Thu Apr 29 20:51:36 2021 +0200
+++ b/libervia/pages/blog/edit/_browser/__init__.py	Sat May 01 18:50:04 2021 +0200
@@ -1,58 +1,6 @@
-from browser import document, window, aio, bind
-from aio_bridge import Bridge, BridgeException
 import editor
-import dialog
-from javascript import JSON
-from template import Template
-
-bridge = Bridge()
-item_tpl = Template('blog/item.html')
-tab_select = window.tab_select
-
-async def on_preview(evt):
-    elt = evt.target
-    tab_preview = document["tab_preview"]
-    tab_preview.clear()
-    form = document["blog_post_edit"]
-    data = {
-        "content_rich": form.select_one('textarea[name="body"]').value.strip()
-    }
-    title = form.select_one('input[name="title"]').value.strip()
-    if title:
-        data["title_rich"] = title
-    tags = form.select_one('input[name="tags"]').value.strip()
-    if tags:
-        data['tags'] = [t.strip() for t in tags.split(',') if t.strip()]
-    try:
-        preview_data = JSON.parse(await bridge.mbPreview("", "", JSON.stringify(data)))
-    except BridgeException as e:
-        dialog.notification.show(
-            f"Can't generate item preview: {e.message}",
-            level="error"
-        )
-    else:
-        tab_select(elt, "tab_preview", "is-active")
-        item_elt = item_tpl.get_elt({
-            "item": preview_data,
-            "dates_format": "short",
-        })
-        tab_preview <= item_elt
-
-
-@bind(".click_to_edit", "click")
-def on_edit(evt):
-    tab_select(evt.target, "tab_edit", "is-active")
-
-
-@bind(document["blog_post_edit"], "submit")
-def on_submit(evt):
-    submit_btn = document.select_one("button[type='submit']")
-    submit_btn.classList.add("is-loading")
-
-
-for elt in document.select('.click_to_preview'):
-    elt.bind("click", lambda evt: aio.run(on_preview(evt)))
 
 
 editor.set_form_autosave("blog_post_edit")
+editor.BlogEditor()
 editor.TagsEditor("input[name='tags']")
--- a/libervia/pages/blog/edit/page_meta.py	Thu Apr 29 20:51:36 2021 +0200
+++ b/libervia/pages/blog/edit/page_meta.py	Sat May 01 18:50:04 2021 +0200
@@ -16,7 +16,6 @@
     if profile is None:
         self.pageError(request, C.HTTP_FORBIDDEN)
     request_data = self.getRData(request)
-    request_data["post_redirect_page"] = self.getPageByName("blog")
     title, tags, body = self.getPostedData(request, ('title', 'tags', 'body'))
     mb_data = {"content_rich": body}
     title = title.strip()
@@ -33,3 +32,5 @@
         data_format.serialise(mb_data),
         profile
     )
+
+    request_data["post_redirect_page"] = self.getPageByName("blog")