context menu: fix go back removing context menu ?
This commit is contained in:
parent
08dce236cf
commit
6c7cb2c642
1 changed files with 19 additions and 9 deletions
|
|
@ -1,25 +1,33 @@
|
||||||
(function() {
|
(function() {
|
||||||
if (window._contextMenuInitialized) return;
|
|
||||||
window._contextMenuInitialized = true;
|
|
||||||
|
|
||||||
const menu = document.createElement('helo');
|
const menu = document.createElement('helo');
|
||||||
|
|
||||||
menu.innerHTML = `
|
menu.innerHTML = `
|
||||||
<helo-content>
|
<helo-content>
|
||||||
<img src="/static/assets/gifs/helo.gif"/>
|
<img src="/static/assets/gifs/helo.gif"/>
|
||||||
</helo-content>
|
</helo-content>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
menu.style.display = 'none';
|
menu.style.display = 'none';
|
||||||
|
|
||||||
|
function ensureMenu() {
|
||||||
|
if (!document.body.contains(menu)) {
|
||||||
document.body.appendChild(menu);
|
document.body.appendChild(menu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const showMenu = (x, y) => {
|
const showMenu = (x, y) => {
|
||||||
|
ensureMenu();
|
||||||
|
|
||||||
menu.style.display = 'block';
|
menu.style.display = 'block';
|
||||||
menu.style.left = `${x}px`;
|
menu.style.left = `${x}px`;
|
||||||
menu.style.top = `${y}px`;
|
menu.style.top = `${y}px`;
|
||||||
|
|
||||||
const rect = menu.getBoundingClientRect();
|
const rect = menu.getBoundingClientRect();
|
||||||
|
|
||||||
if (x + rect.width > window.innerWidth) {
|
if (x + rect.width > window.innerWidth) {
|
||||||
menu.style.left = `${window.innerWidth - rect.width - 10}px`;
|
menu.style.left = `${window.innerWidth - rect.width - 10}px`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (y + rect.height > window.innerHeight) {
|
if (y + rect.height > window.innerHeight) {
|
||||||
menu.style.top = `${window.innerHeight - rect.height - 10}px`;
|
menu.style.top = `${window.innerHeight - rect.height - 10}px`;
|
||||||
}
|
}
|
||||||
|
|
@ -32,12 +40,14 @@
|
||||||
document.addEventListener('contextmenu', (e) => {
|
document.addEventListener('contextmenu', (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
showMenu(e.clientX, e.clientY);
|
showMenu(e.clientX, e.clientY);
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
document.addEventListener('click', hideMenu);
|
document.addEventListener('click', hideMenu);
|
||||||
document.addEventListener('scroll', hideMenu);
|
document.addEventListener('scroll', hideMenu);
|
||||||
|
|
||||||
document.addEventListener('keydown', (e) => {
|
document.addEventListener('keydown', (e) => {
|
||||||
if (e.key === 'Escape') hideMenu();
|
if (e.key === 'Escape') hideMenu();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.body.addEventListener('htmx:historyRestore', ensureMenu);
|
||||||
})();
|
})();
|
||||||
Loading…
Add table
Add a link
Reference in a new issue