@@ -2,78 +2,137 @@ import React, { useRef, useState } from 'react';
22import  PropTypes  from  'prop-types' ; 
33import  {  useEditorKeyMap  }  from  './Editor/contexts' ; 
44
5- function  KeyboardShortcutItem ( {  shortcut ,  desc  } )  { 
5+ function  KeyboardShortcutItem ( {  desc ,  keyName  } )  { 
66  const  [ edit ,  setEdit ]  =  useState ( false ) ; 
77  const  pressedKeyCombination  =  useRef ( { } ) ; 
88  const  inputRef  =  useRef ( null ) ; 
9-   const  {  updateKeyMap }  =  useEditorKeyMap ( ) ; 
9+   const  {  updateKeyMap,  keyMaps  }  =  useEditorKeyMap ( ) ; 
1010
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 )  =>  { 
1222    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' ) ; 
1858    } 
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 ] ; 
1979  } ; 
2080
2181  return  ( 
2282    < 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+       > 
2492        ✎
2593      </ 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 > 
26116      < span 
27117        className = "keyboard-shortcut__command" 
28118        role = "textbox" 
29119        ref = { inputRef } 
30120        tabIndex = { 0 } 
31121        contentEditable = { edit } 
32122        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 } 
66125      > 
67-         { shortcut } 
126+         { keyMaps [ keyName ] } 
68127      </ span > 
69128      < span > { desc } </ span > 
70129    </ li > 
71130  ) ; 
72131} 
73132
74133KeyboardShortcutItem . propTypes  =  { 
75-   shortcut : PropTypes . string . isRequired , 
76-   desc : PropTypes . string . isRequired 
134+   desc : PropTypes . string . isRequired , 
135+   keyName : PropTypes . string . isRequired 
77136} ; 
78137
79138export  default  KeyboardShortcutItem ; 
0 commit comments