浏览代码

Feat: web example + cli writes to browser screen!

Jonathan Kelley 4 年之前
父节点
当前提交
8439994859
共有 5 个文件被更改,包括 68 次插入4 次删除
  1. 1 1
      packages/cli/Cargo.toml
  2. 1 1
      packages/cli/src/logging.rs
  3. 2 1
      packages/web/Cargo.toml
  4. 50 0
      packages/web/examples/basic.rs
  5. 14 1
      packages/web/src/lib.rs

+ 1 - 1
packages/cli/Cargo.toml

@@ -12,7 +12,7 @@ description = "CLI tool for developing, testing, and publishing Dioxus apps"
 thiserror = "1.0.23"
 thiserror = "1.0.23"
 log = "0.4.13"
 log = "0.4.13"
 fern = { version = "0.6.0", features = ["colored"] }
 fern = { version = "0.6.0", features = ["colored"] }
-wasm-bindgen-cli-support = "0.2.69"
+wasm-bindgen-cli-support = "0.2.70"
 anyhow = "1.0.38"
 anyhow = "1.0.38"
 argh = "0.1.4"
 argh = "0.1.4"
 serde = "1.0.120"
 serde = "1.0.120"

+ 1 - 1
packages/cli/src/logging.rs

@@ -36,7 +36,7 @@ pub fn set_up_logging() {
         // field which defaults to the module path but can be overwritten with the `target`
         // field which defaults to the module path but can be overwritten with the `target`
         // parameter:
         // parameter:
         // `info!(target="special_target", "This log message is about special_target");`
         // `info!(target="special_target", "This log message is about special_target");`
-        .level_for("diopack", log::LevelFilter::Info)
+        .level_for("dioxus-cli", log::LevelFilter::Info)
         // .level_for("pretty_colored", log::LevelFilter::Trace)
         // .level_for("pretty_colored", log::LevelFilter::Trace)
         // output to stdout
         // output to stdout
         .chain(std::io::stdout())
         .chain(std::io::stdout())

+ 2 - 1
packages/web/Cargo.toml

@@ -9,7 +9,8 @@ edition = "2018"
 [dependencies]
 [dependencies]
 dioxus-core = { path = "../core" }
 dioxus-core = { path = "../core" }
 js-sys = "0.3"
 js-sys = "0.3"
-wasm-bindgen = "0.2.70"
+wasm-bindgen = "0.2.69"
+# wasm-bindgen = "0.2.70"
 lazy_static = "1.4.0"
 lazy_static = "1.4.0"
 wasm-bindgen-futures = "0.4.20"
 wasm-bindgen-futures = "0.4.20"
 futures = "0.3.12"
 futures = "0.3.12"

+ 50 - 0
packages/web/examples/basic.rs

@@ -0,0 +1,50 @@
+//! basic example that renders a simple domtree to the page :)
+//!
+//!
+//!
+use dioxus_core::prelude::bumpalo::Bump;
+use dioxus_core::prelude::*;
+use dioxus_web::*;
+
+fn main() {
+    wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
+    log::debug!("Hello world, from the app");
+    WebsysRenderer::simple_render(html! {
+
+        // Body
+        <div class="flex items-center justify-center flex-col">
+            <div class="flex items-center justify-center">
+                <div class="flex flex-col bg-white rounded p-4 w-full max-w-xs">
+                    // Title
+                    <div class="font-bold text-xl">
+                        // {format!("Fibonacci Calculator: n = {}",n)}
+                        "Fibonacci Calculator: n = {}"
+                    </div>
+
+                    // Subtext / description
+                    <div class="text-sm text-gray-500">
+                        // {format!("Calculated in {} nanoseconds",duration)}
+                        // {format!("Calculated in {} nanoseconds",duration)}
+                        "Calculated in {} nanoseconds"
+                    </div>
+
+                    <div class="flex flex-row items-center justify-center mt-6">
+                        // Main number
+                        <div class="font-medium text-6xl">
+                            // {format!("{}",fib_n)}
+                        </div>
+                    </div>
+
+                    // Try another
+                    <div class="flex flex-row justify-between mt-6">
+                        // <a href=format!("http://localhost:8080/fib/{}", other_fib_to_try) class="underline">
+                            "Click to try another number"
+                            // {"Click to try another number"}
+                        // </a>
+                    </div>
+                </div>
+            </div>
+        </div>
+
+    });
+}

+ 14 - 1
packages/web/src/lib.rs

@@ -102,15 +102,27 @@ impl WebsysRenderer {
         Ok(())
         Ok(())
     }
     }
 
 
-    fn simple_render(tree: impl for<'a> Fn(&'a Bump) -> VNode<'a>) {
+    pub fn simple_render(tree: impl for<'a> Fn(&'a Bump) -> VNode<'a>) {
         let bump = Bump::new();
         let bump = Bump::new();
 
 
         let old = html! { <div> </div> }(&bump);
         let old = html! { <div> </div> }(&bump);
+
+        let created = create_dom_node(&old);
+        let root_node = created.node;
+
         let new = tree(&bump);
         let new = tree(&bump);
 
 
         let mut machine = DiffMachine::new();
         let mut machine = DiffMachine::new();
 
 
         let patches = machine.diff(&old, &new);
         let patches = machine.diff(&old, &new);
+        log::info!("There are {:?} patches", patches.len());
+
+        let root2 = root_node.clone();
+        patch(root_node, &patches).expect("Failed to simple render");
+        let document = web_sys::window().unwrap().document().unwrap();
+
+        document.body().unwrap().append_child(&root2);
+        log::info!("Succesfully patched the dom");
     }
     }
 }
 }
 
 
@@ -454,6 +466,7 @@ pub fn create_element_node(node: &dioxus_core::nodes::VElement) -> CreatedNode<E
     let mut previous_node_was_text = false;
     let mut previous_node_was_text = false;
 
 
     node.children.iter().for_each(|child| {
     node.children.iter().for_each(|child| {
+        log::info!("Patching child");
         match child {
         match child {
             VNode::Text(text_node) => {
             VNode::Text(text_node) => {
                 let current_node = element.as_ref() as &web_sys::Node;
                 let current_node = element.as_ref() as &web_sys::Node;