annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1416
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import document, window, aio, bind
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 from aio_bridge import Bridge, BridgeException
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 import editor
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 import dialog
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from javascript import JSON
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from template import Template
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 bridge = Bridge()
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 item_tpl = Template('blog/item.html')
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 tab_select = window.tab_select
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 async def on_preview(evt):
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 elt = evt.target
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 tab_preview = document["tab_preview"]
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 tab_preview.clear()
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 form = document["blog_post_edit"]
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 data = {
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 "content_rich": form.select_one('textarea[name="body"]').value.strip()
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 }
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 title = form.select_one('input[name="title"]').value.strip()
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 if title:
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 data["title_rich"] = title
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 tags = form.select_one('input[name="tags"]').value.strip()
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 if tags:
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 data['tags'] = [t.strip() for t in tags.split(',') if t.strip()]
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 try:
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 preview_data = JSON.parse(await bridge.mbPreview("", "", JSON.stringify(data)))
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 except BridgeException as e:
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 dialog.notification.show(
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 f"Can't generate item preview: {e.message}",
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 level="error"
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 )
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 else:
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 tab_select(elt, "tab_preview", "is-active")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 item_elt = item_tpl.get_elt({
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "item": preview_data,
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 "dates_format": "short",
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 })
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 tab_preview <= item_elt
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 @bind(".click_to_edit", "click")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def on_edit(evt):
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 tab_select(evt.target, "tab_edit", "is-active")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @bind(document["blog_post_edit"], "submit")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def on_submit(evt):
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 submit_btn = document.select_one("button[type='submit']")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 submit_btn.classList.add("is-loading")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 for elt in document.select('.click_to_preview'):
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 elt.bind("click", lambda evt: aio.run(on_preview(evt)))
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 editor.set_form_autosave("blog_post_edit")
0554103ec700 pages (blog): new `edit` page:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 editor.TagsEditor("input[name='tags']")