UNPKG

react-application-core

Version:

A react-based application core for the business applications.

148 lines 5.86 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.KeyInterceptor = void 0; var ts_smart_logger_1 = require("ts-smart-logger"); var generic_component_1 = require("../base/generic.component"); var util_1 = require("../../util"); var definition_1 = require("../../definition"); var KeyInterceptor = /** @class */ (function (_super) { __extends(KeyInterceptor, _super); /** * @stable [21.11.2020] * @param originalProps */ function KeyInterceptor(originalProps) { var _this = _super.call(this, originalProps) || this; _this.buffer = ''; _this.onEventCapture = _this.onEventCapture.bind(_this); _this.onDefaultEventCapture = _this.onDefaultEventCapture.bind(_this); _this.delayedTask = new util_1.DelayedTask(_this.onCheckBuffer.bind(_this), originalProps.delayTimeout); return _this; } /** * @stable [18.05.2020] */ KeyInterceptor.prototype.componentDidMount = function () { this.defaultUnSubscriber = this.eventManager.subscribe(this.environment.document, definition_1.EventsEnum.KEY_DOWN, this.onDefaultEventCapture); this.unSubscriber = this.eventManager.subscribe(this.environment.document, definition_1.EventsEnum.KEY_PRESS, this.onEventCapture); }; /** * @stable [18.05.2020] */ KeyInterceptor.prototype.componentWillUnmount = function () { this.delayedTask.stop(); if (util_1.TypeUtils.isFn(this.defaultUnSubscriber)) { this.defaultUnSubscriber(); this.defaultUnSubscriber = null; } if (util_1.TypeUtils.isFn(this.unSubscriber)) { this.unSubscriber(); this.unSubscriber = null; } }; /** * @stable [21.11.2020] */ KeyInterceptor.prototype.render = function () { return null; }; /** * @stable [22.11.2020] * @param e * @private */ KeyInterceptor.prototype.onDefaultEventCapture = function (e) { if (this.domAccessor.isAlreadyFocused()) { // Don't interfere with normal input typings return; } // https://support.google.com/chrome/answer/157179?co=GENIE.Platform%3DDesktop&hl=en // Open a new tab, and jump to it Ctrl + t // Open the Downloads page in a new tab Ctrl + j // Jump to a specific tab Ctrl + 1 through Ctrl + 8 // Jump to the rightmost tab Ctrl + 9 if (e.ctrlKey && /[jmt1-9]/i.test(e.key)) { this.domAccessor.cancelEvent(e); // Disable keyboard shortcuts of Chrome, ... // Simulate 13 key event this.domAccessor.dispatchEvent({ event: new KeyboardEvent(definition_1.EventsEnum.KEY_PRESS, { keyCode: KeyInterceptor.ENTER_KEY_CODE }), element: this.environment.document, }); } }; /** * @stable [21.11.2020] * @param e * @private */ KeyInterceptor.prototype.onEventCapture = function (e) { if (this.domAccessor.isAlreadyFocused()) { // Don't interfere with normal input typings return; } this.domAccessor.cancelEvent(e); var keyCode = e.keyCode; if (KeyInterceptor.SPECIAL_KEY_CODES.includes(keyCode)) { return; } var ignoreEnterKey = this.originalProps.ignoreEnterKey; var char = e.key; if (KeyInterceptor.ENTER_KEY_CODES.includes(keyCode)) { if (ignoreEnterKey) { // TODO Remove this props? KeyInterceptor.logger.debug('[$KeyInterceptor][onEventCapture] Ignore enter key code and exit.'); return; } char = '\n'; } this.buffer = "" + this.buffer + char; this.delayedTask.start(); }; /** * @stable [21.11.2020] * @private */ KeyInterceptor.prototype.onCheckBuffer = function () { var _a = this.originalProps, onSelect = _a.onSelect, robotDetectionMinSymbolsCount = _a.robotDetectionMinSymbolsCount; if (this.buffer.length >= robotDetectionMinSymbolsCount) { if (util_1.TypeUtils.isFn(onSelect)) { var finalValue = this.buffer.trim(); // Normalize a buffer in some specific case KeyInterceptor.logger.debug('[$KeyInterceptor][onCheckBuffer] Final value:', finalValue); onSelect(finalValue); } } this.buffer = ''; }; KeyInterceptor.defaultProps = { delayTimeout: 300, robotDetectionMinSymbolsCount: 3, }; KeyInterceptor.logger = ts_smart_logger_1.LoggerFactory.makeLogger('KeyInterceptor'); KeyInterceptor.ENTER_KEY_CODE = 13; KeyInterceptor.ENTER_KEY_CODES = [ 10, KeyInterceptor.ENTER_KEY_CODE ]; KeyInterceptor.SPECIAL_KEY_CODES = [ 8, 9, 16, 17, 18 // alt ]; return KeyInterceptor; }(generic_component_1.GenericComponent)); exports.KeyInterceptor = KeyInterceptor; //# sourceMappingURL=key-interceptor.component.js.map