From cef484987ae7cb9cee096772075aff3eb57b8eab Mon Sep 17 00:00:00 2001 From: Adrian Wozniak Date: Tue, 19 May 2020 20:18:12 +0200 Subject: [PATCH] Rewire global catch key and prevent action on editable --- jirs-client/src/lib.rs | 50 ++++++++++--------- jirs-client/src/shared/styled_select_child.rs | 4 +- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/jirs-client/src/lib.rs b/jirs-client/src/lib.rs index 63f75c26..45c690ed 100644 --- a/jirs-client/src/lib.rs +++ b/jirs-client/src/lib.rs @@ -1,6 +1,8 @@ use seed::{prelude::*, *}; use web_sys::File; +pub use changes::*; +pub use fields::*; use jirs_data::*; use crate::model::{ModalType, Model, Page}; @@ -23,9 +25,6 @@ mod users; pub mod validations; mod ws; -pub use changes::*; -pub use fields::*; - pub type AppType = App>; #[derive(Debug)] @@ -235,30 +234,10 @@ pub fn render(host_url: String, ws_url: String) { WS_URL = ws_url; } - // if let Some(body) = seed::html_document().body() { - // use wasm_bindgen::JsCast; - // let body = body.dyn_ref::().unwrap().clone(); - // let key_up_closure = - // wasm_bindgen::closure::Closure::wrap(Box::new(|event: web_sys::KeyboardEvent| { - // if let Some(Ok(app)) = unsafe { APP.as_mut().map(|app| app.write()) } { - // let msg = Msg::GlobalKeyDown { - // key: event.key(), - // shift: event.shift_key(), - // ctrl: event.ctrl_key(), - // alt: event.alt_key(), - // }; - // app.update(msg); - // } - // }) - // as Box); - // body.add_event_listener_with_callback("keyup", key_up_closure.as_ref().unchecked_ref()) - // .unwrap(); - // key_up_closure.forget(); - // } - let _app = seed::App::builder(update, view) .routes(routes) .after_mount(after_mount) + .window_events(window_events) .build_and_start(); } @@ -276,6 +255,29 @@ fn after_mount(url: Url, orders: &mut impl Orders) -> AfterMount { AfterMount::new(model).url_handling(UrlHandling::PassToRoutes) } +fn window_events(_model: &Model) -> Vec> { + vec![keyboard_ev( + Ev::KeyDown, + move |event: web_sys::KeyboardEvent| { + let tag_name: String = seed::document() + .active_element() + .map(|el| el.tag_name().to_string()) + .unwrap_or_default(); + let key = match tag_name.to_lowercase().as_str() { + "input" | "textarea" => "".to_string(), + _ => event.key(), + }; + + Msg::GlobalKeyDown { + key, + shift: event.shift_key(), + ctrl: event.ctrl_key(), + alt: event.alt_key(), + } + }, + )] +} + #[inline] fn authorize_or_redirect(model: &Model) { match crate::shared::read_auth_token() { diff --git a/jirs-client/src/shared/styled_select_child.rs b/jirs-client/src/shared/styled_select_child.rs index 5d3895a6..bd6941c5 100644 --- a/jirs-client/src/shared/styled_select_child.rs +++ b/jirs-client/src/shared/styled_select_child.rs @@ -89,8 +89,8 @@ impl StyledSelectChildBuilder { pub fn match_text(&self, text: &str) -> bool { self.text .as_ref() - .map(|t| t.contains(text)) - .unwrap_or_default() + .map(|t| t.contains(text.to_lowercase().as_str())) + .unwrap_or(true) } pub fn add_class(mut self, name: S) -> Self