diff --git a/src/.eslintrc.js b/src/.eslintrc.js index 9e9dd67..0d1c0f9 100644 --- a/src/.eslintrc.js +++ b/src/.eslintrc.js @@ -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 + } }; diff --git a/src/common/ElectronStorageHelper.js b/src/common/ElectronStorageHelper.js new file mode 100644 index 0000000..20d449e --- /dev/null +++ b/src/common/ElectronStorageHelper.js @@ -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.} 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; diff --git a/src/renderer/index.js b/src/renderer/index.js index 5ca3d91..60c98da 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -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);