Skip to content

Commit

Permalink
#453 Undo/Redo Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriána Kohanová committed Nov 13, 2020
1 parent 6469799 commit b519ede
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
43 changes: 34 additions & 9 deletions js/components/preview/viewerControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Created by ricgillams on 28/06/2018.
*/

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

const doUndo = () => {
dispatch(UndoActionCreators.undo());
setCanRedo(dispatch(getCanRedo()));
setCanUndo(dispatch(getCanUndo()));
dispatch(undoAction(nglViewList));
};

const doRedo = () => {
dispatch(UndoActionCreators.redo());
setCanRedo(dispatch(getCanRedo()));
setCanUndo(dispatch(getCanUndo()));
dispatch(redoAction(nglViewList));
};

const handleUserKeyPress = useCallback(e => {
var evtobj = window.event ? window.event : e;
if (evtobj.keyCode === 90 && evtobj.ctrlKey) {
doUndo();
} else if (evtobj.keyCode === 89 && evtobj.ctrlKey) {
doRedo();
}
});

useEffect(() => {
window.addEventListener('keydown', handleUserKeyPress);

return () => {
window.removeEventListener('keydown', handleUserKeyPress);
};
}, [handleUserKeyPress]);

return (
<>
<Grid container justify="center">
Expand All @@ -56,10 +87,7 @@ export const ViewerControls = memo(({}) => {
size="small"
color="primary"
onClick={() => {
dispatch(UndoActionCreators.undo());
setCanRedo(dispatch(getCanRedo()));
setCanUndo(dispatch(getCanUndo()));
dispatch(undoAction(nglViewList));
doUndo();
}}
className={classes.button}
disabled={!canUndo}
Expand Down Expand Up @@ -97,10 +125,7 @@ export const ViewerControls = memo(({}) => {
size="small"
color="primary"
onClick={() => {
dispatch(UndoActionCreators.redo());
setCanRedo(dispatch(getCanRedo()));
setCanUndo(dispatch(getCanUndo()));
dispatch(redoAction(nglViewList));
doRedo();
}}
className={classes.button}
disabled={!canRedo}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"react-redux": "^7.1.3",
"react-router-dom": "^5.1.2",
"react-svg-inline": "^2.1.0",
"react-undo": "^1.2.1",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-logger": "^3.0.6",
Expand Down

0 comments on commit b519ede

Please sign in to comment.