diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-04-01 17:54:07 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-04-01 17:54:07 -0400 |
| commit | 06070a820f5a92c2671242b044cec5f6b3dacff5 (patch) | |
| tree | 257be786bee080c0eb76941df827680eec9912f0 /lib | |
| parent | c78d291679a0b054e20ad35784dd5a8f9238f0a9 (diff) | |
| download | xesite-06070a820f5a92c2671242b044cec5f6b3dacff5.tar.xz xesite-06070a820f5a92c2671242b044cec5f6b3dacff5.zip | |
build javascript files with esbuild
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/xesite_templates/Cargo.toml | 3 | ||||
| -rw-r--r-- | lib/xesite_templates/src/lib.rs | 37 |
2 files changed, 39 insertions, 1 deletions
diff --git a/lib/xesite_templates/Cargo.toml b/lib/xesite_templates/Cargo.toml index fc852b0..dbfa1d3 100644 --- a/lib/xesite_templates/Cargo.toml +++ b/lib/xesite_templates/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde_json = "1" uuid = { version = "1", features = [ "v4" ] } xesite_types = { path = "../xesite_types" } @@ -13,4 +14,4 @@ xesite_types = { path = "../xesite_types" } [dependencies.maud] git = "https://github.com/Xe/maud" rev = "a40596c42c7603cc4610bbeddea04c4bd8b312d9" -features = ["axum"]
\ No newline at end of file +features = ["axum"] diff --git a/lib/xesite_templates/src/lib.rs b/lib/xesite_templates/src/lib.rs index 767ac45..b1945e9 100644 --- a/lib/xesite_templates/src/lib.rs +++ b/lib/xesite_templates/src/lib.rs @@ -228,3 +228,40 @@ pub fn toot_embed(u: User, t: Toot) -> Markup { } } } + +pub fn xeact_component(name: &str, data: serde_json::Value) -> Markup { + let uuid = uuid::Uuid::new_v4(); + let uuid = format!("{uuid}").replace("-", ""); + + let script = PreEscaped(format!( + r#" +<script type="module"> +import Component from "/static/xeact/{name}.js"; + +const g = (name) => document.getElementById(name); +const x = (elem) => {{ + while (elem.lastChild) {{ + elem.removeChild(elem.lastChild); + }} +}}; + +const root = g("{uuid}"); +x(g); + +root.appendChild(Component({data})) +</script> +"#, + data=serde_json::to_string(&data).unwrap(), + )); + + html! { + div id=(uuid) { + noscript { + div.warning { + (conv("Aoi".into(), "coffee".into(), PreEscaped("This dynamic component requires JavaScript to function, sorry!".to_string()))) + } + } + } + (script) + } +} |
