Reading filedropper

This commit is contained in:
aronmal 2022-11-23 20:33:00 +01:00
parent 572340d5bd
commit 3c5dec376d
Signed by: aronmal
GPG key ID: 816B7707426FC612
5 changed files with 52 additions and 3 deletions

View file

@ -22,6 +22,7 @@
"@types/react-router-dom": "^5.3.3", "@types/react-router-dom": "^5.3.3",
"@types/sass": "^1.43.1", "@types/sass": "^1.43.1",
"@types/web-bluetooth": "^0.0.15", "@types/web-bluetooth": "^0.0.15",
"classnames": "^2.3.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-dropzone": "^14.2.3", "react-dropzone": "^14.2.3",
@ -4970,6 +4971,11 @@
"version": "1.2.2", "version": "1.2.2",
"license": "MIT" "license": "MIT"
}, },
"node_modules/classnames": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.2.tgz",
"integrity": "sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw=="
},
"node_modules/clean-css": { "node_modules/clean-css": {
"version": "5.3.0", "version": "5.3.0",
"license": "MIT", "license": "MIT",
@ -18264,6 +18270,11 @@
"cjs-module-lexer": { "cjs-module-lexer": {
"version": "1.2.2" "version": "1.2.2"
}, },
"classnames": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.2.tgz",
"integrity": "sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw=="
},
"clean-css": { "clean-css": {
"version": "5.3.0", "version": "5.3.0",
"requires": { "requires": {

View file

@ -17,6 +17,7 @@
"@types/react-router-dom": "^5.3.3", "@types/react-router-dom": "^5.3.3",
"@types/sass": "^1.43.1", "@types/sass": "^1.43.1",
"@types/web-bluetooth": "^0.0.15", "@types/web-bluetooth": "^0.0.15",
"classnames": "^2.3.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-dropzone": "^14.2.3", "react-dropzone": "^14.2.3",

View file

@ -1,21 +1,26 @@
import {useCallback} from 'react' import classNames from 'classnames'
import {useCallback, useState} from 'react'
import {useDropzone} from 'react-dropzone' import {useDropzone} from 'react-dropzone'
import { readFile } from '../helpers'
function MyDropzone() { function MyDropzone() {
const onDrop = useCallback((acceptedFiles: any) => { const [txt, settxt] = useState('Empty!')
const onDrop = useCallback(async (acceptedFiles: any) => {
// Do something with the files // Do something with the files
console.log(acceptedFiles) console.log(acceptedFiles)
settxt(await readFile(acceptedFiles[0]))
}, []) }, [])
const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop}) const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop})
return ( return (
<div {...getRootProps()}> <div {...getRootProps()} className={classNames('filedropper', isDragActive ? 'dragging' : 'standby')}>
<input {...getInputProps()} /> <input {...getInputProps()} />
{ {
isDragActive ? isDragActive ?
<p>Drop the files here ...</p> : <p>Drop the files here ...</p> :
<p>Drag 'n' drop some files here, or click to select files</p> <p>Drag 'n' drop some files here, or click to select files</p>
} }
{txt}
</div> </div>
) )
} }

View file

@ -51,3 +51,17 @@ export const initlialTargetPreview = {
y: 0 y: 0
}; };
export const isHit = (hits: HitType[], x: number, y: number) => hits.filter(h => h.x === x && h.y === y); export const isHit = (hits: HitType[], x: number, y: number) => hits.filter(h => h.x === x && h.y === y);
export function readFile(file: File): Promise<string> {
return new Promise((resolve) => {
const reader = new FileReader()
reader.onload = async (e) => {
if (!e.target)
return
const text = (e.target.result)
if (typeof text !== 'string')
return
resolve(text)
};
reader.readAsText(file)
})
}

View file

@ -193,4 +193,22 @@
&:hover { &:hover {
background-color: red; background-color: red;
} }
}
.filedropper {
@include transition(.2s);
transition-property: background-color;
background-color: transparent;
border: .25em dashed #fff2;
border-radius: 1em;
width: 40vw;
min-height: 10vw;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
&.dragging {
background-color: #fff1;
border: .25em dashed #00f8;
}
} }