@@ -3,12 +3,14 @@ use crate::{
3
3
schema:: { Field , SCHEMA } ,
4
4
types:: * ,
5
5
} ;
6
+ use gloo_events:: EventListener ;
6
7
use gloo_storage:: { LocalStorage , Storage } ;
7
8
use serde:: { Deserialize , Serialize } ;
8
9
use std:: {
9
10
collections:: { BTreeMap , HashMap } ,
10
11
rc:: Rc ,
11
12
} ;
13
+ use wasm_bindgen:: JsCast ;
12
14
use web_sys:: { window, InputEvent , MouseEvent } ;
13
15
use yew:: { html, prelude:: * , Html , KeyboardEvent } ;
14
16
@@ -26,7 +28,6 @@ impl GlobalState {
26
28
}
27
29
}
28
30
29
- #[ derive( PartialEq , Clone ) ]
30
31
pub struct Model {
31
32
pub global_state : Rc < GlobalState > ,
32
33
@@ -38,6 +39,8 @@ pub struct Model {
38
39
pub node_state : HashMap < Path , NodeState > ,
39
40
40
41
pub stack : Vec < String > ,
42
+
43
+ pub document_keydown_listener : EventListener ,
41
44
}
42
45
43
46
#[ derive( Clone , Debug , PartialEq ) ]
@@ -101,6 +104,7 @@ impl Component for Model {
101
104
e. stop_propagation ( ) ;
102
105
Msg :: CommandKey ( vec ! [ ] , e)
103
106
} ) ;
107
+
104
108
html ! {
105
109
<div
106
110
tabindex="0"
@@ -136,16 +140,21 @@ impl Component for Model {
136
140
}
137
141
}
138
142
139
- fn create ( _ctx : & Context < Self > ) -> Self {
140
- // let key_listener = KeyboardService::register_key_down(
141
- // &window().unwrap(),
142
- // ctx.link().callback(move |e: KeyboardEvent| {
143
- // // e.stop_propagation();
144
- // // e.stop_immediate_propagation();
145
- // // e.prevent_default();
146
- // Msg::CommandKey(e)
147
- // }),
148
- // );
143
+ fn create ( ctx : & Context < Self > ) -> Self {
144
+ let document_callback = ctx
145
+ . link ( )
146
+ . callback ( move |e : KeyboardEvent | Msg :: CommandKey ( vec ! [ ] , e) ) ;
147
+
148
+ let document_keydown_listener = gloo_events:: EventListener :: new (
149
+ & gloo_utils:: document ( ) ,
150
+ "keydown" ,
151
+ move |e : & Event | {
152
+ e. stop_propagation ( ) ;
153
+ e. dyn_ref :: < KeyboardEvent > ( ) . map ( |e| {
154
+ document_callback. emit ( e. clone ( ) ) ;
155
+ } ) ;
156
+ } ,
157
+ ) ;
149
158
let ( node_store, root) = super :: initial:: initial ( ) ;
150
159
Model {
151
160
global_state : Rc :: new ( GlobalState {
@@ -163,6 +172,8 @@ impl Component for Model {
163
172
node_state : HashMap :: new ( ) ,
164
173
165
174
stack : vec ! [ ] ,
175
+
176
+ document_keydown_listener,
166
177
}
167
178
}
168
179
0 commit comments