2022-02-03 21:04:25 +08:00
|
|
|
import { onBeforeUnmount, onMounted, watchEffect } from 'vue'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { addUnit } from '@element-plus/utils'
|
2022-03-25 15:35:56 +08:00
|
|
|
import type { ComputedRef, Ref } from 'vue'
|
2022-02-03 21:04:25 +08:00
|
|
|
|
|
|
|
export const useDraggable = (
|
|
|
|
targetRef: Ref<HTMLElement | undefined>,
|
|
|
|
dragRef: Ref<HTMLElement | undefined>,
|
|
|
|
draggable: ComputedRef<boolean>
|
|
|
|
) => {
|
|
|
|
let transform = {
|
|
|
|
offsetX: 0,
|
|
|
|
offsetY: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
const onMousedown = (e: MouseEvent) => {
|
|
|
|
const downX = e.clientX
|
|
|
|
const downY = e.clientY
|
|
|
|
const { offsetX, offsetY } = transform
|
|
|
|
|
2022-03-01 23:43:50 +08:00
|
|
|
const targetRect = targetRef.value!.getBoundingClientRect()
|
2022-02-03 21:04:25 +08:00
|
|
|
const targetLeft = targetRect.left
|
|
|
|
const targetTop = targetRect.top
|
|
|
|
const targetWidth = targetRect.width
|
|
|
|
const targetHeight = targetRect.height
|
|
|
|
|
|
|
|
const clientWidth = document.documentElement.clientWidth
|
|
|
|
const clientHeight = document.documentElement.clientHeight
|
|
|
|
|
|
|
|
const minLeft = -targetLeft + offsetX
|
|
|
|
const minTop = -targetTop + offsetY
|
|
|
|
const maxLeft = clientWidth - targetLeft - targetWidth + offsetX
|
|
|
|
const maxTop = clientHeight - targetTop - targetHeight + offsetY
|
|
|
|
|
|
|
|
const onMousemove = (e: MouseEvent) => {
|
|
|
|
const moveX = Math.min(
|
|
|
|
Math.max(offsetX + e.clientX - downX, minLeft),
|
|
|
|
maxLeft
|
|
|
|
)
|
|
|
|
const moveY = Math.min(
|
|
|
|
Math.max(offsetY + e.clientY - downY, minTop),
|
|
|
|
maxTop
|
|
|
|
)
|
|
|
|
|
|
|
|
transform = {
|
|
|
|
offsetX: moveX,
|
|
|
|
offsetY: moveY,
|
|
|
|
}
|
2022-03-01 23:43:50 +08:00
|
|
|
targetRef.value!.style.transform = `translate(${addUnit(
|
|
|
|
moveX
|
|
|
|
)}, ${addUnit(moveY)})`
|
2022-02-03 21:04:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const onMouseup = () => {
|
|
|
|
document.removeEventListener('mousemove', onMousemove)
|
|
|
|
document.removeEventListener('mouseup', onMouseup)
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('mousemove', onMousemove)
|
|
|
|
document.addEventListener('mouseup', onMouseup)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onDraggable = () => {
|
|
|
|
if (dragRef.value && targetRef.value) {
|
|
|
|
dragRef.value.addEventListener('mousedown', onMousedown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const offDraggable = () => {
|
|
|
|
if (dragRef.value && targetRef.value) {
|
|
|
|
dragRef.value.removeEventListener('mousedown', onMousedown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
watchEffect(() => {
|
|
|
|
if (draggable.value) {
|
|
|
|
onDraggable()
|
|
|
|
} else {
|
|
|
|
offDraggable()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
offDraggable()
|
|
|
|
})
|
|
|
|
}
|