Skip to content

Commit aad0c15

Browse files
ert78gbmondalaci
authored andcommitted
fix(keymap): Change keymap name and abbr algorithm (#365)
* feat(keymap): Change keymap name and abbr algorithm Close #363 * feat(keymap): When rename a keymap or macro and the new name is exists do nothing
1 parent 42683e3 commit aad0c15

File tree

8 files changed

+169
-120
lines changed

8 files changed

+169
-120
lines changed

shared/src/components/keymap/header/keymap-header.component.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ <h1 class="col-xs-12 pane-title">
55
<input #name cancelable
66
class="keymap__name pane-title__name"
77
type="text"
8-
value="{{ keymap.name }}"
98
(change)="editKeymapName($event.target.value)"
109
(keyup.enter)="name.blur()"
1110
/> keymap
1211
(<input #abbr cancelable
1312
class="keymap__abbrev pane-title__abbrev"
1413
type="text"
15-
value="{{ keymap.abbreviation }}"
1614
(change)="editKeymapAbbr($event.target.value)"
1715
(keyup.enter)="abbr.blur()"
1816
[attr.maxLength]="3"

shared/src/components/keymap/header/keymap-header.component.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import {
22
ChangeDetectionStrategy,
33
Component,
44
ElementRef,
5-
Input,
6-
Output,
75
EventEmitter,
6+
Input,
87
OnChanges,
9-
Renderer,
8+
Output,
9+
Renderer2,
1010
SimpleChanges,
1111
ViewChild
1212
} from '@angular/core';
@@ -35,11 +35,13 @@ export class KeymapHeaderComponent implements OnChanges {
3535
starTitle: string;
3636
trashTitle: string = 'Delete keymap';
3737

38-
constructor(private store: Store<AppState>, private renderer: Renderer) { }
38+
constructor(private store: Store<AppState>, private renderer: Renderer2) { }
3939

4040
ngOnChanges(changes: SimpleChanges) {
4141
if (changes['keymap']) {
4242
this.setKeymapTitle();
43+
this.setName();
44+
this.setAbbreviation();
4345
}
4446
if (changes['deletable']) {
4547
this.setTrashTitle();
@@ -64,7 +66,7 @@ export class KeymapHeaderComponent implements OnChanges {
6466

6567
editKeymapName(name: string) {
6668
if (name.length === 0) {
67-
this.renderer.setElementProperty(this.keymapName.nativeElement, 'value', this.keymap.name);
69+
this.setName();
6870
return;
6971
}
7072

@@ -75,12 +77,12 @@ export class KeymapHeaderComponent implements OnChanges {
7577
const regexp = new RegExp(/^[a-zA-Z\d]+$/g);
7678

7779
if (newAbbr.length < 1 || newAbbr.length > 3 || !regexp.test(newAbbr)) {
78-
this.renderer.setElementProperty(this.keymapAbbr.nativeElement, 'value', this.keymap.abbreviation);
80+
this.setAbbreviation();
7981
return;
8082
}
8183

8284
newAbbr = newAbbr.toUpperCase();
83-
this.store.dispatch(KeymapActions.editKeymapAbbr(this.keymap.abbreviation, newAbbr));
85+
this.store.dispatch(KeymapActions.editKeymapAbbr(this.keymap.name, this.keymap.abbreviation, newAbbr));
8486
}
8587

8688
setKeymapTitle(): void {
@@ -96,4 +98,12 @@ export class KeymapHeaderComponent implements OnChanges {
9698
onDownloadIconClick(): void {
9799
this.downloadClick.emit();
98100
}
101+
102+
private setName(): void {
103+
this.renderer.setProperty(this.keymapName.nativeElement, 'value', this.keymap.name);
104+
}
105+
106+
private setAbbreviation() {
107+
this.renderer.setProperty(this.keymapAbbr.nativeElement, 'value', this.keymap.abbreviation);
108+
}
99109
}

shared/src/components/macro/header/macro-header.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ <h1 class="col-xs-12 pane-title">
55
<input #macroName cancelable
66
class="pane-title__name"
77
type="text"
8-
value="{{ macro.name }}"
98
(change)="editMacroName($event.target.value)"
109
(keyup.enter)="macroName.blur()"
1110
/>

shared/src/components/macro/header/macro-header.component.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import {
2-
AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Input, OnChanges, Renderer,
2+
AfterViewInit,
3+
ChangeDetectionStrategy,
4+
Component,
5+
ElementRef,
6+
Input,
7+
OnChanges,
8+
Renderer2,
9+
SimpleChanges,
310
ViewChild
411
} from '@angular/core';
512

@@ -21,17 +28,20 @@ export class MacroHeaderComponent implements AfterViewInit, OnChanges {
2128
@Input() isNew: boolean;
2229
@ViewChild('macroName') macroName: ElementRef;
2330

24-
constructor(private store: Store<AppState>, private renderer: Renderer) { }
31+
constructor(private store: Store<AppState>, private renderer: Renderer2) { }
2532

26-
ngOnChanges() {
33+
ngOnChanges(changes: SimpleChanges) {
2734
if (this.isNew) {
28-
this.renderer.invokeElementMethod(this.macroName.nativeElement, 'select', []);
35+
this.setFocusOnName();
36+
}
37+
if (changes['macro']) {
38+
this.setName();
2939
}
3040
}
3141

3242
ngAfterViewInit() {
3343
if (this.isNew) {
34-
this.renderer.invokeElementMethod(this.macroName.nativeElement, 'select', []);
44+
this.setFocusOnName();
3545
}
3646
}
3747

@@ -45,11 +55,19 @@ export class MacroHeaderComponent implements AfterViewInit, OnChanges {
4555

4656
editMacroName(name: string) {
4757
if (name.length === 0) {
48-
this.renderer.setElementProperty(this.macroName.nativeElement, 'value', this.macro.name);
58+
this.setName();
4959
return;
5060
}
5161

5262
this.store.dispatch(MacroActions.editMacroName(this.macro.id, name));
5363
}
5464

65+
private setFocusOnName() {
66+
this.macroName.nativeElement.select();
67+
}
68+
69+
private setName(): void {
70+
this.renderer.setProperty(this.macroName.nativeElement, 'value', this.macro.name);
71+
}
72+
5573
}

shared/src/store/actions/keymap.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ export namespace KeymapActions {
6969
};
7070
}
7171

72-
export function editKeymapAbbr(abbr: string, newAbbr: string): Action {
72+
export function editKeymapAbbr(name: string, abbr: string, newAbbr: string): Action {
7373
return {
7474
type: KeymapActions.EDIT_ABBR,
7575
payload: {
76-
abbr: abbr,
77-
newAbbr: newAbbr
76+
name,
77+
abbr,
78+
newAbbr
7879
}
7980
};
8081
}

shared/src/store/effects/keymap.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ export class KeymapEffects {
5454

5555
@Effect({ dispatch: false }) editAbbr$: any = this.actions$
5656
.ofType(KeymapActions.EDIT_ABBR)
57-
.map(action => action.payload.newAbbr)
58-
.do(newAbbr => {
59-
this.router.navigate(['/keymap', newAbbr]);
57+
.withLatestFrom(this.store)
58+
.do(([action, store]) => {
59+
for (const keymap of store.userConfiguration.keymaps) {
60+
if (keymap.name === action.payload.name && keymap.abbreviation === action.payload.newAbbr) {
61+
this.router.navigate(['/keymap', action.payload.newAbbr]);
62+
return;
63+
}
64+
}
6065
});
6166

6267
constructor(private actions$: Actions, private router: Router, private store: Store<AppState>) { }

shared/src/store/effects/macro.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import { AppState } from '../index';
1414
@Injectable()
1515
export class MacroEffects {
1616

17-
@Effect({dispatch: false}) remove$: any = this.actions$
17+
@Effect({ dispatch: false }) remove$: any = this.actions$
1818
.ofType(MacroActions.REMOVE)
1919
.map(action => this.store.dispatch(KeymapActions.checkMacro(action.payload)))
2020
.withLatestFrom(this.store)
21-
.map(latest => latest[1].userConfiguration.macros)
21+
.map(([action, state]) => state.userConfiguration.macros)
2222
.do(macros => {
2323
if (macros.length === 0) {
2424
this.router.navigate(['/macro']);
@@ -27,14 +27,23 @@ export class MacroEffects {
2727
}
2828
});
2929

30-
@Effect({dispatch: false}) add$: any = this.actions$
30+
@Effect({ dispatch: false }) add$: any = this.actions$
3131
.ofType(MacroActions.ADD)
3232
.withLatestFrom(this.store)
33-
.map(latest => latest[1].userConfiguration.macros)
33+
.map(([action, state]) => state.userConfiguration.macros)
3434
.map(macros => macros[macros.length - 1])
3535
.do(lastMacro => {
3636
this.router.navigate(['/macro', lastMacro.id, 'new']);
3737
});
3838

39+
@Effect({ dispatch: false }) duplicate: any = this.actions$
40+
.ofType(MacroActions.DUPLICATE)
41+
.withLatestFrom(this.store)
42+
.map(([action, state]) => state.userConfiguration.macros)
43+
.map(macros => macros[macros.length - 1])
44+
.do(lastMacro => {
45+
this.router.navigate(['/macro', lastMacro.id]);
46+
});
47+
3948
constructor(private actions$: Actions, private router: Router, private store: Store<AppState>) {}
4049
}

0 commit comments

Comments
 (0)