musescore-downloader/src/main.ts

147 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-05-17 22:57:28 +00:00
import './meta'
2019-11-03 19:13:06 +00:00
2020-11-24 17:35:14 +00:00
import FileSaver from 'file-saver'
import { waitForSheetLoaded, console } from './utils'
2020-05-18 22:44:45 +00:00
import { downloadPDF } from './pdf'
import { downloadMscz } from './mscz'
import { getFileUrl } from './file'
2020-11-24 11:03:46 +00:00
import { INDV_DOWNLOADS } from './mscore'
2020-12-28 04:51:37 +00:00
import { getLibreScoreLink } from './librescore-link'
import { BtnList, BtnAction, BtnListMode, ICON } from './btn'
2020-11-29 22:47:39 +00:00
import { ScoreInfoInPage, SheetInfoInPage, getActualId } from './scoreinfo'
2020-11-05 05:22:15 +00:00
import i18n from './i18n'
2019-12-01 09:11:40 +00:00
2020-11-24 17:35:14 +00:00
const { saveAs } = FileSaver
2020-05-17 22:57:28 +00:00
const main = (): void => {
2020-11-12 18:02:50 +00:00
const btnList = new BtnList()
2020-11-24 07:51:43 +00:00
const scoreinfo = new ScoreInfoInPage(document)
2020-11-29 22:47:39 +00:00
const { fileName } = scoreinfo
// eslint-disable-next-line no-void
void getActualId(scoreinfo)
2020-05-19 04:15:22 +00:00
let indvPartBtn: HTMLButtonElement | null = null
2020-11-13 06:00:45 +00:00
const fallback = () => {
// btns fallback to load from MSCZ file (`Individual Parts`)
return indvPartBtn?.click()
}
2020-05-19 04:15:22 +00:00
btnList.add({
2020-11-05 05:22:15 +00:00
name: i18n('DOWNLOAD')('MSCZ'),
2020-11-24 17:35:14 +00:00
action: BtnAction.process(() => downloadMscz(scoreinfo, saveAs)),
2020-05-19 04:15:22 +00:00
})
btnList.add({
2020-11-05 05:22:15 +00:00
name: i18n('DOWNLOAD')('PDF'),
2020-11-24 08:17:50 +00:00
action: BtnAction.process(() => downloadPDF(scoreinfo, new SheetInfoInPage(document)), fallback, 3 * 60 * 1000 /* 3min */),
2020-05-19 04:15:22 +00:00
})
btnList.add({
2020-11-05 05:22:15 +00:00
name: i18n('DOWNLOAD')('MusicXML'),
2020-11-24 09:38:47 +00:00
action: BtnAction.mscoreWindow(scoreinfo, async (w, score) => {
const mxl = await score.saveMxl()
const data = new Blob([mxl])
2020-11-24 07:51:43 +00:00
saveAs(data, `${fileName}.mxl`)
w.close()
}),
2020-05-19 04:15:22 +00:00
})
btnList.add({
2020-11-05 05:22:15 +00:00
name: i18n('DOWNLOAD')('MIDI'),
2020-11-29 22:47:39 +00:00
action: BtnAction.download(() => getFileUrl(scoreinfo.id, 'midi'), fallback, 30 * 1000 /* 30s */),
2020-05-19 04:15:22 +00:00
})
btnList.add({
2020-11-05 05:22:15 +00:00
name: i18n('DOWNLOAD')('MP3'),
2020-11-29 22:47:39 +00:00
action: BtnAction.download(() => getFileUrl(scoreinfo.id, 'mp3'), fallback, 30 * 1000 /* 30s */),
2020-05-19 04:15:22 +00:00
})
indvPartBtn = btnList.add({
2020-11-05 05:22:15 +00:00
name: i18n('IND_PARTS')(),
tooltip: i18n('IND_PARTS_TOOLTIP')(),
2020-11-24 09:38:47 +00:00
action: BtnAction.mscoreWindow(scoreinfo, async (w, score, txt) => {
2020-05-19 04:15:22 +00:00
const metadata = await score.metadata()
console.log('score metadata loaded by webmscore', metadata)
// add the "full score" option as a "part"
2020-11-05 05:22:15 +00:00
metadata.excerpts.unshift({ id: -1, title: i18n('FULL_SCORE')(), parts: [] })
2020-05-19 04:15:22 +00:00
// render the part selection page
txt.remove()
const fieldset = w.document.createElement('fieldset')
2020-10-20 04:54:53 +00:00
w.document.body.append(fieldset)
2020-11-24 11:03:46 +00:00
const downloads = INDV_DOWNLOADS
// part selection
const DEFAULT_PART = -1 // initially select "full score"
2020-05-19 04:15:22 +00:00
for (const excerpt of metadata.excerpts) {
const id = excerpt.id
const partName = excerpt.title
2020-05-19 04:15:22 +00:00
const e = w.document.createElement('input')
e.name = 'score-part'
e.type = 'radio'
e.alt = partName
e.checked = id === DEFAULT_PART
2020-10-20 04:54:53 +00:00
e.onclick = () => {
return score.setExcerptId(id) // set selected part
2020-10-20 04:54:53 +00:00
}
2020-05-19 04:15:22 +00:00
const label = w.document.createElement('label')
label.innerText = partName
2020-05-19 04:15:22 +00:00
const br = w.document.createElement('br')
fieldset.append(e, label, br)
2020-05-17 22:57:28 +00:00
}
await score.setExcerptId(DEFAULT_PART)
// submit buttons
for (const d of downloads) {
const submitBtn = w.document.createElement('input')
submitBtn.type = 'submit'
submitBtn.style.margin = '0.5em'
fieldset.append(submitBtn)
2019-11-03 20:01:29 +00:00
const initBtn = () => {
submitBtn.onclick = onSubmit
submitBtn.disabled = false
submitBtn.value = d.name
}
const onSubmit = async (): Promise<void> => {
// lock the button when processing
submitBtn.onclick = null
submitBtn.disabled = true
2020-11-05 05:22:15 +00:00
submitBtn.value = i18n('PROCESSING')()
const checked = fieldset.querySelector('input:checked') as HTMLInputElement
const partName = checked.alt
2020-05-19 04:15:22 +00:00
const data = new Blob([await d.action(score)])
2020-11-24 07:51:43 +00:00
saveAs(data, `${fileName} - ${partName}.${d.fileExt}`)
// unlock button
initBtn()
}
initBtn()
2020-05-19 04:15:22 +00:00
}
}),
2020-09-28 21:41:51 +00:00
})
2019-11-29 23:07:43 +00:00
2020-12-28 04:51:37 +00:00
btnList.add({
2021-01-02 07:44:43 +00:00
name: i18n('VIEW_IN_LIBRESCORE')(),
2020-12-28 04:51:37 +00:00
action: BtnAction.openUrl(() => getLibreScoreLink(scoreinfo)),
tooltip: 'BETA',
icon: ICON.LIBRESCORE,
})
btnList.commit(BtnListMode.InPage)
2019-11-29 23:07:43 +00:00
}
2019-11-03 19:13:06 +00:00
2020-05-17 22:57:28 +00:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
waitForSheetLoaded().then(main)