import { Component , EventEmitter , Input , NgZone , OnDestroy , OnInit , Output , ViewEncapsulation } from '@angular/core' ; import { remote } from 'electron' ; import { Subject , Subscription } from 'rxjs' ; import { Constants } from '../../core/constants' ; import { SelectionWatcher } from '../../core/selection-watcher' ; import { Settings } from '../../core/settings' ; import { Note } from '../../data/entities/note' ; import { Notebook } from '../../data/entities/notebook' ; import { CollectionService } from '../../services/collection/collection.service' ; import { FileService } from '../../services/file/file.service' ; import { NoteMarkResult } from '../../services/results/note-mark-result' ; import { SearchService } from '../../services/search/search.service' ; import { SnackBarService } from '../../services/snack-bar/snack-bar.service' ; @ Component ( { selector : 'app-notes' , templateUrl : './notes.component.html' , styleUrls : [ './notes.component.scss' ] , encapsulation : ViewEncapsulation . None , } ) export class NotesComponent implements OnInit , OnDestroy { private globalEmitter : any = remote . getGlobal ( 'globalEmitter' ) ; private subscription : Subscription ; private readonly destroy$ : Subject < void > = new Subject ( ) ; private _activeNotebook : Notebook ; private selectionWatcher : SelectionWatcher = new SelectionWatcher ( ) ; constructor ( private collection , private snackBar , public search , private settings , private file , private zone ) { } @ Input ( ) public categoryChangedSubject : Subject < string > ; @ Input ( ) public componentCategory : string ; public selectedCategory : string = Constants . allCategory ; public get activeNotebook ( ) : Notebook { return this . _activeNotebook ; } @ Input ( ) public set activeNotebook ( val ) { this . _activeNotebook = val ; this . getNotes ( ) ; } @ Output ( ) public notesCount : EventEmitter < number > = new EventEmitter < number > ( ) ; @ Output ( ) public selectedNoteIds : EventEmitter < string [ ] > = new EventEmitter < string [ ] > ( ) ; public notes : Note [ ] = [ ] ; public draggableNoteIds : string [ ] = [ ] ; public draggedNote : Note ; public ngOnDestroy ( ) { this . subscription . unsubscribe ( ) ; this . destroy$ . next ( ) ; this . destroy$ . complete ( ) ; } public async ngOnInit ( ) { await this . collection . initializeAsync ( ) ; this . subscription = this . collection . noteEdited$ . subscribe ( ( ) => this . getNotes ( ) ) ; this . subscription . add ( this . collection . notesChanged$ . subscribe ( ( ) => this . getNotes ( ) ) ) ; this . subscription . add ( this . collection . noteNotebookChanged$ . subscribe ( ( ) => this . getNotes ( ) ) ) ; this . subscription . add ( this . search . searchTextChanged$ . subscribe ( ( _ ) => this . getNotes ( ) ) ) ; this . subscription . add ( this . collection . noteMarkChanged$ . subscribe ( ( result ) => { if ( this . componentCategory === Constants . markedCategory ) { this . getNotes ( ) ; } else { this . markNote ( result ) ; } } ) ) ; this . subscription . add ( this . categoryChangedSubject . subscribe ( async ( selectedCategory ) => { this . selectedCategory = selectedCategory ; this . getNotes ( ) ; } ) ) ; } public setSelectedNotes ( note , event = null ) { if ( event && event . ctrlKey ) { this . selectionWatcher . toggleItemSelection ( note ) ; } else if ( event && event . shiftKey ) { this . selectionWatcher . selectItemsRange ( note ) ; } else { this . selectionWatcher . selectSingleItem ( note ) ; } this . selectedNoteIds . next ( this . getSelectedNoteIds ( ) ) ; } public openNote ( note ) { if ( ! this . collection . noteIsOpen ( note . id ) ) { this . globalEmitter . emit ( Constants . setNoteOpenEvent , note . id , true ) ; } else { this . globalEmitter . emit ( Constants . focusNoteEvent , note . id ) ; } } public toggleNoteMark ( note ) { this . collection . setNoteMark ( note . id , ! note . isMarked ) ; } private markNote ( result ) { if ( this . notes . length > 0 ) { const noteToMark = this . notes . find ( ( x ) => x . id === result . noteId ) ; if ( noteToMark ) { noteToMark . isMarked = result . isMarked ; } } } private getNotes ( ) { if ( this . componentCategory !== this . selectedCategory ) { return ; } if ( this . activeNotebook ) { this . zone . run ( async ( ) => { this . notes = await this . collection . getNotesAsync ( this . activeNotebook . id , this . componentCategory , this . settings . showExactDatesInTheNotesList ) ; this . selectionWatcher . reset ( this . notes ) ; this . notesCount . emit ( this . notes . length ) ; this . selectedNoteIds . next ( this . getSelectedNoteIds ( ) ) ; } ) ; } } public getSelectedNoteIds ( ) { return this . notes . filter ( ( x ) => x . isSelected ) . map ( ( x ) => x . id ) ; } public dragStart ( event , draggedNote ) { this . draggedNote = draggedNote ; this . draggableNoteIds = this . getSelectedNoteIds ( ) ; if ( ! this . draggableNoteIds . includes ( draggedNote . id ) ) { this . draggableNoteIds . push ( draggedNote . id ) ; } event . dataTransfer . setDragImage ( document . getElementById ( 'drag-image' ) , - 10 , - 10 ) ; event . dataTransfer . setData ( 'text' , JSON . stringify ( this . draggableNoteIds ) ) ; } }