musescore-downloader/src/main.ts

170 lines
4.6 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-05-18 22:44:45 +00:00
import { waitForDocumentLoaded, saveAs } from './utils'
import { downloadPDF } from './pdf'
import { downloadMscz } from './mscz'
import { getFileUrl } from './file'
2020-10-20 05:58:39 +00:00
import { WebMscore, loadSoundFont } from './mscore'
2020-05-19 04:15:22 +00:00
import { getDownloadBtn, BtnList, BtnAction } from './btn'
2020-05-17 22:57:28 +00:00
import * as recaptcha from './recaptcha'
2020-05-18 22:44:45 +00:00
import scoreinfo from './scoreinfo'
2019-12-01 09:11:40 +00:00
2020-05-17 22:57:28 +00:00
const main = (): void => {
// init recaptcha
// eslint-disable-next-line @typescript-eslint/no-floating-promises
recaptcha.init()
2019-12-01 05:13:14 +00:00
2020-05-19 04:15:22 +00:00
const btnList = new BtnList(getDownloadBtn())
const filename = scoreinfo.fileName
2020-05-19 04:15:22 +00:00
btnList.add({
name: 'Download MSCZ',
action: BtnAction.process(downloadMscz),
})
btnList.add({
name: 'Download PDF',
action: BtnAction.deprecate(
BtnAction.process(downloadPDF),
),
2020-05-19 04:15:22 +00:00
})
btnList.add({
name: 'Download MusicXML',
action: BtnAction.mscoreWindow(async (w, score) => {
const mxl = await score.saveMxl()
const data = new Blob([mxl])
saveAs(data, `${filename}.mxl`)
w.close()
}),
2020-05-19 04:15:22 +00:00
})
btnList.add({
name: 'Download MIDI',
action: BtnAction.deprecate(
BtnAction.download(() => getFileUrl('midi')),
),
2020-05-19 04:15:22 +00:00
})
btnList.add({
name: 'Download MP3',
action: BtnAction.download(() => getFileUrl('mp3')),
2020-05-19 04:15:22 +00:00
})
btnList.add({
name: 'Individual Parts',
2020-09-28 21:41:51 +00:00
tooltip: 'Download individual parts (BETA)',
action: BtnAction.mscoreWindow(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"
metadata.excerpts.unshift({ id: -1, title: '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)
interface IndividualDownload {
name: string;
fileExt: string;
2020-10-20 05:58:39 +00:00
action (score: WebMscore): Promise<Uint8Array>;
}
const downloads: IndividualDownload[] = [
{
name: 'Download PDF',
fileExt: 'pdf',
action: (score) => score.savePdf(),
},
{
name: 'Download Part MSCZ',
fileExt: 'mscz',
action: (score) => score.saveMsc('mscz'),
},
{
name: 'Download Part MusicXML',
fileExt: 'mxl',
action: (score) => score.saveMxl(),
},
2020-10-20 05:58:39 +00:00
{
name: 'Download MIDI',
fileExt: 'mid',
action: (score) => score.saveMidi(true, true),
},
{
name: 'Download FLAC Audio',
fileExt: 'flac',
action: (score) => loadSoundFont(score).then(() => score.saveAudio('flac')),
},
{
name: 'Download OGG Audio',
fileExt: 'ogg',
action: (score) => loadSoundFont(score).then(() => score.saveAudio('ogg')),
},
]
// part selection
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 === 0 // initially select the first 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(0) // initially select the first 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
submitBtn.value = '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)])
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-05-19 04:15:22 +00:00
btnList.commit()
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
2019-11-29 23:07:43 +00:00
waitForDocumentLoaded().then(main)