keys: breaking changes for the greater good

This commit is contained in:
jean-emmanuel 2018-08-07 20:35:12 +02:00
parent 8d1794de76
commit 5ef5bcc673
3 changed files with 52 additions and 66 deletions

View file

@ -5,6 +5,8 @@
- bug fixes
- `linkId` synchronizations now correctly trigger property updates
- widgets with multiple `linkId` are now properly handled
- widgets
- `keys` (**breaking changes**): formulas in `keydown` and `keyup` must be explicitely written using the `#{}` syntax; variable `value` removed from these formulas context (use `@{this}` instead); removed `widgetId` property, the value emitted by the widgets on keypress events can be linked with other widgets via the `linkId` property or the property inheritance syntax;
## 0.35.0

View file

@ -41,11 +41,6 @@
}
```
### `widgetId`
- type: `object`
- default: `empty`
- usage: a widget's `id` whose value will be linked to the Keys
### `binding`
- type: `string|array`
- default: `''`
@ -54,8 +49,7 @@
### `keydown`
- type: `string`
- default: `''`
- usage: [MathJS](http://mathjs.org/docs/expressions/syntax.html) expression whose output will be sent to the linked widget. This will be evaluated when the key combo is pressed. Available variables are ;
- `value`: widget's value
- usage: this property is evaluated each time the key combo is pressed. [Formulas](../extras/advanced-property-syntax/#formulas) are given extras variables in this context:
- `key`: pressed key name (usefull for handling multiple keys with a single `keys` widget)
- `ctrl`: control key state
- `alt`: alt key state

View file

@ -4,12 +4,12 @@ var Widget = require('../common/widget'),
{iconify} = require('../../ui/utils'),
keyboardJS = require('keyboardjs')
module.exports = class Keys extends Widget {
class Keys extends Widget {
static defaults() {
return {
type:'led',
type:'keys',
id:'auto',
linkId:'',
@ -28,7 +28,6 @@ module.exports = class Keys extends Widget {
_keys:'keys',
widgetId:'',
binding:'',
keydown:'',
keyup:'',
@ -48,115 +47,106 @@ module.exports = class Keys extends Widget {
super({...options, html: html})
this.keydownString = String(this.getProp('keydown'))
this.keyupString = String(this.getProp('keyup'))
if (this.getProp('binding') && (this.keydownString || this.keyupString)) {
if (this.getProp('binding')) {
this.widget.appendChild(DOM.create(`<span>${this.getProp('binding')}</span>`))
this.keydownHandler = this.keydownString ? this.keydown.bind(this) : this.showKeydown.bind(this)
this.keyupHandler = this.keyupString ? this.keyup.bind(this) : this.showKeyup.bind(this)
this.keyDownHandler = this.keyDown.bind(this)
this.keyUpHandler = this.keyUp.bind(this)
this.keydownMath = math.compile(this.keydownString)
this.keyupMath = math.compile(this.keyupString)
keyboardJS.bind(this.getProp('binding'), this.keydownHandler, this.keyupHandler)
keyboardJS.bind(this.getProp('binding'), this.keyDownHandler, this.keyUpHandler)
}
this.linkedWidget = null
}
onRemove() {
super.onRemove()
if (this.getProp('binding') && (this.getProp('keydown') || this.getProp('keyup'))) {
if (this.getProp('binding')) {
keyboardJS.unbind(this.getProp('binding'), this.keydownHandler, this.keyupHandler)
keyboardJS.unbind(this.getProp('binding'), this.keyDownHandler, this.keyUpHandler)
}
}
keydown(e) {
keyDown(e) {
if (e.target.classList.contains('input')) return
if (!this.getProp('repeat') && e) e.preventRepeat()
var widget = this.getLinkedWidget()
if (this.getProp('keydown') !== '') {
if (widget) {
var v = this.keydownMath.eval({
value: widget.getValue(),
var context = {
key: e.key,
ctrl: e.ctrlKey,
shift: e.shiftKey,
alt: e.altKey,
meta: e.metaKey
}).valueOf()
}
if (Array.isArray(v) && v.length == 1) v = v[0]
widget.setValue(v, {send:true, sync:true, fromExternal:false})
this.setValue(this.resolveProp('keydown', undefined, false, false, false, context), {sync: true, send: true})
}
this.showKeydown()
}
keyUp(e) {
if (e.target.classList.contains('input')) return
if (this.getProp('keyup') !== '') {
var context = {
key: e.key,
ctrl: e.ctrlKey,
shift: e.shiftKey,
alt: e.altKey,
meta: e.metaKey
}
this.setValue(this.resolveProp('keyup', undefined, false, false, false, context), {sync: true, send: true})
}
this.showKeyup()
}
showKeydown(){
this.widget.style.setProperty('--opacity', 1)
}
keyup(e) {
if (e.target.classList.contains('input')) return
var widget = this.getLinkedWidget()
if (widget) {
var v = this.keyupMath.eval({
value: widget.getValue(),
key: e.key,
ctrl: e.ctrlKey,
shift: e.shiftKey,
alt: e.altKey,
meta: e.metaKey
}).valueOf()
if (Array.isArray(v) && v.length == 1) v = v[0]
widget.setValue(v, {send:true, sync:true, fromExternal:false})
}
this.showKeyup()
}
showKeyup(){
this.widget.style.setProperty('--opacity', 0.75)
}
getLinkedWidget() {
setValue(v, options = {}) {
if (this.getProp('widgetId')) {
this.value = v
return widgetManager.getWidgetById(this.getProp('widgetId')).pop() || null
}
// if (options.send) this.sendValue()
if (options.sync) this.changed(options)
}
setValue() {}
}
Keys.dynamicProps = Keys.prototype.constructor.dynamicProps.concat(
'keydown',
'keyup',
'repeat'
)
module.exports = Keys