Initial commit of 001code-html Scratch frontend project.
Includes scratch-gui, scratch-vm, scratch-blocks, scratch-render, scratch-l10n, and deployment config. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
41
scratch-gui/src/lib/tw-extension-gui-api.js
Normal file
41
scratch-gui/src/lib/tw-extension-gui-api.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import LazyScratchBlocks from './tw-lazy-scratch-blocks';
|
||||
import AddonHooks from '../addons/hooks';
|
||||
|
||||
/**
|
||||
* Implements Scratch.gui API for unsandboxed extensions.
|
||||
* @param {any} Scratch window.Scratch, mutated in place.
|
||||
*/
|
||||
const implementGuiAPI = Scratch => {
|
||||
Scratch.gui = {
|
||||
/**
|
||||
* Lazily get the internal ScratchBlocks object when it becomes available. It may never be
|
||||
* available if, for example, the user never enters the editor.
|
||||
*
|
||||
* You should not assume that ScratchBlocks becoming available means the user is actually
|
||||
* in the editor or that a workspace has been created already.
|
||||
*
|
||||
* @returns {Promise<any>} Promise that may eventually resolve to ScratchBlocks
|
||||
*/
|
||||
getBlockly: () => {
|
||||
if (AddonHooks.blockly) {
|
||||
return Promise.resolve(AddonHooks.blockly);
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
AddonHooks.blocklyCallbacks.push(() => resolve(AddonHooks.blockly));
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the internal ScratchBlocks object as soon as possible. This lets you access it even
|
||||
* if the user never enters the editor.
|
||||
*
|
||||
* This method is VERY SLOW and will cause A LOT OF CPU AND NETWORK ACTIVITY because it
|
||||
* downloads and evaluates all of scratch-blocks, a multi-megabyte JavaScript bundle.
|
||||
*
|
||||
* @returns {Promise<any>} Promise that will resolve to ScratchBlocks.
|
||||
*/
|
||||
getBlocklyEagerly: () => LazyScratchBlocks.load()
|
||||
};
|
||||
};
|
||||
|
||||
export default implementGuiAPI;
|
||||
Reference in New Issue
Block a user