livebook/assets/js/keyboard_control/index.js
Jonatan Kłosko 264d6c3ff2
Add support for controls output type (#710)
* Add support for controls output type

* Split controls into individual widgets

* Adjust ids

* Improve widget and controls garbage collection

* Allow arbitrary functions as object release hook

* Add type to button and input events

* Add keyboard status event

* Change release hooks into monitor messages

* Rename pointer to reference and return an error on bad monitor
2021-12-02 16:45:00 +01:00

91 lines
2.2 KiB
JavaScript

import { getAttributeOrThrow, parseBoolean } from "../lib/attribute";
import { cancelEvent } from "../lib/utils";
/**
* A hook for ControlComponent to handle user keyboard interactions.
*
* Configuration:
*
* * `data-keydown-enabled` - whether keydown events should be intercepted
*
* * `data-keyup-enabled` - whether keyup events should be intercepted
*
* * `data-target` - the target to send live events to
*/
const KeyboardControl = {
mounted() {
this.props = getProps(this);
this.handleDocumentKeyDown = (event) => {
handleDocumentKeyDown(this, event);
};
// We intentionally register on window rather than document,
// to intercept clicks as early on as possible, even before
// the session shortcuts
window.addEventListener("keydown", this.handleDocumentKeyDown, true);
this.handleDocumentKeyUp = (event) => {
handleDocumentKeyUp(this, event);
};
window.addEventListener("keyup", this.handleDocumentKeyUp, true);
},
updated() {
this.props = getProps(this);
},
destroyed() {
window.removeEventListener("keydown", this.handleDocumentKeyDown, true);
window.removeEventListener("keyup", this.handleDocumentKeyUp, true);
},
};
function getProps(hook) {
return {
isKeydownEnabled: getAttributeOrThrow(
hook.el,
"data-keydown-enabled",
parseBoolean
),
isKeyupEnabled: getAttributeOrThrow(
hook.el,
"data-keyup-enabled",
parseBoolean
),
target: getAttributeOrThrow(hook.el, "data-target"),
};
}
function handleDocumentKeyDown(hook, event) {
if (keyboardEnabled(hook)) {
cancelEvent(event);
}
if (hook.props.isKeydownEnabled) {
if (event.repeat) {
return;
}
const key = event.key;
hook.pushEventTo(hook.props.target, "keydown", { key });
}
}
function handleDocumentKeyUp(hook, event) {
if (keyboardEnabled(hook)) {
cancelEvent(event);
}
if (hook.props.isKeyupEnabled) {
const key = event.key;
hook.pushEventTo(hook.props.target, "keyup", { key });
}
}
function keyboardEnabled(hook) {
return hook.props.isKeydownEnabled || hook.props.isKeyupEnabled;
}
export default KeyboardControl;