@kubohiroya/turbowarp-plus-editor-test-runtime 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +6 -1
- package/README.md +5 -0
- package/dist/runtime.js +111 -82
- package/package.json +2 -2
- package/page.d.ts +18 -1
- package/src/runtime.js +38 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-test-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A minimal TurboWarp Plus Editor runtime for package tests: the Editor's pinned VM and Scratch Blocks with one workspace, implementing the editor host contract",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"test:browser": "npm run build && node --test test/*.browser.js"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@kubohiroya/turbowarp-plus-editor-host": "0.1.
|
|
38
|
+
"@kubohiroya/turbowarp-plus-editor-host": "0.1.1",
|
|
39
39
|
"@kubohiroya/turbowarp-plus-editor-test-runtime-blocks": "0.1.0",
|
|
40
40
|
"esbuild": "0.25.12"
|
|
41
41
|
},
|
package/page.d.ts
CHANGED
|
@@ -6,11 +6,28 @@ export interface EditorRuntimeHost extends EditorHost {
|
|
|
6
6
|
workspace: any;
|
|
7
7
|
/** Loads a project (`project.json` contents) and edits its first sprite. The project is then saved. */
|
|
8
8
|
loadProject(project: string | object): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Registers an extension object as the VM registers its built-in ones (no sandbox, Loader or review)
|
|
11
|
+
* and defines its blocks in the workspace. Returns the VM service name.
|
|
12
|
+
*/
|
|
13
|
+
registerExtension(extension: {getInfo(): {id: string}}): string;
|
|
14
|
+
}
|
|
15
|
+
/** The VM's own modules an extension host needs, such as the Loader's registrar. */
|
|
16
|
+
export interface VMModules {
|
|
17
|
+
ScratchCommon: Record<string, any>;
|
|
18
|
+
createTranslate(vm: unknown): unknown;
|
|
19
|
+
staticFetch(url: unknown): Response | null;
|
|
20
|
+
dispatch: {services: Record<string, any>; setServiceSync(name: string, provider: unknown): void};
|
|
9
21
|
}
|
|
10
22
|
export interface EditorRuntimeOptions {
|
|
11
23
|
locale?: string;
|
|
12
24
|
project?: string | object;
|
|
13
25
|
}
|
|
14
26
|
declare global {
|
|
15
|
-
const EditorRuntime: {
|
|
27
|
+
const EditorRuntime: {
|
|
28
|
+
start(container: HTMLElement, options?: EditorRuntimeOptions): Promise<EditorRuntimeHost>;
|
|
29
|
+
vmModules: VMModules;
|
|
30
|
+
/** The VM constructor, for a second VM such as a preview. */
|
|
31
|
+
VirtualMachine: new () => any;
|
|
32
|
+
};
|
|
16
33
|
}
|
package/src/runtime.js
CHANGED
|
@@ -6,6 +6,30 @@ import {deserializeBlocks} from 'scratch-vm/src/serialization/sb3.js';
|
|
|
6
6
|
import ScratchStorage from '@turbowarp/scratch-storage';
|
|
7
7
|
import ScratchRender from 'scratch-render';
|
|
8
8
|
import {BitmapAdapter} from '@turbowarp/scratch-svg-renderer';
|
|
9
|
+
import ScratchCommon from 'scratch-vm/src/extension-support/tw-extension-api-common.js';
|
|
10
|
+
import createTranslate from 'scratch-vm/src/extension-support/tw-l10n.js';
|
|
11
|
+
import staticFetch from 'scratch-vm/src/util/tw-static-fetch.js';
|
|
12
|
+
import dispatch from 'scratch-vm/src/dispatch/central-dispatch.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The VM modules an extension host needs as the VM's own instances: `dispatch` is a singleton, so a
|
|
16
|
+
* copy from anywhere else would not be the one this VM uses. The Loader's registrar takes these.
|
|
17
|
+
*/
|
|
18
|
+
export const vmModules = Object.freeze({ScratchCommon, createTranslate, staticFetch, dispatch});
|
|
19
|
+
/** The VM constructor, for a test that needs a second VM, such as a preview. */
|
|
20
|
+
export {VirtualMachine};
|
|
21
|
+
|
|
22
|
+
// Define an extension's blocks in Scratch Blocks, as the GUI does when one is added or changes:
|
|
23
|
+
// custom fields and menus are blocks too, so they are defined first. Dynamic blocks need the GUI's own
|
|
24
|
+
// block factory and are not supported here.
|
|
25
|
+
function defineExtensionBlocks(Blockly, categoryInfo) {
|
|
26
|
+
const json = [
|
|
27
|
+
...Object.values(categoryInfo.customFieldTypes || {}).map(field => field.scratchBlocksDefinition),
|
|
28
|
+
...(categoryInfo.menus || []),
|
|
29
|
+
...(categoryInfo.blocks || [])
|
|
30
|
+
].filter(block => block && block.json && !(block.info && block.info.isDynamic)).map(block => block.json);
|
|
31
|
+
if (json.length) Blockly.defineBlocksWithJsonArray(json);
|
|
32
|
+
}
|
|
9
33
|
|
|
10
34
|
function workspaceXml(vm, target) {
|
|
11
35
|
const variables = Object.assign({}, vm.runtime.getTargetForStage().variables, target.variables);
|
|
@@ -30,6 +54,9 @@ export async function start(container, {locale = 'en', project} = {}) {
|
|
|
30
54
|
zoom: {controls: false, wheel: false, startScale: 0.675}, scrollbars: true
|
|
31
55
|
});
|
|
32
56
|
workspace.addChangeListener(vm.blockListener);
|
|
57
|
+
const defineBlocks = categoryInfo => defineExtensionBlocks(Blockly, categoryInfo);
|
|
58
|
+
vm.on('EXTENSION_ADDED', defineBlocks);
|
|
59
|
+
vm.on('BLOCKSINFO_UPDATE', defineBlocks);
|
|
33
60
|
vm.on('workspaceUpdate', data => {
|
|
34
61
|
Blockly.Events.disable();
|
|
35
62
|
try {Blockly.Xml.clearWorkspaceAndLoadFromXml(Blockly.Xml.textToDom(data.xml), workspace);}
|
|
@@ -54,6 +81,17 @@ export async function start(container, {locale = 'en', project} = {}) {
|
|
|
54
81
|
if (targets.includes(vm.editingTarget)) vm.emit('workspaceUpdate', {xml: workspaceXml(vm, vm.editingTarget)});
|
|
55
82
|
vm.emit('EXTERNAL_BLOCKS_RELOADED', targets);
|
|
56
83
|
},
|
|
84
|
+
/**
|
|
85
|
+
* Registers an extension object with the VM as the VM does for its built-in ones: no sandbox, no
|
|
86
|
+
* Loader and no review. Its blocks are defined in the workspace. Returns the VM service name.
|
|
87
|
+
*/
|
|
88
|
+
registerExtension(extension) {
|
|
89
|
+
const {id} = extension.getInfo();
|
|
90
|
+
if (vm.extensionManager.isExtensionLoaded(id)) throw new Error(`Extension ${id} is already registered`);
|
|
91
|
+
const service = vm.extensionManager._registerInternalExtension(extension);
|
|
92
|
+
vm.extensionManager._loadedExtensions.set(id, service);
|
|
93
|
+
return service;
|
|
94
|
+
},
|
|
57
95
|
async loadProject(json) {
|
|
58
96
|
await vm.loadProject(typeof json === 'string' ? json : JSON.stringify(json));
|
|
59
97
|
const sprite = vm.runtime.targets.find(target => !target.isStage) ?? vm.runtime.targets[0];
|