scripting: refactor script to reduce heap (builtin script are not widgets anymore)
script widget: add onKeyboard and remove onEvent
This commit is contained in:
parent
233c9c8920
commit
df6f9e110a
8 changed files with 129 additions and 256 deletions
|
|
@ -69,31 +69,23 @@ Note: the [canvas](../canvas/) widgets exposes more informations in its `onTouch
|
|||
|
||||
See [canvas](../canvas/).
|
||||
|
||||
### `onEvent`
|
||||
### `onKeyboard`
|
||||
|
||||
*Script widget only*
|
||||
|
||||
This script is executed when the event specified in the `event` property occurs.
|
||||
|
||||
=== "Event: keyboard"
|
||||
|
||||
When set on this mode, the script is called when the key(s) defined in the widget's `keyBinding` property are pressed.
|
||||
This script is executed when the key(s) defined in the widget's `keyBinding` property are pressed.
|
||||
|
||||
|
||||
The following variables are available in this context:
|
||||
The following variables are available in this context:
|
||||
|
||||
|
||||
- `type` (`string`): `keydown` or `keyup`
|
||||
- `key` (`string`): key name,
|
||||
- `code` (`number`): key code
|
||||
- `ctrl` (`boolean)`: `true` if ctrl key is pressed
|
||||
- `shift` (`boolean`): `true` if shift key is pressed
|
||||
- `alt` (`boolean`): `true` if alt key is pressed
|
||||
- `meta` (`boolean`): `true` if meta key is pressed
|
||||
|
||||
=== "Event: value"
|
||||
|
||||
See `onValue`.
|
||||
- `type` (`string`): `keydown` or `keyup`
|
||||
- `key` (`string`): key name,
|
||||
- `code` (`number`): key code
|
||||
- `ctrl` (`boolean)`: `true` if ctrl key is pressed
|
||||
- `shift` (`boolean`): `true` if shift key is pressed
|
||||
- `alt` (`boolean`): `true` if alt key is pressed
|
||||
- `meta` (`boolean`): `true` if meta key is pressed
|
||||
|
||||
|
||||
## Available variables
|
||||
|
|
|
|||
|
|
@ -296,9 +296,11 @@ var converters = [
|
|||
if (data.type === 'script') {
|
||||
if (data.event === 'once') {
|
||||
data.onCreate = data.script
|
||||
data.event = 'value'
|
||||
} else if (data.event === 'keyboard') {
|
||||
data.onKeyboard = data.script
|
||||
} else if (data.event === 'value') {
|
||||
data.onValue = data.script
|
||||
}
|
||||
else data.onEvent = data.script
|
||||
} else {
|
||||
data.onValue = data.script
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ var UiSidePanel = require('./ui-sidepanel'),
|
|||
raw = require('nanohtml/raw'),
|
||||
locales = require('../locales'),
|
||||
{icon} = require('./utils'),
|
||||
Script = require('../widgets/scripts/script'),
|
||||
ScriptWidget = require('../widgets/scripts/script-widget'),
|
||||
widgetManager = require('../managers/widgets')
|
||||
|
||||
class UiConsole extends UiSidePanel {
|
||||
|
|
@ -57,7 +57,7 @@ class UiConsole extends UiSidePanel {
|
|||
console._clear()
|
||||
}
|
||||
|
||||
this.script = new Script({props:{
|
||||
this.script = new ScriptWidget({props:{
|
||||
id: 'CONSOLE',
|
||||
// detached context++ reading global navigator == crash electron
|
||||
script: 'var navigator; return eval(value)',
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ var EventEmitter = require('../../events/event-emitter'),
|
|||
morph = require('nanomorph'),
|
||||
sanitizeHtml = require('sanitize-html'),
|
||||
updateWidget = ()=>{},
|
||||
Script, uiConsole, uiTree, uiDragResize, sessionManager
|
||||
Script = require('../scripts/script'),
|
||||
uiConsole, uiTree, uiDragResize, sessionManager
|
||||
|
||||
|
||||
var oscReceiverState = {}
|
||||
|
|
@ -197,6 +198,8 @@ class Widget extends EventEmitter {
|
|||
|
||||
this.parsers = {}
|
||||
this.parsersLocalScope = options.locals || {}
|
||||
this.timeouts = {}
|
||||
this.intervals = {}
|
||||
this.variables = options.variables || {}
|
||||
this.fragments = {}
|
||||
|
||||
|
|
@ -219,55 +222,65 @@ class Widget extends EventEmitter {
|
|||
this.decimals = Math.min(20, Math.max(this.getProp('decimals'), 0))
|
||||
}
|
||||
|
||||
// scripting
|
||||
this.scripts = {}
|
||||
Script = Script || require('../scripts/script')
|
||||
if (!(this instanceof Script)) {
|
||||
if (this.getProp('onCreate')) {
|
||||
this.scripts.onCreate = new Script({props: {
|
||||
id: this.getProp('id') + '.onCreate',
|
||||
onEvent: this.getProp('onCreate')
|
||||
}, builtIn: true, parent: this, context: {}})
|
||||
}
|
||||
if (this.getProp('onValue')) {
|
||||
this.scripts.onValue = new Script({props:{
|
||||
id: this.getProp('id') + '.onValue',
|
||||
onEvent: this.getProp('onValue'),
|
||||
event: 'value'
|
||||
}, builtIn: true, parent: this})
|
||||
this.on('change', (e)=>{
|
||||
if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) {
|
||||
this.scripts.onValue.setValue(e.options.widget ? e.options.widget.value : this.value, {...e.options, id: e.options.id || e.id})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (this.getProp('onTouch')) {
|
||||
this.scripts.onTouch = new Script({props:{
|
||||
id: this.getProp('id') + '.onTouch',
|
||||
onEvent: this.getProp('onTouch'),
|
||||
}, builtIn: true, parent: this, context: {
|
||||
event: {},
|
||||
value: 0
|
||||
}})
|
||||
this.on('touch', (e)=>{
|
||||
var multi = Array.isArray(e.touch),
|
||||
state = multi ? e.touch[1] : e.touch
|
||||
this.scripts.onTouch.run({
|
||||
event: {type: state ? 'start' : 'stop', handle: multi ? e.touch[0] : undefined},
|
||||
value: this.value
|
||||
}, {sync: true, send: true})
|
||||
})
|
||||
}
|
||||
|
||||
// legacy touch state
|
||||
if (String(this.getProp('onValue')).includes('touch') && !this.getProp('onTouch')) {
|
||||
this.on('touch', (e)=>{
|
||||
this.scripts.onValue.setValue(this.value, {id: this.getProp('id'), touch: e.touch, sync: true, send: true})
|
||||
})
|
||||
}
|
||||
|
||||
if (this.getProp('onCreate')) {
|
||||
this.scripts.onCreate = new Script({
|
||||
widget: this,
|
||||
property: 'onCreate',
|
||||
code: this.getProp('onCreate'),
|
||||
context: {}
|
||||
})
|
||||
}
|
||||
|
||||
if (this.getProp('onValue')) {
|
||||
this.scripts.onValue = new Script({
|
||||
widget: this,
|
||||
property: 'onValue',
|
||||
code: this.getProp('onValue'),
|
||||
context: {value: 0, id: '', touch: 0}
|
||||
})
|
||||
this.on('change', (e)=>{
|
||||
if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) {
|
||||
this.scripts.onValue.run({
|
||||
value: e.options.widget ? e.options.widget.value : this.value,
|
||||
id: e.options.id || e.id,
|
||||
touch: undefined
|
||||
}, e.options)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (this.getProp('onTouch') && this.getProp('type') !== 'canvas') {
|
||||
this.scripts.onTouch = new Script({
|
||||
widget: this,
|
||||
property: 'onTouch',
|
||||
code: this.getProp('onTouch'),
|
||||
context: {event: {}, value: 0}
|
||||
})
|
||||
this.on('touch', (e)=>{
|
||||
var multi = Array.isArray(e.touch),
|
||||
state = multi ? e.touch[1] : e.touch
|
||||
this.scripts.onTouch.run({
|
||||
event: {type: state ? 'start' : 'stop', handle: multi ? e.touch[0] : undefined},
|
||||
value: this.value
|
||||
}, {sync: true, send: true})
|
||||
})
|
||||
}
|
||||
|
||||
// legacy touch state in onValue script
|
||||
if (String(this.getProp('onValue')).includes('touch') && !this.getProp('onTouch')) {
|
||||
this.on('touch', (e)=>{
|
||||
this.scripts.onValue.run({
|
||||
value: this.value,
|
||||
id: this.getProp('id'),
|
||||
touch: e.touch
|
||||
}, {sync: true, send: true})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
this.style = null
|
||||
this.container = html`
|
||||
<div class="widget ${options.props.type}-container" id="${this.hash}" data-widget="${this.hash}"></div>
|
||||
|
|
@ -323,7 +336,7 @@ class Widget extends EventEmitter {
|
|||
index: index
|
||||
})
|
||||
|
||||
if (this.scripts.onCreate) this.scripts.onCreate.run({})
|
||||
if (this.scripts.onCreate) this.scripts.onCreate.run({}, {sync: true, send: true})
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -747,12 +760,21 @@ class Widget extends EventEmitter {
|
|||
if (!this.fragments[file]) this.fragments[file] = []
|
||||
if (!this.fragments[file].includes(propName)) this.fragments[file].push(propName)
|
||||
}
|
||||
var r
|
||||
if (sessionManager.getFragment(file)) {
|
||||
return sessionManager.getFragment(file)
|
||||
r = sessionManager.getFragment(file)
|
||||
} else {
|
||||
sessionManager.loadFragment(file, true)
|
||||
return ''
|
||||
r = ''
|
||||
}
|
||||
|
||||
var varname = 'VAR_' + varnumber
|
||||
varnumber--
|
||||
|
||||
variables[varname] = r
|
||||
jsScope[varname] = r
|
||||
|
||||
return varname
|
||||
})
|
||||
|
||||
propValue = balancedReplace('OSC', '{', '}', propValue, (args)=>{
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ module.exports.widgets = {
|
|||
range: require('./sliders/range'),
|
||||
|
||||
// scripts
|
||||
script: require('./scripts/script'),
|
||||
script: require('./scripts/script-widget'),
|
||||
variable: require('./scripts/variable'),
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,31 +50,33 @@ class CanvasWidget extends Canvas {
|
|||
this.value = this.valueLength > 1 ? Array(this.valueLength).fill(0) : 0
|
||||
|
||||
if (this.getProp('onDraw')) {
|
||||
this.scripts.onDraw = new Script({props:{
|
||||
id: this.getProp('id') + '.onDraw',
|
||||
onEvent: this.getProp('onDraw'),
|
||||
}, builtIn: true, parent: this, context: {
|
||||
value: 0,
|
||||
width: 100,
|
||||
height: 100,
|
||||
ctx: {},
|
||||
cssVars: {}
|
||||
}})
|
||||
this.scripts.onDraw._not_editable = true
|
||||
this.scripts.onDraw = new Script({
|
||||
widget: this,
|
||||
property: 'onDraw',
|
||||
code: this.getProp('onDraw'),
|
||||
context: {
|
||||
value: 0,
|
||||
width: 100,
|
||||
height: 100,
|
||||
ctx: {},
|
||||
cssVars: {}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (this.getProp('onTouch')) {
|
||||
|
||||
this.scripts.onTouch = new Script({props:{
|
||||
id: this.getProp('id') + '.onTouch',
|
||||
onEvent: this.getProp('onTouch'),
|
||||
}, builtIn: true, parent: this, context: {
|
||||
value: 0,
|
||||
width: 100,
|
||||
height: 100,
|
||||
event: {}
|
||||
}})
|
||||
this.scripts.onTouch._not_editable = true
|
||||
this.scripts.onTouch = new Script({
|
||||
widget: this,
|
||||
property: 'onTouch',
|
||||
code: this.getProp('onTouch'),
|
||||
context: {
|
||||
value: 0,
|
||||
width: 100,
|
||||
height: 100,
|
||||
event: {}
|
||||
}
|
||||
})
|
||||
|
||||
var touchCb = (e, type)=>{
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ class ScriptVm extends Vm {
|
|||
resolveId(id) {
|
||||
|
||||
var widget = this.getWidget()
|
||||
if (widget.builtIn) widget = widget.parent
|
||||
|
||||
if (id === 'this') return [widget]
|
||||
else if (id === 'parent' && widget.parent !== widgetManager) return [widget.parent]
|
||||
|
|
@ -82,7 +81,6 @@ class ScriptVm extends Vm {
|
|||
var widgets
|
||||
if (id.includes('*')) {
|
||||
var widget = this.getWidget()
|
||||
if (widget.builtIn) widget = widget.parent
|
||||
widgets = this.resolveId(
|
||||
Object.keys(widgetManager.idRoute).filter(key => key.match(new RegExp('^' + id.replace(/\*/g, '.*') + '$')))
|
||||
).filter(w => w !== widget)
|
||||
|
|
@ -106,7 +104,6 @@ class ScriptVm extends Vm {
|
|||
var widgets
|
||||
if (id.includes('*')) {
|
||||
var widget = this.getWidget()
|
||||
if (widget.builtIn) widget = widget.parent
|
||||
widgets = this.resolveId(
|
||||
Object.keys(widgetManager.idRoute).filter(key => key.match(new RegExp('^' + id.replace(/\*/g, '.*') + '$')))
|
||||
).filter(w => w !== widget)
|
||||
|
|
@ -158,7 +155,6 @@ class ScriptVm extends Vm {
|
|||
if (target) overrides.target = Array.isArray(target) ? target : [target]
|
||||
|
||||
var widget = this.getWidget()
|
||||
if (widget.builtIn) widget = widget.parent
|
||||
|
||||
widget.sendValue(overrides, {force: true})
|
||||
|
||||
|
|
@ -219,8 +215,6 @@ class ScriptVm extends Vm {
|
|||
var widgets = this.resolveId(id),
|
||||
widget = this.getWidget()
|
||||
|
||||
if (widget.builtIn) widget = widget.parent
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
widgets[i].updateProps(Array.isArray(prop) ? prop : [prop], widget)
|
||||
|
|
|
|||
|
|
@ -1,108 +1,23 @@
|
|||
var Widget = require('../common/widget'),
|
||||
keyboardJS = require('keyboardjs/dist/keyboard.min.js'),
|
||||
ScriptVm = require('./script-vm'),
|
||||
var ScriptVm = require('./script-vm'),
|
||||
scriptVm = new ScriptVm()
|
||||
|
||||
|
||||
class Script extends Widget {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Evaluates a script each time it receives a value.'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
var defaults = super.defaults()
|
||||
delete defaults.widget.visible
|
||||
delete defaults.widget.interaction
|
||||
delete defaults.geometry
|
||||
delete defaults.style
|
||||
defaults.scripting = {
|
||||
event: {type: 'string', value: 'value', choices: ['value', 'keyboard'], help: 'Define which events trigger the script\'s execution.'},
|
||||
onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
|
||||
onEvent: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives the defined event. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.'},
|
||||
|
||||
_separator: 'event: keyboard',
|
||||
|
||||
keyBinding: {type: 'string|array', value: '', help: [
|
||||
'Key combo `string` or `array` of strings (see <a href="https://github.com/RobertWHurst/KeyboardJS">KeyboardJS</a> documentation).',
|
||||
'If the editor is enabled, some keys / combos will not work.'
|
||||
]},
|
||||
keyRepeat: {type: 'boolean', value: true, help: 'Set to `false` to prevent keydown repeats when holding the key combo pressed'},
|
||||
keyType: {type: 'string', value: 'keydown', choices: ['keydown', 'keyup', 'both'], help: 'Determines which key event trigger the script\'s execution'},
|
||||
|
||||
}
|
||||
return defaults
|
||||
|
||||
}
|
||||
class Script {
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: null})
|
||||
|
||||
this.noValueState = true
|
||||
|
||||
this.builtIn = options.builtIn
|
||||
if (this.builtIn) this._not_editable = true
|
||||
|
||||
this.scriptLock = false
|
||||
|
||||
this.lock = false
|
||||
this.timeouts = {}
|
||||
this.intervals = {}
|
||||
this.widget = options.widget
|
||||
this.property = options.property
|
||||
|
||||
if (!this.getProp('onEvent')) {
|
||||
if (!options.code) {
|
||||
|
||||
this.script = ()=>{}
|
||||
|
||||
} else if (this.getProp('event') === 'value') {
|
||||
} else {
|
||||
|
||||
this.compile({
|
||||
id: '',
|
||||
value: 0,
|
||||
touch: undefined
|
||||
})
|
||||
|
||||
} else if (this.getProp('event') === 'keyboard' && this.getProp('keyBinding')) {
|
||||
|
||||
this.keyCb = this.keyboardCallback.bind(this)
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.bind(this.getProp('keyBinding'), this.keyCb, this.keyCb)
|
||||
|
||||
})
|
||||
|
||||
this.compile({
|
||||
type: '',
|
||||
key: '',
|
||||
code: 0,
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
meta: false,
|
||||
})
|
||||
|
||||
} else if (options.context) {
|
||||
|
||||
// custom (internal/builtin) event
|
||||
this.compile(options.context)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
compile(context) {
|
||||
|
||||
try {
|
||||
|
||||
this.script = scriptVm.compile(this.getProp('onEvent'), context)
|
||||
|
||||
} catch(err) {
|
||||
|
||||
this.errorProp(this.builtIn ? null : 'onEvent', 'javascript', err)
|
||||
this.script = ()=>{}
|
||||
this.script = scriptVm.compile(options.code, options.context)
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -110,19 +25,19 @@ class Script extends Widget {
|
|||
|
||||
run(context, options) {
|
||||
|
||||
if (this.scriptLock) return
|
||||
if (this.lock) return
|
||||
|
||||
scriptVm.setValueOptions(options)
|
||||
scriptVm.setWidget(this)
|
||||
scriptVm.setWidget(this.widget)
|
||||
|
||||
this.scriptLock = true
|
||||
this.lock = true
|
||||
var returnValue
|
||||
try {
|
||||
returnValue = this.script(context, this.builtIn ? this.parent.parsersLocalScope : this.parsersLocalScope)
|
||||
returnValue = this.script(context, this.widget.parsersLocalScope)
|
||||
} catch(err) {
|
||||
this.errorProp(this.builtIn ? null : 'onEvent', 'javascript', err)
|
||||
this.widget.errorProp(this.property, 'javascript', err)
|
||||
}
|
||||
this.scriptLock = false
|
||||
this.lock = false
|
||||
|
||||
scriptVm.setWidget()
|
||||
scriptVm.setValueOptions()
|
||||
|
|
@ -131,70 +46,16 @@ class Script extends Widget {
|
|||
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
if (this.getProp('event') === 'value' && options.script !== false) {
|
||||
|
||||
var returnValue = this.run({
|
||||
value: v,
|
||||
id: options.id,
|
||||
touch: options.touch
|
||||
}, options)
|
||||
|
||||
return returnValue
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
keyboardCallback(e) {
|
||||
|
||||
if (e.catchedByEditor) return
|
||||
|
||||
if (e.target && (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT')) return
|
||||
|
||||
if (this.getProp('keyType') !== 'both' && e.type !== this.getProp('keyType')) return
|
||||
|
||||
if (e.type === 'keydown' && !this.getProp('keyRepeat')) e.preventRepeat()
|
||||
|
||||
e.preventDefault()
|
||||
|
||||
this.run({
|
||||
type: e.type,
|
||||
key: e.key,
|
||||
code: e.code,
|
||||
ctrl: e.ctrlKey,
|
||||
shift: e.shiftKey,
|
||||
alt: e.altKey,
|
||||
meta: e.metaKey
|
||||
}, {send: true, sync: true})
|
||||
|
||||
|
||||
}
|
||||
|
||||
onRemove() {
|
||||
|
||||
|
||||
if (this.getProp('event') === 'keyboard' && this.getProp('keyBinding')) {
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.unbind(this.getProp('keyBinding'), this.keyCb, this.keyCb)
|
||||
|
||||
})
|
||||
|
||||
for (let k in this.widget.timeouts) {
|
||||
clearTimeout(this.widget.timeouts[k])
|
||||
}
|
||||
|
||||
for (let k in this.timeouts) {
|
||||
clearTimeout(this.timeouts[k])
|
||||
for (let k in this.widget.intervals) {
|
||||
clearInterval(this.widget.intervals[k])
|
||||
}
|
||||
|
||||
for (let k in this.intervals) {
|
||||
clearInterval(this.intervals[k])
|
||||
}
|
||||
|
||||
super.onRemove()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue