a-syntax: compile JS{{}} before #{}
This commit is contained in:
parent
f0951e7aab
commit
9c9b132c62
2 changed files with 25 additions and 20 deletions
|
|
@ -67,6 +67,8 @@ This syntax allows writing formulas in pure javascript. The code will be compile
|
|||
- 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
|
||||
|
||||
#### Available variables
|
||||
|
||||
- `console`: javascript console
|
||||
|
|
@ -87,4 +89,7 @@ Parsing errors can be read in the DevTool console (F12).
|
|||
|
||||
## 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
|
||||
|
|
|
|||
|
|
@ -629,6 +629,24 @@ class Widget extends EventEmitter {
|
|||
return varname
|
||||
})
|
||||
|
||||
try {
|
||||
propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{
|
||||
|
||||
if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope)
|
||||
|
||||
let r = this.parsers[code](jsScope, this.parsersLocalScope)
|
||||
|
||||
if (r === undefined) r = ''
|
||||
|
||||
return typeof r !== 'string' ? JSON.stringify(r) : r
|
||||
|
||||
})
|
||||
} catch (err) {
|
||||
let stackline = err.stack ? (err.stack.match(/>:([0-9]+):[0-9]+/) || '') : '',
|
||||
line = stackline.length > 1 ? ' at line ' + (parseInt(stackline[1]) - 2) : ''
|
||||
console.log((this.getProp('id') || this.props.id) + '.' + propName + ': JS{{}} error:\n' + err + line)
|
||||
}
|
||||
|
||||
try {
|
||||
propValue = propValue.replace(/#\{(?:[^{}]|\{[^{}]*\})*\}/g, (m)=>{
|
||||
// one bracket nesting allowed, if we need two: #\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}
|
||||
|
|
@ -650,24 +668,6 @@ class Widget extends EventEmitter {
|
|||
console.log((this.getProp('id') || this.props.id) + '.' + propName + ': #{} error:\n' + err + line)
|
||||
}
|
||||
|
||||
try {
|
||||
propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{
|
||||
|
||||
if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope)
|
||||
|
||||
let r = this.parsers[code](jsScope, this.parsersLocalScope)
|
||||
|
||||
if (r === undefined) r = ''
|
||||
|
||||
return typeof r !== 'string' ? JSON.stringify(r) : r
|
||||
|
||||
})
|
||||
} catch (err) {
|
||||
let stackline = err.stack ? (err.stack.match(/>:([0-9]+):[0-9]+/) || '') : '',
|
||||
line = stackline.length > 1 ? ' at line ' + (parseInt(stackline[1]) - 2) : ''
|
||||
console.log((this.getProp('id') || this.props.id) + '.' + propName + ': JS{{}} error:\n' + err + line)
|
||||
}
|
||||
|
||||
for (let k in variables) {
|
||||
var v = typeof variables[k] === 'string' ? variables[k] : JSON.stringify(variables[k])
|
||||
propValue = propValue.replace(new RegExp(k, 'g'), v)
|
||||
|
|
@ -846,7 +846,7 @@ class Widget extends EventEmitter {
|
|||
|
||||
if (data.address === 'auto') data.address = '/' + this.getProp('id')
|
||||
if (oldData.address === 'auto') oldData.address = '/' + this.getProp('id')
|
||||
|
||||
|
||||
widgetManager.registerWidget(this, data, oldData)
|
||||
return
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue