Skip to content

Commit

Permalink
feat: update angular
Browse files Browse the repository at this point in the history
  • Loading branch information
d3lm committed Oct 25, 2019
1 parent 3213dad commit 875ed98
Show file tree
Hide file tree
Showing 4 changed files with 1,285 additions and 649 deletions.
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,30 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^8.0.0",
"@angular/animations": "^8.2.12",
"@angular/cdk": "^8.0.0",
"@angular/common": "^8.0.0",
"@angular/compiler": "^8.0.0",
"@angular/core": "^8.0.0",
"@angular/forms": "^8.0.0",
"@angular/common": "^8.2.12",
"@angular/compiler": "^8.2.12",
"@angular/core": "^8.2.12",
"@angular/forms": "^8.2.12",
"@angular/http": "^7.2.15",
"@angular/material": "^8.0.0",
"@angular/platform-browser": "^8.0.0",
"@angular/platform-browser-dynamic": "^8.0.0",
"@angular/platform-server": "^8.0.0",
"@angular/router": "^8.0.0",
"@angular/platform-browser": "^8.2.12",
"@angular/platform-browser-dynamic": "^8.2.12",
"@angular/platform-server": "^8.2.12",
"@angular/router": "^8.2.12",
"core-js": "^3.1.3",
"rxjs": "^6.5.2",
"rxjs": "^6.5.3",
"web-animations-js": "^2.3.1",
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular-builders/jest": "^8.0.2",
"@angular-devkit/build-angular": "~0.800.0",
"@angular-devkit/build-ng-packagr": "^0.800.1",
"@angular/cli": "^8.0.1",
"@angular/compiler-cli": "^8.0.0",
"@angular/language-service": "^8.0.0",
"@angular-devkit/build-angular": "~0.803.14",
"@angular-devkit/build-ng-packagr": "^0.803.14",
"@angular/cli": "^8.3.14",
"@angular/compiler-cli": "^8.2.12",
"@angular/language-service": "^8.2.12",
"@bahmutov/add-typescript-to-cypress": "^2.1.1",
"@commitlint/cli": "^8.0.0",
"@commitlint/config-angular": "^8.0.0",
Expand All @@ -95,16 +95,16 @@
"jest": "^24.8.0",
"jest-preset-angular": "^7.1.1",
"lint-staged": "^8.1.7",
"ng-packagr": "^5.2.0",
"ng-packagr": "^5.7.0",
"node-sass": "^4.12.0",
"prettier": "^1.17.1",
"start-server-and-test": "^1.9.1",
"ts-loader": "^6.0.2",
"ts-node": "^8.2.0",
"tsickle": "^0.35.0",
"tslib": "^1.9.3",
"tslib": "^1.10.0",
"tslint": "^5.17.0",
"typescript": "3.4.5",
"typescript": "3.5.3",
"webpack-cli": "^3.3.2"
}
}
}
48 changes: 32 additions & 16 deletions projects/ngx-drag-to-select/src/lib/keyboard-events.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import { Injectable } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { fromEvent, Observable } from 'rxjs';
import { share } from 'rxjs/operators';
import { fromEvent } from 'rxjs';
import { distinctKeyEvents } from './operators';

@Injectable()
export class KeyboardEventsService {
keydown$ = fromEvent<KeyboardEvent>(window, 'keydown').pipe(share());
keyup$ = fromEvent<KeyboardEvent>(window, 'keyup').pipe(share());
keydown$: Observable<KeyboardEvent>;
keyup$: Observable<KeyboardEvent>;
distinctKeydown$: Observable<KeyboardEvent>;
distinctKeyup$: Observable<KeyboardEvent>;
mouseup$: Observable<MouseEvent>;
mousemove$: Observable<MouseEvent>;

// distinctKeyEvents is used to prevent multiple key events to be fired repeatedly
// on Windows when a key is being pressed
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformBrowser(this.platformId)) {
this._initializeKeyboardStreams();
}
}

distinctKeydown$ = this.keydown$.pipe(
distinctKeyEvents(),
share()
);
private _initializeKeyboardStreams() {
this.keydown$ = fromEvent<KeyboardEvent>(window, 'keydown').pipe(share());
this.keyup$ = fromEvent<KeyboardEvent>(window, 'keyup').pipe(share());

distinctKeyup$ = this.keyup$.pipe(
distinctKeyEvents(),
share()
);
// distinctKeyEvents is used to prevent multiple key events to be fired repeatedly
// on Windows when a key is being pressed

mouseup$ = fromEvent<MouseEvent>(window, 'mouseup').pipe(share());
mousemove$ = fromEvent<MouseEvent>(window, 'mousemove').pipe(share());
this.distinctKeydown$ = this.keydown$.pipe(
distinctKeyEvents(),
share()
);

this.distinctKeyup$ = this.keyup$.pipe(
distinctKeyEvents(),
share()
);

this.mouseup$ = fromEvent<MouseEvent>(window, 'mouseup').pipe(share());
this.mousemove$ = fromEvent<MouseEvent>(window, 'mousemove').pipe(share());
}
}
53 changes: 30 additions & 23 deletions projects/ngx-drag-to-select/src/lib/shortcut.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject, Injectable } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { merge } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { KeyboardEventsService } from './keyboard-events.service';
Expand Down Expand Up @@ -41,30 +42,36 @@ export class ShortcutService {

private _latestShortcut: Map<string, boolean> = new Map();

constructor(@Inject(CONFIG) config: DragToSelectConfig, private keyboardEvents: KeyboardEventsService) {
constructor(
@Inject(PLATFORM_ID) private platformId: Object,
@Inject(CONFIG) config: DragToSelectConfig,
private keyboardEvents: KeyboardEventsService
) {
this._shortcuts = this._createShortcutsFromConfig(config.shortcuts);

const keydown$ = this.keyboardEvents.keydown$.pipe(
map<KeyboardEvent, KeyState>(event => ({ code: event.code, pressed: true }))
);

const keyup$ = this.keyboardEvents.keyup$.pipe(
map<KeyboardEvent, KeyState>(event => ({ code: event.code, pressed: false }))
);

merge<KeyState>(keydown$, keyup$)
.pipe(
distinctUntilChanged((prev, curr) => {
return prev.pressed === curr.pressed && prev.code === curr.code;
})
)
.subscribe(keyState => {
if (keyState.pressed) {
this._latestShortcut.set(keyState.code, true);
} else {
this._latestShortcut.delete(keyState.code);
}
});
if (isPlatformBrowser(this.platformId)) {
const keydown$ = this.keyboardEvents.keydown$.pipe(
map<KeyboardEvent, KeyState>(event => ({ code: event.code, pressed: true }))
);

const keyup$ = this.keyboardEvents.keyup$.pipe(
map<KeyboardEvent, KeyState>(event => ({ code: event.code, pressed: false }))
);

merge<KeyState>(keydown$, keyup$)
.pipe(
distinctUntilChanged((prev, curr) => {
return prev.pressed === curr.pressed && prev.code === curr.code;
})
)
.subscribe(keyState => {
if (keyState.pressed) {
this._latestShortcut.set(keyState.code, true);
} else {
this._latestShortcut.delete(keyState.code);
}
});
}
}

disableSelection(event: Event) {
Expand Down

0 comments on commit 875ed98

Please sign in to comment.