feat: btns fallback to load from MSCZ file (`Individual Parts`)

This commit is contained in:
Xmader 2020-11-13 00:54:20 -05:00
parent 0764117544
commit 9aa6c406c6
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
3 changed files with 31 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import { loadMscore, WebMscore } from './mscore' import { loadMscore, WebMscore } from './mscore'
import { useTimeout } from './utils'
import i18n from './i18n' import i18n from './i18n'
// @ts-ignore // @ts-ignore
import btnListCss from './btn.css' import btnListCss from './btn.css'
@ -159,12 +160,18 @@ export namespace BtnAction {
}) })
} }
export const download = (url: UrlInput): BtnAction => { export const download = (url: UrlInput, fallback?: () => Promisable<void>, timeout = 30 * 1000): BtnAction => {
return process(async (): Promise<any> => { return process(async (): Promise<void> => {
const _url = await normalizeUrlInput(url) try {
const a = document.createElement('a') const _url = await useTimeout(normalizeUrlInput(url), timeout)
a.href = _url const a = document.createElement('a')
a.dispatchEvent(new MouseEvent('click')) a.href = _url
a.dispatchEvent(new MouseEvent('click'))
} catch (err) {
// use fallback
console.error(err)
return fallback?.()
}
}) })
} }

View File

@ -13,6 +13,8 @@ const main = (): void => {
const btnList = new BtnList() const btnList = new BtnList()
const filename = scoreinfo.fileName const filename = scoreinfo.fileName
let indvPartBtn: HTMLButtonElement | null = null
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MSCZ'), name: i18n('DOWNLOAD')('MSCZ'),
action: BtnAction.process(downloadMscz), action: BtnAction.process(downloadMscz),
@ -35,15 +37,15 @@ const main = (): void => {
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MIDI'), name: i18n('DOWNLOAD')('MIDI'),
action: BtnAction.download(() => getFileUrl('midi')), action: BtnAction.download(() => getFileUrl('midi'), () => indvPartBtn?.click()),
}) })
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MP3'), name: i18n('DOWNLOAD')('MP3'),
action: BtnAction.download(() => getFileUrl('mp3')), action: BtnAction.download(() => getFileUrl('mp3'), () => indvPartBtn?.click()),
}) })
btnList.add({ indvPartBtn = btnList.add({
name: i18n('IND_PARTS')(), name: i18n('IND_PARTS')(),
tooltip: i18n('IND_PARTS_TOOLTIP')(), tooltip: i18n('IND_PARTS_TOOLTIP')(),
action: BtnAction.mscoreWindow(async (w, score, txt) => { action: BtnAction.mscoreWindow(async (w, score, txt) => {

View File

@ -19,6 +19,19 @@ export const fetchData = async (url: string, init?: RequestInit): Promise<Uint8A
return new Uint8Array(data) return new Uint8Array(data)
} }
export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promise<T> => {
if (!(promise instanceof Promise)) {
return promise
}
return new Promise((resolve, reject) => {
const i = setTimeout(() => {
reject(new Error('timeout'))
}, ms)
promise.then(resolve, reject).finally(() => clearTimeout(i))
})
}
export const waitForDocumentLoaded = (): Promise<void> => { export const waitForDocumentLoaded = (): Promise<void> => {
if (document.readyState !== 'complete') { if (document.readyState !== 'complete') {
return new Promise(resolve => { return new Promise(resolve => {