2017-10-16 14:18:04 -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-10-26 17:09:44 -04:00
|
|
|
import {changeStrokeColor} from '../reducers/stroke-color';
|
2017-10-26 17:42:15 -04:00
|
|
|
import {changeStrokeWidth} from '../reducers/stroke-width';
|
2017-10-16 14:18:04 -04:00
|
|
|
import {changeMode} from '../reducers/modes';
|
2017-10-18 19:22:02 -04:00
|
|
|
import {clearSelectedItems} from '../reducers/selected-items';
|
2017-10-26 17:09:44 -04:00
|
|
|
import {MIXED} from '../helper/style-path';
|
2017-10-16 14:18:04 -04:00
|
|
|
|
2017-10-18 19:22:02 -04:00
|
|
|
import {clearSelection} from '../helper/selection';
|
2017-10-16 14:18:04 -04:00
|
|
|
import PenTool from '../helper/tools/pen-tool';
|
2017-10-19 15:08:15 -04:00
|
|
|
import PenModeComponent from '../components/pen-mode/pen-mode.jsx';
|
2017-10-16 14:18:04 -04:00
|
|
|
|
|
|
|
class PenMode extends React.Component {
|
2017-10-26 17:09:44 -04:00
|
|
|
static get DEFAULT_COLOR () {
|
|
|
|
return '#000000';
|
|
|
|
}
|
2017-10-16 14:18:04 -04:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
|
|
|
'activateTool',
|
|
|
|
'deactivateTool'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
componentDidMount () {
|
|
|
|
if (this.props.isPenModeActive) {
|
|
|
|
this.activateTool(this.props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
componentWillReceiveProps (nextProps) {
|
2017-10-18 19:22:02 -04:00
|
|
|
if (this.tool &&
|
|
|
|
(nextProps.colorState.strokeColor !== this.props.colorState.strokeColor ||
|
|
|
|
nextProps.colorState.strokeWidth !== this.props.colorState.strokeWidth)) {
|
|
|
|
this.tool.setColorState(nextProps.colorState);
|
2017-10-16 14:18:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nextProps.isPenModeActive && !this.props.isPenModeActive) {
|
|
|
|
this.activateTool();
|
|
|
|
} else if (!nextProps.isPenModeActive && this.props.isPenModeActive) {
|
|
|
|
this.deactivateTool();
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 15:08:15 -04:00
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
return nextProps.isPenModeActive !== this.props.isPenModeActive;
|
2017-10-16 14:18:04 -04:00
|
|
|
}
|
|
|
|
activateTool () {
|
2017-10-18 19:22:02 -04:00
|
|
|
clearSelection(this.props.clearSelectedItems);
|
2017-10-26 17:09:44 -04:00
|
|
|
// Force the default pen color if stroke is MIXED or transparent
|
|
|
|
const {strokeColor} = this.props.colorState;
|
|
|
|
if (strokeColor === MIXED || strokeColor === null) {
|
2017-10-26 18:16:14 -04:00
|
|
|
this.props.onChangeStrokeColor(PenMode.DEFAULT_COLOR);
|
2017-10-26 17:09:44 -04:00
|
|
|
}
|
2017-10-26 17:42:15 -04:00
|
|
|
// Force a minimum stroke width
|
|
|
|
if (!this.props.colorState.strokeWidth) {
|
|
|
|
this.props.onChangeStrokeWidth(1);
|
|
|
|
}
|
2017-10-16 14:18:04 -04:00
|
|
|
this.tool = new PenTool(
|
|
|
|
this.props.clearSelectedItems,
|
|
|
|
this.props.onUpdateSvg
|
|
|
|
);
|
2017-10-18 19:22:02 -04:00
|
|
|
this.tool.setColorState(this.props.colorState);
|
2017-10-16 14:18:04 -04:00
|
|
|
this.tool.activate();
|
|
|
|
}
|
|
|
|
deactivateTool () {
|
|
|
|
this.tool.deactivateTool();
|
|
|
|
this.tool.remove();
|
|
|
|
this.tool = null;
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
2017-10-19 15:08:15 -04:00
|
|
|
<PenModeComponent
|
|
|
|
isSelected={this.props.isPenModeActive}
|
|
|
|
onMouseDown={this.props.handleMouseDown}
|
|
|
|
/>
|
2017-10-16 14:18:04 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PenMode.propTypes = {
|
|
|
|
clearSelectedItems: PropTypes.func.isRequired,
|
2017-10-18 19:22:02 -04:00
|
|
|
colorState: PropTypes.shape({
|
|
|
|
fillColor: PropTypes.string,
|
|
|
|
strokeColor: PropTypes.string,
|
|
|
|
strokeWidth: PropTypes.number
|
|
|
|
}).isRequired,
|
2017-10-16 14:18:04 -04:00
|
|
|
handleMouseDown: PropTypes.func.isRequired,
|
|
|
|
isPenModeActive: PropTypes.bool.isRequired,
|
2017-10-26 18:16:14 -04:00
|
|
|
onChangeStrokeColor: PropTypes.func.isRequired,
|
|
|
|
onChangeStrokeWidth: PropTypes.func.isRequired,
|
2017-10-18 19:33:13 -04:00
|
|
|
onUpdateSvg: PropTypes.func.isRequired
|
2017-10-16 14:18:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2017-10-18 19:22:02 -04:00
|
|
|
colorState: state.scratchPaint.color,
|
2017-10-18 19:33:13 -04:00
|
|
|
isPenModeActive: state.scratchPaint.mode === Modes.PEN
|
2017-10-18 19:22:02 -04:00
|
|
|
|
2017-10-16 14:18:04 -04:00
|
|
|
});
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
clearSelectedItems: () => {
|
|
|
|
dispatch(clearSelectedItems());
|
|
|
|
},
|
|
|
|
handleMouseDown: () => {
|
|
|
|
dispatch(changeMode(Modes.PEN));
|
|
|
|
},
|
|
|
|
deactivateTool () {
|
2017-10-26 17:09:44 -04:00
|
|
|
},
|
|
|
|
onChangeStrokeColor: strokeColor => {
|
|
|
|
dispatch(changeStrokeColor(strokeColor));
|
2017-10-26 17:42:15 -04:00
|
|
|
},
|
|
|
|
onChangeStrokeWidth: strokeWidth => {
|
|
|
|
dispatch(changeStrokeWidth(strokeWidth));
|
2017-10-16 14:18:04 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(PenMode);
|