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

107 lines
3.3 KiB
React
Raw Normal View History

2017-10-23 15:38:52 -04:00
import paper from '@scratch/paper';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
import Modes from '../modes/modes';
import {changeMode} from '../reducers/modes';
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
2017-10-19 20:43:28 -04:00
import {clearSelection, getSelectedLeafItems} from '../helper/selection';
import RectTool from '../helper/tools/rect-tool';
import RectModeComponent from '../components/rect-mode/rect-mode.jsx';
class RectMode extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
'deactivateTool'
]);
}
componentDidMount () {
if (this.props.isRectModeActive) {
this.activateTool(this.props);
}
}
componentWillReceiveProps (nextProps) {
2017-10-19 16:52:24 -04:00
if (this.tool && nextProps.colorState !== this.props.colorState) {
this.tool.setColorState(nextProps.colorState);
}
2017-10-23 15:38:52 -04:00
if (this.tool && nextProps.selectedItems !== this.props.selectedItems) {
this.tool.onSelectionChanged(nextProps.selectedItems);
}
2017-10-19 16:52:24 -04:00
if (nextProps.isRectModeActive && !this.props.isRectModeActive) {
this.activateTool();
} else if (!nextProps.isRectModeActive && this.props.isRectModeActive) {
this.deactivateTool();
}
}
shouldComponentUpdate (nextProps) {
return nextProps.isRectModeActive !== this.props.isRectModeActive;
}
activateTool () {
2017-10-19 20:43:28 -04:00
clearSelection(this.props.clearSelectedItems);
this.tool = new RectTool(
this.props.setSelectedItems,
this.props.clearSelectedItems,
this.props.onUpdateSvg
);
2017-10-19 16:52:24 -04:00
this.tool.setColorState(this.props.colorState);
this.tool.activate();
}
deactivateTool () {
this.tool.deactivateTool();
this.tool.remove();
this.tool = null;
}
render () {
return (
<RectModeComponent
isSelected={this.props.isRectModeActive}
onMouseDown={this.props.handleMouseDown}
/>
);
}
}
RectMode.propTypes = {
clearSelectedItems: PropTypes.func.isRequired,
2017-10-19 16:52:24 -04:00
colorState: PropTypes.shape({
fillColor: PropTypes.string,
strokeColor: PropTypes.string,
strokeWidth: PropTypes.number
}).isRequired,
handleMouseDown: PropTypes.func.isRequired,
isRectModeActive: PropTypes.bool.isRequired,
onUpdateSvg: PropTypes.func.isRequired,
2017-10-23 15:38:52 -04:00
selectedItems: PropTypes.arrayOf(PropTypes.instanceOf(paper.Item)),
setSelectedItems: PropTypes.func.isRequired
};
const mapStateToProps = state => ({
2017-10-19 16:52:24 -04:00
colorState: state.scratchPaint.color,
2017-10-23 15:38:52 -04:00
isRectModeActive: state.scratchPaint.mode === Modes.RECT,
selectedItems: state.scratchPaint.selectedItems
});
const mapDispatchToProps = dispatch => ({
clearSelectedItems: () => {
dispatch(clearSelectedItems());
},
setSelectedItems: () => {
dispatch(setSelectedItems(getSelectedLeafItems()));
},
handleMouseDown: () => {
dispatch(changeMode(Modes.RECT));
},
deactivateTool () {
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(RectMode);