ItemActions.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { __awaiter } from "tslib";
  2. import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
  3. import { useMemo, useRef, useState } from "react";
  4. import { DeleteOutlined, DownloadOutlined, EyeOutlined, } from "@ant-design/icons";
  5. import { Button, Progress, message } from "antd";
  6. import { DownloadCancelBtn, DownloadProgress, UploadFileItem, UploadFileItemActions, UploadPictureItem, UploadPictureItemActions, } from "./style";
  7. import { requestWithPercent } from "../../utils";
  8. export const DageUploadItemActions = ({ children, disabled, file, isPictureCard, actions, downloadErrorMessage, }) => {
  9. const controller = useRef(null);
  10. const [downloading, setDownloading] = useState(false);
  11. /** 下载进度百分比 */
  12. const [downloadPercent, setDownloadPercent] = useState(0);
  13. /** 显示下载按钮 */
  14. const showDownload = (file.url || "").indexOf("http") > -1;
  15. /** 显示下载进度条 */
  16. const showDownloadProgress = useMemo(() => !isPictureCard && downloading, [isPictureCard, downloading]);
  17. const UploadItem = isPictureCard ? UploadPictureItem : UploadFileItem;
  18. const UploadItemActions = isPictureCard
  19. ? UploadPictureItemActions
  20. : UploadFileItemActions;
  21. const handleDownload = () => __awaiter(void 0, void 0, void 0, function* () {
  22. var _a;
  23. if (!file.url)
  24. return;
  25. try {
  26. setDownloading(true);
  27. controller.current = new AbortController();
  28. const res = yield requestWithPercent({
  29. url: file.url,
  30. option: {
  31. signal: (_a = controller.current) === null || _a === void 0 ? void 0 : _a.signal,
  32. },
  33. onProcess: (now, all) => {
  34. setDownloadPercent(Math.round((now / all) * 100));
  35. },
  36. });
  37. // @ts-ignore
  38. const blob = new Blob([res]);
  39. const url = URL.createObjectURL(blob);
  40. const link = document.createElement("a");
  41. link.href = url;
  42. link.target = "_blank";
  43. link.download = file.name;
  44. link.style.display = "none";
  45. document.body.appendChild(link);
  46. link.click();
  47. document.body.removeChild(link);
  48. }
  49. catch (err) {
  50. if (err instanceof Error && err.name !== "AbortError") {
  51. message.error(downloadErrorMessage);
  52. }
  53. }
  54. finally {
  55. // 等待进度条动画完成
  56. setTimeout(() => {
  57. setDownloading(false);
  58. setDownloadPercent(0);
  59. }, 300);
  60. }
  61. });
  62. const handleDownloadCancel = () => {
  63. var _a;
  64. (_a = controller.current) === null || _a === void 0 ? void 0 : _a.abort();
  65. };
  66. return (_jsxs(UploadItem, { className: "dage-upload__item", "$showDownloadProgress": showDownloadProgress, children: [children, _jsxs(UploadItemActions, { className: "dage-upload__item__actions", children: [isPictureCard && (_jsx(Button, { type: "text", size: "small", icon: _jsx(EyeOutlined, {}), onClick: actions.preview })), showDownload && (_jsx(Button, { type: "text", size: "small", loading: downloading, disabled: downloading, icon: _jsx(DownloadOutlined, {}), onClick: handleDownload })), !disabled && (_jsx(Button, { type: "text", size: "small", icon: _jsx(DeleteOutlined, {}), onClick: actions.remove }))] }), showDownloadProgress && (_jsxs(DownloadProgress, { className: "dage-upload__item__progress", children: [_jsx(Progress, { size: "small", percent: downloadPercent, style: { margin: 0 } }), downloadPercent < 100 && (_jsx(DownloadCancelBtn, { onClick: handleDownloadCancel }))] }))] }));
  67. };