main
Elnu 1 year ago
parent f5f353803a
commit 33447c13d0

@ -1,8 +1,10 @@
use leptos::{*, html::Input}; use leptos::{html::Input, *};
use web_sys::File; use web_sys::File;
use crate::{components::ResultMessageData, utils::read_file, error::ReadKleError, models::Colorway};
use super::ResultMessage; use super::ResultMessage;
use crate::{
components::ResultMessageData, error::ReadKleError, models::Colorway, utils::read_file,
};
#[component] #[component]
pub fn KeyboardFromFile( pub fn KeyboardFromFile(

@ -1,8 +1,8 @@
use leptos::{*, html::Input}; use leptos::{html::Input, *};
use web_sys::SubmitEvent; use web_sys::SubmitEvent;
use crate::{error::FetchKleError, utils::fetch_file, models::Colorway};
use super::{ResultMessage, ResultMessageData}; use super::{ResultMessage, ResultMessageData};
use crate::{error::FetchKleError, models::Colorway, utils::fetch_file};
async fn fetch_layout(url: &str) -> Result<kle_serial::Keyboard, FetchKleError> { async fn fetch_layout(url: &str) -> Result<kle_serial::Keyboard, FetchKleError> {
let layout_string = fetch_file(url).await?; let layout_string = fetch_file(url).await?;
@ -31,7 +31,7 @@ pub fn KeyboardFromWeb(
colorway: Colorway::Ok, colorway: Colorway::Ok,
})); }));
set_keyboard(Some(keyboard)); set_keyboard(Some(keyboard));
}, }
Err(err) => { Err(err) => {
set_result_message(Some(ResultMessageData { set_result_message(Some(ResultMessageData {
message: view! { cx, message: view! { cx,

@ -1,5 +1,5 @@
use leptos::*;
use class_list::class_list; use class_list::class_list;
use leptos::*;
use crate::models::Colorway; use crate::models::Colorway;
@ -17,15 +17,17 @@ pub fn ResultMessage(cx: Scope, message: ReadSignal<Option<ResultMessageData>>)
message.track(); message.track();
set_open(true); set_open(true);
}); });
move || view! { cx, move || {
<Show when=open fallback=|_| ()> view! { cx,
{move || message().map(|ResultMessageData { title, message, colorway }| view! { cx, <Show when=open fallback=|_| ()>
<div class=class_list!["box", colorway] style="position: relative"> {move || message().map(|ResultMessageData { title, message, colorway }| view! { cx,
<strong class="block titlebar">{title}</strong> <div class=class_list!["box", colorway] style="position: relative">
<button class="iconbutton" on:click=move |_| set_open(false) style="position: absolute; bottom: 0.5em; right: 0.5em">"×"</button> <strong class="block titlebar">{title}</strong>
<p>{message}</p> <button class="iconbutton" on:click=move |_| set_open(false) style="position: absolute; bottom: 0.5em; right: 0.5em">"×"</button>
</div> <p>{message}</p>
})} </div>
</Show> })}
</Show>
}
} }
} }

@ -16,6 +16,7 @@ impl ToString for FetchFileError {
match self { match self {
Request(error) => error.as_string(), Request(error) => error.as_string(),
_ => None, _ => None,
}.unwrap_or_else(|| "".to_string()) }
.unwrap_or_else(|| "".to_string())
} }
} }

@ -5,7 +5,9 @@ use super::FetchFileError;
#[derive(From, IntoStaticStr)] #[derive(From, IntoStaticStr)]
pub enum FetchKleError { pub enum FetchKleError {
#[strum(serialize = "Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file")] #[strum(
serialize = "Invalid source. Must be KLE layout, gist URL, gist ID, or direct URL to JSON file"
)]
InvalidSource, InvalidSource,
#[strum(serialize = "Failed to fetch file")] #[strum(serialize = "Failed to fetch file")]
FetchFile(FetchFileError), FetchFile(FetchFileError),
@ -25,11 +27,12 @@ impl ToString for FetchKleError {
full_error.push_str(&error_info); full_error.push_str(&error_info);
} }
full_error full_error
}, }
FetchFileError::ReadToString => error.to_string(), FetchFileError::ReadToString => error.to_string(),
}), }),
Serde(error) => Some(error.to_string()), Serde(error) => Some(error.to_string()),
_ => None, _ => None,
}.unwrap_or_else(|| "".to_string()) }
.unwrap_or_else(|| "".to_string())
} }
} }

@ -17,7 +17,8 @@ impl ToString for ReadFileError {
use ReadFileError::*; use ReadFileError::*;
match self { match self {
TextAwait(error) => error.as_string(), TextAwait(error) => error.as_string(),
_ => None _ => None,
}.unwrap_or_else(|| "".to_string()) }
.unwrap_or_else(|| "".to_string())
} }
} }

@ -1,13 +1,13 @@
#![feature(async_closure)] #![feature(async_closure)]
#[allow(dead_code)]
mod render;
mod error;
mod components; mod components;
mod utils; mod error;
mod models; mod models;
#[allow(dead_code)] #[allow(dead_code)]
mod render;
#[allow(dead_code)]
pub mod svg; pub mod svg;
mod utils;
use leptos::*; use leptos::*;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;

@ -1,24 +1,19 @@
use leptos::*; use leptos::*;
use wasm_bindgen_futures::JsFuture; use wasm_bindgen_futures::JsFuture;
use web_sys::{File, Window, Request}; use web_sys::{File, Request, Window};
use crate::error::{ReadFileError, FetchFileError}; use crate::error::{FetchFileError, ReadFileError};
pub fn window() -> Window { pub fn window() -> Window {
web_sys::window().unwrap() web_sys::window().unwrap()
} }
pub async fn read_file( pub async fn read_file(file: ReadSignal<Option<File>>) -> Result<String, ReadFileError> {
file: ReadSignal<Option<File>>,
) -> Result<String, ReadFileError> {
let file = match file() { let file = match file() {
Some(file) => file, Some(file) => file,
None => return Err(ReadFileError::NoFile), None => return Err(ReadFileError::NoFile),
}; };
match JsFuture::from(file.text()) match JsFuture::from(file.text()).await?.as_string() {
.await?
.as_string()
{
Some(contents) => Ok(contents), Some(contents) => Ok(contents),
None => Err(ReadFileError::ParseToString), None => Err(ReadFileError::ParseToString),
} }

Loading…
Cancel
Save