Add ElectronStorageHelper to load bundled assets

This commit is contained in:
Christopher Willis-Ford 2018-10-22 18:59:36 -07:00
parent 071ba28fc8
commit 78e6831cbb
3 changed files with 51 additions and 2 deletions

View file

@ -4,5 +4,7 @@ module.exports = {
node: true
},
extends: ['scratch', 'scratch/es6'],
globals: ['__static']
globals: {
__static: false // electron-webpack provides this constant to access bundled static assets
}
};

View file

@ -0,0 +1,40 @@
const fs = require('fs');
const path = require('path');
const staticAssets = path.resolve(__static, 'assets');
/**
* Allow the storage module to load files bundled in the Electron application.
*/
class ElectronStorageHelper {
constructor (storageInstance) {
this.parent = storageInstance;
}
/**
* Fetch an asset but don't process dependencies.
* @param {AssetType} assetType - The type of asset to fetch.
* @param {string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
* @param {DataFormat} dataFormat - The file format / file extension of the asset to fetch: PNG, JPG, etc.
* @return {Promise.<Asset>} A promise for the contents of the asset.
*/
load (assetType, assetId, dataFormat) {
assetId = path.basename(assetId);
dataFormat = path.basename(dataFormat);
return new Promise((resolve, reject) => {
fs.readFile(
path.resolve(staticAssets, `${assetId}.${dataFormat}`),
(err, data) => {
if (err) {
reject(err);
} else {
resolve(new this.parent.Asset(assetType, assetId, dataFormat, data));
}
}
);
});
}
}
module.exports = ElectronStorageHelper;

View file

@ -4,6 +4,8 @@ import ReactDOM from 'react-dom';
import GUI, {AppStateHOC} from 'scratch-gui';
import styles from 'scratch-gui/src/playground/index.css';
import ElectronStorageHelper from '../common/ElectronStorageHelper';
// Register "base" page view
// analytics.pageview('/');
@ -19,5 +21,10 @@ if (process.env.NODE_ENV === 'production' && typeof window === 'object') {
window.onbeforeunload = () => true;
}
const wrappedGui = React.createElement(WrappedGui);
const onStorageInit = storageInstance => {
storageInstance.addHelper(new ElectronStorageHelper(storageInstance));
// storageInstance.addOfficialScratchWebStores(); // TODO: do we want this?
};
const wrappedGui = React.createElement(WrappedGui, {onStorageInit});
ReactDOM.render(wrappedGui, appTarget);