missing file
This commit is contained in:
parent
e712e1b144
commit
5c08c7167e
1 changed files with 126 additions and 0 deletions
126
src/client/widgets/scripts/script-widget.js
Normal file
126
src/client/widgets/scripts/script-widget.js
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
var Widget = require('../common/widget'),
|
||||
keyboardJS = require('keyboardjs/dist/keyboard.min.js'),
|
||||
Script = require('./script')
|
||||
|
||||
class ScriptWidget extends Widget {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Sscripting widget utility with keyboard bindings'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
var defaults = super.defaults()
|
||||
delete defaults.widget.visible
|
||||
delete defaults.widget.interaction
|
||||
delete defaults.geometry
|
||||
delete defaults.style
|
||||
defaults.scripting = {
|
||||
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>.']},
|
||||
onValue: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget\'s value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
|
||||
onKeyboard: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives a keyboard event if `keyBinding` is set). See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.'},
|
||||
|
||||
_separator: 'Keyboard events',
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: null})
|
||||
|
||||
this.noValueState = true
|
||||
|
||||
|
||||
if (this.getProp('onKeyboard') && this.getProp('keyBinding')) {
|
||||
|
||||
this.keyCb = this.keyboardCallback.bind(this)
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.bind(this.getProp('keyBinding'), this.keyCb, this.keyCb)
|
||||
|
||||
})
|
||||
|
||||
this.scripts.onKeyboard = new Script({
|
||||
widget: this,
|
||||
property: 'onKeyboard',
|
||||
code: this.getProp('onKeyboard'),
|
||||
context: {
|
||||
type: '',
|
||||
key: '',
|
||||
code: 0,
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
meta: false,
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
this.value = v
|
||||
|
||||
if (options.sync) this.changed(options)
|
||||
|
||||
}
|
||||
|
||||
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.scripts.onKeyboard.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)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
super.onRemove()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = ScriptWidget
|
||||
Loading…
Add table
Add a link
Reference in a new issue