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

99 lines
3.2 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';
import Modes from '../modes/modes';
2017-08-17 13:53:54 -04:00
import Blobbiness from './blob/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.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',
'onScroll'
]);
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 () {
2017-08-23 10:56:37 -04:00
return false; // Static component, for now
2017-07-25 11:53:54 -04:00
}
activateTool () {
2017-07-27 11:45:41 -04:00
this.props.canvas.addEventListener('mousewheel', this.onScroll);
2017-07-25 11:53:54 -04:00
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 () {
2017-07-27 11:45:41 -04:00
this.props.canvas.removeEventListener('mousewheel', this.onScroll);
2017-07-25 11:53:54 -04:00
this.blob.deactivateTool();
}
onScroll (event) {
2017-07-27 16:41:41 -04:00
event.preventDefault();
2017-07-25 11:53:54 -04:00
if (event.deltaY < 0) {
this.props.changeBrushSize(this.props.eraserModeState.brushSize + 1);
} else if (event.deltaY > 0 && this.props.eraserModeState.brushSize > 1) {
this.props.changeBrushSize(this.props.eraserModeState.brushSize - 1);
2017-07-25 11:53:54 -04:00
}
}
render () {
return (
<EraserModeComponent onMouseDown={this.props.handleMouseDown} />
2017-07-25 11:53:54 -04:00
);
}
}
EraserMode.propTypes = {
2017-07-27 16:41:41 -04:00
canvas: PropTypes.instanceOf(Element).isRequired,
2017-07-25 11:53:54 -04:00
changeBrushSize: PropTypes.func.isRequired,
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);