Skip to content

Commit

Permalink
Merge pull request #575 from vizhub-core/drag-drop
Browse files Browse the repository at this point in the history
drag and drop v1
  • Loading branch information
curran committed Mar 11, 2024
2 parents 580051e + 366a903 commit 65f7d5f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/client/VZCodeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type VZCodeContextValue = {
docPresence: any;

files: Files | null;
createFile: (fileName: string) => void;
createFile: (fileName: string, text?: string) => void;
renameFile: (fileId: string, fileName: string) => void;
deleteFile: (fileId: string) => void;
renameDirectory: (
Expand Down
74 changes: 74 additions & 0 deletions src/client/VZSidebar/DragAndDrop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useState, useContext, useCallback } from 'react';
import { VZCodeContext } from '../VZCodeContext';
import './styles.scss';

const debug = false;

export const DragAndDrop = () => {
const [isDragOver, setIsDragOver] = useState(false);
const { createFile } = useContext(VZCodeContext);

const handleDragEnter = useCallback((event) => {
event.preventDefault();
event.stopPropagation();
setIsDragOver(true);
}, []);

const handleDragOver = useCallback(
(event) => {
event.preventDefault();
event.stopPropagation();
if (!isDragOver) setIsDragOver(true);
},
[isDragOver],
);

const handleDragLeave = useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
event.stopPropagation();
setIsDragOver(false);
},
[],
);

const handleDrop = useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
event.stopPropagation();
setIsDragOver(false);
const files = event.dataTransfer.files;

if (debug) {
console.log(files);
}

Array.from(files).forEach((file) => {
const reader = new FileReader();
reader.onload = (readEvent) => {
createFile(
file.name,
readEvent.target.result as string,
);
};
reader.onerror = (error) => {
console.error('Error reading file:', error);
};
reader.readAsText(file);
});
},
[createFile],
);

return (
<div
className={`drop-zone ${isDragOver ? 'dragover' : ''}`}
onDragEnter={handleDragEnter}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
Drag files here
</div>
);
};
2 changes: 2 additions & 0 deletions src/client/VZSidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { SplitPaneResizeContext } from '../SplitPaneResizeContext';
import { BugSVG, GearSVG, NewSVG } from '../Icons';
import { Listing } from './Listing';
import { VZCodeContext } from '../VZCodeContext';
import { DragAndDrop } from './DragAndDrop';
import './styles.scss';

// TODO turn this UI back on when we are actually detecting
Expand Down Expand Up @@ -146,6 +147,7 @@ export const VZSidebar = ({
</OverlayTrigger>
</div>
</div>
<DragAndDrop />
{filesExist ? (
fileTree.children.map((entity) => {
const { fileId } = entity as FileTreeFile;
Expand Down
14 changes: 14 additions & 0 deletions src/client/VZSidebar/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,18 @@
}
}
}

.drop-zone {
border: 3px dashed #bdc7dc;
width: auto;
padding: 20px;
color: #bdc7dc;
text-align: center;
font-family: var(--vzcode-font-family);
font-weight: 500;
}

.drop-zone.dragover {
border-color: #009639;
}
}
4 changes: 2 additions & 2 deletions src/client/useFileCRUD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export const useFileCRUD = ({
}) => {
// Create a new file
const createFile = useCallback(
(name: string) => {
(name: string, text = '') => {
if (name) {
const fileId: FileId = randomId();
submitOperation((document: VZCodeContent) => ({
...document,
files: {
...document.files,
[fileId]: { name, text: '' },
[fileId]: { name, text },
},
}));
// When a new file is created, open it in a new tab
Expand Down

0 comments on commit 65f7d5f

Please sign in to comment.