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 crate::{components::ResultMessageData, utils::read_file, error::ReadKleError, models::Colorway};
use super::ResultMessage;
use crate::{
components::ResultMessageData, error::ReadKleError, models::Colorway, utils::read_file,
};
#[component]
pub fn KeyboardFromFile(
@ -61,4 +63,4 @@ async fn read_kle_from_file(
file: ReadSignal<Option<File>>,
) -> Result<kle_serial::Keyboard, ReadKleError> {
Ok(serde_json::from_str(&read_file(file).await?)?)
}
}

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

@ -5,4 +5,4 @@ mod keyboardfromfile;
pub use keyboardfromfile::KeyboardFromFile;
mod keyboardfromweb;
pub use keyboardfromweb::KeyboardFromWeb;
pub use keyboardfromweb::KeyboardFromWeb;

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

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

@ -5,7 +5,9 @@ use super::FetchFileError;
#[derive(From, IntoStaticStr)]
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,
#[strum(serialize = "Failed to fetch file")]
FetchFile(FetchFileError),
@ -25,11 +27,12 @@ impl ToString for FetchKleError {
full_error.push_str(&error_info);
}
full_error
},
}
FetchFileError::ReadToString => error.to_string(),
}),
Serde(error) => Some(error.to_string()),
_ => None,
}.unwrap_or_else(|| "".to_string())
}
.unwrap_or_else(|| "".to_string())
}
}
}

@ -8,4 +8,4 @@ mod fetchkle;
pub use fetchkle::FetchKleError;
mod readkle;
pub use readkle::ReadKleError;
pub use readkle::ReadKleError;

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

@ -18,4 +18,4 @@ impl ToString for ReadKleError {
Serde(error) => error.to_string(),
}
}
}
}

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

@ -22,4 +22,4 @@ impl ClassList for Colorway {
let class: &str = self.into();
class.to_string()
}
}
}

@ -1,2 +1,2 @@
mod colorway;
pub use colorway::Colorway;
pub use colorway::Colorway;

@ -1,24 +1,19 @@
use leptos::*;
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 {
web_sys::window().unwrap()
}
pub async fn read_file(
file: ReadSignal<Option<File>>,
) -> Result<String, ReadFileError> {
pub async fn read_file(file: ReadSignal<Option<File>>) -> Result<String, ReadFileError> {
let file = match file() {
Some(file) => file,
None => return Err(ReadFileError::NoFile),
};
match JsFuture::from(file.text())
.await?
.as_string()
{
match JsFuture::from(file.text()).await?.as_string() {
Some(contents) => Ok(contents),
None => Err(ReadFileError::ParseToString),
}
@ -31,4 +26,4 @@ pub async fn fetch_file(url: &str) -> Result<String, FetchFileError> {
Some(string) => Ok(string),
None => Err(FetchFileError::ReadToString),
}
}
}

Loading…
Cancel
Save