-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add right-click context menu for ADV/DIS rolls
- Loading branch information
Showing
10 changed files
with
268 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
"fonts", | ||
"/characters.js", | ||
"/css/dialog.css", | ||
"/css/contextmenu.css", | ||
"/css/styles.css" | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import svgLogo from "~/icons/icon.svg"; | ||
import { getOptions } from "~/utils/storage"; | ||
import { triggerTalespire } from "~/utils/talespire"; | ||
|
||
const removeAllMenus = () => { | ||
for (const activeMenu of document.querySelectorAll( | ||
".tales-beyond-extension-contextmenu", | ||
)) { | ||
activeMenu.remove(); | ||
} | ||
|
||
window.removeEventListener("click", detectLightDismiss); | ||
window.removeEventListener("scroll", removeAllMenus); | ||
}; | ||
|
||
const detectLightDismiss = (event) => { | ||
const activeMenu = Array.from( | ||
document.querySelectorAll(".tales-beyond-extension-contextmenu"), | ||
); | ||
|
||
const clickedMenu = activeMenu.some((elem) => elem.contains(event.target)); | ||
if (!clickedMenu) { | ||
removeAllMenus(); | ||
} | ||
}; | ||
|
||
const setupListeners = (button, contextmenu) => { | ||
const label = button.dataset.tsLabel; | ||
const dice = button.dataset.tsDice; | ||
|
||
const action = (labelSuffix) => () => { | ||
if (labelSuffix) { | ||
const name = label ? `${label} (${labelSuffix})` : labelSuffix; | ||
triggerTalespire(name, dice, true); | ||
} else { | ||
triggerTalespire(label, dice); | ||
} | ||
|
||
removeAllMenus(); | ||
}; | ||
|
||
const [adv, flat, dis] = contextmenu.querySelectorAll(".item"); | ||
adv.addEventListener("click", action("ADV")); | ||
flat.addEventListener("click", action()); | ||
dis.addEventListener("click", action("DIS")); | ||
|
||
window.addEventListener("click", detectLightDismiss, { | ||
capture: true, | ||
passive: true, | ||
}); | ||
|
||
window.addEventListener("scroll", removeAllMenus, { | ||
capture: true, | ||
passive: true, | ||
}); | ||
}; | ||
|
||
const positionMenu = (button, contextmenu) => { | ||
const menu = contextmenu.querySelector(".menu"); | ||
const arrow = contextmenu.querySelector(" .arrow"); | ||
|
||
const buttonRect = button.getBoundingClientRect(); | ||
const menuRect = menu.getBoundingClientRect(); | ||
|
||
const top = | ||
(buttonRect.top + buttonRect.bottom) / 2 - | ||
menuRect.height / 2 + | ||
window.scrollY; | ||
|
||
menu.style.top = `${top}px`; | ||
|
||
// Avoid end of screen | ||
if (window.innerWidth < buttonRect.right + menuRect.width + 10) { | ||
arrow.classList.add("right"); | ||
menu.style.left = `${buttonRect.left - menuRect.right - 10}px`; | ||
} else { | ||
arrow.classList.add("left"); | ||
menu.style.left = `${buttonRect.right - menuRect.left + 10}px`; | ||
} | ||
}; | ||
|
||
const contextMenu = (event) => { | ||
const diceButton = event.target.closest(".integrated-dice__container"); | ||
if (!diceButton) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
|
||
removeAllMenus(); | ||
|
||
const contextmenu = document.createElement("div"); | ||
contextmenu.classList.add("tales-beyond-extension-contextmenu"); | ||
contextmenu.innerHTML = ` | ||
<div class="menu"> | ||
<div class="arrow"></div> | ||
<h2> | ||
<img> | ||
Tales Beyond | ||
</h2> | ||
<hr /> | ||
<div class="item advantage">Advantage</div> | ||
<div class="item">Normal</div> | ||
<div class="item disadvantage">Disadvantage</div> | ||
</div> | ||
`; | ||
const img = contextmenu.querySelector("img"); | ||
img.src = svgLogo; | ||
|
||
document.body.appendChild(contextmenu); | ||
positionMenu(diceButton, contextmenu); | ||
setupListeners(diceButton, contextmenu); | ||
}; | ||
|
||
export const injectContextMenu = async () => { | ||
const settings = await getOptions(); | ||
if (settings.contextMenuEnabled) { | ||
window.addEventListener("contextmenu", contextMenu); | ||
} else { | ||
window.removeEventListener("contextmenu", contextMenu); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
.tales-beyond-extension-contextmenu .arrow { | ||
position: absolute; | ||
width: 0; | ||
height: 0; | ||
border-top: 10px solid transparent; | ||
border-bottom: 10px solid transparent; | ||
top: calc(50% - 10px); | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .arrow.left { | ||
border-right: 10px solid #27272c; | ||
left: -10px; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .arrow.right { | ||
border-left: 10px solid #27272c; | ||
right: -10px; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .menu { | ||
position: absolute; | ||
z-index: 2000; | ||
background: #27272c; | ||
padding: 6px 8px; | ||
color: #c5c5ce; | ||
border-radius: .5em; | ||
box-shadow: 0 .5rem 1rem rgba(0, 0, 0, 0.3); | ||
text-align: center; | ||
font-size: 12px; | ||
min-width: max-content; | ||
/* Required because of Popover API */ | ||
inset: 0px auto auto 0px; | ||
margin: 0; | ||
border: none; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .item { | ||
padding: 6px 8px; | ||
margin: 2px; | ||
line-height: 18px; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .item:hover { | ||
background-color: #ab79d6; | ||
color: #351d4a; | ||
cursor: pointer; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .item.advantage:not(:hover) { | ||
text-decoration: green wavy underline; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu .item.disadvantage:not(:hover) { | ||
text-decoration: red wavy underline; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu h2 { | ||
color: #ab79d6; | ||
margin: 0rem 0 0 0; | ||
display: flex; | ||
gap: .5rem; | ||
line-height: 1.2; | ||
align-items: center; | ||
font-size: 1rem; | ||
} | ||
|
||
.tales-beyond-extension-contextmenu h2 img { | ||
width: 1.5rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters