import { DraftHandleValue , EditorState , SelectionState } from 'draft-js' ; import { PluginFunctions } from '@draft-js-plugins/editor' ; import { DndUploadPluginConfig } from '.' ; import { readFiles } from './utils/file' ; export default function onDropFile ( config ) { return function onDropFileInner ( selection , files , { getEditorState , setEditorState } ) : DraftHandleValue { const { handleUpload } = config ; if ( handleUpload ) { const formData = new FormData ( ) ; const data = { files : [ ] , formData , } ; for ( const file of files ) { if ( file && file instanceof File ) { data . formData . append ( 'files' , file ) ; data . files . push ( file ) ; } } setEditorState ( EditorState . acceptSelection ( getEditorState ( ) , selection ) ) ; readFiles ( data . files ) . then ( ( placeholders ) => { let editorState = getEditorState ( ) ; placeholders . forEach ( ( placeholder ) => { if ( config . addImage ) { editorState = config . addImage ( editorState , placeholder . src ) ; } } ) ; setEditorState ( editorState ) ; } ) ; return 'handled' ; } return 'not-handled' ; } ; }