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

101 lines
3 KiB
React
Raw Normal View History

2017-09-11 10:52:00 -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-09-11 14:23:30 -04:00
2017-09-11 10:52:00 -04:00
import {changeMode} from '../reducers/modes';
import {clearHoveredItem, setHoveredItem} from '../reducers/hover';
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
2017-09-11 14:23:30 -04:00
import {getSelectedLeafItems} from '../helper/selection';
2017-09-21 18:20:44 -04:00
import SelectTool from '../helper/selection-tools/select-tool';
2017-09-11 10:52:00 -04:00
import SelectModeComponent from '../components/select-mode.jsx';
class SelectMode extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
2017-09-21 18:20:44 -04:00
'deactivateTool'
2017-09-11 10:52:00 -04:00
]);
}
componentDidMount () {
if (this.props.isSelectModeActive) {
this.activateTool(this.props);
}
}
componentWillReceiveProps (nextProps) {
if (this.tool && nextProps.hoveredItemId !== this.props.hoveredItemId) {
this.tool.setPrevHoveredItemId(nextProps.hoveredItemId);
2017-09-21 18:20:44 -04:00
}
2017-09-11 10:52:00 -04:00
if (nextProps.isSelectModeActive && !this.props.isSelectModeActive) {
this.activateTool();
} else if (!nextProps.isSelectModeActive && this.props.isSelectModeActive) {
this.deactivateTool();
}
}
shouldComponentUpdate () {
return false; // Static component, for now
}
activateTool () {
this.tool = new SelectTool(
this.props.setHoveredItem,
this.props.clearHoveredItem,
this.props.setSelectedItems,
this.props.clearSelectedItems,
2017-10-05 18:12:22 -04:00
this.props.onUpdateSvg
2017-10-05 15:41:22 -04:00
);
2017-09-11 10:52:00 -04:00
this.tool.activate();
}
deactivateTool () {
2017-09-21 18:20:44 -04:00
this.tool.deactivateTool();
2017-09-11 10:52:00 -04:00
this.tool.remove();
this.tool = null;
}
render () {
return (
<SelectModeComponent onMouseDown={this.props.handleMouseDown} />
);
}
}
SelectMode.propTypes = {
2017-09-11 14:23:30 -04:00
clearHoveredItem: PropTypes.func.isRequired,
clearSelectedItems: PropTypes.func.isRequired,
2017-09-11 10:52:00 -04:00
handleMouseDown: PropTypes.func.isRequired,
hoveredItemId: PropTypes.number,
2017-09-11 10:52:00 -04:00
isSelectModeActive: PropTypes.bool.isRequired,
2017-09-21 18:20:44 -04:00
onUpdateSvg: PropTypes.func.isRequired,
setHoveredItem: PropTypes.func.isRequired,
2017-10-05 18:12:22 -04:00
setSelectedItems: PropTypes.func.isRequired
2017-09-11 10:52:00 -04:00
};
const mapStateToProps = state => ({
2017-09-13 15:17:59 -04:00
isSelectModeActive: state.scratchPaint.mode === Modes.SELECT,
hoveredItemId: state.scratchPaint.hoveredItemId
2017-09-11 10:52:00 -04:00
});
const mapDispatchToProps = dispatch => ({
setHoveredItem: hoveredItemId => {
dispatch(setHoveredItem(hoveredItemId));
2017-09-11 10:52:00 -04:00
},
2017-09-11 14:23:30 -04:00
clearHoveredItem: () => {
dispatch(clearHoveredItem());
},
clearSelectedItems: () => {
dispatch(clearSelectedItems());
},
setSelectedItems: () => {
dispatch(setSelectedItems(getSelectedLeafItems()));
},
2017-09-11 10:52:00 -04:00
handleMouseDown: () => {
dispatch(changeMode(Modes.SELECT));
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(SelectMode);