Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed May 5, 2024
1 parent 47e13b6 commit 45a340a
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,24 @@ let delegatedEvents: Record<string, WeakMap<HTMLElement, (e: Event) => void>> =
click: new WeakMap(),
};

function handleDelegatedEvent(e: Event) {
let target = e.target as HTMLElement;
const body = getDocument().body;
while (target && target !== body) {
if (delegatedEvents.click.has(target)) {
break;
function handleDelegatedEvent(name: string) {
const eventsList = delegatedEvents[name];
return (e: Event) => {
let target = e.target as HTMLElement;
while (target) {
if (!target.isConnected) {
return;
} else if (eventsList.has(target)) {
break;
}
target = target.parentElement!;
}
target = target.parentElement!;
}
if (!target.isConnected) {
return;
}
const fn = delegatedEvents.click.get(target);
if (fn) {
fn(e);
eventsList.get(target)!?.(e);
}
}

Object.keys(delegatedEvents).forEach((name) => {
api.addEventListener(getDocument(), name, handleDelegatedEvent);
api.addEventListener(getDocument(), name, handleDelegatedEvent(name));
});

export function $_delegateEvent(element: HTMLElement, name: string, fn: (e: Event) => void) {
Expand Down

0 comments on commit 45a340a

Please sign in to comment.