Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: duplicate macro item #2294

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[selectedMacroActionId]="selectedMacroActionIdModel"
(add)="addAction($event.macroId, $event.action)"
(edit)="editAction($event.macroId, $event.index, $event.action)"
(duplicate)="duplicateAction($event)"
(delete)="deleteAction($event.macroId, $event.index, $event.action)"
(reorder)="reorderAction($event.macroId, $event.macroActions)"
(selectedMacroActionChanged)="onSelectedMacroAction($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Observable, Subscription } from 'rxjs';

import {
AddMacroActionAction,
DuplicateMacroActionAction,
DeleteMacroActionAction,
ReorderMacroActionAction,
SaveMacroActionAction,
Expand All @@ -28,7 +29,7 @@ import {
} from '../../../store';
import { IOutputData, SplitComponent } from 'angular-split';

import { SelectedMacroAction, SelectedMacroActionIdModel } from '../../../models';
import { DuplicateMacroActionPayload, SelectedMacroAction, SelectedMacroActionIdModel } from '../../../models';
import { PanelSizeChangedAction, TogglePanelVisibilityAction } from '../../../store/actions/smart-macro-doc.action';

@Component({
Expand Down Expand Up @@ -124,6 +125,10 @@ export class MacroEditComponent implements OnDestroy {
this.store.dispatch(new DeleteMacroActionAction({ id: macroId, index, action }));
}

duplicateAction(payload: DuplicateMacroActionPayload) {
this.store.dispatch(new DuplicateMacroActionAction(payload));
}

reorderAction(macroId: number, macroActions: MacroAction[]) {
this.store.dispatch(new ReorderMacroActionAction({ id: macroId, macroActions }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</div>
<icon *ngIf="editable && macroAction && !editing" name="pencil" class="align-items-top"></icon>
</div>
<icon *ngIf="editable" name="copy" (click)="duplicateAction()" class="align-items-top"></icon>
<icon *ngIf="deletable" name="trash" (click)="deleteAction()" class="align-items-top"></icon>
</ng-container>

Expand All @@ -25,6 +26,7 @@
<span class="pe-1 me-auto">Command:</span>

<icon *ngIf="editable" name="pencil" (click)="editAction()"></icon>
<icon *ngIf="editable" name="copy" (click)="duplicateAction()"></icon>
<icon *ngIf="deletable" name="trash" (click)="deleteAction()"></icon>
</div>
<div class="macro-command-editor-container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
icon {
margin: 0 5px;

&[name='copy'],
&[name='trash'] {
padding-top: 0.25rem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class MacroItemComponent implements OnInit, OnChanges {
@Output() cancel = new EventEmitter<void>();
@Output() edit = new EventEmitter<void>();
@Output() delete = new EventEmitter<void>();
@Output() duplicate = new EventEmitter<void>();
@Output() selected = new EventEmitter<SelectedMacroItem>();

title: string;
Expand Down Expand Up @@ -125,6 +126,10 @@ export class MacroItemComponent implements OnInit, OnChanges {
this.delete.emit();
}

duplicateAction(): void {
this.duplicate.emit();
}

macroEditorGotFocus():void {
this.selected.emit({
inlineEdit: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[movable]="true"
(save)="saveAction($event, macroActionIndex)"
(edit)="editAction(macroActionIndex)"
(duplicate)="duplicateAction(macroActionIndex)"
(cancel)="cancelAction()"
(delete)="deleteAction(macroAction, macroActionIndex)"
(selected)="onSelectedMacroAction(macroActionIndex, $event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { KeyMacroAction, KeystrokeAction, Macro, MacroAction, MacroKeySubAction

import { mapLeftRightModifierToKeyActionModifier } from '../../../util';
import { KeyCaptureData } from '../../../models/svg-key-events';
import { SelectedMacroAction, SelectedMacroActionId, SelectedMacroActionIdModel, SelectedMacroItem } from '../../../models';
import { DuplicateMacroActionPayload, SelectedMacroAction, SelectedMacroActionId, SelectedMacroActionIdModel, SelectedMacroItem } from '../../../models';

const ANIMATION_TIME = 500;
const ANIMATION_INTERVAL = 5;
Expand Down Expand Up @@ -68,6 +68,7 @@ export class MacroListComponent implements AfterViewChecked, OnDestroy {
@Input() selectedMacroActionId: SelectedMacroActionIdModel;

@Output() add = new EventEmitter();
@Output() duplicate = new EventEmitter<DuplicateMacroActionPayload>();
@Output() edit = new EventEmitter();
@Output() delete = new EventEmitter();
@Output() reorder = new EventEmitter();
Expand Down Expand Up @@ -144,6 +145,13 @@ export class MacroListComponent implements AfterViewChecked, OnDestroy {
this.newMacro = undefined;
}

duplicateAction(index: number) {
this.duplicate.emit({
macroId: this.macro.id,
actionId: index,
});
}

editAction(index: number) {
this.selectedMacroActionIdChanged.emit({ id: index });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IconDefinition } from '@fortawesome/fontawesome-common-types';
import {
faCode,
faClock,
faCopy,
faFont,
faGripLinesVertical,
faHandPaper,
Expand Down Expand Up @@ -44,6 +45,11 @@ export class IconComponent implements OnChanges {
this.icon = faCode;
break;

case 'copy':
this.icon = faCopy;
this.css = 'action--edit';
break;

case 'clock':
this.icon = faClock;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface DuplicateMacroActionPayload {
macroId: number;
actionId: number;
}
1 change: 1 addition & 0 deletions packages/uhk-web/src/app/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './apply-user-configuration-from-file-payload';
export * from './backlighting-option';
export * from './config-size-state';
export * from './device-ui-states';
export * from './duplicate-macro-action-payload';
export * from './exchange-keys-action.model';
export * from './firmware-upgrade-state';
export * from './firmware-upgrade-steps';
Expand Down
12 changes: 11 additions & 1 deletion packages/uhk-web/src/app/store/actions/macro.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Action } from '@ngrx/store';
import { Macro, MacroAction as ConfigItemMacroAction } from 'uhk-common';

import { SelectedMacroAction } from '../../models';
import { DuplicateMacroActionPayload, SelectedMacroAction } from '../../models';

export enum ActionTypes {
Duplicate = '[Macro] Duplicate macro',
Expand All @@ -11,6 +11,7 @@ export enum ActionTypes {
Select = '[Macro] Select macro',

AddAction = '[Macro] Add macro action',
DuplicateAction = '[Macro] Duplicate macro action',
SaveAction = '[Macro] Save macro action',
DeleteAction = '[Macro] Delete macro action',
ReorderAction = '[Macro] Reorder macro action',
Expand Down Expand Up @@ -56,6 +57,14 @@ export class AddMacroActionAction implements Action {
}
}

export class DuplicateMacroActionAction implements Action {
type = ActionTypes.DuplicateAction;

constructor(public payload: DuplicateMacroActionPayload) {
}
}


export class SaveMacroActionAction implements Action {
type = ActionTypes.SaveAction;

Expand Down Expand Up @@ -91,6 +100,7 @@ export type Actions
| EditMacroNameAction
| SelectMacroAction
| AddMacroActionAction
| DuplicateMacroActionAction
| SaveMacroActionAction
| DeleteMacroActionAction
| SelectMacroActionAction
Expand Down
2 changes: 1 addition & 1 deletion packages/uhk-web/src/app/store/effects/user-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class UserConfigEffects {
Keymaps.ActionTypes.AddLayer, Keymaps.ActionTypes.RemoveLayer, Keymaps.ActionTypes.SetKeyColor,
Macros.ActionTypes.Add, Macros.ActionTypes.Duplicate, Macros.ActionTypes.EditName, Macros.ActionTypes.Remove,
Macros.ActionTypes.AddAction, Macros.ActionTypes.SaveAction, Macros.ActionTypes.DeleteAction,
Macros.ActionTypes.ReorderAction,
Macros.ActionTypes.ReorderAction, Macros.ActionTypes.DuplicateAction,
ActionTypes.RenameUserConfiguration, ActionTypes.SetUserConfigurationValue, ActionTypes.SetUserConfigurationRgbValue,
ActionTypes.RecoverLEDSpaces, ActionTypes.SetModuleConfigurationValue
),
Expand Down
26 changes: 26 additions & 0 deletions packages/uhk-web/src/app/store/reducers/user-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
LayerName,
LeftSlotModules,
Macro,
MacroActionHelper,
Module,
ModuleConfiguration,
MODIFIER_LAYER_NAMES,
Expand Down Expand Up @@ -728,6 +729,31 @@ export function reducer(
};
}

case MacroActions.ActionTypes.DuplicateAction: {
const payload = (action as MacroActions.DuplicateMacroActionAction).payload;
const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration);

userConfiguration.macros = state.userConfiguration.macros.map((macro: Macro) => {
if (macro.id === payload.macroId) {
macro = new Macro(macro);
const cloned = MacroActionHelper.fromMacroAction(macro.macroActions[payload.actionId]);

macro.macroActions = [
...macro.macroActions.slice(0, payload.actionId),
cloned,
...macro.macroActions.slice(payload.actionId)
];
}

return macro;
});

return {
...state,
userConfiguration
};
}

case MacroActions.ActionTypes.SaveAction: {
const payload = (action as MacroActions.SaveMacroActionAction).payload;
const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration);
Expand Down