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 all 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
22 changes: 21 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,9 @@ 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 +191,8 @@ 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;
}
69 changes: 63 additions & 6 deletions src/components/Burst.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
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';

class Burst extends Component {
constructor(props) {
super(props);

this.state = {
idx: 1,
min: -10,
max: 10,
step: 1,
isCapturing: false,
canUndo: false,
prevFrames: {},
prevFrameIDs: [],
prevCalcState: {},
errors: {}
};

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

componentDidUpdate(prevProps) {
if (this.props.frameIDs.length !== prevProps.frameIDs.length) {
this.setState({ canUndo: false });
}
}

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

handleRequestBurst() {
const { requestBurst, expanded, ...imgOpts } = this.props;
requestBurst({ ...this.state, ...imgOpts });
async handleRequestBurst() {
this.setState({ isCapturing: true, canUndo: false });
const { requestBurst, expanded, frames, frameIDs, ...imgOpts } = this.props;
const prevCalcState = getCalcState();
const undoData = await requestBurst({
...this.state,
...imgOpts,
frames,
frameIDs
});
if (undoData) {
const { prevFrames, prevFrameIDs } = undoData;
this.setState({
isCapturing: false,
canUndo: true,
prevFrames,
prevFrameIDs,
prevCalcState
});
} else {
this.setState({ isCapturing: false });
}
}

handleUndoBurst() {
const { undoBurst } = this.props;
const { prevFrames, prevFrameIDs, prevCalcState } = this.state;
undoBurst(prevFrames, prevFrameIDs);
setCalcState(prevCalcState);
this.setState({
canUndo: false,
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.canUndo ? (
<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;
8 changes: 8 additions & 0 deletions src/lib/calc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ export const setSliderByIndex = (idx, val) => {
const identifier = match[1];
calculator.setExpression({ id, latex: `${identifier}=${val}` });
};

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

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