startAndEndEventsDNDPlugin.ts 822 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { DNDPlugin } from '@formkit/drag-and-drop'
  3. export const startAndEndEventsDNDPlugin =
  4. (
  5. startCallback?: (parent: HTMLElement, event: DragEvent) => void,
  6. endCallback?: (parent: HTMLElement) => void,
  7. ): DNDPlugin =>
  8. (parent) => {
  9. const dragStart = (event: DragEvent) => {
  10. startCallback?.(parent, event)
  11. }
  12. const dragEnd = () => {
  13. endCallback?.(parent)
  14. }
  15. return {
  16. setupNode: (data) => {
  17. data.node.addEventListener('dragstart', dragStart)
  18. data.node.addEventListener('dragend', dragEnd)
  19. },
  20. tearDownNode: (data) => {
  21. data.node.removeEventListener('dragstart', dragStart)
  22. data.node.removeEventListener('dragend', dragEnd)
  23. },
  24. }
  25. }