Skip to content

Commit

Permalink
fix(cut): add title search inside path
Browse files Browse the repository at this point in the history
  • Loading branch information
Postamentovich committed Sep 6, 2022
1 parent 6144260 commit 8966384
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/js/cut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,47 @@ const ClassName = {
};

function toggleCut(element: HTMLElement) {
const cutEl = element.parentNode as HTMLElement;
cutEl.classList.toggle(ClassName.OPEN);
const cutNode = element.parentNode;

if (!(cutNode instanceof HTMLElement)) {
return;
}

cutNode.classList.toggle(ClassName.OPEN);
}

function matchTitle(target: EventTarget | null) {
if (!(target instanceof HTMLElement)) {
return false;
}

return target?.matches?.(Selector.TITLE);
}

function findTitleInPath(event: MouseEvent): HTMLElement | undefined {
const target = getEventTarget(event);

if (matchTitle(target)) {
return target as HTMLElement;
}

const path = event.composedPath?.();

return path?.find(matchTitle) as HTMLElement | undefined;
}

if (typeof document !== 'undefined') {
document.addEventListener('click', (event) => {
const target = getEventTarget(event) as HTMLElement;
if (isCustom(event) || !target.matches(Selector.TITLE)) {
if (isCustom(event)) {
return;
}

const title = findTitleInPath(event);

if (!title) {
return;
}

toggleCut(target);
toggleCut(title);
});
}

0 comments on commit 8966384

Please sign in to comment.