Skip to content

Commit baeb5cb

Browse files
committed
restore styles and drag and drop
1 parent 967ba42 commit baeb5cb

File tree

4 files changed

+106
-8
lines changed

4 files changed

+106
-8
lines changed

src/wasm/file_input.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use std::collections::HashMap;
2+
3+
use gloo::file::callbacks::FileReader;
4+
use web_sys::HtmlInputElement;
5+
use yew::prelude::*;
6+
7+
#[derive(Clone, PartialEq)]
8+
pub struct FileUpload {
9+
pub name: String,
10+
pub mime_type: String,
11+
pub data: Vec<u8>,
12+
}
13+
14+
pub struct FileInput {
15+
readers: HashMap<String, FileReader>,
16+
}
17+
18+
pub enum Msg {
19+
Loaded(FileUpload),
20+
Submit(Option<web_sys::FileList>),
21+
}
22+
23+
#[derive(Properties, PartialEq)]
24+
pub struct Props {
25+
pub accept: AttrValue,
26+
#[prop_or(false)]
27+
pub multiple: bool,
28+
pub onload: Callback<FileUpload>,
29+
#[prop_or(AttrValue::Static("Drop Files Here"))]
30+
pub label: AttrValue,
31+
}
32+
33+
impl Component for FileInput {
34+
type Message = Msg;
35+
type Properties = Props;
36+
37+
fn create(_ctx: &Context<Self>) -> Self {
38+
Self {
39+
readers: HashMap::default(),
40+
}
41+
}
42+
43+
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
44+
match msg {
45+
Msg::Loaded(file) => {
46+
self.readers.remove(&file.name);
47+
ctx.props().onload.emit(file);
48+
}
49+
Msg::Submit(files) => {
50+
if let Some(files) = files {
51+
for file in gloo::file::FileList::from(files).iter() {
52+
let link = ctx.link().clone();
53+
let name = file.name().clone();
54+
let mime_type = file.raw_mime_type();
55+
let task = {
56+
gloo::file::callbacks::read_as_bytes(&file, move |res| {
57+
link.send_message(Msg::Loaded(FileUpload {
58+
data: res.expect("failed to read file"),
59+
mime_type,
60+
name,
61+
}))
62+
})
63+
};
64+
self.readers.insert(file.name(), task);
65+
}
66+
}
67+
}
68+
}
69+
true
70+
}
71+
72+
fn view(&self, ctx: &Context<Self>) -> Html {
73+
let noop_drag = Callback::from(|e: DragEvent| {
74+
e.prevent_default();
75+
});
76+
html! {
77+
<label class="drop-container" ondragover={&noop_drag} ondragenter={&noop_drag}
78+
ondrop={ctx.link().callback(|event: DragEvent| {
79+
event.prevent_default();
80+
Msg::Submit(event.data_transfer().unwrap().files())
81+
})}
82+
><i>{ ctx.props().label.clone() }</i>
83+
<input
84+
type="file"
85+
accept="{ ctx.props().accept }"
86+
multiple={ ctx.props().multiple }
87+
onchange={ctx.link().callback(|e: Event| {
88+
let input: HtmlInputElement = e.target_unchecked_into();
89+
Msg::Submit(input.files())
90+
})}
91+
/>
92+
</label>
93+
}
94+
}
95+
}

src/wasm/image.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,17 @@ impl Component for ImageComponent {
107107
});
108108

109109
html! {
110-
<div>
110+
<div class="preview-tile ">
111111
<form onsubmit={noop}>
112112
<label for="width">{"[Tile] Width"}</label>
113113
<input name="width" type="number" value={image.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
114114
<label for="height">{"[Tile] Height"}</label>
115115
<input name="height" type="number" value={image.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
116116
</form>
117-
{ &image.name() }
118-
<img src={output} />
117+
<p class="preview-name">{ &image.name() }</p>
118+
<div class=".preview-media">
119+
<img src={output} />
120+
</div>
119121
</div>
120122
}
121123
}

src/wasm/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ impl Component for App {
3434
fn view(&self, ctx: &Context<Self>) -> Html {
3535
html! {
3636
<div id="wrapper">
37-
<FileInput accept="image/*,.bin,.cga,.ega" multiple=false onload={ctx.link().callback( Msg::Loaded )}/>
37+
<h1>{ "Process your CGA/EGAs" }</h1>
38+
<FileInput accept="image/*,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )}/>
3839
<div id="preview-area">
3940
{for self.files.iter().map(|f|
4041
html! { <ImageComponent file={f.clone()} /> }

src/wasm/styles.css

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ label {
2929
margin: auto;
3030
}
3131

32-
#title {
32+
#h1 {
3333
font-size: 2rem;
3434
text-align: center;
3535
}
3636

37-
#drop-container {
37+
.drop-container {
3838
padding: 4rem;
3939
display: flex;
4040
flex-direction: column;
@@ -45,11 +45,11 @@ label {
4545
border-radius: 1rem;
4646
}
4747

48-
#drop-container i {
48+
.drop-container i {
4949
font-size: 4rem;
5050
}
5151

52-
#preview-area {
52+
.preview-area {
5353
display: flex;
5454
flex-wrap: wrap;
5555
justify-content: center;

0 commit comments

Comments
 (0)