import { AfterViewInit , ChangeDetectorRef , Component , EventEmitter , HostListener , Input , OnChanges , OnInit , Output , ViewChild } from '@angular/core' ; import { faCode , faClock , faFont , faKeyboard , faMousePointer } from '@fortawesome/free-solid-svg-icons' ; import { CommandMacroAction , MacroAction , DelayMacroAction , KeyMacroAction , ScrollMouseMacroAction , MoveMouseMacroAction , MouseButtonMacroAction , TextMacroAction , MacroActionHelper } from 'uhk-common' ; import { MacroDelayTabComponent , MacroMouseTabComponent , MacroKeyTabComponent , MacroTextTabComponent } from './tab' ; enum TabName { Keypress , Text , Mouse , Delay , Command } @ Component ( { selector : 'macro-action-editor' , templateUrl : './macro-action-editor.component.html' , styleUrls : [ './macro-action-editor.component.scss' ] , host : { 'class' : 'macro-action-editor' } } ) export class MacroActionEditorComponent implements AfterViewInit , OnInit , OnChanges { @ Input ( ) macroAction : MacroAction ; @ Output ( ) save = new EventEmitter < MacroAction > ( ) ; @ Output ( ) cancel = new EventEmitter < void > ( ) ; @ ViewChild ( 'tab' , { static : false } ) selectedTab : MacroTextTabComponent | MacroKeyTabComponent | MacroMouseTabComponent | MacroDelayTabComponent ; editableMacroAction : MacroAction ; activeTab : TabName ; TabName = TabName ; isSelectedMacroValid = false ; faFont = faFont ; faKeyboard = faKeyboard ; faMousePointer = faMousePointer ; faClock = faClock ; faCode = faCode ; constructor ( private _cdRef ) { } ngAfterViewInit ( ) { const isValid = this . selectedTab && this . selectedTab . isMacroValid ( ) ; if ( isValid !== this . isSelectedMacroValid ) { this . isSelectedMacroValid = isValid ; this . _cdRef . detectChanges ( ) ; } } ngOnInit ( ) { this . updateEditableMacroAction ( ) ; const tab = this . getTabName ( this . editableMacroAction ) ; this . activeTab = tab ; } ngOnChanges ( ) { this . ngOnInit ( ) ; } onCancelClick ( ) { this . cancel . emit ( ) ; } @ HostListener ( 'document:keydown.control.enter' , [ '$event' ] ) onKeyDown ( event ) { if ( this . isSelectedMacroValid ) { this . onSaveClick ( ) ; event . preventDefault ( ) ; } } onSaveClick ( ) { try { const action = this . selectedTab instanceof MacroKeyTabComponent ? this . selectedTab . getKeyMacroAction ( ) : this . selectedTab . macroAction ; this . save . emit ( action ) ; } catch ( e ) { console . error ( e ) ; } } onValid ( isMacroValid ) { if ( isMacroValid === this . isSelectedMacroValid ) { return this . isSelectedMacroValid ; } this . isSelectedMacroValid = isMacroValid ; this . _cdRef . detectChanges ( ) ; return this . isSelectedMacroValid ; } selectTab ( tab ) { this . activeTab = tab ; if ( tab === this . getTabName ( this . macroAction ) ) { this . updateEditableMacroAction ( ) ; } else { this . editableMacroAction = undefined ; this . isSelectedMacroValid = false ; this . _cdRef . detectChanges ( ) ; } } getTabName ( action ) { if ( action instanceof DelayMacroAction ) { return TabName . Delay ; } else if ( action instanceof TextMacroAction ) { return TabName . Text ; } else if ( action instanceof KeyMacroAction ) { return TabName . Keypress ; } else if ( action instanceof MouseButtonMacroAction || action instanceof MoveMouseMacroAction || action instanceof ScrollMouseMacroAction ) { return TabName . Mouse ; } else if ( action instanceof CommandMacroAction ) { return TabName . Command ; } return undefined ; } private updateEditableMacroAction ( ) { const macroAction = this . macroAction ? this . macroAction : new TextMacroAction ( ) ; this . editableMacroAction = MacroActionHelper . createMacroAction ( macroAction ) ; } }