Skip to content

Commit

Permalink
WiP
Browse files Browse the repository at this point in the history
  • Loading branch information
jelly committed Jul 16, 2024
1 parent 3176db7 commit 5d7b197
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
33 changes: 33 additions & 0 deletions src/files-card-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,31 @@ export const FilesCardBody = ({
}
};

const handleDragEnter = (event: DragEvent) => {
event.preventDefault();
event.stopPropagation();
folderViewElem.classList.add("files-drag-hover");
};

const handleDragLeave = (event: DragEvent) => {
event.preventDefault();
event.stopPropagation();
folderViewElem.classList.remove("files-drag-hover");
};

const handleDrop = (event: DragEvent) => {
cockpit.assert(event.dataTransfer !== null, "dataTransfer cannot be null");
dispatchEvent(new CustomEvent('files-drop', { detail: event.dataTransfer.files }));
console.log("drop", event.dataTransfer.files);
event.preventDefault();
event.stopPropagation();
};

const handleDragOver = (event: DragEvent) => {
event.preventDefault();
event.stopPropagation();
};

const onKeyboardNav = (e: KeyboardEvent) => {
if (e.key === "ArrowRight") {
setSelected(_selected => {
Expand Down Expand Up @@ -273,6 +298,10 @@ export const FilesCardBody = ({
folderViewElem.addEventListener("click", handleClick);
folderViewElem.addEventListener("dblclick", handleDoubleClick);
folderViewElem.addEventListener("contextmenu", handleContextMenu);
folderViewElem.addEventListener("dragenter", handleDragEnter, false);
folderViewElem.addEventListener("dragleave", handleDragLeave, false);
folderViewElem.addEventListener("dragover", handleDragOver, false);
folderViewElem.addEventListener("drop", handleDrop, false);
}

if (!isMounted.current && !dialogs.isActive()) {
Expand All @@ -288,6 +317,10 @@ export const FilesCardBody = ({
folderViewElem.removeEventListener("click", handleClick);
folderViewElem.removeEventListener("dblclick", handleDoubleClick);
folderViewElem.removeEventListener("contextmenu", handleContextMenu);
folderViewElem.removeEventListener("dragenter", handleDragEnter);
folderViewElem.removeEventListener("dragover", handleDragOver);
folderViewElem.removeEventListener("dragleave", handleDragLeave);
folderViewElem.removeEventListener("drop", handleDrop);
}
};
}, [
Expand Down
34 changes: 26 additions & 8 deletions src/upload-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useState, useRef } from "react";
import React, { useState, useRef, useEffect } from "react";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import useEffect.

import { AlertVariant } from "@patternfly/react-core/dist/esm/components/Alert";
import { Button } from "@patternfly/react-core/dist/esm/components/Button";
Expand All @@ -39,6 +39,7 @@ import { fmt_to_fragments } from "utils";
import { useFilesContext } from "./app";

import "./upload-button.scss";
import { useInit } from "hooks";

const _ = cockpit.gettext;

Expand Down Expand Up @@ -138,6 +139,19 @@ export const UploadButton = ({
const [uploadedFiles, setUploadedFiles] = useState<{[name: string]:
{file: File, progress: number, cancel:() => void}}>({});

const handleFilesDrop = (event: CustomEvent) => {
console.log(event);
onUpload(event.detail);
};

useInit(() => {
console.log('doing the fun');
window.addEventListener("files-drop", handleFilesDrop);
return () => {
window.removeEventListener("files-drop", handleFilesDrop);
};
});

const handleClick = () => {
if (ref.current) {
ref.current.click();
Expand All @@ -152,23 +166,24 @@ export const UploadButton = ({
event.returnValue = true;
};

const onUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
cockpit.assert(event.target.files, "not an <input type='file'>?");
const onUpload = async (files: FileList, event?: React.ChangeEvent<HTMLInputElement>) => {
console.log(files);
cockpit.assert(cwdInfo?.entries, "cwdInfo.entries is undefined");
let next_progress = 0;
const toUploadFiles = [];

const resetInput = () => {
// Reset input field in the case a download was cancelled and has to be re-uploaded
// https://stackoverflow.com/questions/26634616/filereader-upload-same-file-again-not-working
event.target.value = "";
if (event)
event.target.value = "";
};

let resolution;
let replaceAll = false;
let skipAll = false;
for (let i = 0; i < event.target.files.length; i++) {
const uploadFile = event.target.files[i];
for (let i = 0; i < files.length; i++) {
const uploadFile = files[i];
const file = cwdInfo?.entries[uploadFile.name];

if (replaceAll)
Expand All @@ -178,7 +193,7 @@ export const UploadButton = ({
} else if (file) {
try {
resolution = await dialogs.run(FileConflictDialog, {
path, file, uploadFile, isMultiUpload: event.target.files.length > 1
path, file, uploadFile, isMultiUpload: files.length > 1
});
} catch (exc) {
resetInput();
Expand Down Expand Up @@ -344,7 +359,10 @@ export const UploadButton = ({
type="file"
hidden
multiple
onChange={onUpload}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
cockpit.assert(event?.target.files, "not an <input type='file'>?");
onUpload(event.target.files, event);
}}
/>
</>
);
Expand Down

0 comments on commit 5d7b197

Please sign in to comment.