committed by
József Farkas
parent
01214e58b5
commit
f2ce7398d0
@@ -2,6 +2,7 @@ import { Action } from '@ngrx/store';
|
|||||||
|
|
||||||
import { KeyAction } from '../../config-serializer/config-items/key-action';
|
import { KeyAction } from '../../config-serializer/config-items/key-action';
|
||||||
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
||||||
|
import { Macro } from '../../config-serializer/config-items/Macro';
|
||||||
|
|
||||||
export namespace KeymapActions {
|
export namespace KeymapActions {
|
||||||
export const PREFIX = '[Keymap] ';
|
export const PREFIX = '[Keymap] ';
|
||||||
@@ -12,6 +13,7 @@ export namespace KeymapActions {
|
|||||||
export const SAVE_KEY = KeymapActions.PREFIX + 'Save key action';
|
export const SAVE_KEY = KeymapActions.PREFIX + 'Save key action';
|
||||||
export const SET_DEFAULT = KeymapActions.PREFIX + 'Set default option';
|
export const SET_DEFAULT = KeymapActions.PREFIX + 'Set default option';
|
||||||
export const REMOVE = KeymapActions.PREFIX + 'Remove keymap';
|
export const REMOVE = KeymapActions.PREFIX + 'Remove keymap';
|
||||||
|
export const CHECK_MACRO = KeymapActions.PREFIX + 'Check deleted macro';
|
||||||
|
|
||||||
export function addKeymap(item: Keymap): Action {
|
export function addKeymap(item: Keymap): Action {
|
||||||
return {
|
return {
|
||||||
@@ -73,4 +75,11 @@ export namespace KeymapActions {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function checkMacro(macro: Macro): Action {
|
||||||
|
return {
|
||||||
|
type: KeymapActions.CHECK_MACRO,
|
||||||
|
payload: macro
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { Actions, Effect } from '@ngrx/effects';
|
|||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
|
|
||||||
import 'rxjs/add/operator/do';
|
import 'rxjs/add/operator/do';
|
||||||
|
import 'rxjs/add/operator/map';
|
||||||
import 'rxjs/add/operator/withLatestFrom';
|
import 'rxjs/add/operator/withLatestFrom';
|
||||||
|
|
||||||
import { KeymapActions } from '../actions';
|
import { KeymapActions } from '../actions';
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import { Actions, Effect } from '@ngrx/effects';
|
|||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
|
|
||||||
import 'rxjs/add/operator/do';
|
import 'rxjs/add/operator/do';
|
||||||
|
import 'rxjs/add/operator/map';
|
||||||
import 'rxjs/add/operator/withLatestFrom';
|
import 'rxjs/add/operator/withLatestFrom';
|
||||||
|
|
||||||
import { Macro } from '../../config-serializer/config-items/Macro';
|
import { Macro } from '../../config-serializer/config-items/Macro';
|
||||||
|
|
||||||
import { MacroActions } from '../actions';
|
import { KeymapActions, MacroActions } from '../actions';
|
||||||
import { AppState } from '../index';
|
import { AppState } from '../index';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -17,14 +18,16 @@ export class MacroEffects {
|
|||||||
|
|
||||||
@Effect({dispatch: false}) remove$: any = this.actions$
|
@Effect({dispatch: false}) remove$: any = this.actions$
|
||||||
.ofType(MacroActions.REMOVE)
|
.ofType(MacroActions.REMOVE)
|
||||||
|
.map(action => this.store.dispatch(KeymapActions.checkMacro(action.payload)))
|
||||||
.withLatestFrom(this.store)
|
.withLatestFrom(this.store)
|
||||||
.do((latest) => {
|
.do((latest) => {
|
||||||
const state: AppState = latest[1];
|
const state: AppState = latest[1];
|
||||||
|
const macro: Macro[] = state.macros.entities;
|
||||||
|
|
||||||
if (state.macros.entities.length === 0) {
|
if (state.macros.entities.length === 0) {
|
||||||
this.router.navigate(['/macro/add']);
|
this.router.navigate(['/macro/add']);
|
||||||
} else {
|
} else {
|
||||||
this.router.navigate(['/macro', '0']);
|
this.router.navigate(['/macro', macro[0].id]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Action } from '@ngrx/store';
|
|||||||
import 'rxjs/add/operator/map';
|
import 'rxjs/add/operator/map';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
|
||||||
import { Helper as KeyActionHelper } from '../../config-serializer/config-items/key-action';
|
import { Helper as KeyActionHelper, KeyAction } from '../../config-serializer/config-items/key-action';
|
||||||
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
import { Keymap } from '../../config-serializer/config-items/Keymap';
|
||||||
import { Layer } from '../../config-serializer/config-items/Layer';
|
import { Layer } from '../../config-serializer/config-items/Layer';
|
||||||
import { Module } from '../../config-serializer/config-items/Module';
|
import { Module } from '../../config-serializer/config-items/Module';
|
||||||
@@ -17,6 +17,7 @@ const initialState: KeymapState = {
|
|||||||
|
|
||||||
export default function (state = initialState, action: Action): KeymapState {
|
export default function (state = initialState, action: Action): KeymapState {
|
||||||
let newState: Keymap[];
|
let newState: Keymap[];
|
||||||
|
let changedKeymap: Keymap = new Keymap();
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case KeymapActions.ADD:
|
case KeymapActions.ADD:
|
||||||
@@ -89,22 +90,29 @@ export default function (state = initialState, action: Action): KeymapState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// If deleted one is default set default keymap to the first on the list of keymaps
|
// If deleted one is default set default keymap to the first on the list of keymaps
|
||||||
if (isDefault && filtered.length > 0) {
|
if (isDefault && filtered.length > 0) {
|
||||||
filtered[0].isDefault = true;
|
filtered[0].isDefault = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for the deleted keymap in other keymaps
|
||||||
|
newState = filtered.map((keymap: Keymap) => {
|
||||||
|
changedKeymap = new Keymap();
|
||||||
|
Object.assign(changedKeymap, keymap);
|
||||||
|
changedKeymap.layers = checkExistence(changedKeymap.layers, 'keymapAbbreviation', action.payload);
|
||||||
|
|
||||||
|
return changedKeymap;
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
entities: filtered
|
entities: newState
|
||||||
};
|
};
|
||||||
|
|
||||||
case KeymapActions.SAVE_KEY:
|
case KeymapActions.SAVE_KEY:
|
||||||
|
|
||||||
const keymap: Keymap = action.payload.keymap;
|
const keymap: Keymap = action.payload.keymap;
|
||||||
const changedKeymap: Keymap = new Keymap();
|
|
||||||
Object.assign(changedKeymap, keymap);
|
Object.assign(changedKeymap, keymap);
|
||||||
|
|
||||||
const layerIndex: number = action.payload.layer;
|
const layerIndex: number = action.payload.layer;
|
||||||
@@ -134,6 +142,19 @@ export default function (state = initialState, action: Action): KeymapState {
|
|||||||
entities: newState
|
entities: newState
|
||||||
};
|
};
|
||||||
|
|
||||||
|
case KeymapActions.CHECK_MACRO:
|
||||||
|
newState = state.entities.map((keymap: Keymap) => {
|
||||||
|
changedKeymap = new Keymap();
|
||||||
|
Object.assign(changedKeymap, keymap);
|
||||||
|
changedKeymap.layers = checkExistence(changedKeymap.layers, '_macroId', action.payload);
|
||||||
|
|
||||||
|
return changedKeymap;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
entities: newState
|
||||||
|
};
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
@@ -188,3 +209,23 @@ function generateName(keymaps: Keymap[], name: string) {
|
|||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkExistence(layers: Layer[], property: string, value: string | number) {
|
||||||
|
let newLayers = layers.map((layer) => {
|
||||||
|
let newLayer = new Layer(layer);
|
||||||
|
|
||||||
|
newLayer.modules = layer.modules.map((module: Module) => {
|
||||||
|
module.keyActions.forEach((action: KeyAction, index: number) => {
|
||||||
|
if (action && action.hasOwnProperty(property) && action[property] === value) {
|
||||||
|
module.keyActions[index] = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return module;
|
||||||
|
});
|
||||||
|
|
||||||
|
return newLayer;
|
||||||
|
});
|
||||||
|
|
||||||
|
return newLayers;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user