Skip to content

Commit 2538f31

Browse files
committed
Saving changes before reverting
1 parent 732569e commit 2538f31

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/dom_types.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ impl Attrs {
372372
}
373373
}
374374

375+
// todo temp duplication of trait and main methods for vals
376+
impl crate::vdom::Attrs for Attrs {
377+
fn vals(self) -> HashMap<String, String> {
378+
self.vals
379+
}
380+
}
381+
375382
impl ToString for Attrs {
376383
/// Create an HTML-compatible string representation
377384
fn to_string(&self) -> String {
@@ -423,6 +430,13 @@ impl Style {
423430
}
424431
}
425432

433+
// todo temp duplication of trait and main methods for vals
434+
impl crate::vdom::Style for Style {
435+
fn vals(self) -> HashMap<String, String> {
436+
self.vals
437+
}
438+
}
439+
426440
impl ToString for Style {
427441
/// Output style as a string, as would be set in the DOM as the attribute value
428442
/// for 'style'. Eg: "display: flex; font-size: 1.5em"
@@ -535,7 +549,7 @@ macro_rules! make_tags {
535549
}
536550

537551
impl ToString for Tag {
538-
pub fn to_string(&self) -> String {
552+
fn to_string(&self) -> String {
539553
match self {
540554
Tag::Custom(name) => &name,
541555
$ (
@@ -798,7 +812,7 @@ impl<Ms: Clone + 'static> El<Ms> {
798812
fn _html(&self) -> String {
799813
let text = self.text.clone().unwrap_or_default();
800814

801-
let opening = String::from("<") + self.tag.as_str() + &self.attrs.to_string() +
815+
let opening = String::from("<") + &self.tag.to_string() + &self.attrs.to_string() +
802816
" style=\"" + &self.style.to_string() + ">\n";
803817

804818
let inner = self.children.iter().fold(String::new(), |result, child| result + &child._html());
@@ -925,7 +939,7 @@ impl <Ms: Clone + 'static>crate::vdom::DomEl<Ms> for El<Ms> {
925939
fn id(self) -> Option<u32> {
926940
self.id
927941
}
928-
fn raw_html(self) -> boolean {
942+
fn raw_html(self) -> bool {
929943
self.raw_html
930944
}
931945
fn namespace(self) -> Option<Namespace> {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ pub use crate::{
2727
fetch::{Method, Request, spawn_local},
2828
websys_bridge::{to_input, to_kbevent, to_mouse_event, to_select, to_textarea, to_html_el},
2929
routing::push_route,
30-
vdom::{App, run}
30+
util::{document, window},
31+
vdom::{App, run},
3132
};
3233

3334
/// Create an element flagged in a way that it will not be rendered. Useful

src/vdom.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ pub fn setup_els<Ms>(document: &web_sys::Document, el_vdom: &mut impl DomEl<Ms>,
185185
let mut id = active_id;
186186
el_vdom.set_id(Some(id));
187187
id += 1; // Raise the id after each element we process.
188-
el_vdom.nest_level() = Some(active_level);
188+
// todo perhaps put back
189+
// el_vdom.nest_level() = Some(active_level);
189190

190191
// Create the web_sys element; add it to the working tree; store it in
191192
// its corresponding vdom El.
@@ -512,6 +513,14 @@ pub fn run<Ms, Mdl, E>(
512513
panic::set_hook(Box::new(console_error_panic_hook::hook));
513514
}
514515

516+
pub trait Attrs: PartialEq + ToString {
517+
fn vals(self) -> HashMap<String, String>;
518+
}
519+
520+
pub trait Style: PartialEq + ToString {
521+
fn vals(self) -> HashMap<String, String>;
522+
}
523+
515524
pub trait Listener<Ms>: Sized {
516525
fn attach<T: AsRef<web_sys::EventTarget>>(&mut self, el_ws: &T, mailbox: Mailbox<Ms>);
517526
fn detach<T: AsRef<web_sys::EventTarget>>(&self, el_ws: &T);
@@ -520,10 +529,10 @@ pub trait Listener<Ms>: Sized {
520529
/// WIP towards a modular VDOM
521530
/// Assumes dependency on web_sys.
522531
// todo: Do we need <Ms> ?
523-
pub trait DomEl<Ms>: Sized {
532+
pub trait DomEl<Ms>: Sized + PartialEq {
524533
type Tg: PartialEq + ToString; // todo tostring
525-
type At: PartialEq + ToString;
526-
type St: PartialEq + ToString;
534+
type At: Attrs;
535+
type St: Style;
527536
type Ls: Listener<Ms>;
528537
type Tx: PartialEq + ToString + Clone + Default;
529538

@@ -539,7 +548,7 @@ pub trait DomEl<Ms>: Sized {
539548
fn will_unmount(self) -> Option<Box<FnMut(&web_sys::Element)>>;
540549
fn websys_el(self) -> Option<web_sys::Element>;
541550
fn id(self) -> Option<u32>;
542-
fn raw_html(self) -> boolean;
551+
fn raw_html(self) -> bool;
543552
// todo tying to dom_types is temp - defeats the urpose of the trait
544553
fn namespace(self) -> Option<crate::dom_types::Namespace>;
545554

src/websys_bridge.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This file contains interactions with web_sys.
22
use wasm_bindgen::JsCast;
33

4-
use crate::vdom::DomEl;
4+
use crate::vdom::{DomEl, Attrs, Style};
55

66
// todo: How coupled should this be to Seed, vdom, and dom_types?
77

@@ -116,19 +116,19 @@ pub fn make_websys_el<Ms>(el_vdom: &impl DomEl<Ms>, document: &web_sys::Document
116116
where Ms: Clone,
117117
{
118118
// Create the DOM-analog element; it won't render until attached to something.
119-
let tag = el_vdom.tag().as_str();
119+
let tag = &el_vdom.tag().to_string();
120120
let el_ws = match el_vdom.namespace() {
121121
Some(ref ns) => document.create_element_ns(Some(ns.as_str()), tag).expect("Problem creating web-sys El"),
122122
None => document.create_element(tag).expect("Problem creating web-sys El")
123123
};
124124

125-
for (name, val) in &el_vdom.attrs().vals {
125+
for (name, val) in &el_vdom.attrs().vals() {
126126
set_attr_shim(&el_ws, name, val);
127127
}
128128

129129
// Style is just an attribute in the actual Dom, but is handled specially in our vdom;
130130
// merge the different parts of style here.
131-
if el_vdom.style().vals.keys().len() > 0 {
131+
if el_vdom.style().vals().keys().len() > 0 {
132132
el_ws.set_attribute("style", &el_vdom.style().to_string()).expect("Problem setting style");
133133
}
134134

@@ -206,7 +206,7 @@ pub fn patch_el_details<Ms>(
206206
}
207207

208208
if old.attrs() != new.attrs() {
209-
for (key, new_val) in &new.attrs().vals {
209+
for (key, new_val) in &new.attrs().vals() {
210210
match old.attrs().vals().get(key) {
211211
Some(old_val) => {
212212
// The value's different
@@ -218,7 +218,7 @@ pub fn patch_el_details<Ms>(
218218
}
219219
}
220220
// Remove attributes that aren't in the new vdom.
221-
for name in old.attrs().vals.keys() {
221+
for name in old.attrs().vals().keys() {
222222
if new.attrs().vals().get(name).is_none() {
223223
old_el_ws.remove_attribute(name).expect("Removing an attribute");
224224
}
@@ -240,7 +240,7 @@ pub fn patch_el_details<Ms>(
240240
// Text is stored in special Text nodes that don't have a direct-relation to
241241
// the vdom.
242242

243-
let text = new.text().clone().unwrap_or_default();
243+
let text = new.text().clone().unwrap_or_default().to_string();
244244

245245
if new.raw_html() {
246246
old_el_ws.set_inner_html(&text)

0 commit comments

Comments
 (0)