Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for undo burst functionality (#36) #37

Merged
merged 6 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export const addGIF = imageData => ({
payload: { imageData }
});

export const undoBurst = (frames, frameIDs) => ({
type: types.UNDO_BURST,
payload: { frames, frameIDs }
});

export const togglePane = pane => {
clearTimer();
return {
Expand Down Expand Up @@ -140,7 +145,17 @@ export const requestFrame = opts => async dispatch => {
};

export const requestBurst = opts => async (dispatch, getState) => {
const { idx, min, max, step, width, height, oversample } = opts;
const {
idx,
min,
max,
step,
width,
height,
oversample,
frames,
frameIDs
} = opts;
const imageOpts = {
width,
height,
Expand All @@ -161,6 +176,10 @@ export const requestBurst = opts => async (dispatch, getState) => {
return;
}

//
const prevFrames = { ...frames };
const prevFrameIDs = [...frameIDs];

let imageData;
let sliderErrorMessage;
for (let val = min; val <= max; val += step) {
Expand All @@ -173,6 +192,9 @@ export const requestBurst = opts => async (dispatch, getState) => {
imageData = await getImageData(imageOpts);
dispatch(addFrame(imageData));
}

//
return { prevFrames, prevFrameIDs };
};

export const startAnimation = () => (dispatch, getState) => {
Expand Down
5 changes: 5 additions & 0 deletions src/components/Burst.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@
background: #484848;
border-color: #e79600;
}

.capturing {
opacity: 0.5;
pointer-events: none;
}
67 changes: 62 additions & 5 deletions src/components/Burst.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import classNames from 'classnames';
import { getCalcState, setCalcState } from '../lib/calc-helpers';
import { getBurstErrors } from '../lib/input-helpers';
import './Burst.css';

Expand All @@ -12,11 +13,16 @@ class Burst extends Component {
min: -10,
max: 10,
step: 1,
isCapturing: false,
prevFrames: {},
prevFrameIDs: [],
prevCalcState: {},
errors: {}
};

this.handleInputUpdate = this.handleInputUpdate.bind(this);
this.handleRequestBurst = this.handleRequestBurst.bind(this);
this.handleUndoBurst = this.handleUndoBurst.bind(this);
}

handleInputUpdate(evt) {
Expand All @@ -32,9 +38,47 @@ class Burst extends Component {
this.setState(newState);
}

handleRequestBurst() {
const { requestBurst, expanded, ...imgOpts } = this.props;
requestBurst({ ...this.state, ...imgOpts });
async handleRequestBurst() {
this.setState({ isCapturing: true });
const { requestBurst, expanded, frames, frameIDs, ...imgOpts } = this.props;
// grab calculator state prior to capture
Copy link
Contributor

@japamat0 japamat0 Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With some of your nicely named function names, some of these comments may not be necessary :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const prevCalcState = getCalcState();
// capture
const undoData = await requestBurst({
...this.state,
...imgOpts,
frames,
frameIDs
});
// if capture was successful, update state accordingly
if (undoData) {
const { prevFrames, prevFrameIDs } = undoData;
this.setState({
isCapturing: false,
prevFrames,
prevFrameIDs,
prevCalcState
});
}
// update isCapturing regardless
else {
this.setState({ isCapturing: false });
}
}

handleUndoBurst() {
const { undoBurst } = this.props;
const { prevFrames, prevFrameIDs, prevCalcState } = this.state;
// dispatch action to change frames, frameIDs in state
undoBurst(prevFrames, prevFrameIDs);
// revert calculator state
setCalcState(prevCalcState);
// clear out undo data in local state
this.setState({
prevFrames: {},
prevFrameIDs: [],
prevCalcState: {}
});
}

render() {
Expand Down Expand Up @@ -91,13 +135,26 @@ class Burst extends Component {
/>
<div>
<button
className="Burst-button"
className={classNames('Burst-button', {
capturing: this.state.isCapturing
})}
onClick={this.handleRequestBurst}
aria-label="capture several frames"
>
Capture
{this.state.isCapturing ? 'Capturing...' : 'Capture'}
</button>
</div>
{this.state.prevFrameIDs.length ? (
<div>
<button
className="Burst-button"
onClick={this.handleUndoBurst}
aria-label="undo last burst"
>
Undo
</button>
</div>
) : null}
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/constants/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
export const ADD_FRAME = 'ADD_FRAME';
export const UPDATE_GIF_PROGRESS = 'UPDATE_GIF_PROGRESS';
export const ADD_GIF = 'ADD_GIF';
export const UNDO_BURST = 'UNDO_BURST';

// UI
export const UPDATE_PREVIEW_IDX = 'UPDATE_PREVIEW_IDX';
Expand Down
11 changes: 7 additions & 4 deletions src/containers/BurstContainer.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { connect } from 'react-redux';
import Burst from '../components/Burst';
import { requestBurst } from '../actions';
import { requestBurst, undoBurst } from '../actions';
import panes from '../constants/pane-types';

const mapStateToProps = (state, ownProps) => {
const { settings, ui } = state;
const { settings, ui, images } = state;
const { width, height, oversample } = settings.image;
const { frames, frameIDs } = images;

return {
expanded: ui.expandedPane === panes.BURST,
width,
height,
oversample
oversample,
frames,
frameIDs
};
};

const BurstContainer = connect(
mapStateToProps,
{ requestBurst }
{ requestBurst, undoBurst }
)(Burst);

export default BurstContainer;
10 changes: 10 additions & 0 deletions src/lib/calc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ export const setSliderByIndex = (idx, val) => {
const identifier = match[1];
calculator.setExpression({ id, latex: `${identifier}=${val}` });
};

// gets current state of calculator instance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments may not be necessary for these functions, they are perfectly described by their name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

export const getCalcState = () => {
return calculator.getState();
};

// accepts some calculator state and updates calculator instance
export const setCalcState = state => {
return calculator.setState(state);
};
8 changes: 8 additions & 0 deletions src/reducers/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ADD_FRAME,
UPDATE_GIF_PROGRESS,
ADD_GIF,
UNDO_BURST,
UPDATE_IMAGE_SETTING,
UPDATE_BOUNDS_SETTING,
UPDATE_STRATEGY,
Expand Down Expand Up @@ -53,6 +54,13 @@ const images = (state = initialState, { type, payload }) => {
gifData: payload.imageData
};

case UNDO_BURST:
return {
...state,
frames: payload.frames,
frameIDs: payload.frameIDs
};

case UPDATE_IMAGE_SETTING:
case UPDATE_BOUNDS_SETTING:
case UPDATE_STRATEGY:
Expand Down