diff --git a/Cargo.lock b/Cargo.lock index f436429..bf6bdad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -404,7 +404,9 @@ name = "linc" version = "0.1.0" dependencies = [ "console_error_panic_hook", + "gloo-events", "gloo-storage", + "gloo-utils", "hex", "html_parser", "itertools", diff --git a/Cargo.toml b/Cargo.toml index bed1919..d9c8ba6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" [dependencies] console_error_panic_hook = "*" gloo-storage = "*" +gloo-events = "*" +gloo-utils = "*" hex = "*" html_parser = "*" itertools = "*" diff --git a/src/model.rs b/src/model.rs index e21284e..063ab46 100644 --- a/src/model.rs +++ b/src/model.rs @@ -3,12 +3,14 @@ use crate::{ schema::{Field, SCHEMA}, types::*, }; +use gloo_events::EventListener; use gloo_storage::{LocalStorage, Storage}; use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, HashMap}, rc::Rc, }; +use wasm_bindgen::JsCast; use web_sys::{window, InputEvent, MouseEvent}; use yew::{html, prelude::*, Html, KeyboardEvent}; @@ -26,7 +28,6 @@ impl GlobalState { } } -#[derive(PartialEq, Clone)] pub struct Model { pub global_state: Rc, @@ -38,6 +39,8 @@ pub struct Model { pub node_state: HashMap, pub stack: Vec, + + pub document_keydown_listener: EventListener, } #[derive(Clone, Debug, PartialEq)] @@ -101,6 +104,7 @@ impl Component for Model { e.stop_propagation(); Msg::CommandKey(vec![], e) }); + html! {
) -> Self { - // let key_listener = KeyboardService::register_key_down( - // &window().unwrap(), - // ctx.link().callback(move |e: KeyboardEvent| { - // // e.stop_propagation(); - // // e.stop_immediate_propagation(); - // // e.prevent_default(); - // Msg::CommandKey(e) - // }), - // ); + fn create(ctx: &Context) -> Self { + let document_callback = ctx + .link() + .callback(move |e: KeyboardEvent| Msg::CommandKey(vec![], e)); + + let document_keydown_listener = gloo_events::EventListener::new( + &gloo_utils::document(), + "keydown", + move |e: &Event| { + e.stop_propagation(); + e.dyn_ref::().map(|e| { + document_callback.emit(e.clone()); + }); + }, + ); let (node_store, root) = super::initial::initial(); Model { global_state: Rc::new(GlobalState { @@ -163,6 +172,8 @@ impl Component for Model { node_state: HashMap::new(), stack: vec![], + + document_keydown_listener, } }