Skip to content

Commit

Permalink
- finished fixing the undo/redo behavior for viewer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Feb 2, 2021
1 parent 0104ed4 commit ed95f12
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
3 changes: 1 addition & 2 deletions js/components/preview/viewerControls/settingsControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { setNglViewParams } from '../../../reducers/ngl/actions';
import { setNglBckGrndColor, setNglClipNear, setNglClipFar, setNglClipDist, setNglFogNear, setNglFogFar } from '../../../reducers/ngl/dispatchActions';
import { NglContext } from '../../nglView/nglProvider';
import { VIEWS } from '../../../constants/constants';
import { throttle, debounce } from 'lodash';


const useStyles = makeStyles(theme => ({
Expand Down Expand Up @@ -75,7 +74,7 @@ export const SettingControls = memo(({ open, onClose }) => {
step={1}
min={0}
max={100}
onChange={debounce((e, value) => dispatch(setNglClipNear(value, viewParams[NGL_PARAMS.clipNear], majorView)), 50)}
onChange={(e, value) => dispatch(setNglClipNear(value, viewParams[NGL_PARAMS.clipNear], majorView))}
/>
</Grid>
</Grid>
Expand Down
48 changes: 27 additions & 21 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import {
setInspirationMoleculeDataList
} from '../../components/datasets/redux/actions';
import { selectVectorAndResetCompounds } from '../../../js/reducers/selection/dispatchActions';
import { ActionCreators as UndoActionCreators } from '../../undoredo/actions'

export const addCurrentActionsListToSnapshot = (snapshot, project, nglViewList) => async (dispatch, getState) => {
let projectID = project && project.projectID;
Expand Down Expand Up @@ -2231,7 +2232,12 @@ export const appendAndSendTrackingActions = trackAction => async (dispatch, getS
if (isUndoRedoAction === false) {
const undoRedoActionList = state.trackingReducers.undo_redo_actions_list;
const mergedUndoRedoActionList = mergeActions(trackAction, [...undoRedoActionList]);
dispatch(setUndoRedoActionList(mergedUndoRedoActionList.list));
if (mergedActionList.merged) {
dispatch(setUndoRedoActionList(mergedUndoRedoActionList.list));
dispatch(UndoActionCreators.removeLastPast());
} else {
dispatch(setUndoRedoActionList(mergedUndoRedoActionList.list));
}
}
}
dispatch(setIsActionTracking(false));
Expand All @@ -2240,26 +2246,26 @@ export const appendAndSendTrackingActions = trackAction => async (dispatch, getS

export const mergeActions = (trackAction, list) => {
let merged = false;
// if (needsToBeMerged(trackAction)) {
// let newList = [];
// if (list.length > 0) {
// const lastEntry = list[list.length - 1];
// if (isSameTypeOfAction(trackAction, lastEntry) && isActionWithinTimeLimit(lastEntry, trackAction)) {
// trackAction.oldSetting = lastEntry.oldSetting;
// trackAction.text = trackAction.getText();
// newList = [...list.slice(0, list.length - 1), trackAction];
// merged = true;
// } else {
// newList = [...list, trackAction];
// }
// } else {
// newList.push(trackAction);
// }
// return {merged: merged, list: newList};
// } else {
// return {merged: merged, list: [...list, trackAction]};
// }
return {merged: merged, list: [...list, trackAction]};
if (needsToBeMerged(trackAction)) {
let newList = [];
if (list.length > 0) {
const lastEntry = list[list.length - 1];
if (isSameTypeOfAction(trackAction, lastEntry) && isActionWithinTimeLimit(lastEntry, trackAction)) {
trackAction.oldSetting = lastEntry.oldSetting;
trackAction.text = trackAction.getText();
newList = [...list.slice(0, list.length - 1), trackAction];
merged = true;
} else {
newList = [...list, trackAction];
}
} else {
newList.push(trackAction);
}
return {merged: merged, list: newList};
} else {
return {merged: merged, list: [...list, trackAction]};
}
// return {merged: merged, list: [...list, trackAction]};
};

const needsToBeMerged = trackAction => {
Expand Down
6 changes: 5 additions & 1 deletion js/undoredo/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export const ActionTypes = {
JUMP_TO_FUTURE: '@@redux-undo/JUMP_TO_FUTURE',
JUMP_TO_PAST: '@@redux-undo/JUMP_TO_PAST',
JUMP: '@@redux-undo/JUMP',
CLEAR_HISTORY: '@@redux-undo/CLEAR_HISTORY'
CLEAR_HISTORY: '@@redux-undo/CLEAR_HISTORY',
REMOVE_LAST_PAST: '@@redux-undo/REMOVE_LAST_PAST'
}

export const ActionCreators = {
Expand All @@ -25,5 +26,8 @@ export const ActionCreators = {
},
clearHistory () {
return { type: ActionTypes.CLEAR_HISTORY }
},
removeLastPast() {
return {type: ActionTypes.REMOVE_LAST_PAST}
}
}
14 changes: 14 additions & 0 deletions js/undoredo/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ function createHistory (state, ignoreInitialState) {
return history
}

function removeLastPast(history) {
const { past, present, future } = history;

const newPast = [...past];
newPast.pop();

return newHistory(newPast, present, future)
}

// insert: insert `state` into history, which means adding the current state
// into `past`, setting the new `state` as `present` and erasing
// the `future`.
Expand Down Expand Up @@ -87,6 +96,7 @@ export function undoable (reducer, rawConfig = {}) {
jumpToPastType: ActionTypes.JUMP_TO_PAST,
jumpToFutureType: ActionTypes.JUMP_TO_FUTURE,
jumpType: ActionTypes.JUMP,
removeLastPast: ActionTypes.REMOVE_LAST_PAST,
neverSkipReducer: false,
ignoreInitialState: false,
syncFilter: false,
Expand Down Expand Up @@ -185,6 +195,10 @@ export function undoable (reducer, rawConfig = {}) {
debug.log(`perform jump to ${action.index}`)
debug.end(res)
return skipReducer(res, action, ...slices)

case config.removeLastPast:
res = removeLastPast(history);
return skipReducer(res, action, ...slices);

case actionTypeAmongClearHistoryType(action.type, config.clearHistoryType):
res = createHistory(history.present, config.ignoreInitialState)
Expand Down

0 comments on commit ed95f12

Please sign in to comment.