Correctly updates new keymap abbrevation in SwitchKeymapActions (#270)

Fixes #269
This commit is contained in:
József Farkas
2017-02-26 15:57:51 +01:00
committed by GitHub
parent 715b924be0
commit 9fcbac808b
6 changed files with 84 additions and 4 deletions

View File

@@ -69,4 +69,26 @@ export class Keymap {
toString(): string {
return `<Keymap abbreviation="${this.abbreviation}" name="${this.name}">`;
}
renameKeymap(oldAbbr: string, newAbbr: string): Keymap {
let layers: Layer[];
let layerModified = false;
this.layers.forEach((layer, index) => {
const newLayer = layer.renameKeymap(oldAbbr, newAbbr);
if (newLayer !== layer) {
if (!layerModified) {
layers = this.layers.slice();
layerModified = true;
}
layers[index] = newLayer;
}
});
if (layerModified) {
const newKeymap = Object.assign(new Keymap(), this);
newKeymap.layers = layers;
return newKeymap;
}
return this;
}
}

View File

@@ -42,4 +42,25 @@ export class Layer {
return `<Layer>`;
}
renameKeymap(oldAbbr: string, newAbbr: string): Layer {
let modules: Module[];
let moduleModified = false;
this.modules.forEach((module, index) => {
const newModule = module.renameKeymap(oldAbbr, newAbbr);
if (newModule !== module) {
if (!moduleModified) {
modules = this.modules.slice();
moduleModified = true;
}
modules[index] = newModule;
}
});
if (moduleModified) {
const newLayer = Object.assign(new Layer(), this);
newLayer.modules = modules;
return newLayer;
}
return this;
}
}

View File

@@ -84,4 +84,26 @@ export class Module {
return `<Module id="${this.id}" pointerRole="${this.pointerRole}">`;
}
renameKeymap(oldAbbr: string, newAbbr: string): Module {
let keyActions: KeyAction[];
let keyActionModified = false;
this.keyActions.forEach((keyAction, index) => {
if (!keyAction) { return; }
const newKeyAction = keyAction.renameKeymap(oldAbbr, newAbbr);
if (newKeyAction !== keyAction) {
if (!keyActionModified) {
keyActions = this.keyActions.slice();
keyActionModified = true;
}
keyActions[index] = newKeyAction;
}
});
if (keyActionModified) {
const newModule = Object.assign(new Module(), this);
newModule.keyActions = keyActions;
return newModule;
}
return this;
}
}

View File

@@ -56,4 +56,7 @@ export abstract class KeyAction {
abstract toJsonObject(macros?: Macro[]): any;
abstract toBinary(buffer: UhkBuffer, macros?: Macro[]): any;
renameKeymap(oldAbbr: string, newAbbr: string): KeyAction {
return this;
}
}

View File

@@ -6,15 +6,17 @@ export class SwitchKeymapAction extends KeyAction {
keymapAbbreviation: string;
constructor(parameter?: SwitchKeymapAction | Keymap) {
constructor(parameter?: SwitchKeymapAction | Keymap | string) {
super();
if (!parameter) {
return;
}
if (parameter instanceof SwitchKeymapAction) {
this.keymapAbbreviation = parameter.keymapAbbreviation;
} else {
} else if (parameter instanceof Keymap) {
this.keymapAbbreviation = parameter.abbreviation;
} else {
this.keymapAbbreviation = parameter;
}
}
@@ -45,4 +47,11 @@ export class SwitchKeymapAction extends KeyAction {
toString(): string {
return `<SwitchKeymapAction keymapAbbreviation="${this.keymapAbbreviation}">`;
}
renameKeymap(oldAbbr: string, newAbbr: string): KeyAction {
if (this.keymapAbbreviation !== oldAbbr) {
return this;
}
return new SwitchKeymapAction(newAbbr);
}
}

View File

@@ -1,10 +1,11 @@
import '@ngrx/core/add/operator/select';
import { Action } from '@ngrx/store';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import { Helper as KeyActionHelper, KeyAction } from '../../config-serializer/config-items/key-action';
import { Helper as KeyActionHelper, KeyAction, SwitchKeymapAction } from '../../config-serializer/config-items/key-action';
import { Keymap } from '../../config-serializer/config-items/Keymap';
import { Macro } from '../../config-serializer/config-items/Macro';
import { UserConfiguration } from '../../config-serializer/config-items/UserConfiguration';
@@ -53,6 +54,8 @@ export default function (state = initialState, action: Action): UserConfiguratio
if (keymap.abbreviation === action.payload.abbr) {
keymap = Object.assign(new Keymap(), keymap);
keymap.abbreviation = abbr;
} else {
keymap = keymap.renameKeymap(action.payload.abbr, action.payload.newAbbr);
}
return keymap;