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

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import select from 'select'
import { OverlayState } from 'koel/types/ui'
2022-06-10 10:47:46 +00:00
import { eventBus, noop } 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
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.
*/
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 => {
if (process.env.NODE_ENV === 'test') {
2022-04-15 14:24:30 +00:00
return
}
window.onbeforeunload = noop
window.location.reload()
}
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
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')
}
2022-04-29 20:15:10 +00:00
export const isDemo = KOEL_ENV === 'demo'