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 { fetchMscz, downloadMscz } from './mscz'
|
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 WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.5/webmscore.js'
|
2020-05-13 10:29:11 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
const main = (): void => {
|
|
|
|
// @ts-ignore
|
|
|
|
if (!window.UGAPP || !window.UGAPP.store || !window.UGAPP.store.jmuse_settings) { return }
|
2019-11-03 19:13:06 +00:00
|
|
|
|
2020-05-17 22:57:28 +00:00
|
|
|
// 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())
|
|
|
|
|
|
|
|
btnList.add({
|
|
|
|
name: 'Download MSCZ',
|
|
|
|
action: BtnAction.process(downloadMscz),
|
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
|
|
|
name: 'Download PDF',
|
|
|
|
action: BtnAction.process(downloadPDF),
|
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
|
|
|
name: 'Download MusicXML',
|
|
|
|
action: BtnAction.openUrl(scoreinfo.mxlUrl),
|
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
|
|
|
name: 'Download MIDI',
|
|
|
|
action: BtnAction.openUrl(scoreinfo.midiUrl),
|
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
|
|
|
name: 'Download MP3',
|
|
|
|
action: BtnAction.openUrl(scoreinfo.mp3Url),
|
|
|
|
})
|
|
|
|
|
|
|
|
btnList.add({
|
|
|
|
name: 'Individual Parts',
|
|
|
|
async action (btnName, btn, setText) {
|
|
|
|
const _onclick = btn.onclick
|
|
|
|
btn.onclick = null
|
|
|
|
setText(BtnAction.PROCESSING_TEXT)
|
|
|
|
|
|
|
|
const w = window.open('') as Window
|
|
|
|
const txt = document.createTextNode(BtnAction.PROCESSING_TEXT)
|
|
|
|
w.document.body.append(txt)
|
|
|
|
|
|
|
|
// set page hooks
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
let score: any
|
|
|
|
const destroy = (): void => {
|
|
|
|
score.destroy()
|
|
|
|
w.close()
|
2020-05-17 22:57:28 +00:00
|
|
|
}
|
2020-05-19 04:15:22 +00:00
|
|
|
window.addEventListener('unload', destroy)
|
|
|
|
w.addEventListener('beforeunload', () => {
|
|
|
|
score.destroy()
|
|
|
|
window.removeEventListener('unload', destroy)
|
|
|
|
setText(btnName)
|
|
|
|
btn.onclick = _onclick
|
|
|
|
})
|
|
|
|
|
|
|
|
// load webmscore (https://github.com/LibreScore/webmscore)
|
|
|
|
const script = w.document.createElement('script')
|
|
|
|
script.src = WEBMSCORE_URL
|
|
|
|
w.document.body.append(script)
|
|
|
|
await new Promise(resolve => { script.onload = resolve })
|
|
|
|
|
|
|
|
// parse mscz data
|
|
|
|
const data = new Uint8Array(
|
|
|
|
new Uint8Array(await fetchMscz()) // copy its ArrayBuffer
|
|
|
|
)
|
|
|
|
score = await w['WebMscore'].load('mscz', data)
|
|
|
|
await score.generateExcerpts()
|
|
|
|
const metadata = await score.metadata()
|
|
|
|
console.log('score metadata loaded by webmscore', metadata)
|
|
|
|
|
|
|
|
// render the part selection page
|
|
|
|
txt.remove()
|
|
|
|
const fieldset = w.document.createElement('fieldset')
|
|
|
|
for (const excerpt of metadata.excerpts) {
|
|
|
|
const e = w.document.createElement('input')
|
|
|
|
e.name = 'score-part'
|
|
|
|
e.type = 'radio'
|
|
|
|
e.value = excerpt.id
|
|
|
|
const label = w.document.createElement('label')
|
|
|
|
label.innerText = excerpt.title
|
|
|
|
const br = w.document.createElement('br')
|
|
|
|
fieldset.append(e, label, br)
|
2020-05-17 22:57:28 +00:00
|
|
|
}
|
2020-05-19 04:15:22 +00:00
|
|
|
const submitBtn = w.document.createElement('input')
|
|
|
|
submitBtn.type = 'submit'
|
|
|
|
submitBtn.value = 'Download PDF'
|
|
|
|
fieldset.append(submitBtn)
|
|
|
|
w.document.body.append(fieldset)
|
2019-11-03 20:01:29 +00:00
|
|
|
|
2020-05-19 04:15:22 +00:00
|
|
|
submitBtn.onclick = async (): Promise<void> => {
|
|
|
|
const checked = fieldset.querySelector('input:checked') as HTMLInputElement
|
|
|
|
const id = checked.value
|
|
|
|
|
|
|
|
await score.setExcerptId(id)
|
|
|
|
|
|
|
|
const filename = scoreinfo.fileName
|
|
|
|
const data = new Blob([await score.savePdf()])
|
|
|
|
saveAs(data, `${filename}-part-${id}.pdf`)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}).title = 'Download individual parts (BETA)'
|
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)
|