From 141dac44ac83288c0f6abe114f82deb69201f95e Mon Sep 17 00:00:00 2001 From: Xmader Date: Tue, 24 Nov 2020 03:17:50 -0500 Subject: [PATCH] refactor: sheetinfo --- src/main.ts | 4 ++-- src/pdf.ts | 10 +++++----- src/scoreinfo.ts | 24 +++++++++++++++--------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index db87a74..0c7935a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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({ diff --git a/src/pdf.ts b/src/pdf.ts index 56c86d2..c9da3a8 100644 --- a/src/pdf.ts +++ b/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 => { - const imgType = scoreinfo.sheetImgType - const pageCount = scoreinfo.pageCount +export const downloadPDF = async (scoreinfo: ScoreInfo, sheet: SheetInfo): Promise => { + 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) } diff --git a/src/scoreinfo.ts b/src/scoreinfo.ts index 8b105a2..53c0f8a 100644 --- a/src/scoreinfo.ts +++ b/src/scoreinfo.ts @@ -7,9 +7,6 @@ export abstract class ScoreInfo { abstract id: number; abstract title: string; - abstract pageCount: number; - abstract thumbnailUrl: string; - public store = new Map(); 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