Skip to content

Commit f639381

Browse files
committed
get rid of a layer of indirection to reorg for multiple previews
1 parent 7f51c3f commit f639381

File tree

2 files changed

+22
-55
lines changed

2 files changed

+22
-55
lines changed

src/wasm/image.rs

+21-54
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,39 @@ use crate::png;
1111

1212
use crate::wasm::FileUpload;
1313

14-
pub struct ImageFile<'a> {
15-
file_input: &'a FileUpload,
14+
#[derive(Clone, PartialEq, Properties)]
15+
pub struct Props {
16+
pub file: FileUpload,
17+
}
1618

19+
pub struct ImageComponent {
1720
width: usize,
1821
height: usize,
1922
}
2023

21-
impl ImageFile<'_> {
22-
pub fn name(&self) -> String {
23-
if self.file_input.mime_type.contains("image") {
24-
self.file_input.name.to_string()
25-
} else {
26-
format!("{}{}", self.file_input.name, ".png")
27-
}
28-
}
29-
30-
pub fn mime_type(&self) -> String {
31-
if self.file_input.mime_type.contains("image") {
32-
self.file_input.mime_type.to_string()
33-
} else {
34-
"image/png".to_string()
35-
}
36-
}
24+
pub enum Msg {
25+
Width(Event),
26+
Height(Event),
27+
}
3728

38-
pub fn data(&self) -> Vec<u8> {
39-
if self.file_input.mime_type.contains("image") {
40-
self.file_input.data.clone()
29+
impl ImageComponent {
30+
pub fn src(&self, file: &FileUpload) -> String {
31+
let data = if file.mime_type.contains("image") {
32+
file.data.clone()
4133
} else {
42-
let file_data = file_data::Raw::new(&self.file_input.data);
34+
let file_data = file_data::Raw::new(&file.data);
4335
let parser = ParserType::CGA;
4436
let image = file_data.parse(parser, self.width);
4537
let palette = palette_from_abbr("cga0");
4638
let mut bytes: Vec<u8> = Vec::new();
4739

4840
let _ = png::write_to(&mut bytes, tile(image.data(), self.height), palette.clone());
4941
bytes
50-
}
42+
};
43+
format!("data:application/png;base64,{}", STANDARD.encode(data))
5144
}
5245
}
5346

54-
#[derive(Clone, PartialEq, Properties)]
55-
pub struct Props {
56-
pub file: FileUpload,
57-
}
58-
59-
pub struct ImageComponent {
60-
width: usize,
61-
height: usize,
62-
}
63-
64-
pub enum Msg {
65-
Width(Event),
66-
Height(Event),
67-
}
68-
6947
impl Component for ImageComponent {
7048
type Message = Msg;
7149
type Properties = Props;
@@ -92,33 +70,22 @@ impl Component for ImageComponent {
9270
}
9371

9472
fn view(&self, ctx: &Context<Self>) -> Html {
95-
let image = ImageFile {
96-
file_input: &ctx.props().file,
97-
width: self.width,
98-
height: self.height,
99-
};
100-
101-
let output = format!(
102-
"data:{};base64,{}",
103-
image.mime_type(),
104-
STANDARD.encode(image.data())
105-
);
106-
10773
let noop = Callback::from(|e: SubmitEvent| {
10874
e.prevent_default();
10975
});
76+
let file = &ctx.props().file;
11077

11178
html! {
11279
<div class="preview-tile ">
11380
<form onsubmit={noop}>
11481
<label for="width">{"[Tile] Width"}</label>
115-
<input name="width" type="number" value={image.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
82+
<input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
11683
<label for="height">{"[Tile] Height"}</label>
117-
<input name="height" type="number" value={image.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
84+
<input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
11885
</form>
119-
<p class="preview-name">{ &image.name() }</p>
86+
<p class="preview-name">{ file.name.to_string() }</p>
12087
<div class=".preview-media">
121-
<img src={output} />
88+
<img src={ self.src(file) } />
12289
</div>
12390
</div>
12491
}

src/wasm/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Component for App {
4040
html! {
4141
<div id="wrapper">
4242
<h1>{ "Process your CGA/EGAs" }</h1>
43-
<FileInput accept="image/*,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )} children={None}/>
43+
<FileInput accept="image/png,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )} children={None}/>
4444
<div id="preview-area">
4545
{{ images }}
4646
</div>

0 commit comments

Comments
 (0)