From f6393815052055851bc0e300dd4308e86d116a38 Mon Sep 17 00:00:00 2001 From: Kenzi Connor Date: Sat, 3 Aug 2024 19:31:14 -0700 Subject: [PATCH] get rid of a layer of indirection to reorg for multiple previews --- src/wasm/image.rs | 75 +++++++++++++---------------------------------- src/wasm/main.rs | 2 +- 2 files changed, 22 insertions(+), 55 deletions(-) diff --git a/src/wasm/image.rs b/src/wasm/image.rs index 0d2b488..44796b4 100644 --- a/src/wasm/image.rs +++ b/src/wasm/image.rs @@ -11,35 +11,27 @@ use crate::png; use crate::wasm::FileUpload; -pub struct ImageFile<'a> { - file_input: &'a FileUpload, +#[derive(Clone, PartialEq, Properties)] +pub struct Props { + pub file: FileUpload, +} +pub struct ImageComponent { width: usize, height: usize, } -impl ImageFile<'_> { - pub fn name(&self) -> String { - if self.file_input.mime_type.contains("image") { - self.file_input.name.to_string() - } else { - format!("{}{}", self.file_input.name, ".png") - } - } - - pub fn mime_type(&self) -> String { - if self.file_input.mime_type.contains("image") { - self.file_input.mime_type.to_string() - } else { - "image/png".to_string() - } - } +pub enum Msg { + Width(Event), + Height(Event), +} - pub fn data(&self) -> Vec { - if self.file_input.mime_type.contains("image") { - self.file_input.data.clone() +impl ImageComponent { + pub fn src(&self, file: &FileUpload) -> String { + let data = if file.mime_type.contains("image") { + file.data.clone() } else { - let file_data = file_data::Raw::new(&self.file_input.data); + let file_data = file_data::Raw::new(&file.data); let parser = ParserType::CGA; let image = file_data.parse(parser, self.width); let palette = palette_from_abbr("cga0"); @@ -47,25 +39,11 @@ impl ImageFile<'_> { let _ = png::write_to(&mut bytes, tile(image.data(), self.height), palette.clone()); bytes - } + }; + format!("data:application/png;base64,{}", STANDARD.encode(data)) } } -#[derive(Clone, PartialEq, Properties)] -pub struct Props { - pub file: FileUpload, -} - -pub struct ImageComponent { - width: usize, - height: usize, -} - -pub enum Msg { - Width(Event), - Height(Event), -} - impl Component for ImageComponent { type Message = Msg; type Properties = Props; @@ -92,33 +70,22 @@ impl Component for ImageComponent { } fn view(&self, ctx: &Context) -> Html { - let image = ImageFile { - file_input: &ctx.props().file, - width: self.width, - height: self.height, - }; - - let output = format!( - "data:{};base64,{}", - image.mime_type(), - STANDARD.encode(image.data()) - ); - let noop = Callback::from(|e: SubmitEvent| { e.prevent_default(); }); + let file = &ctx.props().file; html! {
- + - +
-

{ &image.name() }

+

{ file.name.to_string() }

- +
} diff --git a/src/wasm/main.rs b/src/wasm/main.rs index 55e18f4..4df408d 100644 --- a/src/wasm/main.rs +++ b/src/wasm/main.rs @@ -40,7 +40,7 @@ impl Component for App { html! {

{ "Process your CGA/EGAs" }

- +
{{ images }}