prop-syntaxes: add JS{{}}, print #{} errors
This commit is contained in:
parent
f840a5e114
commit
edc96b89e3
4 changed files with 56 additions and 2 deletions
|
|
@ -1,5 +1,11 @@
|
|||
# Changelog
|
||||
|
||||
## next
|
||||
|
||||
- widgets
|
||||
- new `JS{{}}` property syntax for javascript lovers
|
||||
- errors encountered in `#{}` blocks are now printed to the client console (F12)
|
||||
|
||||
## 0.44.0
|
||||
|
||||
- bug fixes
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@
|
|||
"json5": "2.1.0",
|
||||
"jsondiffpatch": "0.3.11",
|
||||
"keyboardjs": "github:jean-emmanuel/KeyboardJS",
|
||||
"loop-protect": "github:jean-emmanuel/loop-protect#v1.0.1",
|
||||
"licensify": "3.1.3",
|
||||
"mathjs": "5.8.0",
|
||||
"minimatch": "3.0.4",
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ var EventEmitter = require('../../events/event-emitter'),
|
|||
osc = require('../../osc'),
|
||||
nanoid = require('nanoid/generate'),
|
||||
widgetManager = require('../../managers/widgets'),
|
||||
{math} = require('../utils'),
|
||||
{math, evaljs} = require('../utils'),
|
||||
scopeCss = require('scope-css'),
|
||||
{iconify} = require('../../ui/utils'),
|
||||
resize = require('../../events/resize'),
|
||||
|
|
@ -136,6 +136,7 @@ class Widget extends EventEmitter {
|
|||
this.props = options.props
|
||||
this.errors = {}
|
||||
this.parsers = {}
|
||||
this.jsparsers = {}
|
||||
this.parent = options.root ? widgetManager : options.parent
|
||||
this.parentNode = options.parentNode
|
||||
this.hash = nanoid('abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
|
||||
|
|
@ -593,7 +594,17 @@ class Widget extends EventEmitter {
|
|||
|
||||
return typeof r != 'string' ? JSON.stringify(r) : r
|
||||
})
|
||||
} catch (err) {}
|
||||
} catch (err) {console.debug('#{} parsing error:\n' + err)}
|
||||
|
||||
try {
|
||||
propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{
|
||||
if (!this.jsparsers[code]) this.jsparsers[code] = evaljs(code)
|
||||
|
||||
let r = this.jsparsers[code](mathscope)
|
||||
|
||||
return typeof r !== 'string' ? JSON.stringify(r) : r
|
||||
})
|
||||
} catch (err) {console.debug('JS{{}} parsing error:\n' + err)}
|
||||
|
||||
for (let k in variables) {
|
||||
var v = typeof variables[k] === 'string' ? variables[k] : JSON.stringify(variables[k])
|
||||
|
|
|
|||
|
|
@ -104,6 +104,42 @@ module.exports = {
|
|||
math.expression.mathWithTransform = Object.assign({}, math)
|
||||
|
||||
return math
|
||||
})(),
|
||||
|
||||
evaljs: (function(){
|
||||
|
||||
var sandbox = document.createElement('iframe'),
|
||||
loopProtect = require('loop-protect')
|
||||
|
||||
loopProtect.alias = '__protect'
|
||||
loopProtect.hit = function (line) {
|
||||
throw 'Potential infinite loop found on line ' + line
|
||||
}
|
||||
|
||||
sandbox.style.display = 'none'
|
||||
sandbox.sandbox = "allow-scripts allow-same-origin"
|
||||
document.body.appendChild(sandbox)
|
||||
sandbox.contentWindow.parsers = {}
|
||||
sandbox.contentWindow.__protect = loopProtect
|
||||
|
||||
function evaljs(code) {
|
||||
|
||||
sandbox.contentWindow.parsers[code] = sandbox.contentWindow.Function(loopProtect(code))
|
||||
|
||||
return (context)=>{
|
||||
|
||||
for (var k in context) {
|
||||
sandbox.contentWindow[k] = context[k]
|
||||
}
|
||||
|
||||
return sandbox.contentWindow.parsers[code]()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return evaljs
|
||||
|
||||
})()
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue