@teamias/rex-design 0.0.38 → 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.
- package/dist/components/file-viewer/core/file-viewer-container.d.ts +3 -0
- package/dist/components/file-viewer/core/file-viewer-container.js +292 -0
- package/dist/components/file-viewer/core/file-viewer-root.d.ts +7 -0
- package/dist/components/file-viewer/core/file-viewer-root.js +35 -0
- package/dist/components/file-viewer/core/index.d.ts +6 -0
- package/dist/components/file-viewer/core/index.js +83 -0
- package/dist/components/file-viewer/demo/Demo.d.ts +1 -0
- package/dist/components/file-viewer/demo/Demo.js +61 -0
- package/dist/components/file-viewer/index.d.ts +6 -0
- package/dist/components/file-viewer/index.js +8 -0
- package/dist/components/file-viewer/style.d.ts +2 -0
- package/dist/components/file-viewer/style.js +5 -0
- package/dist/components/file-viewer/types.d.ts +16 -0
- package/dist/components/file-viewer/types.js +2 -0
- package/dist/components/file-viewer/utils/index.d.ts +18 -0
- package/dist/components/file-viewer/utils/index.js +130 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/package.json +3 -1
|
@@ -0,0 +1,292 @@
|
|
|
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 = ["fileUrl", "fileName", "fileType", "onDestroy"];
|
|
3
|
+
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; }
|
|
4
|
+
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; }
|
|
5
|
+
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; }
|
|
6
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
7
|
+
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); }
|
|
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
|
+
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
|
+
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 _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; }
|
|
13
|
+
import { useStateData } from "../../../hooks";
|
|
14
|
+
import { DownloadOutlined, LoadingOutlined } from '@ant-design/icons';
|
|
15
|
+
import { Button, Flex, FloatButton, Modal, Result, Space, Spin } from 'antd';
|
|
16
|
+
import { sleep } from 'radash';
|
|
17
|
+
import { useEffect } from 'react';
|
|
18
|
+
import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom';
|
|
19
|
+
import { ContainerBox } from "../style";
|
|
20
|
+
import { FILE_VIEWER_FILE_TYPES } from "../types";
|
|
21
|
+
import { downloadResourceFile, loadImagePreviewer, loadPdfPreviewer } from "../utils";
|
|
22
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
23
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
24
|
+
export var FileViewerContainer = function FileViewerContainer(_ref) {
|
|
25
|
+
var fileUrl = _ref.fileUrl,
|
|
26
|
+
_ref$fileName = _ref.fileName,
|
|
27
|
+
fileName = _ref$fileName === void 0 ? '' : _ref$fileName,
|
|
28
|
+
fileType = _ref.fileType,
|
|
29
|
+
onDestroy = _ref.onDestroy,
|
|
30
|
+
modalProps = _objectWithoutProperties(_ref, _excluded);
|
|
31
|
+
var _useStateData = useStateData(function () {
|
|
32
|
+
return {
|
|
33
|
+
open: true,
|
|
34
|
+
domId: 'viewer-container-' + Date.now(),
|
|
35
|
+
loading: false,
|
|
36
|
+
errorTitle: '',
|
|
37
|
+
errorDetail: '',
|
|
38
|
+
downloadLoading: false,
|
|
39
|
+
download: undefined,
|
|
40
|
+
destroy: undefined
|
|
41
|
+
};
|
|
42
|
+
}),
|
|
43
|
+
state = _useStateData.state,
|
|
44
|
+
setState = _useStateData.setState;
|
|
45
|
+
|
|
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
|
+
/** 缩放 */
|
|
92
|
+
var zoomUpdate = function zoomUpdate(_ref3) {
|
|
93
|
+
var x = _ref3.x,
|
|
94
|
+
y = _ref3.y,
|
|
95
|
+
scale = _ref3.scale;
|
|
96
|
+
if (state.errorTitle) return;
|
|
97
|
+
var value = make3dTransformValue({
|
|
98
|
+
x: x,
|
|
99
|
+
y: y,
|
|
100
|
+
scale: scale
|
|
101
|
+
});
|
|
102
|
+
document.getElementById(state.domId).style.setProperty('transform', value);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** 查看 pdf */
|
|
106
|
+
var pdfRun = /*#__PURE__*/function () {
|
|
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) {
|
|
111
|
+
case 0:
|
|
112
|
+
setState('loading', true);
|
|
113
|
+
_context3.prev = 1;
|
|
114
|
+
_context3.next = 4;
|
|
115
|
+
return sleep(200);
|
|
116
|
+
case 4:
|
|
117
|
+
_context3.next = 6;
|
|
118
|
+
return loadPdfPreviewer(state.domId, fileUrl);
|
|
119
|
+
case 6:
|
|
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
|
+
}));
|
|
132
|
+
state.destroy = function () {
|
|
133
|
+
return pdfPreviewer.destroy();
|
|
134
|
+
};
|
|
135
|
+
_context3.next = 15;
|
|
136
|
+
break;
|
|
137
|
+
case 11:
|
|
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));
|
|
142
|
+
case 15:
|
|
143
|
+
setState('loading', false);
|
|
144
|
+
case 16:
|
|
145
|
+
case "end":
|
|
146
|
+
return _context3.stop();
|
|
147
|
+
}
|
|
148
|
+
}, _callee3, null, [[1, 11]]);
|
|
149
|
+
}));
|
|
150
|
+
return function pdfRun() {
|
|
151
|
+
return _ref4.apply(this, arguments);
|
|
152
|
+
};
|
|
153
|
+
}();
|
|
154
|
+
|
|
155
|
+
/** 查看 image */
|
|
156
|
+
var imageRun = /*#__PURE__*/function () {
|
|
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) {
|
|
161
|
+
case 0:
|
|
162
|
+
setState('loading', true);
|
|
163
|
+
_context5.prev = 1;
|
|
164
|
+
_context5.next = 4;
|
|
165
|
+
return loadImagePreviewer(state.domId, fileUrl);
|
|
166
|
+
case 4:
|
|
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;
|
|
180
|
+
break;
|
|
181
|
+
case 8:
|
|
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));
|
|
186
|
+
case 12:
|
|
187
|
+
setState('loading', false);
|
|
188
|
+
case 13:
|
|
189
|
+
case "end":
|
|
190
|
+
return _context5.stop();
|
|
191
|
+
}
|
|
192
|
+
}, _callee5, null, [[1, 8]]);
|
|
193
|
+
}));
|
|
194
|
+
return function imageRun() {
|
|
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);
|
|
227
|
+
};
|
|
228
|
+
}();
|
|
229
|
+
useEffect(function () {
|
|
230
|
+
startRun();
|
|
231
|
+
return function () {
|
|
232
|
+
var _state$destroy;
|
|
233
|
+
(_state$destroy = state.destroy) === null || _state$destroy === void 0 || _state$destroy.call(state);
|
|
234
|
+
};
|
|
235
|
+
}, []);
|
|
236
|
+
return /*#__PURE__*/_jsx(Modal, _objectSpread(_objectSpread({}, modalProps), {}, {
|
|
237
|
+
open: state.open,
|
|
238
|
+
onCancel: function onCancel() {
|
|
239
|
+
return setState('open', false);
|
|
240
|
+
},
|
|
241
|
+
footer: null,
|
|
242
|
+
centered: true,
|
|
243
|
+
width: "90vw",
|
|
244
|
+
afterOpenChange: function afterOpenChange(open) {
|
|
245
|
+
if (!open) {
|
|
246
|
+
onDestroy();
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
children: /*#__PURE__*/_jsxs(Spin, {
|
|
250
|
+
spinning: state.loading,
|
|
251
|
+
tip: "\u6587\u4EF6\u52A0\u8F7D\u4E2D...",
|
|
252
|
+
children: [/*#__PURE__*/_jsx(ContainerBox, {
|
|
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, {
|
|
281
|
+
onUpdate: zoomUpdate,
|
|
282
|
+
children: /*#__PURE__*/_jsx("div", {
|
|
283
|
+
id: state.domId
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
}), state.download && !state.errorTitle && /*#__PURE__*/_jsx(FloatButton, {
|
|
287
|
+
icon: state.downloadLoading ? /*#__PURE__*/_jsx(LoadingOutlined, {}) : /*#__PURE__*/_jsx(DownloadOutlined, {}),
|
|
288
|
+
onClick: downloadFile
|
|
289
|
+
})]
|
|
290
|
+
})
|
|
291
|
+
}));
|
|
292
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
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."); }
|
|
3
|
+
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); }
|
|
4
|
+
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; }
|
|
5
|
+
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; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { useStateData } from "../../../hooks";
|
|
8
|
+
import React from 'react';
|
|
9
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
+
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
11
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
12
|
+
export var context = {
|
|
13
|
+
domId: 'rex-file-viewer-root',
|
|
14
|
+
portals: {},
|
|
15
|
+
update: function update() {}
|
|
16
|
+
};
|
|
17
|
+
export var FileViewerRoot = function FileViewerRoot() {
|
|
18
|
+
var _useStateData = useStateData(function () {
|
|
19
|
+
return {};
|
|
20
|
+
}),
|
|
21
|
+
update = _useStateData.update;
|
|
22
|
+
context.update = update;
|
|
23
|
+
return /*#__PURE__*/_jsxs(_Fragment, {
|
|
24
|
+
children: [/*#__PURE__*/_jsx("div", {
|
|
25
|
+
id: context.domId
|
|
26
|
+
}), Object.entries(context.portals).map(function (_ref) {
|
|
27
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
28
|
+
key = _ref2[0],
|
|
29
|
+
portal = _ref2[1];
|
|
30
|
+
return /*#__PURE__*/_jsx(React.Fragment, {
|
|
31
|
+
children: portal
|
|
32
|
+
}, key);
|
|
33
|
+
})]
|
|
34
|
+
});
|
|
35
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { IFileViewerOptions } from '../types';
|
|
2
|
+
export { FileViewerRoot } from './file-viewer-root';
|
|
3
|
+
/** 资源展示 */
|
|
4
|
+
export declare const showFileViewer: (options: IFileViewerOptions) => void;
|
|
5
|
+
/** 预览或下载,解析到未支持的文件类型时自动下载 */
|
|
6
|
+
export declare const previewOrDownloadFile: (options: IFileViewerOptions) => Promise<void>;
|
|
@@ -0,0 +1,83 @@
|
|
|
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); }); }; }
|
|
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; }
|
|
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; }
|
|
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; }
|
|
9
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
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; }
|
|
13
|
+
import { createPortal } from 'react-dom';
|
|
14
|
+
import { FILE_VIEWER_FILE_TYPES } from "../types";
|
|
15
|
+
import { downloadResourceFile, getFileInfo } from "../utils";
|
|
16
|
+
import { FileViewerContainer } from "./file-viewer-container";
|
|
17
|
+
import { context } from "./file-viewer-root";
|
|
18
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
19
|
+
export { FileViewerRoot } from "./file-viewer-root";
|
|
20
|
+
|
|
21
|
+
/** 资源展示 */
|
|
22
|
+
export var showFileViewer = function showFileViewer(options) {
|
|
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
|
+
}();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function Demo(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { downloadResourceFile, FileViewerRoot, previewOrDownloadFile, showFileViewer } from "../../..";
|
|
2
|
+
import { Button, Flex } from 'antd';
|
|
3
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
4
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
6
|
+
export default function Demo() {
|
|
7
|
+
return /*#__PURE__*/_jsxs(_Fragment, {
|
|
8
|
+
children: [/*#__PURE__*/_jsx(FileViewerRoot, {}), /*#__PURE__*/_jsxs(Flex, {
|
|
9
|
+
vertical: true,
|
|
10
|
+
gap: 16,
|
|
11
|
+
children: [/*#__PURE__*/_jsx(Button, {
|
|
12
|
+
onClick: function onClick() {
|
|
13
|
+
return showFileViewer({
|
|
14
|
+
fileUrl: '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'
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
children: "pdf"
|
|
18
|
+
}), /*#__PURE__*/_jsx(Button, {
|
|
19
|
+
onClick: function onClick() {
|
|
20
|
+
return showFileViewer({
|
|
21
|
+
fileUrl: 'https://zzzz-zkp.oss-cn-shenzhen.aliyuncs.com/badge%20%281%29.png'
|
|
22
|
+
});
|
|
23
|
+
},
|
|
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"
|
|
58
|
+
})]
|
|
59
|
+
})]
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { IFileViewerOptions } from './types';
|
|
3
|
+
export { downloadResourceFile } from './utils';
|
|
4
|
+
export { FileViewerRoot, previewOrDownloadFile, showFileViewer } from './core';
|
|
5
|
+
export * from './types';
|
|
6
|
+
export declare const FakeFileViewer: FC<IFileViewerOptions>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
export { downloadResourceFile } from "./utils";
|
|
4
|
+
export { FileViewerRoot, previewOrDownloadFile, showFileViewer } from "./core";
|
|
5
|
+
export * from "./types";
|
|
6
|
+
export var FakeFileViewer = function FakeFileViewer() {
|
|
7
|
+
return /*#__PURE__*/_jsx(_Fragment, {});
|
|
8
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const ContainerBox: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GetProps, Modal } from 'antd';
|
|
2
|
+
/** 支持的文件类型 */
|
|
3
|
+
export declare const FILE_VIEWER_FILE_TYPES: readonly ["pdf", "image"];
|
|
4
|
+
export interface IFileViewerOptions extends ModalProps {
|
|
5
|
+
/** 文件路径,注意文件跨域问题 */
|
|
6
|
+
fileUrl: string;
|
|
7
|
+
/** 文件名称 */
|
|
8
|
+
fileName?: string;
|
|
9
|
+
/** 文件类型 */
|
|
10
|
+
fileType?: (typeof FILE_VIEWER_FILE_TYPES)[number];
|
|
11
|
+
}
|
|
12
|
+
export interface IFileViewerContainerProps extends IFileViewerOptions {
|
|
13
|
+
onDestroy: () => void;
|
|
14
|
+
}
|
|
15
|
+
type ModalProps = Omit<GetProps<typeof Modal>, 'open' | 'onCancel' | 'footer' | 'centered' | 'width' | 'afterOpenChange'>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { JsPdfPreview } from '@js-preview/pdf';
|
|
2
|
+
/** 加载 pdf */
|
|
3
|
+
export declare const loadPdfPreviewer: (domId: string, fileUrl: string) => Promise<JsPdfPreview>;
|
|
4
|
+
/** 加载 image */
|
|
5
|
+
export declare const loadImagePreviewer: (domId: string, fileUrl: string) => Promise<{
|
|
6
|
+
save: (fileName: string) => Promise<void>;
|
|
7
|
+
}>;
|
|
8
|
+
/** 下载资源文件, 使用 fetch */
|
|
9
|
+
export declare const downloadResourceFile: (fileUrl: string, fileName?: string) => Promise<void>;
|
|
10
|
+
/** 匹配图片类型 为 image */
|
|
11
|
+
export declare const isImageType: (fileType: string) => boolean;
|
|
12
|
+
/** 根据文件后缀,返回文件类型 */
|
|
13
|
+
export declare const getFileTypeBySuffix: (fileName: string) => string;
|
|
14
|
+
/** 获取文件信息 */
|
|
15
|
+
export declare const getFileInfo: (fileUrl: string) => {
|
|
16
|
+
fileName: string;
|
|
17
|
+
fileType: string;
|
|
18
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
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
|
+
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
|
+
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
|
+
|
|
7
|
+
/** 加载 pdf */
|
|
8
|
+
export var loadPdfPreviewer = /*#__PURE__*/function () {
|
|
9
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(domId, fileUrl) {
|
|
10
|
+
var _yield$import, jsPreviewPdf;
|
|
11
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
12
|
+
while (1) switch (_context.prev = _context.next) {
|
|
13
|
+
case 0:
|
|
14
|
+
_context.next = 2;
|
|
15
|
+
return import('@js-preview/pdf');
|
|
16
|
+
case 2:
|
|
17
|
+
_yield$import = _context.sent;
|
|
18
|
+
jsPreviewPdf = _yield$import.default;
|
|
19
|
+
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
20
|
+
var myPdfPreviewer = jsPreviewPdf.init(document.getElementById(domId), {
|
|
21
|
+
onError: function onError(e) {
|
|
22
|
+
// console.log('发生错误', e);
|
|
23
|
+
reject(e);
|
|
24
|
+
},
|
|
25
|
+
onRendered: function onRendered() {
|
|
26
|
+
// console.log('渲染完成');
|
|
27
|
+
resolve(myPdfPreviewer);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
myPdfPreviewer.preview(fileUrl);
|
|
31
|
+
}));
|
|
32
|
+
case 5:
|
|
33
|
+
case "end":
|
|
34
|
+
return _context.stop();
|
|
35
|
+
}
|
|
36
|
+
}, _callee);
|
|
37
|
+
}));
|
|
38
|
+
return function loadPdfPreviewer(_x, _x2) {
|
|
39
|
+
return _ref.apply(this, arguments);
|
|
40
|
+
};
|
|
41
|
+
}();
|
|
42
|
+
|
|
43
|
+
/** 加载 image */
|
|
44
|
+
export var loadImagePreviewer = /*#__PURE__*/function () {
|
|
45
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(domId, fileUrl) {
|
|
46
|
+
var container, img, obj;
|
|
47
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
48
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
49
|
+
case 0:
|
|
50
|
+
container = document.getElementById(domId);
|
|
51
|
+
img = document.createElement('img');
|
|
52
|
+
img.src = fileUrl;
|
|
53
|
+
img.style.maxWidth = '100%';
|
|
54
|
+
img.style.height = '100%';
|
|
55
|
+
img.style.objectFit = 'contain';
|
|
56
|
+
img.style.display = 'block';
|
|
57
|
+
img.style.margin = 'auto';
|
|
58
|
+
container.appendChild(img);
|
|
59
|
+
obj = {
|
|
60
|
+
save: function save(fileName) {
|
|
61
|
+
return downloadResourceFile(fileUrl, fileName);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
return _context2.abrupt("return", new Promise(function (rel, rej) {
|
|
65
|
+
img.onload = function () {
|
|
66
|
+
// 图片加载完成
|
|
67
|
+
rel(obj);
|
|
68
|
+
};
|
|
69
|
+
img.onerror = function (e) {
|
|
70
|
+
// console.error('图片加载失败', e);
|
|
71
|
+
rej(e);
|
|
72
|
+
};
|
|
73
|
+
}));
|
|
74
|
+
case 11:
|
|
75
|
+
case "end":
|
|
76
|
+
return _context2.stop();
|
|
77
|
+
}
|
|
78
|
+
}, _callee2);
|
|
79
|
+
}));
|
|
80
|
+
return function loadImagePreviewer(_x3, _x4) {
|
|
81
|
+
return _ref2.apply(this, arguments);
|
|
82
|
+
};
|
|
83
|
+
}();
|
|
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
|
+
|
|
104
|
+
/** 匹配图片类型 为 image */
|
|
105
|
+
export var isImageType = function isImageType(fileType) {
|
|
106
|
+
return ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'svg'].includes(fileType.toLowerCase());
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** 根据文件后缀,返回文件类型 */
|
|
110
|
+
export var getFileTypeBySuffix = function getFileTypeBySuffix(fileName) {
|
|
111
|
+
var _fileName$split$pop;
|
|
112
|
+
var suffix = (_fileName$split$pop = fileName.split('.').pop()) === null || _fileName$split$pop === void 0 ? void 0 : _fileName$split$pop.toLowerCase();
|
|
113
|
+
if (!suffix) return 'unknown';
|
|
114
|
+
if (suffix === 'pdf') return 'pdf';
|
|
115
|
+
if (isImageType(suffix)) return 'image';
|
|
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
|
+
};
|
|
130
|
+
};
|
package/dist/components/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teamias/rex-design",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"description": "A react library developed with dumi",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"@ant-design/pro-components": "^2.8.10",
|
|
53
53
|
"@ant-design/pro-form": "^2.32.0",
|
|
54
54
|
"@iconify/react": "^6.0.0",
|
|
55
|
+
"@js-preview/pdf": "^2.0.10",
|
|
55
56
|
"@tiptap/core": "^2.12.0",
|
|
56
57
|
"@tiptap/extension-bullet-list": "2.12.0",
|
|
57
58
|
"@tiptap/extension-color": "^2.12.0",
|
|
@@ -76,6 +77,7 @@
|
|
|
76
77
|
"radash": "^12.1.1",
|
|
77
78
|
"rc-virtual-list": "^3.19.1",
|
|
78
79
|
"react-intl": "^7.1.11",
|
|
80
|
+
"react-quick-pinch-zoom": "^5.1.1",
|
|
79
81
|
"react-router": "6",
|
|
80
82
|
"styled-components": "^6.1.19"
|
|
81
83
|
},
|