koel/resources/assets/js/directives/droppable.ts

31 lines
811 B
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import { Directive } from 'vue'
import { $ } from '@/utils'
2022-04-15 14:24:30 +00:00
export const droppable: Directive = {
created: (el: HTMLElement, binding) => {
const acceptsDrops = binding.arg === undefined || Boolean(binding.arg)
if (!acceptsDrops) {
return
2022-04-15 14:24:30 +00:00
}
2022-04-25 16:38:33 +00:00
el.addEventListener('dragenter', (event: DragEvent) => {
2022-04-15 14:24:30 +00:00
event.preventDefault()
$.addClass(el, 'droppable')
event.dataTransfer!.dropEffect = 'move'
return false
})
el.addEventListener('dragover', (event: DragEvent) => event.preventDefault())
2022-04-25 16:38:33 +00:00
el.addEventListener('dragleave', () => $.removeClass(el, 'droppable'))
2022-04-15 14:24:30 +00:00
2022-04-25 16:38:33 +00:00
el.addEventListener('drop', (event: DragEvent) => {
2022-04-15 14:24:30 +00:00
event.preventDefault()
event.stopPropagation()
$.removeClass(el, 'droppable')
binding.value(event)
2022-04-15 14:24:30 +00:00
})
}
}