Skip to content

Commit abee314

Browse files
committed
Fix blockly timeout
1 parent be5199e commit abee314

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ And then call "npm run build".
3333
- ...
3434

3535
## Changelog
36-
37-
### 4.6.8 (2020-05-16)
38-
* (Apollon77) Fix error filtering
39-
40-
### 4.6.7 (2020-05-16)
36+
### 4.6.9 (2020-05-16)
4137
* (bluefox) Fixed blockly blocks because of deprecated functions
4238
* (bluefox) Corrected schedule wizard
4339

io-package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"common": {
33
"name": "javascript",
4-
"version": "4.6.8",
4+
"version": "4.6.9",
55
"title": "Script Engine",
66
"titleLang": {
77
"en": "Script Engine",
@@ -22,7 +22,7 @@
2222
"AlCalzone"
2323
],
2424
"news": {
25-
"4.6.8": {
25+
"4.6.9": {
2626
"en": "Fixed blockly blocks because of deprecated functions",
2727
"de": "Blockierte Blöcke wegen veralteter Funktionen behoben",
2828
"ru": "Исправлены блочные блоки из-за устаревших функций",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iobroker.javascript",
3-
"version": "4.6.8",
3+
"version": "4.6.9",
44
"description": "Javascript/Coffescript Script Engine for ioBroker",
55
"author": "bluefox <dogafox@gmail.com>",
66
"contributors": [

src/public/google-blockly/own/blocks_timeout.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Blockly.Blocks['timeouts_settimeout'] = {
131131
},
132132
getVarModels: function () {
133133
var name = this.getFieldValue('NAME');
134-
return [{getId: function () {return name}, name: name}];
134+
return [{getId: function () {return name;}, type: 'timeout', name: name}];
135135
}
136136
};
137137

@@ -159,6 +159,16 @@ Blockly.Timeouts.getAllTimeouts = function (workspace) {
159159
result.push([blocks[i].getFieldValue('NAME'), blocks[i].getFieldValue('NAME')]);
160160
}
161161
}
162+
163+
// BF(2020.05.16) : for back compatibility. Remove it after 5 years
164+
if (window.scripts.loading) {
165+
var variables = workspace.getVariablesOfType('');
166+
variables.forEach(v => !result.find(it => it[0] === v.name) && result.push([v.name, v.name]));
167+
}
168+
169+
var variables1 = workspace.getVariablesOfType('timeout');
170+
variables1.forEach(v => !result.find(it => it[0] === v.name) && result.push([v.name, v.name]));
171+
162172
!result.length && result.push(['', '']);
163173

164174
return result;
@@ -175,7 +185,7 @@ Blockly.Blocks['timeouts_cleartimeout'] = {
175185
this.appendDummyInput('NAME')
176186
.appendField(Blockly.Translate('timeouts_cleartimeout'))
177187
.appendField(new Blockly.FieldDropdown(function () {
178-
return scripts.blocklyWorkspace ? Blockly.Timeouts.getAllTimeouts(scripts.blocklyWorkspace) : [];
188+
return window.scripts && window.scripts.blocklyWorkspace ? Blockly.Timeouts.getAllTimeouts(window.scripts.blocklyWorkspace) : [];
179189
}), 'NAME');
180190

181191
this.setPreviousStatement(true, null);
@@ -241,7 +251,7 @@ Blockly.Blocks['timeouts_setinterval'] = {
241251
},
242252
getVarModels: function () {
243253
var name = this.getFieldValue('NAME');
244-
return [{getId: function () {return name}, name: name}];
254+
return [{getId: function () {return name;}, type: 'interval', name: name}];
245255
}
246256
};
247257

@@ -277,7 +287,16 @@ Blockly.Timeouts.getAllIntervals = function (workspace) {
277287
}
278288
}
279289

280-
if (!result.length) result.push(['', '']);
290+
// BF(2020.05.16) : for back compatibility. Remove it after 5 years
291+
if (window.scripts.loading) {
292+
var variables = workspace.getVariablesOfType('');
293+
variables.forEach(v => !result.find(it => it[0] === v.name) && result.push([v.name, v.name]));
294+
}
295+
296+
var variables1 = workspace.getVariablesOfType('interval');
297+
variables1.forEach(v => !result.find(it => it[0] === v.name) && result.push([v.name, v.name]));
298+
299+
!result.length && result.push(['', '']);
281300

282301
return result;
283302
};
@@ -287,7 +306,7 @@ Blockly.Blocks['timeouts_clearinterval'] = {
287306
this.appendDummyInput("NAME")
288307
.appendField(Blockly.Translate('timeouts_clearinterval'))
289308
.appendField(new Blockly.FieldDropdown(function () {
290-
return scripts.blocklyWorkspace ? Blockly.Timeouts.getAllIntervals(scripts.blocklyWorkspace) : [];
309+
return window.scripts.blocklyWorkspace ? Blockly.Timeouts.getAllIntervals(window.scripts.blocklyWorkspace) : [];
291310
}), "NAME");
292311

293312
this.setPreviousStatement(true, null);

src/src/Components/BlocklyEditor.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,15 @@ class BlocklyEditor extends React.Component {
8787
// get all adapters, that can have blockly
8888
const toLoad = [];
8989
for (const id in objects) {
90-
if (!objects.hasOwnProperty(id) || !objects[id]) continue;
91-
if (!id.match(/^system\.adapter\./)) continue;
92-
if (objects[id].type !== 'adapter') continue;
90+
if (
91+
!objects.hasOwnProperty(id) ||
92+
!objects[id] ||
93+
!id.match(/^system\.adapter\./) ||
94+
objects[id].type !== 'adapter'
95+
) {
96+
continue;
97+
}
98+
9399
if (objects[id].common && objects[id].common.blockly) {
94100
console.log('Detected custom blockly: ' + objects[id].common.name);
95101
toLoad.push(objects[id].common.name);
@@ -394,7 +400,20 @@ class BlocklyEditor extends React.Component {
394400
if (!xml.startsWith('<xml')) {
395401
xml = '<xml xmlns="http://www.w3.org/1999/xhtml">' + xml + '</xml>';
396402
}
403+
let variables = xml.replace(/[\n\r]/g, '').match(/<variables>(.*)<\/variables>/);
404+
if (variables) {
405+
let vars = this.Blockly.utils.xml.textToDomDocument('<variables>' + variables[1] + '</variables>');
406+
if (vars) {
407+
let nodes = vars.childNodes && vars.childNodes[0] && vars.childNodes[0].childNodes;
408+
if (nodes) {
409+
for (let i = 0; i < nodes.length; i++) {
410+
nodes[i].id && this.blocklyWorkspace.createVariable(nodes[i].id);
411+
}
412+
}
413+
}
414+
}
397415
xml = xml.replace(/[\n\r]/g, '').replace(/<variables>.*<\/variables>/g, '');
416+
window.scripts.loading = true;
398417
let xmlBlocks = this.Blockly.Xml.textToDom(xml);
399418
if (xmlBlocks.nodeName === 'xml') {
400419
for (let b = 0; b < xmlBlocks.children.length; b++) {
@@ -403,6 +422,10 @@ class BlocklyEditor extends React.Component {
403422
} else {
404423
this.blocklyWorkspace.paste(xmlBlocks);
405424
}
425+
426+
window.scripts.loading = false;
427+
428+
406429
this.onBlocklyChanged();
407430
} catch (e) {
408431
this.setState({error: {text: e, title: I18n.t('Import error')}});
@@ -411,15 +434,19 @@ class BlocklyEditor extends React.Component {
411434
}
412435

413436
loadCode() {
414-
if (!this.blocklyWorkspace) return;
437+
if (!this.blocklyWorkspace) {
438+
return;
439+
}
415440

416441
this.ignoreChanges = true;
417442
this.blocklyWorkspace.clear();
418443

419444
try {
420445
const xml = this.jsCode2Blockly(this.originalCode) || '<xml xmlns="http://www.w3.org/1999/xhtml"></xml>';
446+
window.scripts.loading = true;
421447
const dom = this.Blockly.Xml.textToDom(xml);
422448
this.Blockly.Xml.domToWorkspace(dom, this.blocklyWorkspace);
449+
window.scripts.loading = false;
423450
} catch (e) {
424451
console.error(e);
425452
window.alert('Cannot extract Blockly code!');

0 commit comments

Comments
 (0)