dropperx
Version:
File drop target
1 lines • 3.6 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../src/index.js"],"sourcesContent":["import React from 'react'\nimport Dropzone from 'react-dropzone'\n\nconst DATA_URL = 'DATA_URL'\nconst TEXT = 'TEXT'\n\nconst style = { outline: 'none' }\n\nfunction readAs(file) {\n if (file.type === '') return DATA_URL\n\n const mimeStart = file.type.split('/')[0]\n\n switch (mimeStart) {\n case 'application':\n case 'text':\n return TEXT\n case 'image':\n case 'video':\n return DATA_URL\n default:\n return TEXT\n }\n}\n\nclass Dropperx extends React.Component {\n state = {\n lastContent: null,\n history: []\n }\n\n handleDrop = files => {\n if (!files[0]) return\n\n const reader = new FileReader()\n Promise.all(\n files.filter(this.props.filter).map(\n file =>\n new Promise(resolve => {\n reader.onload = event => {\n file.content = event.target.result\n resolve(file)\n }\n\n const type = readAs(file)\n\n if (type === DATA_URL) {\n reader.readAsDataURL(file)\n } else {\n reader.readAsText(file, 'UTF-8')\n }\n })\n )\n ).then(files => {\n this.setState(state => ({\n lastContent: files,\n history: [files, ...state.history]\n }))\n this.props.onDrop(files)\n })\n }\n\n render() {\n return (\n <Dropzone\n onDrop={this.handleDrop}\n accept={this.props.accept}\n minSize={this.props.minSize}\n maxSize={this.props.maxSize}\n >\n {({ getRootProps, isDragActive, isDragAccept }) => {\n const rootProps = getRootProps()\n return (\n <div {...rootProps} style={style}>\n {this.props.children({\n isOver: isDragActive,\n canDrop: isDragAccept,\n files: this.state.lastContent,\n history: this.state.history\n })}\n </div>\n )\n }}\n </Dropzone>\n )\n }\n}\n\nDropperx.defaultProps = {\n filter: i => i,\n onDrop: () => {}\n}\n\nexport default Dropperx\n"],"names":["const","DATA_URL","TEXT","style","outline","Dropperx","state","lastContent","history","handleDrop","files","reader","FileReader","Promise","all","filter","this","props","map","file","resolve","onload","event","content","target","result","type","split","readAs","readAsDataURL","readAsText","then","setState","onDrop","render","React","Dropzone","accept","minSize","maxSize","ref","rootProps","getRootProps","children","isOver","isDragActive","canDrop","isDragAccept","Component","defaultProps","i"],"mappings":"mDAGAA,IAAMC,EAAW,WACXC,EAAO,OAEPC,EAAQ,CAAEC,QAAS,QAmBzB,IAAMC,8GACJC,MAAQ,CACNC,YAAa,KACbC,QAAS,SAGXC,oBAAaC,MACNA,EAAM,QAELC,EAAS,IAAIC,WACnBC,QAAQC,IACNJ,EAAMK,OAAOC,EAAKC,MAAMF,QAAQG,aAC9BC,UACE,IAAIN,iBAAQO,GACVT,EAAOU,gBAASC,GACdH,EAAKI,QAAUD,EAAME,OAAOC,OAC5BL,EAAQD,IAjCtB,SAAgBA,MACI,KAAdA,EAAKO,KAAa,OAAOzB,SAEXkB,EAAKO,KAAKC,MAAM,KAAK,QAGhC,kBACA,cACIzB,MACJ,YACA,eACID,iBAEAC,GAuBY0B,CAAOT,KAEPlB,EACXU,EAAOkB,cAAcV,GAErBR,EAAOmB,WAAWX,EAAM,cAIhCY,cAAKrB,KACAsB,kBAAS1B,UACZC,YAAaG,EACbF,QAAS,CAACE,UAAUJ,EAAME,cAEvBS,MAAMgB,OAAOvB,uGAItBwB,oCAEIC,gBAACC,GACCH,OAAQjB,KAAKP,WACb4B,OAAQrB,KAAKC,MAAMoB,OACnBC,QAAStB,KAAKC,MAAMqB,QACpBC,QAASvB,KAAKC,MAAMsB,SAEpB,SAAEC,yCACMC,GAAYC,2BAEhBP,uCAASM,GAAWtC,MAAOA,IACxBa,EAAKC,MAAM0B,SAAS,CACnBC,OAAQC,EACRC,QAASC,EACTrC,MAAOM,EAAKV,MAAMC,YAClBC,QAASQ,EAAKV,MAAME,iBArDb2B,EAAMa,WA+D7B3C,EAAS4C,aAAe,CACtBlC,gBAAQmC,UAAKA,GACbjB"}