Skip to content

Commit dbfa873

Browse files
committed
Move away from exposing the VM, but still use some black magic :smile :
1 parent 6ed6be7 commit dbfa873

File tree

1 file changed

+61
-27
lines changed

1 file changed

+61
-27
lines changed

src/containers/blocks.jsx

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ class Blocks extends React.Component {
156156

157157
const workspaceConfig = defaultsDeep({},
158158
this.props.options,
159-
{
160-
vm: this.props.vm
161-
},
162159
{
163160
rtl: this.props.isRtl,
164161
toolbox: this.props.toolboxXML,
@@ -170,7 +167,36 @@ class Blocks extends React.Component {
170167
Blocks.defaultOptions
171168
);
172169
this.workspace = this.ScratchBlocks.inject(this.blocks, workspaceConfig);
173-
this.workspace.vm = this.props.vm;
170+
171+
if (this.ScratchBlocks.Procedures &&
172+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback) {
173+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback(() => {
174+
const runtime = this.props.vm && this.props.vm.runtime;
175+
if (!runtime || !runtime.targets) {
176+
return [];
177+
}
178+
179+
const procedureBlocks = [];
180+
for (let i = 0; i < runtime.targets.length; i++) {
181+
const target = runtime.targets[i];
182+
const blocks = target && target.blocks && target.blocks._blocks;
183+
if (!blocks) continue;
184+
185+
for (const blockId in blocks) {
186+
if (!Object.prototype.hasOwnProperty.call(blocks, blockId)) continue;
187+
const block = blocks[blockId];
188+
if (!block) continue;
189+
if (
190+
block.opcode === 'procedures_prototype' ||
191+
block.opcode === this.ScratchBlocks.PROCEDURES_CALL_BLOCK_TYPE
192+
) {
193+
procedureBlocks.push(block);
194+
}
195+
}
196+
}
197+
return procedureBlocks;
198+
});
199+
}
174200
AddonHooks.blocklyWorkspace = this.workspace;
175201

176202
// Register buttons under new callback keys for creating variables,
@@ -293,6 +319,12 @@ class Blocks extends React.Component {
293319
componentWillUnmount () {
294320
this.detachVM();
295321
this.unmounted = true;
322+
323+
if (this.ScratchBlocks && this.ScratchBlocks.Procedures &&
324+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback) {
325+
this.ScratchBlocks.Procedures.setProcedureBlocksAcrossTargetsCallback(() => []);
326+
}
327+
296328
this.workspace.dispose();
297329
clearTimeout(this.toolboxUpdateTimeout);
298330

@@ -509,6 +541,29 @@ class Blocks extends React.Component {
509541
}
510542
log.error(error);
511543
}
544+
545+
const allBlocks = this.workspace.getAllBlocks(false);
546+
for (let i = 0; i < allBlocks.length; i++) {
547+
const block = allBlocks[i];
548+
if (block.type === this.ScratchBlocks.PROCEDURES_CALL_BLOCK_TYPE && block.getProcCode) {
549+
const procCode = block.getProcCode();
550+
if (procCode) {
551+
// Get the latest global procedure mutation from the provider
552+
const globalMutations = this.ScratchBlocks.Procedures.allGlobalProcedureMutations(this.workspace);
553+
for (let j = 0; j < globalMutations.length; j++) {
554+
const mutation = globalMutations[j];
555+
if (this.ScratchBlocks.Names.equals(mutation.getAttribute('proccode'), procCode)) {
556+
// Found the matching procedure, update the call block with the latest mutation
557+
if (block.domToMutation) {
558+
block.domToMutation(mutation);
559+
}
560+
break;
561+
}
562+
}
563+
}
564+
}
565+
}
566+
512567
this.workspace.addChangeListener(this.props.vm.blockListener);
513568

514569
if (this.props.vm.editingTarget && this.props.workspaceMetrics.targets[this.props.vm.editingTarget.id]) {
@@ -658,29 +713,8 @@ class Blocks extends React.Component {
658713
handleCustomProceduresClose (data) {
659714
this.props.onRequestCloseCustomProcedures(data);
660715
const ws = this.workspace;
661-
if (!ws) return;
662-
663-
const flyout = ws.getFlyout && ws.getFlyout();
664-
if (flyout) {
665-
flyout.setRecyclingEnabled(false);
666-
}
667-
668-
const toolboxXML = this.getToolboxXML();
669-
if (toolboxXML) {
670-
this.props.updateToolboxState(toolboxXML);
671-
}
672-
673-
this.requestToolboxUpdate();
674-
this.withToolboxUpdates(() => {
675-
const refreshedFlyout = ws.getFlyout && ws.getFlyout();
676-
ws.refreshToolboxSelection_();
677-
if (ws.toolbox_) {
678-
ws.toolbox_.scrollToCategoryById('myBlocks');
679-
}
680-
if (refreshedFlyout) {
681-
refreshedFlyout.setRecyclingEnabled(true);
682-
}
683-
});
716+
ws.refreshToolboxSelection_();
717+
ws.toolbox_.scrollToCategoryById('myBlocks');
684718
}
685719
handleDrop (dragInfo) {
686720
fetch(dragInfo.payload.bodyUrl)

0 commit comments

Comments
 (0)