scratch-paint/src/containers/eraser-mode.jsx

88 lines
2.7 KiB
React
Raw Normal View History

2017-07-25 11:53:54 -04:00
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
2017-11-07 14:02:39 -05:00
import Modes from '../lib/modes';
import Blobbiness from '../helper/blob-tools/blob';
2017-08-16 18:16:37 -04:00
import {changeBrushSize} from '../reducers/eraser-mode';
import {clearSelectedItems} from '../reducers/selected-items';
import EraserModeComponent from '../components/eraser-mode/eraser-mode.jsx';
import {changeMode} from '../reducers/modes';
2017-07-25 11:53:54 -04:00
class EraserMode extends React.Component {
2017-07-25 11:53:54 -04:00
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
'deactivateTool'
2017-07-25 11:53:54 -04:00
]);
2017-10-05 15:41:22 -04:00
this.blob = new Blobbiness(
2017-10-05 18:12:22 -04:00
this.props.onUpdateSvg, this.props.clearSelectedItems);
2017-07-25 11:53:54 -04:00
}
componentDidMount () {
if (this.props.isEraserModeActive) {
2017-07-25 11:53:54 -04:00
this.activateTool();
}
}
componentWillReceiveProps (nextProps) {
if (nextProps.isEraserModeActive && !this.props.isEraserModeActive) {
2017-07-25 11:53:54 -04:00
this.activateTool();
} else if (!nextProps.isEraserModeActive && this.props.isEraserModeActive) {
2017-07-25 11:53:54 -04:00
this.deactivateTool();
} else if (nextProps.isEraserModeActive && this.props.isEraserModeActive) {
this.blob.setOptions({
isEraser: true,
...nextProps.eraserModeState
});
2017-07-25 11:53:54 -04:00
}
}
shouldComponentUpdate (nextProps) {
return nextProps.isEraserModeActive !== this.props.isEraserModeActive;
2017-07-25 11:53:54 -04:00
}
activateTool () {
2017-08-16 18:16:37 -04:00
this.blob.activateTool({isEraser: true, ...this.props.eraserModeState});
2017-07-25 11:53:54 -04:00
}
deactivateTool () {
this.blob.deactivateTool();
}
render () {
return (
<EraserModeComponent
isSelected={this.props.isEraserModeActive}
onMouseDown={this.props.handleMouseDown}
/>
2017-07-25 11:53:54 -04:00
);
}
}
EraserMode.propTypes = {
clearSelectedItems: PropTypes.func.isRequired,
eraserModeState: PropTypes.shape({
2017-07-25 11:53:54 -04:00
brushSize: PropTypes.number.isRequired
}),
handleMouseDown: PropTypes.func.isRequired,
isEraserModeActive: PropTypes.bool.isRequired,
2017-10-05 18:12:22 -04:00
onUpdateSvg: PropTypes.func.isRequired
2017-07-25 11:53:54 -04:00
};
const mapStateToProps = state => ({
2017-08-22 18:20:47 -04:00
eraserModeState: state.scratchPaint.eraserMode,
isEraserModeActive: state.scratchPaint.mode === Modes.ERASER
2017-07-25 11:53:54 -04:00
});
const mapDispatchToProps = dispatch => ({
clearSelectedItems: () => {
dispatch(clearSelectedItems());
},
2017-07-25 11:53:54 -04:00
changeBrushSize: brushSize => {
2017-08-16 18:16:37 -04:00
dispatch(changeBrushSize(brushSize));
},
handleMouseDown: () => {
dispatch(changeMode(Modes.ERASER));
2017-07-25 11:53:54 -04:00
}
});
2017-07-27 11:45:41 -04:00
export default connect(
2017-07-25 11:53:54 -04:00
mapStateToProps,
mapDispatchToProps
)(EraserMode);