szld-libs 0.1.7 → 0.1.8

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.
@@ -7,6 +7,7 @@ interface BackHeaderProps {
7
7
  className?: string;
8
8
  style?: CSSProperties;
9
9
  titleStyle?: CSSProperties;
10
+ level?: 1 | 2 | 3 | 4 | 5;
10
11
  }
11
12
  declare const BackHeader: (props: BackHeaderProps) => JSX.Element;
12
13
  export default BackHeader;
@@ -20,7 +20,8 @@ const BackHeader = (props) => {
20
20
  subTitle = null,
21
21
  className,
22
22
  style,
23
- titleStyle
23
+ titleStyle,
24
+ level = 2
24
25
  } = props;
25
26
  const navigate = useNavigate();
26
27
  const renderSubtitle = () => {
@@ -35,7 +36,7 @@ const BackHeader = (props) => {
35
36
  return /* @__PURE__ */ jsxs("div", { className: classNames(styles.main, className), style, children: [
36
37
  /* @__PURE__ */ jsxs(Space, { children: [
37
38
  isBack && /* @__PURE__ */ jsx(ArrowLeftOutlined, { style: { fontSize: 24 }, onClick: goBack }),
38
- /* @__PURE__ */ jsxs(Title, { level: 2, className: styles.title, style: titleStyle, children: [
39
+ /* @__PURE__ */ jsxs(Title, { level, className: styles.title, style: titleStyle, children: [
39
40
  title2,
40
41
  renderSubtitle()
41
42
  ] })
@@ -4,6 +4,11 @@ export interface UploadFileProps extends UploadProps {
4
4
  maxSize?: number;
5
5
  uploadBtnName?: string | ReactNode;
6
6
  uploadHint?: any;
7
+ compression?: {
8
+ type?: string;
9
+ quality?: number;
10
+ limit?: number;
11
+ };
7
12
  }
8
13
  declare const UploadFile: (props: UploadFileProps) => JSX.Element;
9
14
  export default UploadFile;
@@ -5,6 +5,7 @@ import { FileSuffix } from "../../utils/filetype";
5
5
  import { useState } from "react";
6
6
  import { getFileSuffix, getBase64 } from "../../utils";
7
7
  import "react-dom";
8
+ import { compressionFile } from "../../utils/compression-file";
8
9
  const defaultFileType = [FileSuffix.jpeg, FileSuffix.jpg, FileSuffix.png];
9
10
  const UploadFile = (props) => {
10
11
  const {
@@ -14,7 +15,8 @@ const UploadFile = (props) => {
14
15
  maxSize = 2,
15
16
  accept = defaultFileType.join(","),
16
17
  uploadBtnName = "上传",
17
- uploadHint = null
18
+ uploadHint = null,
19
+ compression
18
20
  } = props;
19
21
  const [preview, setPreview] = useState(false);
20
22
  const [previewSrc, setPreviewSrc] = useState("");
@@ -34,18 +36,33 @@ const UploadFile = (props) => {
34
36
  ] });
35
37
  };
36
38
  const beforeUpload = (file) => {
37
- const acceptList = accept.split(",");
38
- const suffix = getFileSuffix(file.name) || "";
39
- if (!acceptList.includes(suffix)) {
40
- message.error(`只允许上传 ${accept} 文件`);
41
- return Upload.LIST_IGNORE;
42
- }
43
- const isLess = file.size / 1024 / 1024 < maxSize;
44
- if (!isLess) {
45
- message.error(`文件必须小于${maxSize}MB`);
39
+ const result = new Promise((resolve, reject) => {
40
+ const acceptList = accept.split(",");
41
+ const suffix = getFileSuffix(file.name) || "";
42
+ if (!acceptList.includes(suffix)) {
43
+ message.error(`只允许上传 ${accept} 文件`);
44
+ reject(false);
45
+ }
46
+ const isLess = file.size / 1024 / 1024 < maxSize;
47
+ if (!isLess) {
48
+ message.error(`文件必须小于${maxSize}MB`);
49
+ reject(false);
50
+ }
51
+ if (compression) {
52
+ resolve(
53
+ compressionFile(
54
+ file,
55
+ compression.type,
56
+ compression.quality,
57
+ compression.limit
58
+ )
59
+ );
60
+ }
61
+ resolve(file);
62
+ }).catch((err) => {
46
63
  return Upload.LIST_IGNORE;
47
- }
48
- return true;
64
+ });
65
+ return result;
49
66
  };
50
67
  const onChange = (info) => {
51
68
  props.onChange && props.onChange(info);
package/es/index.js CHANGED
@@ -2,11 +2,12 @@ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useState } from "react";
3
3
  import ReactDOM from "react-dom/client";
4
4
  import { BrowserRouter } from "react-router-dom";
5
- import { EditTable, showWorkFlow } from "./main";
6
- import { Form, Button } from "antd";
5
+ import { UploadFile, showWorkFlow } from "./main";
6
+ import { Form, Button, Input } from "antd";
7
+ import { fileToDataURL, compressionFile } from "./utils/compression-file";
7
8
  const Demo = () => {
8
9
  Form.useForm();
9
- const [list, setList] = useState([
10
+ useState([
10
11
  {
11
12
  PNOrderNo: 1,
12
13
  PNName: "未分配",
@@ -26,40 +27,6 @@ const Demo = () => {
26
27
  Remark: ""
27
28
  }
28
29
  ]);
29
- const columns = [
30
- {
31
- dataIndex: "PNOrderNo",
32
- title: "序号",
33
- width: 80
34
- },
35
- {
36
- dataIndex: "PNName",
37
- title: "节点名称"
38
- },
39
- {
40
- dataIndex: "PlanFinishDate",
41
- title: "预计完成时间",
42
- editable: true,
43
- width: 400,
44
- type: "datePicker",
45
- valueProps: {
46
- placeholder: "请输入内容"
47
- }
48
- },
49
- {
50
- dataIndex: "Remark",
51
- title: "备注",
52
- editable: true,
53
- type: "textarea",
54
- width: 400,
55
- valueProps: {
56
- placeholder: "请输入内容",
57
- autoSize: {
58
- minRows: 3
59
- }
60
- }
61
- }
62
- ];
63
30
  useState(
64
31
  Array.from({ length: 1e5 }, (_, key) => ({
65
32
  key,
@@ -68,6 +35,8 @@ const Demo = () => {
68
35
  }))
69
36
  );
70
37
  useState(false);
38
+ const [file, setFile] = useState("");
39
+ const [file2, setFile2] = useState("");
71
40
  useState([
72
41
  {
73
42
  uid: "-1",
@@ -102,20 +71,29 @@ const Demo = () => {
102
71
  renderContent: (record) => /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx("p", { children: record.PNName }) })
103
72
  });
104
73
  };
74
+ const onChange = async (e) => {
75
+ console.log(e);
76
+ const file3 = e.target.files[0];
77
+ const base64 = await fileToDataURL(file3);
78
+ setFile(base64);
79
+ const newFile = await compressionFile(file3, "image/jpeg", 0.2);
80
+ console.log(newFile);
81
+ const base64_2 = await fileToDataURL(newFile);
82
+ setFile2(base64_2);
83
+ };
105
84
  return /* @__PURE__ */ jsxs("div", { style: { height: "100vh" }, children: [
106
85
  /* @__PURE__ */ jsx(Button, { onClick: handleClick, children: "click" }),
86
+ /* @__PURE__ */ jsx(Input, { type: "file", onChange }),
107
87
  /* @__PURE__ */ jsx(
108
- EditTable,
88
+ UploadFile,
109
89
  {
110
- columns,
111
- rowKey: "PNOrderNo",
112
- onChange: (row) => {
113
- console.log(row);
114
- },
115
- dataSource: list,
116
- pagination: false
90
+ compression: { type: "image/jpeg", quality: 0.2, limit: 300 },
91
+ maxSize: 100,
92
+ onChange: (info) => console.log(info)
117
93
  }
118
- )
94
+ ),
95
+ /* @__PURE__ */ jsx("img", { src: file, style: { width: 400 } }),
96
+ /* @__PURE__ */ jsx("img", { src: file2, style: { width: 400 } })
119
97
  ] });
120
98
  };
121
99
  ReactDOM.createRoot(document.getElementById("root")).render(
@@ -0,0 +1,11 @@
1
+ export declare const fileToDataURL: (file: Blob | File) => Promise<any>;
2
+ export declare const dataURLToImage: (dataURL: string) => Promise<HTMLImageElement>;
3
+ /**
4
+ * 图片压缩方法
5
+ * @param {Object} file 图片文件
6
+ * @param {String} type 想压缩成的文件类型
7
+ * @param {Nubmber} quality 压缩质量参数
8
+ * @param {limit} limit 最小限制大小,小于该值时不进行压缩
9
+ * @returns 压缩后的新图片
10
+ */
11
+ export declare const compressionFile: (file: File, type?: string, quality?: number, limit?: number) => Promise<File>;
@@ -0,0 +1,65 @@
1
+ const fileToDataURL = (file) => {
2
+ return new Promise((resolve) => {
3
+ const reader = new FileReader();
4
+ reader.onloadend = (e) => resolve(e.target.result);
5
+ reader.readAsDataURL(file);
6
+ });
7
+ };
8
+ const dataURLToImage = (dataURL) => {
9
+ return new Promise((resolve) => {
10
+ const img = new Image();
11
+ img.onload = () => resolve(img);
12
+ img.src = dataURL;
13
+ });
14
+ };
15
+ const canvastoFile = (canvas, type, quality) => {
16
+ return new Promise(
17
+ (resolve) => canvas.toBlob((blob) => resolve(blob), type, quality)
18
+ );
19
+ };
20
+ const scaleWidthHeight = (img) => {
21
+ const maxWidth = 1400;
22
+ const maxHeight = 1400;
23
+ const originWidth = img.width;
24
+ const originHeight = img.height;
25
+ let targetWidth = img.width;
26
+ let targetHeight = img.height;
27
+ if (originWidth > maxWidth || originHeight > maxHeight) {
28
+ if (originWidth / originHeight > maxWidth / maxHeight) {
29
+ targetWidth = maxWidth;
30
+ targetHeight = Math.round(maxWidth * (originHeight / originWidth));
31
+ } else {
32
+ targetHeight = maxHeight;
33
+ targetWidth = Math.round(maxHeight * (originWidth / originHeight));
34
+ }
35
+ }
36
+ return {
37
+ width: targetWidth,
38
+ height: targetHeight
39
+ };
40
+ };
41
+ const compressionFile = async (file, type = "image/jpeg", quality = 0.5, limit = 300) => {
42
+ const fileName = file.name;
43
+ if (file.size / 1024 < limit) {
44
+ return file;
45
+ }
46
+ const canvas = document.createElement("canvas");
47
+ const context = canvas.getContext("2d");
48
+ const base64 = await fileToDataURL(file);
49
+ const img = await dataURLToImage(base64);
50
+ const target = scaleWidthHeight(img);
51
+ canvas.width = target.width;
52
+ canvas.height = target.height;
53
+ context.clearRect(0, 0, target.width, target.height);
54
+ context.drawImage(img, 0, 0, target.width, target.height);
55
+ const blob = await canvastoFile(canvas, type, quality);
56
+ const newFile = new File([blob], fileName, {
57
+ type
58
+ });
59
+ return newFile;
60
+ };
61
+ export {
62
+ compressionFile,
63
+ dataURLToImage,
64
+ fileToDataURL
65
+ };
@@ -7,6 +7,7 @@ interface BackHeaderProps {
7
7
  className?: string;
8
8
  style?: CSSProperties;
9
9
  titleStyle?: CSSProperties;
10
+ level?: 1 | 2 | 3 | 4 | 5;
10
11
  }
11
12
  declare const BackHeader: (props: BackHeaderProps) => JSX.Element;
12
13
  export default BackHeader;
@@ -21,7 +21,8 @@ const BackHeader = (props) => {
21
21
  subTitle = null,
22
22
  className,
23
23
  style,
24
- titleStyle
24
+ titleStyle,
25
+ level = 2
25
26
  } = props;
26
27
  const navigate = reactRouterDom.useNavigate();
27
28
  const renderSubtitle = () => {
@@ -36,7 +37,7 @@ const BackHeader = (props) => {
36
37
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: classNames(styles.main, className), style, children: [
37
38
  /* @__PURE__ */ jsxRuntime.jsxs(antd.Space, { children: [
38
39
  isBack && /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowLeftOutlined, { style: { fontSize: 24 }, onClick: goBack }),
39
- /* @__PURE__ */ jsxRuntime.jsxs(Title, { level: 2, className: styles.title, style: titleStyle, children: [
40
+ /* @__PURE__ */ jsxRuntime.jsxs(Title, { level, className: styles.title, style: titleStyle, children: [
40
41
  title2,
41
42
  renderSubtitle()
42
43
  ] })
@@ -4,6 +4,11 @@ export interface UploadFileProps extends UploadProps {
4
4
  maxSize?: number;
5
5
  uploadBtnName?: string | ReactNode;
6
6
  uploadHint?: any;
7
+ compression?: {
8
+ type?: string;
9
+ quality?: number;
10
+ limit?: number;
11
+ };
7
12
  }
8
13
  declare const UploadFile: (props: UploadFileProps) => JSX.Element;
9
14
  export default UploadFile;
@@ -6,6 +6,7 @@ const filetype = require("../../utils/filetype");
6
6
  const react = require("react");
7
7
  const utils = require("../../utils");
8
8
  require("react-dom");
9
+ const compressionFile = require("../../utils/compression-file");
9
10
  const defaultFileType = [filetype.FileSuffix.jpeg, filetype.FileSuffix.jpg, filetype.FileSuffix.png];
10
11
  const UploadFile = (props) => {
11
12
  const {
@@ -15,7 +16,8 @@ const UploadFile = (props) => {
15
16
  maxSize = 2,
16
17
  accept = defaultFileType.join(","),
17
18
  uploadBtnName = "上传",
18
- uploadHint = null
19
+ uploadHint = null,
20
+ compression
19
21
  } = props;
20
22
  const [preview, setPreview] = react.useState(false);
21
23
  const [previewSrc, setPreviewSrc] = react.useState("");
@@ -35,18 +37,33 @@ const UploadFile = (props) => {
35
37
  ] });
36
38
  };
37
39
  const beforeUpload = (file) => {
38
- const acceptList = accept.split(",");
39
- const suffix = utils.getFileSuffix(file.name) || "";
40
- if (!acceptList.includes(suffix)) {
41
- antd.message.error(`只允许上传 ${accept} 文件`);
42
- return antd.Upload.LIST_IGNORE;
43
- }
44
- const isLess = file.size / 1024 / 1024 < maxSize;
45
- if (!isLess) {
46
- antd.message.error(`文件必须小于${maxSize}MB`);
40
+ const result = new Promise((resolve, reject) => {
41
+ const acceptList = accept.split(",");
42
+ const suffix = utils.getFileSuffix(file.name) || "";
43
+ if (!acceptList.includes(suffix)) {
44
+ antd.message.error(`只允许上传 ${accept} 文件`);
45
+ reject(false);
46
+ }
47
+ const isLess = file.size / 1024 / 1024 < maxSize;
48
+ if (!isLess) {
49
+ antd.message.error(`文件必须小于${maxSize}MB`);
50
+ reject(false);
51
+ }
52
+ if (compression) {
53
+ resolve(
54
+ compressionFile.compressionFile(
55
+ file,
56
+ compression.type,
57
+ compression.quality,
58
+ compression.limit
59
+ )
60
+ );
61
+ }
62
+ resolve(file);
63
+ }).catch((err) => {
47
64
  return antd.Upload.LIST_IGNORE;
48
- }
49
- return true;
65
+ });
66
+ return result;
50
67
  };
51
68
  const onChange = (info) => {
52
69
  props.onChange && props.onChange(info);
package/lib/index.js CHANGED
@@ -5,9 +5,10 @@ const ReactDOM = require("react-dom/client");
5
5
  const reactRouterDom = require("react-router-dom");
6
6
  const main = require("./main");
7
7
  const antd = require("antd");
8
+ const compressionFile = require("./utils/compression-file");
8
9
  const Demo = () => {
9
10
  antd.Form.useForm();
10
- const [list, setList] = react.useState([
11
+ react.useState([
11
12
  {
12
13
  PNOrderNo: 1,
13
14
  PNName: "未分配",
@@ -27,40 +28,6 @@ const Demo = () => {
27
28
  Remark: ""
28
29
  }
29
30
  ]);
30
- const columns = [
31
- {
32
- dataIndex: "PNOrderNo",
33
- title: "序号",
34
- width: 80
35
- },
36
- {
37
- dataIndex: "PNName",
38
- title: "节点名称"
39
- },
40
- {
41
- dataIndex: "PlanFinishDate",
42
- title: "预计完成时间",
43
- editable: true,
44
- width: 400,
45
- type: "datePicker",
46
- valueProps: {
47
- placeholder: "请输入内容"
48
- }
49
- },
50
- {
51
- dataIndex: "Remark",
52
- title: "备注",
53
- editable: true,
54
- type: "textarea",
55
- width: 400,
56
- valueProps: {
57
- placeholder: "请输入内容",
58
- autoSize: {
59
- minRows: 3
60
- }
61
- }
62
- }
63
- ];
64
31
  react.useState(
65
32
  Array.from({ length: 1e5 }, (_, key) => ({
66
33
  key,
@@ -69,6 +36,8 @@ const Demo = () => {
69
36
  }))
70
37
  );
71
38
  react.useState(false);
39
+ const [file, setFile] = react.useState("");
40
+ const [file2, setFile2] = react.useState("");
72
41
  react.useState([
73
42
  {
74
43
  uid: "-1",
@@ -103,20 +72,29 @@ const Demo = () => {
103
72
  renderContent: (record) => /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx("p", { children: record.PNName }) })
104
73
  });
105
74
  };
75
+ const onChange = async (e) => {
76
+ console.log(e);
77
+ const file3 = e.target.files[0];
78
+ const base64 = await compressionFile.fileToDataURL(file3);
79
+ setFile(base64);
80
+ const newFile = await compressionFile.compressionFile(file3, "image/jpeg", 0.2);
81
+ console.log(newFile);
82
+ const base64_2 = await compressionFile.fileToDataURL(newFile);
83
+ setFile2(base64_2);
84
+ };
106
85
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { height: "100vh" }, children: [
107
86
  /* @__PURE__ */ jsxRuntime.jsx(antd.Button, { onClick: handleClick, children: "click" }),
87
+ /* @__PURE__ */ jsxRuntime.jsx(antd.Input, { type: "file", onChange }),
108
88
  /* @__PURE__ */ jsxRuntime.jsx(
109
- main.EditTable,
89
+ main.UploadFile,
110
90
  {
111
- columns,
112
- rowKey: "PNOrderNo",
113
- onChange: (row) => {
114
- console.log(row);
115
- },
116
- dataSource: list,
117
- pagination: false
91
+ compression: { type: "image/jpeg", quality: 0.2, limit: 300 },
92
+ maxSize: 100,
93
+ onChange: (info) => console.log(info)
118
94
  }
119
- )
95
+ ),
96
+ /* @__PURE__ */ jsxRuntime.jsx("img", { src: file, style: { width: 400 } }),
97
+ /* @__PURE__ */ jsxRuntime.jsx("img", { src: file2, style: { width: 400 } })
120
98
  ] });
121
99
  };
122
100
  ReactDOM.createRoot(document.getElementById("root")).render(
@@ -0,0 +1,11 @@
1
+ export declare const fileToDataURL: (file: Blob | File) => Promise<any>;
2
+ export declare const dataURLToImage: (dataURL: string) => Promise<HTMLImageElement>;
3
+ /**
4
+ * 图片压缩方法
5
+ * @param {Object} file 图片文件
6
+ * @param {String} type 想压缩成的文件类型
7
+ * @param {Nubmber} quality 压缩质量参数
8
+ * @param {limit} limit 最小限制大小,小于该值时不进行压缩
9
+ * @returns 压缩后的新图片
10
+ */
11
+ export declare const compressionFile: (file: File, type?: string, quality?: number, limit?: number) => Promise<File>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const fileToDataURL = (file) => {
4
+ return new Promise((resolve) => {
5
+ const reader = new FileReader();
6
+ reader.onloadend = (e) => resolve(e.target.result);
7
+ reader.readAsDataURL(file);
8
+ });
9
+ };
10
+ const dataURLToImage = (dataURL) => {
11
+ return new Promise((resolve) => {
12
+ const img = new Image();
13
+ img.onload = () => resolve(img);
14
+ img.src = dataURL;
15
+ });
16
+ };
17
+ const canvastoFile = (canvas, type, quality) => {
18
+ return new Promise(
19
+ (resolve) => canvas.toBlob((blob) => resolve(blob), type, quality)
20
+ );
21
+ };
22
+ const scaleWidthHeight = (img) => {
23
+ const maxWidth = 1400;
24
+ const maxHeight = 1400;
25
+ const originWidth = img.width;
26
+ const originHeight = img.height;
27
+ let targetWidth = img.width;
28
+ let targetHeight = img.height;
29
+ if (originWidth > maxWidth || originHeight > maxHeight) {
30
+ if (originWidth / originHeight > maxWidth / maxHeight) {
31
+ targetWidth = maxWidth;
32
+ targetHeight = Math.round(maxWidth * (originHeight / originWidth));
33
+ } else {
34
+ targetHeight = maxHeight;
35
+ targetWidth = Math.round(maxHeight * (originWidth / originHeight));
36
+ }
37
+ }
38
+ return {
39
+ width: targetWidth,
40
+ height: targetHeight
41
+ };
42
+ };
43
+ const compressionFile = async (file, type = "image/jpeg", quality = 0.5, limit = 300) => {
44
+ const fileName = file.name;
45
+ if (file.size / 1024 < limit) {
46
+ return file;
47
+ }
48
+ const canvas = document.createElement("canvas");
49
+ const context = canvas.getContext("2d");
50
+ const base64 = await fileToDataURL(file);
51
+ const img = await dataURLToImage(base64);
52
+ const target = scaleWidthHeight(img);
53
+ canvas.width = target.width;
54
+ canvas.height = target.height;
55
+ context.clearRect(0, 0, target.width, target.height);
56
+ context.drawImage(img, 0, 0, target.width, target.height);
57
+ const blob = await canvastoFile(canvas, type, quality);
58
+ const newFile = new File([blob], fileName, {
59
+ type
60
+ });
61
+ return newFile;
62
+ };
63
+ exports.compressionFile = compressionFile;
64
+ exports.dataURLToImage = dataURLToImage;
65
+ exports.fileToDataURL = fileToDataURL;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "szld-libs",
3
3
  "private": false,
4
- "version": "0.1.7",
4
+ "version": "0.1.8",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",