2
2
* Created by ricgillams on 28/06/2018.
3
3
*/
4
4
5
- import React , { memo , useState } from 'react' ;
5
+ import React , { memo , useState , useContext , useEffect , useCallback } from 'react' ;
6
+ import { useDispatch } from 'react-redux' ;
6
7
import { Button } from '../../common/Inputs/Button' ;
7
- import { Settings , Mouse , PersonalVideo } from '@material-ui/icons' ;
8
+ import { Settings , Mouse , PersonalVideo , Undo , Redo } from '@material-ui/icons' ;
8
9
import { ButtonGroup , Grid , makeStyles , Tooltip } from '@material-ui/core' ;
9
10
import { SettingControls } from './settingsControls' ;
10
11
import DisplayControls from './displayControls/' ;
11
12
import { MouseControls } from './mouseControls' ;
13
+ import { ActionCreators as UndoActionCreators } from 'redux-undo' ;
14
+ import { undoAction , redoAction , getCanRedo , getCanUndo } from '../../../../js/reducers/tracking/dispatchActions' ;
15
+ import { NglContext } from '../../nglView/nglProvider' ;
12
16
13
17
const drawers = {
14
18
settings : 'settings' ,
@@ -27,6 +31,10 @@ const useStyles = makeStyles(theme => ({
27
31
export const ViewerControls = memo ( ( { } ) => {
28
32
const [ drawerSettings , setDrawerSettings ] = useState ( JSON . parse ( JSON . stringify ( initDrawers ) ) ) ;
29
33
const classes = useStyles ( ) ;
34
+ const dispatch = useDispatch ( ) ;
35
+ const { nglViewList } = useContext ( NglContext ) ;
36
+ const [ canUndo , setCanUndo ] = useState ( true ) ;
37
+ const [ canRedo , setCanRedo ] = useState ( false ) ;
30
38
31
39
const openDrawer = key => {
32
40
//close all and open selected by key
@@ -38,11 +46,55 @@ export const ViewerControls = memo(({}) => {
38
46
setDrawerSettings ( JSON . parse ( JSON . stringify ( initDrawers ) ) ) ;
39
47
} ;
40
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
+
41
80
return (
42
81
< >
43
82
< Grid container justify = "center" >
44
83
< Grid item >
45
84
< ButtonGroup variant = "contained" color = "primary" >
85
+ < Tooltip title = "Undo" >
86
+ < Button
87
+ size = "small"
88
+ color = "primary"
89
+ onClick = { ( ) => {
90
+ doUndo ( ) ;
91
+ } }
92
+ className = { classes . button }
93
+ disabled = { ! canUndo }
94
+ >
95
+ < Undo />
96
+ </ Button >
97
+ </ Tooltip >
46
98
< Tooltip title = "Settings controls" >
47
99
< Button
48
100
size = "small"
@@ -68,6 +120,19 @@ export const ViewerControls = memo(({}) => {
68
120
< Mouse />
69
121
</ Button >
70
122
</ Tooltip >
123
+ < Tooltip title = "Redo" >
124
+ < Button
125
+ size = "small"
126
+ color = "primary"
127
+ onClick = { ( ) => {
128
+ doRedo ( ) ;
129
+ } }
130
+ className = { classes . button }
131
+ disabled = { ! canRedo }
132
+ >
133
+ < Redo />
134
+ </ Button >
135
+ </ Tooltip >
71
136
</ ButtonGroup >
72
137
</ Grid >
73
138
</ Grid >
0 commit comments