2017-07-17 18:39:50 -04:00
|
|
|
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';
|
2017-08-15 18:11:02 -04:00
|
|
|
import Modes from '../modes/modes';
|
2017-07-17 18:39:50 -04:00
|
|
|
import {connect} from 'react-redux';
|
2017-08-30 10:50:05 -04:00
|
|
|
import bindAll from 'lodash.bindall';
|
2017-08-30 18:43:34 -04:00
|
|
|
import paper from 'paper';
|
2017-07-17 18:39:50 -04:00
|
|
|
|
|
|
|
class PaintEditor extends React.Component {
|
2017-08-30 10:50:05 -04:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
2017-08-30 18:43:34 -04:00
|
|
|
'handleUpdateSvg'
|
2017-08-30 10:50:05 -04:00
|
|
|
]);
|
|
|
|
}
|
2017-07-17 18:39:50 -04:00
|
|
|
componentDidMount () {
|
2017-07-27 00:34:33 -04:00
|
|
|
document.addEventListener('keydown', this.props.onKeyPress);
|
|
|
|
}
|
|
|
|
componentWillUnmount () {
|
|
|
|
document.removeEventListener('keydown', this.props.onKeyPress);
|
2017-07-17 18:39:50 -04:00
|
|
|
}
|
2017-08-30 18:43:34 -04:00
|
|
|
handleUpdateSvg () {
|
|
|
|
if (!this.props.onUpdateSvg) {
|
2017-08-30 10:50:05 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-08-30 18:43:34 -04:00
|
|
|
this.props.onUpdateSvg(
|
|
|
|
paper.project.exportSVG({asString: true}) // TODO can this be made independent of paper
|
2017-08-30 10:50:05 -04:00
|
|
|
);
|
|
|
|
}
|
2017-07-17 18:39:50 -04:00
|
|
|
render () {
|
|
|
|
return (
|
2017-08-30 18:43:34 -04:00
|
|
|
<PaintEditorComponent
|
|
|
|
svg={this.props.svg}
|
|
|
|
onUpdateSvg={this.handleUpdateSvg}
|
|
|
|
/>
|
2017-07-17 18:39:50 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PaintEditor.propTypes = {
|
2017-08-30 10:50:05 -04:00
|
|
|
onKeyPress: PropTypes.func.isRequired,
|
2017-08-30 18:43:34 -04:00
|
|
|
onUpdateSvg: PropTypes.func.isRequired,
|
2017-08-30 10:50:05 -04:00
|
|
|
svg: PropTypes.string
|
2017-07-17 18:39:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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-17 18:39:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-26 20:39:12 -04:00
|
|
|
export default connect(
|
2017-07-27 16:41:41 -04:00
|
|
|
null,
|
2017-07-17 18:39:50 -04:00
|
|
|
mapDispatchToProps
|
|
|
|
)(PaintEditor);
|