mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-24 15:47:08 +00:00
Code-split howler
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Howl } from 'howler'
|
import type { Howl, HowlOptions } from 'howler'
|
||||||
import { useEffect, useRef, useState } from 'preact/hooks'
|
import { useEffect, useRef, useState } from 'preact/hooks'
|
||||||
import { Btn, NumberInput, RangeInput, TextInput } from '..'
|
import { Btn, NumberInput, RangeInput, TextInput } from '..'
|
||||||
import { useLocale, useVersion } from '../../contexts'
|
import { useLocale, useVersion } from '../../contexts'
|
||||||
@@ -13,12 +13,13 @@ export interface SoundConfig {
|
|||||||
volume: number,
|
volume: number,
|
||||||
}
|
}
|
||||||
type SoundConfigProps = SoundConfig & {
|
type SoundConfigProps = SoundConfig & {
|
||||||
|
howler: (options: HowlOptions) => Howl,
|
||||||
sounds: SoundEvents,
|
sounds: SoundEvents,
|
||||||
onEdit: (changes: Partial<SoundConfig>) => unknown,
|
onEdit: (changes: Partial<SoundConfig>) => unknown,
|
||||||
onDelete: () => unknown,
|
onDelete: () => unknown,
|
||||||
delayedPlay?: number,
|
delayedPlay?: number,
|
||||||
}
|
}
|
||||||
export function SoundConfig({ sounds, sound, delay, pitch, volume, onEdit, onDelete, delayedPlay }: SoundConfigProps) {
|
export function SoundConfig({ howler, sounds, sound, delay, pitch, volume, onEdit, onDelete, delayedPlay }: SoundConfigProps) {
|
||||||
const { locale } = useLocale()
|
const { locale } = useLocale()
|
||||||
const { version } = useVersion()
|
const { version } = useVersion()
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
@@ -34,7 +35,7 @@ export function SoundConfig({ sounds, sound, delay, pitch, volume, onEdit, onDel
|
|||||||
howls.current = (soundEvent?.sounds ?? []).map(entry => {
|
howls.current = (soundEvent?.sounds ?? []).map(entry => {
|
||||||
const soundPath = typeof entry === 'string' ? entry : entry.name
|
const soundPath = typeof entry === 'string' ? entry : entry.name
|
||||||
const url = getSoundUrl(version, soundPath)
|
const url = getSoundUrl(version, soundPath)
|
||||||
const howl = new Howl({
|
const howl = howler({
|
||||||
src: [url],
|
src: [url],
|
||||||
format: ['ogg'],
|
format: ['ogg'],
|
||||||
volume,
|
volume,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { Howl, HowlOptions } from 'howler'
|
||||||
import { useEffect, useRef, useState } from 'preact/hooks'
|
import { useEffect, useRef, useState } from 'preact/hooks'
|
||||||
import config from '../../config.json'
|
import config from '../../config.json'
|
||||||
import { Btn, BtnMenu, ErrorPanel, SoundConfig, TextInput } from '../components'
|
import { Btn, BtnMenu, ErrorPanel, SoundConfig, TextInput } from '../components'
|
||||||
@@ -15,6 +16,14 @@ export function Sounds({}: Props) {
|
|||||||
const [error, setError] = useState<Error | null>(null)
|
const [error, setError] = useState<Error | null>(null)
|
||||||
useTitle(locale('title.sounds'))
|
useTitle(locale('title.sounds'))
|
||||||
|
|
||||||
|
const [howler, setHowler] = useState<undefined | ((options: HowlOptions) => Howl)>(undefined)
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
const howler = await import('howler')
|
||||||
|
setHowler(() => (options: HowlOptions) => new howler.Howl(options))
|
||||||
|
})()
|
||||||
|
}, [])
|
||||||
|
|
||||||
const [sounds, setSounds] = useState<SoundEvents>({})
|
const [sounds, setSounds] = useState<SoundEvents>({})
|
||||||
const soundKeys = Object.keys(sounds ?? {})
|
const soundKeys = Object.keys(sounds ?? {})
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -71,7 +80,9 @@ export function Sounds({}: Props) {
|
|||||||
</BtnMenu>
|
</BtnMenu>
|
||||||
</div>
|
</div>
|
||||||
<div class="sounds">
|
<div class="sounds">
|
||||||
{configs.map(c => <SoundConfig key={c.id} {...c} {...{ sounds, delayedPlay }} onEdit={editConfig(c.id)} onDelete={deleteConfig(c.id)} />)}
|
{howler && configs.map(c =>
|
||||||
|
<SoundConfig key={c.id} {...c} {...{ howler, sounds, delayedPlay }} onEdit={editConfig(c.id)} onDelete={deleteConfig(c.id)} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<a ref={download} style="display: none;"></a>
|
<a ref={download} style="display: none;"></a>
|
||||||
</>}
|
</>}
|
||||||
|
|||||||
Reference in New Issue
Block a user