2017-08-04 14:25:17 -04:00
|
|
|
const dispatch = require('../dispatch/central-dispatch');
|
2017-07-21 16:46:08 -04:00
|
|
|
const log = require('../util/log');
|
2018-03-27 15:30:23 -04:00
|
|
|
const maybeFormatMessage = require('../util/maybe-format-message');
|
2017-07-21 16:46:08 -04:00
|
|
|
|
|
|
|
const BlockType = require('./block-type');
|
|
|
|
|
2017-10-04 15:16:27 -04:00
|
|
|
// These extensions are currently built into the VM repository but should not be loaded at startup.
|
|
|
|
// TODO: move these out into a separate repository?
|
|
|
|
// TODO: change extension spec so that library info, including extension ID, can be collected through static methods
|
2018-03-22 11:51:44 -04:00
|
|
|
|
2017-10-04 15:16:27 -04:00
|
|
|
const builtinExtensions = {
|
2019-03-20 14:45:21 -04:00
|
|
|
// This is an example that isn't loaded with the other core blocks,
|
|
|
|
// but serves as a reference for loading core blocks as extensions.
|
2019-03-26 12:03:00 -04:00
|
|
|
coreExample: () => require('../blocks/scratch3_core_example'),
|
2019-03-20 14:45:21 -04:00
|
|
|
// These are the non-core built-in extensions.
|
2019-01-23 17:08:57 -05:00
|
|
|
pen: () => require('../extensions/scratch3_pen'),
|
|
|
|
wedo2: () => require('../extensions/scratch3_wedo2'),
|
|
|
|
music: () => require('../extensions/scratch3_music'),
|
|
|
|
microbit: () => require('../extensions/scratch3_microbit'),
|
|
|
|
text2speech: () => require('../extensions/scratch3_text2speech'),
|
|
|
|
translate: () => require('../extensions/scratch3_translate'),
|
|
|
|
videoSensing: () => require('../extensions/scratch3_video_sensing'),
|
|
|
|
speech2text: () => require('../extensions/scratch3_speech2text'),
|
|
|
|
ev3: () => require('../extensions/scratch3_ev3'),
|
2019-03-25 17:13:39 -04:00
|
|
|
makeymakey: () => require('../extensions/scratch3_makeymakey'),
|
2019-03-27 13:21:32 -04:00
|
|
|
boost: () => require('../extensions/scratch3_boost'),
|
2019-03-25 17:13:39 -04:00
|
|
|
gdxfor: () => require('../extensions/scratch3_gdx_for')
|
2017-10-04 15:16:27 -04:00
|
|
|
};
|
|
|
|
|
2017-07-21 16:46:08 -04:00
|
|
|
/**
|
|
|
|
* @typedef {object} ArgumentInfo - Information about an extension block argument
|
|
|
|
* @property {ArgumentType} type - the type of value this argument can take
|
|
|
|
* @property {*|undefined} default - the default value of this argument (default: blank)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-03-27 17:35:04 -04:00
|
|
|
* @typedef {object} ConvertedBlockInfo - Raw extension block data paired with processed data ready for scratch-blocks
|
2018-03-27 17:35:04 -04:00
|
|
|
* @property {ExtensionBlockMetadata} info - the raw block info
|
2018-03-27 17:35:04 -04:00
|
|
|
* @property {object} json - the scratch-blocks JSON definition for this block
|
|
|
|
* @property {string} xml - the scratch-blocks XML definition for this block
|
2017-07-21 16:46:08 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} CategoryInfo - Information about a block category
|
|
|
|
* @property {string} id - the unique ID of this category
|
2018-03-27 17:35:04 -04:00
|
|
|
* @property {string} name - the human-readable name of this category
|
|
|
|
* @property {string|undefined} blockIconURI - optional URI for the block icon image
|
2017-07-21 16:46:08 -04:00
|
|
|
* @property {string} color1 - the primary color for this category, in '#rrggbb' format
|
|
|
|
* @property {string} color2 - the secondary color for this category, in '#rrggbb' format
|
|
|
|
* @property {string} color3 - the tertiary color for this category, in '#rrggbb' format
|
2018-03-27 17:35:04 -04:00
|
|
|
* @property {Array.<ConvertedBlockInfo>} blocks - the blocks, separators, etc. in this category
|
|
|
|
* @property {Array.<object>} menus - the menus provided by this category
|
2017-08-22 17:28:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} PendingExtensionWorker - Information about an extension worker still initializing
|
|
|
|
* @property {string} extensionURL - the URL of the extension to be loaded by this worker
|
|
|
|
* @property {Function} resolve - function to call on successful worker startup
|
|
|
|
* @property {Function} reject - function to call on failed worker startup
|
2017-07-21 16:46:08 -04:00
|
|
|
*/
|
|
|
|
|
2017-07-14 15:37:59 -04:00
|
|
|
class ExtensionManager {
|
2017-10-04 15:16:27 -04:00
|
|
|
constructor (runtime) {
|
2017-07-14 15:37:59 -04:00
|
|
|
/**
|
|
|
|
* The ID number to provide to the next extension worker.
|
|
|
|
* @type {int}
|
|
|
|
*/
|
|
|
|
this.nextExtensionWorker = 0;
|
|
|
|
|
|
|
|
/**
|
2017-08-22 17:28:38 -04:00
|
|
|
* FIFO queue of extensions which have been requested but not yet loaded in a worker,
|
|
|
|
* along with promise resolution functions to call once the worker is ready or failed.
|
|
|
|
*
|
|
|
|
* @type {Array.<PendingExtensionWorker>}
|
|
|
|
*/
|
|
|
|
this.pendingExtensions = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of worker ID to workers which have been allocated but have not yet finished initialization.
|
|
|
|
* @type {Array.<PendingExtensionWorker>}
|
2017-07-14 15:37:59 -04:00
|
|
|
*/
|
2017-08-22 17:28:38 -04:00
|
|
|
this.pendingWorkers = [];
|
2017-07-14 15:37:59 -04:00
|
|
|
|
2017-10-31 14:32:21 -04:00
|
|
|
/**
|
2017-10-31 15:35:18 -04:00
|
|
|
* Set of loaded extension URLs/IDs (equivalent for built-in extensions).
|
2017-10-31 14:32:21 -04:00
|
|
|
* @type {Set.<string>}
|
|
|
|
* @private
|
|
|
|
*/
|
2017-12-11 15:41:45 -05:00
|
|
|
this._loadedExtensions = new Map();
|
2017-10-31 14:32:21 -04:00
|
|
|
|
2017-10-04 15:16:27 -04:00
|
|
|
/**
|
|
|
|
* Keep a reference to the runtime so we can construct internal extension objects.
|
|
|
|
* TODO: remove this in favor of extensions accessing the runtime as a service.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
|
2017-08-04 14:25:17 -04:00
|
|
|
dispatch.setService('extensions', this).catch(e => {
|
|
|
|
log.error(`ExtensionManager was unable to register extension service: ${JSON.stringify(e)}`);
|
|
|
|
});
|
2017-07-14 15:37:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-31 14:32:21 -04:00
|
|
|
/**
|
|
|
|
* Check whether an extension is registered or is in the process of loading. This is intended to control loading or
|
|
|
|
* adding extensions so it may return `true` before the extension is ready to be used. Use the promise returned by
|
|
|
|
* `loadExtensionURL` if you need to wait until the extension is truly ready.
|
2017-10-31 15:35:18 -04:00
|
|
|
* @param {string} extensionID - the ID of the extension.
|
2017-10-31 14:32:21 -04:00
|
|
|
* @returns {boolean} - true if loaded, false otherwise.
|
|
|
|
*/
|
|
|
|
isExtensionLoaded (extensionID) {
|
|
|
|
return this._loadedExtensions.has(extensionID);
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:45:21 -04:00
|
|
|
/**
|
|
|
|
* Synchronously load an internal extension (core or non-core) by ID. This call will
|
|
|
|
* fail if the provided id is not does not match an internal extension.
|
|
|
|
* @param {string} extensionId - the ID of an internal extension
|
|
|
|
*/
|
|
|
|
loadExtensionIdSync (extensionId) {
|
2019-03-26 12:03:00 -04:00
|
|
|
if (!builtinExtensions.hasOwnProperty(extensionId)) {
|
|
|
|
log.warn(`Could not find extension ${extensionId} in the built in extensions.`);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-20 14:45:21 -04:00
|
|
|
|
2019-03-26 12:03:00 -04:00
|
|
|
/** @TODO dupe handling for non-builtin extensions. See commit 670e51d33580e8a2e852b3b038bb3afc282f81b9 */
|
|
|
|
if (this.isExtensionLoaded(extensionId)) {
|
|
|
|
const message = `Rejecting attempt to load a second extension with ID ${extensionId}`;
|
|
|
|
log.warn(message);
|
|
|
|
return;
|
2019-03-20 14:45:21 -04:00
|
|
|
}
|
2019-03-26 12:03:00 -04:00
|
|
|
|
|
|
|
const extension = builtinExtensions[extensionId]();
|
|
|
|
const extensionInstance = new extension(this.runtime);
|
|
|
|
const serviceName = this._registerInternalExtension(extensionInstance);
|
|
|
|
this._loadedExtensions.set(extensionId, serviceName);
|
2019-03-20 14:45:21 -04:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:28:38 -04:00
|
|
|
/**
|
2017-10-04 15:16:27 -04:00
|
|
|
* Load an extension by URL or internal extension ID
|
|
|
|
* @param {string} extensionURL - the URL for the extension to load OR the ID of an internal extension
|
2017-08-22 17:28:38 -04:00
|
|
|
* @returns {Promise} resolved once the extension is loaded and initialized or rejected on failure
|
|
|
|
*/
|
2017-07-14 15:37:59 -04:00
|
|
|
loadExtensionURL (extensionURL) {
|
2017-10-04 15:16:27 -04:00
|
|
|
if (builtinExtensions.hasOwnProperty(extensionURL)) {
|
2017-10-31 15:35:18 -04:00
|
|
|
/** @TODO dupe handling for non-builtin extensions. See commit 670e51d33580e8a2e852b3b038bb3afc282f81b9 */
|
|
|
|
if (this.isExtensionLoaded(extensionURL)) {
|
|
|
|
const message = `Rejecting attempt to load a second extension with ID ${extensionURL}`;
|
|
|
|
log.warn(message);
|
2019-03-27 17:42:10 -04:00
|
|
|
return Promise.resolve();
|
2017-10-31 15:35:18 -04:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:08:57 -05:00
|
|
|
const extension = builtinExtensions[extensionURL]();
|
2017-10-04 15:16:27 -04:00
|
|
|
const extensionInstance = new extension(this.runtime);
|
2019-03-25 16:39:00 -04:00
|
|
|
const serviceName = this._registerInternalExtension(extensionInstance);
|
|
|
|
this._loadedExtensions.set(extensionURL, serviceName);
|
|
|
|
return Promise.resolve();
|
2017-10-04 15:16:27 -04:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:28:38 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// If we `require` this at the global level it breaks non-webpack targets, including tests
|
2017-08-29 14:43:09 -04:00
|
|
|
const ExtensionWorker = require('worker-loader?name=extension-worker.js!./extension-worker');
|
2017-07-14 15:37:59 -04:00
|
|
|
|
2017-08-22 17:28:38 -04:00
|
|
|
this.pendingExtensions.push({extensionURL, resolve, reject});
|
|
|
|
dispatch.addWorker(new ExtensionWorker());
|
|
|
|
});
|
2017-07-14 15:37:59 -04:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:41:45 -05:00
|
|
|
/**
|
2018-07-11 08:35:22 -04:00
|
|
|
* Regenerate blockinfo for any loaded extensions
|
|
|
|
* @returns {Promise} resolved once all the extensions have been reinitialized
|
|
|
|
*/
|
2017-12-11 15:41:45 -05:00
|
|
|
refreshBlocks () {
|
2018-07-11 08:35:22 -04:00
|
|
|
const allPromises = Array.from(this._loadedExtensions.values()).map(serviceName =>
|
2017-12-11 15:41:45 -05:00
|
|
|
dispatch.call(serviceName, 'getInfo')
|
|
|
|
.then(info => {
|
2018-07-11 08:35:22 -04:00
|
|
|
info = this._prepareExtensionInfo(serviceName, info);
|
2017-12-11 15:41:45 -05:00
|
|
|
dispatch.call('runtime', '_refreshExtensionPrimitives', info);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
2019-03-20 14:45:21 -04:00
|
|
|
log.error(`Failed to refresh built-in extension primitives: ${JSON.stringify(e)}`);
|
2018-07-11 08:35:22 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
return Promise.all(allPromises);
|
2017-12-11 15:41:45 -05:00
|
|
|
}
|
|
|
|
|
2017-07-14 15:37:59 -04:00
|
|
|
allocateWorker () {
|
|
|
|
const id = this.nextExtensionWorker++;
|
2017-08-22 17:28:38 -04:00
|
|
|
const workerInfo = this.pendingExtensions.shift();
|
|
|
|
this.pendingWorkers[id] = workerInfo;
|
|
|
|
return [id, workerInfo.extensionURL];
|
2017-07-14 15:37:59 -04:00
|
|
|
}
|
2017-07-21 16:46:08 -04:00
|
|
|
|
2019-03-20 14:45:21 -04:00
|
|
|
/**
|
|
|
|
* Synchronously collect extension metadata from the specified service and begin the extension registration process.
|
|
|
|
* @param {string} serviceName - the name of the service hosting the extension.
|
|
|
|
*/
|
|
|
|
registerExtensionServiceSync (serviceName) {
|
|
|
|
const info = dispatch.callSync(serviceName, 'getInfo');
|
|
|
|
this._registerExtensionInfo(serviceName, info);
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:28:56 -04:00
|
|
|
/**
|
|
|
|
* Collect extension metadata from the specified service and begin the extension registration process.
|
|
|
|
* @param {string} serviceName - the name of the service hosting the extension.
|
|
|
|
*/
|
2017-07-21 16:46:08 -04:00
|
|
|
registerExtensionService (serviceName) {
|
2017-08-04 14:25:17 -04:00
|
|
|
dispatch.call(serviceName, 'getInfo').then(info => {
|
2017-07-21 16:46:08 -04:00
|
|
|
this._registerExtensionInfo(serviceName, info);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:28:56 -04:00
|
|
|
/**
|
|
|
|
* Called by an extension worker to indicate that the worker has finished initialization.
|
|
|
|
* @param {int} id - the worker ID.
|
|
|
|
* @param {*?} e - the error encountered during initialization, if any.
|
|
|
|
*/
|
2017-08-22 17:28:38 -04:00
|
|
|
onWorkerInit (id, e) {
|
|
|
|
const workerInfo = this.pendingWorkers[id];
|
|
|
|
delete this.pendingWorkers[id];
|
|
|
|
if (e) {
|
|
|
|
workerInfo.reject(e);
|
|
|
|
} else {
|
|
|
|
workerInfo.resolve(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 00:00:59 -04:00
|
|
|
/**
|
|
|
|
* Register an internal (non-Worker) extension object
|
|
|
|
* @param {object} extensionObject - the extension object to register
|
2019-03-20 14:45:21 -04:00
|
|
|
* @returns {string} The name of the registered extension service
|
2017-09-27 00:00:59 -04:00
|
|
|
*/
|
|
|
|
_registerInternalExtension (extensionObject) {
|
|
|
|
const extensionInfo = extensionObject.getInfo();
|
2017-10-31 14:28:56 -04:00
|
|
|
const fakeWorkerId = this.nextExtensionWorker++;
|
2018-04-20 07:54:02 -04:00
|
|
|
const serviceName = `extension_${fakeWorkerId}_${extensionInfo.id}`;
|
2019-03-20 14:45:21 -04:00
|
|
|
dispatch.setServiceSync(serviceName, extensionObject);
|
|
|
|
dispatch.callSync('extensions', 'registerExtensionServiceSync', serviceName);
|
|
|
|
return serviceName;
|
2017-09-27 00:00:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-31 14:28:56 -04:00
|
|
|
/**
|
|
|
|
* Sanitize extension info then register its primitives with the VM.
|
|
|
|
* @param {string} serviceName - the name of the service hosting the extension
|
|
|
|
* @param {ExtensionInfo} extensionInfo - the extension's metadata
|
|
|
|
* @private
|
|
|
|
*/
|
2017-07-21 16:46:08 -04:00
|
|
|
_registerExtensionInfo (serviceName, extensionInfo) {
|
2017-08-04 14:25:17 -04:00
|
|
|
extensionInfo = this._prepareExtensionInfo(serviceName, extensionInfo);
|
|
|
|
dispatch.call('runtime', '_registerExtensionPrimitives', extensionInfo).catch(e => {
|
2018-03-27 17:35:04 -04:00
|
|
|
log.error(`Failed to register primitives for extension on service ${serviceName}:`, e);
|
2017-08-04 14:25:17 -04:00
|
|
|
});
|
2017-07-21 16:46:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify the provided text as necessary to ensure that it may be used as an attribute value in valid XML.
|
|
|
|
* @param {string} text - the text to be sanitized
|
|
|
|
* @returns {string} - the sanitized text
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_sanitizeID (text) {
|
|
|
|
return text.toString().replace(/[<"&]/, '_');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply minor cleanup and defaults for optional extension fields.
|
|
|
|
* TODO: make the ID unique in cases where two copies of the same extension are loaded.
|
2017-08-04 14:25:17 -04:00
|
|
|
* @param {string} serviceName - the name of the service hosting this extension block
|
2017-07-21 16:46:08 -04:00
|
|
|
* @param {ExtensionInfo} extensionInfo - the extension info to be sanitized
|
|
|
|
* @returns {ExtensionInfo} - a new extension info object with cleaned-up values
|
|
|
|
* @private
|
|
|
|
*/
|
2017-08-04 14:25:17 -04:00
|
|
|
_prepareExtensionInfo (serviceName, extensionInfo) {
|
2017-07-21 16:46:08 -04:00
|
|
|
extensionInfo = Object.assign({}, extensionInfo);
|
2018-04-20 07:54:02 -04:00
|
|
|
if (!/^[a-z0-9]+$/i.test(extensionInfo.id)) {
|
|
|
|
throw new Error('Invalid extension id');
|
|
|
|
}
|
2017-07-21 16:46:08 -04:00
|
|
|
extensionInfo.name = extensionInfo.name || extensionInfo.id;
|
|
|
|
extensionInfo.blocks = extensionInfo.blocks || [];
|
|
|
|
extensionInfo.targetTypes = extensionInfo.targetTypes || [];
|
2018-03-27 17:35:04 -04:00
|
|
|
extensionInfo.blocks = extensionInfo.blocks.reduce((results, blockInfo) => {
|
2017-08-04 14:25:17 -04:00
|
|
|
try {
|
2018-03-27 17:35:04 -04:00
|
|
|
let result;
|
|
|
|
switch (blockInfo) {
|
|
|
|
case '---': // separator
|
|
|
|
result = '---';
|
|
|
|
break;
|
2018-03-27 17:35:04 -04:00
|
|
|
default: // an ExtensionBlockMetadata object
|
2018-03-27 17:35:04 -04:00
|
|
|
result = this._prepareBlockInfo(serviceName, blockInfo);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
results.push(result);
|
2017-08-04 14:25:17 -04:00
|
|
|
} catch (e) {
|
|
|
|
// TODO: more meaningful error reporting
|
2017-10-06 16:37:59 -04:00
|
|
|
log.error(`Error processing block: ${e.message}, Block:\n${JSON.stringify(blockInfo)}`);
|
2017-08-04 14:25:17 -04:00
|
|
|
}
|
2018-03-27 17:35:04 -04:00
|
|
|
return results;
|
2017-08-04 14:25:17 -04:00
|
|
|
}, []);
|
2019-04-29 15:33:56 -04:00
|
|
|
extensionInfo.menus = extensionInfo.menus || {};
|
2018-01-26 17:36:36 -05:00
|
|
|
extensionInfo.menus = this._prepareMenuInfo(serviceName, extensionInfo.menus);
|
2017-07-21 16:46:08 -04:00
|
|
|
return extensionInfo;
|
|
|
|
}
|
2018-03-12 18:03:23 -04:00
|
|
|
|
2018-01-26 17:36:36 -05:00
|
|
|
/**
|
|
|
|
* Prepare extension menus. e.g. setup binding for dynamic menu functions.
|
|
|
|
* @param {string} serviceName - the name of the service hosting this extension block
|
2018-02-16 13:49:26 -05:00
|
|
|
* @param {Array.<MenuInfo>} menus - the menu defined by the extension.
|
2018-01-26 17:36:36 -05:00
|
|
|
* @returns {Array.<MenuInfo>} - a menuInfo object with all preprocessing done.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_prepareMenuInfo (serviceName, menus) {
|
2018-02-16 13:49:26 -05:00
|
|
|
const menuNames = Object.getOwnPropertyNames(menus);
|
2018-01-26 17:36:36 -05:00
|
|
|
for (let i = 0; i < menuNames.length; i++) {
|
2019-04-24 14:15:58 -04:00
|
|
|
const menuName = menuNames[i];
|
|
|
|
let menuInfo = menus[menuName];
|
|
|
|
if (!menuInfo.items) {
|
|
|
|
menuInfo = {
|
|
|
|
items: menuInfo
|
|
|
|
};
|
|
|
|
menus[menuName] = menuInfo;
|
|
|
|
}
|
2018-01-26 17:36:36 -05:00
|
|
|
// If the value is a string, it should be the name of a function in the
|
|
|
|
// extension object to call to populate the menu whenever it is opened.
|
|
|
|
// Set up the binding for the function object here so
|
|
|
|
// we can use it later when converting the menu for Scratch Blocks.
|
2019-04-24 14:15:58 -04:00
|
|
|
if (typeof menuInfo.items === 'string') {
|
|
|
|
const menuItemFunctionName = menuInfo.items;
|
2018-02-16 13:49:26 -05:00
|
|
|
const serviceObject = dispatch.services[serviceName];
|
2019-04-24 14:15:58 -04:00
|
|
|
menuInfo.items = this._getExtensionMenuItems.bind(this, serviceObject, menuItemFunctionName);
|
2018-01-26 17:36:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return menus;
|
|
|
|
}
|
2017-07-21 16:46:08 -04:00
|
|
|
|
2018-03-12 18:06:14 -04:00
|
|
|
/**
|
|
|
|
* Fetch the items for a particular extension menu, providing the target ID for context.
|
|
|
|
* @param {object} extensionObject - the extension object providing the menu.
|
2019-04-24 14:15:58 -04:00
|
|
|
* @param {string} menuItemFunctionName - the name of the menu function to call.
|
2018-03-12 18:06:14 -04:00
|
|
|
* @returns {Array} menu items ready for scratch-blocks.
|
|
|
|
* @private
|
|
|
|
*/
|
2019-04-24 14:15:58 -04:00
|
|
|
_getExtensionMenuItems (extensionObject, menuItemFunctionName) {
|
2018-03-12 18:06:14 -04:00
|
|
|
// Fetch the items appropriate for the target currently being edited. This assumes that menus only
|
|
|
|
// collect items when opened by the user while editing a particular target.
|
2018-03-27 15:30:23 -04:00
|
|
|
const editingTarget = this.runtime.getEditingTarget() || this.runtime.getTargetForStage();
|
2018-03-12 18:06:14 -04:00
|
|
|
const editingTargetID = editingTarget ? editingTarget.id : null;
|
2018-03-27 15:30:23 -04:00
|
|
|
const extensionMessageContext = this.runtime.makeMessageContextForTarget(editingTarget);
|
2018-03-12 18:06:14 -04:00
|
|
|
|
|
|
|
// TODO: Fix this to use dispatch.call when extensions are running in workers.
|
2019-04-24 14:15:58 -04:00
|
|
|
const menuFunc = extensionObject[menuItemFunctionName];
|
2018-03-27 15:30:23 -04:00
|
|
|
const menuItems = menuFunc.call(extensionObject, editingTargetID).map(
|
|
|
|
item => {
|
|
|
|
item = maybeFormatMessage(item, extensionMessageContext);
|
2018-12-26 05:26:59 -05:00
|
|
|
switch (typeof item) {
|
|
|
|
case 'object':
|
2018-03-27 15:30:23 -04:00
|
|
|
return [
|
|
|
|
maybeFormatMessage(item.text, extensionMessageContext),
|
|
|
|
item.value
|
|
|
|
];
|
2018-12-26 05:26:59 -05:00
|
|
|
case 'string':
|
|
|
|
return [item, item];
|
|
|
|
default:
|
|
|
|
return item;
|
2018-03-27 15:30:23 -04:00
|
|
|
}
|
|
|
|
});
|
2018-03-12 18:06:14 -04:00
|
|
|
|
|
|
|
if (!menuItems || menuItems.length < 1) {
|
2019-04-24 14:15:58 -04:00
|
|
|
throw new Error(`Extension menu returned no items: ${menuItemFunctionName}`);
|
2018-03-12 18:06:14 -04:00
|
|
|
}
|
|
|
|
return menuItems;
|
|
|
|
}
|
|
|
|
|
2017-07-21 16:46:08 -04:00
|
|
|
/**
|
|
|
|
* Apply defaults for optional block fields.
|
2017-08-04 14:25:17 -04:00
|
|
|
* @param {string} serviceName - the name of the service hosting this extension block
|
2018-03-27 17:35:04 -04:00
|
|
|
* @param {ExtensionBlockMetadata} blockInfo - the block info from the extension
|
|
|
|
* @returns {ExtensionBlockMetadata} - a new block info object which has values for all relevant optional fields.
|
2017-07-21 16:46:08 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2017-08-04 14:25:17 -04:00
|
|
|
_prepareBlockInfo (serviceName, blockInfo) {
|
2017-07-21 16:46:08 -04:00
|
|
|
blockInfo = Object.assign({}, {
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
terminal: false,
|
|
|
|
blockAllThreads: false,
|
|
|
|
arguments: {}
|
|
|
|
}, blockInfo);
|
2019-04-01 16:10:19 -04:00
|
|
|
blockInfo.opcode = blockInfo.opcode && this._sanitizeID(blockInfo.opcode);
|
2017-09-27 00:00:59 -04:00
|
|
|
blockInfo.text = blockInfo.text || blockInfo.opcode;
|
2017-10-04 18:40:25 -04:00
|
|
|
|
2019-04-01 16:10:19 -04:00
|
|
|
switch (blockInfo.blockType) {
|
|
|
|
case BlockType.EVENT:
|
|
|
|
if (blockInfo.func) {
|
|
|
|
log.warn(`Ignoring function "${blockInfo.func}" for event block ${blockInfo.opcode}`);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BlockType.BUTTON:
|
|
|
|
if (blockInfo.opcode) {
|
|
|
|
log.warn(`Ignoring opcode "${blockInfo.opcode}" for button with text: ${blockInfo.text}`);
|
|
|
|
}
|
|
|
|
break;
|
2019-04-19 15:28:19 -04:00
|
|
|
default: {
|
|
|
|
if (!blockInfo.opcode) {
|
|
|
|
throw new Error('Missing opcode for block');
|
|
|
|
}
|
2018-03-27 17:35:04 -04:00
|
|
|
|
2019-04-19 15:28:19 -04:00
|
|
|
const funcName = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
|
|
|
|
|
|
|
|
const getBlockInfo = blockInfo.isDynamic ?
|
|
|
|
args => args && args.mutation && args.mutation.blockInfo :
|
|
|
|
() => blockInfo;
|
|
|
|
const callBlockFunc = (() => {
|
2019-04-08 18:20:14 -04:00
|
|
|
if (dispatch._isRemoteService(serviceName)) {
|
2019-04-19 15:28:19 -04:00
|
|
|
return (args, util, realBlockInfo) =>
|
|
|
|
dispatch.call(serviceName, funcName, args, util, realBlockInfo);
|
2018-03-27 17:35:04 -04:00
|
|
|
}
|
2019-04-19 15:28:19 -04:00
|
|
|
|
|
|
|
// avoid promise latency if we can call direct
|
|
|
|
const serviceObject = dispatch.services[serviceName];
|
|
|
|
if (!serviceObject[funcName]) {
|
|
|
|
// The function might show up later as a dynamic property of the service object
|
|
|
|
log.warn(`Could not find extension block function called ${funcName}`);
|
|
|
|
}
|
|
|
|
return (args, util, realBlockInfo) =>
|
|
|
|
serviceObject[funcName](args, util, realBlockInfo);
|
|
|
|
})();
|
|
|
|
|
|
|
|
blockInfo.func = (args, util) => {
|
|
|
|
const realBlockInfo = getBlockInfo(args);
|
|
|
|
// TODO: filter args using the keys of realBlockInfo.arguments? maybe only if sandboxed?
|
|
|
|
return callBlockFunc(args, util, realBlockInfo);
|
|
|
|
};
|
2019-04-01 16:10:19 -04:00
|
|
|
break;
|
2017-10-04 18:40:25 -04:00
|
|
|
}
|
2019-04-19 15:28:19 -04:00
|
|
|
}
|
2018-03-27 17:35:04 -04:00
|
|
|
|
2017-07-21 16:46:08 -04:00
|
|
|
return blockInfo;
|
|
|
|
}
|
2017-07-14 15:37:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ExtensionManager;
|