Skip to content

Commit 4afeab7

Browse files
committed
(bluefox) fix import of old scripts
(bluefox) ignore ".xxx" folders and names by mirroring (bluefox) fix extendForeignObject problem of js-controller 2.0
1 parent cce09b2 commit 4afeab7

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

admin/google-blockly/own/field_script.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ Blockly.b64EncodeUnicode = function(text) {
4040

4141
// Decoding base64 ⇢ UTF8
4242
Blockly.b64DecodeUnicode = function(text) {
43-
return decodeURIComponent(Array.prototype.map.call(atob(text), function(s) {
44-
return '%' + ('00' + s.charCodeAt(0).toString(16)).slice(-2)
45-
}).join(''));
43+
try {
44+
return decodeURIComponent(Array.prototype.map.call(atob(text), function(s) {
45+
return '%' + ('00' + s.charCodeAt(0).toString(16)).slice(-2)
46+
}).join(''));
47+
} catch (e) {
48+
// old style
49+
return atob(text || '');
50+
}
4651
};
4752

53+
4854
/**
4955
* Class for an editable text field.
5056
* @param {string} text The initial content of the field.

docs/en/javascript.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ Format of selector:
10641064
"name[commonAttr=something1](enumName=something2){nativeName=something3}[id=idfilter][state.id=idfilter]"
10651065
```
10661066

1067-
name can be: state, channel or device
1067+
name can be: state, channel, device or schedule
10681068
"idfilter" can have wildcards '*'
10691069

10701070
Prefixes ***(not implemented - should be discussed)*** :
@@ -1081,6 +1081,7 @@ Prefixes ***(not implemented - should be discussed)*** :
10811081
- `$('channel{TYPE=BLIND}[state.id=*.LEVEL]')` - Get all shutter of Homematic
10821082
- `$('channel[role=switch](rooms=Living room)[state.id=*.STATE]').setState(false)` - Switch all states with .STATE of channels with role "switch" in "Living room" to false
10831083
- `$('channel[state.id=*.STATE](functions=Windows)').each(function (id, i) {log(id);});` - print all states of enum "windows" in log
1084+
- `$('schedule[id=*65]').each(function (id, i) {log(id);});` - print all schedules with 65 at the end
10841085

10851086

10861087
- `$('.switch §"Living room")` - Take states with all switches in 'Living room' ***(not implemented - should be discussed)***

lib/mirror.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ class Mirror {
5959
watchFolders(root_) {
6060
root_ = root_.endsWith('/') ? root_ : root_ + '/';
6161

62-
const files = fs.readdirSync(root_);
62+
// ignore all .xxx folders, like ".git", ".idea", so users may not have scripts starting with "."
63+
const files = fs.readdirSync(root_).filter(name => !name.startsWith('.'));
6364
files.forEach(file => {
6465
const name = path.join(root_, file).replace(/\\/g, '/');
6566
const stat = fs.statSync(name);
66-
stat.isDirectory()&& this.watchFolders(name);
67+
stat.isDirectory() && this.watchFolders(name);
6768
});
6869

6970
if (!this.watchedFolder[root_]) {
@@ -159,6 +160,7 @@ class Mirror {
159160
}
160161
}
161162

163+
// TODO: node.js supports mkdirSync('filter/folder', {recursive: true}) => rewrite
162164
// create the folder recursively if not exists
163165
static createRecursiveDir(dirDisk) {
164166
const parts = dirDisk.replace(/\\/g, '/').split('/');
@@ -318,7 +320,8 @@ class Mirror {
318320
if (exists) {
319321
!this.watchedFolder[file] && this.watchFolders(file);
320322
// scan folder anew
321-
const files = fs.readdirSync(file);
323+
const files = fs.readdirSync(file).filter(name => !name.startsWith('.'));
324+
322325
// update all files in this directory
323326
files.forEach(f => this.onFileChange('change', path.join(file, f).replace(/\\/g, '/')));
324327
} else {
@@ -484,7 +487,7 @@ class Mirror {
484487
dirPath = dirPath || this.diskRoot;
485488
list = list || {};
486489
if (fs.existsSync(dirPath)) {
487-
const files = fs.readdirSync(dirPath);
490+
const files = fs.readdirSync(dirPath).filter(name => !name.startsWith('.'));
488491
files.forEach(file => {
489492
const fullName = path.join(dirPath, file);
490493
const stats = fs.statSync(fullName);

lib/sandbox.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ function sandBox(script, name, verbose, debug, context) {
215215
function getCommonTypeOf(value) {
216216
// @ts-ignore we do not support bigint
217217
return isArray(value) ? 'array'
218-
: isObject(value) ? 'object'
219-
: typeof value;
218+
: (isObject(value) ? 'object'
219+
: typeof value);
220220
}
221221

222222
function setStateHelper(sandbox, isBinary, id, state, isAck, callback) {
@@ -674,10 +674,10 @@ function sandBox(script, name, verbose, debug, context) {
674674

675675
// make sure this object satisfies all selectors
676676
return commonSelectors.every(selector =>
677-
// ensure a property exists
678-
(selector.value === undefined && objCommon[selector.attr] !== undefined)
679-
// or match exact values
680-
|| looselyEqualsString(objCommon[selector.attr], selector.value));
677+
// ensure a property exists
678+
(selector.value === undefined && objCommon[selector.attr] !== undefined)
679+
// or match exact values
680+
|| looselyEqualsString(objCommon[selector.attr], selector.value));
681681
}
682682

683683
/**
@@ -690,10 +690,10 @@ function sandBox(script, name, verbose, debug, context) {
690690
const objNative = obj.native;
691691
// make sure this object satisfies all selectors
692692
return nativeSelectors.every(selector =>
693-
// ensure a property exists
694-
(selector.value === undefined && objNative[selector.attr] !== undefined)
695-
// or match exact values
696-
|| looselyEqualsString(objNative[selector.attr], selector.value));
693+
// ensure a property exists
694+
(selector.value === undefined && objNative[selector.attr] !== undefined)
695+
// or match exact values
696+
|| looselyEqualsString(objNative[selector.attr], selector.value));
697697
}
698698

699699
/**
@@ -1345,7 +1345,7 @@ function sandBox(script, name, verbose, debug, context) {
13451345
}
13461346

13471347
let ts = mods.suncalc.getTimes(date, adapter.config.latitude, adapter.config.longitude)[pattern];
1348-
let nadir = mods.suncalc.getTimes(date, adapter.config.latitude, adapter.config.longitude)['nadir'];
1348+
const nadir = mods.suncalc.getTimes(date, adapter.config.latitude, adapter.config.longitude)['nadir'];
13491349
if (nadir.getDate() === date.getDate() && nadir.getHours() < 12) {
13501350
ts = mods.suncalc.getTimes(date.setDate(date.getDate() + 1), adapter.config.latitude, adapter.config.longitude)[pattern];
13511351
}
@@ -2752,7 +2752,7 @@ function sandBox(script, name, verbose, debug, context) {
27522752
}
27532753
} else {
27542754
sandbox.verbose && sandbox.log('extendObject(id=' + id + ', obj=' + JSON.stringify(obj) + ')', 'info');
2755-
adapter.extendForeignObject(id, obj, callback);
2755+
adapter.extendForeignObject(id, JSON.parse(JSON.stringify(obj)), callback);
27562756
}
27572757
};
27582758
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ Blockly.b64EncodeUnicode = function(text) {
4040

4141
// Decoding base64 ⇢ UTF8
4242
Blockly.b64DecodeUnicode = function(text) {
43-
return decodeURIComponent(Array.prototype.map.call(atob(text), function(s) {
44-
return '%' + ('00' + s.charCodeAt(0).toString(16)).slice(-2)
45-
}).join(''));
43+
try {
44+
return decodeURIComponent(Array.prototype.map.call(atob(text), function(s) {
45+
return '%' + ('00' + s.charCodeAt(0).toString(16)).slice(-2)
46+
}).join(''));
47+
} catch (e) {
48+
// old style
49+
return atob(text || '');
50+
}
4651
};
4752

53+
4854
/**
4955
* Class for an editable text field.
5056
* @param {string} text The initial content of the field.
@@ -138,7 +144,7 @@ Blockly.FieldScript.prototype.render_ = function() {
138144
this.textElement_.setAttribute('x', 0);
139145
}
140146

141-
// this.size_.height = Blockly.BlockSvg.MIN_BLOCK_Y;
147+
// this.size_.height = Blockly.BlockSvg.MIN_BLOCK_Y;
142148
// this.size_.width = Blockly.Field.getCachedWidth(this.textElement_);
143149
} else {
144150
width = 0;

0 commit comments

Comments
 (0)