-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
521 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Directive, DirectiveBinding, VNode } from 'vue' | ||
import type { DragonDropVueDragOptions, DragonDropVueOptions } from './options' | ||
import { log } from './logger' | ||
import { addClasses, addEventHandler, removeEventHandler } from './htmlHelpers' | ||
import constants from './constants' | ||
import { onDragEnd, onDragStart } from './eventHandlers' | ||
|
||
export function useDragDirective(opts: DragonDropVueOptions) { | ||
return { | ||
beforeMount: ( | ||
el: HTMLElement, | ||
binding: DirectiveBinding<DragonDropVueDragOptions | false>, | ||
vnode: VNode<any, HTMLElement>, | ||
prevVnode: VNode<any, HTMLElement> | null, | ||
) => { | ||
if (binding.value === false) { | ||
return | ||
} | ||
setupDrag(el, binding.value, opts) | ||
log({ eventName: 'drag | beforeMount', domEl: el, dragOpts: binding.value, opts }) | ||
}, | ||
updated: ( | ||
el: HTMLElement, | ||
binding: DirectiveBinding<DragonDropVueDragOptions | false>, | ||
vnode: VNode<any, HTMLElement>, | ||
prevVnode: VNode<any, HTMLElement> | null, | ||
) => { | ||
if (binding.value === false) { | ||
el.setAttribute('draggable', 'false') | ||
} else if (el.getAttribute('draggable') !== 'true') { | ||
setupDrag(el, binding.value, opts) | ||
} | ||
log({ eventName: 'drag | updated', domEl: el, dragOpts: binding.value, opts }) | ||
}, | ||
beforeUnmount: ( | ||
el: HTMLElement, | ||
binding: DirectiveBinding<DragonDropVueDragOptions | false>, | ||
vnode: VNode<any, HTMLElement>, | ||
prevVnode: VNode<any, HTMLElement> | null, | ||
) => { | ||
// remove drag events | ||
removeEventHandler(el, 'dragstart', opts) | ||
removeEventHandler(el, 'dragend', opts) | ||
log({ eventName: 'drag | beforeUnmount', domEl: el, dragOpts: binding.value, opts }) | ||
}, | ||
} as Directive<HTMLElement, DragonDropVueDragOptions | false> | ||
} | ||
|
||
function setupDrag(el: HTMLElement, dragOpts: DragonDropVueDragOptions, opts: DragonDropVueOptions) { | ||
// add css classes | ||
addClasses(el, [constants.dragClass, opts.dragClass]) | ||
|
||
// add draggable attribute | ||
el.setAttribute('draggable', 'true') | ||
|
||
// wire drag events | ||
addEventHandler(el, 'dragstart', ev => onDragStart(ev, dragOpts, opts), dragOpts, opts) | ||
addEventHandler(el, 'dragend', ev => onDragEnd(ev, dragOpts, opts), dragOpts, opts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Directive, DirectiveBinding, VNode } from 'vue' | ||
import constants from './constants' | ||
import { onDragEnter, onDragLeave, onDragOver, onDrop } from './eventHandlers' | ||
import { addClasses, addEventHandler, removeEventHandler } from './htmlHelpers' | ||
import type { DragonDropVueDragOptions, DragonDropVueOptions } from './options' | ||
import { log } from './logger' | ||
|
||
export function useDropDirective(opts: DragonDropVueOptions) { | ||
return { | ||
beforeMount: ( | ||
el: HTMLElement, | ||
binding: DirectiveBinding<DragonDropVueDragOptions | false>, | ||
vnode: VNode<any, HTMLElement>, | ||
prevVnode: VNode<any, HTMLElement> | null, | ||
) => { | ||
if (binding.value === false) { | ||
return | ||
} | ||
|
||
setupDrop(el, binding.value, opts) | ||
log({ eventName: 'drop | beforeMount', domEl: el, dragOpts: binding.value, opts }) | ||
}, | ||
updated: ( | ||
el: HTMLElement, | ||
binding: DirectiveBinding<DragonDropVueDragOptions | false>, | ||
vnode: VNode<any, HTMLElement>, | ||
prevVnode: VNode<any, HTMLElement> | null, | ||
) => { | ||
if (binding.value === false) { | ||
return | ||
} | ||
|
||
const domEl = el as HTMLElement | ||
if (!domEl.classList.contains(constants.dropClass)) { | ||
setupDrop(el, binding.value, opts) | ||
} | ||
log({ eventName: 'drop | updated', domEl: el, dragOpts: binding.value, opts }) | ||
}, | ||
beforeUnmount: ( | ||
el: HTMLElement, | ||
binding: DirectiveBinding<DragonDropVueDragOptions | false>, | ||
vnode: VNode<any, HTMLElement>, | ||
prevVnode: VNode<any, HTMLElement> | null, | ||
) => { | ||
// remove drag events | ||
removeEventHandler(el, 'dragover', opts) | ||
removeEventHandler(el, 'dragenter', opts) | ||
removeEventHandler(el, 'dragleave', opts) | ||
removeEventHandler(el, 'drop', opts) | ||
log({ eventName: 'drop | beforeUnmount', domEl: el, dragOpts: binding.value, opts }) | ||
}, | ||
} as Directive<HTMLElement, DragonDropVueDragOptions | false> | ||
} | ||
|
||
function setupDrop(el: HTMLElement, dragOpts: DragonDropVueDragOptions, opts: DragonDropVueOptions) { | ||
// add css classes | ||
addClasses(el, [constants.dropClass, opts.dropClass]) | ||
|
||
// wire drag events | ||
addEventHandler(el, 'dragover', ev => onDragOver(ev, dragOpts, opts), dragOpts, opts) | ||
addEventHandler(el, 'dragenter', ev => onDragEnter(ev, dragOpts, opts), dragOpts, opts) | ||
addEventHandler(el, 'dragleave', ev => onDragLeave(ev, dragOpts, opts), dragOpts, opts) | ||
addEventHandler(el, 'drop', ev => onDrop(ev, dragOpts, opts), dragOpts, opts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import DragonDropVue from './DragonDropVue' | ||
import type { DragonDropVueOptions, DragonDropVueDragOptions, DragonDropVueDragImageOptions } from './options' | ||
import { DropEffect } from './htmlHelpers' | ||
import { DebounceMode } from 'native-event-vue' | ||
|
||
export { DragonDropVue, type DragonDropVueOptions, type DragonDropVueDragOptions, type DragonDropVueDragImageOptions, DropEffect } | ||
export { DragonDropVue, type DragonDropVueOptions, type DragonDropVueDragOptions, type DragonDropVueDragImageOptions, DropEffect, DebounceMode } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.