Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/#485' into allfunctionality
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Jan 20, 2021
2 parents 1bce81d + 29e4ff4 commit db5330b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 19 deletions.
30 changes: 25 additions & 5 deletions js/components/preview/viewerControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
*/

import React, { memo, useState, useContext, useEffect, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { Button } from '../../common/Inputs/Button';
import { Settings, Mouse, PersonalVideo, Undo, Redo } from '@material-ui/icons';
import { ButtonGroup, Grid, makeStyles, Tooltip } from '@material-ui/core';
import { SettingControls } from './settingsControls';
import DisplayControls from './displayControls/';
import { MouseControls } from './mouseControls';
import { ActionCreators as UndoActionCreators } from 'redux-undo';
import { undoAction, redoAction, getCanRedo, getCanUndo } from '../../../../js/reducers/tracking/dispatchActions';
import {
undoAction,
redoAction,
getCanRedo,
getCanUndo,
getUndoActionText,
getRedoActionText
} from '../../../../js/reducers/tracking/dispatchActions';
import { NglContext } from '../../nglView/nglProvider';

const drawers = {
Expand All @@ -33,8 +40,11 @@ export const ViewerControls = memo(({}) => {
const classes = useStyles();
const dispatch = useDispatch();
const { nglViewList } = useContext(NglContext);
const [undoTooltip, setUndoTooltip] = useState('Undo');
const [redoTooltip, setRedoTooltip] = useState('Redo');
const [canUndo, setCanUndo] = useState(true);
const [canRedo, setCanRedo] = useState(false);
const isActionTracking = useSelector(state => state.trackingReducers.isActionTracking);

const openDrawer = key => {
//close all and open selected by key
Expand All @@ -51,13 +61,19 @@ export const ViewerControls = memo(({}) => {
setCanRedo(dispatch(getCanRedo()));
setCanUndo(dispatch(getCanUndo()));
dispatch(undoAction(nglViewList));

setUndoTooltip(dispatch(getUndoActionText()));
setRedoTooltip(dispatch(getRedoActionText()));
};

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

setUndoTooltip(dispatch(getUndoActionText()));
setRedoTooltip(dispatch(getRedoActionText()));
};

const handleUserKeyPress = useCallback(e => {
Expand All @@ -70,19 +86,23 @@ export const ViewerControls = memo(({}) => {
});

useEffect(() => {
if (isActionTracking === false) {
setUndoTooltip(dispatch(getUndoActionText()));
setRedoTooltip(dispatch(getRedoActionText()));
}
window.addEventListener('keydown', handleUserKeyPress);

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

return (
<>
<Grid container justify="center">
<Grid item>
<ButtonGroup variant="contained" color="primary">
<Tooltip title="Undo">
<Tooltip title={undoTooltip}>
<Button
size="small"
color="primary"
Expand Down Expand Up @@ -120,7 +140,7 @@ export const ViewerControls = memo(({}) => {
<Mouse />
</Button>
</Tooltip>
<Tooltip title="Redo">
<Tooltip title={redoTooltip}>
<Button
size="small"
color="primary"
Expand Down
7 changes: 7 additions & 0 deletions js/reducers/tracking/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ export const setIsActionsRestoring = function(isActionRestoring, isActionRestore
};
};

export const setIsActionTracking = function(isActionTracking) {
return {
type: constants.SET_IS_ACTION_TRACKING,
isActionTracking: isActionTracking
};
};

export const resetTrackingState = function() {
return {
type: constants.RESET_TRACKING_STATE
Expand Down
1 change: 1 addition & 0 deletions js/reducers/tracking/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const constants = {
SET_PROJECT_ACTIONS_LIST: prefix + 'SET_PROJECT_ACTIONS_LIST',
SET_IS_ACTIONS_SAVING: prefix + 'SET_IS_ACTIONS_SAVING',
SET_IS_ACTIONS_RESTORING: prefix + 'SET_IS_ACTIONS_RESTORING',
SET_IS_ACTION_TRACKING: prefix + 'SET_IS_ACTION_TRACKING',
RESET_TRACKING_STATE: prefix + 'RESET_TRACKING_STATE',
SET_TRACKING_IMAGE_SOURCE: prefix + 'SET_TRACKING_IMAGE_SOURCE',
SET_SNAPSOT_IMAGE_ACTIONS_LIST: prefix + 'SET_SNAPSOT_IMAGE_ACTIONS_LIST',
Expand Down
85 changes: 71 additions & 14 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ import {
setProjectActionList,
setIsActionsSaving,
setIsActionsRestoring,
resetTrackingState
appendToUndoRedoActionList,
resetTrackingState,
setIsActionTracking
} from './actions';
import {
setSelectedAll,
Expand Down Expand Up @@ -1119,36 +1121,80 @@ const getCompound = (action, state) => {
};

export const undoAction = (stages = []) => (dispatch, getState) => {
const state = getState();
let action = null;

dispatch(setIsUndoRedoAction(true));
let action = dispatch(getUndoAction());
if (action) {
Promise.resolve(dispatch(handleUndoAction(action, stages))).then(() => {
dispatch(setIsUndoRedoAction(false));
});
}
};

const getUndoAction = () => (dispatch, getState) => {
const state = getState();
const actionUndoList = state.undoableTrackingReducers.future;

let action = { text: '' };
let actions = actionUndoList && actionUndoList[0];
if (actions) {
let actionsLenght = actions.undo_redo_actions_list.length;
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
action = actions.undo_redo_actions_list[actionsLenght];

Promise.resolve(dispatch(handleUndoAction(action, stages))).then(() => {
dispatch(setIsUndoRedoAction(false));
});
}

return action;
};

export const redoAction = (stages = []) => (dispatch, getState) => {
const getRedoAction = () => (dispatch, getState) => {
const state = getState();
let action = null;

dispatch(setIsUndoRedoAction(true));

const actions = state.undoableTrackingReducers.present;

let action = { text: '' };
if (actions) {
let actionsLenght = actions.undo_redo_actions_list.length;
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
action = actions.undo_redo_actions_list[actionsLenght];
}

return action;
};

const getNextUndoAction = () => (dispatch, getState) => {
const state = getState();
const actionUndoList = state.undoableTrackingReducers.present;

let action = { text: '' };
let actions = actionUndoList && actionUndoList.undo_redo_actions_list;
if (actions) {
let actionsLenght = actions.length;
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
action = actions[actionsLenght];
}

return action;
};

const getNextRedoAction = () => (dispatch, getState) => {
const state = getState();
const actionUndoList = state.undoableTrackingReducers.future;

let action = { text: '' };
let actionss = actionUndoList && actionUndoList[0];

let actions = actionss && actionss.undo_redo_actions_list;
if (actions) {
let actionsLenght = actions.length;
actionsLenght = actionsLenght > 0 ? actionsLenght - 1 : actionsLenght;
action = actions[actionsLenght];
}

return action;
};

export const redoAction = (stages = []) => (dispatch, getState) => {
dispatch(setIsUndoRedoAction(true));
let action = dispatch(getRedoAction());
if (action) {
Promise.resolve(dispatch(dispatch(handleRedoAction(action, stages)))).then(() => {
dispatch(setIsUndoRedoAction(false));
});
Expand Down Expand Up @@ -1788,9 +1834,20 @@ export const getCanRedo = () => (dispatch, getState) => {
return state.undoableTrackingReducers.future.length > 0;
};

export const getUndoActionText = () => (dispatch, getState) => {
let action = dispatch(getNextUndoAction());
return action?.text ?? '';
};

export const getRedoActionText = () => (dispatch, getState) => {
let action = dispatch(getNextRedoAction());
return action?.text ?? '';
};

export const appendAndSendTrackingActions = trackAction => (dispatch, getState) => {
const state = getState();
const isUndoRedoAction = state.trackingReducers.isUndoRedoAction;
dispatch(setIsActionTracking(true));

if (trackAction && trackAction !== null) {
const actionList = state.trackingReducers.track_actions_list;
Expand All @@ -1806,7 +1863,7 @@ export const appendAndSendTrackingActions = trackAction => (dispatch, getState)
dispatch(setUndoRedoActionList(mergedUndoRedoActionList));
}
}

dispatch(setIsActionTracking(false));
dispatch(checkSendTrackingActions());
};

Expand Down
5 changes: 5 additions & 0 deletions js/reducers/tracking/trackingReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const INITIAL_STATE = {
snapshotActionImageList: [],
isActionRestoring: false,
isActionRestored: false,
isActionTracking: false,
trackingImageSource: ''
};

Expand Down Expand Up @@ -101,6 +102,10 @@ export function trackingReducers(state = INITIAL_STATE, action = {}) {
isActionRestoring: action.isActionRestoring,
isActionRestored: action.isActionRestored
});
case constants.SET_IS_ACTION_TRACKING:
return Object.assign({}, state, {
isActionTracking: action.isActionTracking
});

case constants.SET_TRACKING_IMAGE_SOURCE:
return Object.assign({}, state, {
Expand Down

0 comments on commit db5330b

Please sign in to comment.