refactor: sheetinfo
This commit is contained in:
parent
9e9f25ff80
commit
141dac44ac
3 changed files with 22 additions and 16 deletions
|
@ -6,7 +6,7 @@ import { downloadMscz } from './mscz'
|
|||
import { getFileUrl } from './file'
|
||||
import { WebMscore, loadSoundFont } from './mscore'
|
||||
import { BtnList, BtnAction, BtnListMode } from './btn'
|
||||
import { ScoreInfoInPage } from './scoreinfo'
|
||||
import { ScoreInfoInPage, SheetInfoInPage } from './scoreinfo'
|
||||
import i18n from './i18n'
|
||||
|
||||
const main = (): void => {
|
||||
|
@ -27,7 +27,7 @@ const main = (): void => {
|
|||
|
||||
btnList.add({
|
||||
name: i18n('DOWNLOAD')('PDF'),
|
||||
action: BtnAction.process(() => downloadPDF(scoreinfo), fallback, 3 * 60 * 1000 /* 3min */),
|
||||
action: BtnAction.process(() => downloadPDF(scoreinfo, new SheetInfoInPage(document)), fallback, 3 * 60 * 1000 /* 3min */),
|
||||
})
|
||||
|
||||
btnList.add({
|
||||
|
|
10
src/pdf.ts
10
src/pdf.ts
|
@ -2,7 +2,7 @@
|
|||
import { PDFWorkerHelper } from './worker-helper'
|
||||
import { getFileUrl } from './file'
|
||||
import { saveAs } from './utils'
|
||||
import { ScoreInfo } from './scoreinfo'
|
||||
import { ScoreInfo, SheetInfo } from './scoreinfo'
|
||||
|
||||
let pdfBlob: Blob
|
||||
|
||||
|
@ -23,13 +23,13 @@ const _downloadPDF = async (imgURLs: string[], imgType: 'svg' | 'png', name = ''
|
|||
saveAs(pdfBlob, `${name}.pdf`)
|
||||
}
|
||||
|
||||
export const downloadPDF = async (scoreinfo: ScoreInfo): Promise<void> => {
|
||||
const imgType = scoreinfo.sheetImgType
|
||||
const pageCount = scoreinfo.pageCount
|
||||
export const downloadPDF = async (scoreinfo: ScoreInfo, sheet: SheetInfo): Promise<void> => {
|
||||
const imgType = sheet.imgType
|
||||
const pageCount = sheet.pageCount
|
||||
|
||||
const rs = Array.from({ length: pageCount }).map((_, i) => {
|
||||
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 sheet.thumbnailUrl
|
||||
} else { // obtain image urls using the API
|
||||
return getFileUrl(scoreinfo.id, 'img', i)
|
||||
}
|
||||
|
|
|
@ -7,9 +7,6 @@ export abstract class ScoreInfo {
|
|||
abstract id: number;
|
||||
abstract title: string;
|
||||
|
||||
abstract pageCount: number;
|
||||
abstract thumbnailUrl: string;
|
||||
|
||||
public store = new Map<symbol, any>();
|
||||
|
||||
get idLastDigit (): number {
|
||||
|
@ -27,12 +24,6 @@ export abstract class ScoreInfo {
|
|||
get msczCidUrl (): string {
|
||||
return `https://ipfs.infura.io:5001/api/v0/block/stat?arg=${this.msczIpfsRef}`
|
||||
}
|
||||
|
||||
get sheetImgType (): 'svg' | 'png' {
|
||||
const thumbnail = this.thumbnailUrl
|
||||
const imgtype = thumbnail.match(/\.(\w+)$/)![1]
|
||||
return imgtype as 'svg' | 'png'
|
||||
}
|
||||
}
|
||||
|
||||
export class ScoreInfoInPage extends ScoreInfo {
|
||||
|
@ -48,6 +39,21 @@ export class ScoreInfoInPage extends ScoreInfo {
|
|||
const el = this.document.querySelector("meta[property='og:title']") as HTMLMetaElement
|
||||
return el.content
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class SheetInfo {
|
||||
abstract pageCount: number;
|
||||
abstract thumbnailUrl: string;
|
||||
|
||||
get imgType (): 'svg' | 'png' {
|
||||
const thumbnail = this.thumbnailUrl
|
||||
const imgtype = thumbnail.match(/\.(\w+)$/)![1]
|
||||
return imgtype as 'svg' | 'png'
|
||||
}
|
||||
}
|
||||
|
||||
export class SheetInfoInPage extends SheetInfo {
|
||||
constructor (private document: Document) { super() }
|
||||
|
||||
get pageCount (): number {
|
||||
return this.document.querySelectorAll('.gXB83').length
|
||||
|
|
Loading…
Reference in a new issue