diff --git a/crates/core/src/dom/component/stateful_component.rs b/crates/core/src/dom/component/stateful_component.rs index 32bb0e24..e49fe72c 100644 --- a/crates/core/src/dom/component/stateful_component.rs +++ b/crates/core/src/dom/component/stateful_component.rs @@ -1,11 +1,11 @@ -use std::{any::TypeId, cell::RefCell, fmt, rc::Rc}; use crate::{ dom::{ - events::on_component_mount, program::MountProcedure, Application, Cmd, Component, DomAttrValue, - DomNode, Program, + events::on_component_mount, program::MountProcedure, Application, Cmd, Component, + DomAttrValue, DomNode, Program, }, vdom::{Attribute, AttributeName, Leaf, Node}, }; +use std::{any::TypeId, cell::RefCell, fmt, rc::Rc}; /// A component that can be used directly in the view without mapping pub trait StatefulComponent { @@ -24,8 +24,6 @@ pub trait StatefulComponent { /// remove the attribute with this name fn remove_attribute(&mut self, _attr_name: AttributeName) {} - - /// return the DomNode which contains the children DomNode fn child_container(&self) -> Option; diff --git a/crates/core/src/dom/component/template.rs b/crates/core/src/dom/component/template.rs index a8af8c0c..7a333c90 100644 --- a/crates/core/src/dom/component/template.rs +++ b/crates/core/src/dom/component/template.rs @@ -4,7 +4,7 @@ use wasm_bindgen::JsCast; use crate::{ dom::{ - document, dom_node::intern, dom_node::DomInner, Application, DomAttr, DomAttrValue, + document, dom_node::intern, dom_node::DomInner, Application, DomAttr, DomAttrValue, DomNode, GroupedDomAttrValues, Program, StatelessModel, }, vdom::{self, Attribute, AttributeValue, Leaf}, @@ -324,7 +324,9 @@ where template } _ => { - log::warn!("template and skip_diff is not found, fallback to no template and skip_diff"); + log::warn!( + "template and skip_diff is not found, fallback to no template and skip_diff" + ); self.create_stateless_component(parent_node, comp) } } @@ -349,7 +351,9 @@ where dom_template } _ => { - log::warn!("template and skip_diff is not found, fallback to no template and skip_diff"); + log::warn!( + "template and skip_diff is not found, fallback to no template and skip_diff" + ); self.create_initial_view() } } @@ -389,7 +393,7 @@ impl DomInner { Self::Symbol(_) => todo!(), Self::Comment(_) => todo!(), Self::Fragment { .. } => todo!(), - Self::StatefulComponent{..} => unreachable!("can not deep clone stateful component"), + Self::StatefulComponent { .. } => unreachable!("can not deep clone stateful component"), } } } diff --git a/crates/core/src/dom/dom_node.rs b/crates/core/src/dom/dom_node.rs index 56898896..b37fb1fd 100644 --- a/crates/core/src/dom/dom_node.rs +++ b/crates/core/src/dom/dom_node.rs @@ -4,6 +4,7 @@ use crate::dom::GroupedDomAttrValues; use crate::dom::StatefulComponent; use crate::dom::StatefulModel; use crate::html::lookup; +use crate::vdom::TreePath; use crate::{ dom::document, dom::events::MountEvent, @@ -19,7 +20,6 @@ use std::fmt; use std::rc::Rc; use wasm_bindgen::{closure::Closure, JsCast, JsValue}; use web_sys::{self, Element, Node}; -use crate::vdom::TreePath; pub(crate) type EventClosure = Closure; pub type NamedEventClosures = IndexMap<&'static str, EventClosure>; @@ -60,7 +60,7 @@ pub enum DomInner { children: Rc>>, }, /// StatefulComponent - StatefulComponent{ + StatefulComponent { comp: Rc>, dom_node: Rc, }, @@ -92,7 +92,7 @@ impl fmt::Debug for DomInner { Self::Symbol(symbol) => f.debug_tuple("Symbol").field(&symbol).finish(), Self::Comment(_) => write!(f, "Comment"), Self::Fragment { .. } => write!(f, "Fragment"), - Self::StatefulComponent{..} => write!(f, "StatefulComponent"), + Self::StatefulComponent { .. } => write!(f, "StatefulComponent"), } } } @@ -148,7 +148,7 @@ impl PartialEq for DomNode { (DomInner::Text(v), DomInner::Text(o)) => v == o, (DomInner::Symbol(v), DomInner::Symbol(o)) => v == o, (DomInner::Comment(v), DomInner::Comment(o)) => v == o, - (DomInner::StatefulComponent{..}, DomInner::StatefulComponent{..}) => todo!(), + (DomInner::StatefulComponent { .. }, DomInner::StatefulComponent { .. }) => todo!(), _ => false, } } @@ -169,7 +169,7 @@ impl DomNode { #[allow(unused)] pub(crate) fn is_stateful_component(&self) -> bool { - matches!(&self.inner, DomInner::StatefulComponent{..}) + matches!(&self.inner, DomInner::StatefulComponent { .. }) } pub(crate) fn tag(&self) -> Option { @@ -187,9 +187,7 @@ impl DomNode { DomInner::Text(text_node) => text_node.clone().unchecked_into(), DomInner::Symbol(_) => panic!("don't know how to deal with symbol"), DomInner::Comment(comment_node) => comment_node.clone().unchecked_into(), - DomInner::StatefulComponent{dom_node,..} => { - dom_node.as_node() - } + DomInner::StatefulComponent { dom_node, .. } => dom_node.as_node(), } } @@ -205,7 +203,7 @@ impl DomNode { DomInner::Text(text_node) => text_node.clone().unchecked_into(), DomInner::Symbol(_) => panic!("don't know how to deal with symbol"), DomInner::Comment(comment_node) => comment_node.clone().unchecked_into(), - DomInner::StatefulComponent{dom_node,..} => dom_node.as_element(), + DomInner::StatefulComponent { dom_node, .. } => dom_node.as_element(), } } @@ -533,15 +531,15 @@ impl DomNode { } #[allow(unused)] - pub(crate) fn find_child(&self, target_child: &DomNode, path: TreePath) -> Option{ + pub(crate) fn find_child(&self, target_child: &DomNode, path: TreePath) -> Option { if self == target_child { Some(path) - }else{ + } else { let children = self.children()?; - for (i,child) in children.iter().enumerate(){ + for (i, child) in children.iter().enumerate() { let child_path = path.traverse(i); let found = child.find_child(target_child, child_path); - if found.is_some(){ + if found.is_some() { return found; } } @@ -599,14 +597,13 @@ impl DomNode { } Ok(()) } - DomInner::StatefulComponent{comp: _, dom_node} => { + DomInner::StatefulComponent { comp: _, dom_node } => { dom_node.render(buffer)?; Ok(()) } _ => todo!("for other else"), } } - } #[cfg(feature = "with-interning")] @@ -706,10 +703,12 @@ where Leaf::NodeList(nodes) => self.create_fragment_node(parent_node, nodes), Leaf::StatefulComponent(comp) => { //TODO: also put the children and attributes here - DomNode{ - inner: DomInner::StatefulComponent{ + DomNode { + inner: DomInner::StatefulComponent { comp: Rc::clone(&comp.comp), - dom_node: Rc::new(self.create_stateful_component(Rc::clone(&parent_node), comp)), + dom_node: Rc::new( + self.create_stateful_component(Rc::clone(&parent_node), comp), + ), }, parent: parent_node, } @@ -877,7 +876,6 @@ where } } - /// render the underlying real dom node into string pub fn render_real_dom_to_string(node: &web_sys::Node) -> String { let mut f = String::new(); @@ -918,7 +916,7 @@ pub fn render_real_dom(node: &web_sys::Node, buffer: &mut dyn fmt::Write) -> fmt let child_nodes = element.child_nodes(); let child_count = child_nodes.length(); - for i in 0..child_count{ + for i in 0..child_count { let child = child_nodes.get(i).unwrap(); render_real_dom(&child, buffer)?; } diff --git a/crates/core/src/dom/dom_patch.rs b/crates/core/src/dom/dom_patch.rs index 95c2e0bd..1f9d0de4 100644 --- a/crates/core/src/dom/dom_patch.rs +++ b/crates/core/src/dom/dom_patch.rs @@ -1,9 +1,11 @@ use crate::dom; +use crate::dom::dom_node; use crate::dom::dom_node::DomInner; use crate::dom::DomAttr; use crate::dom::DomAttrValue; use crate::dom::DomNode; use crate::dom::{Application, Program}; +use crate::vdom::ComponentEventCallback; use crate::vdom::EventCallback; use crate::vdom::TreePath; use crate::vdom::{Attribute, AttributeValue, Patch, PatchType}; @@ -11,8 +13,6 @@ use indexmap::IndexMap; use std::rc::Rc; use wasm_bindgen::closure::Closure; use wasm_bindgen::JsValue; -use crate::vdom::ComponentEventCallback; -use crate::dom::dom_node; /// a Patch where the virtual nodes are all created in the document. /// This is necessary since the created Node doesn't contain references @@ -77,14 +77,19 @@ pub enum PatchVariant { }, } -impl DomNode{ - +impl DomNode { pub(crate) fn find_node(&self, path: &mut TreePath) -> Option { - match &self.inner{ - DomInner::StatefulComponent{comp,..} => { - log::info!("This is a stateful component, should return the element - inside relative to the child container at this path: {:?}", path); - let child_container = comp.borrow().child_container().expect("stateful component should provide the child container"); + match &self.inner { + DomInner::StatefulComponent { comp, .. } => { + log::info!( + "This is a stateful component, should return the element + inside relative to the child container at this path: {:?}", + path + ); + let child_container = comp + .borrow() + .child_container() + .expect("stateful component should provide the child container"); child_container.find_node(path) } _ => { @@ -92,14 +97,14 @@ impl DomNode{ Some(self.clone()) } else { let idx = path.remove_first(); - if let Some(children) = self.children(){ + if let Some(children) = self.children() { if let Some(child) = children.get(idx) { child.find_node(path) } else { log::warn!("There is no child at index: {idx}"); None } - }else{ + } else { log::warn!("Traversing to a childless node.."); None } @@ -125,7 +130,10 @@ impl DomNode{ tag, &self ); - log::info!("real entire dom: {:#?}", dom_node::render_real_dom_to_string(&self.as_node())); + log::info!( + "real entire dom: {:#?}", + dom_node::render_real_dom_to_string(&self.as_node()) + ); log::warn!("entire dom: {}", self.render_to_string()); } } @@ -133,7 +141,6 @@ impl DomNode{ } } - impl Program where APP: Application + 'static, @@ -157,9 +164,9 @@ where AttributeValue::EventListener(v) => { Some(DomAttrValue::EventListener(self.convert_event_listener(v))) } - AttributeValue::ComponentEventListener(v) => { - Some(DomAttrValue::EventListener(self.convert_component_event_listener(v))) - } + AttributeValue::ComponentEventListener(v) => Some(DomAttrValue::EventListener( + self.convert_component_event_listener(v), + )), AttributeValue::Empty => None, } } diff --git a/crates/core/src/dom/events.rs b/crates/core/src/dom/events.rs index 9762acdd..2cfb6310 100644 --- a/crates/core/src/dom/events.rs +++ b/crates/core/src/dom/events.rs @@ -4,6 +4,7 @@ use crate::dom::DomNode; use crate::dom::{document, window, Event}; use crate::vdom; +use crate::vdom::ComponentEventCallback; use crate::vdom::{Attribute, AttributeValue, EventCallback}; use wasm_bindgen::JsCast; #[cfg(web_sys_unstable_apis)] @@ -16,7 +17,6 @@ use web_sys::{ EventTarget, HtmlDetailsElement, HtmlElement, HtmlInputElement, HtmlSelectElement, HtmlTextAreaElement, }; -use crate::vdom::ComponentEventCallback; #[derive(Clone, Copy)] #[repr(i16)] @@ -177,7 +177,6 @@ where ) } - macro_rules! declare_events { ( $( diff --git a/crates/core/src/vdom.rs b/crates/core/src/vdom.rs index 3e07d500..c959279d 100644 --- a/crates/core/src/vdom.rs +++ b/crates/core/src/vdom.rs @@ -39,7 +39,6 @@ pub mod patch; /// for Components pub type EventCallback = Callback; - /// Mount callback is used for mounting the component into the DOM /// This requires no MSG to be emitted pub type ComponentEventCallback = Callback; diff --git a/crates/core/src/vdom/attribute.rs b/crates/core/src/vdom/attribute.rs index e1a00946..a33dbafc 100644 --- a/crates/core/src/vdom/attribute.rs +++ b/crates/core/src/vdom/attribute.rs @@ -1,9 +1,9 @@ #![allow(clippy::type_complexity)] -use derive_where::derive_where; -use indexmap::IndexMap; use crate::vdom::ComponentEventCallback; use crate::vdom::EventCallback; +use derive_where::derive_where; +use indexmap::IndexMap; pub use attribute_value::AttributeValue; pub use callback::Callback; diff --git a/crates/core/src/vdom/attribute/attribute_value.rs b/crates/core/src/vdom/attribute/attribute_value.rs index 96190baf..c29621a3 100644 --- a/crates/core/src/vdom/attribute/attribute_value.rs +++ b/crates/core/src/vdom/attribute/attribute_value.rs @@ -1,5 +1,5 @@ -use crate::{html::attributes::Style, vdom::EventCallback, vdom::Value}; use crate::vdom::ComponentEventCallback; +use crate::{html::attributes::Style, vdom::EventCallback, vdom::Value}; use derive_where::derive_where; /// Values of an attribute can be in these variants diff --git a/crates/core/src/vdom/attribute/callback.rs b/crates/core/src/vdom/attribute/callback.rs index c1a205c2..b0743331 100644 --- a/crates/core/src/vdom/attribute/callback.rs +++ b/crates/core/src/vdom/attribute/callback.rs @@ -1,6 +1,6 @@ //! Callbacks contains function that can be called at a later time. //! This is used in containing an event listener attached to an DOM element. -use std::{any::TypeId, fmt, rc::Rc, cell::RefCell}; +use std::{any::TypeId, cell::RefCell, fmt, rc::Rc}; /// A generic sized representation of a function that can be /// attached to a Node. The callback will essentially be owned by the element diff --git a/crates/core/src/vdom/map_msg.rs b/crates/core/src/vdom/map_msg.rs index 5b9df0a6..505cf986 100644 --- a/crates/core/src/vdom/map_msg.rs +++ b/crates/core/src/vdom/map_msg.rs @@ -80,7 +80,9 @@ impl AttributeValue { AttributeValue::Simple(this) => AttributeValue::Simple(this), AttributeValue::Style(this) => AttributeValue::Style(this), AttributeValue::EventListener(this) => AttributeValue::EventListener(this.map_msg(cb)), - AttributeValue::ComponentEventListener(this) => AttributeValue::ComponentEventListener(this), + AttributeValue::ComponentEventListener(this) => { + AttributeValue::ComponentEventListener(this) + } AttributeValue::Empty => AttributeValue::Empty, } } diff --git a/examples/experimentals/src/button.rs b/examples/experimentals/src/button.rs index d14cec54..26290903 100644 --- a/examples/experimentals/src/button.rs +++ b/examples/experimentals/src/button.rs @@ -70,7 +70,7 @@ impl StatefulComponent for Button { self.children.extend(children); } } - fn child_container(&self) -> Option { + fn child_container(&self) -> Option { self.external_children_node.clone() } } diff --git a/examples/experimentals/src/date_time.rs b/examples/experimentals/src/date_time.rs index 5fe7b5dd..3e7756dc 100644 --- a/examples/experimentals/src/date_time.rs +++ b/examples/experimentals/src/date_time.rs @@ -222,7 +222,9 @@ impl StatefulComponent for DateTimeWidget<()> { self.children.extend(children); } } - fn child_container(&self) -> Option { todo!()} + fn child_container(&self) -> Option { + todo!() + } } #[wasm_bindgen] diff --git a/examples/js-performance-benchmark-sauron/src/lib.rs b/examples/js-performance-benchmark-sauron/src/lib.rs index a586fc61..47376670 100644 --- a/examples/js-performance-benchmark-sauron/src/lib.rs +++ b/examples/js-performance-benchmark-sauron/src/lib.rs @@ -1,8 +1,8 @@ #![deny(warnings)] use rand::prelude::*; +use sauron::dom::component; use sauron::*; use std::cmp::min; -use sauron::dom::component; static ADJECTIVES: &[&str] = &[ "pretty", @@ -174,7 +174,7 @@ impl Application for App { Cmd::none() } - view!{ + view! {
{self.view_jumbotron()} @@ -189,10 +189,9 @@ impl Application for App { } } - -impl App{ - fn view_jumbotron(&self) -> Node{ - node!{ +impl App { + fn view_jumbotron(&self) -> Node { + node! {