Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ThinLiquid/buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinLiquid committed May 12, 2024
2 parents 588b3fa + bd1caea commit 6a15733
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 93 deletions.
12 changes: 7 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ window.onload = async () => { await updateSW() }
// Set localForage drivers
try {
await localforage.setDriver([
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
])
} catch (e) {
Expand All @@ -48,9 +48,11 @@ if (params.has('crt')) {
document.body.classList.add('crt')
}
if (params.has('debug')) {
const eruda = await import('eruda')
// @ts-expect-error
eruda.init()
;(function () {
var src = 'https://cdn.jsdelivr.net/npm/eruda';
document.body.innerHTML += ('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
document.body.innerHTML += ('<scr' + 'ipt>eruda.init();</scr' + 'ipt>');
})();
}

const auth = (): SpotifyApi => SpotifyApi.withUserAuthorization(
Expand Down Expand Up @@ -93,7 +95,7 @@ window.onload = async () => {
const queuePalette = new QueuePalette.default(player, queue)

const SearchPalette = await import('./searchpal')
const palette = new SearchPalette.default(sdk, player, localforage, queue)
const palette = new SearchPalette.default(sdk, player, queue)

const Color = await import('./color')
const color = new Color.default()
Expand Down
7 changes: 4 additions & 3 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class Metadata {
) {
// Initialize the elements
this.container = new HTML('div').classOn('meta')
this.image = new HTML('img').attr({ src: VolumeOffIcon, alt: 'Not playing' })
this.image = new HTML('img')
this.image.attr({ src: VolumeOffIcon, alt: 'Not playing' })
this.meta = new HTML('div')
this.text = new HTML('div').text('Not playing')
this.icons = new HTML('div').classOn('icons')
Expand Down Expand Up @@ -142,7 +143,7 @@ class Metadata {
private registerEvents (): void {
// Set the metadata when the metadata changes
this.player.on('metadatachange', () => {
this.setMetadata().catch(console.error)
this.setMetadata().catch((e) => console.error(e))
})

// Check if the track is liked
Expand All @@ -156,7 +157,7 @@ class Metadata {
.then((data) => {
this.like.text((data as boolean[])[0] ? 'done' : 'add')
})
.catch(console.error)
.catch((e) => console.error(e))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class Player {
this.state = 'loading'

// Check if the track is cached
if (localforage.getItem(track.id) != null) {
if (await localforage.getItem(track.id) != null) {
const cached = (await localforage.getItem(track.id)) as CachedData
this.registerMetadata(track, cached.image)
await this.playFromBuffers(cached.buffers)
Expand Down
82 changes: 2 additions & 80 deletions src/searchpal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import HTML from '@datkat21/html'
import { Album, Playlist, SearchResults, SimplifiedAlbum, SpotifyApi, Track } from '@spotify/web-api-ts-sdk'
import Player from './player'
import localforage from 'localforage'
import { CachedData } from './types'
import { throttle } from 'throttle-debounce'
import Queue from './queue'

Expand All @@ -18,13 +16,11 @@ class SearchPalette {
*
* @param sdk The Spotify API instance
* @param player The player instance
* @param localForage The localForage instance
* @param queue The queue instance
*/
constructor (
private readonly sdk: SpotifyApi | null,
private readonly player: Player,
private readonly localForage: typeof localforage,
private readonly queue: Queue
) {
// Initialize the elements
Expand Down Expand Up @@ -229,59 +225,6 @@ class SearchPalette {
})
}

/**
* Handle a cached track
*
* @private
* @param key The key of the cached track
* @memberof SearchPalette
*/
private async handleCachedTrack (key: string): Promise<void> {
const data: CachedData | null = await this.localForage.getItem(key)
if (data == null || (data.image == null || data.track == null || data.track.name == null || data.track.artists == null)) return

const item = new HTML('div').classOn('item').attr({ tabindex: '0' })
const icon = new HTML('img').classOn('image').attr({ src: data.image, alt: `${data.track.name} by ${data.track.artists.map(artist => artist.name).join(', ')}` })
const meta = new HTML('span').text(
`${data.track.name}\n${data.track.artists
.map(artist => artist.name)
.join(', ')}`
)
const icons = new HTML('div').classOn('icons')

const add = new HTML('button')
.classOn('material-symbols-sharp')
.text('playlist_add')
.appendTo(icons)

const remove = new HTML('button')
.classOn('material-symbols-sharp')
.text('delete')
.appendTo(icons)

remove.on('click', e => {
e.preventDefault()
e.stopPropagation()
this.localForage.removeItem(key).catch(console.error)
item.cleanup()
})

add.on('click', e => {
e.preventDefault()
e.stopPropagation()
this.queue.add(data.track)
})

item.appendMany(icon, meta, icons)
item.appendTo(this.container)

item.on('click', () => {
this.queue.load(data.track)
this.player.start().catch(console.error)
this.hide()
})
}

/**
* Render the search results
*
Expand Down Expand Up @@ -342,22 +285,6 @@ class SearchPalette {
})
}

/**
* Render the cached data
*
* @private
* @memberof SearchPalette
*/
private renderCached (): void {
this.container.html('')
this.container.append(new HTML('div').id('tracks'))
this.localForage.keys().then(keys => {
keys.forEach(key => {
this.handleCachedTrack(key).catch(console.error)
})
}).catch(console.error)
}

/**
* Register the events
*
Expand All @@ -384,7 +311,7 @@ class SearchPalette {
throttle(1000, async () => {
const query = this.input.getValue()
if (query === '') {
this.renderCached()
this.container.html('')
return
}
if (this.sdk != null) {
Expand All @@ -398,18 +325,13 @@ class SearchPalette {
})
)
}

opened = false

/**
* Show the search palette
*
* @memberof SearchPalette
*/
show (): void {
if (!this.opened) {
// Fill the search palette with cached data
this.localForage.ready(() => this.renderCached()).catch(console.error)
}
this.element.classOn('show')
}

Expand Down
6 changes: 2 additions & 4 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import viteCompression from 'vite-plugin-compression'
export default defineConfig({
build: {
target: 'ES2022',
sourcemap: true,
minify: 'terser'
sourcemap: true
},
plugins: [
VitePWA({
Expand All @@ -18,7 +17,6 @@ export default defineConfig({
'**/*'
]
}),
nodePolyfills(),
viteCompression()
nodePolyfills()
]
})

0 comments on commit 6a15733

Please sign in to comment.