Skip to content

Commit

Permalink
perf: 增加「只下载完成翻译的图片」选项
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Mar 9, 2024
1 parent 1fab3b8 commit 8c6df39
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"download_failed": "Download failed",
"fetch_comic_img_failed": "Failed to fetch comic images",
"img_load_failed": "Image loading failed",
"no_img_download": "No images available for download",
"repeat_load": "Loading image, please wait",
"server_connect_failed": "Unable to connect to the server"
},
Expand Down Expand Up @@ -146,6 +147,7 @@
"direction_vertical": "Vertical only",
"forceRetry": "Force retry (ignore cache)",
"localUrl": "customize server URL",
"onlyDownloadTranslated": "Download only the translated images",
"target_language": "Target language",
"text_detector": "Text detector",
"translator": "Translator"
Expand Down
2 changes: 2 additions & 0 deletions locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"download_failed": "下载失败",
"fetch_comic_img_failed": "获取漫画图片失败",
"img_load_failed": "图片加载失败",
"no_img_download": "没有能下载的图片",
"repeat_load": "加载图片中,请稍候",
"server_connect_failed": "无法连接到服务器"
},
Expand Down Expand Up @@ -146,6 +147,7 @@
"direction_vertical": "仅限垂直",
"forceRetry": "忽略缓存强制重试",
"localUrl": "自定义服务器 URL",
"onlyDownloadTranslated": "只下载完成翻译的图片",
"target_language": "目标语言",
"text_detector": "文本扫描器",
"translator": "翻译服务"
Expand Down
6 changes: 6 additions & 0 deletions src/components/Manga/components/SettingTranslation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ export const SettingTranslation = () => {
/>
</Show>
</Show>

<SettingsItemSwitch
name={t('setting.translation.options.onlyDownloadTranslated')}
value={store.option.translation.onlyDownloadTranslated}
onChange={createStateSetFn('translation.onlyDownloadTranslated')}
/>
</SettingsShowItem>
</>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/Manga/store/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export interface Option {
translator: string;
targetLanguage: string;
};
/** 只下载完成翻译的图片 */
onlyDownloadTranslated: boolean;
};
}

Expand Down Expand Up @@ -113,6 +115,7 @@ export const defaultOption: Readonly<Option> = {
direction: 'auto',
targetLanguage,
},
onlyDownloadTranslated: false,
},
};

Expand Down
33 changes: 23 additions & 10 deletions src/components/useComponents/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,29 @@ export const DownloadButton = () => {
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;
const { imgList } = store;
const imgIndexNum = `${imgList.length}`.length;

for (let i = 0; i < downImgList.length; i += 1) {
setStatu(`${i}/${downImgList.length}`);
const index = `${i}`.padStart(imgIndexNum, '0');
for (let i = 0; i < imgList.length; i += 1) {
setStatu(`${i}/${imgList.length}`);

if (
store.option.translation.onlyDownloadTranslated &&
imgList[i].translationType !== 'show'
)
continue;

let data: ArrayBuffer;
let fileName: string;

const url = downImgList[i];
const img = imgList[i];
const url =
img.translationType === 'show'
? `${img.translationUrl!}#.${getFileExt(img.src)}`
: img.src;

const index = `${i}`.padStart(imgIndexNum, '0');

if (url.startsWith('blob:')) {
const res = await fetch(url);
const blob = await res.blob();
Expand All @@ -56,6 +64,11 @@ export const DownloadButton = () => {
fileData[fileName] = new Uint8Array(data!);
}

if (Object.keys(fileData).length === 0) {
toast.warn(t('alert.no_img_download'));
setStatu('button.download');
return;
}
setStatu('button.packaging');
const zipped = zipSync(fileData, {
level: 0,
Expand Down

0 comments on commit 8c6df39

Please sign in to comment.