Mercurial > libervia-web
comparison libervia/pages/blog/edit/_browser/__init__.py @ 1416:0554103ec700
pages (blog): new `edit` page:
this page is used for new blog post and may be used for editing existing posts too.
New `editor` module is using for autosave and tags editor.
New `mbPreview` bridge method is used to generate preview when javascript is available.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 29 Apr 2021 16:55:07 +0200 |
parents | |
children | 925a7c498cda |
comparison
equal
deleted
inserted
replaced
1415:8415d882b686 | 1416:0554103ec700 |
---|---|
1 from browser import document, window, aio, bind | |
2 from aio_bridge import Bridge, BridgeException | |
3 import editor | |
4 import dialog | |
5 from javascript import JSON | |
6 from template import Template | |
7 | |
8 bridge = Bridge() | |
9 item_tpl = Template('blog/item.html') | |
10 tab_select = window.tab_select | |
11 | |
12 async def on_preview(evt): | |
13 elt = evt.target | |
14 tab_preview = document["tab_preview"] | |
15 tab_preview.clear() | |
16 form = document["blog_post_edit"] | |
17 data = { | |
18 "content_rich": form.select_one('textarea[name="body"]').value.strip() | |
19 } | |
20 title = form.select_one('input[name="title"]').value.strip() | |
21 if title: | |
22 data["title_rich"] = title | |
23 tags = form.select_one('input[name="tags"]').value.strip() | |
24 if tags: | |
25 data['tags'] = [t.strip() for t in tags.split(',') if t.strip()] | |
26 try: | |
27 preview_data = JSON.parse(await bridge.mbPreview("", "", JSON.stringify(data))) | |
28 except BridgeException as e: | |
29 dialog.notification.show( | |
30 f"Can't generate item preview: {e.message}", | |
31 level="error" | |
32 ) | |
33 else: | |
34 tab_select(elt, "tab_preview", "is-active") | |
35 item_elt = item_tpl.get_elt({ | |
36 "item": preview_data, | |
37 "dates_format": "short", | |
38 }) | |
39 tab_preview <= item_elt | |
40 | |
41 | |
42 @bind(".click_to_edit", "click") | |
43 def on_edit(evt): | |
44 tab_select(evt.target, "tab_edit", "is-active") | |
45 | |
46 | |
47 @bind(document["blog_post_edit"], "submit") | |
48 def on_submit(evt): | |
49 submit_btn = document.select_one("button[type='submit']") | |
50 submit_btn.classList.add("is-loading") | |
51 | |
52 | |
53 for elt in document.select('.click_to_preview'): | |
54 elt.bind("click", lambda evt: aio.run(on_preview(evt))) | |
55 | |
56 | |
57 editor.set_form_autosave("blog_post_edit") | |
58 editor.TagsEditor("input[name='tags']") |