scratch-paint/src/containers/stroke-color-indicator.jsx

89 lines
3 KiB
React
Raw Normal View History

2017-09-07 17:59:14 -04:00
import {connect} from 'react-redux';
2017-10-05 18:12:22 -04:00
import PropTypes from 'prop-types';
import React from 'react';
import bindAll from 'lodash.bindall';
2017-09-07 17:59:14 -04:00
import {changeStrokeColor} from '../reducers/stroke-color';
2017-10-11 09:05:34 -04:00
import {openStrokeColor, closeStrokeColor} from '../reducers/modals';
2017-11-07 14:02:39 -05:00
import Modes from '../lib/modes';
2017-10-11 09:05:34 -04:00
2017-09-07 17:59:14 -04:00
import StrokeColorIndicatorComponent from '../components/stroke-color-indicator.jsx';
import {applyStrokeColorToSelection} from '../helper/style-path';
2017-10-05 18:12:22 -04:00
class StrokeColorIndicator extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleChangeStrokeColor',
'handleCloseStrokeColor'
2017-10-05 18:12:22 -04:00
]);
// Flag to track whether an svg-update-worthy change has been made
this._hasChanged = false;
}
componentWillReceiveProps (newProps) {
const {strokeColorModalVisible, onUpdateImage} = this.props;
if (strokeColorModalVisible && !newProps.strokeColorModalVisible) {
// Submit the new SVG, which also stores a single undo/redo action.
if (this._hasChanged) onUpdateImage();
this._hasChanged = false;
}
2017-10-05 18:12:22 -04:00
}
handleChangeStrokeColor (newColor) {
// Apply color and update redux, but do not update svg until picker closes.
const isDifferent = applyStrokeColorToSelection(newColor, this.props.textEditTarget);
this._hasChanged = this._hasChanged || isDifferent;
2017-10-05 18:12:22 -04:00
this.props.onChangeStrokeColor(newColor);
}
handleCloseStrokeColor () {
if (!this.props.isEyeDropping) {
this.props.onCloseStrokeColor();
}
}
2017-10-05 18:12:22 -04:00
render () {
return (
<StrokeColorIndicatorComponent
2017-10-11 09:05:34 -04:00
{...this.props}
2017-10-05 18:12:22 -04:00
onChangeStrokeColor={this.handleChangeStrokeColor}
onCloseStrokeColor={this.handleCloseStrokeColor}
2017-10-05 18:12:22 -04:00
/>
);
}
}
2017-09-07 17:59:14 -04:00
const mapStateToProps = state => ({
2018-03-22 17:53:17 -04:00
disabled: state.scratchPaint.mode === Modes.BRUSH ||
state.scratchPaint.mode === Modes.TEXT,
isEyeDropping: state.scratchPaint.color.eyeDropper.active,
2017-10-11 09:05:34 -04:00
strokeColor: state.scratchPaint.color.strokeColor,
strokeColorModalVisible: state.scratchPaint.modals.strokeColor,
textEditTarget: state.scratchPaint.textEditTarget
2017-09-07 17:59:14 -04:00
});
2017-10-11 09:05:34 -04:00
2017-09-07 17:59:14 -04:00
const mapDispatchToProps = dispatch => ({
onChangeStrokeColor: strokeColor => {
2017-09-07 17:59:14 -04:00
dispatch(changeStrokeColor(strokeColor));
2017-10-11 09:05:34 -04:00
},
onOpenStrokeColor: () => {
dispatch(openStrokeColor());
},
onCloseStrokeColor: () => {
dispatch(closeStrokeColor());
2017-09-07 17:59:14 -04:00
}
});
2017-10-05 18:12:22 -04:00
StrokeColorIndicator.propTypes = {
disabled: PropTypes.bool.isRequired,
isEyeDropping: PropTypes.bool.isRequired,
2017-10-05 18:12:22 -04:00
onChangeStrokeColor: PropTypes.func.isRequired,
onCloseStrokeColor: PropTypes.func.isRequired,
onUpdateImage: PropTypes.func.isRequired,
strokeColor: PropTypes.string,
strokeColorModalVisible: PropTypes.bool.isRequired,
2018-03-16 14:16:27 -04:00
textEditTarget: PropTypes.number
2017-10-05 18:12:22 -04:00
};
2017-09-07 17:59:14 -04:00
export default connect(
mapStateToProps,
mapDispatchToProps
2017-10-05 18:12:22 -04:00
)(StrokeColorIndicator);