UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

83 lines (82 loc) 3.3 kB
/** * DevExtreme (esm/__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/ */ import eventsEngine from "../../../common/core/events/core/events_engine"; import { addNamespace, normalizeKeyName } from "../../../common/core/events/utils/index"; import Class from "../../../core/class"; import $ from "../../../core/renderer"; const COMPOSITION_START_EVENT = "compositionstart"; const COMPOSITION_END_EVENT = "compositionend"; const KEYDOWN_EVENT = "keydown"; const NAMESPACE = "KeyboardProcessor"; 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 }); const KeyboardProcessor = Class.inherit({ _keydown: addNamespace("keydown", NAMESPACE), _compositionStart: addNamespace("compositionstart", NAMESPACE), _compositionEnd: addNamespace("compositionend", NAMESPACE), ctor(options) { options = options || {}; if (options.element) { this._element = $(options.element) } if (options.focusTarget) { this._focusTarget = options.focusTarget } this._handler = options.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, this._keydown, this._processFunction); eventsEngine.on(this._element, this._compositionStart, this._toggleProcessingWithContext); eventsEngine.on(this._element, this._compositionEnd, this._toggleProcessingWithContext) } }, dispose() { if (this._element) { eventsEngine.off(this._element, this._keydown, this._processFunction); eventsEngine.off(this._element, this._compositionStart, this._toggleProcessingWithContext); eventsEngine.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; export default KeyboardProcessor;