This commit is contained in:
Greg Johnston 2022-09-20 14:31:06 -04:00
parent d4204241e8
commit 1695f129e2
8 changed files with 34 additions and 60 deletions

View file

@ -141,11 +141,6 @@ pub fn insert(
.unwrap_or(Child::Null);
let mut value = (f.borrow_mut())();
log::debug!(
"rendering Child::Fn on {} — value is {:?}",
parent.node_name(),
value
);
if current != value {
while let Child::Fn(f) = value {
@ -252,15 +247,8 @@ pub fn insert_expression(
Child::Nodes(new_nodes.to_vec())
}
} else {
log::debug!("branch C clean_children on {}", parent.node_name());
clean_children(&parent, Child::Null, &Marker::NoChildren, None);
log::debug!("branch C cleaned children on {}", parent.node_name());
append_nodes(&parent, new_nodes, before);
log::debug!(
"branch C append_nodes on {} {:?}",
parent.node_name(),
new_nodes
);
Child::Nodes(new_nodes.to_vec())
}
}
@ -284,10 +272,10 @@ pub fn insert_str(
multi: bool,
current: Child,
) -> Child {
log::debug!(
/* log::debug!(
"insert_str {data:?} on {} before {before:?} — multi = {multi} and current = {current:?}",
parent.node_name()
);
); */
if multi {
if let Child::Node(node) = &current {
@ -301,16 +289,8 @@ pub fn insert_str(
}
} else {
let node = if let Child::Nodes(nodes) = &current {
log::debug!(
"ok here we are nodes = {:?}!",
nodes
.iter()
.map(|n| (n.node_name(), n.node_value()))
.collect::<Vec<_>>()
);
if let Some(node) = nodes.get(0) {
if node.node_type() == 3 {
log::debug!("setting node data to {data:?}");
node.unchecked_ref::<web_sys::Text>().set_data(data);
node.clone()
} else {
@ -366,7 +346,7 @@ fn clean_children(
marker: &Marker,
replacement: Option<web_sys::Node>,
) -> Child {
log::debug!("clean_children on {} with current = {current:?} and marker = {marker:#?} and replacement = {replacement:#?}", parent.node_name());
//log::debug!("clean_children on {} with current = {current:?} and marker = {marker:#?} and replacement = {replacement:#?}", parent.node_name());
if marker == &Marker::NoChildren {
parent.set_text_content(Some(""));
@ -383,32 +363,14 @@ fn clean_children(
Child::Node(insert_before(parent, &node, marker.as_some_node()))
} else {
let mut inserted = false;
log::debug!("iterating over current nodes");
//log::debug!("node = {} => {:?}", node.node_name(), node.node_value());
for (idx, el) in nodes.iter().enumerate().rev() {
//log::debug!("{idx}: {} => {:?}", el.node_name(), el.node_value());
if &node != el {
let is_parent =
el.parent_node() == Some(parent.clone().unchecked_into());
//log::debug!("is_parent = {is_parent}");
if !inserted && idx == 0 {
//log::debug!("!insert && idx == 0");
if is_parent {
/* log::debug!(
"replacing child {}/{:?} with {}/{:?}",
el.node_name(),
el.node_value(),
node.node_name(),
node.node_value()
); */
replace_child(parent, &node, el);
} else {
/* log::debug!(
"inserting {:?} before {:?} on {:?}",
node.node_name(),
marker.as_some_node().map(|n| n.node_name()),
parent.outer_html()
); */
node = insert_before(parent, &node, marker.as_some_node());
}
} else {

View file

@ -337,7 +337,13 @@ fn attr_to_tokens(
};
let value = match &node.value {
Some(expr) => match expr {
syn::Expr::Lit(_) => AttributeValue::Static(node.value_as_string().unwrap()),
syn::Expr::Lit(expr_lit) => {
if matches!(expr_lit.lit, syn::Lit::Str(_)) {
AttributeValue::Static(node.value_as_string().unwrap())
} else {
AttributeValue::Dynamic(expr)
}
}
_ => AttributeValue::Dynamic(expr),
},
None => AttributeValue::Empty,
@ -466,7 +472,7 @@ fn attr_to_tokens(
// For client-side rendering, dynamic attributes don't need to be rendered in the template
// They'll immediately be set synchronously before the cloned template is mounted
expressions.push(quote_spanned! {
span => leptos_dom::attribute(cx, #el_id.unchecked_ref(), #name, #value.into_attribute(cx))
span => leptos_dom::attribute(cx, #el_id.unchecked_ref(), #name, {#value}.into_attribute(cx))
});
}
}

View file

@ -7,7 +7,6 @@ where
T: Clone + 'static,
{
let id = value.type_id();
log::debug!("[Context] provide_context {} => {id:?}", type_name::<T>(),);
cx.runtime.scope(cx.id, |scope_state| {
scope_state
.contexts
@ -21,7 +20,6 @@ where
T: Clone + 'static,
{
let id = TypeId::of::<T>();
log::debug!("[Context] use_context {} => {id:?}", type_name::<T>(),);
cx.runtime.scope(cx.id, |scope_state| {
let contexts = scope_state.contexts.borrow();
let local_value = contexts.get(&id).and_then(|val| val.downcast_ref::<T>());

View file

@ -23,7 +23,6 @@ typed-builder = "0.10"
js-sys = { version = "0.3", optional = true }
wasm-bindgen = "0.2"
[dependencies.web-sys]
version = "0.3"
features = [
@ -43,7 +42,6 @@ features = [
]
[features]
default = ["csr"]
csr = ["leptos_core/csr", "leptos_dom/csr", "leptos_macro/csr", "leptos_reactive/csr", "dep:js-sys"]
hydrate = ["leptos_core/hydrate", "leptos_dom/hydrate", "leptos_macro/hydrate", "leptos_reactive/hydrate", "dep:js-sys"]
ssr = ["leptos_core/ssr", "leptos_dom/ssr", "leptos_macro/ssr", "leptos_reactive/ssr", "dep:url", "dep:regex"]

View file

@ -33,6 +33,7 @@ where
children,
} = props;
#[cfg(any(feature = "csr", feature = "hydrate"))]
let on_submit = move |ev: web_sys::Event| {
if ev.default_prevented() {
return;

View file

@ -269,13 +269,12 @@ impl RouterContext {
});
// handle all click events on anchor tags
if cfg!(any(feature = "csr", feature = "hydrate")) {
#[cfg(any(feature = "csr", feature = "hydrate"))]
leptos_dom::window_event_listener("click", {
let inner = Rc::clone(&inner);
move |ev| inner.clone().handle_anchor_click(ev)
});
// TODO on_cleanup remove event listener
}
Self { inner }
}
@ -379,8 +378,8 @@ impl RouterContextInner {
}
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
pub(crate) fn handle_anchor_click(self: Rc<Self>, ev: web_sys::Event) {
use leptos_dom::wasm_bindgen::JsCast;
let ev = ev.unchecked_into::<web_sys::MouseEvent>();
if ev.default_prevented()
|| ev.button() != 0

View file

@ -12,7 +12,10 @@ where
let data = match route.data().as_ref() {
Some(data) => data,
None => {
debug_warn!("(use_loader) could not find any data for route");
debug_warn!(
"(use_loader) could not find any data for route {}",
route.path()
);
panic!()
}
};

View file

@ -19,9 +19,11 @@ pub trait History {
fn navigate(&self, loc: &LocationChange);
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct BrowserIntegration {}
#[cfg(any(feature = "csr", feature = "hydrate"))]
impl BrowserIntegration {
fn current() -> LocationChange {
let loc = leptos_dom::location();
@ -36,6 +38,7 @@ impl BrowserIntegration {
}
}
#[cfg(any(feature = "csr", feature = "hydrate"))]
impl History for BrowserIntegration {
fn location(&self, cx: Scope) -> ReadSignal<LocationChange> {
let (location, set_location) = create_signal(cx, Self::current());
@ -75,6 +78,7 @@ impl History for BrowserIntegration {
fn navigate(&self, loc: &LocationChange) {
log::debug!("[BrowserIntegration::navigate] {loc:#?}");
let history = leptos_dom::window().history().unwrap();
if loc.replace {
history
.replace_state_with_url(&loc.state.to_js_value(), "", Some(&loc.value))
@ -87,10 +91,12 @@ impl History for BrowserIntegration {
// scroll to el
if let Ok(hash) = leptos_dom::location().hash() {
if !hash.is_empty() {
let hash = &hash[1..];
let el = leptos_dom::document()
.query_selector(&format!("#{}", hash))
.unwrap();
let hash = js_sys::decode_uri(&hash[1..])
.ok()
.and_then(|decoded| decoded.as_string())
.unwrap_or(hash);
let el = leptos_dom::document().get_element_by_id(&hash);
log::debug!("el to scroll to = {hash:?} => {el:?}");
if let Some(el) = el {
el.scroll_into_view()
} else if loc.scroll {
@ -98,5 +104,6 @@ impl History for BrowserIntegration {
}
}
}
log::debug!("[BrowserIntegration::navigate 5]");
}
}