koel/resources/assets/js/utils/common.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import select from 'select'
2024-05-19 05:49:42 +00:00
import { isSong, noop } from '@/utils'
import defaultCover from '@/../img/covers/default.svg'
2022-04-15 14:24:30 +00:00
export { defaultCover }
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 => {
if (process.env.NODE_ENV === 'test') {
2022-04-15 14:24:30 +00:00
return
}
window.onbeforeunload = noop
window.location.reload()
}
2024-01-18 11:13:05 +00:00
export const copyText = async (text: string) => {
try {
await navigator.clipboard.writeText(text)
} catch (error: unknown) {
2024-01-18 11:13:05 +00:00
let copyArea = document.querySelector<HTMLTextAreaElement>('#copyArea')
2022-04-15 14:24:30 +00:00
2024-01-18 11:13:05 +00:00
if (!copyArea) {
copyArea = document.createElement('textarea')
copyArea.id = 'copyArea'
document.body.appendChild(copyArea)
}
2022-04-15 14:24:30 +00:00
2024-01-18 11:13:05 +00:00
copyArea.style.top = `${window.scrollY || document.documentElement.scrollTop}px`
copyArea.value = text
select(copyArea)
document.execCommand('copy')
}
2022-04-15 14:24:30 +00:00
}
2024-05-19 05:49:42 +00:00
export const getPlayableProp = <T>(playable: Playable, songKey: keyof Song, episodeKey: keyof Episode): T => {
return isSong(playable) ? playable[songKey] : playable[episodeKey]
}