import { IDragAndDrop , ITree , IDragAndDropData , DragMouseEvent , IDragOverReaction , DragOverBubble } from "./monaco-extra" ; import { File , Directory } from "./models" ; import { uploadFilesToDirectory , isUploadAllowedForMimeType } from "./util" ; export class DragAndDrop implements IDragAndDrop { private target : any ; constructor ( target ) { this . target = target ; } /**
     * Returns a uri if the given element should be allowed to drag.
     * Returns null, otherwise.
     */ getDragURI ( tree , element ) { return element . key ; } /**
     * Returns a label to display when dragging the element.
     */ getDragLabel ? ( tree , elements ) { return elements [ 0 ] . name ; } /**
     * Sent when the drag operation is starting.
     */ onDragStart ( tree , data , originalEvent ) { return ; } /**
     * Returns a DragOverReaction indicating whether sources can be
     * dropped into target or some parent of the target.
     */ onDragOver ( tree , data , targetElement , originalEvent ) { if ( ! ( data as any ) . elements ) { const items = Array . from ( originalEvent . browserEvent . dataTransfer . items ) ; const hasItemsUriListType = items . some ( item => item . type === "text/uri-list" ) ; if ( items . length && ! hasItemsUriListType ) { return { accept : items . every ( item => isUploadAllowedForMimeType ( item . type ) ) , bubble : DragOverBubble . BUBBLE_DOWN , autoExpand : true } ; } } const file = data . elements [ 0 ] ; return { accept : targetElement instanceof Directory && targetElement !== file && ! targetElement . isDescendantOf ( file ) , bubble : DragOverBubble . BUBBLE_DOWN , autoExpand : true } ; } /**
     * Handles the action of dropping sources into target.
     */ async drop ( tree , data , targetElement , originalEvent ) { const items = originalEvent . browserEvent . dataTransfer . items ; if ( ! ( data as any ) . elements && items . length ) { await uploadFilesToDirectory ( items , targetElement as Directory ) ; return ; } const file = data . elements [ 0 ] ; return this . target . props . onMoveFile && this . target . props . onMoveFile ( file , targetElement as Directory ) ; } }