diff --git a/package.json b/package.json index 6ea3edebd..410302db7 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/components/gui.js b/src/components/gui.js index b29aa7d50..32a0c0cfb 100644 --- a/src/components/gui.js +++ b/src/components/gui.js @@ -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 = {