diff --git a/package.json b/package.json index 41888d82b5e5fbebb8f9351850a995efb5eb155f..ac8a9824effa7a005dfa24565b3d136c45fe358f 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "scratch-audio": "latest", "scratch-blocks": "latest", "scratch-render": "latest", + "scratch-storage": "latest", "scratch-vm": "latest", "style-loader": "0.13.2", "svg-to-image": "1.1.3", diff --git a/src/containers/stage.jsx b/src/containers/stage.jsx index 3f693b78ca5d01ed2fdef4189fedafba49105a89..63d09b05c5ed99cded4153b0abfb167bc5fda729 100644 --- a/src/containers/stage.jsx +++ b/src/containers/stage.jsx @@ -1,10 +1,11 @@ const bindAll = require('lodash.bindall'); +const AudioEngine = require('scratch-audio'); const React = require('react'); const Renderer = require('scratch-render'); -const AudioEngine = require('scratch-audio'); const VM = require('scratch-vm'); const StageComponent = require('../components/stage/stage.jsx'); +const Storage = require('../lib/storage'); class Stage extends React.Component { constructor (props) { @@ -32,10 +33,12 @@ class Stage extends React.Component { this.attachRectEvents(); this.attachMouseEvents(this.canvas); this.updateRect(); - this.renderer = new Renderer(this.canvas); - this.props.vm.attachRenderer(this.renderer); this.audioEngine = new AudioEngine(); this.props.vm.attachAudioEngine(this.audioEngine); + this.renderer = new Renderer(this.canvas); + this.props.vm.attachRenderer(this.renderer); + this.storage = new Storage(); + this.props.vm.attachStorage(this.storage); } shouldComponentUpdate () { return false; diff --git a/src/lib/storage.js b/src/lib/storage.js new file mode 100644 index 0000000000000000000000000000000000000000..619632eef2f8970788c518887ff08cdcbf76bd6d --- /dev/null +++ b/src/lib/storage.js @@ -0,0 +1,30 @@ +const ScratchStorage = require('scratch-storage'); +const AssetType = ScratchStorage.AssetType; + +const PROJECT_SERVER = 'https://cdn.projects.scratch.mit.edu'; +const ASSET_SERVER = 'https://cdn.assets.scratch.mit.edu'; + +/** + * Wrapper for ScratchStorage which adds default web sources. + * @todo make this more configurable + */ +class Storage extends ScratchStorage { + constructor () { + super(); + this.addWebSource( + [AssetType.Project], + projectAsset => { + const [projectId, revision] = projectAsset.assetId.split('.'); + return revision ? + `${PROJECT_SERVER}/internalapi/project/${projectId}/get/${revision}` : + `${PROJECT_SERVER}/internalapi/project/${projectId}/get/`; + } + ); + this.addWebSource( + [AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound], + asset => `${ASSET_SERVER}/internalapi/asset/${asset.assetId}.${asset.assetType.runtimeFormat}/get/` + ); + } +} + +module.exports = Storage;