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) #13

Merged
merged 8 commits into from
Jun 25, 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
1 change: 1 addition & 0 deletions src/actions/action-creators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ describe('Action creators', () => {
oversample: false,
step: 1,
width: 300,
frameIDs: [0, 1, 2],
mode: 'contain',
left: -10,
right: 10,
Expand Down
13 changes: 11 additions & 2 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -197,6 +202,8 @@ export const requestBurst = opts => async (dispatch, getState) => {
width,
height,
oversample,
frames,
frameIDs,
left,
right,
top,
Expand Down Expand Up @@ -229,13 +236,13 @@ export const requestBurst = opts => async (dispatch, getState) => {
dispatch(flashError(badSettingsInput(settingsErrors)));
return;
}

const prevFrames = { ...frames };
const prevFrameIDs = [...frameIDs];
const boundErrors = getBoundErrors({ top, bottom, left, right });
if (Object.keys(boundErrors).length) {
dispatch(flashError(invalidBounds(boundErrors)));
return;
}

let imageData;
let sliderErrorMessage;
for (let val = min; val <= max; val += step) {
Expand All @@ -248,6 +255,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 @@ -61,6 +61,11 @@
border-color: #e79600;
}

.capturing {
opacity: 0.5;
pointer-events: none;
}

.Bound-text {
margin: 1rem;
font-size: 17px;
Expand Down
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';
export const UPDATE_TEXT = 'UPDATE_TEXT';
export const UPDATE_TEXT_COLOR = 'UPDATE_TEXT_COLOR';
export const UPDATE_GIF_FILENAME = 'UPDATE_GIF_FILENAME';
Expand Down
9 changes: 6 additions & 3 deletions src/containers/BurstContainer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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;
const { left, right, top, bottom } = settings.bounds;
const { strategy } = settings;

Expand All @@ -14,6 +15,8 @@ const mapStateToProps = (state, ownProps) => {
width,
height,
oversample,
frames,
frameIDs,
left,
right,
top,
Expand All @@ -24,7 +27,7 @@ const mapStateToProps = (state, ownProps) => {

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 @@ -46,6 +46,14 @@ export const setSliderByIndex = (idx, val) => {
calculator.setExpression({ id, latex: `${identifier}=${val}` });
};

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

export const setCalcState = state => {
return calculator.setState(state);
};

export const saveCurrentGraph = async (name, frames, frameIDs) => {
const graph = calculator.getState();
const preview = await getImageData({
Expand Down
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_GIF_FILENAME,
UPDATE_IMAGE_SETTING,
UPDATE_BOUNDS_SETTING,
Expand Down Expand Up @@ -59,6 +60,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