Skip to content

Commit 14e0a1f

Browse files
author
Adriána Kohanová
committed
#485 Put tooltips on undo/redo buttons that show the action being undone/redone
1 parent 602eeb8 commit 14e0a1f

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

js/components/preview/viewerControls/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import { SettingControls } from './settingsControls';
1111
import DisplayControls from './displayControls/';
1212
import { MouseControls } from './mouseControls';
1313
import { ActionCreators as UndoActionCreators } from 'redux-undo';
14-
import { undoAction, redoAction, getCanRedo, getCanUndo } from '../../../../js/reducers/tracking/dispatchActions';
14+
import {
15+
undoAction,
16+
redoAction,
17+
getCanRedo,
18+
getCanUndo,
19+
getUndoActionText,
20+
getRedoActionText
21+
} from '../../../../js/reducers/tracking/dispatchActions';
1522
import { NglContext } from '../../nglView/nglProvider';
1623

1724
const drawers = {
@@ -33,6 +40,8 @@ export const ViewerControls = memo(({}) => {
3340
const classes = useStyles();
3441
const dispatch = useDispatch();
3542
const { nglViewList } = useContext(NglContext);
43+
const [undoTooltip, setUndoTooltip] = useState('Undo');
44+
const [redoTooltip, setRedoTooltip] = useState('Redo');
3645
const [canUndo, setCanUndo] = useState(true);
3746
const [canRedo, setCanRedo] = useState(false);
3847

@@ -51,13 +60,19 @@ export const ViewerControls = memo(({}) => {
5160
setCanRedo(dispatch(getCanRedo()));
5261
setCanUndo(dispatch(getCanUndo()));
5362
dispatch(undoAction(nglViewList));
63+
64+
setUndoTooltip(dispatch(getUndoActionText()));
65+
setRedoTooltip(dispatch(getRedoActionText()));
5466
};
5567

5668
const doRedo = () => {
5769
dispatch(UndoActionCreators.redo());
5870
setCanRedo(dispatch(getCanRedo()));
5971
setCanUndo(dispatch(getCanUndo()));
6072
dispatch(redoAction(nglViewList));
73+
74+
setUndoTooltip(dispatch(getUndoActionText()));
75+
setRedoTooltip(dispatch(getRedoActionText()));
6176
};
6277

6378
const handleUserKeyPress = useCallback(e => {
@@ -82,7 +97,7 @@ export const ViewerControls = memo(({}) => {
8297
<Grid container justify="center">
8398
<Grid item>
8499
<ButtonGroup variant="contained" color="primary">
85-
<Tooltip title="Undo">
100+
<Tooltip title={undoTooltip}>
86101
<Button
87102
size="small"
88103
color="primary"
@@ -120,7 +135,7 @@ export const ViewerControls = memo(({}) => {
120135
<Mouse />
121136
</Button>
122137
</Tooltip>
123-
<Tooltip title="Redo">
138+
<Tooltip title={redoTooltip}>
124139
<Button
125140
size="small"
126141
color="primary"

js/reducers/tracking/dispatchActions.js

+56-13
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,6 @@ const restoreAllSelectionActions = (moleculesAction, stage, isSelection) => (dis
834834
};
835835

836836
const restoreAllSelectionByTypeActions = (moleculesAction, stage, isSelection) => (dispatch, getState) => {
837-
838837
let actions =
839838
isSelection === true
840839
? moleculesAction.filter(
@@ -1092,36 +1091,70 @@ const getCompound = (action, state) => {
10921091
};
10931092

10941093
export const undoAction = (stages = []) => (dispatch, getState) => {
1095-
const state = getState();
1096-
let action = null;
1097-
10981094
dispatch(setIsUndoRedoAction(true));
1095+
let action = dispatch(getUndoAction());
1096+
if (action) {
1097+
Promise.resolve(dispatch(handleUndoAction(action, stages))).then(() => {
1098+
dispatch(setIsUndoRedoAction(false));
1099+
});
1100+
}
1101+
};
10991102

1103+
const getUndoAction = () => (dispatch, getState) => {
1104+
const state = getState();
11001105
const actionUndoList = state.undoableTrackingReducers.future;
1106+
1107+
let action = { text: '' };
11011108
let actions = actionUndoList && actionUndoList[0];
11021109
if (actions) {
11031110
let actionsLenght = actions.undo_redo_actions_list.length;
11041111
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
11051112
action = actions.undo_redo_actions_list[actionsLenght];
1106-
1107-
Promise.resolve(dispatch(handleUndoAction(action, stages))).then(() => {
1108-
dispatch(setIsUndoRedoAction(false));
1109-
});
11101113
}
1114+
1115+
return action;
11111116
};
11121117

1113-
export const redoAction = (stages = []) => (dispatch, getState) => {
1118+
const getRedoAction = () => (dispatch, getState) => {
11141119
const state = getState();
1115-
let action = null;
1116-
1117-
dispatch(setIsUndoRedoAction(true));
1118-
11191120
const actions = state.undoableTrackingReducers.present;
1121+
1122+
let action = { text: '' };
11201123
if (actions) {
11211124
let actionsLenght = actions.undo_redo_actions_list.length;
11221125
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
11231126
action = actions.undo_redo_actions_list[actionsLenght];
1127+
}
11241128

1129+
return action;
1130+
};
1131+
1132+
const getNextUndoAction = () => (dispatch, getState) => {
1133+
const state = getState();
1134+
const actionUndoList = state.undoableTrackingReducers.present;
1135+
1136+
let action = { text: '' };
1137+
let actions = actionUndoList && actionUndoList.undo_redo_actions_list;
1138+
if (actions) {
1139+
let actionsLenght = actions.length;
1140+
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
1141+
action = actions[actionsLenght];
1142+
}
1143+
1144+
return action;
1145+
};
1146+
1147+
const getNextRedoAction = () => (dispatch, getState) => {
1148+
const state = getState();
1149+
let action = { text: '' };
1150+
1151+
return action;
1152+
};
1153+
1154+
export const redoAction = (stages = []) => (dispatch, getState) => {
1155+
dispatch(setIsUndoRedoAction(true));
1156+
let action = dispatch(getRedoAction());
1157+
if (action) {
11251158
Promise.resolve(dispatch(dispatch(handleRedoAction(action, stages)))).then(() => {
11261159
dispatch(setIsUndoRedoAction(false));
11271160
});
@@ -1709,6 +1742,16 @@ export const getCanRedo = () => (dispatch, getState) => {
17091742
return state.undoableTrackingReducers.future.length > 0;
17101743
};
17111744

1745+
export const getUndoActionText = () => (dispatch, getState) => {
1746+
let action = dispatch(getNextUndoAction());
1747+
return action?.text ?? '';
1748+
};
1749+
1750+
export const getRedoActionText = () => (dispatch, getState) => {
1751+
let action = dispatch(getNextRedoAction());
1752+
return action?.text ?? '';
1753+
};
1754+
17121755
export const appendAndSendTrackingActions = trackAction => (dispatch, getState) => {
17131756
const state = getState();
17141757
const isUndoRedoAction = state.trackingReducers.isUndoRedoAction;

0 commit comments

Comments
 (0)