mirror of
https://github.com/google/blockly.git
synced 2026-05-09 21:50:12 +02:00
Fix interpreter restart when blocks change
`event instanceof Blockly.Events.Ui` no longer works for some reason, use event.isUiEvent property instead. Also, remove redundant `generateCodeAndLoadIntoInterpreter` function.
This commit is contained in:
@@ -141,6 +141,8 @@
|
||||
var demoWorkspace = Blockly.inject('blocklyDiv',
|
||||
{media: '../../media/',
|
||||
toolbox: document.getElementById('toolbox')});
|
||||
Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
|
||||
Blockly.JavaScript.addReservedWords('highlightBlock');
|
||||
Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),
|
||||
demoWorkspace);
|
||||
|
||||
@@ -174,7 +176,6 @@
|
||||
}
|
||||
|
||||
var highlightPause = false;
|
||||
var latestCode = '';
|
||||
|
||||
function highlightBlock(id) {
|
||||
demoWorkspace.highlightBlock(id);
|
||||
@@ -188,14 +189,7 @@
|
||||
if (clearOutput) {
|
||||
outputArea.value = 'Program output:\n=================';
|
||||
}
|
||||
}
|
||||
|
||||
function generateCodeAndLoadIntoInterpreter() {
|
||||
// Generate JavaScript code and parse it.
|
||||
Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
|
||||
Blockly.JavaScript.addReservedWords('highlightBlock');
|
||||
latestCode = Blockly.JavaScript.workspaceToCode(demoWorkspace);
|
||||
resetStepUi(true);
|
||||
myInterpreter = null;
|
||||
}
|
||||
|
||||
function stepCode() {
|
||||
@@ -203,6 +197,7 @@
|
||||
// First statement of this code.
|
||||
// Clear the program output.
|
||||
resetStepUi(true);
|
||||
var latestCode = Blockly.JavaScript.workspaceToCode(demoWorkspace);
|
||||
myInterpreter = new Interpreter(latestCode, initApi);
|
||||
|
||||
// And then show generated code in an alert.
|
||||
@@ -224,7 +219,6 @@
|
||||
// Program complete, no more code to execute.
|
||||
outputArea.value += '\n\n<< Program complete >>';
|
||||
|
||||
myInterpreter = null;
|
||||
resetStepUi(false);
|
||||
|
||||
// Cool down, to discourage accidentally restarting the program.
|
||||
@@ -241,12 +235,10 @@
|
||||
} while (hasMoreCode && !highlightPause);
|
||||
}
|
||||
|
||||
// Load the interpreter now, and upon future changes.
|
||||
generateCodeAndLoadIntoInterpreter();
|
||||
demoWorkspace.addChangeListener(function(event) {
|
||||
if (!(event instanceof Blockly.Events.Ui)) {
|
||||
// Something changed. Parser needs to be reloaded.
|
||||
generateCodeAndLoadIntoInterpreter();
|
||||
if (!event.isUiEvent) {
|
||||
// Something changed. Interpreter needs to be reloaded.
|
||||
resetStepUi(true);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user