|
32 | 32 | import web |
33 | 33 | import yaml |
34 | 34 | from babel.lists import format_list |
| 35 | +from markupsafe import Markup |
35 | 36 | from web.template import TemplateResult |
36 | 37 | from web.utils import Storage |
37 | 38 |
|
@@ -188,36 +189,48 @@ def kebab_case(upper_camel_case: str) -> str: |
188 | 189 |
|
189 | 190 |
|
190 | 191 | @public |
191 | | -def render_component( |
192 | | - name: str, |
193 | | - attrs: dict | None = None, |
194 | | - json_encode: bool = True, |
195 | | - asyncDefer=False, |
196 | | -) -> str: |
197 | | - """ |
198 | | - :param str name: Name of the component (excluding extension) |
199 | | - :param dict attrs: attributes to add to the component element |
| 192 | +def vue_component_html(name: str, attrs: dict | None = None, async_defer: bool = False) -> Markup: |
| 193 | + """The script tag and custom element that render the Vue component built from ``openlibrary/components/<name>.vue``. |
| 194 | +
|
| 195 | + Reads no request state, so it is safe in Jinja templates served by FastAPI. |
| 196 | +
|
| 197 | + :param name: component name, e.g. ``"BulkSearch"`` for ``<ol-bulk-search>`` |
| 198 | + :param attrs: element attributes. Dicts and lists are JSON- then URL-encoded; |
| 199 | + the component reads them back with ``JSON.parse(decodeURIComponent(...))``. |
200 | 200 | """ |
| 201 | + return _vue_component_script(name, async_defer) + _vue_component_element(name, attrs) |
| 202 | + |
| 203 | + |
| 204 | +def _vue_component_script(name: str, async_defer: bool) -> Markup: |
201 | 205 | from openlibrary.plugins.upstream.code import static_url |
202 | 206 |
|
203 | | - attrs = attrs or {} |
204 | | - attrs_str = "" |
205 | | - for key, val in attrs.items(): |
206 | | - if (json_encode and isinstance(val, dict)) or isinstance(val, list): |
207 | | - val = json.dumps(val) |
208 | | - # On the Vue side use decodeURIComponent to decode |
209 | | - val = urllib.parse.quote(val) |
210 | | - attrs_str += f' {key}="{val}"' |
211 | | - html = "" |
| 207 | + url = static_url(f"build/components/production/ol-{name}.js") |
| 208 | + async_attrs = " async defer" if async_defer else "" |
| 209 | + return Markup('<script type="module"{} src="{}"></script>').format(async_attrs, url) |
| 210 | + |
212 | 211 |
|
| 212 | +def _vue_component_element(name: str, attrs: dict | None) -> Markup: |
| 213 | + tag = kebab_case(name) |
| 214 | + attrs_html = Markup("") |
| 215 | + for key, val in (attrs or {}).items(): |
| 216 | + if isinstance(val, dict | list): |
| 217 | + val = urllib.parse.quote(json.dumps(val)) |
| 218 | + attrs_html += Markup(' {}="{}"').format(key, val) |
| 219 | + return Markup("<ol-{0}{1}></ol-{0}>").format(tag, attrs_html) |
| 220 | + |
| 221 | + |
| 222 | +@public |
| 223 | +def render_component(name: str, attrs: dict | None = None, asyncDefer=False) -> str: |
| 224 | + """:func:`vue_component_html` for Templetor pages on web.py. |
| 225 | +
|
| 226 | + Writes each component's script tag only once per request, tracked in ``web.ctx``, |
| 227 | + so it must not run on FastAPI. |
| 228 | + """ |
| 229 | + html = Markup("") |
213 | 230 | if name not in (included := web.ctx.setdefault("included-components", [])): |
214 | | - url = static_url("build/components/production/ol-%s.js" % name) |
215 | | - script_attrs = "" if not asyncDefer else "async defer" |
216 | | - html += f'<script type="module" {script_attrs} src="{url}"></script>' |
| 231 | + html += _vue_component_script(name, asyncDefer) |
217 | 232 | included.append(name) |
218 | | - |
219 | | - html += f"<ol-{kebab_case(name)} {attrs_str}></ol-{kebab_case(name)}>" |
220 | | - return html |
| 233 | + return html + _vue_component_element(name, attrs) |
221 | 234 |
|
222 | 235 |
|
223 | 236 | @public |
|
0 commit comments