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

This commit is contained in:
Xmader 2020-11-13 01:25:39 -05:00
commit e98d639000
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
3 changed files with 36 additions and 10 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,13 +160,13 @@ export namespace BtnAction {
}) })
} }
export const download = (url: UrlInput): BtnAction => { export const download = (url: UrlInput, fallback?: () => Promisable<void>, timeout?: number): BtnAction => {
return process(async (): Promise<any> => { return process(async (): Promise<void> => {
const _url = await normalizeUrlInput(url) const _url = await normalizeUrlInput(url)
const a = document.createElement('a') const a = document.createElement('a')
a.href = _url a.href = _url
a.dispatchEvent(new MouseEvent('click')) a.dispatchEvent(new MouseEvent('click'))
}) }, fallback, timeout)
} }
export const mscoreWindow = (fn: (w: Window, score: WebMscore, processingTextEl: ChildNode) => any): BtnAction => { export const mscoreWindow = (fn: (w: Window, score: WebMscore, processingTextEl: ChildNode) => any): BtnAction => {
@ -199,7 +200,7 @@ export namespace BtnAction {
} }
} }
export const process = (fn: () => any): BtnAction => { export const process = (fn: () => any, fallback?: () => Promisable<void>, timeout = Infinity): BtnAction => {
return async (name, btn, setText): Promise<void> => { return async (name, btn, setText): Promise<void> => {
const _onclick = btn.onclick const _onclick = btn.onclick
@ -207,11 +208,17 @@ export namespace BtnAction {
setText(i18n('PROCESSING')()) setText(i18n('PROCESSING')())
try { try {
await fn() await useTimeout(fn(), timeout)
setText(name) setText(name)
} catch (err) { } catch (err) {
setText(i18n('BTN_ERROR')())
console.error(err) console.error(err)
if (fallback) {
// use fallback
await fallback()
setText(name)
} else {
setText(i18n('BTN_ERROR')())
}
} }
btn.onclick = _onclick btn.onclick = _onclick

View File

@ -13,6 +13,12 @@ const main = (): void => {
const btnList = new BtnList() const btnList = new BtnList()
const filename = scoreinfo.fileName const filename = scoreinfo.fileName
let indvPartBtn: HTMLButtonElement | null = null
const fallback = () => {
// btns fallback to load from MSCZ file (`Individual Parts`)
return indvPartBtn?.click()
}
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MSCZ'), name: i18n('DOWNLOAD')('MSCZ'),
action: BtnAction.process(downloadMscz), action: BtnAction.process(downloadMscz),
@ -20,7 +26,7 @@ const main = (): void => {
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('PDF'), name: i18n('DOWNLOAD')('PDF'),
action: BtnAction.process(downloadPDF), action: BtnAction.process(downloadPDF, fallback, 3 * 60 * 1000 /* 3min */),
}) })
btnList.add({ btnList.add({
@ -35,15 +41,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'), fallback, 30 * 1000 /* 30s */),
}) })
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MP3'), name: i18n('DOWNLOAD')('MP3'),
action: BtnAction.download(() => getFileUrl('mp3')), action: BtnAction.download(() => getFileUrl('mp3'), fallback, 30 * 1000 /* 30s */),
}) })
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 => {