Files
scratch-gui/src/lib/storage.js
T

72 lines
2.1 KiB
JavaScript

import ScratchStorage from 'scratch-storage';
import defaultProject from './default-project';
/**
* Wrapper for ScratchStorage which adds default web sources.
* @todo make this more configurable
*/
class Storage extends ScratchStorage {
constructor () {
super();
this.cacheDefaultProject();
this.addWebStore(
[this.AssetType.Project],
this.getProjectGetConfig.bind(this),
this.getProjectCreateConfig.bind(this),
this.getProjectUpdateConfig.bind(this)
);
this.addWebStore(
[this.AssetType.ImageVector, this.AssetType.ImageBitmap, this.AssetType.Sound],
this.getAssetGetConfig.bind(this)
);
this.addWebStore(
[this.AssetType.Sound],
asset => `static/extension-assets/scratch3_music/${asset.assetId}.${asset.dataFormat}`
);
}
setProjectHost (projectHost) {
this.projectHost = projectHost;
}
getProjectGetConfig (projectAsset) {
return `${this.projectHost}/internalapi/project/${projectAsset.assetId}/get/`;
}
getProjectCreateConfig () {
return {
url: `${this.projectHost}/`,
json: true,
withCredentials: true
};
}
getProjectUpdateConfig (projectAsset) {
return {
url: `${this.projectHost}/${projectAsset.assetId}`,
json: true,
withCredentials: true
};
}
setAssetHost (assetHost) {
this.assetHost = assetHost;
}
getAssetGetConfig (asset) {
return `${this.assetHost}/internalapi/asset/${asset.assetId}.${asset.dataFormat}/get/`;
}
setTranslatorFunction (translator) {
this.translator = translator;
this.cacheDefaultProject();
}
cacheDefaultProject () {
const defaultProjectAssets = defaultProject(this.translator);
defaultProjectAssets.forEach(asset => this.cache(
this.AssetType[asset.assetType],
this.DataFormat[asset.dataFormat],
asset.data,
asset.id
));
}
}
const storage = new Storage();
export default storage;