-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdebug_menu.js
More file actions
62 lines (51 loc) · 1.57 KB
/
debug_menu.js
File metadata and controls
62 lines (51 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import initDebugButton from './debug_button/debug_button';
import initDebugOptions from './debug_options/debug_options';
import { dispatchCustomEvent } from '../utils/dom';
import { isDebugButtonEnabled } from '../utils/meta';
export default function initDebugMenu(
metaTag,
liveDebuggerURL,
debugChannel,
shadowRoot
) {
const debugButton = initDebugButton();
const debugMenu = initDebugOptions({ liveDebuggerURL, debugChannel });
let suppressOutsideClick = false;
const suppressNext = () => {
suppressOutsideClick = true;
setTimeout(() => {
suppressOutsideClick = false;
}, 0);
};
debugButton.addEventListener('click', suppressNext, true);
debugMenu.addEventListener('click', suppressNext, true);
const mount = () => {
shadowRoot.appendChild(debugButton);
shadowRoot.appendChild(debugMenu);
};
const unmount = () => {
debugButton.remove();
debugMenu.remove();
};
if (isDebugButtonEnabled(metaTag)) mount();
debugChannel.on('toggle-debug-button', ({ enabled }) => {
if (enabled) {
mount();
} else {
unmount();
}
});
// Hide menu when clicking outside
document.addEventListener('click', (event) => {
const path = event.composedPath?.() ?? [event.target];
const clickedInside =
path.includes(debugButton) ||
path.includes(debugMenu) ||
path.some((node) => node?.getRootNode?.() === shadowRoot);
if (suppressOutsideClick) return;
if (!clickedInside) {
dispatchCustomEvent('lvdbg:click-outside-debug-menu');
}
});
return { debugButton, debugMenu };
}