Skip to content

Commit 967ba42

Browse files
committed
move file_input to a component
1 parent 8d018d5 commit 967ba42

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

src/wasm/image.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::wasm::FileUpload;
1313

1414
pub struct ImageFile<'a> {
1515
file_input: &'a FileUpload,
16+
1617
width: usize,
1718
height: usize,
1819
}
@@ -106,14 +107,16 @@ impl Component for ImageComponent {
106107
});
107108

108109
html! {
109-
<form onsubmit={noop}>
110+
<div>
111+
<form onsubmit={noop}>
112+
<label for="width">{"[Tile] Width"}</label>
113+
<input name="width" type="number" value={image.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
114+
<label for="height">{"[Tile] Height"}</label>
115+
<input name="height" type="number" value={image.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
116+
</form>
110117
{ &image.name() }
111-
<label for="width">{"[Tile] Width"}</label>
112-
<input name="width" type="number" value={image.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
113-
<label for="height">{"[Tile] Height"}</label>
114-
<input name="height" type="number" value={image.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
115118
<img src={output} />
116-
</form>
119+
</div>
117120
}
118121
}
119122
}

src/wasm/main.rs

+3-39
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
#![cfg(feature = "wasm")]
2-
use std::collections::HashMap;
3-
4-
use gloo::file::callbacks::FileReader;
5-
use web_sys::HtmlInputElement;
62
use yew::prelude::*;
73

84
use cega::wasm::image::*;
9-
use cega::wasm::FileUpload;
5+
use cega::wasm::{FileInput, FileUpload};
106

117
pub enum Msg {
128
Loaded(FileUpload),
13-
Submit(Event),
149
}
1510

1611
pub struct App {
17-
readers: HashMap<String, FileReader>,
1812
files: Vec<FileUpload>,
1913
}
2014

@@ -24,53 +18,23 @@ impl Component for App {
2418

2519
fn create(_ctx: &Context<Self>) -> Self {
2620
Self {
27-
readers: HashMap::default(),
2821
files: Vec::default(),
2922
}
3023
}
3124

32-
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
25+
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
3326
match msg {
3427
Msg::Loaded(file) => {
35-
self.readers.remove(&file.name);
3628
self.files.push(file);
3729
}
38-
Msg::Submit(e) => {
39-
let input: HtmlInputElement = e.target_unchecked_into();
40-
if let Some(files) = input.files() {
41-
for file in gloo::file::FileList::from(files).iter() {
42-
let link = ctx.link().clone();
43-
let name = file.name().clone();
44-
let mime_type = file.raw_mime_type();
45-
let task = {
46-
gloo::file::callbacks::read_as_bytes(&file, move |res| {
47-
link.send_message(Msg::Loaded(FileUpload {
48-
data: res.expect("failed to read file"),
49-
mime_type,
50-
name,
51-
}))
52-
})
53-
};
54-
self.readers.insert(file.name(), task);
55-
}
56-
}
57-
}
5830
}
5931
true
6032
}
6133

6234
fn view(&self, ctx: &Context<Self>) -> Html {
6335
html! {
6436
<div id="wrapper">
65-
<label for="file-upload">
66-
</label>
67-
<input
68-
id="file-upload"
69-
type="file"
70-
accept="image/*,.bin,.cga,.ega"
71-
multiple={false}
72-
onchange={ctx.link().callback(Msg::Submit)}
73-
/>
37+
<FileInput accept="image/*,.bin,.cga,.ega" multiple=false onload={ctx.link().callback( Msg::Loaded )}/>
7438
<div id="preview-area">
7539
{for self.files.iter().map(|f|
7640
html! { <ImageComponent file={f.clone()} /> }

src/wasm/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1+
pub mod file_input;
12
pub mod image;
23

3-
#[derive(Clone, PartialEq)]
4-
pub struct FileUpload {
5-
pub name: String,
6-
pub mime_type: String,
7-
pub data: Vec<u8>,
8-
}
4+
pub use file_input::FileInput;
5+
pub use file_input::FileUpload;

0 commit comments

Comments
 (0)