From 5ee27bc648ba8a5db96dc496968e49ab7e43775f Mon Sep 17 00:00:00 2001 From: Kenzi Connor Date: Mon, 5 Aug 2024 23:14:59 -0700 Subject: [PATCH] click to select preview width --- src/wasm/image.rs | 29 ++++++++++++++++------------- src/wasm/styles.css | 1 + 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/wasm/image.rs b/src/wasm/image.rs index 50b7698..69f362e 100644 --- a/src/wasm/image.rs +++ b/src/wasm/image.rs @@ -4,7 +4,6 @@ use web_sys::HtmlInputElement; use yew::{html, Callback, Component, Context, Event, Html, Properties, SubmitEvent, TargetCast}; use crate::color::palette::palette_from_abbr; -use crate::image::tile; use crate::parser::ParserType; use crate::{file_data, png}; @@ -20,7 +19,7 @@ pub struct ImageComponent { } pub enum Msg { - Width(Event), + Width(usize), } impl ImageComponent { @@ -40,7 +39,7 @@ impl ImageComponent { format!("data:application/png;base64,{}", STANDARD.encode(data)) } - pub fn previews(&self, file: &FileUpload) -> Html { + pub fn previews(&self, ctx: &Context, file: &FileUpload) -> Html { if file.mime_type.contains("image") { "".into() } else { @@ -54,9 +53,12 @@ impl ImageComponent { let _ = png::write_to(&mut bytes, p.data(), palette.clone()); let src = format!("data:application/png;base64,{}", STANDARD.encode(bytes)); + let width = p.width().clone(); html! { -
-

{p.width()}

+
+

{&width.to_string()}

} @@ -71,16 +73,13 @@ impl Component for ImageComponent { type Properties = Props; fn create(_ctx: &Context) -> Self { - Self { - width: 320, - } + Self { width: 320 } } fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { match msg { - Msg::Width(e) => { - let input: HtmlInputElement = e.target_unchecked_into(); - self.width = input.value().parse().expect("fail to parse width"); + Msg::Width(w) => { + self.width = w; } } true @@ -90,6 +89,10 @@ impl Component for ImageComponent { let noop = Callback::from(|e: SubmitEvent| { e.prevent_default(); }); + let onchange = ctx.link().callback(|e: Event| { + let input: HtmlInputElement = e.target_unchecked_into(); + Msg::Width(input.value().parse().expect("fail to parse width")) + }); let file = &ctx.props().file; html! { @@ -101,11 +104,11 @@ impl Component for ImageComponent {
- +
- {self.previews(file)} + {self.previews(ctx, file)}
} diff --git a/src/wasm/styles.css b/src/wasm/styles.css index 7b836f6..511a652 100644 --- a/src/wasm/styles.css +++ b/src/wasm/styles.css @@ -71,6 +71,7 @@ label { .preview-row .preview-tile { background: #111717; border-radius: 1rem; + cursor: pointer; }