Skip to content

Commit

Permalink
Impl parent-child relations in html
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 30, 2023
1 parent d12d19c commit 9d41220
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ thread_local! {
}

pub trait Platform {
fn from_str(&mut self, s: &str) -> Box<dyn AnyView>;
fn from_str(&self, s: &str) -> Box<dyn AnyView>;
}

impl Platform for () {
fn from_str(&mut self, _s: &str) -> Box<dyn AnyView> {
fn from_str(&self, _s: &str) -> Box<dyn AnyView> {
Box::new(())
}
}
Expand All @@ -81,7 +81,7 @@ pub struct BuildContext {
nodes: SlotMap<DefaultKey, Rc<RefCell<Node>>>,
children: SparseSecondaryMap<DefaultKey, Vec<DefaultKey>>,
tracked: SparseSecondaryMap<DefaultKey, Vec<DefaultKey>>,
platform: Box<dyn Platform>,
platform: Rc<dyn Platform>,
is_done: bool,
}

Expand All @@ -92,7 +92,7 @@ impl BuildContext {
nodes: Default::default(),
children: Default::default(),
tracked: Default::default(),
platform: Box::new(platform),
platform: Rc::new(platform),
is_done: false,
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ impl View for () {

impl View for &'static str {
fn view(&mut self) -> impl IntoView {
BUILD_CONTEXT
let platform = BUILD_CONTEXT
.try_with(|cx| {
let g = cx.borrow();
let mut cx = g.as_ref().unwrap().borrow_mut();
cx.platform.from_str(self).any_view()
let cx = g.as_ref().unwrap().borrow_mut();
cx.platform.clone()
})
.unwrap();
platform.from_str(self).any_view();
}
}

impl View for String {
fn view(&mut self) -> impl IntoView {
BUILD_CONTEXT
let platform = BUILD_CONTEXT
.try_with(|cx| {
let g = cx.borrow();
let mut cx = g.as_ref().unwrap().borrow_mut();
cx.platform.from_str(self).any_view()
let cx = g.as_ref().unwrap().borrow_mut();
cx.platform.clone()
})
.unwrap();
platform.from_str(self).any_view();
}
}
9 changes: 6 additions & 3 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
html::{Builder, Html, HtmlParent, HtmlPlatform},
use_provider, Child, IntoView, Platform, Tree,
use_context, use_provider, Child, IntoView, Platform, Tree,
};
use std::{cell::RefCell, rc::Rc};
use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -78,12 +78,15 @@ impl HtmlPlatform for WebHtml {
pub struct Web;

impl Platform for Web {
fn from_str(&mut self, s: &str) -> Box<dyn crate::AnyView> {
fn from_str(&self, s: &str) -> Box<dyn crate::AnyView> {
let cx = WebContext::current();
let inner = cx.inner.borrow_mut();
let node = inner.document.create_text_node(s);

inner.body.append_child(&node).unwrap();
let parent = use_context::<HtmlParent>().map(|parent| parent.get().node.clone());

let parent = parent.as_ref().unwrap_or(inner.body.unchecked_ref());
parent.append_child(&node).unwrap();

Box::new(())
}
Expand Down

0 comments on commit 9d41220

Please sign in to comment.