@@ -7,14 +7,17 @@ import { OwlComponent } from '../../../plugin-owl/src/OwlComponent';
7
7
import { CommandIdentifier , CommandParams } from '../../../core/src/Dispatcher' ;
8
8
import { nodeName } from '../../../utils/src/utils' ;
9
9
import { hooks } from '@odoo/owl' ;
10
+ import { ProcessKeydownParams } from '../../../plugin-dom-layout/src/DomLayout' ;
10
11
11
- ////////////////////////////// todo: use API ///////////////////////////////////
12
+ const keystrokeDiv = document . createElement ( 'div' ) ;
13
+ keystrokeDiv . id = 'keys_log' ;
12
14
13
15
interface DevToolsState {
14
16
closed : boolean ; // Are the devtools open?
15
17
height : number ; // In px
16
18
currentTab : string ; // Name of the current tab
17
19
commands : Array < [ CommandIdentifier , CommandParams ] > ;
20
+ displayKeystrokes : boolean ;
18
21
}
19
22
20
23
export class DevToolsComponent < T = { } > extends OwlComponent < T > {
@@ -33,18 +36,34 @@ export class DevToolsComponent<T = {}> extends OwlComponent<T> {
33
36
currentTab : 'inspector' ,
34
37
height : 300 ,
35
38
commands : [ ] , // Stack of all commands executed since init.
39
+ displayKeystrokes : false ,
36
40
} ;
37
- localStorage = [ 'closed' , 'currentTab' , 'height' ] ;
41
+ localStorage = [ 'closed' , 'currentTab' , 'height' , 'displayKeystrokes' ] ;
38
42
// For resizing/opening (see toggleClosed)
39
43
_heightOnLastMousedown : number ;
40
44
41
45
async willStart ( ) : Promise < void > {
42
46
this . env . editor . dispatcher . registerCommandHook ( '*' , this . addCommand . bind ( this ) ) ;
43
47
this . env . editor . dispatcher . registerCommandHook ( '@commit' , this . render . bind ( this ) ) ;
48
+ this . env . editor . dispatcher . registerCommandHook (
49
+ '@layout-keydown' ,
50
+ this . displayKeystroke . bind ( this ) ,
51
+ ) ;
44
52
return super . willStart ( ) ;
45
53
}
54
+ async mounted ( ) : Promise < void > {
55
+ if ( this . state . displayKeystrokes ) {
56
+ if ( ! document . getElementById ( 'keys_log' ) ) {
57
+ document . body . appendChild ( keystrokeDiv ) ;
58
+ }
59
+ }
60
+ return super . mounted ( ) ;
61
+ }
46
62
willUnmount ( ) : void {
47
63
this . state . commands = [ ] ;
64
+ if ( this . state . displayKeystrokes && keystrokeDiv . parentNode === document . body ) {
65
+ document . body . removeChild ( keystrokeDiv ) ;
66
+ }
48
67
}
49
68
50
69
//--------------------------------------------------------------------------
@@ -69,6 +88,45 @@ export class DevToolsComponent<T = {}> extends OwlComponent<T> {
69
88
this . state . currentTab = 'inspector' ;
70
89
( this . inspectorRef . comp as InspectorComponent ) ?. inspectDom ( ) ;
71
90
}
91
+ /**
92
+ * Toggle display keystrokes.
93
+ */
94
+ toggleKeystrokes ( ) : void {
95
+ this . state . displayKeystrokes = ! this . state . displayKeystrokes ;
96
+ if ( this . state . displayKeystrokes ) {
97
+ if ( ! document . getElementById ( 'keys_log' ) ) {
98
+ document . body . appendChild ( keystrokeDiv ) ;
99
+ }
100
+ } else if ( keystrokeDiv . parentNode === document . body ) {
101
+ document . body . removeChild ( keystrokeDiv ) ;
102
+ }
103
+ }
104
+ /**
105
+ * Display the key hit on the screen.
106
+ *
107
+ * @param params
108
+ */
109
+ displayKeystroke ( params : ProcessKeydownParams ) : void {
110
+ if ( ! this . state . displayKeystrokes ) return ;
111
+ const ev = params . event ;
112
+ keystrokeDiv . textContent = '' ;
113
+ keystrokeDiv . className = '' ;
114
+ if ( [ 'Control' , 'Alt' , 'Shift' , 'Meta' ] . includes ( ev . key ) ) {
115
+ keystrokeDiv . className = 'has-key' ;
116
+ keystrokeDiv . textContent = `'${ ev . key } '` ;
117
+ } else if ( ev . key ) {
118
+ keystrokeDiv . className = 'has-key' ;
119
+ const keyModifiers = [
120
+ ev . metaKey ? 'Meta+' : false ,
121
+ ev . ctrlKey ? 'Ctrl+' : false ,
122
+ ev . shiftKey ? 'Shift+' : false ,
123
+ ev . altKey ? 'Alt+' : false ,
124
+ ]
125
+ . filter ( mod => mod )
126
+ . join ( '' ) ;
127
+ keystrokeDiv . textContent = `${ keyModifiers } '${ ev . key } '` ;
128
+ }
129
+ }
72
130
/**
73
131
* Add the recent dispatching of the given command with the given arguments.
74
132
*/
0 commit comments