mirror of
https://github.com/scratchfoundation/scratch-paint.git
synced 2024-12-24 14:32:26 -05:00
2e0b34a325
This also adds a generic `tool-select` component which can be composed by each of the individual tools for presentation purposes. Towards #40.
103 lines
3.2 KiB
JavaScript
103 lines
3.2 KiB
JavaScript
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 {clearHoveredItem, setHoveredItem} from '../reducers/hover';
|
|
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
|
|
|
|
import {getSelectedLeafItems} from '../helper/selection';
|
|
import SelectTool from '../helper/selection-tools/select-tool';
|
|
import SelectModeComponent from '../components/select-mode/select-mode.jsx';
|
|
|
|
class SelectMode extends React.Component {
|
|
constructor (props) {
|
|
super(props);
|
|
bindAll(this, [
|
|
'activateTool',
|
|
'deactivateTool'
|
|
]);
|
|
}
|
|
componentDidMount () {
|
|
if (this.props.isSelectModeActive) {
|
|
this.activateTool(this.props);
|
|
}
|
|
}
|
|
componentWillReceiveProps (nextProps) {
|
|
if (this.tool && nextProps.hoveredItemId !== this.props.hoveredItemId) {
|
|
this.tool.setPrevHoveredItemId(nextProps.hoveredItemId);
|
|
}
|
|
|
|
if (nextProps.isSelectModeActive && !this.props.isSelectModeActive) {
|
|
this.activateTool();
|
|
} else if (!nextProps.isSelectModeActive && this.props.isSelectModeActive) {
|
|
this.deactivateTool();
|
|
}
|
|
}
|
|
shouldComponentUpdate (nextProps) {
|
|
return nextProps.isSelectModeActive !== this.props.isSelectModeActive;
|
|
}
|
|
activateTool () {
|
|
this.tool = new SelectTool(
|
|
this.props.setHoveredItem,
|
|
this.props.clearHoveredItem,
|
|
this.props.setSelectedItems,
|
|
this.props.clearSelectedItems,
|
|
this.props.onUpdateSvg
|
|
);
|
|
this.tool.activate();
|
|
}
|
|
deactivateTool () {
|
|
this.tool.deactivateTool();
|
|
this.tool.remove();
|
|
this.tool = null;
|
|
}
|
|
render () {
|
|
return (
|
|
<SelectModeComponent
|
|
isSelected={this.props.isSelectModeActive}
|
|
onMouseDown={this.props.handleMouseDown}
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
|
|
SelectMode.propTypes = {
|
|
clearHoveredItem: PropTypes.func.isRequired,
|
|
clearSelectedItems: PropTypes.func.isRequired,
|
|
handleMouseDown: PropTypes.func.isRequired,
|
|
hoveredItemId: PropTypes.number,
|
|
isSelectModeActive: PropTypes.bool.isRequired,
|
|
onUpdateSvg: PropTypes.func.isRequired,
|
|
setHoveredItem: PropTypes.func.isRequired,
|
|
setSelectedItems: PropTypes.func.isRequired
|
|
};
|
|
|
|
const mapStateToProps = state => ({
|
|
isSelectModeActive: state.scratchPaint.mode === Modes.SELECT,
|
|
hoveredItemId: state.scratchPaint.hoveredItemId
|
|
});
|
|
const mapDispatchToProps = dispatch => ({
|
|
setHoveredItem: hoveredItemId => {
|
|
dispatch(setHoveredItem(hoveredItemId));
|
|
},
|
|
clearHoveredItem: () => {
|
|
dispatch(clearHoveredItem());
|
|
},
|
|
clearSelectedItems: () => {
|
|
dispatch(clearSelectedItems());
|
|
},
|
|
setSelectedItems: () => {
|
|
dispatch(setSelectedItems(getSelectedLeafItems()));
|
|
},
|
|
handleMouseDown: () => {
|
|
dispatch(changeMode(Modes.SELECT));
|
|
}
|
|
});
|
|
|
|
export default connect(
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
)(SelectMode);
|