Skip to main content

canonrs_interactions_content/
lib.rs

1#![deny(warnings)]
2//! canonrs-interactions-content
3
4pub mod markdown;
5pub mod copy_button;
6
7
8use canonrs_interactions_core::runtime::bootstrap;
9
10use wasm_bindgen::prelude::*;
11
12/// WASM entry point — initialize all content components in document
13#[wasm_bindgen]
14pub fn init_content_all() {
15    if let Some(doc) = web_sys::window().and_then(|w| w.document()) {
16        let Ok(nodes) = doc.query_selector_all("[data-rs-interaction=\"content\"]") else { return };
17        for i in 0..nodes.length() {
18            let Some(raw) = nodes.item(i) else { continue };
19            if let Ok(el) = wasm_bindgen::JsCast::dyn_into::<web_sys::Element>(raw) {
20                if !el.has_attribute("data-rs-initialized") {
21                    init_content(el);
22                }
23            }
24        }
25    }
26}
27
28/// WASM entry point — initialize content subtree
29#[wasm_bindgen]
30pub fn init_content_subtree(root: web_sys::Element) {
31    let Ok(nodes) = root.query_selector_all("[data-rs-interaction=\"content\"]") else { return };
32    for i in 0..nodes.length() {
33        let Some(raw) = nodes.item(i) else { continue };
34        if let Ok(el) = wasm_bindgen::JsCast::dyn_into::<web_sys::Element>(raw) {
35            if !el.has_attribute("data-rs-initialized") {
36                init_content(el);
37            }
38        }
39    }
40    // also check root itself
41    if root.get_attribute("data-rs-interaction").as_deref() == Some("content") {
42        if !root.has_attribute("data-rs-initialized") {
43            init_content(root);
44        }
45    }
46}
47
48
49/// Registra o grupo content no bootstrap kernel.
50pub fn register() {
51    bootstrap::register("content", init_content);
52}
53
54/// Init subtree — replay-safe, delega para bootstrap kernel.
55pub fn init_subtree(root: &web_sys::Element) {
56    bootstrap::init_subtree(root);
57}
58pub fn init_content(el: web_sys::Element) {
59    if el.has_attribute("data-rs-markdown") { markdown::init(el.clone()); }
60    if el.has_attribute("data-rs-copy-button") { copy_button::init(el.clone()); }
61}