devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
91 lines (89 loc) • 3.76 kB
JavaScript
/**
* DevExtreme (cjs/__internal/events/core/m_keyboard_processor.js)
* Version: 24.2.6
* Build date: Mon Mar 17 2025
*
* Copyright (c) 2012 - 2025 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 _class = _interopRequireDefault(require("../../../core/class"));
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 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
});
const KeyboardProcessor = _class.default.inherit({
_keydown: (0, _index.addNamespace)("keydown", NAMESPACE),
_compositionStart: (0, _index.addNamespace)("compositionstart", NAMESPACE),
_compositionEnd: (0, _index.addNamespace)("compositionend", NAMESPACE),
ctor(options) {
options = options || {};
if (options.element) {
this._element = (0, _renderer.default)(options.element)
}
if (options.focusTarget) {
this._focusTarget = options.focusTarget
}
this._handler = options.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, this._keydown, this._processFunction);
_events_engine.default.on(this._element, this._compositionStart, this._toggleProcessingWithContext);
_events_engine.default.on(this._element, this._compositionEnd, this._toggleProcessingWithContext)
}
},
dispose() {
if (this._element) {
_events_engine.default.off(this._element, this._keydown, this._processFunction);
_events_engine.default.off(this._element, this._compositionStart, this._toggleProcessingWithContext);
_events_engine.default.off(this._element, this._compositionEnd, this._toggleProcessingWithContext)
}
this._element = void 0;
this._handler = void 0
},
process(e) {
this._handler(createKeyDownOptions(e))
},
toggleProcessing(_ref) {
let {
type: type
} = _ref;
this._isComposing = "compositionstart" === type;
this._isComposingJustFinished = !this._isComposing
}
});
KeyboardProcessor.createKeyDownOptions = createKeyDownOptions;
var _default = exports.default = KeyboardProcessor;