Skip to content

Commit 5c90e4b

Browse files
authored
Merge pull request #227 from /issues/217
fixed could not convert extension ruby code to block if never enabled…
2 parents e6dc3e8 + f446c2c commit 5c90e4b

File tree

1 file changed

+45
-3
lines changed
  • src/lib/ruby-to-blocks-converter

1 file changed

+45
-3
lines changed

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ import GdxForConverter from './gdx_for';
2727
/* eslint-disable no-invalid-this */
2828
const ColorRegexp = /^#[0-9a-fA-F]{6}$/;
2929

30+
// from scratch-vm/src/serialization/sb3.js
31+
const CORE_EXTENSIONS = [
32+
'argument',
33+
'colour',
34+
'control',
35+
'data',
36+
'event',
37+
'looks',
38+
'math',
39+
'motion',
40+
'operator',
41+
'procedures',
42+
'sensing',
43+
'sound'
44+
];
45+
46+
// from scratch-vm/src/serialization/sb3.js
47+
const getExtensionIdForOpcode = function (opcode) {
48+
const index = opcode.indexOf('_');
49+
const prefix = opcode.substring(0, index);
50+
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
51+
if (prefix !== '') return prefix;
52+
}
53+
return null;
54+
};
55+
3056
/**
3157
* Class for a block converter that translates ruby code into the blocks.
3258
*/
@@ -80,6 +106,7 @@ class RubyToBlocksConverter {
80106
errors: [],
81107
argumentBlocks: {},
82108
procedureCallBlocks: {},
109+
extensionIDs: new Set(),
83110

84111
blocks: {},
85112
blockTypes: {},
@@ -127,6 +154,11 @@ class RubyToBlocksConverter {
127154
`could not convert ${block.opcode}: ${this._getSource(block.node)}`
128155
);
129156
}
157+
158+
const extensionID = getExtensionIdForOpcode(block.opcode);
159+
if (extensionID) {
160+
this._context.extensionIDs.add(extensionID);
161+
}
130162
});
131163
return true;
132164
} catch (e) {
@@ -182,11 +214,21 @@ class RubyToBlocksConverter {
182214
Object.keys(target.blocks._blocks).forEach(blockId => {
183215
target.blocks.deleteBlock(blockId);
184216
});
185-
Object.keys(this._context.blocks).forEach(blockId => {
186-
target.blocks.createBlock(this._context.blocks[blockId]);
217+
218+
const extensionPromises = [];
219+
this._context.extensionIDs.forEach(extensionID => {
220+
if (!this.vm.extensionManager.isExtensionLoaded(extensionID)) {
221+
extensionPromises.push(this.vm.extensionManager.loadExtensionURL(extensionID));
222+
}
187223
});
188224

189-
this.vm.emitWorkspaceUpdate();
225+
Promise.all(extensionPromises).then(() => {
226+
Object.keys(this._context.blocks).forEach(blockId => {
227+
target.blocks.createBlock(this._context.blocks[blockId]);
228+
});
229+
230+
this.vm.emitWorkspaceUpdate();
231+
});
190232
}
191233

192234
_callConvertersHandler (handlerName) {

0 commit comments

Comments
 (0)