import { Injectable , NgZone , OnDestroy } from '@angular/core' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { IpcActionClient , IpcMessage } from '../../libs/ipc' ; import { enterZone } from '../../libs/rx' ; export enum MenuEvent { NEW_TEXT_SNIPPET = '[MenuEvent] New text snippet' , NEW_CODE_SNIPPET = '[MenuEvent] New code snippet' , INSERT_IMAGE = '[MenuEvent] Insert image' , TOGGLE_NOTE_LIST = '[MenuEvent] Toggle note list' , TOGGLE_VCS = '[MenuEvent] Toggle vcs' , CHANGE_EDITOR_VIEW_MODE_TO_EDITOR_ONLY = '[MenuEvent] Change editor view mode to editor only' , CHANGE_EDITOR_VIEW_MODE_TO_PREVIEW_ONLY = '[MenuEvent] Change editor view mode to preview only' , CHANGE_EDITOR_VIEW_MODE_TO_SHOW_BOTH = '[MenuEvent] Change editor view mode to show both' , OPEN_SETTINGS = '[MenuEvent] Open settings' , COMMIT_NOTE = '[MenuEvent] Commit note' } @ Injectable ( ) export class MenuService implements OnDestroy { private ipcClient = new IpcActionClient ( 'menu' ) ; constructor ( private ngZone ) { } ngOnDestroy ( ) { this . ipcClient . destroy ( ) ; } updateNoteEditorViewMenuState ( activeMode ) { this . ipcClient . performAction < string , void > ( 'updateNoteEditorViewMenuState' , activeMode ) ; } onMessage ( ) { const parseMessage = ( message ) : MenuEvent => { switch ( message . name ) { case 'newTextSnippet' : return MenuEvent . NEW_TEXT_SNIPPET ; case 'newCodeSnippet' : return MenuEvent . NEW_CODE_SNIPPET ; case 'insertImage' : return MenuEvent . INSERT_IMAGE ; case 'toggleNoteList' : return MenuEvent . TOGGLE_NOTE_LIST ; case 'toggleVcs' : return MenuEvent . TOGGLE_VCS ; case 'changeEditorViewModeToEditorOnly' : return MenuEvent . CHANGE_EDITOR_VIEW_MODE_TO_EDITOR_ONLY ; case 'changeEditorViewModeToPreviewOnly' : return MenuEvent . CHANGE_EDITOR_VIEW_MODE_TO_PREVIEW_ONLY ; case 'changeEditorViewModeToShowBoth' : return MenuEvent . CHANGE_EDITOR_VIEW_MODE_TO_SHOW_BOTH ; case 'showSettings' : return MenuEvent . OPEN_SETTINGS ; case 'commitNote' : return MenuEvent . COMMIT_NOTE ; default : return null ; } } ; return this . ipcClient . onMessage < any > ( ) . pipe ( enterZone ( this . ngZone ) , map ( parseMessage ) ) ; } }