2
2
* Created by ricgillams on 28/06/2018.
3
3
*/
4
4
5
- import React , { memo , useState , useContext } from 'react' ;
5
+ import React , { memo , useState , useContext , useEffect , useCallback } from 'react' ;
6
6
import { useDispatch } from 'react-redux' ;
7
7
import { Button } from '../../common/Inputs/Button' ;
8
8
import { Settings , Mouse , PersonalVideo , Undo , Redo } from '@material-ui/icons' ;
@@ -46,6 +46,37 @@ export const ViewerControls = memo(({}) => {
46
46
setDrawerSettings ( JSON . parse ( JSON . stringify ( initDrawers ) ) ) ;
47
47
} ;
48
48
49
+ const doUndo = ( ) => {
50
+ dispatch ( UndoActionCreators . undo ( ) ) ;
51
+ setCanRedo ( dispatch ( getCanRedo ( ) ) ) ;
52
+ setCanUndo ( dispatch ( getCanUndo ( ) ) ) ;
53
+ dispatch ( undoAction ( nglViewList ) ) ;
54
+ } ;
55
+
56
+ const doRedo = ( ) => {
57
+ dispatch ( UndoActionCreators . redo ( ) ) ;
58
+ setCanRedo ( dispatch ( getCanRedo ( ) ) ) ;
59
+ setCanUndo ( dispatch ( getCanUndo ( ) ) ) ;
60
+ dispatch ( redoAction ( nglViewList ) ) ;
61
+ } ;
62
+
63
+ const handleUserKeyPress = useCallback ( e => {
64
+ var evtobj = window . event ? window . event : e ;
65
+ if ( evtobj . keyCode === 90 && evtobj . ctrlKey ) {
66
+ doUndo ( ) ;
67
+ } else if ( evtobj . keyCode === 89 && evtobj . ctrlKey ) {
68
+ doRedo ( ) ;
69
+ }
70
+ } ) ;
71
+
72
+ useEffect ( ( ) => {
73
+ window . addEventListener ( 'keydown' , handleUserKeyPress ) ;
74
+
75
+ return ( ) => {
76
+ window . removeEventListener ( 'keydown' , handleUserKeyPress ) ;
77
+ } ;
78
+ } , [ handleUserKeyPress ] ) ;
79
+
49
80
return (
50
81
< >
51
82
< Grid container justify = "center" >
@@ -56,10 +87,7 @@ export const ViewerControls = memo(({}) => {
56
87
size = "small"
57
88
color = "primary"
58
89
onClick = { ( ) => {
59
- dispatch ( UndoActionCreators . undo ( ) ) ;
60
- setCanRedo ( dispatch ( getCanRedo ( ) ) ) ;
61
- setCanUndo ( dispatch ( getCanUndo ( ) ) ) ;
62
- dispatch ( undoAction ( nglViewList ) ) ;
90
+ doUndo ( ) ;
63
91
} }
64
92
className = { classes . button }
65
93
disabled = { ! canUndo }
@@ -97,10 +125,7 @@ export const ViewerControls = memo(({}) => {
97
125
size = "small"
98
126
color = "primary"
99
127
onClick = { ( ) => {
100
- dispatch ( UndoActionCreators . redo ( ) ) ;
101
- setCanRedo ( dispatch ( getCanRedo ( ) ) ) ;
102
- setCanUndo ( dispatch ( getCanUndo ( ) ) ) ;
103
- dispatch ( redoAction ( nglViewList ) ) ;
128
+ doRedo ( ) ;
104
129
} }
105
130
className = { classes . button }
106
131
disabled = { ! canRedo }
0 commit comments