-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Receive Event in callback (Part 1) (#16)
* Reorder declaration of TemplateResult * Refactor element attribute parsing * Add compile test for unknown directive * Add hello world example Demonstrates data binding * Update README.md * Fix broken test
- Loading branch information
Showing
17 changed files
with
207 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ members = [ | |
"maple-core-macro", | ||
"examples/components", | ||
"examples/counter", | ||
"examples/hello", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
authors = ["Luke Chu <37006668+lukechu10@users.noreply.github.com>"] | ||
edition = "2018" | ||
name = "hello" | ||
version = "0.1.0" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
console_error_panic_hook = "0.1.6" | ||
console_log = "0.2.0" | ||
log = "0.4.14" | ||
maple-core = {path = "../../maple-core"} | ||
wasm-bindgen = "0.2.71" | ||
|
||
[dependencies.web-sys] | ||
features = ["HtmlInputElement", "InputEvent"] | ||
version = "0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<title>Hello World!</title> | ||
|
||
<style> | ||
body { | ||
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; | ||
} | ||
</style> | ||
</head> | ||
<body></body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![allow(non_snake_case)] | ||
|
||
use maple_core::prelude::*; | ||
use wasm_bindgen::JsCast; | ||
use web_sys::{Event, HtmlInputElement}; | ||
|
||
fn main() { | ||
console_error_panic_hook::set_once(); | ||
console_log::init_with_level(log::Level::Debug).unwrap(); | ||
|
||
let (name, set_name) = create_signal(String::new()); | ||
|
||
let displayed_name = create_memo(move || { | ||
if *name() == "" { | ||
"World".to_string() | ||
} else { | ||
name().as_ref().clone() | ||
} | ||
}); | ||
|
||
let handle_change = move |event: Event| { | ||
set_name( | ||
event | ||
.target() | ||
.unwrap() | ||
.dyn_into::<HtmlInputElement>() | ||
.unwrap() | ||
.value(), | ||
); | ||
}; | ||
|
||
let root = template! { | ||
div { | ||
h1 { | ||
# "Hello " | ||
# displayed_name() | ||
# "!" | ||
} | ||
|
||
input(on:input=handle_change) | ||
} | ||
}; | ||
|
||
render(root); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use syn::ext::IdentExt; | ||
use syn::parse::{Parse, ParseStream}; | ||
use syn::punctuated::Punctuated; | ||
use syn::token::Paren; | ||
use syn::{parenthesized, Expr, Ident, Result, Token}; | ||
|
||
pub enum AttributeType { | ||
/// Syntax: `name`. | ||
DomAttribute { name: String }, | ||
/// Syntax: `on:name`. | ||
Event { name: String }, | ||
} | ||
|
||
impl Parse for AttributeType { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let ident = input.call(Ident::parse_any)?; | ||
let ident_str = ident.to_string(); | ||
|
||
if input.peek(Token![:]) { | ||
let _colon: Token![:] = input.parse()?; | ||
match ident_str.as_str() { | ||
"on" => { | ||
let event_name = input.call(Ident::parse_any)?; | ||
Ok(Self::Event { | ||
name: event_name.to_string(), | ||
}) | ||
} | ||
_ => Err(syn::Error::new_spanned( | ||
ident, | ||
format!("unknown directive `{}`", ident_str), | ||
)), | ||
} | ||
} else { | ||
Ok(Self::DomAttribute { name: ident_str }) | ||
} | ||
} | ||
} | ||
|
||
pub struct Attribute { | ||
pub ty: AttributeType, | ||
pub equals_token: Token![=], | ||
pub expr: Expr, | ||
} | ||
|
||
impl Parse for Attribute { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
Ok(Self { | ||
ty: input.parse()?, | ||
equals_token: input.parse()?, | ||
expr: input.parse()?, | ||
}) | ||
} | ||
} | ||
|
||
pub struct AttributeList { | ||
pub paren_token: Paren, | ||
pub attributes: Punctuated<Attribute, Token![,]>, | ||
} | ||
|
||
impl Parse for AttributeList { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let content; | ||
let paren_token = parenthesized!(content in input); | ||
|
||
let attributes = content.parse_terminated(Attribute::parse)?; | ||
|
||
Ok(Self { | ||
paren_token, | ||
attributes, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod attributes; | ||
mod children; | ||
mod component; | ||
mod element; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.