@teamias/rex-design 0.0.39 → 0.0.40

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.
@@ -8,27 +8,23 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
8
8
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
9
9
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
10
10
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
12
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
14
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
15
- function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
16
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
17
11
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
18
12
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
19
13
  import { useStateData } from "../../../hooks";
20
- import { DownloadOutlined } from '@ant-design/icons';
21
- import { FloatButton, Modal, Spin } from 'antd';
14
+ import { DownloadOutlined, LoadingOutlined } from '@ant-design/icons';
15
+ import { Button, Flex, FloatButton, Modal, Result, Space, Spin } from 'antd';
22
16
  import { sleep } from 'radash';
23
- import { useEffect, useState } from 'react';
17
+ import { useEffect } from 'react';
24
18
  import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom';
25
19
  import { ContainerBox } from "../style";
26
- import { getFileTypeBySuffix, loadImagePreviewer, loadPdfPreviewer } from "../utils";
20
+ import { FILE_VIEWER_FILE_TYPES } from "../types";
21
+ import { downloadResourceFile, loadImagePreviewer, loadPdfPreviewer } from "../utils";
27
22
  import { jsx as _jsx } from "react/jsx-runtime";
28
23
  import { jsxs as _jsxs } from "react/jsx-runtime";
29
24
  export var FileViewerContainer = function FileViewerContainer(_ref) {
30
25
  var fileUrl = _ref.fileUrl,
31
- fileName = _ref.fileName,
26
+ _ref$fileName = _ref.fileName,
27
+ fileName = _ref$fileName === void 0 ? '' : _ref$fileName,
32
28
  fileType = _ref.fileType,
33
29
  onDestroy = _ref.onDestroy,
34
30
  modalProps = _objectWithoutProperties(_ref, _excluded);
@@ -37,118 +33,201 @@ export var FileViewerContainer = function FileViewerContainer(_ref) {
37
33
  open: true,
38
34
  domId: 'viewer-container-' + Date.now(),
39
35
  loading: false,
40
- error: '',
36
+ errorTitle: '',
37
+ errorDetail: '',
38
+ downloadLoading: false,
41
39
  download: undefined,
42
40
  destroy: undefined
43
41
  };
44
42
  }),
45
43
  state = _useStateData.state,
46
44
  setState = _useStateData.setState;
47
- var _useState = useState(function () {
48
- var _ref2;
49
- var obj = new URL(fileUrl);
50
- var pathname = obj.pathname;
51
- // console.log('obj', obj);
52
45
 
53
- return {
54
- fileName: decodeURIComponent((_ref2 = fileName !== null && fileName !== void 0 ? fileName : pathname.split('/').pop()) !== null && _ref2 !== void 0 ? _ref2 : ''),
55
- fileType: fileType !== null && fileType !== void 0 ? fileType : getFileTypeBySuffix(pathname)
56
- };
57
- }),
58
- _useState2 = _slicedToArray(_useState, 1),
59
- config = _useState2[0];
46
+ /** 设置错误状态 */
47
+ var setError = function setError(title, detail) {
48
+ setState('errorTitle', title);
49
+ setState('errorDetail', detail !== null && detail !== void 0 ? detail : '');
50
+ };
51
+
52
+ /** 清除错误状态 */
53
+ var clearError = function clearError() {
54
+ setState('errorTitle', '');
55
+ setState('errorDetail', '');
56
+ };
57
+
58
+ /** 下载文件 */
59
+ var downloadFile = /*#__PURE__*/function () {
60
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
61
+ var _state$download;
62
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
63
+ while (1) switch (_context.prev = _context.next) {
64
+ case 0:
65
+ setState('downloadLoading', true);
66
+ if (!state.download) {
67
+ _context.next = 6;
68
+ break;
69
+ }
70
+ _context.next = 4;
71
+ return (_state$download = state.download) === null || _state$download === void 0 ? void 0 : _state$download.call(state);
72
+ case 4:
73
+ _context.next = 8;
74
+ break;
75
+ case 6:
76
+ _context.next = 8;
77
+ return downloadResourceFile(fileUrl, fileName);
78
+ case 8:
79
+ setState('downloadLoading', false);
80
+ case 9:
81
+ case "end":
82
+ return _context.stop();
83
+ }
84
+ }, _callee);
85
+ }));
86
+ return function downloadFile() {
87
+ return _ref2.apply(this, arguments);
88
+ };
89
+ }();
90
+
91
+ /** 缩放 */
60
92
  var zoomUpdate = function zoomUpdate(_ref3) {
61
93
  var x = _ref3.x,
62
94
  y = _ref3.y,
63
95
  scale = _ref3.scale;
96
+ if (state.errorTitle) return;
64
97
  var value = make3dTransformValue({
65
98
  x: x,
66
99
  y: y,
67
100
  scale: scale
68
101
  });
69
- // console.log(value);
70
102
  document.getElementById(state.domId).style.setProperty('transform', value);
71
103
  };
104
+
105
+ /** 查看 pdf */
72
106
  var pdfRun = /*#__PURE__*/function () {
73
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
74
- var pdfPreviewer;
75
- return _regeneratorRuntime().wrap(function _callee$(_context) {
76
- while (1) switch (_context.prev = _context.next) {
107
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
108
+ var pdfPreviewer, errorMsg;
109
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
110
+ while (1) switch (_context3.prev = _context3.next) {
77
111
  case 0:
78
112
  setState('loading', true);
79
- _context.prev = 1;
80
- _context.next = 4;
113
+ _context3.prev = 1;
114
+ _context3.next = 4;
81
115
  return sleep(200);
82
116
  case 4:
83
- _context.next = 6;
117
+ _context3.next = 6;
84
118
  return loadPdfPreviewer(state.domId, fileUrl);
85
119
  case 6:
86
- pdfPreviewer = _context.sent;
87
- state.download = function () {
88
- return pdfPreviewer.save(config.fileName);
89
- };
120
+ pdfPreviewer = _context3.sent;
121
+ state.download = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
122
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
123
+ while (1) switch (_context2.prev = _context2.next) {
124
+ case 0:
125
+ return _context2.abrupt("return", pdfPreviewer.save(fileName));
126
+ case 1:
127
+ case "end":
128
+ return _context2.stop();
129
+ }
130
+ }, _callee2);
131
+ }));
90
132
  state.destroy = function () {
91
133
  return pdfPreviewer.destroy();
92
134
  };
93
- _context.next = 14;
135
+ _context3.next = 15;
94
136
  break;
95
137
  case 11:
96
- _context.prev = 11;
97
- _context.t0 = _context["catch"](1);
98
- setState('error', _context.t0.toString());
99
- case 14:
100
- setState('loading', false);
138
+ _context3.prev = 11;
139
+ _context3.t0 = _context3["catch"](1);
140
+ errorMsg = _context3.t0 instanceof Error ? _context3.t0.message : JSON.stringify(_context3.t0, null, 2);
141
+ setError('文件加载失败', "PDF \u52A0\u8F7D\u5931\u8D25\uFF1A".concat(errorMsg));
101
142
  case 15:
143
+ setState('loading', false);
144
+ case 16:
102
145
  case "end":
103
- return _context.stop();
146
+ return _context3.stop();
104
147
  }
105
- }, _callee, null, [[1, 11]]);
148
+ }, _callee3, null, [[1, 11]]);
106
149
  }));
107
150
  return function pdfRun() {
108
151
  return _ref4.apply(this, arguments);
109
152
  };
110
153
  }();
154
+
155
+ /** 查看 image */
111
156
  var imageRun = /*#__PURE__*/function () {
112
- var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
113
- var imgPreviewer;
114
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
115
- while (1) switch (_context2.prev = _context2.next) {
157
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
158
+ var imgPreviewer, errorMsg;
159
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
160
+ while (1) switch (_context5.prev = _context5.next) {
116
161
  case 0:
117
162
  setState('loading', true);
118
- _context2.prev = 1;
119
- _context2.next = 4;
163
+ _context5.prev = 1;
164
+ _context5.next = 4;
120
165
  return loadImagePreviewer(state.domId, fileUrl);
121
166
  case 4:
122
- imgPreviewer = _context2.sent;
123
- state.download = function () {
124
- return imgPreviewer.save(config.fileName);
125
- };
126
- _context2.next = 11;
167
+ imgPreviewer = _context5.sent;
168
+ state.download = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
169
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
170
+ while (1) switch (_context4.prev = _context4.next) {
171
+ case 0:
172
+ return _context4.abrupt("return", imgPreviewer.save(fileName));
173
+ case 1:
174
+ case "end":
175
+ return _context4.stop();
176
+ }
177
+ }, _callee4);
178
+ }));
179
+ _context5.next = 12;
127
180
  break;
128
181
  case 8:
129
- _context2.prev = 8;
130
- _context2.t0 = _context2["catch"](1);
131
- setState('error', _context2.t0.toString());
132
- case 11:
133
- setState('loading', false);
182
+ _context5.prev = 8;
183
+ _context5.t0 = _context5["catch"](1);
184
+ errorMsg = _context5.t0 instanceof Error ? _context5.t0.message : JSON.stringify(_context5.t0, null, 2);
185
+ setError('文件加载失败', "\u56FE\u7247\u52A0\u8F7D\u5931\u8D25\uFF1A".concat(errorMsg));
134
186
  case 12:
187
+ setState('loading', false);
188
+ case 13:
135
189
  case "end":
136
- return _context2.stop();
190
+ return _context5.stop();
137
191
  }
138
- }, _callee2, null, [[1, 8]]);
192
+ }, _callee5, null, [[1, 8]]);
139
193
  }));
140
194
  return function imageRun() {
141
- return _ref5.apply(this, arguments);
195
+ return _ref6.apply(this, arguments);
196
+ };
197
+ }();
198
+ var startRun = /*#__PURE__*/function () {
199
+ var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
200
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
201
+ while (1) switch (_context6.prev = _context6.next) {
202
+ case 0:
203
+ clearError();
204
+ _context6.next = 3;
205
+ return sleep(100);
206
+ case 3:
207
+ if (FILE_VIEWER_FILE_TYPES.includes(fileType)) {
208
+ _context6.next = 6;
209
+ break;
210
+ }
211
+ setError('不支持的文件格式', "\u5F53\u524D\u4EC5\u652F\u6301 PDF \u548C\u56FE\u7247\u683C\u5F0F(png\u3001jpg\u3001jpeg\u3001gif\u3001bmp\u3001webp\u3001svg), \u5F53\u524D\u6587\u4EF6\u7C7B\u578B: ".concat(fileType));
212
+ return _context6.abrupt("return");
213
+ case 6:
214
+ if (fileType === 'pdf') {
215
+ pdfRun();
216
+ } else if (fileType === 'image') {
217
+ imageRun();
218
+ }
219
+ case 7:
220
+ case "end":
221
+ return _context6.stop();
222
+ }
223
+ }, _callee6);
224
+ }));
225
+ return function startRun() {
226
+ return _ref8.apply(this, arguments);
142
227
  };
143
228
  }();
144
229
  useEffect(function () {
145
- // console.log(config);
146
-
147
- if (config.fileType === 'pdf') {
148
- pdfRun();
149
- } else if (config.fileType === 'image') {
150
- imageRun();
151
- }
230
+ startRun();
152
231
  return function () {
153
232
  var _state$destroy;
154
233
  (_state$destroy = state.destroy) === null || _state$destroy === void 0 || _state$destroy.call(state);
@@ -171,15 +250,42 @@ export var FileViewerContainer = function FileViewerContainer(_ref) {
171
250
  spinning: state.loading,
172
251
  tip: "\u6587\u4EF6\u52A0\u8F7D\u4E2D...",
173
252
  children: [/*#__PURE__*/_jsx(ContainerBox, {
174
- children: /*#__PURE__*/_jsx(QuickPinchZoom, {
253
+ children: state.errorTitle ? /*#__PURE__*/_jsx("div", {
254
+ children: /*#__PURE__*/_jsxs(Flex, {
255
+ vertical: true,
256
+ align: "center",
257
+ gap: 16,
258
+ children: [/*#__PURE__*/_jsx(Result, {
259
+ status: "error",
260
+ title: state.errorTitle,
261
+ subTitle: state.errorDetail
262
+ }), /*#__PURE__*/_jsxs(Space.Compact, {
263
+ children: [/*#__PURE__*/_jsx(Button, {
264
+ type: "link",
265
+ href: fileUrl,
266
+ target: "_blank",
267
+ children: "\u6253\u5F00\u8D44\u6E90\u94FE\u63A5"
268
+ }), /*#__PURE__*/_jsx(Button, {
269
+ type: "link",
270
+ loading: state.downloadLoading,
271
+ onClick: downloadFile,
272
+ children: "\u5C1D\u8BD5\u4E0B\u8F7D"
273
+ }), /*#__PURE__*/_jsx(Button, {
274
+ type: "link",
275
+ onClick: startRun,
276
+ children: "\u5237\u65B0\u91CD\u8BD5"
277
+ })]
278
+ })]
279
+ })
280
+ }) : /*#__PURE__*/_jsx(QuickPinchZoom, {
175
281
  onUpdate: zoomUpdate,
176
282
  children: /*#__PURE__*/_jsx("div", {
177
283
  id: state.domId
178
284
  })
179
285
  })
180
- }), state.download && /*#__PURE__*/_jsx(FloatButton, {
181
- icon: /*#__PURE__*/_jsx(DownloadOutlined, {}),
182
- onClick: state.download
286
+ }), state.download && !state.errorTitle && /*#__PURE__*/_jsx(FloatButton, {
287
+ icon: state.downloadLoading ? /*#__PURE__*/_jsx(LoadingOutlined, {}) : /*#__PURE__*/_jsx(DownloadOutlined, {}),
288
+ onClick: downloadFile
183
289
  })]
184
290
  })
185
291
  }));
@@ -1,3 +1,6 @@
1
1
  import { IFileViewerOptions } from '../types';
2
2
  export { FileViewerRoot } from './file-viewer-root';
3
+ /** 资源展示 */
3
4
  export declare const showFileViewer: (options: IFileViewerOptions) => void;
5
+ /** 预览或下载,解析到未支持的文件类型时自动下载 */
6
+ export declare const previewOrDownloadFile: (options: IFileViewerOptions) => Promise<void>;
@@ -1,22 +1,83 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ var _excluded = ["fileType", "fileUrl", "fileName"];
3
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
4
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
5
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
2
6
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
7
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
8
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
9
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
10
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
11
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
12
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
7
13
  import { createPortal } from 'react-dom';
14
+ import { FILE_VIEWER_FILE_TYPES } from "../types";
15
+ import { downloadResourceFile, getFileInfo } from "../utils";
8
16
  import { FileViewerContainer } from "./file-viewer-container";
9
17
  import { context } from "./file-viewer-root";
10
18
  import { jsx as _jsx } from "react/jsx-runtime";
11
19
  export { FileViewerRoot } from "./file-viewer-root";
20
+
21
+ /** 资源展示 */
12
22
  export var showFileViewer = function showFileViewer(options) {
13
- var key = "file-viewer-".concat(Date.now());
14
- var portal = /*#__PURE__*/createPortal( /*#__PURE__*/_jsx(FileViewerContainer, _objectSpread(_objectSpread({}, options), {}, {
15
- onDestroy: function onDestroy() {
16
- delete context.portals[key];
17
- context.update();
18
- }
19
- })), document.getElementById(context.domId));
20
- context.portals[key] = portal;
21
- context.update();
22
- };
23
+ try {
24
+ var fileType = options.fileType,
25
+ fileUrl = options.fileUrl,
26
+ fileName = options.fileName,
27
+ modalProps = _objectWithoutProperties(options, _excluded);
28
+ var info = getFileInfo(fileUrl);
29
+ var key = "file-viewer-".concat(Date.now());
30
+ var portal = /*#__PURE__*/createPortal( /*#__PURE__*/_jsx(FileViewerContainer, _objectSpread(_objectSpread({}, modalProps), {}, {
31
+ fileUrl: fileUrl,
32
+ fileName: fileName !== null && fileName !== void 0 ? fileName : info.fileName,
33
+ fileType: fileType !== null && fileType !== void 0 ? fileType : info.fileType,
34
+ onDestroy: function onDestroy() {
35
+ delete context.portals[key];
36
+ context.update();
37
+ }
38
+ })), document.getElementById(context.domId));
39
+ context.portals[key] = portal;
40
+ context.update();
41
+ } catch (error) {
42
+ console.error('[showFileViewer]', error);
43
+ }
44
+ };
45
+
46
+ /** 预览或下载,解析到未支持的文件类型时自动下载 */
47
+ export var previewOrDownloadFile = /*#__PURE__*/function () {
48
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
49
+ var fileType, fileUrl, fileName, _getFileInfo, finalFileType;
50
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
51
+ while (1) switch (_context.prev = _context.next) {
52
+ case 0:
53
+ _context.prev = 0;
54
+ fileType = options.fileType, fileUrl = options.fileUrl, fileName = options.fileName;
55
+ _getFileInfo = getFileInfo(fileUrl), finalFileType = _getFileInfo.fileType;
56
+ if (!FILE_VIEWER_FILE_TYPES.includes(fileType !== null && fileType !== void 0 ? fileType : finalFileType)) {
57
+ _context.next = 7;
58
+ break;
59
+ }
60
+ // 支持预览的格式
61
+ showFileViewer(_objectSpread({}, options));
62
+ _context.next = 9;
63
+ break;
64
+ case 7:
65
+ _context.next = 9;
66
+ return downloadResourceFile(fileUrl, fileName);
67
+ case 9:
68
+ _context.next = 14;
69
+ break;
70
+ case 11:
71
+ _context.prev = 11;
72
+ _context.t0 = _context["catch"](0);
73
+ console.error('[previewOrDownloadFile]', _context.t0);
74
+ case 14:
75
+ case "end":
76
+ return _context.stop();
77
+ }
78
+ }, _callee, null, [[0, 11]]);
79
+ }));
80
+ return function previewOrDownloadFile(_x) {
81
+ return _ref.apply(this, arguments);
82
+ };
83
+ }();
@@ -1,11 +1,13 @@
1
- import { FileViewerRoot, showFileViewer } from "../../..";
2
- import { Button, Space } from 'antd';
1
+ import { downloadResourceFile, FileViewerRoot, previewOrDownloadFile, showFileViewer } from "../../..";
2
+ import { Button, Flex } from 'antd';
3
3
  import { jsx as _jsx } from "react/jsx-runtime";
4
4
  import { jsxs as _jsxs } from "react/jsx-runtime";
5
5
  import { Fragment as _Fragment } from "react/jsx-runtime";
6
6
  export default function Demo() {
7
7
  return /*#__PURE__*/_jsxs(_Fragment, {
8
- children: [/*#__PURE__*/_jsx(FileViewerRoot, {}), /*#__PURE__*/_jsxs(Space.Compact, {
8
+ children: [/*#__PURE__*/_jsx(FileViewerRoot, {}), /*#__PURE__*/_jsxs(Flex, {
9
+ vertical: true,
10
+ gap: 16,
9
11
  children: [/*#__PURE__*/_jsx(Button, {
10
12
  onClick: function onClick() {
11
13
  return showFileViewer({
@@ -20,6 +22,39 @@ export default function Demo() {
20
22
  });
21
23
  },
22
24
  children: "image"
25
+ }), /*#__PURE__*/_jsx(Button, {
26
+ onClick: function onClick() {
27
+ return showFileViewer({
28
+ fileUrl: 'https://zzzz-zkp.oss-cn-shenzhen.aliyuncs.com/pinia-state.json'
29
+ });
30
+ },
31
+ children: "\u5176\u4ED6\u8D44\u6E90"
32
+ }), /*#__PURE__*/_jsx(Button, {
33
+ onClick: function onClick() {
34
+ return showFileViewer({
35
+ fileUrl: 'https://zzzz-zkp.oss-cn-shenzhe33n.aliyuncs.com/badge%20%281%29.png'
36
+ });
37
+ },
38
+ children: "\u9519\u8BEF\u8D44\u6E90"
39
+ }), /*#__PURE__*/_jsx(Button, {
40
+ onClick: function onClick() {
41
+ return downloadResourceFile('https://zzzz-zkp.oss-cn-shenzhen.aliyuncs.com/%E5%85%A5%E8%81%8C%E5%BD%95%E7%94%A8%E9%80%9A%E7%9F%A5.pdf', 'xxx.pdf');
42
+ },
43
+ children: "\u4E0B\u8F7D pdf"
44
+ }), /*#__PURE__*/_jsx(Button, {
45
+ onClick: function onClick() {
46
+ previewOrDownloadFile({
47
+ fileUrl: 'https://zzzz-zkp.oss-cn-shenzhen.aliyuncs.com/badge%20%281%29.png'
48
+ });
49
+ },
50
+ children: "[img]\u81EA\u52A8\u8BC6\u522B\u6587\u4EF6, \u8FDB\u884C\u9884\u89C8\u6216\u4E0B\u8F7D"
51
+ }), /*#__PURE__*/_jsx(Button, {
52
+ onClick: function onClick() {
53
+ previewOrDownloadFile({
54
+ fileUrl: 'https://zzzz-zkp.oss-cn-shenzhen.aliyuncs.com/pinia-state.json'
55
+ });
56
+ },
57
+ children: "[json]\u81EA\u52A8\u8BC6\u522B\u6587\u4EF6, \u8FDB\u884C\u9884\u89C8\u6216\u4E0B\u8F7D"
23
58
  })]
24
59
  })]
25
60
  });
@@ -1,5 +1,6 @@
1
1
  import { FC } from 'react';
2
2
  import { IFileViewerOptions } from './types';
3
- export { FileViewerRoot, showFileViewer } from './core';
3
+ export { downloadResourceFile } from './utils';
4
+ export { FileViewerRoot, previewOrDownloadFile, showFileViewer } from './core';
4
5
  export * from './types';
5
6
  export declare const FakeFileViewer: FC<IFileViewerOptions>;
@@ -1,6 +1,7 @@
1
1
  import { Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- export { FileViewerRoot, showFileViewer } from "./core";
3
+ export { downloadResourceFile } from "./utils";
4
+ export { FileViewerRoot, previewOrDownloadFile, showFileViewer } from "./core";
4
5
  export * from "./types";
5
6
  export var FakeFileViewer = function FakeFileViewer() {
6
7
  return /*#__PURE__*/_jsx(_Fragment, {});
@@ -1,11 +1,13 @@
1
1
  import { GetProps, Modal } from 'antd';
2
+ /** 支持的文件类型 */
3
+ export declare const FILE_VIEWER_FILE_TYPES: readonly ["pdf", "image"];
2
4
  export interface IFileViewerOptions extends ModalProps {
3
5
  /** 文件路径,注意文件跨域问题 */
4
6
  fileUrl: string;
5
7
  /** 文件名称 */
6
8
  fileName?: string;
7
9
  /** 文件类型 */
8
- fileType?: 'pdf' | 'image';
10
+ fileType?: (typeof FILE_VIEWER_FILE_TYPES)[number];
9
11
  }
10
12
  export interface IFileViewerContainerProps extends IFileViewerOptions {
11
13
  onDestroy: () => void;
@@ -1 +1,2 @@
1
- export {};
1
+ /** 支持的文件类型 */
2
+ export var FILE_VIEWER_FILE_TYPES = ['pdf', 'image'];
@@ -3,9 +3,16 @@ import { JsPdfPreview } from '@js-preview/pdf';
3
3
  export declare const loadPdfPreviewer: (domId: string, fileUrl: string) => Promise<JsPdfPreview>;
4
4
  /** 加载 image */
5
5
  export declare const loadImagePreviewer: (domId: string, fileUrl: string) => Promise<{
6
- save: (fileName: string) => void;
6
+ save: (fileName: string) => Promise<void>;
7
7
  }>;
8
+ /** 下载资源文件, 使用 fetch */
9
+ export declare const downloadResourceFile: (fileUrl: string, fileName?: string) => Promise<void>;
8
10
  /** 匹配图片类型 为 image */
9
11
  export declare const isImageType: (fileType: string) => boolean;
10
12
  /** 根据文件后缀,返回文件类型 */
11
- export declare const getFileTypeBySuffix: (fileName: string) => "image" | "unknown" | "pdf";
13
+ export declare const getFileTypeBySuffix: (fileName: string) => string;
14
+ /** 获取文件信息 */
15
+ export declare const getFileInfo: (fileUrl: string) => {
16
+ fileName: string;
17
+ fileType: string;
18
+ };
@@ -2,6 +2,8 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" ==
2
2
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
3
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
4
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
+ import { message } from 'antd';
6
+
5
7
  /** 加载 pdf */
6
8
  export var loadPdfPreviewer = /*#__PURE__*/function () {
7
9
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(domId, fileUrl) {
@@ -56,12 +58,7 @@ export var loadImagePreviewer = /*#__PURE__*/function () {
56
58
  container.appendChild(img);
57
59
  obj = {
58
60
  save: function save(fileName) {
59
- var link = document.createElement('a');
60
- link.href = fileUrl;
61
- link.download = fileName;
62
- document.body.appendChild(link);
63
- link.click();
64
- document.body.removeChild(link);
61
+ return downloadResourceFile(fileUrl, fileName);
65
62
  }
66
63
  };
67
64
  return _context2.abrupt("return", new Promise(function (rel, rej) {
@@ -85,6 +82,25 @@ export var loadImagePreviewer = /*#__PURE__*/function () {
85
82
  };
86
83
  }();
87
84
 
85
+ /** 下载资源文件, 使用 fetch */
86
+ export var downloadResourceFile = function downloadResourceFile(fileUrl, fileName) {
87
+ return fetch(fileUrl).then(function (response) {
88
+ return response.blob();
89
+ }).then(function (blob) {
90
+ var url = window.URL.createObjectURL(blob);
91
+ var link = document.createElement('a');
92
+ link.href = url;
93
+ link.download = fileName || getFileInfo(fileUrl).fileName;
94
+ document.body.appendChild(link);
95
+ link.click();
96
+ document.body.removeChild(link);
97
+ window.URL.revokeObjectURL(url);
98
+ }).catch(function (error) {
99
+ console.error('downloadResourceFile: ', error);
100
+ message.error('资源下载失败');
101
+ });
102
+ };
103
+
88
104
  /** 匹配图片类型 为 image */
89
105
  export var isImageType = function isImageType(fileType) {
90
106
  return ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'svg'].includes(fileType.toLowerCase());
@@ -97,5 +113,18 @@ export var getFileTypeBySuffix = function getFileTypeBySuffix(fileName) {
97
113
  if (!suffix) return 'unknown';
98
114
  if (suffix === 'pdf') return 'pdf';
99
115
  if (isImageType(suffix)) return 'image';
100
- return 'unknown';
116
+ return suffix;
117
+ };
118
+
119
+ /** 获取文件信息 */
120
+ export var getFileInfo = function getFileInfo(fileUrl) {
121
+ var _pathname$split$pop;
122
+ var obj = new URL(fileUrl);
123
+ var pathname = obj.pathname;
124
+ // console.log('obj', obj);
125
+
126
+ return {
127
+ fileName: decodeURIComponent((_pathname$split$pop = pathname.split('/').pop()) !== null && _pathname$split$pop !== void 0 ? _pathname$split$pop : ''),
128
+ fileType: getFileTypeBySuffix(pathname)
129
+ };
101
130
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamias/rex-design",
3
- "version": "0.0.39",
3
+ "version": "0.0.40",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",