scratch-paint/src/containers/paint-editor.jsx

63 lines
1.6 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import React from 'react';
import PaintEditorComponent from '../components/paint-editor.jsx';
2017-08-16 18:16:37 -04:00
import {changeMode} from '../reducers/modes';
import Modes from '../modes/modes';
import {connect} from 'react-redux';
2017-08-30 10:50:05 -04:00
import bindAll from 'lodash.bindall';
import paper from 'paper';
class PaintEditor extends React.Component {
2017-08-30 10:50:05 -04:00
constructor (props) {
super(props);
bindAll(this, [
'handleUpdateSvg'
2017-08-30 10:50:05 -04:00
]);
}
componentDidMount () {
2017-07-27 00:34:33 -04:00
document.addEventListener('keydown', this.props.onKeyPress);
}
componentWillUnmount () {
document.removeEventListener('keydown', this.props.onKeyPress);
}
handleUpdateSvg () {
if (!this.props.onUpdateSvg) {
2017-08-30 10:50:05 -04:00
return;
}
this.props.onUpdateSvg(
paper.project.exportSVG({asString: true}) // TODO can this be made independent of paper
2017-08-30 10:50:05 -04:00
);
}
render () {
return (
<PaintEditorComponent
svg={this.props.svg}
onUpdateSvg={this.handleUpdateSvg}
/>
);
}
}
PaintEditor.propTypes = {
2017-08-30 10:50:05 -04:00
onKeyPress: PropTypes.func.isRequired,
onUpdateSvg: PropTypes.func.isRequired,
2017-08-30 10:50:05 -04:00
svg: PropTypes.string
};
const mapDispatchToProps = dispatch => ({
2017-07-27 16:41:41 -04:00
onKeyPress: event => {
if (event.key === 'e') {
2017-08-16 18:16:37 -04:00
dispatch(changeMode(Modes.ERASER));
2017-07-27 16:41:41 -04:00
} else if (event.key === 'b') {
2017-08-16 18:16:37 -04:00
dispatch(changeMode(Modes.BRUSH));
2017-08-22 15:53:35 -04:00
} else if (event.key === 'l') {
dispatch(changeMode(Modes.LINE));
}
}
});
2017-07-26 20:39:12 -04:00
export default connect(
2017-07-27 16:41:41 -04:00
null,
mapDispatchToProps
)(PaintEditor);