Skip to content

Commit b519ede

Browse files
author
Adriána Kohanová
committed
#453 Undo/Redo Actions
1 parent 6469799 commit b519ede

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

js/components/preview/viewerControls/index.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Created by ricgillams on 28/06/2018.
33
*/
44

5-
import React, { memo, useState, useContext } from 'react';
5+
import React, { memo, useState, useContext, useEffect, useCallback } from 'react';
66
import { useDispatch } from 'react-redux';
77
import { Button } from '../../common/Inputs/Button';
88
import { Settings, Mouse, PersonalVideo, Undo, Redo } from '@material-ui/icons';
@@ -46,6 +46,37 @@ export const ViewerControls = memo(({}) => {
4646
setDrawerSettings(JSON.parse(JSON.stringify(initDrawers)));
4747
};
4848

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+
4980
return (
5081
<>
5182
<Grid container justify="center">
@@ -56,10 +87,7 @@ export const ViewerControls = memo(({}) => {
5687
size="small"
5788
color="primary"
5889
onClick={() => {
59-
dispatch(UndoActionCreators.undo());
60-
setCanRedo(dispatch(getCanRedo()));
61-
setCanUndo(dispatch(getCanUndo()));
62-
dispatch(undoAction(nglViewList));
90+
doUndo();
6391
}}
6492
className={classes.button}
6593
disabled={!canUndo}
@@ -97,10 +125,7 @@ export const ViewerControls = memo(({}) => {
97125
size="small"
98126
color="primary"
99127
onClick={() => {
100-
dispatch(UndoActionCreators.redo());
101-
setCanRedo(dispatch(getCanRedo()));
102-
setCanUndo(dispatch(getCanUndo()));
103-
dispatch(redoAction(nglViewList));
128+
doRedo();
104129
}}
105130
className={classes.button}
106131
disabled={!canRedo}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"react-redux": "^7.1.3",
6767
"react-router-dom": "^5.1.2",
6868
"react-svg-inline": "^2.1.0",
69+
"react-undo": "^1.2.1",
6970
"redux": "^4.0.5",
7071
"redux-devtools-extension": "^2.13.8",
7172
"redux-logger": "^3.0.6",

0 commit comments

Comments
 (0)