advanced syntaxes: fix nested bracket regression, remove need for 2 brackets in JS{}

This commit is contained in:
jean-emmanuel 2021-05-05 09:41:06 +02:00
parent 88ce77ab70
commit e417399158
4 changed files with 27 additions and 17 deletions

View file

@ -4,9 +4,11 @@
- bug fixes
- prevent error with empty `OSC{}` blocks
- regression breaking advanced syntax blocks containing nested brackets
- advanced syntaxes
- new syntax for using custom variables in properties: `VAR{name, default}`
- `JS{}` blocks don't require 2 brackets anymore (`JS{{}}` still works)
- scripting
- add `getVar()` and `setVar` for reading and modifying custom variables. This allows modifying properties directly from scripts.

View file

@ -48,12 +48,12 @@ Options can contain `@{}` blocks.
??? example "Array/Object default value"
```js
JS{{
JS{
return OSC{address} || {
"key a": 1,
"key b", 2
}
}}
}
```
## Custom variables: `VAR{variableName, default}`
@ -61,7 +61,7 @@ Options can contain `@{}` blocks.
This syntax creates a custom variable in the widget that can be read and modified from any widget's `script` property (see [scripting](./scripting.md#getvarid-name)). Changes made to this variable will be applied automatically.
## Javascript: `JS{{ <code> }}`
## Javascript: `JS{ <code> }`
This syntax allows defining a property using [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript). The code will be compiled as a function and executed in a restricted context.
@ -69,7 +69,7 @@ This syntax allows defining a property using [Javascript](https://developer.mozi
- javascript [strict mode](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Strict_mode) is always enabled
- `setTimeout` and `setInterval` functions are not available
A property *cannot* contain multiple `JS{{}}` blocks
A property *cannot* contain multiple `JS{}` blocks
#### Available variables
@ -92,9 +92,9 @@ Parsing errors can be read in the console (++ctrl+k++).
## Javascript: `#{ <code> }`
This is a shorthand for the `JS{{}}` syntax, the only difference being that `<code>` is automatically prepended with a `return` statement.
This is a shorthand for the `JS{}` syntax, the only difference being that `<code>` is automatically prepended with a `return` statement.
A property *can* contain multiple `#{}` blocks.
!!! note "Compilation order"
`#{}` blocks are compiled after `JS{{}}` blocks
`#{}` blocks are compiled after `JS{}` blocks

View file

@ -510,11 +510,11 @@ class Widget extends EventEmitter {
if (typeof propValue == 'string') {
propValue = balancedReplace('@{', '}', propValue, (content)=>{
propValue = balancedReplace('@', '{', '}', propValue, (content)=>{
if (content.indexOf('@{') >= 0) {
content = balancedReplace('@{', '}', content, (subcontent)=>{
content = balancedReplace('@', '{', '}', content, (subcontent)=>{
return this.resolveProp(propName, '@{' + subcontent + '}', storeLinks ? 'nested' : false, this)
})
@ -627,7 +627,7 @@ class Widget extends EventEmitter {
})
propValue = balancedReplace('VAR{', '}', propValue, (args)=>{
propValue = balancedReplace('VAR', '{', '}', propValue, (args)=>{
if (args === '') return 'undefined'
@ -662,7 +662,7 @@ class Widget extends EventEmitter {
return varname
})
propValue = balancedReplace('OSC{', '}', propValue, (args)=>{
propValue = balancedReplace('OSC', '{', '}', propValue, (args)=>{
if (args === '') return 'undefined'
@ -704,7 +704,9 @@ class Widget extends EventEmitter {
})
try {
propValue = balancedReplace('JS{{', '}}', propValue, (code)=>{
propValue = balancedReplace('JS', '{', '}', propValue, (code)=>{
if (code[0] === '{' && code[code.length - 1] === '}') code = code.slice(1, code.length - 1)
if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope)
@ -720,7 +722,8 @@ class Widget extends EventEmitter {
}
try {
propValue = balancedReplace('#{', '}', propValue, (code)=>{
propValue = balancedReplace('#', '{', '}', propValue, (code)=>{
if (!this.parsers[code]) this.parsers[code] = vm.compile('return ' + code.trim(), defaultScope)
let r = this.parsers[code](jsScope, this.parsersLocalScope)

View file

@ -117,22 +117,27 @@ module.exports = {
})(),
balancedReplace(open, close, string, replacement) {
balancedReplace(tag, open, close, string, replacement) {
var b
var start
while (b = balanced(open, close, string)) {
while ((start = string.indexOf(tag + open)) >= 0) {
var substring = string.slice(start + tag.length),
b = balanced(open, close, substring)
if (typeof replacement === 'function') {
string = b.pre + replacement(b.body) + b.post
substring = b.pre + replacement(b.body) + b.post
} else {
string = b.pre + replacement + b.post
substring = b.pre + replacement + b.post
}
string = string.slice(0, start) + substring
}
return string