refactor: get rid of global scoreinfo

This commit is contained in:
Xmader 2020-11-24 02:51:43 -05:00
parent aafb71fc4b
commit 64b0e4d441
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
5 changed files with 20 additions and 24 deletions

View File

@ -1,7 +1,6 @@
/* eslint-disable no-extend-native */ /* eslint-disable no-extend-native */
/* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-return */
import scoreinfo from './scoreinfo'
import { ALL, webpackGlobalOverride } from './webpack-hook' import { ALL, webpackGlobalOverride } from './webpack-hook'
import { console } from './utils' import { console } from './utils'
@ -53,8 +52,8 @@ const magics: Record<FileType, Promise<string>> = {
mp3: magicHookConstr('mp3'), mp3: magicHookConstr('mp3'),
} }
const getApiUrl = (type: FileType, index: number): string => { const getApiUrl = (id: number, type: FileType, index: number): string => {
return `/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}&v2=1` return `/api/jmuse?id=${id}&type=${type}&index=${index}&v2=1`
} }
const getApiAuth = async (type: FileType, index: number): Promise<string> => { const getApiAuth = async (type: FileType, index: number): Promise<string> => {
@ -87,8 +86,8 @@ const getApiAuth = async (type: FileType, index: number): Promise<string> => {
return magic return magic
} }
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => { export const getFileUrl = async (id: number, type: FileType, index = 0): Promise<string> => {
const url = getApiUrl(type, index) const url = getApiUrl(id, type, index)
const auth = await getApiAuth(type, index) const auth = await getApiAuth(type, index)
const r = await fetch(url, { const r = await fetch(url, {

View File

@ -6,12 +6,13 @@ import { downloadMscz } from './mscz'
import { getFileUrl } from './file' import { getFileUrl } from './file'
import { WebMscore, loadSoundFont } from './mscore' import { WebMscore, loadSoundFont } from './mscore'
import { BtnList, BtnAction, BtnListMode } from './btn' import { BtnList, BtnAction, BtnListMode } from './btn'
import scoreinfo from './scoreinfo' import { ScoreInfoInPage } from './scoreinfo'
import i18n from './i18n' import i18n from './i18n'
const main = (): void => { const main = (): void => {
const btnList = new BtnList() const btnList = new BtnList()
const filename = scoreinfo.fileName const scoreinfo = new ScoreInfoInPage(document)
const { fileName, id } = scoreinfo
let indvPartBtn: HTMLButtonElement | null = null let indvPartBtn: HTMLButtonElement | null = null
const fallback = () => { const fallback = () => {
@ -21,12 +22,12 @@ const main = (): void => {
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MSCZ'), name: i18n('DOWNLOAD')('MSCZ'),
action: BtnAction.process(downloadMscz), action: BtnAction.process(() => downloadMscz(scoreinfo)),
}) })
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('PDF'), name: i18n('DOWNLOAD')('PDF'),
action: BtnAction.process(downloadPDF, fallback, 3 * 60 * 1000 /* 3min */), action: BtnAction.process(() => downloadPDF(scoreinfo), fallback, 3 * 60 * 1000 /* 3min */),
}) })
btnList.add({ btnList.add({
@ -34,19 +35,19 @@ const main = (): void => {
action: BtnAction.mscoreWindow(async (w, score) => { action: BtnAction.mscoreWindow(async (w, score) => {
const mxl = await score.saveMxl() const mxl = await score.saveMxl()
const data = new Blob([mxl]) const data = new Blob([mxl])
saveAs(data, `${filename}.mxl`) saveAs(data, `${fileName}.mxl`)
w.close() w.close()
}), }),
}) })
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MIDI'), name: i18n('DOWNLOAD')('MIDI'),
action: BtnAction.download(() => getFileUrl('midi'), fallback, 30 * 1000 /* 30s */), action: BtnAction.download(() => getFileUrl(id, 'midi'), fallback, 30 * 1000 /* 30s */),
}) })
btnList.add({ btnList.add({
name: i18n('DOWNLOAD')('MP3'), name: i18n('DOWNLOAD')('MP3'),
action: BtnAction.download(() => getFileUrl('mp3'), fallback, 30 * 1000 /* 30s */), action: BtnAction.download(() => getFileUrl(id, 'mp3'), fallback, 30 * 1000 /* 30s */),
}) })
indvPartBtn = btnList.add({ indvPartBtn = btnList.add({
@ -150,7 +151,7 @@ const main = (): void => {
const partName = checked.alt const partName = checked.alt
const data = new Blob([await d.action(score)]) const data = new Blob([await d.action(score)])
saveAs(data, `${filename} - ${partName}.${d.fileExt}`) saveAs(data, `${fileName} - ${partName}.${d.fileExt}`)
// unlock button // unlock button
initBtn() initBtn()

View File

@ -1,10 +1,10 @@
import { saveAs, assertRes } from './utils' import { saveAs, assertRes } from './utils'
import scoreinfo from './scoreinfo' import { ScoreInfo } from './scoreinfo'
let msczBufferP: Promise<ArrayBuffer> | undefined let msczBufferP: Promise<ArrayBuffer> | undefined
export const fetchMscz = async (): Promise<ArrayBuffer> => { export const fetchMscz = async (scoreinfo: ScoreInfo): Promise<ArrayBuffer> => {
if (!msczBufferP) { if (!msczBufferP) {
const url = scoreinfo.msczCidUrl const url = scoreinfo.msczCidUrl
msczBufferP = (async (): Promise<ArrayBuffer> => { msczBufferP = (async (): Promise<ArrayBuffer> => {
@ -21,8 +21,8 @@ export const fetchMscz = async (): Promise<ArrayBuffer> => {
return msczBufferP return msczBufferP
} }
export const downloadMscz = async (): Promise<void> => { export const downloadMscz = async (scoreinfo: ScoreInfo): Promise<void> => {
const data = new Blob([await fetchMscz()]) const data = new Blob([await fetchMscz(scoreinfo)])
const filename = scoreinfo.fileName const filename = scoreinfo.fileName
saveAs(data, `${filename}.mscz`) saveAs(data, `${filename}.mscz`)
} }

View File

@ -2,7 +2,7 @@
import { PDFWorkerHelper } from './worker-helper' import { PDFWorkerHelper } from './worker-helper'
import { getFileUrl } from './file' import { getFileUrl } from './file'
import { saveAs } from './utils' import { saveAs } from './utils'
import scoreinfo from './scoreinfo' import { ScoreInfo } from './scoreinfo'
let pdfBlob: Blob let pdfBlob: Blob
@ -23,7 +23,7 @@ const _downloadPDF = async (imgURLs: string[], imgType: 'svg' | 'png', name = ''
saveAs(pdfBlob, `${name}.pdf`) saveAs(pdfBlob, `${name}.pdf`)
} }
export const downloadPDF = async (): Promise<void> => { export const downloadPDF = async (scoreinfo: ScoreInfo): Promise<void> => {
const imgType = scoreinfo.sheetImgType const imgType = scoreinfo.sheetImgType
const pageCount = scoreinfo.pageCount const pageCount = scoreinfo.pageCount
@ -31,7 +31,7 @@ export const downloadPDF = async (): Promise<void> => {
if (i === 0) { // The url to the first page is static. We don't need to use API to obtain it. if (i === 0) { // The url to the first page is static. We don't need to use API to obtain it.
return scoreinfo.thumbnailUrl return scoreinfo.thumbnailUrl
} else { // obtain image urls using the API } else { // obtain image urls using the API
return getFileUrl('img', i) return getFileUrl(scoreinfo.id, 'img', i)
} }
}) })
const sheetImgURLs = await Promise.all(rs) const sheetImgURLs = await Promise.all(rs)

View File

@ -58,7 +58,3 @@ export class ScoreInfoInPage extends ScoreInfo {
return url.split('@')[0] return url.split('@')[0]
} }
} }
export const scoreinfo = new ScoreInfoInPage(document)
export default scoreinfo