diff --git a/tests/test-scratch-vm-extension-tw.js b/tests/test-scratch-vm-extension-tw.js index e263eba..ff768f3 100644 --- a/tests/test-scratch-vm-extension-tw.js +++ b/tests/test-scratch-vm-extension-tw.js @@ -139,3 +139,12 @@ Scratch.fetch('https://example.com', { 'Content-Type': 'application/json' } }); + +Scratch.gui.getBlockly().then((ScratchBlocks) => { + ScratchBlocks.getMainWorkspace()?.blockDB_; + ScratchBlocks.Workspace; +}); +Scratch.gui.getBlocklyEagerly().then((ScratchBlocks) => { + ScratchBlocks.getMainWorkspace()?.blockDB_; + ScratchBlocks.Workspace; +}); diff --git a/types/scratch-blocks.d.ts b/types/scratch-blocks.d.ts index ac653a1..5694a89 100644 --- a/types/scratch-blocks.d.ts +++ b/types/scratch-blocks.d.ts @@ -170,7 +170,7 @@ declare namespace ScratchBlocks { } - interface RealBlockly { + interface RealBlockly extends BlocklyGlobal { Workspace: WorkspaceConstructor; WorkspaceSvg: typeof WorkspaceSvg; } diff --git a/types/scratch-vm-extension.d.ts b/types/scratch-vm-extension.d.ts index 7f76b0d..9b8acbf 100644 --- a/types/scratch-vm-extension.d.ts +++ b/types/scratch-vm-extension.d.ts @@ -70,6 +70,28 @@ declare namespace Scratch { /** Current language. May change while the project runs. Possible values: "en", "es", "it", etc. */ let language: string; } + /** + * Only available when run in a scratch-gui environment (website, desktop app). + * In other environments (packager), will be undefined. + */ + namespace 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. + * + * ScratchBlocks becoming available does not necessarily mean the user is in the editor due + * to getBlocklyEagerly() also existing. + */ + function getBlockly(): Promise; + /** + * 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. + */ + function getBlocklyEagerly(): Promise; + } // Note that the 'B' in the BOOLEAN enums are capitalized in Scratch. It is not a typo in this file.