annotate libervia/pages/_browser/editor.py @ 1443:3821168b9459 v0.8.0b1

v0.8.0b1 release
author Goffi <goffi@goffi.org>
date Fri, 18 Jun 2021 16:21:28 +0200
parents 925a7c498cda
children 106bae41f5c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1415
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 """text edition management"""
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
1420
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
3 from browser import document, window, aio, bind, timer
1415
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from browser.local_storage import storage
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from browser.object_storage import ObjectStorage
1420
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
6 from javascript import JSON
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
7 from aio_bridge import Bridge, BridgeException
1415
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from template import Template
1420
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
9 import dialog
1415
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
1420
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
11 bridge = Bridge()
1415
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 object_storage = ObjectStorage(storage)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 profile = window.profile
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # how often we save forms, in seconds
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 AUTOSAVE_FREQUENCY = 20
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def serialise_form(form_elt):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 ret = {}
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 for elt in form_elt.elements:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 if elt.tagName == "INPUT":
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 if elt.type in ("hidden", "submit"):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 continue
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 elif elt.type == "text":
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 ret[elt.name] = elt.value
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 else:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 print(f"elt.type not managet yet: {elt.type}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 continue
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 elif elt.tagName == "TEXTAREA":
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 ret[elt.name] = elt.value
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 elif elt.tagName in ("BUTTON",):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 continue
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 else:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 print(f"tag not managet yet: {elt.tagName}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 continue
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 return ret
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 def restore_form(form_elt, data):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 for elt in form_elt.elements:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 if elt.tagName not in ("INPUT", "TEXTAREA"):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 continue
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 try:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 value = data[elt.name]
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 except KeyError:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 continue
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 else:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 elt.value = value
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def set_form_autosave(form_id):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """Save locally form data regularly and restore it until it's submitted
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 form is saved every AUTOSAVE_FREQUENCY seconds and when visibility is lost.
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 Saved data is restored when the method is called.
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 Saved data is cleared when the form is submitted.
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 if profile is None:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 print(f"No session started, won't save and restore form {form_id}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 return
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 form_elt = document[form_id]
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 submitted = False
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 key = {"profile": profile, "type": "form_autosave", "form": form_id}
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 try:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 form_saved_data = object_storage[key]
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 except KeyError:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 last_serialised = None
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 else:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 print(f"restoring content of form {form_id!r}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 last_serialised = form_saved_data
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 restore_form(form_elt, form_saved_data)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def save_form():
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 if not submitted:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 nonlocal last_serialised
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 serialised = serialise_form(form_elt)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if serialised != last_serialised:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 last_serialised = serialised
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 print(f"saving content of form {form_id!r}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 object_storage[key] = serialised
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @bind(form_elt, "submit")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def on_submit(evt):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 nonlocal submitted
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 submitted = True
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 print(f"clearing stored content of form {form_id!r}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 try:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 del object_storage[key]
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 except KeyError:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 print("key error")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 pass
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 @bind(document, "visibilitychange")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def on_visibiliy_change(evt):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 print("visibility change")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if document.visibilityState != "visible":
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 save_form()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 timer.set_interval(save_form, AUTOSAVE_FREQUENCY * 1000)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 class TagsEditor:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def __init__(self, input_selector):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 print("installing Tags Editor")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 self.input_elt = document.select_one(input_selector)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self.input_elt.style.display = "none"
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 tags_editor_tpl = Template('editor/tags_editor.html')
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 self.tag_tpl = Template('editor/tag.html')
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 editor_elt = tags_editor_tpl.get_elt()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.input_elt.parent <= editor_elt
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.tag_input_elt = editor_elt.select_one(".tag_input")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 self.tag_input_elt.bind("keydown", self.on_key_down)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self._current_tags = None
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 self.tags_map = {}
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 for tag in self.current_tags:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 self.add_tag(tag, force=True)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 @property
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 def current_tags(self):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if self._current_tags is None:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self._current_tags = {
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 t.strip() for t in self.input_elt.value.split(',') if t.strip()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 }
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return self._current_tags
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 @current_tags.setter
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 def current_tags(self, tags):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self._current_tags = tags
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def add_tag(self, tag, force=False):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 tag = tag.strip()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 if not force and (not tag or tag in self.current_tags):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self.current_tags = self.current_tags | {tag}
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 self.input_elt.value = ','.join(self.current_tags)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 tag_elt = self.tag_tpl.get_elt({"label": tag})
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self.tags_map[tag] = tag_elt
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 self.tag_input_elt.parent.insertBefore(tag_elt, self.tag_input_elt)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 tag_elt.select_one(".click_to_delete").bind(
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 "click", lambda evt: self.on_tag_click(evt, tag)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 )
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 def remove_tag(self, tag):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 try:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 tag_elt = self.tags_map[tag]
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 except KeyError:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 print(f"trying to remove an inexistant tag: {tag}")
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 else:
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 self.current_tags = self.current_tags - {tag}
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 self.input_elt.value = ','.join(self.current_tags)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 tag_elt.remove()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 def on_tag_click(self, evt, tag):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 evt.stopPropagation()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 self.remove_tag(tag)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 def on_key_down(self, evt):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 if evt.key in (",", "Enter"):
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 evt.stopPropagation()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 evt.preventDefault()
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 self.add_tag(self.tag_input_elt.value)
8415d882b686 browser: new `editor` module:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self.tag_input_elt.value = ""
1420
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
168
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
169
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
170 class BlogEditor:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
171 """Editor class, handling tabs, preview, and submit loading button
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
172
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
173 It's using and HTML form as source
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
174 The form must have:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
175 - a "title" text input
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
176 - a "body" textarea
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
177 - an optional "tags" text input with comma separated tags (may be using Tags
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
178 Editor)
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
179 - a "tab_preview" tab element
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
180 """
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
181
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
182 def __init__(self, form_id="blog_post_edit"):
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
183 self.tab_select = window.tab_select
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
184 self.item_tpl = Template('blog/item.html')
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
185 self.form = document[form_id]
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
186 for elt in document.select(".click_to_edit"):
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
187 elt.bind("click", self.on_edit)
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
188 for elt in document.select('.click_to_preview'):
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
189 elt.bind("click", lambda evt: aio.run(self.on_preview(evt)))
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
190 self.form.bind("submit", self.on_submit)
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
191
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
192
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
193 def on_edit(self, evt):
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
194 self.tab_select(evt.target, "tab_edit", "is-active")
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
195
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
196 async def on_preview(self, evt):
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
197 """Generate a blog preview from HTML form
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
198
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
199 """
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
200 print("on preview OK")
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
201 elt = evt.target
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
202 tab_preview = document["tab_preview"]
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
203 tab_preview.clear()
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
204 data = {
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
205 "content_rich": self.form.select_one('textarea[name="body"]').value.strip()
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
206 }
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
207 title = self.form.select_one('input[name="title"]').value.strip()
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
208 if title:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
209 data["title_rich"] = title
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
210 tags_input_elt = self.form.select_one('input[name="tags"]')
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
211 if tags_input_elt is not None:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
212 tags = tags_input_elt.value.strip()
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
213 if tags:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
214 data['tags'] = [t.strip() for t in tags.split(',') if t.strip()]
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
215 try:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
216 preview_data = JSON.parse(
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
217 await bridge.mbPreview("", "", JSON.stringify(data))
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
218 )
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
219 except BridgeException as e:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
220 dialog.notification.show(
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
221 f"Can't generate item preview: {e.message}",
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
222 level="error"
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
223 )
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
224 else:
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
225 self.tab_select(elt, "tab_preview", "is-active")
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
226 item_elt = self.item_tpl.get_elt({
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
227 "item": preview_data,
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
228 "dates_format": "short",
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
229 })
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
230 tab_preview <= item_elt
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
231
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
232 def on_submit(self, evt):
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
233 submit_btn = document.select_one("button[type='submit']")
925a7c498cda pages (blog/edit): move preview code to new `BlogEditor` class in `editor` module
Goffi <goffi@goffi.org>
parents: 1415
diff changeset
234 submit_btn.classList.add("is-loading")