2022-07-20 04:32:51 -07:00
|
|
|
<template>
|
2024-07-02 11:55:19 -07:00
|
|
|
<div ref="targetRef" @mouseenter="onMouseEnter" @mouseleave="onMouseLeave" @mouseup="onMouseUp">
|
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>
|
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
<script setup lang="ts">
|
|
|
|
import type { XYPosition } from '@/Interface';
|
2023-05-16 02:43:46 -07:00
|
|
|
import { useNDVStore } from '@/stores/ndv.store';
|
2023-12-13 05:45:22 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2024-07-02 11:55:19 -07:00
|
|
|
import { computed, ref, watch } from 'vue';
|
2024-02-06 09:34:34 -08:00
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
type Props = {
|
|
|
|
type: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
sticky?: boolean;
|
|
|
|
stickyOffset?: XYPosition;
|
|
|
|
stickyOrigin?: 'top-left' | 'center';
|
|
|
|
};
|
2024-02-06 09:34:34 -08:00
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
disabled: false,
|
|
|
|
sticky: false,
|
|
|
|
stickyOffset: () => [0, 0],
|
|
|
|
stickyOrigin: 'top-left',
|
|
|
|
});
|
2024-07-04 00:30:51 -07:00
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
const emit = defineEmits<{
|
2024-07-04 00:30:51 -07:00
|
|
|
drop: [value: string];
|
2024-07-02 11:55:19 -07:00
|
|
|
}>();
|
2024-02-06 09:34:34 -08:00
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
const hovering = ref(false);
|
|
|
|
const targetRef = ref<HTMLElement>();
|
|
|
|
const id = ref(uuid());
|
2022-07-20 04:32:51 -07:00
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
const ndvStore = useNDVStore();
|
|
|
|
const isDragging = computed(() => ndvStore.isDraggableDragging);
|
|
|
|
const draggableType = computed(() => ndvStore.draggableType);
|
|
|
|
const draggableDimensions = computed(() => ndvStore.draggable.dimensions);
|
|
|
|
const droppable = computed(
|
|
|
|
() => !props.disabled && isDragging.value && draggableType.value === props.type,
|
|
|
|
);
|
|
|
|
const activeDrop = computed(() => droppable.value && hovering.value);
|
2022-07-20 04:32:51 -07:00
|
|
|
|
2024-07-02 11:55:19 -07:00
|
|
|
watch(activeDrop, (active) => {
|
|
|
|
if (active) {
|
|
|
|
const stickyPosition = getStickyPosition();
|
|
|
|
ndvStore.setDraggableTarget({ id: id.value, stickyPosition });
|
|
|
|
} else if (ndvStore.draggable.activeTarget?.id === id.value) {
|
|
|
|
// Only clear active target if it is this one
|
|
|
|
ndvStore.setDraggableTarget(null);
|
|
|
|
}
|
2022-07-20 04:32:51 -07:00
|
|
|
});
|
2024-07-02 11:55:19 -07:00
|
|
|
|
|
|
|
function onMouseEnter() {
|
|
|
|
hovering.value = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseLeave() {
|
|
|
|
hovering.value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseUp() {
|
|
|
|
if (activeDrop.value) {
|
|
|
|
const data = ndvStore.draggableData;
|
|
|
|
emit('drop', data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStickyPosition(): XYPosition | null {
|
|
|
|
if (props.disabled || !props.sticky || !hovering.value || !targetRef.value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { left, top, width, height } = targetRef.value.getBoundingClientRect();
|
|
|
|
|
|
|
|
if (props.stickyOrigin === 'center') {
|
|
|
|
return [
|
|
|
|
left + props.stickyOffset[0] + width / 2 - (draggableDimensions.value?.width ?? 0) / 2,
|
|
|
|
top + props.stickyOffset[1] + height / 2 - (draggableDimensions.value?.height ?? 0) / 2,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [left + props.stickyOffset[0], top + props.stickyOffset[1]];
|
|
|
|
}
|
2022-07-20 04:32:51 -07:00
|
|
|
</script>
|