From 5ef5bcc673386aec5a3c4aba4919466c0e518bf9 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 7 Aug 2018 20:35:12 +0200 Subject: [PATCH] keys: breaking changes for the greater good --- CHANGELOG.md | 2 + resources/docs/widgets/inputs.md | 8 +- src/browser/js/app/widgets/inputs/keys.js | 108 ++++++++++------------ 3 files changed, 52 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 800ecaf8..6e1d9514 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/resources/docs/widgets/inputs.md b/resources/docs/widgets/inputs.md index 854db9ef..99565bfd 100644 --- a/resources/docs/widgets/inputs.md +++ b/resources/docs/widgets/inputs.md @@ -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 diff --git a/src/browser/js/app/widgets/inputs/keys.js b/src/browser/js/app/widgets/inputs/keys.js index 3de60acc..d998ee43 100644 --- a/src/browser/js/app/widgets/inputs/keys.js +++ b/src/browser/js/app/widgets/inputs/keys.js @@ -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(`${this.getProp('binding')}`)) - 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