Skip to content

Commit 46bfb9c

Browse files
committed
Added initial-routing
1 parent e0719fe commit 46bfb9c

24 files changed

+378
-502
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ wasm-bindgen-futures = "0.3.6"
2424

2525
# Markdown conversion
2626
pulldown-cmark = "^0.2.0"
27-
#syntect = "^3.0.2" // todo bugs out
28-
regex = "^1.1.0"
2927

3028
[dependencies.web-sys]
3129
version = "0.3.4"
@@ -46,6 +44,7 @@ features = [
4644
"Node",
4745
"NodeList",
4846
"Performance",
47+
"PopStateEvent",
4948
"Request",
5049
"RequestInit",
5150
"RequestMode",

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2018 David O'Connor
1+
Copyright (c) 2019 David O'Connor
22

33
Permission is hereby granted, free of charge, to any
44
person obtaining a copy of this software and associated

README.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ And wasm-bindgen: `cargo install wasm-bindgen-cli`
2323
### The theoretical minimum
2424
To start, clone [This quickstart repo](https://github.com/David-OConnor/seed-quickstart),
2525
run `build.sh` or `build.ps1` in a terminal, then start a dev server that supports WASM.
26-
For example, with [Python](https://www.python.org/downloads/) installed, run `python server.py`.
27-
(Linux users may need to run `python3 server.py`.)
26+
For example, with [Python](https://www.python.org/downloads/) installed, run `python serve.py`.
27+
(Linux users may need to run `python3 serve.py`.)
2828
Once you change your package name, you'll
2929
need to tweak the html file and build script, as described below.
3030

@@ -170,13 +170,13 @@ fn view(model: Model) -> El<Msg> {
170170
// When passing numerical values to style!, "px" is implied.
171171
"border" => "2px solid #004422"; "padding" => 20
172172
},
173-
// We can use normal Rust code and comments in the view.
174-
h3![ format!("{} {}{} so far", model.count, model.what_we_count, plural) ],
175-
button![ simple_ev("click", Msg::Increment), "+" ],
176-
button![ simple_ev("click", Msg::Decrement), "-" ],
173+
// We can use normal Rust code and comments in the view.
174+
h3![ format!("{} {}{} so far", model.count, model.what_we_count, plural) ],
175+
button![ simple_ev("click", Msg::Increment), "+" ],
176+
button![ simple_ev("click", Msg::Decrement), "-" ],
177177

178-
// Optionally-displaying an element
179-
if model.count >= 10 { h2![ style!{"padding" => 50}, "Nice!" ] } else { seed::empty() }
178+
// Optionally-displaying an element
179+
if model.count >= 10 { h2![ style!{"padding" => 50}, "Nice!" ] } else { seed::empty() }
180180

181181
],
182182
success_level(model.count), // Incorporating a separate component
@@ -212,8 +212,8 @@ The Quickstart repo includes these, but you'll still need to do the rename. You
212212
`./build.sh` or `.\build.ps1`
213213

214214
For development, you can view your app using a shimmed Python dev server described above.
215-
(Set up [this mime-type shim](https://github.com/David-OConnor/seed-quickstart/blob/master/server.py)
216-
from the quickstart repo, and run `python server.py`).
215+
(Set up [this mime-type shim](https://github.com/David-OConnor/seed-quickstart/blob/master/serve.py)
216+
from the quickstart repo, and run `python serve.py`).
217217

218218
For details, reference [the wasm-bindgen documention](https://rustwasm.github.io/wasm-bindgen/whirlwind-tour/basic-usage.html).
219219
In the future, I'd like the build script and commands above to be replaced by [wasm-pack](https://github.com/rustwasm/wasm-pack).
@@ -468,7 +468,7 @@ extern crate seed;
468468

469469
div![]
470470
```
471-
These macros accept any combination (0 or 1 per) of the following parameters:
471+
These macros accept any combination of the following parameters:
472472
- One [Attrs](https://docs.rs/seed/0.1.6/seed/dom_types/struct.Attrs.html) struct
473473
- One [Style](https://docs.rs/seed/0.1.6/seed/dom_types/struct.Style.html) struct
474474
- One or more [Listener](https://docs.rs/seed/0.1.6/seed/dom_types/struct.Listener.html) structs, which handle events
@@ -525,10 +525,10 @@ the same one more than once:
525525

526526
Setting an InputElement's `checked` property is done through normal attributes:
527527
```rust
528-
input![ attrs!{"type" => "checkbox"; "checked" => true ]
528+
input![ attrs!{"type" => "checkbox"; "checked" => true} ]
529529
```
530530

531-
To edit Attrs or Styles you've created, you can edit their .vals HashMap. To add
531+
To change Attrs or Styles you've created, edit their .vals HashMap. To add
532532
a new part to them, use their .add method:
533533
```rust
534534
let mut attributes = attrs!{};
@@ -559,6 +559,19 @@ fn view(model: Model) -> El<Msg> {
559559
}
560560
```
561561

562+
We can combine Attrs and Style instances using their `merge` methods, which take
563+
an &Attrs and &Style respectively. This can be used to compose styles from reusable parts.
564+
Example:
565+
```rust
566+
let base_style = !style{"color" => "lavender"};
567+
568+
div![
569+
h1![ &base_style.merge(&style!{"grid-row" => "1 / 2"}) "First row" ],
570+
h1![ &base_style.merge(&style!{"grid-row" => "2 / 3"}) "Second row" ],
571+
]
572+
```
573+
574+
562575
Overall: we leverage of Rust's strict type system to flexibly-create the view
563576
using normal Rust code.
564577

@@ -885,10 +898,6 @@ function run() {
885898
```
886899
Note that you don't need to pass your Msg enum; it's inferred from the update function.
887900

888-
### Comments in the view
889-
The Element-creation macros used to create views are normal Rust code, you can
890-
use comments in them normally: either on their own line, or in line.
891-
892901

893902
### Logging in the web browser
894903
To output to the web browser's console (ie `console.log()` in JS), use `web_sys::console_log1`,
@@ -942,11 +951,12 @@ let data = serde_json::from_str(&loaded_serialized).unwrap();
942951

943952
```
944953

945-
### Display markdown
954+
### Display markdown and raw HTML
946955
Seed supports creating elements from markdown text, using [pulldown-cmark](https://github.com/raphlinus/pulldown-cmark)
947956
internally. Use the [El::from_markdown()](https://docs.rs/seed/0.1.6/seed/dom_types/struct.El.html#method.from_markdown)
948957
method to create an element that accepts a markdown &str as its only parameter, and displays
949-
it normally as html.
958+
it normally as html. Note that it does not support syntax highlighting. You can render raw HTML with `El::from_html(html)`, where `html` is a
959+
&str of HTML.
950960

951961
Example:
952962
```rust
@@ -958,13 +968,26 @@ fn view(model: Model) -> El<Msg> {
958968
959969
Let's set the existence-of-God issue aside for a later volume,
960970
and just [learn to code](https://play.rust-lang.org/).
961-
";
971+
"
972+
;
973+
974+
let html =
975+
"
976+
<div>
977+
<p>It is a truth universally acknowledged, that a single man in
978+
possession of a good fortune, must be in want of a good time./p>
979+
</div>
980+
"
981+
;
962982

963983
div![
964984
El::from_markdown(markdown)
985+
El::from_html(html)
965986
]
966987
}
967988

989+
990+
968991
```
969992

970993
### Building a release version
@@ -1016,8 +1039,8 @@ of your familiarity with Rust.
10161039

10171040
- Complete documentation that always matches the current version. Getting examples working, and
10181041
starting a project should be painless, and require nothing beyond this guide.
1019-
1020-
- An API that's easy to read, write, and understand.
1042+
1043+
- Concise, flexibilty vew syntax that's easy to read and write.
10211044

10221045

10231046
### A note on view syntax
@@ -1081,10 +1104,10 @@ You may choose
10811104
this approach over Elm if you're already comfortable with Rust, want the performance
10821105
benefits, or don't want to code business logic in a purely-functional langauge.
10831106

1084-
Compared to React, for example, you may appreciate the consistency of how to write apps:
1107+
Compared with React, you may appreciate the consistency of how to write apps:
10851108
There's no distinction between logic and display code; no restrictions on comments;
10861109
no distinction between components and normal functions. The API is
1087-
flexible, and avoids the OOP boilerplate.
1110+
flexible, and avoids OOP boilerplate.
10881111

10891112
I also hope that config, building, and dependency-management is cleaner with Cargo and
10901113
wasm-bindgen than with npm.

examples/counter/pkg/counter.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ function GetOwnOrInheritedPropertyDescriptor(obj, id) {
141141
return {}
142142
}
143143

144+
const __widl_f_set_inner_html_Element_target = GetOwnOrInheritedPropertyDescriptor(typeof Element === 'undefined' ? null : Element.prototype, 'innerHTML').set || function() {
145+
throw new Error(`wasm-bindgen: Element.innerHTML does not exist`);
146+
};
147+
148+
__exports.__widl_f_set_inner_html_Element = function(arg0, arg1, arg2) {
149+
let varg1 = getStringFromWasm(arg1, arg2);
150+
__widl_f_set_inner_html_Element_target.call(getObject(arg0), varg1);
151+
};
152+
144153
const __widl_f_target_Event_target = GetOwnOrInheritedPropertyDescriptor(typeof Event === 'undefined' ? null : Event.prototype, 'target').get || function() {
145154
throw new Error(`wasm-bindgen: Event.target does not exist`);
146155
};
@@ -259,6 +268,21 @@ __exports.__widl_f_value_HTMLTextAreaElement = function(ret, arg0) {
259268

260269
};
261270

271+
const __widl_f_back_History_target = typeof History === 'undefined' ? null : History.prototype.back || function() {
272+
throw new Error(`wasm-bindgen: History.back does not exist`);
273+
};
274+
275+
__exports.__widl_f_back_History = function(arg0, exnptr) {
276+
try {
277+
__widl_f_back_History_target.call(getObject(arg0));
278+
} catch (e) {
279+
const view = getUint32Memory();
280+
view[exnptr / 4] = 1;
281+
view[exnptr / 4 + 1] = addHeapObject(e);
282+
283+
}
284+
};
285+
262286
const __widl_f_append_child_Node_target = typeof Node === 'undefined' ? null : Node.prototype.appendChild || function() {
263287
throw new Error(`wasm-bindgen: Node.appendChild does not exist`);
264288
};
@@ -333,6 +357,18 @@ __exports.__widl_f_length_NodeList = function(arg0) {
333357
return __widl_f_length_NodeList_target.call(getObject(arg0));
334358
};
335359

360+
__exports.__widl_instanceof_PopStateEvent = function(idx) {
361+
return getObject(idx) instanceof PopStateEvent ? 1 : 0;
362+
};
363+
364+
const __widl_f_state_PopStateEvent_target = GetOwnOrInheritedPropertyDescriptor(typeof PopStateEvent === 'undefined' ? null : PopStateEvent.prototype, 'state').get || function() {
365+
throw new Error(`wasm-bindgen: PopStateEvent.state does not exist`);
366+
};
367+
368+
__exports.__widl_f_state_PopStateEvent = function(arg0) {
369+
return addHeapObject(__widl_f_state_PopStateEvent_target.call(getObject(arg0)));
370+
};
371+
336372
__exports.__widl_instanceof_Window = function(idx) {
337373
return getObject(idx) instanceof Window ? 1 : 0;
338374
};
@@ -344,6 +380,17 @@ __exports.__widl_f_document_Window = function(arg0) {
344380

345381
};
346382

383+
__exports.__widl_f_history_Window = function(arg0, exnptr) {
384+
try {
385+
return addHeapObject(getObject(arg0).history);
386+
} catch (e) {
387+
const view = getUint32Memory();
388+
view[exnptr / 4] = 1;
389+
view[exnptr / 4 + 1] = addHeapObject(e);
390+
391+
}
392+
};
393+
347394
const __widl_f_log_1__target = console.log;
348395

349396
__exports.__widl_f_log_1_ = function(arg0) {
@@ -428,9 +475,11 @@ __exports.__wbindgen_cb_drop = function(i) {
428475
return 0;
429476
};
430477

431-
__exports.__wbindgen_closure_wrapper765 = function(a, b, _ignored) {
432-
const f = wasm.__wbg_function_table.get(2);
433-
const d = wasm.__wbg_function_table.get(3);
478+
__exports.__wbindgen_cb_forget = dropObject;
479+
480+
__exports.__wbindgen_closure_wrapper790 = function(a, b, _ignored) {
481+
const f = wasm.__wbg_function_table.get(6);
482+
const d = wasm.__wbg_function_table.get(7);
434483
const cb = function(arg0) {
435484
this.cnt++;
436485
let a = this.a;

examples/counter/pkg/counter_bg.wasm

48 KB
Binary file not shown.
File renamed without changes.

examples/counter/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ enum Msg {
3535
}
3636

3737
/// The sole source of updating the model; returns a fresh one.
38-
fn update(msg: Msg, model: Model) -> Model {
38+
fn update(history: &History<Model, Msg>, msg: Msg, model: Model) -> Model {
3939
match msg {
4040
Msg::Increment => Model {count: model.count + 1, ..model},
4141
Msg::Decrement => Model {count: model.count - 1, ..model},
@@ -98,5 +98,5 @@ fn view(model: Model) -> El<Msg> {
9898

9999
#[wasm_bindgen]
100100
pub fn render() {
101-
seed::run(Model::default(), update, view, "main");
101+
seed::run(Model::default(), update, view, "main", None);
102102
}

examples/homepage/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
The Seed homepage, also serving as an example. Includes
2-
lots of view syntax, and elements created from markdown.
2+
//! simple interactions, markdown elements, basic routing, and lots of view markup.
33

44
# [Homepage repo](https://github.com/David-OConnor/seed-homepage)

examples/server_interaction/pkg/appname.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)