-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
148 lines (119 loc) · 4.39 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import './meta'
import FileSaver from 'file-saver'
import { waitForSheetLoaded, console } from './utils'
import { downloadPDF } from './pdf'
import { downloadMscz } from './mscz'
import { getFileUrl } from './file'
import { INDV_DOWNLOADS } from './mscore'
import { getLibreScoreLink } from './librescore-link'
import { BtnList, BtnAction, BtnListMode, ICON } from './btn'
import { ScoreInfoInPage, SheetInfoInPage, getActualId } from './scoreinfo'
import i18n from './i18n'
const { saveAs } = FileSaver
const main = (): void => {
const btnList = new BtnList()
const scoreinfo = new ScoreInfoInPage(document)
const { fileName } = scoreinfo
// eslint-disable-next-line no-void
void getActualId(scoreinfo)
let indvPartBtn: HTMLButtonElement | null = null
const fallback = () => {
// btns fallback to load from MSCZ file (`Individual Parts`)
return indvPartBtn?.click()
}
btnList.add({
name: i18n('DOWNLOAD')('MSCZ'),
action: BtnAction.process(() => downloadMscz(scoreinfo, saveAs)),
})
btnList.add({
name: i18n('DOWNLOAD')('PDF'),
action: BtnAction.process(() => downloadPDF(scoreinfo, new SheetInfoInPage(document), saveAs), fallback, 3 * 60 * 1000 /* 3min */),
})
btnList.add({
name: i18n('DOWNLOAD')('MXL'),
action: BtnAction.mscoreWindow(scoreinfo, async (w, score) => {
const mxl = await score.saveMxl()
const data = new Blob([mxl])
saveAs(data, `${fileName}.mxl`)
w.close()
}),
})
btnList.add({
name: i18n('DOWNLOAD')('MIDI'),
action: BtnAction.download(() => getFileUrl(scoreinfo.id, 'midi'), fallback, 30 * 1000 /* 30s */),
})
btnList.add({
name: i18n('DOWNLOAD')('MP3'),
action: BtnAction.download(() => getFileUrl(scoreinfo.id, 'mp3'), fallback, 30 * 1000 /* 30s */),
})
indvPartBtn = btnList.add({
name: i18n('IND_PARTS')(),
tooltip: i18n('IND_PARTS_TOOLTIP')(),
action: BtnAction.mscoreWindow(scoreinfo, async (w, score, txt) => {
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: i18n('FULL_SCORE')(), parts: [] })
// render the part selection page
txt.remove()
const fieldset = w.document.createElement('fieldset')
w.document.body.append(fieldset)
const downloads = INDV_DOWNLOADS
// part selection
const DEFAULT_PART = -1 // initially select "full score"
for (const excerpt of metadata.excerpts) {
const id = excerpt.id
const partName = excerpt.title
const e = w.document.createElement('input')
e.name = 'score-part'
e.type = 'radio'
e.alt = partName
e.checked = id === DEFAULT_PART
e.onclick = () => {
return score.setExcerptId(id) // set selected part
}
const label = w.document.createElement('label')
label.innerText = partName
const br = w.document.createElement('br')
fieldset.append(e, label, br)
}
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)
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 = i18n('PROCESSING')()
const checked = fieldset.querySelector('input:checked') as HTMLInputElement
const partName = checked.alt
const data = new Blob([await d.action(score)])
saveAs(data, `${fileName} - ${partName}.${d.fileExt}`)
// unlock button
initBtn()
}
initBtn()
}
}),
})
btnList.add({
name: i18n('VIEW_IN_LIBRESCORE')(),
action: BtnAction.openUrl(() => getLibreScoreLink(scoreinfo)),
tooltip: 'BETA',
icon: ICON.LIBRESCORE,
lightTheme: true,
})
// eslint-disable-next-line @typescript-eslint/no-floating-promises
btnList.commit(BtnListMode.InPage)
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
waitForSheetLoaded().then(main)