devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
83 lines (82 loc) • 3.41 kB
JavaScript
/**
* DevExtreme (esm/__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/
*/
import eventsEngine from "../../../common/core/events/core/events_engine";
import {
addNamespace,
normalizeKeyName
} from "../../../common/core/events/utils/index";
import $ from "../../../core/renderer";
const COMPOSITION_START_EVENT = "compositionstart";
const COMPOSITION_END_EVENT = "compositionend";
const KEYDOWN_EVENT = "keydown";
const NAMESPACE = "KeyboardProcessor";
const KEYDOWN_NAMESPACED_EVENT = addNamespace("keydown", NAMESPACE);
const COMPOSITION_START_NAMESPACED_EVENT = addNamespace("compositionstart", NAMESPACE);
const COMPOSITION_END_NAMESPACED_EVENT = addNamespace("compositionend", NAMESPACE);
const createKeyDownOptions = e => ({
keyName: 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 = $(config.element)
}
if (config.focusTarget) {
this._focusTarget = config.focusTarget
}
this._handler = config.handler;
if (this._element) {
this._processFunction = e => {
const focusTargets = $(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);
eventsEngine.on(this._element, KEYDOWN_NAMESPACED_EVENT, this._processFunction);
eventsEngine.on(this._element, COMPOSITION_START_NAMESPACED_EVENT, this._toggleProcessingWithContext);
eventsEngine.on(this._element, COMPOSITION_END_NAMESPACED_EVENT, this._toggleProcessingWithContext)
}
}
dispose() {
if (this._element) {
eventsEngine.off(this._element, KEYDOWN_NAMESPACED_EVENT, this._processFunction);
eventsEngine.off(this._element, COMPOSITION_START_NAMESPACED_EVENT, this._toggleProcessingWithContext);
eventsEngine.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;
export default KeyboardProcessor;