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 78156fc commit 6469799
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ export const EditRepresentationMenu = memo(
const handleRepresentationPropertyChange = throttle((key, value) => {
const r = comp.reprList.find(rep => rep.uuid === representation.uuid || rep.uuid === representation.lastKnownID);
if (r) {
let oldValue = oldRepresentation.params[key];
let change = { key, value, oldValue };

// update in ngl
r.setParameters({ [key]: value });
//update in redux
oldRepresentation.params[key] = value;
dispatch(updateComponentRepresentation(parentKey, oldRepresentation.uuid, oldRepresentation));

dispatch(updateComponentRepresentation(parentKey, oldRepresentation.uuid, oldRepresentation, change));
}
}, 250);

Expand Down
5 changes: 3 additions & 2 deletions js/reducers/ngl/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ export const deleteNglObject = target => ({
target
});

export const updateComponentRepresentation = (objectInViewID, representationID, newRepresentation) => ({
export const updateComponentRepresentation = (objectInViewID, representationID, newRepresentation, change) => ({
type: CONSTANTS.UPDATE_COMPONENT_REPRESENTATION,
representationID,
newRepresentation,
objectInViewID
objectInViewID,
change
});

export const addComponentRepresentation = (objectInViewID, newRepresentation) => ({
Expand Down
29 changes: 25 additions & 4 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ const handleUndoAction = (action, stages) => (dispatch, getState) => {
dispatch(handleCompoundAction(action, true));
break;
case actionType.REPRESENTATION_CHANGED:
dispatch(handleChangeRepresentationAction(action, false, majorView));
break;
case actionType.REPRESENTATION_ADDED:
dispatch(handleRepresentationAction(action, false, majorView));
Expand Down Expand Up @@ -685,6 +686,7 @@ const handleRedoAction = (action, stages) => (dispatch, getState) => {
dispatch(handleCompoundAction(action, false));
break;
case actionType.REPRESENTATION_CHANGED:
dispatch(handleChangeRepresentationAction(action, true, majorView));
break;
case actionType.REPRESENTATION_ADDED:
dispatch(handleRepresentationAction(action, true, majorView));
Expand Down Expand Up @@ -728,12 +730,12 @@ const handleCompoundAction = (action, isSelected) => (dispatch, getState) => {
};

const handleShoppingCartAction = (action, isAdd) => (dispatch, getState) => {
const state = getState();
if (action) {
let data = action.item;
if (isAdd) {
//dispatch(appendToBuyList(data));
dispatch(appendToBuyList(data));
} else {
//dispatch(removeFromToBuyList(data));
dispatch(removeFromToBuyList(data));
}
}
};
Expand All @@ -752,7 +754,6 @@ const addRepresentation = (parentKey, representation, nglView) => (dispatch, get
const oldRepresentation = representation;
const newRepresentationType = oldRepresentation.type;
const comp = nglView.stage.getComponentsByName(parentKey).first;
// add representation to NGL
const newRepresentation = assignRepresentationToComp(
newRepresentationType,
oldRepresentation.params,
Expand All @@ -762,6 +763,26 @@ const addRepresentation = (parentKey, representation, nglView) => (dispatch, get
dispatch(addComponentRepresentation(parentKey, newRepresentation));
};

const handleChangeRepresentationAction = (action, isAdd, nglView) => (dispatch, getState) => {
if (action) {
dispatch(changeRepresentation(isAdd, action.change, action.object_id, action.representation, nglView));
}
};

const changeRepresentation = (isAdd, change, parentKey, representation, nglView) => (dispatch, getState) => {
const comp = nglView.stage.getComponentsByName(parentKey).first;
const r = comp.reprList.find(rep => rep.uuid === representation.uuid || rep.uuid === representation.lastKnownID);
if (r && change) {
let key = change.key;
let value = isAdd ? change.value : change.oldValue;

r.setParameters({ [key]: value });
representation.params[key] = value;

dispatch(updateComponentRepresentation(parentKey, representation.uuid, representation));
}
};

const removeRepresentation = (parentKey, representation, nglView) => (dispatch, getState) => {
const comp = nglView.stage.getComponentsByName(parentKey).first;
let foundedRepresentation = undefined;
Expand Down
9 changes: 6 additions & 3 deletions js/reducers/tracking/trackingActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export const findTruckAction = (action, state) => {
object_type: actionObjectType.MOLECULE,
object_name: objectName,
object_id: objectName,
item: action.item,
text: `${objectType} ${objectName} ${actionDescription.ADDED} ${actionDescription.TO_SHOPPING_CART}`
};
}
Expand All @@ -263,6 +264,7 @@ export const findTruckAction = (action, state) => {
object_type: objectType,
object_name: objectName,
object_id: objectName,
item: action.item,
text: `${objectType} ${objectName} ${actionDescription.REMOVED} ${actionDescription.FROM_SHOPPING_CART}`
};
}
Expand Down Expand Up @@ -461,7 +463,8 @@ export const findTruckAction = (action, state) => {
object_id: action.objectInViewID,
representation_id: action.representationID,
representation: action.newRepresentation,
text: `${objectType} parameter of ${action.objectInViewID} ${actionDescription.CHANGED}`
change: action.change,
text: `${objectType} '${action.change?.key}' of ${action.objectInViewID} ${actionDescription.CHANGED} from value: ${action.change?.oldValue} to value: ${action.change?.value}`
};
} else if (action.type.includes(nglConstants.ADD_COMPONENT_REPRESENTATION)) {
let objectType = actionObjectType.REPRESENTATION;
Expand All @@ -475,7 +478,7 @@ export const findTruckAction = (action, state) => {
object_name: representationName,
object_id: action.objectInViewID,
representation: action.newRepresentation,
text: `${objectType} ${representationName} of ${action.objectInViewID} ${actionDescription.ADDED}`
text: `${objectType} '${representationName}' of ${action.objectInViewID} ${actionDescription.ADDED}`
};
} else if (action.type.includes(nglConstants.REMOVE_COMPONENT_REPRESENTATION)) {
let objectType = actionObjectType.REPRESENTATION;
Expand All @@ -489,7 +492,7 @@ export const findTruckAction = (action, state) => {
object_name: representationName,
object_id: action.objectInViewID,
representation: action.representation,
text: `${objectType} ${representationName} of ${action.objectInViewID} ${actionDescription.REMOVED}`
text: `${objectType} '${representationName}' of ${action.objectInViewID} ${actionDescription.REMOVED}`
};
}
}
Expand Down

0 comments on commit 6469799

Please sign in to comment.