ronds-metadata 1.1.73 → 1.1.74

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.
@@ -1,7 +1,20 @@
1
1
  interface IFileViewProps {
2
+ /**
3
+ * 文件远程链接
4
+ */
2
5
  path?: string;
3
- limit?: number;
6
+ /**
7
+ * json数据
8
+ */
4
9
  data?: any[];
10
+ /**
11
+ * 文件
12
+ */
13
+ file?: any;
14
+ /**
15
+ * 最多展示多少行数据
16
+ */
17
+ limit?: number;
5
18
  }
6
19
  declare const FileView: (props: IFileViewProps) => JSX.Element;
7
20
  export default FileView;
@@ -11,6 +11,7 @@ import * as XLSX from 'xlsx';
11
11
  var FileView = function FileView(props) {
12
12
  var path = props.path,
13
13
  data = props.data,
14
+ file = props.file,
14
15
  _props$limit = props.limit,
15
16
  limit = _props$limit === void 0 ? 1000 : _props$limit;
16
17
  var ref = React.useRef(null);
@@ -61,76 +62,98 @@ var FileView = function FileView(props) {
61
62
  setValue([].concat(_value));
62
63
  firstLoadRef.current = false;
63
64
  }
64
- }, []);
65
- var getFileData = React.useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
66
- var res, blob, fileReader;
67
- return _regeneratorRuntime.wrap(function _callee$(_context) {
68
- while (1) {
69
- switch (_context.prev = _context.next) {
70
- case 0:
71
- if (!(path && firstLoadRef.current)) {
72
- _context.next = 10;
65
+ }, []); // 文件流转 base64
66
+
67
+ var fixdata = function fixdata(data) {
68
+ var o = '',
69
+ l = 0,
70
+ w = 10240;
71
+
72
+ for (; l < data.byteLength / w; ++l) {
73
+ o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
74
+ }
75
+
76
+ o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
77
+ return o;
78
+ };
79
+
80
+ var getFileData = React.useCallback( /*#__PURE__*/function () {
81
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_file) {
82
+ var blob, res, fileReader;
83
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
84
+ while (1) {
85
+ switch (_context.prev = _context.next) {
86
+ case 0:
87
+ if (!firstLoadRef.current) {
88
+ _context.next = 14;
89
+ break;
90
+ }
91
+
92
+ if (!path) {
93
+ _context.next = 10;
94
+ break;
95
+ }
96
+
97
+ _context.next = 4;
98
+ return fetch(path);
99
+
100
+ case 4:
101
+ res = _context.sent;
102
+ _context.next = 7;
103
+ return res.blob();
104
+
105
+ case 7:
106
+ blob = _context.sent;
107
+ _context.next = 11;
73
108
  break;
74
- }
75
-
76
- _context.next = 3;
77
- return fetch(path);
78
-
79
- case 3:
80
- res = _context.sent;
81
- _context.next = 6;
82
- return res.blob();
83
-
84
- case 6:
85
- blob = _context.sent;
86
- fileReader = new FileReader();
87
- // blob.type = 'text/csv;charset=utf-8;';
88
- // const bl = new Blob([blob], { type: 'text/csv;charset=utf-8;' });
89
- // console.log('blob2', bl);
90
- //二进制
91
- // fileReader.readAsBinaryString(blob);
92
- fileReader.readAsArrayBuffer(blob);
93
-
94
- fileReader.onload = function (event) {
95
- try {
96
- var _data = [];
97
- var binary = ''; // 以二进制流方式读取得到整份excel表格对象
98
-
99
- var result = event.target.result;
100
- var bytes = new Uint8Array(result);
101
-
102
- for (var i = 0; i < bytes.byteLength; i++) {
103
- binary += String.fromCharCode(bytes[i]);
104
- }
105
109
 
106
- // 遍历每张工作表进行读取(这里默认只读取第一张表)
107
- var workbook = XLSX.read(binary, {
108
- type: 'binary',
109
- codepage: 936
110
- });
110
+ case 10:
111
+ blob = _file;
112
+
113
+ case 11:
114
+ fileReader = new FileReader(); //二进制
115
+ // fileReader.readAsBinaryString(blob);
116
+
117
+ fileReader.onload = function (event) {
118
+ try {
119
+ var _data = []; // 以二进制流方式读取得到整份excel表格对象
111
120
 
112
- for (var sheet in workbook.Sheets) {
113
- if (workbook.Sheets.hasOwnProperty(sheet)) {
114
- // 利用 sheet_to_json 方法将 excel 转成 json 数据
115
- _data = _data.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet])); // 如果只取第一张表,就取消注释这行
121
+ var result = event.target.result; // 遍历每张工作表进行读取(这里默认只读取第一张表)
116
122
 
117
- break;
123
+ var workbook = XLSX.read(fixdata(result), {
124
+ type: 'binary',
125
+ codepage: 65001
126
+ });
127
+
128
+ for (var sheet in workbook.Sheets) {
129
+ if (workbook.Sheets.hasOwnProperty(sheet)) {
130
+ // 利用 sheet_to_json 方法将 excel 转成 json 数据
131
+ _data = _data.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet])); // 如果只取第一张表,就取消注释这行
132
+
133
+ break;
134
+ }
118
135
  }
136
+
137
+ processDataToExcel(_data);
138
+ } catch (e) {
139
+ return;
119
140
  }
141
+ };
120
142
 
121
- processDataToExcel(_data);
122
- } catch (e) {
123
- return;
124
- }
125
- };
143
+ fileReader.readAsArrayBuffer(blob);
126
144
 
127
- case 10:
128
- case "end":
129
- return _context.stop();
145
+ case 14:
146
+ case "end":
147
+ return _context.stop();
148
+ }
130
149
  }
131
- }
132
- }, _callee);
133
- })), []);
150
+ }, _callee);
151
+ }));
152
+
153
+ return function (_x) {
154
+ return _ref.apply(this, arguments);
155
+ };
156
+ }(), []);
134
157
  React.useEffect(function () {
135
158
  if (path && firstLoadRef.current) {
136
159
  getFileData();
@@ -141,6 +164,16 @@ var FileView = function FileView(props) {
141
164
 
142
165
  return function () {};
143
166
  }, [getFileData, path]);
167
+ React.useEffect(function () {
168
+ if (file && firstLoadRef.current) {
169
+ getFileData(file);
170
+ return function () {
171
+ ref.current = null;
172
+ };
173
+ }
174
+
175
+ return function () {};
176
+ }, [getFileData, file]);
144
177
  React.useEffect(function () {
145
178
  if (data && firstLoadRef.current) {
146
179
  processDataToExcel(data);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "ronds-metadata",
4
- "version": "1.1.73",
4
+ "version": "1.1.74",
5
5
  "scripts": {
6
6
  "start": "dumi dev",
7
7
  "docs:build": "dumi build",