-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import MdFileDownload from '@material-design-icons/svg/round/file_download.svg'; | ||
|
||
import { zipSync, type Zippable } from 'fflate'; | ||
import { createMemo, createSignal } from 'solid-js'; | ||
import { saveAs } from 'helper'; | ||
import { request } from 'helper/request'; | ||
import { t } from 'helper/i18n'; | ||
import { store } from '../Manga'; | ||
import { IconButton } from '../IconButton'; | ||
import { toast } from './Toast'; | ||
|
||
/** 下载按钮 */ | ||
export const DownloadButton = () => { | ||
const [statu, setStatu] = createSignal('button.download'); | ||
|
||
const getFileExt = (url: string) => url.split('.').pop(); | ||
|
||
const handleDownload = async () => { | ||
const fileData: Zippable = {}; | ||
|
||
const downImgList = store.imgList.map((img) => | ||
img.translationType === 'show' | ||
? `${img.translationUrl!}#.${getFileExt(img.src)}` | ||
: img.src, | ||
); | ||
const imgIndexNum = `${downImgList.length}`.length; | ||
|
||
for (let i = 0; i < downImgList.length; i += 1) { | ||
setStatu(`${i}/${downImgList.length}`); | ||
const index = `${i}`.padStart(imgIndexNum, '0'); | ||
|
||
let data: ArrayBuffer; | ||
let fileName: string; | ||
|
||
const url = downImgList[i]; | ||
if (url.startsWith('blob:')) { | ||
const res = await fetch(url); | ||
const blob = await res.blob(); | ||
data = await blob.arrayBuffer(); | ||
const fileExt = blob.type.split('/')[1]; | ||
fileName = `${index}.${fileExt}`; | ||
} else { | ||
const fileExt = getFileExt(url) ?? 'jpg'; | ||
fileName = `${index}.${fileExt}`; | ||
try { | ||
const res = await request<ArrayBufferLike>(url, { | ||
responseType: 'arraybuffer', | ||
errorText: `${t('alert.download_failed')}: ${fileName}`, | ||
}); | ||
data = res.response; | ||
} catch (error) { | ||
fileName = `${index} - ${t('alert.download_failed')}.${fileExt}`; | ||
} | ||
} | ||
|
||
fileData[fileName] = new Uint8Array(data!); | ||
} | ||
|
||
setStatu('button.packaging'); | ||
const zipped = zipSync(fileData, { | ||
level: 0, | ||
comment: window.location.href, | ||
}); | ||
saveAs(new Blob([zipped]), `${document.title}.zip`); | ||
setStatu('button.download_completed'); | ||
toast.success(t('button.download_completed')); | ||
}; | ||
|
||
const tip = createMemo( | ||
() => t(statu()) || `${t('button.downloading')} - ${statu()}`, | ||
); | ||
|
||
return ( | ||
<IconButton tip={tip()} onClick={handleDownload}> | ||
<MdFileDownload /> | ||
</IconButton> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import MdClose from '@material-design-icons/svg/round/close.svg'; | ||
|
||
import { t } from 'helper/i18n'; | ||
import { IconButton } from '../../components/IconButton'; | ||
import type { MangaProps } from '../../components/Manga'; | ||
import { buttonListDivider } from '../../components/Manga'; | ||
import { DownloadButton } from '../../components/useComponents/DownloadButton'; | ||
import { handleExit } from './store'; | ||
|
||
const ExitButton = () => ( | ||
<IconButton tip={t('button.exit')} onClick={handleExit}> | ||
<MdClose /> | ||
</IconButton> | ||
); | ||
|
||
export const editButtonList: MangaProps['editButtonList'] = (list) => { | ||
// 在设置按钮上方放置下载按钮 | ||
list.splice(-1, 0, DownloadButton); | ||
return [ | ||
...list, | ||
// 再在最下面添加分隔栏和退出按钮 | ||
buttonListDivider, | ||
ExitButton, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters