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';
|
2018-09-26 13:08:47 -04:00
|
|
|
import {changeStrokeWidth} from '../reducers/stroke-width';
|
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';
|
2018-07-12 15:48:30 -04:00
|
|
|
import Formats from '../lib/format';
|
|
|
|
import {isBitmap} from '../lib/format';
|
2017-10-11 09:05:34 -04:00
|
|
|
|
2017-09-07 17:59:14 -04:00
|
|
|
import StrokeColorIndicatorComponent from '../components/stroke-color-indicator.jsx';
|
2018-09-26 13:08:47 -04:00
|
|
|
import {applyStrokeColorToSelection, applyStrokeWidthToSelection} from '../helper/style-path';
|
2017-10-05 18:12:22 -04:00
|
|
|
|
|
|
|
class StrokeColorIndicator extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
2017-12-19 10:47:12 -05:00
|
|
|
'handleChangeStrokeColor',
|
|
|
|
'handleCloseStrokeColor'
|
2017-10-05 18:12:22 -04:00
|
|
|
]);
|
2017-10-29 14:10:43 -04:00
|
|
|
|
|
|
|
// Flag to track whether an svg-update-worthy change has been made
|
|
|
|
this._hasChanged = false;
|
|
|
|
}
|
|
|
|
componentWillReceiveProps (newProps) {
|
2018-04-26 18:45:50 -04:00
|
|
|
const {strokeColorModalVisible, onUpdateImage} = this.props;
|
2017-10-29 14:10:43 -04:00
|
|
|
if (strokeColorModalVisible && !newProps.strokeColorModalVisible) {
|
|
|
|
// Submit the new SVG, which also stores a single undo/redo action.
|
2018-04-26 18:45:50 -04:00
|
|
|
if (this._hasChanged) onUpdateImage();
|
2017-10-29 14:10:43 -04:00
|
|
|
this._hasChanged = false;
|
|
|
|
}
|
2017-10-05 18:12:22 -04:00
|
|
|
}
|
|
|
|
handleChangeStrokeColor (newColor) {
|
2018-09-26 13:08:47 -04:00
|
|
|
if (this.props.strokeColor === null && newColor !== null) {
|
|
|
|
this._hasChanged = applyStrokeWidthToSelection(1, this.props.textEditTarget) || this._hasChanged;
|
|
|
|
this.props.onChangeStrokeWidth(1);
|
|
|
|
} else if (this.props.strokeColor !== null && newColor === null) {
|
|
|
|
this._hasChanged = applyStrokeWidthToSelection(0, this.props.textEditTarget) || this._hasChanged;
|
|
|
|
this.props.onChangeStrokeWidth(0);
|
|
|
|
}
|
2017-10-29 14:10:43 -04:00
|
|
|
// Apply color and update redux, but do not update svg until picker closes.
|
2018-09-26 13:08:47 -04:00
|
|
|
this._hasChanged =
|
|
|
|
applyStrokeColorToSelection(newColor, isBitmap(this.props.format), this.props.textEditTarget) ||
|
|
|
|
this._hasChanged;
|
2017-10-05 18:12:22 -04:00
|
|
|
this.props.onChangeStrokeColor(newColor);
|
|
|
|
}
|
2017-12-19 10:47:12 -05:00
|
|
|
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}
|
2017-12-19 10:47:12 -05:00
|
|
|
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 ||
|
2018-07-17 16:37:03 -04:00
|
|
|
state.scratchPaint.mode === Modes.TEXT ||
|
|
|
|
state.scratchPaint.mode === Modes.FILL,
|
2018-07-12 15:48:30 -04:00
|
|
|
format: state.scratchPaint.format,
|
2017-12-19 10:47:12 -05:00
|
|
|
isEyeDropping: state.scratchPaint.color.eyeDropper.active,
|
2017-10-11 09:05:34 -04:00
|
|
|
strokeColor: state.scratchPaint.color.strokeColor,
|
2018-03-16 11:36:06 -04:00
|
|
|
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 => ({
|
2017-09-11 13:28:35 -04:00
|
|
|
onChangeStrokeColor: strokeColor => {
|
2017-09-07 17:59:14 -04:00
|
|
|
dispatch(changeStrokeColor(strokeColor));
|
2017-10-11 09:05:34 -04:00
|
|
|
},
|
2018-09-26 13:08:47 -04:00
|
|
|
onChangeStrokeWidth: strokeWidth => {
|
|
|
|
dispatch(changeStrokeWidth(strokeWidth));
|
|
|
|
},
|
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 = {
|
2018-07-12 15:48:30 -04:00
|
|
|
format: PropTypes.oneOf(Object.keys(Formats)),
|
2017-12-19 10:47:12 -05:00
|
|
|
isEyeDropping: PropTypes.bool.isRequired,
|
2017-10-05 18:12:22 -04:00
|
|
|
onChangeStrokeColor: PropTypes.func.isRequired,
|
2018-09-26 13:08:47 -04:00
|
|
|
onChangeStrokeWidth: PropTypes.func.isRequired,
|
2017-12-19 10:47:12 -05:00
|
|
|
onCloseStrokeColor: PropTypes.func.isRequired,
|
2018-04-26 18:45:50 -04:00
|
|
|
onUpdateImage: PropTypes.func.isRequired,
|
2017-10-29 14:10:43 -04:00
|
|
|
strokeColor: PropTypes.string,
|
2018-03-16 11:36:06 -04:00
|
|
|
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);
|