import { MediaHelpers } from './../helpers/MediaHelpers' ; import { DashboardMessage } from "../dashboardWebView/DashboardMessage" ; import { BaseListener } from "./BaseListener" ; import { DashboardCommand } from '../dashboardWebView/DashboardCommand' ; import { SortingOption } from '../dashboardWebView/models' ; import { commands } from 'vscode' ; import { COMMAND_NAME } from '../constants' ; export class MediaListener extends BaseListener { private static timers : { [ folder ] : any ; } = { } ; public static async process ( msg ) { super . process ( msg ) ; switch ( msg . command ) { case DashboardMessage . getMedia : const { page , folder , sorting } = msg ?. data ; this . sendMediaFiles ( page , folder , sorting ) ; break ; case DashboardMessage . refreshMedia : MediaHelpers . resetMedia ( ) ; this . sendMediaFiles ( 0 , msg ?. data ?. folder ) ; break ; case DashboardMessage . uploadMedia : this . store ( msg ?. data ) ; break ; case DashboardMessage . deleteMedia : this . delete ( msg ?. data ) ; break ; case DashboardMessage . insertPreviewImage : MediaHelpers . insertMediaToMarkdown ( msg ?. data ) ; break ; case DashboardMessage . updateMediaMetadata : this . update ( msg . data ) ; break ; case DashboardMessage . createMediaFolder : await commands . executeCommand ( COMMAND_NAME . createFolder , msg ?. data ) ; break ; } } param page param folder param sorting public static async sendMediaFiles ( page = 0 , folder = '' , sorting = null ) { const files = await MediaHelpers . getMedia ( page , folder , sorting ) ; this . sendMsg ( DashboardCommand . media , files ) ; } param data private static async store ( data ) { try { const { folder } = data ; await MediaHelpers . saveFile ( data ) ; const folderPath = `${ folder }` ; if ( this . timers [ folderPath ] ) { clearTimeout ( this . timers [ folderPath ] ) ; delete this . timers [ folderPath ] ; } this . timers [ folderPath ] = setTimeout ( ( ) => { MediaHelpers . resetMedia ( ) ; this . sendMediaFiles ( 0 , folder || "" ) ; delete this . timers [ folderPath ] ; } , 500 ) ; } catch { } } param data private static delete ( data ) { try { MediaHelpers . deleteFile ( data ) ; this . sendMediaFiles ( data . page || 0 , data . folder || "" ) ; } catch { } } param data private static update ( data ) { try { const { page , folder } = data ; MediaHelpers . updateMetadata ( data ) ; this . sendMediaFiles ( page || 0 , folder || "" ) ; } catch { } } }