Correctly updates new keymap abbrevation in SwitchKeymapActions (#270)
Fixes #269
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user