zy-react-library 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ import type { CSSProperties, FC } from "react";
2
+
3
+ export interface PdfProps {
4
+ /** pdf 文件地址 */
5
+ file: string;
6
+ /** 是否显示弹窗 */
7
+ visible?: boolean;
8
+ /** 关闭弹窗的方法 */
9
+ onCancel?: () => void;
10
+ /** 是否使用内联模式,true为不使用弹窗,默认为false */
11
+ inline?: boolean;
12
+ /** 内联模式下的样式 */
13
+ style?: CSSProperties;
14
+ }
15
+
16
+ declare const Pdf: FC<PdfProps>;
17
+
18
+ export default Pdf;
@@ -0,0 +1,84 @@
1
+ import { Button, message, Modal, Spin } from "antd";
2
+ import { useState } from "react";
3
+ import { Document, Page, pdfjs } from "react-pdf";
4
+ import { getFileUrl } from "../../utils/index";
5
+
6
+ function Pdf(props) {
7
+ const {
8
+ visible = false,
9
+ onCancel,
10
+ file,
11
+ inline = false,
12
+ style = {},
13
+ } = props;
14
+
15
+ const fileUrl = getFileUrl();
16
+ const [numPages, setNumPages] = useState(0);
17
+ const [pdfWidth, setPdfWidth] = useState(600);
18
+ const [loading, setLoading] = useState(true);
19
+
20
+ pdfjs.GlobalWorkerOptions.workerSrc = new URL(
21
+ "pdfjs-dist/build/pdf.worker.min.mjs",
22
+ import.meta.url,
23
+ ).toString();
24
+
25
+ const onDocumentLoadSuccess = ({ numPages }) => {
26
+ setNumPages(numPages);
27
+ setLoading(false);
28
+ };
29
+
30
+ const onDocumentLoadError = () => {
31
+ setLoading(false);
32
+ message.error("加载 PDF 文件失败");
33
+ if (onCancel)
34
+ onCancel();
35
+ };
36
+
37
+ const onPageLoadSuccess = ({ width }) => {
38
+ setPdfWidth(width);
39
+ };
40
+
41
+ // 内联模式的PDF内容
42
+ const renderPdfContent = () => (
43
+ <>
44
+ {loading && (
45
+ <div style={{ display: "flex", justifyContent: "center", alignItems: "center", height: "80vh" }}>
46
+ <Spin size="large" />
47
+ </div>
48
+ )}
49
+ <div style={{ height: "88vh", overflowY: "auto", padding: "24px", ...style }}>
50
+ <Document
51
+ file={fileUrl + file}
52
+ onLoadSuccess={onDocumentLoadSuccess}
53
+ onLoadError={onDocumentLoadError}
54
+ >
55
+ {
56
+ Array.from({ length: numPages }).map((el, index) => (
57
+ <Page key={`page_${index + 1}`} pageNumber={index + 1} onLoadSuccess={onPageLoadSuccess} />
58
+ ))
59
+ }
60
+ </Document>
61
+ </div>
62
+ </>
63
+ );
64
+
65
+ // 如果是内联模式,直接返回PDF内容
66
+ if (inline) {
67
+ return renderPdfContent();
68
+ }
69
+
70
+ // 默认弹窗模式
71
+ return (
72
+ <Modal
73
+ open={visible}
74
+ width={pdfWidth + 100}
75
+ title="PDF预览"
76
+ onCancel={onCancel}
77
+ footer={<Button onClick={onCancel}>关闭</Button>}
78
+ >
79
+ {renderPdfContent()}
80
+ </Modal>
81
+ );
82
+ }
83
+
84
+ export default Pdf;
@@ -0,0 +1,18 @@
1
+ import type { FC } from "react";
2
+
3
+ export interface PreviewPdfProps {
4
+ /** 文件列表,和 name、url 冲突 */
5
+ files?: { [p: string]: any }[];
6
+ /** 文件名字段名,传入 files 时会优先查找是否存在 name、fileName */
7
+ nameKey?: string;
8
+ /** 文件路径字段名,传入 files 时会优先查找是否存在 filePath */
9
+ urlKey?: string;
10
+ /** 单个文件名,和 files 冲突 */
11
+ name?: string;
12
+ /** 单个文件路径,和 files 冲突 */
13
+ url?: string;
14
+ }
15
+
16
+ declare const PreviewPdf: FC<PreviewPdfProps>;
17
+
18
+ export default PreviewPdf;
@@ -0,0 +1,76 @@
1
+ import { Button, Space } from "antd";
2
+ import { useState } from "react";
3
+ import Pdf from "../Pdf";
4
+
5
+ const PreviewPdf = (props) => {
6
+ const {
7
+ files = [],
8
+ nameKey = "",
9
+ urlKey = "",
10
+ name = "",
11
+ url = "",
12
+ } = props;
13
+
14
+ const [visible, setVisible] = useState(false);
15
+ const [src, setSrc] = useState("");
16
+
17
+ const previewPdf = (src) => {
18
+ setVisible(true);
19
+ setSrc(src);
20
+ };
21
+
22
+ const onCancel = () => {
23
+ setVisible(false);
24
+ setSrc("");
25
+ };
26
+
27
+ // 单个文件预览模式
28
+ if (files.length === 0 && name && url) {
29
+ return (
30
+ <>
31
+ <Space>
32
+ <span>{name}</span>
33
+ <Button type="primary" size="small" onClick={() => previewPdf(url)}>
34
+ 预览
35
+ </Button>
36
+ </Space>
37
+ <Pdf
38
+ visible={visible}
39
+ file={src}
40
+ onCancel={onCancel}
41
+ />
42
+ </>
43
+ );
44
+ }
45
+
46
+ // 多文件预览模式
47
+ if (files.length > 0 && !name && !url) {
48
+ return (
49
+ <>
50
+ {files.map(item => (
51
+ <div key={item.filePath || item[urlKey]} style={{ marginTop: 5 }}>
52
+ <Space>
53
+ <span>{item.name || item.fileName || item[nameKey]}</span>
54
+ <Button
55
+ type="primary"
56
+ size="small"
57
+ onClick={() => previewPdf(item.filePath || item[urlKey])}
58
+ >
59
+ 预览
60
+ </Button>
61
+ </Space>
62
+ </div>
63
+ ))}
64
+ <Pdf
65
+ visible={visible}
66
+ file={src}
67
+ onCancel={onCancel}
68
+ />
69
+ </>
70
+ );
71
+ }
72
+
73
+ return null;
74
+ };
75
+
76
+ export default PreviewPdf;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zy-react-library",
3
3
  "private": false,
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "type": "module",
6
6
  "description": "",
7
7
  "author": "LiuJiaNan",
@@ -28,6 +28,7 @@
28
28
  "antd": "^5.27.6",
29
29
  "dayjs": "^1.11.18",
30
30
  "lodash-es": "^4.17.21",
31
- "react": "^18.3.1"
31
+ "react": "^18.3.1",
32
+ "react-pdf": "^10.2.0"
32
33
  }
33
34
  }