From 9b2aa188e6deb6e2710f1a8d17a95f6a53d31ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zsef=20Farkas?= Date: Fri, 30 Dec 2016 11:51:46 +0100 Subject: [PATCH] Use ids in key actions instead of references (#239) Partially reverts e48fdea --- .../tab/keymap/keymap-tab.component.ts | 5 +- .../popover/tab/macro/macro-tab.component.ts | 4 +- .../svg-keyboard-key.component.ts | 5 +- .../svg/wrap/svg-keyboard-wrap.component.html | 2 +- .../svg/wrap/svg-keyboard-wrap.component.ts | 166 +++++++++++------- src/config-serializer/config-items/Keymap.ts | 12 +- src/config-serializer/config-items/Layer.ts | 12 +- src/config-serializer/config-items/Module.ts | 12 +- .../config-items/UhkConfiguration.ts | 28 +-- .../key-action/PlayMacroAction.ts | 23 +-- .../key-action/SwitchKeymapAction.ts | 21 ++- .../config-items/key-action/helper.ts | 30 +--- src/store/storage/index.ts | 9 +- 13 files changed, 165 insertions(+), 164 deletions(-) diff --git a/src/components/popover/tab/keymap/keymap-tab.component.ts b/src/components/popover/tab/keymap/keymap-tab.component.ts index 554d6e42..76800771 100644 --- a/src/components/popover/tab/keymap/keymap-tab.component.ts +++ b/src/components/popover/tab/keymap/keymap-tab.component.ts @@ -59,7 +59,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab { } const switchKeymapAction: SwitchKeymapAction = keyAction; - this.selectedKeymap = switchKeymapAction.keymap; + this.selectedKeymap = this.keymaps + .find((keymap: Keymap) => keymap.abbreviation === switchKeymapAction.keymapAbbreviation); } toKeyAction(): SwitchKeymapAction { @@ -68,7 +69,7 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab { } const keymapAction = new SwitchKeymapAction(); - keymapAction.keymap = this.selectedKeymap; + keymapAction.keymapAbbreviation = this.selectedKeymap.abbreviation; return keymapAction; } } diff --git a/src/components/popover/tab/macro/macro-tab.component.ts b/src/components/popover/tab/macro/macro-tab.component.ts index 9bd5574f..ba657722 100644 --- a/src/components/popover/tab/macro/macro-tab.component.ts +++ b/src/components/popover/tab/macro/macro-tab.component.ts @@ -61,7 +61,7 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab { return false; } const playMacroAction: PlayMacroAction = keyAction; - this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macro === macro); + this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macroId === macro.id); return true; } @@ -71,7 +71,7 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab { } const keymapAction = new PlayMacroAction(); - keymapAction.macro = this.macros[this.selectedMacroIndex]; + keymapAction.macroId = this.macros[this.selectedMacroIndex].id; return keymapAction; } diff --git a/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts b/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts index a42cbe45..156e6be3 100644 --- a/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts +++ b/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts @@ -283,13 +283,14 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges, OnDestroy { } else if (this.keyAction instanceof SwitchKeymapAction) { let keyAction: SwitchKeymapAction = this.keyAction as SwitchKeymapAction; this.labelType = LabelTypes.SwitchKeymap; - this.labelSource = keyAction.keymap.abbreviation; + this.labelSource = keyAction.keymapAbbreviation; } else if (this.keyAction instanceof PlayMacroAction) { let keyAction: PlayMacroAction = this.keyAction as PlayMacroAction; + const macro: Macro = this.macros.find((macro: Macro) => macro.id === keyAction.macroId); this.labelType = LabelTypes.IconText; this.labelSource = { icon: this.mapper.getIcon('macro'), - text: keyAction.macro.name + text: macro.name }; } else if (this.keyAction instanceof MouseAction) { this.labelType = LabelTypes.MouseKey; diff --git a/src/components/svg/wrap/svg-keyboard-wrap.component.html b/src/components/svg/wrap/svg-keyboard-wrap.component.html index 0bb9d8ff..7b30fddd 100644 --- a/src/components/svg/wrap/svg-keyboard-wrap.component.html +++ b/src/components/svg/wrap/svg-keyboard-wrap.component.html @@ -16,7 +16,7 @@ >
-

+

{{ item.name }}: {{ item.value }}

diff --git a/src/components/svg/wrap/svg-keyboard-wrap.component.ts b/src/components/svg/wrap/svg-keyboard-wrap.component.ts index edd95870..95944635 100644 --- a/src/components/svg/wrap/svg-keyboard-wrap.component.ts +++ b/src/components/svg/wrap/svg-keyboard-wrap.component.ts @@ -12,6 +12,9 @@ import { SimpleChanges } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; +import 'rxjs/add/observable/of'; +import 'rxjs/add/operator/map'; import { Store } from '@ngrx/store'; import { MapperService } from '../../../services/mapper.service'; @@ -35,6 +38,11 @@ import { AppState } from '../../../store'; import { KeymapActions } from '../../../store/actions'; import { PopoverComponent } from '../../popover'; +interface NameValuePair { + name: string; + value: string; +} + @Component({ selector: 'svg-keyboard-wrap', template: require('./svg-keyboard-wrap.component.html'), @@ -46,14 +54,19 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { @Input() popoverEnabled: boolean = true; @Input() tooltipEnabled: boolean = false; - @ViewChild(PopoverComponent, { read: ElementRef}) popover: ElementRef; + @ViewChild(PopoverComponent, { read: ElementRef }) popover: ElementRef; private popoverShown: boolean; private keyEditConfig: { moduleId: number, keyId: number }; private popoverInitKeyAction: KeyAction; private keybindAnimationEnabled: boolean; private currentLayer: number = 0; - private tooltipData: { posTop: number, posLeft: number, content: { name: string, value: string }[], show: boolean }; + private tooltipData: { + posTop: number, + posLeft: number, + content: Observable, + show: boolean + }; private layers: Layer[]; private keyPosition: ClientRect; private wrapPosition: ClientRect; @@ -89,7 +102,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { this.tooltipData = { posTop: 0, posLeft: 0, - content: [], + content: Observable.of([]), show: false }; } @@ -139,7 +152,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { } } - onCapture(moduleId: number, keyId: number, captured: {code: number, left: boolean[], right: boolean[]}): void { + onCapture(moduleId: number, keyId: number, captured: { code: number, left: boolean[], right: boolean[] }): void { let keystrokeAction: KeystrokeAction = new KeystrokeAction(); const modifiers = captured.left.concat(captured.right).map(x => x ? 1 : 0); @@ -194,13 +207,30 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { posTop = position.top + position.height; } - let content: { - name: string, - value: string - }[] = []; + this.tooltipData = { + posLeft: posLeft, + posTop: posTop, + content: this.getKeyActionContent(keyAction), + show: true + }; + } + hideTooltip() { + this.tooltipData.show = false; + } + + hidePopover(): void { + this.popoverShown = false; + } + + selectLayer(index: number): void { + this.currentLayer = index; + } + + private getKeyActionContent(keyAction: KeyAction): Observable { if (keyAction instanceof KeystrokeAction) { const keystrokeAction: KeystrokeAction = keyAction; + const content: NameValuePair[] = []; content.push({ name: 'Action type', value: 'Keystroke' @@ -231,71 +261,79 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { value: LongPressAction[keystrokeAction.longPressAction] }); } + return Observable.of(content); } else if (keyAction instanceof MouseAction) { const mouseAction: MouseAction = keyAction; - content.push({ - name: 'Action type', - value: 'Mouse' - }); - content.push({ - name: 'Action', - value: camelCaseToSentence(MouseActionParam[mouseAction.mouseAction]) - }); + const content: NameValuePair[] = + [ + { + name: 'Action type', + value: 'Mouse' + }, + { + name: 'Action', + value: camelCaseToSentence(MouseActionParam[mouseAction.mouseAction]) + } + ]; + return Observable.of(content); } else if (keyAction instanceof PlayMacroAction) { const playMacroAction: PlayMacroAction = keyAction; - content.push({ - name: 'Action type', - value: 'Play macro' - }); - - content.push({ - name: 'Macro name', - value: playMacroAction.macro.name.toString() - }); - + return this.store + .select(appState => appState.macros) + .map(macroState => macroState.entities.find(macro => { + return macro.id === playMacroAction.macroId; + }).name) + .map(macroName => { + const content: NameValuePair[] = [ + { + name: 'Action type', + value: 'Play macro' + }, + { + name: 'Macro name', + value: macroName + } + ]; + return content; + }); } else if (keyAction instanceof SwitchKeymapAction) { const switchKeymapAction: SwitchKeymapAction = keyAction; - content.push({ - name: 'Action type', - value: 'Switch keymap' - }); - content.push({ - name: 'Keymap', - value: switchKeymapAction.keymap.name - }); + return this.store + .select(appState => appState.keymaps.entities) + .map(keymaps => keymaps.find(keymap => keymap.abbreviation === switchKeymapAction.keymapAbbreviation).name) + .map(keymapName => { + const content: NameValuePair[] = [ + { + name: 'Action type', + value: 'Switch keymap' + }, + { + name: 'Keymap', + value: keymapName + } + ]; + return content; + }); } else if (keyAction instanceof SwitchLayerAction) { const switchLayerAction: SwitchLayerAction = keyAction; - content.push({ - name: 'Action type', - value: 'Switch layer' - }); - content.push({ - name: 'Layer', - value: capitalizeFirstLetter(LayerName[switchLayerAction.layer]) - }); - content.push({ - name: 'Toogle', - value: switchLayerAction.isLayerToggleable ? 'On' : 'Off' - }); + const content: NameValuePair[] = + [ + { + name: 'Action type', + value: 'Switch layer' + }, + { + name: 'Layer', + value: capitalizeFirstLetter(LayerName[switchLayerAction.layer]) + }, + { + name: 'Toogle', + value: switchLayerAction.isLayerToggleable ? 'On' : 'Off' + } + ]; + return Observable.of(content); } - this.tooltipData = { - posLeft: posLeft, - posTop: posTop, - content, - show: true - }; - } - - hideTooltip() { - this.tooltipData.show = false; - } - - hidePopover(): void { - this.popoverShown = false; - } - - selectLayer(index: number): void { - this.currentLayer = index; + return Observable.of([]); } } diff --git a/src/config-serializer/config-items/Keymap.ts b/src/config-serializer/config-items/Keymap.ts index fc417a7f..e963b2a7 100644 --- a/src/config-serializer/config-items/Keymap.ts +++ b/src/config-serializer/config-items/Keymap.ts @@ -15,7 +15,7 @@ export class Keymap extends Serializable { layers: Layer[]; - constructor(keymap?: Keymap, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) { + constructor(keymap?: Keymap) { super(); if (!keymap) { return; @@ -25,25 +25,25 @@ export class Keymap extends Serializable { this.description = keymap.description; this.abbreviation = keymap.abbreviation; this.isDefault = keymap.isDefault; - this.layers = keymap.layers.map(layer => new Layer(layer, getKeymap, getMacro)); + this.layers = keymap.layers.map(layer => new Layer(layer)); } - fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap { + fromJsonObject(jsonObject: any): Keymap { this.isDefault = jsonObject.isDefault; this.abbreviation = jsonObject.abbreviation; this.name = jsonObject.name; this.description = jsonObject.description; - this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer, getKeymap, getMacro)); + this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer)); return this; } - fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap { + fromBinary(buffer: UhkBuffer): Keymap { this.abbreviation = buffer.readString(); this.isDefault = buffer.readBoolean(); this.name = buffer.readString(); this.description = buffer.readString(); this.layers = buffer.readArray(uhkBuffer => { - return new Layer().fromBinary(uhkBuffer, getKeymap, getMacro); + return new Layer().fromBinary(uhkBuffer); }); return this; } diff --git a/src/config-serializer/config-items/Layer.ts b/src/config-serializer/config-items/Layer.ts index 5d79148f..df7e311e 100644 --- a/src/config-serializer/config-items/Layer.ts +++ b/src/config-serializer/config-items/Layer.ts @@ -8,22 +8,22 @@ export class Layer extends Serializable { modules: Module[]; - constructor(layers?: Layer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) { + constructor(layers?: Layer) { super(); if (!layers) { return; } - this.modules = layers.modules.map(module => new Module(module, getKeymap, getMacro)); + this.modules = layers.modules.map(module => new Module(module)); } - fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer { - this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module, getKeymap, getMacro)); + fromJsonObject(jsonObject: any): Layer { + this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module)); return this; } - fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer { + fromBinary(buffer: UhkBuffer): Layer { this.modules = buffer.readArray(uhkBuffer => { - return new Module().fromBinary(uhkBuffer, getKeymap, getMacro); + return new Module().fromBinary(uhkBuffer); }); return this; } diff --git a/src/config-serializer/config-items/Module.ts b/src/config-serializer/config-items/Module.ts index 623d9933..99fdd207 100644 --- a/src/config-serializer/config-items/Module.ts +++ b/src/config-serializer/config-items/Module.ts @@ -21,32 +21,32 @@ export class Module extends Serializable { @assertEnum(PointerRole) pointerRole: PointerRole; - constructor(other?: Module, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) { + constructor(other?: Module) { super(); if (!other) { return; } this.id = other.id; - this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro)); + this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction)); this.pointerRole = other.pointerRole; } - fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module { + fromJsonObject(jsonObject: any): Module { this.id = jsonObject.id; this.pointerRole = PointerRole[jsonObject.pointerRole]; this.keyActions = jsonObject.keyActions.map((keyAction: any) => { - return KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro); + return KeyActionHelper.createKeyAction(keyAction); }); return this; } - fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module { + fromBinary(buffer: UhkBuffer): Module { this.id = buffer.readUInt8(); this.pointerRole = buffer.readUInt8(); let keyActionsLength: number = buffer.readCompactLength(); this.keyActions = []; for (let i = 0; i < keyActionsLength; ++i) { - this.keyActions.push(KeyActionHelper.createKeyAction(buffer, getKeymap, getMacro)); + this.keyActions.push(KeyActionHelper.createKeyAction(buffer)); } return this; } diff --git a/src/config-serializer/config-items/UhkConfiguration.ts b/src/config-serializer/config-items/UhkConfiguration.ts index 5f37382d..7e497341 100644 --- a/src/config-serializer/config-items/UhkConfiguration.ts +++ b/src/config-serializer/config-items/UhkConfiguration.ts @@ -40,18 +40,7 @@ export class UhkConfiguration extends Serializable { return new ModuleConfiguration().fromJsonObject(moduleConfiguration); }); this.macros = jsonObject.macros.map((macro: any) => new Macro().fromJsonObject(macro)); - this.keymaps = jsonObject.keymaps.map((keymap: any) => { - const newKeymap = new Keymap(); - newKeymap.abbreviation = keymap.abbreviation; - return newKeymap; - }); - for (let i = 0; i < this.keymaps.length; ++i) { - this.keymaps[i].fromJsonObject( - jsonObject.keymaps[i], - abbrevation => this.getKeymap(abbrevation, true), - this.getMacro.bind(this) - ); - } + this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap)); this.epilogue = jsonObject.epilogue; return this; } @@ -66,10 +55,7 @@ export class UhkConfiguration extends Serializable { return new ModuleConfiguration().fromBinary(uhkBuffer); }); this.macros = buffer.readArray(uhkBuffer => new Macro().fromBinary(uhkBuffer)); - this.keymaps = []; - this.keymaps = buffer.readArray(uhkBuffer => { - return new Keymap().fromBinary(uhkBuffer, abbrevation => this.getKeymap(abbrevation, true), this.getMacro.bind(this)); - }); + this.keymaps = buffer.readArray(uhkBuffer => new Keymap().fromBinary(uhkBuffer)); this.epilogue = buffer.readUInt32(); return this; } @@ -104,14 +90,8 @@ export class UhkConfiguration extends Serializable { return ``; } - getKeymap(keymapAbbreviation: string, createIfNotExist = false): Keymap { - let resultKeymap = this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation); - if (createIfNotExist && !resultKeymap) { - resultKeymap = new Keymap(); - resultKeymap.abbreviation = keymapAbbreviation; - this.keymaps.push(resultKeymap); - } - return resultKeymap; + getKeymap(keymapAbbreviation: string): Keymap { + return this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation); } getMacro(macroId: number): Macro { diff --git a/src/config-serializer/config-items/key-action/PlayMacroAction.ts b/src/config-serializer/config-items/key-action/PlayMacroAction.ts index 1dc756d8..3d7baaca 100644 --- a/src/config-serializer/config-items/key-action/PlayMacroAction.ts +++ b/src/config-serializer/config-items/key-action/PlayMacroAction.ts @@ -1,10 +1,12 @@ +import { assertUInt8 } from '../../assert'; import { UhkBuffer } from '../../UhkBuffer'; import { Macro } from '../Macro'; import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; export class PlayMacroAction extends KeyAction { - macro: Macro; + @assertUInt8 + macroId: number; constructor(parameter?: PlayMacroAction | Macro) { super(); @@ -12,38 +14,37 @@ export class PlayMacroAction extends KeyAction { return; } if (parameter instanceof PlayMacroAction) { - this.macro = parameter.macro; + this.macroId = parameter.macroId; } else { - this.macro = parameter; + this.macroId = parameter.id; } } - fromJsonObject(jsonObject: any, getMacro: (macroId: number) => Macro): PlayMacroAction { + fromJsonObject(jsonObject: any): PlayMacroAction { this.assertKeyActionType(jsonObject); - this.macro = getMacro(jsonObject.macroId); + this.macroId = jsonObject.macroId; return this; } - fromBinary(buffer: UhkBuffer, getMacro: (macroId: number) => Macro): PlayMacroAction { + fromBinary(buffer: UhkBuffer): PlayMacroAction { this.readAndAssertKeyActionId(buffer); - const macroId = buffer.readUInt8(); - this.macro = getMacro(macroId); + this.macroId = buffer.readUInt8(); return this; } _toJsonObject(): any { return { keyActionType: keyActionType.PlayMacroAction, - macroId: this.macro.id + macroId: this.macroId }; } _toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.PlayMacroAction); - buffer.writeUInt8(this.macro.id); + buffer.writeUInt8(this.macroId); } toString(): string { - return ``; + return ``; } } diff --git a/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts b/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts index f0c6c3a8..aa98c08a 100644 --- a/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts +++ b/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts @@ -4,7 +4,7 @@ import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; export class SwitchKeymapAction extends KeyAction { - keymap: Keymap; + keymapAbbreviation: string; constructor(parameter?: SwitchKeymapAction | Keymap) { super(); @@ -12,38 +12,37 @@ export class SwitchKeymapAction extends KeyAction { return; } if (parameter instanceof SwitchKeymapAction) { - this.keymap = parameter.keymap; + this.keymapAbbreviation = parameter.keymapAbbreviation; } else { - this.keymap = parameter; + this.keymapAbbreviation = parameter.abbreviation; } } - fromJsonObject(jsonObject: any, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction { + fromJsonObject(jsonObject: any): SwitchKeymapAction { this.assertKeyActionType(jsonObject); - this.keymap = getKeymap(jsonObject.keymapAbbreviation); + this.keymapAbbreviation = jsonObject.keymapAbbreviation; return this; } - fromBinary(buffer: UhkBuffer, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction { + fromBinary(buffer: UhkBuffer): SwitchKeymapAction { this.readAndAssertKeyActionId(buffer); - const keymapAbbreviation = buffer.readString(); - this.keymap = getKeymap(keymapAbbreviation); + this.keymapAbbreviation = buffer.readString(); return this; } _toJsonObject(): any { return { keyActionType: keyActionType.SwitchKeymapAction, - keymapAbbreviation: this.keymap.abbreviation + keymapAbbreviation: this.keymapAbbreviation }; } _toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.SwitchKeymapAction); - buffer.writeString(this.keymap.abbreviation); + buffer.writeString(this.keymapAbbreviation); } toString(): string { - return ``; + return ``; } } diff --git a/src/config-serializer/config-items/key-action/helper.ts b/src/config-serializer/config-items/key-action/helper.ts index 1463b373..3c6d8deb 100644 --- a/src/config-serializer/config-items/key-action/helper.ts +++ b/src/config-serializer/config-items/key-action/helper.ts @@ -15,25 +15,17 @@ import { Macro } from '../Macro'; export class Helper { - static createKeyAction( - source: KeyAction | UhkBuffer | any, - getKeymap?: (abbrevation: string) => Keymap, - getMacro?: (macroId: number) => Macro - ): KeyAction { + static createKeyAction(source: KeyAction | UhkBuffer | any): KeyAction { if (source instanceof KeyAction) { return Helper.fromKeyAction(source); } else if (source instanceof UhkBuffer) { - return Helper.fromUhkBuffer(source, getKeymap, getMacro); + return Helper.fromUhkBuffer(source); } else { - return Helper.fromJSONObject(source, getKeymap, getMacro); + return Helper.fromJSONObject(source); } } - private static fromUhkBuffer( - buffer: UhkBuffer, - getKeymap?: (abbrevation: string) => Keymap, - getMacro?: (macroId: number) => Macro - ): KeyAction { + private static fromUhkBuffer(buffer: UhkBuffer): KeyAction { let keyActionFirstByte = buffer.readUInt8(); buffer.backtrack(); @@ -48,11 +40,11 @@ export class Helper { case KeyActionId.SwitchLayerAction: return new SwitchLayerAction().fromBinary(buffer); case KeyActionId.SwitchKeymapAction: - return new SwitchKeymapAction().fromBinary(buffer, getKeymap); + return new SwitchKeymapAction().fromBinary(buffer); case KeyActionId.MouseAction: return new MouseAction().fromBinary(buffer); case KeyActionId.PlayMacroAction: - return new PlayMacroAction().fromBinary(buffer, getMacro); + return new PlayMacroAction().fromBinary(buffer); default: throw `Invalid KeyAction first byte: ${keyActionFirstByte}`; } @@ -74,11 +66,7 @@ export class Helper { return newKeyAction; } - private static fromJSONObject( - keyAction: any, - getKeymap?: (abbrevation: string) => Keymap, - getMacro?: (macroId: number) => Macro - ): KeyAction { + private static fromJSONObject(keyAction: any): KeyAction { if (!keyAction) { return; } @@ -89,11 +77,11 @@ export class Helper { case keyActionType.SwitchLayerAction: return new SwitchLayerAction().fromJsonObject(keyAction); case keyActionType.SwitchKeymapAction: - return new SwitchKeymapAction().fromJsonObject(keyAction, getKeymap); + return new SwitchKeymapAction().fromJsonObject(keyAction); case keyActionType.MouseAction: return new MouseAction().fromJsonObject(keyAction); case keyActionType.PlayMacroAction: - return new PlayMacroAction().fromJsonObject(keyAction, getMacro); + return new PlayMacroAction().fromJsonObject(keyAction); default: throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`; } diff --git a/src/store/storage/index.ts b/src/store/storage/index.ts index 62513593..f3233184 100644 --- a/src/store/storage/index.ts +++ b/src/store/storage/index.ts @@ -83,14 +83,7 @@ export class DataStorage { initUHKJson() { this.uhkConfiguration = new UhkConfiguration().fromJsonObject(require('json!../../config-serializer/uhk-config.json')); this.uhkPresets = (require('json!../../config-serializer/preset-keymaps.json')) - /* TODO: Remove passing getters, because there shouldn't be any SwitchKeymapAction or PlayMacroAction in presets, - * so they shouldn't be needed. - */ - .map(keymap => new Keymap().fromJsonObject( - keymap, - this.uhkConfiguration.getKeymap.bind(this.uhkConfiguration), - this.uhkConfiguration.getMacro.bind(this.uhkConfiguration) - )); + .map(keymap => new Keymap().fromJsonObject(keymap)); } getConfiguration(): UhkConfiguration {