#[cfg(target_arch = "wasm32")]
mod browser;
#[cfg(any(target_arch = "wasm32", test))]
mod clipboard_state;
#[cfg(any(target_arch = "wasm32", test))]
mod file_reads;
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "browser/plan_fields.rs"]
mod plan_fields_tests;
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "browser/plan_parts.rs"]
mod plan_parts_tests;
#[cfg(any(target_arch = "wasm32", test))]
mod semantic_changes;
#[cfg(target_arch = "wasm32")]
pub mod template;
#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "browser/templates.rs"]
mod template_tests;
pub mod view;
#[cfg(target_arch = "wasm32")]
pub use browser::*;
#[cfg(feature = "stream")]
pub const STREAM_SCRIPT: &str = include_str!("stream.js");
#[cfg(all(target_arch = "wasm32", feature = "resources"))]
pub fn read_resources(
element: &web_sys::Element,
limits: glory_resource::Limits,
) -> Result<glory_resource::Snapshot, glory_resource::Error> {
use glory_resource::{Error, Snapshot, VERSION};
if element.namespace_uri().as_deref() != Some("http://www.w3.org/1999/xhtml")
|| element.local_name() != "script"
|| element.get_attribute("type").as_deref() != Some("application/json")
{
return Err(Error::Payload(
"expected an application/json script element",
));
}
if element.get_attribute("data-gly-resources").as_deref() != Some(VERSION.to_string().as_str())
{
return Err(Error::Payload("resource data block version"));
}
let json = element
.text_content()
.ok_or(Error::Payload("resource data block has no text"))?;
Snapshot::from_json(&json, limits)
}
#[cfg(all(target_arch = "wasm32", feature = "resources", feature = "hydrate"))]
pub fn hydrate_resources_with<W: glory_ui::Widget>(
root: web_sys::Element,
data: &web_sys::Element,
limits: glory_resource::Limits,
factory: impl FnOnce(&glory_ui::Scope, &glory_resource::Resources) -> W,
) -> Result<(WebApp, glory_resource::Resources), HydrationError> {
if root.contains(Some(data.as_ref())) {
return Err(HydrationError::Resources(glory_resource::Error::Payload(
"resource block must be outside the mount",
)));
}
let snapshot = read_resources(data, limits).map_err(HydrationError::Resources)?;
let resources =
glory_resource::Resources::restore(snapshot).map_err(HydrationError::Resources)?;
let app = hydrate_with(root, |scope| factory(scope, &resources))?;
data.remove();
Ok((app, resources))
}