2022-07-20 04:32:51 -07:00
|
|
|
<template>
|
|
|
|
<div ref="target">
|
2023-12-28 00:49:58 -08:00
|
|
|
<slot :droppable="droppable" :active-drop="activeDrop"></slot>
|
2022-07-20 04:32:51 -07:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2023-04-21 08:51:08 -07:00
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
import type { PropType } from 'vue';
|
2023-05-16 02:43:46 -07:00
|
|
|
import { mapStores } from 'pinia';
|
|
|
|
import { useNDVStore } from '@/stores/ndv.store';
|
2023-12-13 05:45:22 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2024-02-06 09:34:34 -08:00
|
|
|
import type { XYPosition } from '@/Interface';
|
2022-07-20 04:32:51 -07:00
|
|
|
|
2023-04-21 08:51:08 -07:00
|
|
|
export default defineComponent({
|
2022-07-20 04:32:51 -07:00
|
|
|
props: {
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
},
|
|
|
|
sticky: {
|
|
|
|
type: Boolean,
|
|
|
|
},
|
|
|
|
stickyOffset: {
|
2022-12-14 05:43:02 -08:00
|
|
|
type: Array as PropType<number[]>,
|
2023-04-12 07:39:45 -07:00
|
|
|
default: () => [0, 0],
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
2024-02-06 09:34:34 -08:00
|
|
|
stickyOrigin: {
|
|
|
|
type: String as PropType<'top-left' | 'center'>,
|
|
|
|
default: 'top-left',
|
|
|
|
},
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
hovering: false,
|
2024-02-06 09:34:34 -08:00
|
|
|
dimensions: null as DOMRect | null,
|
2023-12-13 05:45:22 -08:00
|
|
|
id: uuid(),
|
2022-07-20 04:32:51 -07:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
2022-11-04 06:04:31 -07:00
|
|
|
...mapStores(useNDVStore),
|
2022-07-20 04:32:51 -07:00
|
|
|
isDragging(): boolean {
|
2022-11-04 06:04:31 -07:00
|
|
|
return this.ndvStore.isDraggableDragging;
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
|
|
|
draggableType(): string {
|
2022-11-04 06:04:31 -07:00
|
|
|
return this.ndvStore.draggableType;
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
2024-02-06 09:34:34 -08:00
|
|
|
draggableDimensions(): DOMRect | null {
|
|
|
|
return this.ndvStore.draggable.dimensions;
|
|
|
|
},
|
2022-07-20 04:32:51 -07:00
|
|
|
droppable(): boolean {
|
|
|
|
return !this.disabled && this.isDragging && this.draggableType === this.type;
|
|
|
|
},
|
|
|
|
activeDrop(): boolean {
|
|
|
|
return this.droppable && this.hovering;
|
|
|
|
},
|
2024-02-06 09:34:34 -08:00
|
|
|
stickyPosition(): XYPosition | null {
|
|
|
|
if (this.disabled || !this.sticky || !this.hovering || !this.dimensions) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.stickyOrigin === 'center') {
|
|
|
|
return [
|
|
|
|
this.dimensions.left +
|
|
|
|
this.stickyOffset[0] +
|
|
|
|
this.dimensions.width / 2 -
|
|
|
|
(this.draggableDimensions?.width ?? 0) / 2,
|
|
|
|
this.dimensions.top +
|
|
|
|
this.stickyOffset[1] +
|
|
|
|
this.dimensions.height / 2 -
|
|
|
|
(this.draggableDimensions?.height ?? 0) / 2,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
this.dimensions.left + this.stickyOffset[0],
|
|
|
|
this.dimensions.top + this.stickyOffset[1],
|
|
|
|
];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
activeDrop(active) {
|
|
|
|
if (active) {
|
|
|
|
this.ndvStore.setDraggableTarget({ id: this.id, stickyPosition: this.stickyPosition });
|
|
|
|
} else if (this.ndvStore.draggable.activeTarget?.id === this.id) {
|
|
|
|
// Only clear active target if it is this one
|
|
|
|
this.ndvStore.setDraggableTarget(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
window.addEventListener('mousemove', this.onMouseMove);
|
|
|
|
window.addEventListener('mouseup', this.onMouseUp);
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeUnmount() {
|
|
|
|
window.removeEventListener('mousemove', this.onMouseMove);
|
|
|
|
window.removeEventListener('mouseup', this.onMouseUp);
|
2022-07-20 04:32:51 -07:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onMouseMove(e: MouseEvent) {
|
2023-04-21 06:59:04 -07:00
|
|
|
const targetRef = this.$refs.target as HTMLElement | undefined;
|
2022-07-20 04:32:51 -07:00
|
|
|
|
2023-04-21 06:59:04 -07:00
|
|
|
if (targetRef && this.isDragging) {
|
|
|
|
const dim = targetRef.getBoundingClientRect();
|
2022-07-20 04:32:51 -07:00
|
|
|
|
2024-02-06 09:34:34 -08:00
|
|
|
this.dimensions = dim;
|
2022-07-20 04:32:51 -07:00
|
|
|
this.hovering =
|
|
|
|
e.clientX >= dim.left &&
|
|
|
|
e.clientX <= dim.right &&
|
|
|
|
e.clientY >= dim.top &&
|
|
|
|
e.clientY <= dim.bottom;
|
|
|
|
}
|
|
|
|
},
|
2024-05-31 07:27:56 -07:00
|
|
|
onMouseUp() {
|
2022-07-20 04:32:51 -07:00
|
|
|
if (this.activeDrop) {
|
2022-11-04 06:04:31 -07:00
|
|
|
const data = this.ndvStore.draggableData;
|
2022-07-20 04:32:51 -07:00
|
|
|
this.$emit('drop', data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|