diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bafaea4..bf1c499a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index 165f20f6..dd42afe6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/client/app/widgets/common/widget.js b/src/client/app/widgets/common/widget.js index ba034c5d..59b5aac3 100644 --- a/src/client/app/widgets/common/widget.js +++ b/src/client/app/widgets/common/widget.js @@ -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]) diff --git a/src/client/app/widgets/utils.js b/src/client/app/widgets/utils.js index 990d541b..97dde662 100644 --- a/src/client/app/widgets/utils.js +++ b/src/client/app/widgets/utils.js @@ -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 + })() }