mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
replace wrapper function with delegate in draggable utils
This commit is contained in:
parent
efa5092502
commit
9c8b6b8931
2 changed files with 11 additions and 19 deletions
|
@ -1,4 +1,5 @@
|
|||
import { $$ } from './dom';
|
||||
import { delegate } from './events';
|
||||
|
||||
let dragSrcEl: HTMLElement | null = null;
|
||||
|
||||
|
@ -60,23 +61,14 @@ function dragEnd(event: DragEvent, target: HTMLElement) {
|
|||
}
|
||||
}
|
||||
|
||||
function wrapper<E extends Event, T extends Element>(func: (event: E, target: T) => void) {
|
||||
return function(event: E) {
|
||||
const evtTarget = event.target as EventTarget | Element | null;
|
||||
if (evtTarget && 'closest' in evtTarget && typeof evtTarget.closest === 'function') {
|
||||
const target: T | null = evtTarget.closest('.drag-container [draggable]');
|
||||
if (target) func(event, target);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function initDraggables() {
|
||||
document.addEventListener('dragstart', wrapper(dragStart));
|
||||
document.addEventListener('dragover', wrapper(dragOver));
|
||||
document.addEventListener('dragenter', wrapper(dragEnter));
|
||||
document.addEventListener('dragleave', wrapper(dragLeave));
|
||||
document.addEventListener('dragend', wrapper(dragEnd));
|
||||
document.addEventListener('drop', wrapper(drop));
|
||||
const draggableSelector = '.drag-container [draggable]';
|
||||
delegate(document, 'dragstart', { [draggableSelector]: dragStart});
|
||||
delegate(document, 'dragover', { [draggableSelector]: dragOver});
|
||||
delegate(document, 'dragenter', { [draggableSelector]: dragEnter});
|
||||
delegate(document, 'dragleave', { [draggableSelector]: dragLeave});
|
||||
delegate(document, 'dragend', { [draggableSelector]: dragEnd});
|
||||
delegate(document, 'drop', { [draggableSelector]: drop});
|
||||
}
|
||||
|
||||
export function clearDragSource() {
|
||||
|
|
|
@ -12,12 +12,12 @@ export function leftClick<E extends MouseEvent, Target extends EventTarget>(func
|
|||
return (event: E, target: Target) => { if (event.button === 0) return func(event, target); };
|
||||
}
|
||||
|
||||
export function delegate<K extends keyof GlobalEventHandlersEventMap>(node: GlobalEventHandlers, event: K, selectors: Record<string, ((e: GlobalEventHandlersEventMap[K], target: Element) => boolean)>) {
|
||||
export function delegate<K extends keyof GlobalEventHandlersEventMap, Target extends Element>(node: GlobalEventHandlers, event: K, selectors: Record<string, ((e: GlobalEventHandlersEventMap[K], target: Target) => void | boolean)>) {
|
||||
node.addEventListener(event, e => {
|
||||
for (const selector in selectors) {
|
||||
const evtTarget = e.target as EventTarget | Element | null;
|
||||
const evtTarget = e.target as EventTarget | Target | null;
|
||||
if (evtTarget && 'closest' in evtTarget && typeof evtTarget.closest === 'function') {
|
||||
const target = evtTarget.closest(selector);
|
||||
const target = evtTarget.closest(selector) as Target;
|
||||
if (target && selectors[selector](e, target) === false) break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue