2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-18 22:44:45 +00:00
|
|
|
import FileSaver from 'file-saver/dist/FileSaver.js'
|
|
|
|
|
|
|
|
export const saveAs: typeof import('file-saver').saveAs = FileSaver.saveAs
|
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
export const getIndexPath = (id: number): string => {
|
|
|
|
const idStr = String(id)
|
|
|
|
// 获取最后三位,倒序排列
|
|
|
|
// x, y, z are the reversed last digits of the score id. Example: id 123456789, x = 9, y = 8, z = 7
|
|
|
|
// https://developers.musescore.com/#/file-urls
|
|
|
|
// "5449062" -> ["2", "6", "0"]
|
|
|
|
const indexN = idStr.split('').reverse().slice(0, 3)
|
|
|
|
return indexN.join('/')
|
2019-11-03 19:13:06 +00:00
|
|
|
}
|
2019-11-29 23:07:43 +00:00
|
|
|
|
2020-10-20 05:39:38 +00:00
|
|
|
export const fetchData = async (url: string, init?: RequestInit): Promise<Uint8Array> => {
|
|
|
|
const r = await fetch(url, init)
|
|
|
|
const data = await r.arrayBuffer()
|
|
|
|
return new Uint8Array(data)
|
|
|
|
}
|
|
|
|
|
2019-11-29 23:07:43 +00:00
|
|
|
export const waitForDocumentLoaded = (): Promise<void> => {
|
2020-05-17 22:57:28 +00:00
|
|
|
if (document.readyState !== 'complete') {
|
|
|
|
return new Promise(resolve => {
|
2020-10-23 14:46:45 +00:00
|
|
|
const cb = () => {
|
2020-05-17 22:57:28 +00:00
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
resolve()
|
2020-10-23 14:46:45 +00:00
|
|
|
document.removeEventListener('readystatechange', cb)
|
2020-05-17 22:57:28 +00:00
|
|
|
}
|
2020-10-23 14:46:45 +00:00
|
|
|
}
|
|
|
|
document.addEventListener('readystatechange', cb)
|
2020-05-17 22:57:28 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
2019-11-29 23:07:43 +00:00
|
|
|
}
|