made drag and drop handler global

This commit is contained in:
Djeeberjr 2021-10-01 21:23:27 +02:00
parent c12181fe16
commit 06b7c323ae
2 changed files with 87 additions and 99 deletions

View File

@ -8,7 +8,7 @@ import uploadFile from "../functions/uploadFile"
import { useCopyMutation, useCreateDirMutation, useDeleteDirMutation, useDeleteFileMutation, useMoveMutation, useOpenDirQuery } from "../generated/graphql" import { useCopyMutation, useCreateDirMutation, useDeleteDirMutation, useDeleteFileMutation, useMoveMutation, useOpenDirQuery } from "../generated/graphql"
import Breadcrum from "./Breadcrum" import Breadcrum from "./Breadcrum"
import CreateDirButton from "./CreateDirButton" import CreateDirButton from "./CreateDirButton"
import DragAndDrop from "./DragAndDrop" import GlobalDragAndDrop from "./GlobalDragAndDrop"
import FileBrowserContextMenu, { Action, CONTEXT_MENU_DIR, CONTEXT_MENU_FILE } from "./FileBrowserContextMenu" import FileBrowserContextMenu, { Action, CONTEXT_MENU_DIR, CONTEXT_MENU_FILE } from "./FileBrowserContextMenu"
import FileOpen from "./FileOpen" import FileOpen from "./FileOpen"
import FileUploadButton from "./FileUploadButton" import FileUploadButton from "./FileUploadButton"
@ -103,84 +103,82 @@ const FileBrowser: React.FC<RouteComponentProps> = (props) => {
onSelect={onContextSelect} onSelect={onContextSelect}
pasteActive={!!srcID} pasteActive={!!srcID}
/> />
<DragAndDrop <GlobalDragAndDrop
handleDrop={async (files)=>{ handleDrop={async (files)=>{
await handleDrop(files) await handleDrop(files)
}} }}/>
> <div className="flex justify-between h-12">
<div className="flex justify-between h-12"> <Breadcrum path={path} onDirClick={(newPath)=>{
<Breadcrum path={path} onDirClick={(newPath)=>{ props.history.push(newPath.toURI())
props.history.push(newPath.toURI()) }}/>
}}/> <div className="ml-auto">
<div className="ml-auto"> <CreateDirButton
<CreateDirButton onPressed={async (dirName)=>{
onPressed={async (dirName)=>{ const dirID = new ObjID(path.bucket,path.key + dirName + "/")
const dirID = new ObjID(path.bucket,path.key + dirName + "/") await createDirMutation({variables:{path: dirID}})
await createDirMutation({variables:{path: dirID}}) refetchDir()
refetchDir()
}}
/>
</div>
<div>
<FileUploadButton
onUpload={(files)=>handleDrop(files)}
/>
</div>
<div>
<MoreMenu />
</div>
</div>
<div>
<FileBrowserList
directorys={data?.directories || []}
files={data?.files || []}
onDirClick={(e,path)=>{
props.history.push(path.toURI())
}}
onDirContext={(e,path)=>{
e.preventDefault()
showDirContext(e,{props:{id:path}})
}}
onFileClick={(e,id)=>{
setOpenFileId(id)
setShowFile(true)
}}
onFileContext={(e,id)=>{
e.preventDefault()
showFileContext(e,{props:{id}})
}}
editId={editID}
editEnable={editEnable}
onRenameDone={async (id,changed,newName)=>{
setEditEnable(false)
if (changed){
// TODO: maybe change the name on client so it seems more smooth rather then haveing it refetch
// TODO: input check & error handling
await moveMutation({variables:{
src:id,
dest: id.rename(newName)
}})
refetchDir()
}
}} }}
/> />
{loading &&
<div className="flex justify-center mt-4">
<Spinner className="animate-spin h-6 w-6 dark:text-white" />
</div>
}
</div> </div>
<FileOpen id={openFileId} show={showFile} onCloseClick={()=>{ <div>
setShowFile(false) <FileUploadButton
}} /> onUpload={(files)=>handleDrop(files)}
</DragAndDrop> />
</div>
<div>
<MoreMenu />
</div>
</div>
<div>
<FileBrowserList
directorys={data?.directories || []}
files={data?.files || []}
onDirClick={(e,path)=>{
props.history.push(path.toURI())
}}
onDirContext={(e,path)=>{
e.preventDefault()
showDirContext(e,{props:{id:path}})
}}
onFileClick={(e,id)=>{
setOpenFileId(id)
setShowFile(true)
}}
onFileContext={(e,id)=>{
e.preventDefault()
showFileContext(e,{props:{id}})
}}
editId={editID}
editEnable={editEnable}
onRenameDone={async (id,changed,newName)=>{
setEditEnable(false)
if (changed){
// TODO: maybe change the name on client so it seems more smooth rather then haveing it refetch
// TODO: input check & error handling
await moveMutation({variables:{
src:id,
dest: id.rename(newName)
}})
refetchDir()
}
}}
/>
{loading &&
<div className="flex justify-center mt-4">
<Spinner className="animate-spin h-6 w-6 dark:text-white" />
</div>
}
</div>
<FileOpen id={openFileId} show={showFile} onCloseClick={()=>{
setShowFile(false)
}} />
</div> </div>
) )
} }

View File

@ -10,12 +10,10 @@ interface Props {
handleDrop?: (files: FileList)=>void handleDrop?: (files: FileList)=>void
} }
const DragAndDrop: React.FC<Props> = (props) => { const GlobalDragAndDrop: React.FC<Props> = (props) => {
const dropRef = React.createRef<HTMLDivElement>()
const [hover,setHover] = useState(false) const [hover,setHover] = useState(false)
function handleDragEnter(event: DragEvent) { function handleDragEnter(event: DragEvent) {
// console.debug("dragenter",event)
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
setHover(true) setHover(true)
@ -23,7 +21,6 @@ const DragAndDrop: React.FC<Props> = (props) => {
} }
function handleDragLeave(event: DragEvent) { function handleDragLeave(event: DragEvent) {
// console.debug("dragleave",event)
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
setHover(false) setHover(false)
@ -38,7 +35,6 @@ const DragAndDrop: React.FC<Props> = (props) => {
} }
function handleDrop(event: DragEvent) { function handleDrop(event: DragEvent) {
console.debug("drop",event)
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
setHover(false) setHover(false)
@ -49,31 +45,25 @@ const DragAndDrop: React.FC<Props> = (props) => {
} }
useEffect(()=>{ useEffect(()=>{
if(dropRef.current){ document.addEventListener("dragenter",handleDragEnter)
dropRef.current.addEventListener("dragenter",handleDragEnter) document.addEventListener("dragleave",handleDragLeave)
dropRef.current.addEventListener("dragleave",handleDragLeave) document.addEventListener("dragover",handleDragOver)
dropRef.current.addEventListener("dragover",handleDragOver) document.addEventListener("drop",handleDrop)
dropRef.current.addEventListener("drop",handleDrop)
return ()=>{ return ()=>{
if (dropRef.current){ document.removeEventListener("dragenter",handleDragEnter)
dropRef.current.removeEventListener("dragenter",handleDragEnter) document.removeEventListener("dragleave",handleDragLeave)
dropRef.current.removeEventListener("dragleave",handleDragLeave) document.removeEventListener("dragover",handleDragOver)
dropRef.current.removeEventListener("dragover",handleDragOver) document.removeEventListener("drop",handleDrop)
dropRef.current.removeEventListener("drop",handleDrop)
}
}
} }
},[]) },[])
return ( return (
<div ref={dropRef} <div className={`fixed top-0 left-0 w-full h-full z-10 pointer-events-none
className={`fixed top-0 left-0 w-full h-full z-10 ${hover? "border-dashed border-gray-600 border-4":""}`} />
${hover? "border-dashed border-gray-600 border-4":""}`}
>
{props.children}
</div>
) )
} }
export default DragAndDrop export default GlobalDragAndDrop