Skip to content

Commit 8966384

Browse files
fix(cut): add title search inside path
1 parent 6144260 commit 8966384

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/js/cut.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,47 @@ const ClassName = {
1111
};
1212

1313
function toggleCut(element: HTMLElement) {
14-
const cutEl = element.parentNode as HTMLElement;
15-
cutEl.classList.toggle(ClassName.OPEN);
14+
const cutNode = element.parentNode;
15+
16+
if (!(cutNode instanceof HTMLElement)) {
17+
return;
18+
}
19+
20+
cutNode.classList.toggle(ClassName.OPEN);
21+
}
22+
23+
function matchTitle(target: EventTarget | null) {
24+
if (!(target instanceof HTMLElement)) {
25+
return false;
26+
}
27+
28+
return target?.matches?.(Selector.TITLE);
29+
}
30+
31+
function findTitleInPath(event: MouseEvent): HTMLElement | undefined {
32+
const target = getEventTarget(event);
33+
34+
if (matchTitle(target)) {
35+
return target as HTMLElement;
36+
}
37+
38+
const path = event.composedPath?.();
39+
40+
return path?.find(matchTitle) as HTMLElement | undefined;
1641
}
1742

1843
if (typeof document !== 'undefined') {
1944
document.addEventListener('click', (event) => {
20-
const target = getEventTarget(event) as HTMLElement;
21-
if (isCustom(event) || !target.matches(Selector.TITLE)) {
45+
if (isCustom(event)) {
46+
return;
47+
}
48+
49+
const title = findTitleInPath(event);
50+
51+
if (!title) {
2252
return;
2353
}
2454

25-
toggleCut(target);
55+
toggleCut(title);
2656
});
2757
}

0 commit comments

Comments
 (0)