Skip to content

Commit 5ee27bc

Browse files
committed
click to select preview width
1 parent c656844 commit 5ee27bc

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/wasm/image.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use web_sys::HtmlInputElement;
44
use yew::{html, Callback, Component, Context, Event, Html, Properties, SubmitEvent, TargetCast};
55

66
use crate::color::palette::palette_from_abbr;
7-
use crate::image::tile;
87
use crate::parser::ParserType;
98
use crate::{file_data, png};
109

@@ -20,7 +19,7 @@ pub struct ImageComponent {
2019
}
2120

2221
pub enum Msg {
23-
Width(Event),
22+
Width(usize),
2423
}
2524

2625
impl ImageComponent {
@@ -40,7 +39,7 @@ impl ImageComponent {
4039
format!("data:application/png;base64,{}", STANDARD.encode(data))
4140
}
4241

43-
pub fn previews(&self, file: &FileUpload) -> Html {
42+
pub fn previews(&self, ctx: &Context<Self>, file: &FileUpload) -> Html {
4443
if file.mime_type.contains("image") {
4544
"".into()
4645
} else {
@@ -54,9 +53,12 @@ impl ImageComponent {
5453

5554
let _ = png::write_to(&mut bytes, p.data(), palette.clone());
5655
let src = format!("data:application/png;base64,{}", STANDARD.encode(bytes));
56+
let width = p.width().clone();
5757
html! {
58-
<div class="preview-tile">
59-
<h3>{p.width()}</h3>
58+
<div class="preview-tile" onclick={ctx.link().callback( move |_|{
59+
Msg::Width(width)
60+
})}>
61+
<h3>{&width.to_string()}</h3>
6062
<img src={ src } />
6163
</div>
6264
}
@@ -71,16 +73,13 @@ impl Component for ImageComponent {
7173
type Properties = Props;
7274

7375
fn create(_ctx: &Context<Self>) -> Self {
74-
Self {
75-
width: 320,
76-
}
76+
Self { width: 320 }
7777
}
7878

7979
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
8080
match msg {
81-
Msg::Width(e) => {
82-
let input: HtmlInputElement = e.target_unchecked_into();
83-
self.width = input.value().parse().expect("fail to parse width");
81+
Msg::Width(w) => {
82+
self.width = w;
8483
}
8584
}
8685
true
@@ -90,6 +89,10 @@ impl Component for ImageComponent {
9089
let noop = Callback::from(|e: SubmitEvent| {
9190
e.prevent_default();
9291
});
92+
let onchange = ctx.link().callback(|e: Event| {
93+
let input: HtmlInputElement = e.target_unchecked_into();
94+
Msg::Width(input.value().parse().expect("fail to parse width"))
95+
});
9396
let file = &ctx.props().file;
9497

9598
html! {
@@ -101,11 +104,11 @@ impl Component for ImageComponent {
101104
</div>
102105
<form onsubmit={noop}>
103106
<label for="width">{"Width"}</label>
104-
<input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
107+
<input name="width" type="number" value={self.width.to_string()} {onchange} />
105108
</form>
106109
</div>
107110
<div class="preview-row">
108-
{self.previews(file)}
111+
{self.previews(ctx, file)}
109112
</div>
110113
</>
111114
}

src/wasm/styles.css

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ label {
7171
.preview-row .preview-tile {
7272
background: #111717;
7373
border-radius: 1rem;
74+
cursor: pointer;
7475
}
7576

7677

0 commit comments

Comments
 (0)