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

38 lines
983 B
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import select from 'select'
import { 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 (e) {
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
}