2022-04-15 14:24:30 +00:00
|
|
|
import select from 'select'
|
2022-05-11 15:59:43 +00:00
|
|
|
import { OverlayState } from 'koel/types/ui'
|
2022-05-09 10:11:38 +00:00
|
|
|
import { arrayify, eventBus, noop, pluralize } from '@/utils'
|
2022-04-19 20:53:36 +00:00
|
|
|
import defaultCover from '@/../img/covers/unknown-album.png'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-24 18:07:14 +00:00
|
|
|
export { defaultCover }
|
|
|
|
|
2022-04-15 14:24:30 +00:00
|
|
|
/**
|
|
|
|
* Load (display) a main panel (view).
|
|
|
|
*
|
|
|
|
* @param view
|
|
|
|
* @param {...*} args Extra data to attach to the view.
|
|
|
|
*/
|
2022-05-11 15:59:43 +00:00
|
|
|
export const loadMainView = (view: MainViewName, ...args: any[]) => eventBus.emit('LOAD_MAIN_CONTENT', view, ...args)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Force reloading window regardless of "Confirm before reload" setting.
|
|
|
|
* This is handy for certain cases, for example Last.fm connect/disconnect.
|
|
|
|
*/
|
|
|
|
export const forceReloadWindow = (): void => {
|
2022-05-09 10:11:38 +00:00
|
|
|
if (process.env.NODE_ENV === 'test') {
|
2022-04-15 14:24:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onbeforeunload = noop
|
|
|
|
window.location.reload()
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:59:43 +00:00
|
|
|
export const showOverlay = (
|
|
|
|
message = 'Just a little patience…',
|
|
|
|
type: OverlayState['type'] = 'loading',
|
|
|
|
dismissible = false
|
|
|
|
) => eventBus.emit('SHOW_OVERLAY', { message, type, dismissible })
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-05-11 15:59:43 +00:00
|
|
|
export const hideOverlay = () => eventBus.emit('HIDE_OVERLAY')
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
export const copyText = (text: string): void => {
|
|
|
|
let copyArea = document.querySelector<HTMLTextAreaElement>('#copyArea')
|
|
|
|
|
|
|
|
if (!copyArea) {
|
|
|
|
copyArea = document.createElement('textarea')
|
|
|
|
copyArea.id = 'copyArea'
|
|
|
|
document.body.appendChild(copyArea)
|
|
|
|
}
|
|
|
|
|
|
|
|
copyArea.style.top = `${window.scrollY || document.documentElement.scrollTop}px`
|
|
|
|
copyArea.value = text
|
|
|
|
select(copyArea)
|
|
|
|
document.execCommand('copy')
|
|
|
|
}
|
|
|
|
|
|
|
|
const createGhostDragImage = (event: DragEvent, text: string): void => {
|
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let dragGhost = document.querySelector<HTMLElement>('#dragGhost')
|
|
|
|
|
|
|
|
if (!dragGhost) {
|
|
|
|
// Create the element to be the ghost drag image.
|
|
|
|
dragGhost = document.createElement('div')
|
|
|
|
dragGhost.id = 'dragGhost'
|
|
|
|
document.body.appendChild(dragGhost)
|
|
|
|
}
|
|
|
|
|
|
|
|
dragGhost.innerText = text
|
|
|
|
event.dataTransfer.setDragImage(dragGhost, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle song/album/artist drag start event.
|
|
|
|
*/
|
|
|
|
export const startDragging = (event: DragEvent, dragged: Song | Song[] | Album | Artist, type: DragType): void => {
|
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let text
|
|
|
|
let songIds
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'Song':
|
2022-04-20 09:37:22 +00:00
|
|
|
dragged = arrayify(<Song>dragged)
|
2022-04-15 14:24:30 +00:00
|
|
|
text = dragged.length === 1
|
|
|
|
? `${dragged[0].title} by ${dragged[0].artist.name}`
|
|
|
|
: pluralize(dragged.length, 'song')
|
|
|
|
songIds = dragged.map(song => song.id)
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'Album':
|
|
|
|
dragged = <Album>dragged
|
|
|
|
text = `All ${pluralize(dragged.songs.length, 'song')} in ${dragged.name}`
|
|
|
|
songIds = dragged.songs.map(song => song.id)
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'Artist':
|
|
|
|
dragged = <Artist>dragged
|
|
|
|
text = `All ${pluralize(dragged.songs.length, 'song')} by ${dragged.name}`
|
|
|
|
songIds = dragged.songs.map(song => song.id)
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw Error(`Invalid drag type: ${type}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
event.dataTransfer.setData('application/x-koel.text+plain', songIds.join(','))
|
|
|
|
event.dataTransfer.effectAllowed = 'move'
|
|
|
|
|
|
|
|
createGhostDragImage(event, text)
|
|
|
|
}
|
2022-04-29 20:15:10 +00:00
|
|
|
|
|
|
|
export const isDemo = KOEL_ENV === 'demo'
|