MCPcopy
hub / github.com/autoNumeric/autoNumeric / _onKeydown

Method _onKeydown

src/AutoNumeric.js:6555–6689  ·  view source on GitHub ↗

* Handler for 'keydown' events. * The user just started pushing any key, hence one event is sent. * * Note : * By default a 'normal' input output those events in the right order when inputting a character key (i.e. 'a') : * - keydown * - keypress * - input * -

(e)

Source from the content-addressed store, hash-verified

6553 * @param {KeyboardEvent} e
6554 */
6555 _onKeydown(e) {
6556 if (this.compositioning) {
6557 return;
6558 }
6559 this.formatted = false; // Keep track if the element has been formatted already. If that's the case, prevent further format calculations.
6560 this.isEditing = true; // Keep track if the user is currently editing the element manually
6561
6562 if (!this.formulaMode && !this.isFocused && this.settings.unformatOnHover && e.altKey && this.domElement === AutoNumericHelper.getHoveredElement()) {
6563 // Here I prevent calling _unformatAltHovered if the element is already focused, since the global 'keydown' listener will pick it up as well
6564 this.constructor._unformatAltHovered(this);
6565
6566 return;
6567 }
6568
6569 this._updateEventKeyInfo(e);
6570
6571 this.keydownEventCounter += 1; // Every time the keydown event is caught, increment the counter to keep track if the key is continuously pressed
6572 if (this.keydownEventCounter === 1) {
6573 this.initialValueOnFirstKeydown = AutoNumericHelper.getElementValue(e.target); // This is needed in `onKeyup()` to check if the value as changed during the key press
6574 this.initialRawValueOnFirstKeydown = this.rawValue;
6575 }
6576
6577 if (this.formulaMode) {
6578 if (this.eventKey === AutoNumericEnum.keyName.Esc) { // Cancel the formula
6579 this.formulaMode = false;
6580 this.reformat();
6581
6582 return;
6583 }
6584
6585 if (this.eventKey === AutoNumericEnum.keyName.Enter) { // Calculate the formula
6586 this._exitFormulaMode();
6587
6588 return;
6589 }
6590
6591 // Accept the backspace, delete, arrow, home and end keys
6592 if (this._acceptNonPrintableKeysInFormulaMode()) {
6593 return;
6594 }
6595
6596 //TODO Manage the undo/redo events *while* editing a math expression
6597 //TODO Manage the cut/paste events *while* editing a math expression
6598 } else {
6599 if (this.eventKey === AutoNumericEnum.keyName.Equal) {
6600 this._enterFormulaMode();
6601
6602 return;
6603 }
6604
6605 if (this.settings.modifyValueOnUpDownArrow &&
6606 (this.eventKey === AutoNumericEnum.keyName.UpArrow || this.eventKey === AutoNumericEnum.keyName.DownArrow)) {
6607 this.isEditing = false; // Fix issue #817: When rawValueDivisor is not null: should disable this flag, otherwise this.rawValue becomes invalid in _setRawValue (_setRawValue gets the raw value that is already divided by rawValueDivisor and if isEditing is true the value is divided once more)
6608 this.upDownArrowAction(e);
6609
6610 return;
6611 }
6612 }

Callers 1

_createEventListenersMethod · 0.95

Calls 15

_updateEventKeyInfoMethod · 0.95
reformatMethod · 0.95
_exitFormulaModeMethod · 0.95
_enterFormulaModeMethod · 0.95
upDownArrowActionMethod · 0.95
setMethod · 0.95
_triggerEventMethod · 0.95
selectMethod · 0.95
_saveCancellableValueMethod · 0.95

Tested by

no test coverage detected