Skip to content

Commit d90a8c2

Browse files
committed
Move responsive change to the indicators instead of the reducers
1 parent 2712e62 commit d90a8c2

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

src/containers/stroke-color-indicator.jsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import PropTypes from 'prop-types';
33
import React from 'react';
44
import bindAll from 'lodash.bindall';
55
import {changeStrokeColor} from '../reducers/stroke-color';
6+
import {changeStrokeWidth} from '../reducers/stroke-width';
67
import {openStrokeColor, closeStrokeColor} from '../reducers/modals';
78
import Modes from '../lib/modes';
89
import Formats from '../lib/format';
910
import {isBitmap} from '../lib/format';
1011

1112
import StrokeColorIndicatorComponent from '../components/stroke-color-indicator.jsx';
12-
import {applyStrokeColorToSelection} from '../helper/style-path';
13+
import {applyStrokeColorToSelection, applyStrokeWidthToSelection} from '../helper/style-path';
1314

1415
class StrokeColorIndicator extends React.Component {
1516
constructor (props) {
@@ -31,10 +32,17 @@ class StrokeColorIndicator extends React.Component {
3132
}
3233
}
3334
handleChangeStrokeColor (newColor) {
35+
if (this.props.strokeColor === null && newColor !== null) {
36+
this._hasChanged = applyStrokeWidthToSelection(1, this.props.textEditTarget) || this._hasChanged;
37+
this.props.onChangeStrokeWidth(1);
38+
} else if (this.props.strokeColor !== null && newColor === null) {
39+
this._hasChanged = applyStrokeWidthToSelection(0, this.props.textEditTarget) || this._hasChanged;
40+
this.props.onChangeStrokeWidth(0);
41+
}
3442
// Apply color and update redux, but do not update svg until picker closes.
35-
const isDifferent =
36-
applyStrokeColorToSelection(newColor, isBitmap(this.props.format), this.props.textEditTarget);
37-
this._hasChanged = this._hasChanged || isDifferent;
43+
this._hasChanged =
44+
applyStrokeColorToSelection(newColor, isBitmap(this.props.format), this.props.textEditTarget) ||
45+
this._hasChanged;
3846
this.props.onChangeStrokeColor(newColor);
3947
}
4048
handleCloseStrokeColor () {
@@ -68,6 +76,9 @@ const mapDispatchToProps = dispatch => ({
6876
onChangeStrokeColor: strokeColor => {
6977
dispatch(changeStrokeColor(strokeColor));
7078
},
79+
onChangeStrokeWidth: strokeWidth => {
80+
dispatch(changeStrokeWidth(strokeWidth));
81+
},
7182
onOpenStrokeColor: () => {
7283
dispatch(openStrokeColor());
7384
},
@@ -77,10 +88,10 @@ const mapDispatchToProps = dispatch => ({
7788
});
7889

7990
StrokeColorIndicator.propTypes = {
80-
disabled: PropTypes.bool.isRequired,
8191
format: PropTypes.oneOf(Object.keys(Formats)),
8292
isEyeDropping: PropTypes.bool.isRequired,
8393
onChangeStrokeColor: PropTypes.func.isRequired,
94+
onChangeStrokeWidth: PropTypes.func.isRequired,
8495
onCloseStrokeColor: PropTypes.func.isRequired,
8596
onUpdateImage: PropTypes.func.isRequired,
8697
strokeColor: PropTypes.string,

src/containers/stroke-width-indicator.jsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import {connect} from 'react-redux';
22
import PropTypes from 'prop-types';
33
import React from 'react';
44
import bindAll from 'lodash.bindall';
5+
import {changeStrokeColor} from '../reducers/stroke-color';
56
import {changeStrokeWidth} from '../reducers/stroke-width';
67
import StrokeWidthIndicatorComponent from '../components/stroke-width-indicator.jsx';
7-
import {applyStrokeWidthToSelection} from '../helper/style-path';
8+
import {applyStrokeColorToSelection, applyStrokeWidthToSelection} from '../helper/style-path';
89
import Modes from '../lib/modes';
10+
import Formats from '../lib/format';
11+
import {isBitmap} from '../lib/format';
912

1013
class StrokeWidthIndicator extends React.Component {
1114
constructor (props) {
@@ -15,10 +18,19 @@ class StrokeWidthIndicator extends React.Component {
1518
]);
1619
}
1720
handleChangeStrokeWidth (newWidth) {
18-
if (applyStrokeWidthToSelection(newWidth, this.props.textEditTarget)) {
19-
this.props.onUpdateImage();
21+
let changed = false;
22+
if (this.props.strokeWidth === 0 && newWidth > 0) {
23+
changed = applyStrokeColorToSelection('#000', isBitmap(this.props.format), this.props.textEditTarget) ||
24+
changed;
25+
this.props.onChangeStrokeColor('#000');
26+
} else if (this.props.strokeWidth > 0 && newWidth === 0) {
27+
changed = applyStrokeColorToSelection(null, isBitmap(this.props.format), this.props.textEditTarget) ||
28+
changed;
29+
this.props.onChangeStrokeColor(null);
2030
}
31+
changed = applyStrokeWidthToSelection(newWidth, this.props.textEditTarget) || changed;
2132
this.props.onChangeStrokeWidth(newWidth);
33+
if (changed) this.props.onUpdateImage();
2234
}
2335
render () {
2436
return (
@@ -35,17 +47,23 @@ const mapStateToProps = state => ({
3547
disabled: state.scratchPaint.mode === Modes.BRUSH ||
3648
state.scratchPaint.mode === Modes.TEXT ||
3749
state.scratchPaint.mode === Modes.FILL,
50+
format: state.scratchPaint.format,
3851
strokeWidth: state.scratchPaint.color.strokeWidth,
3952
textEditTarget: state.scratchPaint.textEditTarget
4053
});
4154
const mapDispatchToProps = dispatch => ({
55+
onChangeStrokeColor: strokeColor => {
56+
dispatch(changeStrokeColor(strokeColor));
57+
},
4258
onChangeStrokeWidth: strokeWidth => {
4359
dispatch(changeStrokeWidth(strokeWidth));
4460
}
4561
});
4662

4763
StrokeWidthIndicator.propTypes = {
4864
disabled: PropTypes.bool.isRequired,
65+
format: PropTypes.oneOf(Object.keys(Formats)),
66+
onChangeStrokeColor: PropTypes.func.isRequired,
4967
onChangeStrokeWidth: PropTypes.func.isRequired,
5068
onUpdateImage: PropTypes.func.isRequired,
5169
strokeWidth: PropTypes.number,

src/reducers/stroke-color.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import log from '../log/log';
22
import {CHANGE_SELECTED_ITEMS} from './selected-items';
3-
import {CHANGE_STROKE_WIDTH} from './stroke-width';
43
import {getColorsFromSelection} from '../helper/style-path';
54

65
const CHANGE_STROKE_COLOR = 'scratch-paint/stroke-color/CHANGE_STROKE_COLOR';
@@ -11,13 +10,6 @@ const regExp = /^#([0-9a-f]{3}){1,2}$/i;
1110
const reducer = function (state, action) {
1211
if (typeof state === 'undefined') state = initialState;
1312
switch (action.type) {
14-
case CHANGE_STROKE_WIDTH:
15-
if (!isNaN(action.strokeWidth) && Math.max(0, action.strokeWidth) === 0) {
16-
return null; // Change color to transparent if stroke width is set to 0
17-
} else if (state === null && action.strokeWidth > 0) {
18-
return '#000';
19-
}
20-
return state;
2113
case CHANGE_STROKE_COLOR:
2214
if (!regExp.test(action.strokeColor) && action.strokeColor !== null) {
2315
log.warn(`Invalid hex color code: ${action.fillColor}`);
@@ -49,6 +41,5 @@ const changeStrokeColor = function (strokeColor) {
4941

5042
export {
5143
reducer as default,
52-
changeStrokeColor,
53-
CHANGE_STROKE_COLOR
44+
changeStrokeColor
5445
};

src/reducers/stroke-width.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import log from '../log/log';
22
import {CHANGE_SELECTED_ITEMS} from './selected-items';
3-
import {CHANGE_STROKE_COLOR} from './stroke-color';
43
import {getColorsFromSelection} from '../helper/style-path';
54

65
const CHANGE_STROKE_WIDTH = 'scratch-paint/stroke-width/CHANGE_STROKE_WIDTH';
@@ -10,13 +9,6 @@ const initialState = 4;
109
const reducer = function (state, action) {
1110
if (typeof state === 'undefined') state = initialState;
1211
switch (action.type) {
13-
case CHANGE_STROKE_COLOR:
14-
if (action.strokeColor === null) {
15-
return 0; // If stroke color is set to transparent, set stroke width to 0
16-
} else if (state === 0 && action.strokeColor !== null) {
17-
return 1; // If stroke color is changed from transparent, set a stroke width
18-
}
19-
return state;
2012
case CHANGE_STROKE_WIDTH:
2113
if (isNaN(action.strokeWidth)) {
2214
log.warn(`Invalid brush size: ${action.strokeWidth}`);
@@ -49,6 +41,5 @@ const changeStrokeWidth = function (strokeWidth) {
4941
export {
5042
reducer as default,
5143
changeStrokeWidth,
52-
CHANGE_STROKE_WIDTH,
5344
MAX_STROKE_WIDTH
5445
};

0 commit comments

Comments
 (0)