@@ -2,78 +2,137 @@ import React, { useRef, useState } from 'react';
2
2
import PropTypes from 'prop-types' ;
3
3
import { useEditorKeyMap } from './Editor/contexts' ;
4
4
5
- function KeyboardShortcutItem ( { shortcut , desc } ) {
5
+ function KeyboardShortcutItem ( { desc , keyName } ) {
6
6
const [ edit , setEdit ] = useState ( false ) ;
7
7
const pressedKeyCombination = useRef ( { } ) ;
8
8
const inputRef = useRef ( null ) ;
9
- const { updateKeyMap } = useEditorKeyMap ( ) ;
9
+ const { updateKeyMap, keyMaps } = useEditorKeyMap ( ) ;
10
10
11
- const handleEdit = ( state ) => {
11
+ if ( ! Object . keys ( keyMaps ) . includes ( keyName ) ) {
12
+ return null ;
13
+ }
14
+
15
+ const cancelEdit = ( ) => {
16
+ setEdit ( false ) ;
17
+ pressedKeyCombination . current = { } ;
18
+ inputRef . current . innerText = keyMaps [ keyName ] ;
19
+ } ;
20
+
21
+ const handleEdit = ( state , key ) => {
12
22
setEdit ( state ) ;
13
- if ( state ) {
14
- inputRef . current . focus ( ) ;
15
- } else {
16
- inputRef . current . blur ( ) ;
17
- updateKeyMap ( 'tidy' , inputRef . current . innerText ) ;
23
+ if ( ! state ) {
24
+ updateKeyMap ( key , inputRef . current . innerText ) ;
25
+ cancelEdit ( ) ;
26
+ }
27
+ } ;
28
+
29
+ const handleKeyDown = ( event ) => {
30
+ if ( ! edit ) return ;
31
+ event . preventDefault ( ) ;
32
+ event . stopPropagation ( ) ;
33
+ let { key } = event ;
34
+ if ( key === 'Control' ) {
35
+ key = 'Ctrl' ;
36
+ }
37
+ if ( key === ' ' ) {
38
+ key = 'Space' ;
39
+ }
40
+ if ( key . length === 1 && key . match ( / [ a - z ] / i) ) {
41
+ key = key . toUpperCase ( ) ;
42
+ }
43
+
44
+ pressedKeyCombination . current [ key ] = true ;
45
+
46
+ const allKeys = Object . keys ( pressedKeyCombination . current ) . filter (
47
+ ( k ) => ! [ 'Shift' , 'Ctrl' , 'Alt' ] . includes ( k )
48
+ ) ;
49
+
50
+ if ( event . altKey ) {
51
+ allKeys . unshift ( 'Alt' ) ;
52
+ }
53
+ if ( event . ctrlKey ) {
54
+ allKeys . unshift ( 'Ctrl' ) ;
55
+ }
56
+ if ( event . shiftKey ) {
57
+ allKeys . unshift ( 'Shift' ) ;
18
58
}
59
+
60
+ event . currentTarget . innerText = allKeys . join ( '-' ) ;
61
+ } ;
62
+
63
+ const handleKeyUp = ( event ) => {
64
+ if ( ! edit ) return ;
65
+ event . preventDefault ( ) ;
66
+ event . stopPropagation ( ) ;
67
+ let { key } = event ;
68
+ if ( key === 'Control' ) {
69
+ key = 'Ctrl' ;
70
+ }
71
+ if ( key === ' ' ) {
72
+ key = 'Space' ;
73
+ }
74
+ if ( key . length === 1 && key . match ( / [ a - z ] / i) ) {
75
+ key = key . toUpperCase ( ) ;
76
+ }
77
+
78
+ delete pressedKeyCombination . current [ key ] ;
19
79
} ;
20
80
21
81
return (
22
82
< li className = "keyboard-shortcut-item" >
23
- < button type = "button" title = "edit" onClick = { ( ) => handleEdit ( ! edit ) } >
83
+ < button
84
+ type = "button"
85
+ title = "edit shortcut"
86
+ className = "keyboard-shortcut__edit"
87
+ style = { {
88
+ display : edit ? 'none' : 'block'
89
+ } }
90
+ onClick = { ( ) => handleEdit ( true , keyName ) }
91
+ >
24
92
✎
25
93
</ button >
94
+ < button
95
+ type = "button"
96
+ title = "cancel shortcut edit"
97
+ className = "keyboard-shortcut__edit"
98
+ style = { {
99
+ display : ! edit ? 'none' : 'block'
100
+ } }
101
+ onClick = { cancelEdit }
102
+ >
103
+ ⨯
104
+ </ button >
105
+ < button
106
+ type = "button"
107
+ title = "save shortcut"
108
+ className = "keyboard-shortcut__edit"
109
+ style = { {
110
+ display : ! edit ? 'none' : 'block'
111
+ } }
112
+ onClick = { ( ) => handleEdit ( false , keyName ) }
113
+ >
114
+ ✓
115
+ </ button >
26
116
< span
27
117
className = "keyboard-shortcut__command"
28
118
role = "textbox"
29
119
ref = { inputRef }
30
120
tabIndex = { 0 }
31
121
contentEditable = { edit }
32
122
suppressContentEditableWarning
33
- onKeyDown = { ( event ) => {
34
- if ( ! edit ) return ;
35
-
36
- event . preventDefault ( ) ;
37
- event . stopPropagation ( ) ;
38
- let { key } = event ;
39
- if ( key === 'Control' ) {
40
- key = 'Ctrl' ;
41
- }
42
- if ( key === ' ' ) {
43
- key = 'Space' ;
44
- }
45
-
46
- pressedKeyCombination . current [ key ] = true ;
47
-
48
- event . currentTarget . innerText = Object . keys (
49
- pressedKeyCombination . current
50
- ) . join ( '-' ) ;
51
- } }
52
- onKeyUp = { ( event ) => {
53
- if ( ! edit ) return ;
54
- event . preventDefault ( ) ;
55
- event . stopPropagation ( ) ;
56
- let { key } = event ;
57
- if ( key === 'Control' ) {
58
- key = 'Ctrl' ;
59
- }
60
- if ( key === ' ' ) {
61
- key = 'Space' ;
62
- }
63
-
64
- delete pressedKeyCombination . current [ key ] ;
65
- } }
123
+ onKeyDown = { handleKeyDown }
124
+ onKeyUp = { handleKeyUp }
66
125
>
67
- { shortcut }
126
+ { keyMaps [ keyName ] }
68
127
</ span >
69
128
< span > { desc } </ span >
70
129
</ li >
71
130
) ;
72
131
}
73
132
74
133
KeyboardShortcutItem . propTypes = {
75
- shortcut : PropTypes . string . isRequired ,
76
- desc : PropTypes . string . isRequired
134
+ desc : PropTypes . string . isRequired ,
135
+ keyName : PropTypes . string . isRequired
77
136
} ;
78
137
79
138
export default KeyboardShortcutItem ;
0 commit comments