Skip to content

Commit

Permalink
Merge pull request #16995 from hujambo-dunia/enhance-drag-drop
Browse files Browse the repository at this point in the history
Enhance Global Drag-and-Drop feature for Upload component
  • Loading branch information
dannon authored Nov 27, 2023
2 parents 8c48e46 + 0f53b62 commit 0b6d086
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
37 changes: 32 additions & 5 deletions client/src/components/Upload/DragAndDropModal.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<script setup>
import { setIframeEvents } from "components/Upload/utils";
import { useFileDrop } from "composables/fileDrop";
import { useGlobalUploadModal } from "composables/globalUploadModal";
import { computed, ref } from "vue";
import { computed, ref, watch } from "vue";
import { useToast } from "@/composables/toast";
const modalContentElement = ref(null);
const { isFileOverDocument, isFileOverDropZone } = useFileDrop(modalContentElement, onDrop, true);
const { isFileOverDocument, isFileOverDropZone } = useFileDrop(modalContentElement, onDrop, onDropCancel, true);
const modalClass = computed(() => {
if (isFileOverDropZone.value) {
Expand All @@ -16,6 +19,10 @@ const modalClass = computed(() => {
const { openGlobalUploadModal } = useGlobalUploadModal();
const toast = useToast();
const iframesNoInteract = ["galaxy_main", "frame.center-frame"];
function onDrop(event) {
console.debug(event.dataTransfer);
Expand All @@ -26,24 +33,43 @@ function onDrop(event) {
});
}
}
function onDropCancel(event) {
if (event.dataTransfer?.files?.length > 0) {
toast.error("Upload cancelled", "Drop file in the center to upload it");
}
}
watch(isFileOverDocument, (newValue, oldValue) => {
if (!oldValue && newValue) {
setIframeEvents(iframesNoInteract, true);
} else {
setIframeEvents(iframesNoInteract, false);
}
});
</script>
<template>
<b-modal v-model="isFileOverDocument" :modal-class="modalClass" hide-header hide-footer centered>
<BModal v-model="isFileOverDocument" :modal-class="modalClass" hide-header hide-footer centered>
<div ref="modalContentElement" class="inner-content h-xl">Drop Files here to Upload</div>
</b-modal>
</BModal>
</template>
<style lang="scss">
@import "theme/blue.scss";
.ui-drag-and-drop-modal {
.modal-dialog {
width: 100%;
max-width: 85%;
}
.modal-content {
background-color: transparent;
border-radius: 16px;
border: 6px dashed;
border-color: $brand-secondary;
min-height: 40vh;
min-height: 80vh;
.modal-body {
display: flex;
Expand All @@ -61,6 +87,7 @@ function onDrop(event) {
&.drag-over {
.modal-content {
border-color: lighten($brand-info, 30%);
background-color: rgba(darken($brand-info, 20%), 0.4);
.inner-content {
color: lighten($brand-info, 30%);
Expand Down
10 changes: 2 additions & 8 deletions client/src/components/Upload/UploadModal.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup>
import { setIframeEvents } from "components/Upload/utils";
import { useConfig } from "composables/config";
import { useUserHistories } from "composables/userHistories";
import { storeToRefs } from "pinia";
Expand Down Expand Up @@ -59,16 +60,9 @@ async function open(overrideOptions) {
}
}
function setIframeEvents(disableEvents) {
const element = document.getElementById("galaxy_main");
if (element) {
element.style["pointer-events"] = disableEvents ? "none" : "auto";
}
}
watch(
() => showModal.value,
(modalShown) => setIframeEvents(modalShown)
(modalShown) => setIframeEvents(["galaxy_main"], modalShown)
);
defineExpose({
Expand Down
10 changes: 10 additions & 0 deletions client/src/components/Upload/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,13 @@ export default {
getUploadDatatypes,
getUploadDbKeys,
};

export function setIframeEvents(elements, disableEvents) {
let element;
elements.forEach((e) => {
element = document.querySelector(`iframe#${e}`);
if (element) {
element.style.pointerEvents = disableEvents ? "none" : "auto";
}
});
}
5 changes: 5 additions & 0 deletions client/src/composables/fileDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export type FileDropHandler = (event: DragEvent) => void;
* Custom File-Drop composable
* @param dropZone Element which files should be dropped on
* @param onDrop callback function called when drop occurs
* @param onDropCancel callback function called when drop cancelled
* @param solo when true, only reacts if no modal is open
* @param idleTime how long to wait until state resets
*/
export function useFileDrop(
dropZone: MaybeRefOrGetter<EventTarget | null | undefined>,
onDrop: Ref<FileDropHandler> | FileDropHandler,
onDropCancel: Ref<FileDropHandler> | FileDropHandler,
solo: MaybeRefOrGetter<boolean>,
idleTime = 800
) {
Expand Down Expand Up @@ -72,6 +74,9 @@ export function useFileDrop(
if (isFileOverDropZone.value) {
const dropHandler = unref(onDrop);
dropHandler(event as DragEvent);
} else {
const dropCancelHandler = unref(onDropCancel);
dropCancelHandler(event as DragEvent);
}
return "idle";
case "dragend":
Expand Down

0 comments on commit 0b6d086

Please sign in to comment.