From baeb5cb01f0c74aaca341edeaa02dafe8de59342 Mon Sep 17 00:00:00 2001 From: Kenzi Connor Date: Mon, 22 Jul 2024 20:41:16 -0700 Subject: [PATCH] restore styles and drag and drop --- src/wasm/file_input.rs | 95 ++++++++++++++++++++++++++++++++++++++++++ src/wasm/image.rs | 8 ++-- src/wasm/main.rs | 3 +- src/wasm/styles.css | 8 ++-- 4 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 src/wasm/file_input.rs diff --git a/src/wasm/file_input.rs b/src/wasm/file_input.rs new file mode 100644 index 0000000..c1f413b --- /dev/null +++ b/src/wasm/file_input.rs @@ -0,0 +1,95 @@ +use std::collections::HashMap; + +use gloo::file::callbacks::FileReader; +use web_sys::HtmlInputElement; +use yew::prelude::*; + +#[derive(Clone, PartialEq)] +pub struct FileUpload { + pub name: String, + pub mime_type: String, + pub data: Vec, +} + +pub struct FileInput { + readers: HashMap, +} + +pub enum Msg { + Loaded(FileUpload), + Submit(Option), +} + +#[derive(Properties, PartialEq)] +pub struct Props { + pub accept: AttrValue, + #[prop_or(false)] + pub multiple: bool, + pub onload: Callback, + #[prop_or(AttrValue::Static("Drop Files Here"))] + pub label: AttrValue, +} + +impl Component for FileInput { + type Message = Msg; + type Properties = Props; + + fn create(_ctx: &Context) -> Self { + Self { + readers: HashMap::default(), + } + } + + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { + match msg { + Msg::Loaded(file) => { + self.readers.remove(&file.name); + ctx.props().onload.emit(file); + } + Msg::Submit(files) => { + if let Some(files) = files { + for file in gloo::file::FileList::from(files).iter() { + let link = ctx.link().clone(); + let name = file.name().clone(); + let mime_type = file.raw_mime_type(); + let task = { + gloo::file::callbacks::read_as_bytes(&file, move |res| { + link.send_message(Msg::Loaded(FileUpload { + data: res.expect("failed to read file"), + mime_type, + name, + })) + }) + }; + self.readers.insert(file.name(), task); + } + } + } + } + true + } + + fn view(&self, ctx: &Context) -> Html { + let noop_drag = Callback::from(|e: DragEvent| { + e.prevent_default(); + }); + html! { + + } + } +} diff --git a/src/wasm/image.rs b/src/wasm/image.rs index 88e3480..b13e27f 100644 --- a/src/wasm/image.rs +++ b/src/wasm/image.rs @@ -107,15 +107,17 @@ impl Component for ImageComponent { }); html! { -
+
- { &image.name() } - +

{ &image.name() }

+
+ +
} } diff --git a/src/wasm/main.rs b/src/wasm/main.rs index 6651f71..67bc969 100644 --- a/src/wasm/main.rs +++ b/src/wasm/main.rs @@ -34,7 +34,8 @@ impl Component for App { fn view(&self, ctx: &Context) -> Html { html! {
- +

{ "Process your CGA/EGAs" }

+
{for self.files.iter().map(|f| html! { } diff --git a/src/wasm/styles.css b/src/wasm/styles.css index 972d8fb..dd5be8a 100644 --- a/src/wasm/styles.css +++ b/src/wasm/styles.css @@ -29,12 +29,12 @@ label { margin: auto; } -#title { +#h1 { font-size: 2rem; text-align: center; } -#drop-container { +.drop-container { padding: 4rem; display: flex; flex-direction: column; @@ -45,11 +45,11 @@ label { border-radius: 1rem; } -#drop-container i { +.drop-container i { font-size: 4rem; } -#preview-area { +.preview-area { display: flex; flex-wrap: wrap; justify-content: center;