From 7f972cac83f73bf6e33f8befebbdf3dd231f43c1 Mon Sep 17 00:00:00 2001 From: Borislav Popnikolov Date: Tue, 23 Apr 2019 19:37:29 +0200 Subject: [PATCH] fix(mask-directive): replaced native KeyboardEvent with type alias (#424) in order to avoid server errors we have to patch the global object with fake implementation of the KeyboardEvent and add a type alias so that the global object path is executed right before the event reference in the code fixes https://github.com/JsDaddy/ngx-mask/issues/399 close #399 --- src/app/ngx-mask/custom-keyboard-event.ts | 10 ++++++++++ src/app/ngx-mask/mask.directive.ts | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/app/ngx-mask/custom-keyboard-event.ts diff --git a/src/app/ngx-mask/custom-keyboard-event.ts b/src/app/ngx-mask/custom-keyboard-event.ts new file mode 100644 index 00000000..3d543e37 --- /dev/null +++ b/src/app/ngx-mask/custom-keyboard-event.ts @@ -0,0 +1,10 @@ +// tslint:disable: no-any typedef +declare var global: any; + +(function() { + if (!global.KeyboardEvent) { + global.KeyboardEvent = function(_eventType: any, _init: any) {}; + } +})(); + +export type CustomKeyboardEvent = KeyboardEvent; diff --git a/src/app/ngx-mask/mask.directive.ts b/src/app/ngx-mask/mask.directive.ts index 7cdcf902..31a91368 100644 --- a/src/app/ngx-mask/mask.directive.ts +++ b/src/app/ngx-mask/mask.directive.ts @@ -3,6 +3,7 @@ import { DOCUMENT } from '@angular/common'; import { ControlValueAccessor, FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors } from '@angular/forms'; import { MaskService } from './mask.service'; import { config, IConfig, withoutValidation } from './config'; +import { CustomKeyboardEvent } from './custom-keyboard-event'; @Directive({ selector: '[mask]', @@ -180,7 +181,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges { } @HostListener('input', ['$event']) - public onInput(e: KeyboardEvent): void { + public onInput(e: CustomKeyboardEvent): void { const el: HTMLInputElement = e.target as HTMLInputElement; this._inputValue = el.value; if (!this._maskValue) { @@ -218,7 +219,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges { } @HostListener('click', ['$event']) - public onFocus(e: MouseEvent | KeyboardEvent): void { + public onFocus(e: MouseEvent | CustomKeyboardEvent): void { const el: HTMLInputElement = e.target as HTMLInputElement; const posStart: number = 0; const posEnd: number = 0; @@ -252,7 +253,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges { } @HostListener('keydown', ['$event']) - public a(e: KeyboardEvent): void { + public a(e: CustomKeyboardEvent): void { this._code = e.code; const el: HTMLInputElement = e.target as HTMLInputElement; this._maskService.selStart = el.selectionStart;