Skip to content

Commit

Permalink
Introduce Modifier enum to represent key modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
taupiqueur committed Oct 31, 2024
1 parent 229e72a commit 8228009
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions src/manual/manual.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

const MIDDLE_MOUSE_BUTTON = 1

const Modifier = {
None: 0,
Control: 1 << 0,
Alt: 1 << 1,
Shift: 1 << 2,
Meta: 1 << 3,
}

const buttonElements = document.querySelectorAll('button')
const chromeLinkElements = document.querySelectorAll('a[href^="chrome:"]')

Expand Down Expand Up @@ -36,44 +44,29 @@ for (const buttonElement of buttonElements) {

for (const chromeLinkElement of chromeLinkElements) {
chromeLinkElement.addEventListener('click', (pointerEvent) => {
const pointerEventModifiers = (
(pointerEvent.ctrlKey ? Modifier.Control : Modifier.None) |
(pointerEvent.altKey ? Modifier.Alt : Modifier.None) |
(pointerEvent.shiftKey ? Modifier.Shift : Modifier.None) |
(pointerEvent.metaKey ? Modifier.Meta : Modifier.None)
)
if (
pointerEvent.ctrlKey &&
!pointerEvent.altKey &&
!pointerEvent.shiftKey &&
!pointerEvent.metaKey ||

!pointerEvent.ctrlKey &&
!pointerEvent.altKey &&
!pointerEvent.shiftKey &&
pointerEvent.metaKey
pointerEventModifiers === Modifier.Control ||
pointerEventModifiers === Modifier.Meta
) {
openNewBackgroundTab(chromeLinkElement.href)
pointerEvent.preventDefault()
pointerEvent.stopImmediatePropagation()
} else if (
!pointerEvent.ctrlKey &&
!pointerEvent.altKey &&
!pointerEvent.shiftKey &&
!pointerEvent.metaKey ||

pointerEvent.ctrlKey &&
!pointerEvent.altKey &&
pointerEvent.shiftKey &&
!pointerEvent.metaKey ||

!pointerEvent.ctrlKey &&
!pointerEvent.altKey &&
pointerEvent.shiftKey &&
pointerEvent.metaKey
pointerEventModifiers === Modifier.None ||
pointerEventModifiers === (Modifier.Control | Modifier.Shift) ||
pointerEventModifiers === (Modifier.Shift | Modifier.Meta)
) {
openNewForegroundTab(chromeLinkElement.href)
pointerEvent.preventDefault()
pointerEvent.stopImmediatePropagation()
} else if (
!pointerEvent.ctrlKey &&
!pointerEvent.altKey &&
pointerEvent.shiftKey &&
!pointerEvent.metaKey
pointerEventModifiers === Modifier.Shift
) {
openNewWindow(chromeLinkElement.href)
pointerEvent.preventDefault()
Expand Down

0 comments on commit 8228009

Please sign in to comment.