scratch-paint/src/containers/text-mode.jsx

168 lines
6.1 KiB
React
Raw Normal View History

2018-03-09 14:40:08 -05:00
import paper from '@scratch/paper';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
2018-05-17 10:37:02 -04:00
import Fonts from '../lib/fonts';
2018-03-09 14:40:08 -05:00
import Modes from '../lib/modes';
import {MIXED} from '../helper/style-path';
2018-05-17 10:37:02 -04:00
import {changeFont} from '../reducers/font';
2018-03-09 14:40:08 -05:00
import {changeFillColor, DEFAULT_COLOR} from '../reducers/fill-color';
import {changeStrokeColor} from '../reducers/stroke-color';
import {changeMode} from '../reducers/modes';
import {setTextEditTarget} from '../reducers/text-edit-target';
2018-03-09 14:40:08 -05:00
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
import {clearSelection, getSelectedLeafItems} from '../helper/selection';
import TextTool from '../helper/tools/text-tool';
import TextModeComponent from '../components/text-mode/text-mode.jsx';
class TextMode extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
2018-03-21 21:42:49 -04:00
'deactivateTool'
2018-03-09 14:40:08 -05:00
]);
}
componentDidMount () {
if (this.props.isTextModeActive) {
this.activateTool(this.props);
}
}
componentWillReceiveProps (nextProps) {
if (this.tool && nextProps.colorState !== this.props.colorState) {
this.tool.setColorState(nextProps.colorState);
}
if (this.tool && nextProps.selectedItems !== this.props.selectedItems) {
this.tool.onSelectionChanged(nextProps.selectedItems);
}
2018-03-22 14:06:51 -04:00
if (this.tool && !nextProps.textEditTarget && this.props.textEditTarget) {
this.tool.onTextEditCancelled();
}
if (this.tool && !nextProps.viewBounds.equals(this.props.viewBounds)) {
this.tool.onViewBoundsChanged(nextProps.viewBounds);
}
2018-05-17 10:37:02 -04:00
if (this.tool && nextProps.font !== this.props.font) {
this.tool.setFont(nextProps.font);
}
2018-03-09 14:40:08 -05:00
if (nextProps.isTextModeActive && !this.props.isTextModeActive) {
this.activateTool();
} else if (!nextProps.isTextModeActive && this.props.isTextModeActive) {
this.deactivateTool();
}
}
shouldComponentUpdate (nextProps) {
return nextProps.isTextModeActive !== this.props.isTextModeActive;
}
activateTool () {
clearSelection(this.props.clearSelectedItems);
2018-05-17 10:37:02 -04:00
2018-03-09 14:40:08 -05:00
// If fill and stroke color are both mixed/transparent/absent, set fill to default and stroke to transparent.
// If exactly one of fill or stroke color is set, set the other one to transparent.
// This way the tool won't draw an invisible state, or be unclear about what will be drawn.
const {fillColor, strokeColor, strokeWidth} = this.props.colorState;
const fillColorPresent = fillColor !== MIXED && fillColor !== null;
const strokeColorPresent =
strokeColor !== MIXED && strokeColor !== null && strokeWidth !== null && strokeWidth !== 0;
if (!fillColorPresent && !strokeColorPresent) {
this.props.onChangeFillColor(DEFAULT_COLOR);
this.props.onChangeStrokeColor(null);
} else if (!fillColorPresent && strokeColorPresent) {
this.props.onChangeFillColor(null);
} else if (fillColorPresent && !strokeColorPresent) {
this.props.onChangeStrokeColor(null);
}
2018-05-17 10:37:02 -04:00
if (!this.props.font || Object.keys(Fonts).map(key => Fonts[key])
.indexOf(this.props.font) < 0) {
this.props.changeFont(Fonts.SANS_SERIF);
}
2018-03-09 14:40:08 -05:00
this.tool = new TextTool(
2018-03-21 21:42:49 -04:00
this.props.textArea,
2018-03-09 14:40:08 -05:00
this.props.setSelectedItems,
this.props.clearSelectedItems,
this.props.onUpdateImage,
this.props.setTextEditTarget,
2018-05-17 10:37:02 -04:00
this.props.changeFont
2018-03-09 14:40:08 -05:00
);
this.tool.setColorState(this.props.colorState);
2018-05-17 10:37:02 -04:00
this.tool.setFont(this.props.font);
2018-03-09 14:40:08 -05:00
this.tool.activate();
}
deactivateTool () {
this.tool.deactivateTool();
this.tool.remove();
this.tool = null;
}
render () {
return (
<TextModeComponent
isSelected={this.props.isTextModeActive}
onMouseDown={this.props.handleMouseDown}
/>
);
}
}
TextMode.propTypes = {
2018-05-17 10:37:02 -04:00
changeFont: PropTypes.func.isRequired,
2018-03-09 14:40:08 -05:00
clearSelectedItems: PropTypes.func.isRequired,
colorState: PropTypes.shape({
fillColor: PropTypes.string,
strokeColor: PropTypes.string,
strokeWidth: PropTypes.number
}).isRequired,
2018-05-17 10:37:02 -04:00
font: PropTypes.string,
2018-03-09 14:40:08 -05:00
handleMouseDown: PropTypes.func.isRequired,
isTextModeActive: PropTypes.bool.isRequired,
onChangeFillColor: PropTypes.func.isRequired,
onChangeStrokeColor: PropTypes.func.isRequired,
onUpdateImage: PropTypes.func.isRequired,
2018-03-09 14:40:08 -05:00
selectedItems: PropTypes.arrayOf(PropTypes.instanceOf(paper.Item)),
setSelectedItems: PropTypes.func.isRequired,
2018-03-21 21:42:49 -04:00
setTextEditTarget: PropTypes.func.isRequired,
2018-03-22 14:06:51 -04:00
textArea: PropTypes.instanceOf(Element),
2018-03-22 15:29:19 -04:00
textEditTarget: PropTypes.number,
viewBounds: PropTypes.instanceOf(paper.Matrix).isRequired
2018-03-09 14:40:08 -05:00
};
const mapStateToProps = state => ({
colorState: state.scratchPaint.color,
2018-05-17 10:37:02 -04:00
font: state.scratchPaint.font,
2018-03-09 14:40:08 -05:00
isTextModeActive: state.scratchPaint.mode === Modes.TEXT,
2018-03-22 14:06:51 -04:00
selectedItems: state.scratchPaint.selectedItems,
textEditTarget: state.scratchPaint.textEditTarget,
viewBounds: state.scratchPaint.viewBounds
2018-03-09 14:40:08 -05:00
});
const mapDispatchToProps = dispatch => ({
2018-05-17 10:37:02 -04:00
changeFont: font => {
dispatch(changeFont(font));
},
2018-03-09 14:40:08 -05:00
clearSelectedItems: () => {
dispatch(clearSelectedItems());
},
setSelectedItems: () => {
dispatch(setSelectedItems(getSelectedLeafItems()));
},
setTextEditTarget: targetId => {
dispatch(setTextEditTarget(targetId));
},
2018-03-09 14:40:08 -05:00
handleMouseDown: () => {
dispatch(changeMode(Modes.TEXT));
},
onChangeFillColor: fillColor => {
dispatch(changeFillColor(fillColor));
},
onChangeStrokeColor: strokeColor => {
dispatch(changeStrokeColor(strokeColor));
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(TextMode);