Skip to content

Commit 599da1e

Browse files
committed
Filter Blockly events for blocks onchange and arduino code generation
This also fixes an issue when a UI event triggered a code generation, which in turn updated a block in a way that caused a new UI event to fire and repeat this process in a circle.
1 parent 6732413 commit 599da1e

File tree

6 files changed

+40
-22
lines changed

6 files changed

+40
-22
lines changed

ardublockly/ardublockly.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,6 @@ Ardublockly.PREV_ARDUINO_CODE_ = 'void setup() {\n\n}\n\n\nvoid loop() {\n\n}';
520520
* the blocks.
521521
*/
522522
Ardublockly.renderContent = function() {
523-
// Only regenerate the code if a block is not being dragged
524-
if (Ardublockly.blocklyIsDragging()) return;
525-
526523
// Render Arduino Code with latest change highlight and syntax highlighting
527524
var arduinoCode = Ardublockly.generateArduino();
528525
if (arduinoCode !== Ardublockly.PREV_ARDUINO_CODE_) {

ardublockly/ardublockly_blockly.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ Ardublockly.injectBlockly = function(blocklyEl, toolboxXml, blocklyPath) {
6363

6464
/** Binds the event listeners relevant to Blockly. */
6565
Ardublockly.bindBlocklyEventListeners = function() {
66-
Ardublockly.workspace.addChangeListener(Ardublockly.renderContent);
67-
66+
Ardublockly.workspace.addChangeListener(function(event) {
67+
if (event.type != Blockly.Events.UI) {
68+
Ardublockly.renderContent();
69+
}
70+
});
6871
// Ensure the Blockly workspace resizes accordingly
6972
window.addEventListener('resize',
7073
function() { Blockly.asyncSvgResize(Ardublockly.workspace); }, false);
@@ -78,8 +81,7 @@ Ardublockly.generateArduino = function() {
7881
/** @return {!string} Generated XML code from the Blockly workspace. */
7982
Ardublockly.generateXml = function() {
8083
var xmlDom = Blockly.Xml.workspaceToDom(Ardublockly.workspace);
81-
var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
82-
return xmlText;
84+
return Blockly.Xml.domToPrettyText(xmlDom);
8385
};
8486

8587
/**
@@ -133,8 +135,7 @@ Ardublockly.replaceBlocksfromXml = function(blocksXml) {
133135
};
134136

135137
/**
136-
* Parses the XML from its argument input to generate and add blocks to the
137-
* Blockly workspace.
138+
* Parses the XML from its argument to add the blocks to the workspace.
138139
* @param {!string} blocksXmlDom String of XML DOM code for the blocks.
139140
* @return {!boolean} Indicates if the XML into blocks parse was successful.
140141
*/

blockly/blocks/arduino/serial.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ Blockly.Blocks['serial_print'] = {
9090
* block if not valid data is found.
9191
* @this Blockly.Block
9292
*/
93-
onchange: function() {
94-
if (!this.workspace) { return; } // Block has been deleted.
93+
onchange: function(event) {
94+
if (!this.workspace || event.type == Blockly.Events.MOVE ||
95+
event.type == Blockly.Events.UI) {
96+
return; // Block deleted or irrelevant event
97+
}
9598

9699
// Get the Serial instance from this block
97100
var thisInstanceName = this.getFieldValue('SERIAL_ID');
98-
99101
// Iterate through top level blocks to find setup instance for the serial id
100102
var blocks = Blockly.mainWorkspace.getTopBlocks();
101103
var setupInstancePresent = false;
@@ -105,6 +107,7 @@ Blockly.Blocks['serial_print'] = {
105107
var setupBlockInstanceName = func.call(blocks[x]);
106108
if (thisInstanceName == setupBlockInstanceName) {
107109
setupInstancePresent = true;
110+
break;
108111
}
109112
}
110113
}

blockly/blocks/arduino/spi.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ Blockly.Blocks['spi_transfer'] = {
110110
* block if not valid data is found.
111111
* @this Blockly.Block
112112
*/
113-
onchange: function() {
114-
if (!this.workspace) { return; } // Block has been deleted.
113+
onchange: function(event) {
114+
if (!this.workspace || event.type == Blockly.Events.MOVE ||
115+
event.type == Blockly.Events.UI) {
116+
return; // Block deleted or irrelevant event
117+
}
115118

116119
// Get the Serial instance from this block
117120
var thisInstanceName = this.getFieldValue('SPI_ID');
@@ -130,8 +133,9 @@ Blockly.Blocks['spi_transfer'] = {
130133
}
131134

132135
if (!setupInstancePresent) {
133-
this.setWarningText(Blockly.Msg.ARD_SPI_TRANS_WARN1.replace('%1', thisInstanceName),
134-
'spi_setup');
136+
this.setWarningText(
137+
Blockly.Msg.ARD_SPI_TRANS_WARN1.replace('%1', thisInstanceName),
138+
'spi_setup');
135139
} else {
136140
this.setWarningText(null, 'spi_setup');
137141
}
@@ -164,8 +168,8 @@ Blockly.Blocks['spi_transfer'] = {
164168
}
165169
// If the old value is not present any more, add a warning to the block.
166170
if (!currentValuePresent) {
167-
this.setWarningText(Blockly.Msg.ARD_SPI_TRANS_WARN2.replace('%1', fieldValue),
168-
'bPin');
171+
this.setWarningText(
172+
Blockly.Msg.ARD_SPI_TRANS_WARN2.replace('%1', fieldValue), 'bPin');
169173
} else {
170174
this.setWarningText(null, 'bPin');
171175
}

blockly/blocks/arduino/stepper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ Blockly.Blocks['stepper_step'] = {
161161
* It checks/warns if the selected stepper instance has a config block.
162162
* @this Blockly.Block
163163
*/
164-
onchange: function() {
165-
if (!this.workspace) return; // Block has been deleted.
164+
onchange: function(event) {
165+
if (!this.workspace || event.type == Blockly.Events.MOVE ||
166+
event.type == Blockly.Events.UI) {
167+
return; // Block deleted or irrelevant event
168+
}
166169

167170
var instanceName = this.getFieldValue('STEPPER_NAME')
168171
if (Blockly.Instances.isInstancePresent(instanceName, 'Stepper', this)) {

blockly/blocks/arduino/tone.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ Blockly.Blocks['io_tone'] = {
3535
this.setTooltip(Blockly.Msg.ARD_TONE_TIP);
3636
this.setHelpUrl('https://www.arduino.cc/en/Reference/tone');
3737
},
38-
onchange: function() {
39-
var freq = Blockly.Arduino.valueToCode(this, "FREQUENCY", Blockly.Arduino.ORDER_ATOMIC)
38+
/**
39+
* Called whenever anything on the workspace changes.
40+
* It checks frequency values and sets a warning if out of range.
41+
* @this Blockly.Block
42+
*/
43+
onchange: function(event) {
44+
if (!this.workspace || event.type == Blockly.Events.MOVE ||
45+
event.type == Blockly.Events.UI) {
46+
return; // Block deleted or irrelevant event
47+
}
48+
var freq = Blockly.Arduino.valueToCode(
49+
this, "FREQUENCY", Blockly.Arduino.ORDER_ATOMIC)
4050
if (freq < 31 || freq > 65535) {
4151
this.setWarningText(Blockly.Msg.ARD_TONE_WARNING, 'io_tone');
4252
} else {

0 commit comments

Comments
 (0)