refactor: get rid of global scoreinfo
This commit is contained in:
parent
aafb71fc4b
commit
64b0e4d441
5 changed files with 20 additions and 24 deletions
|
@ -1,7 +1,6 @@
|
|||
/* eslint-disable no-extend-native */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
|
||||
import scoreinfo from './scoreinfo'
|
||||
import { ALL, webpackGlobalOverride } from './webpack-hook'
|
||||
import { console } from './utils'
|
||||
|
||||
|
@ -53,8 +52,8 @@ const magics: Record<FileType, Promise<string>> = {
|
|||
mp3: magicHookConstr('mp3'),
|
||||
}
|
||||
|
||||
const getApiUrl = (type: FileType, index: number): string => {
|
||||
return `/api/jmuse?id=${scoreinfo.id}&type=${type}&index=${index}&v2=1`
|
||||
const getApiUrl = (id: number, type: FileType, index: number): string => {
|
||||
return `/api/jmuse?id=${id}&type=${type}&index=${index}&v2=1`
|
||||
}
|
||||
|
||||
const getApiAuth = async (type: FileType, index: number): Promise<string> => {
|
||||
|
@ -87,8 +86,8 @@ const getApiAuth = async (type: FileType, index: number): Promise<string> => {
|
|||
return magic
|
||||
}
|
||||
|
||||
export const getFileUrl = async (type: FileType, index = 0): Promise<string> => {
|
||||
const url = getApiUrl(type, index)
|
||||
export const getFileUrl = async (id: number, type: FileType, index = 0): Promise<string> => {
|
||||
const url = getApiUrl(id, type, index)
|
||||
const auth = await getApiAuth(type, index)
|
||||
|
||||
const r = await fetch(url, {
|
||||
|
|
17
src/main.ts
17
src/main.ts
|
@ -6,12 +6,13 @@ import { downloadMscz } from './mscz'
|
|||
import { getFileUrl } from './file'
|
||||
import { WebMscore, loadSoundFont } from './mscore'
|
||||
import { BtnList, BtnAction, BtnListMode } from './btn'
|
||||
import scoreinfo from './scoreinfo'
|
||||
import { ScoreInfoInPage } from './scoreinfo'
|
||||
import i18n from './i18n'
|
||||
|
||||
const main = (): void => {
|
||||
const btnList = new BtnList()
|
||||
const filename = scoreinfo.fileName
|
||||
const scoreinfo = new ScoreInfoInPage(document)
|
||||
const { fileName, id } = scoreinfo
|
||||
|
||||
let indvPartBtn: HTMLButtonElement | null = null
|
||||
const fallback = () => {
|
||||
|
@ -21,12 +22,12 @@ const main = (): void => {
|
|||
|
||||
btnList.add({
|
||||
name: i18n('DOWNLOAD')('MSCZ'),
|
||||
action: BtnAction.process(downloadMscz),
|
||||
action: BtnAction.process(() => downloadMscz(scoreinfo)),
|
||||
})
|
||||
|
||||
btnList.add({
|
||||
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({
|
||||
|
@ -34,19 +35,19 @@ const main = (): void => {
|
|||
action: BtnAction.mscoreWindow(async (w, score) => {
|
||||
const mxl = await score.saveMxl()
|
||||
const data = new Blob([mxl])
|
||||
saveAs(data, `${filename}.mxl`)
|
||||
saveAs(data, `${fileName}.mxl`)
|
||||
w.close()
|
||||
}),
|
||||
})
|
||||
|
||||
btnList.add({
|
||||
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({
|
||||
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({
|
||||
|
@ -150,7 +151,7 @@ const main = (): void => {
|
|||
const partName = checked.alt
|
||||
|
||||
const data = new Blob([await d.action(score)])
|
||||
saveAs(data, `${filename} - ${partName}.${d.fileExt}`)
|
||||
saveAs(data, `${fileName} - ${partName}.${d.fileExt}`)
|
||||
|
||||
// unlock button
|
||||
initBtn()
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
|
||||
import { saveAs, assertRes } from './utils'
|
||||
import scoreinfo from './scoreinfo'
|
||||
import { ScoreInfo } from './scoreinfo'
|
||||
|
||||
let msczBufferP: Promise<ArrayBuffer> | undefined
|
||||
|
||||
export const fetchMscz = async (): Promise<ArrayBuffer> => {
|
||||
export const fetchMscz = async (scoreinfo: ScoreInfo): Promise<ArrayBuffer> => {
|
||||
if (!msczBufferP) {
|
||||
const url = scoreinfo.msczCidUrl
|
||||
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
||||
|
@ -21,8 +21,8 @@ export const fetchMscz = async (): Promise<ArrayBuffer> => {
|
|||
return msczBufferP
|
||||
}
|
||||
|
||||
export const downloadMscz = async (): Promise<void> => {
|
||||
const data = new Blob([await fetchMscz()])
|
||||
export const downloadMscz = async (scoreinfo: ScoreInfo): Promise<void> => {
|
||||
const data = new Blob([await fetchMscz(scoreinfo)])
|
||||
const filename = scoreinfo.fileName
|
||||
saveAs(data, `${filename}.mscz`)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
import { PDFWorkerHelper } from './worker-helper'
|
||||
import { getFileUrl } from './file'
|
||||
import { saveAs } from './utils'
|
||||
import scoreinfo from './scoreinfo'
|
||||
import { ScoreInfo } from './scoreinfo'
|
||||
|
||||
let pdfBlob: Blob
|
||||
|
||||
|
@ -23,7 +23,7 @@ const _downloadPDF = async (imgURLs: string[], imgType: 'svg' | 'png', name = ''
|
|||
saveAs(pdfBlob, `${name}.pdf`)
|
||||
}
|
||||
|
||||
export const downloadPDF = async (): Promise<void> => {
|
||||
export const downloadPDF = async (scoreinfo: ScoreInfo): Promise<void> => {
|
||||
const imgType = scoreinfo.sheetImgType
|
||||
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.
|
||||
return scoreinfo.thumbnailUrl
|
||||
} else { // obtain image urls using the API
|
||||
return getFileUrl('img', i)
|
||||
return getFileUrl(scoreinfo.id, 'img', i)
|
||||
}
|
||||
})
|
||||
const sheetImgURLs = await Promise.all(rs)
|
||||
|
|
|
@ -58,7 +58,3 @@ export class ScoreInfoInPage extends ScoreInfo {
|
|||
return url.split('@')[0]
|
||||
}
|
||||
}
|
||||
|
||||
export const scoreinfo = new ScoreInfoInPage(document)
|
||||
|
||||
export default scoreinfo
|
||||
|
|
Loading…
Reference in a new issue