Add project loading

This commit is contained in:
Ray Schamp committed 2016-09-28 10:53:37 -04:00
1 parent f1d2c99ef3
commit 9abdc519a1
2 files changed
+28 -9

No files matched your search

+2 -1
View File
@@ -36,6 +36,7 @@
"scratch-vm": "^0.1.0-prerelease",
"travis-after-all": "jamesarosen/travis-after-all#override-api-urls",
"webpack": "1.13.2",
"webpack-dev-server": "1.15.2"
"webpack-dev-server": "1.15.2",
"xhr": "2.2.2"
}
}
+26 -8
View File
@@ -1,4 +1,5 @@
const React = require('react');
const xhr = require('xhr');
const Blocks = require('./blocks');
const GreenFlag = require('./green-flag');
@@ -14,6 +15,7 @@ class GUI extends React.Component {
constructor (props) {
super(props);
this.animate = this.animate.bind(this);
this.getProject = this.getProject.bind(this);
this.onReceiveWorkspace = this.onReceiveWorkspace.bind(this);
this.state = {};
}
@@ -22,6 +24,19 @@ class GUI extends React.Component {
toolbox: this.toolbox
});
}
animate () {
this.props.vm.animationFrame();
requestAnimationFrame(this.animate);
}
getProject (callback) {
let id = location.hash.substring(1);
if (id.length < 1) return callback();
xhr({
uri: `https://projects.scratch.mit.edu/internalapi/project/${id}/get/`
}, (err, res, body) => {
callback(body);
});
}
onReceiveWorkspace (workspace) {
this.workspace = workspace;
VMManager.attachWorkspace(this.props.vm, this.workspace);
@@ -29,13 +44,15 @@ class GUI extends React.Component {
VMManager.attachKeyboardEvents(this.props.vm);
this.renderer = new Renderer(this.stage);
this.props.vm.attachRenderer(this.renderer);
this.props.vm.createEmptyProject();
this.props.vm.start();
requestAnimationFrame(this.animate);
}
animate () {
this.props.vm.animationFrame();
requestAnimationFrame(this.animate);
this.getProject(projectData => {
if (projectData) {
this.props.vm.loadProject(projectData);
} else {
this.props.vm.createEmptyProject();
}
this.props.vm.start();
requestAnimationFrame(this.animate);
});
}
render () {
return (
@@ -58,7 +75,8 @@ class GUI extends React.Component {
}
GUI.propTypes = {
vm: React.PropTypes.object
projectData: React.PropTypes.string,
vm: React.PropTypes.object,
};
GUI.defaultProps = {