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'
|
2020-09-27 16:20:33 +00:00
|
|
|
import { downloadMscz } from './mscz'
|
2020-10-18 11:21:25 +00:00
|
|
|
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'
|
2020-11-05 05:22:15 +00:00
|
|
|
import i18n from './i18n'
|
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())
|
2020-10-18 11:21:25 +00:00
|
|
|
const filename = scoreinfo.fileName
|
2020-05-19 04:15:22 +00:00
|
|
|
|
|
|
|
btnList.add({
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('MSCZ'),
|
2020-05-19 04:15:22 +00:00
|
|
|
action: BtnAction.process(downloadMscz),
|
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('PDF'),
|
2020-11-09 18:58:25 +00:00
|
|
|
action: BtnAction.process(downloadPDF),
|
2020-05-19 04:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('MusicXML'),
|
2020-09-27 16:20:33 +00:00
|
|
|
action: BtnAction.mscoreWindow(async (w, score) => {
|
|
|
|
const mxl = await score.saveMxl()
|
|
|
|
const data = new Blob([mxl])
|
2020-10-18 11:21:25 +00:00
|
|
|
saveAs(data, `${filename}.mxl`)
|
2020-09-27 16:20:33 +00:00
|
|
|
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-09 18:58:25 +00:00
|
|
|
action: BtnAction.download(() => getFileUrl('midi')),
|
2020-05-19 04:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('MP3'),
|
2020-10-26 01:08:39 +00:00
|
|
|
action: BtnAction.download(() => getFileUrl('mp3')),
|
2020-05-19 04:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('IND_PARTS')(),
|
|
|
|
tooltip: i18n('IND_PARTS_TOOLTIP')(),
|
2020-09-27 16:20:33 +00:00
|
|
|
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)
|
|
|
|
|
2020-09-28 21:08:52 +00:00
|
|
|
// 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-09-28 21:08:52 +00:00
|
|
|
|
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-09-28 21:08:52 +00:00
|
|
|
|
2020-10-20 05:38:12 +00:00
|
|
|
interface IndividualDownload {
|
|
|
|
name: string;
|
|
|
|
fileExt: string;
|
2020-10-20 05:58:39 +00:00
|
|
|
action (score: WebMscore): Promise<Uint8Array>;
|
2020-10-20 05:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const downloads: IndividualDownload[] = [
|
|
|
|
{
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('PDF'),
|
2020-10-20 05:38:12 +00:00
|
|
|
fileExt: 'pdf',
|
|
|
|
action: (score) => score.savePdf(),
|
|
|
|
},
|
|
|
|
{
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('MSCZ'),
|
2020-10-20 05:38:12 +00:00
|
|
|
fileExt: 'mscz',
|
|
|
|
action: (score) => score.saveMsc('mscz'),
|
|
|
|
},
|
|
|
|
{
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('MusicXML'),
|
2020-10-20 05:38:12 +00:00
|
|
|
fileExt: 'mxl',
|
|
|
|
action: (score) => score.saveMxl(),
|
|
|
|
},
|
2020-10-20 05:58:39 +00:00
|
|
|
{
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD')('MIDI'),
|
2020-10-20 05:58:39 +00:00
|
|
|
fileExt: 'mid',
|
|
|
|
action: (score) => score.saveMidi(true, true),
|
|
|
|
},
|
|
|
|
{
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD_AUDIO')('FLAC'),
|
2020-10-20 05:58:39 +00:00
|
|
|
fileExt: 'flac',
|
|
|
|
action: (score) => loadSoundFont(score).then(() => score.saveAudio('flac')),
|
|
|
|
},
|
|
|
|
{
|
2020-11-05 05:22:15 +00:00
|
|
|
name: i18n('DOWNLOAD_AUDIO')('OGG'),
|
2020-10-20 05:58:39 +00:00
|
|
|
fileExt: 'ogg',
|
|
|
|
action: (score) => loadSoundFont(score).then(() => score.saveAudio('ogg')),
|
|
|
|
},
|
2020-10-20 05:38:12 +00:00
|
|
|
]
|
|
|
|
|
2020-09-28 21:08:52 +00:00
|
|
|
// part selection
|
2020-11-09 19:04:18 +00:00
|
|
|
const DEFAULT_PART = -1 // initially select "full score"
|
2020-05-19 04:15:22 +00:00
|
|
|
for (const excerpt of metadata.excerpts) {
|
2020-05-19 04:40:16 +00:00
|
|
|
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'
|
2020-05-19 04:40:16 +00:00
|
|
|
e.alt = partName
|
2020-11-09 19:04:18 +00:00
|
|
|
e.checked = id === DEFAULT_PART
|
2020-10-20 04:54:53 +00:00
|
|
|
e.onclick = () => {
|
2020-10-20 05:38:12 +00:00
|
|
|
return score.setExcerptId(id) // set selected part
|
2020-10-20 04:54:53 +00:00
|
|
|
}
|
2020-05-19 04:40:16 +00:00
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
const label = w.document.createElement('label')
|
2020-05-19 04:40:16 +00:00
|
|
|
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
|
|
|
}
|
2020-09-28 21:08:52 +00:00
|
|
|
|
2020-11-09 19:04:18 +00:00
|
|
|
await score.setExcerptId(DEFAULT_PART)
|
2020-10-20 05:10:41 +00:00
|
|
|
|
2020-10-20 05:09:28 +00:00
|
|
|
// submit buttons
|
2020-10-20 05:38:12 +00:00
|
|
|
for (const d of downloads) {
|
2020-10-20 05:09:28 +00:00
|
|
|
const submitBtn = w.document.createElement('input')
|
|
|
|
submitBtn.type = 'submit'
|
2020-10-20 05:38:12 +00:00
|
|
|
submitBtn.style.margin = '0.5em'
|
2020-10-20 05:09:28 +00:00
|
|
|
fieldset.append(submitBtn)
|
2019-11-03 20:01:29 +00:00
|
|
|
|
2020-10-20 05:38:12 +00:00
|
|
|
const initBtn = () => {
|
|
|
|
submitBtn.onclick = onSubmit
|
|
|
|
submitBtn.disabled = false
|
|
|
|
submitBtn.value = d.name
|
|
|
|
}
|
|
|
|
|
2020-10-20 05:09:28 +00:00
|
|
|
const onSubmit = async (): Promise<void> => {
|
|
|
|
// lock the button when processing
|
|
|
|
submitBtn.onclick = null
|
2020-10-20 05:38:12 +00:00
|
|
|
submitBtn.disabled = true
|
2020-11-05 05:22:15 +00:00
|
|
|
submitBtn.value = i18n('PROCESSING')()
|
2020-10-20 04:55:40 +00:00
|
|
|
|
2020-10-20 05:09:28 +00:00
|
|
|
const checked = fieldset.querySelector('input:checked') as HTMLInputElement
|
|
|
|
const partName = checked.alt
|
2020-05-19 04:15:22 +00:00
|
|
|
|
2020-10-20 05:38:12 +00:00
|
|
|
const data = new Blob([await d.action(score)])
|
|
|
|
saveAs(data, `${filename} - ${partName}.${d.fileExt}`)
|
2020-10-20 04:55:40 +00:00
|
|
|
|
2020-10-20 05:38:12 +00:00
|
|
|
// unlock button
|
|
|
|
initBtn()
|
2020-10-20 05:09:28 +00:00
|
|
|
}
|
2020-10-20 05:38:12 +00:00
|
|
|
|
|
|
|
initBtn()
|
2020-05-19 04:15:22 +00:00
|
|
|
}
|
2020-09-27 16:20:33 +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)
|