refactor: file-saver

This commit is contained in:
Xmader 2020-11-24 12:35:14 -05:00
parent caba1c041a
commit bb6eba5fdb
No known key found for this signature in database
GPG key ID: A20B97FB9EB730E4
4 changed files with 10 additions and 10 deletions

View file

@ -1,6 +1,7 @@
import './meta'
import { waitForDocumentLoaded, saveAs, console } from './utils'
import FileSaver from 'file-saver'
import { waitForDocumentLoaded, console } from './utils'
import { downloadPDF } from './pdf'
import { downloadMscz } from './mscz'
import { getFileUrl } from './file'
@ -9,6 +10,8 @@ import { BtnList, BtnAction, BtnListMode } from './btn'
import { ScoreInfoInPage, SheetInfoInPage } from './scoreinfo'
import i18n from './i18n'
const { saveAs } = FileSaver
const main = (): void => {
const btnList = new BtnList()
const scoreinfo = new ScoreInfoInPage(document)
@ -22,7 +25,7 @@ const main = (): void => {
btnList.add({
name: i18n('DOWNLOAD')('MSCZ'),
action: BtnAction.process(() => downloadMscz(scoreinfo)),
action: BtnAction.process(() => downloadMscz(scoreinfo, saveAs)),
})
btnList.add({

View file

@ -1,5 +1,5 @@
import { saveAs, assertRes, getFetch } from './utils'
import { assertRes, getFetch } from './utils'
import { ScoreInfo } from './scoreinfo'
const MSCZ_BUF_SYM = Symbol('msczBufferP')
@ -29,7 +29,7 @@ export const fetchMscz = async (scoreinfo: ScoreInfo, _fetch = getFetch()): Prom
return msczBufferP
}
export const downloadMscz = async (scoreinfo: ScoreInfo): Promise<void> => {
export const downloadMscz = async (scoreinfo: ScoreInfo, saveAs: typeof import('file-saver').saveAs): Promise<void> => {
const data = new Blob([await fetchMscz(scoreinfo)])
const filename = scoreinfo.fileName
saveAs(data, `${filename}.mscz`)

View file

@ -1,14 +1,14 @@
import { PDFWorkerHelper } from './worker-helper'
import { getFileUrl } from './file'
import { saveAs } from './utils'
import FileSaver from 'file-saver'
import { ScoreInfo, SheetInfo } from './scoreinfo'
let pdfBlob: Blob
const _downloadPDF = async (imgURLs: string[], imgType: 'svg' | 'png', name = ''): Promise<void> => {
if (pdfBlob) {
return saveAs(pdfBlob, `${name}.pdf`)
return FileSaver.saveAs(pdfBlob, `${name}.pdf`)
}
const cachedImg = document.querySelector('img[src*=score_]') as HTMLImageElement
@ -20,7 +20,7 @@ const _downloadPDF = async (imgURLs: string[], imgType: 'svg' | 'png', name = ''
pdfBlob = new Blob([pdfArrayBuffer])
saveAs(pdfBlob, `${name}.pdf`)
FileSaver.saveAs(pdfBlob, `${name}.pdf`)
}
export const downloadPDF = async (scoreinfo: ScoreInfo, sheet: SheetInfo): Promise<void> => {

View file

@ -1,9 +1,6 @@
import FileSaver from 'file-saver/dist/FileSaver.js'
import isNodeJs from 'detect-node'
export const saveAs: typeof import('file-saver').saveAs = FileSaver.saveAs
export const getIndexPath = (id: number): string => {
const idStr = String(id)
// 获取最后三位,倒序排列