Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS updates #7

Merged
merged 6 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"trailingComma": "none",
"endOfLine": "lf"
}
51 changes: 26 additions & 25 deletions themes/default/assets/js/gamer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
globalThis?.addEventListener('DOMContentLoaded', () => {
const document = globalThis?.document
globalThis.addEventListener('DOMContentLoaded', () => {
const document = globalThis.document
if (document == null) return
initHandlers(document)
})
Expand All @@ -17,7 +17,7 @@ const initHandler = (element) => {
if (textEl == null || iconEl == null) return

let n = games.findIndex(
({ icon }) => iconEl.getAttribute('data-icon') === icon
({ icon }) => iconEl.getAttribute('data-icon') === icon,
)
preloadSounds(n)

Expand Down Expand Up @@ -48,95 +48,96 @@ const initSound = (n) => {
}

const createSound = (src) => {
const fetch = globalThis?.fetch
const fetch = globalThis.fetch
const preloaded = fetch(src)
.then((res) => res.arrayBuffer())
.catch(console.error)

return {
play: () => preloaded.then(playAudioBuffer).catch(console.error)
play: () => preloaded.then(playAudioBuffer).catch(console.error),
}
}

let audioContext
const playAudioBuffer = async (arrayBuffer) => {
const AudioContext = globalThis?.AudioContext
const context = audioContext ?? new AudioContext()
const source = context.createBufferSource()
source.buffer = await context.decodeAudioData(arrayBuffer.slice(0))
source.connect(context.destination)
const AudioContext = globalThis.AudioContext
if (AudioContext == null) return
audioContext ??= new AudioContext()
const source = audioContext.createBufferSource()
source.buffer = await audioContext.decodeAudioData(arrayBuffer.slice(0))
source.connect(audioContext.destination)
source.start()
}

const games = [
{
icon: 'alien-8bit-solid',
sound:
'{{ (resources.Get "audio/shoot.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/shoot.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'block-question-solid',
sound:
'{{ (resources.Get "audio/coin.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/coin.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'mushroom-duotone',
sound:
'{{ (resources.Get "audio/1up.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/1up.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'dungeon-duotone',
sound:
'{{ (resources.Get "audio/secret.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/secret.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'treasure-chest-solid',
sound:
'{{ (resources.Get "audio/item.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/item.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'skull-duotone',
sound:
'{{ (resources.Get "audio/eekum-bokum.mp3" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/eekum-bokum.mp3" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'music-regular',
sound:
'{{ (resources.Get "audio/note.ogg" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/note.ogg" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'puzzle-piece-duotone',
sound:
'{{ (resources.Get "audio/jiggy.ogg" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/jiggy.ogg" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'starfighter-twin-ion-engine-solid',
sound:
'{{ (resources.Get "audio/tie-fighter.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/tie-fighter.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'starfighter-solid',
sound:
'{{ (resources.Get "audio/laser.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/laser.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'starship-freighter-solid',
sound:
'{{ (resources.Get "audio/chewbacca.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/chewbacca.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'starship-solid',
sound:
'{{ (resources.Get "audio/intercom.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/intercom.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'ring-duotone',
sound:
'{{ (resources.Get "audio/ring.wav" | resources.Fingerprint).RelPermalink }}'
'{{ (resources.Get "audio/ring.wav" | resources.Fingerprint).RelPermalink }}',
},
{
icon: 'ghost-duotone',
sound:
'{{ (resources.Get "audio/death.mp3" | resources.Fingerprint).RelPermalink }}'
}
'{{ (resources.Get "audio/death.mp3" | resources.Fingerprint).RelPermalink }}',
},
]
4 changes: 2 additions & 2 deletions themes/default/assets/js/modal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
globalThis?.addEventListener('DOMContentLoaded', () => {
const document = globalThis?.document
globalThis.addEventListener('DOMContentLoaded', () => {
const document = globalThis.document
if (document == null) return
initHandlers(document)
})
Expand Down
14 changes: 7 additions & 7 deletions themes/default/assets/js/theme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
globalThis?.addEventListener('DOMContentLoaded', () => {
const document = globalThis?.document
globalThis.addEventListener('DOMContentLoaded', () => {
const document = globalThis.document
if (document == null) return
initHandlers(document)
})
Expand All @@ -24,26 +24,26 @@ const initHandler = (document, element) => {
const toggleTheme = (document) => {
const colorScheme = activeColorScheme === 'dark' ? 'light' : 'dark'
document.querySelector('html').classList.add('theme-transistion')
setTimeout(() => {
globalThis.setTimeout(() => {
setTheme(document, colorScheme)
}, 0)
setTimeout(() => {
globalThis.setTimeout(() => {
document.querySelector('html').classList.remove('theme-transistion')
}, 2000)
}

const setTheme = (document, colorScheme) => {
activeColorScheme = colorScheme
globalThis?.sessionStorage?.setItem('activeColorScheme', colorScheme)
globalThis.sessionStorage?.setItem('activeColorScheme', colorScheme)
document.querySelector('html').setAttribute('data-theme', colorScheme)
}

const getActiveTheme = () =>
globalThis?.sessionStorage?.getItem('activeColorScheme') ??
globalThis.sessionStorage?.getItem('activeColorScheme') ??
preferedColorScheme()

const preferedColorScheme = () => {
const matchMedia = globalThis?.matchMedia
const matchMedia = globalThis.matchMedia
if (matchMedia == null) return
const isDark = matchMedia('(prefers-color-scheme: dark)').matches
return isDark ? 'dark' : 'light'
Expand Down