devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
91 lines (89 loc) • 3.81 kB
JavaScript
/**
* DevExtreme (cjs/__internal/events/core/keyboard_processor.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _events_engine = _interopRequireDefault(require("../../../common/core/events/core/events_engine"));
var _index = require("../../../common/core/events/utils/index");
var _renderer = _interopRequireDefault(require("../../../core/renderer"));
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const COMPOSITION_START_EVENT = "compositionstart";
const COMPOSITION_END_EVENT = "compositionend";
const KEYDOWN_EVENT = "keydown";
const NAMESPACE = "KeyboardProcessor";
const KEYDOWN_NAMESPACED_EVENT = (0, _index.addNamespace)("keydown", NAMESPACE);
const COMPOSITION_START_NAMESPACED_EVENT = (0, _index.addNamespace)("compositionstart", NAMESPACE);
const COMPOSITION_END_NAMESPACED_EVENT = (0, _index.addNamespace)("compositionend", NAMESPACE);
const createKeyDownOptions = e => ({
keyName: (0, _index.normalizeKeyName)(e),
key: e.key,
code: e.code,
ctrl: e.ctrlKey,
location: e.location,
metaKey: e.metaKey,
shift: e.shiftKey,
alt: e.altKey,
which: e.which,
originalEvent: e
});
class KeyboardProcessor {
constructor(options) {
const config = options ?? {};
if (config.element) {
this._element = (0, _renderer.default)(config.element)
}
if (config.focusTarget) {
this._focusTarget = config.focusTarget
}
this._handler = config.handler;
if (this._element) {
this._processFunction = e => {
const focusTargets = (0, _renderer.default)(this._focusTarget).toArray();
const isNotFocusTarget = this._focusTarget && this._focusTarget !== e.target && !focusTargets.includes(e.target);
const shouldSkipProcessing = this._isComposingJustFinished && 229 === e.which || this._isComposing || isNotFocusTarget;
this._isComposingJustFinished = false;
if (!shouldSkipProcessing) {
this.process(e)
}
};
this._toggleProcessingWithContext = this.toggleProcessing.bind(this);
_events_engine.default.on(this._element, KEYDOWN_NAMESPACED_EVENT, this._processFunction);
_events_engine.default.on(this._element, COMPOSITION_START_NAMESPACED_EVENT, this._toggleProcessingWithContext);
_events_engine.default.on(this._element, COMPOSITION_END_NAMESPACED_EVENT, this._toggleProcessingWithContext)
}
}
dispose() {
if (this._element) {
_events_engine.default.off(this._element, KEYDOWN_NAMESPACED_EVENT, this._processFunction);
_events_engine.default.off(this._element, COMPOSITION_START_NAMESPACED_EVENT, this._toggleProcessingWithContext);
_events_engine.default.off(this._element, COMPOSITION_END_NAMESPACED_EVENT, this._toggleProcessingWithContext)
}
this._element = void 0;
this._handler = void 0
}
process(e) {
var _this$_handler;
null === (_this$_handler = this._handler) || void 0 === _this$_handler || _this$_handler.call(this, createKeyDownOptions(e))
}
toggleProcessing(_ref) {
let {
type: type
} = _ref;
this._isComposing = "compositionstart" === type;
this._isComposingJustFinished = !this._isComposing
}
}
KeyboardProcessor.createKeyDownOptions = createKeyDownOptions;
var _default = exports.default = KeyboardProcessor;