@jzwd/vnd-design 0.0.0-stage → 0.2.0

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.
Files changed (47) hide show
  1. package/README.md +259 -2
  2. package/dist/file-center/api/endpoints.d.ts +42 -0
  3. package/dist/file-center/api/endpoints.js +45 -0
  4. package/dist/file-center/api/error.d.ts +6 -0
  5. package/dist/file-center/api/error.js +34 -0
  6. package/dist/file-center/api/index.d.ts +64 -0
  7. package/dist/file-center/api/index.js +238 -0
  8. package/dist/file-center/api/request.d.ts +28 -0
  9. package/dist/file-center/api/request.js +170 -0
  10. package/dist/file-center/api/token.d.ts +10 -0
  11. package/dist/file-center/api/token.js +122 -0
  12. package/dist/file-center/components/FcFileList/index.d.ts +39 -0
  13. package/dist/file-center/components/FcFileList/index.js +804 -0
  14. package/dist/file-center/components/FcFileList/style.css +106 -0
  15. package/dist/file-center/components/FcImage/index.d.ts +30 -0
  16. package/dist/file-center/components/FcImage/index.js +144 -0
  17. package/dist/file-center/components/FcImage/style.css +14 -0
  18. package/dist/file-center/components/FcUpload/index.d.ts +5 -0
  19. package/dist/file-center/components/FcUpload/index.js +642 -0
  20. package/dist/file-center/components/FcUpload/interface.d.ts +65 -0
  21. package/dist/file-center/components/FcUpload/interface.js +1 -0
  22. package/dist/file-center/components/FcUpload/style.css +279 -0
  23. package/dist/file-center/context.d.ts +22 -0
  24. package/dist/file-center/context.js +22 -0
  25. package/dist/file-center/index.d.ts +26 -0
  26. package/dist/file-center/index.js +21 -0
  27. package/dist/file-center/types.d.ts +144 -0
  28. package/dist/file-center/types.js +1 -0
  29. package/dist/file-center/upload.d.ts +35 -0
  30. package/dist/file-center/upload.js +295 -0
  31. package/dist/file-center/utils/common.d.ts +10 -0
  32. package/dist/file-center/utils/common.js +165 -0
  33. package/dist/file-center/utils/compress.d.ts +18 -0
  34. package/dist/file-center/utils/compress.js +108 -0
  35. package/dist/file-center/utils/md5.d.ts +5 -0
  36. package/dist/file-center/utils/md5.js +41 -0
  37. package/dist/index.d.ts +11 -0
  38. package/dist/index.js +15 -0
  39. package/dist/mobile/FcImage/index.d.ts +26 -0
  40. package/dist/mobile/FcImage/index.js +114 -0
  41. package/dist/mobile/FcImage/style.css +11 -0
  42. package/dist/mobile/FcUpload/index.d.ts +74 -0
  43. package/dist/mobile/FcUpload/index.js +539 -0
  44. package/dist/mobile/FcUpload/style.css +233 -0
  45. package/dist/mobile/index.d.ts +13 -0
  46. package/dist/mobile/index.js +9 -0
  47. package/package.json +70 -4
@@ -0,0 +1,295 @@
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 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
+ 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
+ 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
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ 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); }
7
+ 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; }
8
+ 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); } }
9
+ 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); }); }; }
10
+ import { FC_ENDPOINTS, DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_THRESHOLD, addReferences, checkInstantUpload, fcFormRequest, initChunkUpload, mergeChunkUpload, uploadChunk } from "./api";
11
+ import { FcError } from "./api/error";
12
+ import { resolveFcContext } from "./context";
13
+ import { compressImage } from "./utils/compress";
14
+ import { withRetry } from "./utils/common";
15
+ import { calcFileMd5 } from "./utils/md5";
16
+
17
+ /** 上传参数 */
18
+
19
+ /** 进度分段权重:MD5 占 10%,上传占 90% */
20
+ var MD5_PERCENT_WEIGHT = 10;
21
+ function buildProgress(stage, percent) {
22
+ return {
23
+ stage: stage,
24
+ percent: Math.min(100, Math.max(0, Math.round(percent)))
25
+ };
26
+ }
27
+
28
+ /** 普通上传(OPEN-2):表单单文件,带进度与重试 */
29
+ function simpleUpload(_x, _x2, _x3, _x4) {
30
+ return _simpleUpload.apply(this, arguments);
31
+ }
32
+ /** 分片上传(OPEN-5):初始化 → 逐片上传(断点续传跳过已完成分片) → 合并 */
33
+ function _simpleUpload() {
34
+ _simpleUpload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ctx, options, file, md5) {
35
+ var bizType, _onProgress, signal;
36
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
37
+ while (1) switch (_context.prev = _context.next) {
38
+ case 0:
39
+ bizType = options.bizType, _onProgress = options.onProgress, signal = options.signal;
40
+ return _context.abrupt("return", withRetry(function () {
41
+ var form = new FormData();
42
+ form.append('file', file, file.name);
43
+ form.append('md5', md5);
44
+ if (bizType) form.append('bizType', bizType);
45
+ return fcFormRequest(ctx, FC_ENDPOINTS.upload, form, {
46
+ signal: signal,
47
+ onProgress: function onProgress(percent) {
48
+ _onProgress === null || _onProgress === void 0 || _onProgress(buildProgress('uploading', MD5_PERCENT_WEIGHT + percent * (100 - MD5_PERCENT_WEIGHT) / 100));
49
+ }
50
+ });
51
+ }, 2));
52
+ case 2:
53
+ case "end":
54
+ return _context.stop();
55
+ }
56
+ }, _callee);
57
+ }));
58
+ return _simpleUpload.apply(this, arguments);
59
+ }
60
+ function chunkUpload(_x5, _x6, _x7, _x8) {
61
+ return _chunkUpload.apply(this, arguments);
62
+ }
63
+ /**
64
+ * 上传文件到文件中心(组件库上传能力总入口)
65
+ *
66
+ * 完整流程:
67
+ * 1. 图片按配置压缩(可选);
68
+ * 2. 分片读取计算 MD5(进度 0-10%);
69
+ * 3. 秒传校验:同 MD5 已存在则直接返回(OPEN-4);
70
+ * 4. 大文件走分片上传(OPEN-5),小文件走普通上传(OPEN-2),均自动重试;
71
+ * 5. 传入 bizInfo 时自动登记业务引用(OPEN-12),失败不影响上传结果。
72
+ */
73
+ function _chunkUpload() {
74
+ _chunkUpload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(ctx, options, file, md5) {
75
+ var _options$chunkSize, _init$chunkSize, _init$doneParts;
76
+ var bizType, onProgress, signal, chunkSize, totalParts, init, actualChunkSize, total, doneParts, _loop, partNumber;
77
+ return _regeneratorRuntime().wrap(function _callee2$(_context3) {
78
+ while (1) switch (_context3.prev = _context3.next) {
79
+ case 0:
80
+ bizType = options.bizType, onProgress = options.onProgress, signal = options.signal;
81
+ chunkSize = (_options$chunkSize = options.chunkSize) !== null && _options$chunkSize !== void 0 ? _options$chunkSize : DEFAULT_CHUNK_SIZE;
82
+ totalParts = Math.ceil(file.size / chunkSize);
83
+ _context3.next = 5;
84
+ return initChunkUpload(ctx, {
85
+ md5: md5,
86
+ name: file.name,
87
+ size: file.size,
88
+ totalParts: totalParts,
89
+ bizType: bizType
90
+ });
91
+ case 5:
92
+ init = _context3.sent;
93
+ actualChunkSize = (_init$chunkSize = init.chunkSize) !== null && _init$chunkSize !== void 0 ? _init$chunkSize : chunkSize;
94
+ total = Math.ceil(file.size / actualChunkSize);
95
+ doneParts = new Set((_init$doneParts = init.doneParts) !== null && _init$doneParts !== void 0 ? _init$doneParts : []);
96
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop(partNumber) {
97
+ var start, blob;
98
+ return _regeneratorRuntime().wrap(function _loop$(_context2) {
99
+ while (1) switch (_context2.prev = _context2.next) {
100
+ case 0:
101
+ if (!(signal !== null && signal !== void 0 && signal.aborted)) {
102
+ _context2.next = 2;
103
+ break;
104
+ }
105
+ throw new FcError('上传已取消', 'ABORT');
106
+ case 2:
107
+ if (!doneParts.has(partNumber)) {
108
+ _context2.next = 4;
109
+ break;
110
+ }
111
+ return _context2.abrupt("return", 1);
112
+ case 4:
113
+ start = (partNumber - 1) * actualChunkSize;
114
+ blob = file.slice(start, start + actualChunkSize); // 单片失败重试 2 次;uploadId 不变,服务端按分片号幂等覆盖
115
+ _context2.next = 8;
116
+ return withRetry(function () {
117
+ return uploadChunk(ctx, {
118
+ uploadId: init.uploadId,
119
+ partNumber: partNumber,
120
+ blob: blob
121
+ }, {
122
+ signal: signal
123
+ });
124
+ }, 2);
125
+ case 8:
126
+ onProgress === null || onProgress === void 0 || onProgress(buildProgress('uploading', MD5_PERCENT_WEIGHT + partNumber / total * (100 - MD5_PERCENT_WEIGHT)));
127
+ case 9:
128
+ case "end":
129
+ return _context2.stop();
130
+ }
131
+ }, _loop);
132
+ });
133
+ partNumber = 1;
134
+ case 11:
135
+ if (!(partNumber <= total)) {
136
+ _context3.next = 18;
137
+ break;
138
+ }
139
+ return _context3.delegateYield(_loop(partNumber), "t0", 13);
140
+ case 13:
141
+ if (!_context3.t0) {
142
+ _context3.next = 15;
143
+ break;
144
+ }
145
+ return _context3.abrupt("continue", 15);
146
+ case 15:
147
+ partNumber += 1;
148
+ _context3.next = 11;
149
+ break;
150
+ case 18:
151
+ onProgress === null || onProgress === void 0 || onProgress(buildProgress('merging', 99));
152
+ return _context3.abrupt("return", mergeChunkUpload(ctx, {
153
+ uploadId: init.uploadId
154
+ }));
155
+ case 20:
156
+ case "end":
157
+ return _context3.stop();
158
+ }
159
+ }, _callee2);
160
+ }));
161
+ return _chunkUpload.apply(this, arguments);
162
+ }
163
+ export function uploadFile(_x9) {
164
+ return _uploadFile.apply(this, arguments);
165
+ }
166
+
167
+ /** 登记业务引用(OPEN-12);失败仅告警,不影响上传结果 */
168
+ function _uploadFile() {
169
+ _uploadFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(options) {
170
+ var _options$chunkThresho;
171
+ var file, bizType, bizInfo, _options$instant, instant, onProgress, signal, ctx, chunkThreshold, uploadTarget, compressOptions, md5, result, fileInfo;
172
+ return _regeneratorRuntime().wrap(function _callee3$(_context4) {
173
+ while (1) switch (_context4.prev = _context4.next) {
174
+ case 0:
175
+ file = options.file, bizType = options.bizType, bizInfo = options.bizInfo, _options$instant = options.instant, instant = _options$instant === void 0 ? true : _options$instant, onProgress = options.onProgress, signal = options.signal;
176
+ ctx = resolveFcContext(options);
177
+ chunkThreshold = (_options$chunkThresho = options.chunkThreshold) !== null && _options$chunkThresho !== void 0 ? _options$chunkThresho : DEFAULT_CHUNK_THRESHOLD;
178
+ if (!(signal !== null && signal !== void 0 && signal.aborted)) {
179
+ _context4.next = 5;
180
+ break;
181
+ }
182
+ throw new FcError('上传已取消', 'ABORT');
183
+ case 5:
184
+ // 1. 图片压缩(可选)
185
+ uploadTarget = file;
186
+ if (!(options.compress && file.type.startsWith('image/'))) {
187
+ _context4.next = 11;
188
+ break;
189
+ }
190
+ compressOptions = options.compress === true ? {} : options.compress;
191
+ _context4.next = 10;
192
+ return compressImage(file, compressOptions);
193
+ case 10:
194
+ uploadTarget = _context4.sent;
195
+ case 11:
196
+ _context4.next = 13;
197
+ return calcFileMd5(uploadTarget, function (percent) {
198
+ onProgress === null || onProgress === void 0 || onProgress(buildProgress('md5', percent * MD5_PERCENT_WEIGHT / 100));
199
+ });
200
+ case 13:
201
+ md5 = _context4.sent;
202
+ if (!instant) {
203
+ _context4.next = 28;
204
+ break;
205
+ }
206
+ _context4.prev = 15;
207
+ _context4.next = 18;
208
+ return checkInstantUpload(ctx, {
209
+ md5: md5,
210
+ name: uploadTarget.name,
211
+ size: uploadTarget.size,
212
+ bizType: bizType
213
+ });
214
+ case 18:
215
+ result = _context4.sent;
216
+ if (!(result.uploaded && result.file)) {
217
+ _context4.next = 24;
218
+ break;
219
+ }
220
+ onProgress === null || onProgress === void 0 || onProgress(buildProgress('uploading', 100));
221
+ _context4.next = 23;
222
+ return registerReferenceIfNeeded(ctx, result.file, bizInfo);
223
+ case 23:
224
+ return _context4.abrupt("return", _context4.sent);
225
+ case 24:
226
+ _context4.next = 28;
227
+ break;
228
+ case 26:
229
+ _context4.prev = 26;
230
+ _context4.t0 = _context4["catch"](15);
231
+ case 28:
232
+ if (!(uploadTarget.size > chunkThreshold)) {
233
+ _context4.next = 34;
234
+ break;
235
+ }
236
+ _context4.next = 31;
237
+ return chunkUpload(ctx, options, uploadTarget, md5);
238
+ case 31:
239
+ _context4.t1 = _context4.sent;
240
+ _context4.next = 37;
241
+ break;
242
+ case 34:
243
+ _context4.next = 36;
244
+ return simpleUpload(ctx, options, uploadTarget, md5);
245
+ case 36:
246
+ _context4.t1 = _context4.sent;
247
+ case 37:
248
+ fileInfo = _context4.t1;
249
+ onProgress === null || onProgress === void 0 || onProgress(buildProgress('uploading', 100));
250
+
251
+ // 5. 自动登记业务引用
252
+ return _context4.abrupt("return", registerReferenceIfNeeded(ctx, fileInfo, bizInfo));
253
+ case 40:
254
+ case "end":
255
+ return _context4.stop();
256
+ }
257
+ }, _callee3, null, [[15, 26]]);
258
+ }));
259
+ return _uploadFile.apply(this, arguments);
260
+ }
261
+ function registerReferenceIfNeeded(_x10, _x11, _x12) {
262
+ return _registerReferenceIfNeeded.apply(this, arguments);
263
+ }
264
+ function _registerReferenceIfNeeded() {
265
+ _registerReferenceIfNeeded = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(ctx, fileInfo, bizInfo) {
266
+ return _regeneratorRuntime().wrap(function _callee4$(_context5) {
267
+ while (1) switch (_context5.prev = _context5.next) {
268
+ case 0:
269
+ if (!(bizInfo !== null && bizInfo !== void 0 && bizInfo.bizType && bizInfo !== null && bizInfo !== void 0 && bizInfo.bizId)) {
270
+ _context5.next = 9;
271
+ break;
272
+ }
273
+ _context5.prev = 1;
274
+ _context5.next = 4;
275
+ return addReferences(ctx, [_objectSpread({
276
+ fileId: fileInfo.id
277
+ }, bizInfo)]);
278
+ case 4:
279
+ _context5.next = 9;
280
+ break;
281
+ case 6:
282
+ _context5.prev = 6;
283
+ _context5.t0 = _context5["catch"](1);
284
+ // eslint-disable-next-line no-console
285
+ console.warn('[vnd-design] 文件上传成功,但业务引用登记失败:', _context5.t0);
286
+ case 9:
287
+ return _context5.abrupt("return", fileInfo);
288
+ case 10:
289
+ case "end":
290
+ return _context5.stop();
291
+ }
292
+ }, _callee4, null, [[1, 6]]);
293
+ }));
294
+ return _registerReferenceIfNeeded.apply(this, arguments);
295
+ }
@@ -0,0 +1,10 @@
1
+ /** 格式化文件大小显示 */
2
+ export declare function formatFileSize(size: number): string;
3
+ /** 复制文本到剪贴板(clipboard API 不可用时降级 execCommand) */
4
+ export declare function copyText(text: string): Promise<boolean>;
5
+ /** 触发浏览器下载 */
6
+ export declare function downloadFile(url: string, filename?: string): void;
7
+ /** 根据 MIME/扩展名粗略判断文件大类 */
8
+ export declare function getFileCategory(mime: string, ext?: string): 'image' | 'video' | 'audio' | 'pdf' | 'archive' | 'other';
9
+ /** 简单重试包装:失败后延迟重试 */
10
+ export declare function withRetry<T>(fn: () => Promise<T>, retries?: number, delay?: number): Promise<T>;
@@ -0,0 +1,165 @@
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
+ /** 格式化文件大小显示 */
6
+ export function formatFileSize(size) {
7
+ if (size < 1024) return "".concat(size, " B");
8
+ if (size < 1024 * 1024) return "".concat((size / 1024).toFixed(1), " KB");
9
+ if (size < 1024 * 1024 * 1024) return "".concat((size / 1024 / 1024).toFixed(1), " MB");
10
+ return "".concat((size / 1024 / 1024 / 1024).toFixed(2), " GB");
11
+ }
12
+
13
+ /** 复制文本到剪贴板(clipboard API 不可用时降级 execCommand) */
14
+ export function copyText(_x) {
15
+ return _copyText.apply(this, arguments);
16
+ }
17
+
18
+ /** 触发浏览器下载 */
19
+ function _copyText() {
20
+ _copyText = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(text) {
21
+ var textarea, ok;
22
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
23
+ while (1) switch (_context.prev = _context.next) {
24
+ case 0:
25
+ _context.prev = 0;
26
+ if (!(navigator.clipboard && window.isSecureContext)) {
27
+ _context.next = 5;
28
+ break;
29
+ }
30
+ _context.next = 4;
31
+ return navigator.clipboard.writeText(text);
32
+ case 4:
33
+ return _context.abrupt("return", true);
34
+ case 5:
35
+ _context.next = 9;
36
+ break;
37
+ case 7:
38
+ _context.prev = 7;
39
+ _context.t0 = _context["catch"](0);
40
+ case 9:
41
+ _context.prev = 9;
42
+ textarea = document.createElement('textarea');
43
+ textarea.value = text;
44
+ textarea.style.position = 'fixed';
45
+ textarea.style.opacity = '0';
46
+ document.body.appendChild(textarea);
47
+ textarea.select();
48
+ ok = document.execCommand('copy');
49
+ document.body.removeChild(textarea);
50
+ return _context.abrupt("return", ok);
51
+ case 21:
52
+ _context.prev = 21;
53
+ _context.t1 = _context["catch"](9);
54
+ return _context.abrupt("return", false);
55
+ case 24:
56
+ case "end":
57
+ return _context.stop();
58
+ }
59
+ }, _callee, null, [[0, 7], [9, 21]]);
60
+ }));
61
+ return _copyText.apply(this, arguments);
62
+ }
63
+ export function downloadFile(url, filename) {
64
+ var anchor = document.createElement('a');
65
+ anchor.href = url;
66
+ if (filename) anchor.download = filename;
67
+ anchor.target = '_blank';
68
+ anchor.rel = 'noopener';
69
+ document.body.appendChild(anchor);
70
+ anchor.click();
71
+ document.body.removeChild(anchor);
72
+ }
73
+
74
+ /** 根据 MIME/扩展名粗略判断文件大类 */
75
+ export function getFileCategory(mime, ext) {
76
+ if (mime.startsWith('image/')) return 'image';
77
+ if (mime.startsWith('video/')) return 'video';
78
+ if (mime.startsWith('audio/')) return 'audio';
79
+ if (mime === 'application/pdf' || ext === 'pdf') return 'pdf';
80
+ if (['zip', 'rar', '7z', 'tar', 'gz'].includes(ext !== null && ext !== void 0 ? ext : '')) return 'archive';
81
+ return 'other';
82
+ }
83
+
84
+ /** 简单重试包装:失败后延迟重试 */
85
+ export function withRetry(_x2) {
86
+ return _withRetry.apply(this, arguments);
87
+ }
88
+ function _withRetry() {
89
+ _withRetry = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(fn) {
90
+ var retries,
91
+ delay,
92
+ lastError,
93
+ _loop,
94
+ _ret,
95
+ attempt,
96
+ _args3 = arguments;
97
+ return _regeneratorRuntime().wrap(function _callee2$(_context3) {
98
+ while (1) switch (_context3.prev = _context3.next) {
99
+ case 0:
100
+ retries = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : 2;
101
+ delay = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : 1000;
102
+ _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop(attempt) {
103
+ return _regeneratorRuntime().wrap(function _loop$(_context2) {
104
+ while (1) switch (_context2.prev = _context2.next) {
105
+ case 0:
106
+ _context2.prev = 0;
107
+ _context2.next = 3;
108
+ return fn();
109
+ case 3:
110
+ _context2.t0 = _context2.sent;
111
+ return _context2.abrupt("return", {
112
+ v: _context2.t0
113
+ });
114
+ case 7:
115
+ _context2.prev = 7;
116
+ _context2.t1 = _context2["catch"](0);
117
+ if (!((_context2.t1 === null || _context2.t1 === void 0 ? void 0 : _context2.t1.code) === 'ABORT')) {
118
+ _context2.next = 11;
119
+ break;
120
+ }
121
+ throw _context2.t1;
122
+ case 11:
123
+ lastError = _context2.t1;
124
+ if (!(attempt < retries)) {
125
+ _context2.next = 15;
126
+ break;
127
+ }
128
+ _context2.next = 15;
129
+ return new Promise(function (resolve) {
130
+ return setTimeout(resolve, delay * (attempt + 1));
131
+ });
132
+ case 15:
133
+ case "end":
134
+ return _context2.stop();
135
+ }
136
+ }, _loop, null, [[0, 7]]);
137
+ });
138
+ attempt = 0;
139
+ case 4:
140
+ if (!(attempt <= retries)) {
141
+ _context3.next = 12;
142
+ break;
143
+ }
144
+ return _context3.delegateYield(_loop(attempt), "t0", 6);
145
+ case 6:
146
+ _ret = _context3.t0;
147
+ if (!_ret) {
148
+ _context3.next = 9;
149
+ break;
150
+ }
151
+ return _context3.abrupt("return", _ret.v);
152
+ case 9:
153
+ attempt += 1;
154
+ _context3.next = 4;
155
+ break;
156
+ case 12:
157
+ throw lastError;
158
+ case 13:
159
+ case "end":
160
+ return _context3.stop();
161
+ }
162
+ }, _callee2);
163
+ }));
164
+ return _withRetry.apply(this, arguments);
165
+ }
@@ -0,0 +1,18 @@
1
+ /** 图片上传前压缩配置 */
2
+ export interface FcCompressOptions {
3
+ /** 压缩后最大宽度(像素),超出等比缩放;默认不限制 */
4
+ maxWidth?: number;
5
+ /** 压缩后最大高度(像素),超出等比缩放;默认不限制 */
6
+ maxHeight?: number;
7
+ /** JPEG/WebP 压缩质量(0-1),默认 0.8 */
8
+ quality?: number;
9
+ /** 小于该体积(字节)的图片跳过压缩,默认 100KB */
10
+ skipBelow?: number;
11
+ }
12
+ /**
13
+ * 图片上传前压缩(对应需求 3.2.4「图片上传支持上传前压缩选项」)
14
+ * - 非图片、gif/svg(可能含动画)、体积过小的图片直接返回原文件;
15
+ * - 压缩结果比原文件还大时也返回原文件;
16
+ * - 压缩失败(浏览器不支持等)静默降级为原文件,不阻塞上传。
17
+ */
18
+ export declare function compressImage(file: File, options?: FcCompressOptions): Promise<File>;
@@ -0,0 +1,108 @@
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
+ /** 图片上传前压缩配置 */
6
+
7
+ var DEFAULT_SKIP_BELOW = 100 * 1024;
8
+
9
+ /** 判断是否为可压缩的图片类型 */
10
+ function isCompressible(file) {
11
+ return /^image\/(jpeg|png|webp|bmp)$/.test(file.type);
12
+ }
13
+
14
+ /**
15
+ * 图片上传前压缩(对应需求 3.2.4「图片上传支持上传前压缩选项」)
16
+ * - 非图片、gif/svg(可能含动画)、体积过小的图片直接返回原文件;
17
+ * - 压缩结果比原文件还大时也返回原文件;
18
+ * - 压缩失败(浏览器不支持等)静默降级为原文件,不阻塞上传。
19
+ */
20
+ export function compressImage(_x) {
21
+ return _compressImage.apply(this, arguments);
22
+ }
23
+ function _compressImage() {
24
+ _compressImage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(file) {
25
+ var options,
26
+ maxWidth,
27
+ maxHeight,
28
+ _options$quality,
29
+ quality,
30
+ _options$skipBelow,
31
+ skipBelow,
32
+ _bitmap$close,
33
+ bitmap,
34
+ width,
35
+ height,
36
+ canvas,
37
+ ctx,
38
+ outputType,
39
+ blob,
40
+ _args = arguments;
41
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
42
+ while (1) switch (_context.prev = _context.next) {
43
+ case 0:
44
+ options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
45
+ maxWidth = options.maxWidth, maxHeight = options.maxHeight, _options$quality = options.quality, quality = _options$quality === void 0 ? 0.8 : _options$quality, _options$skipBelow = options.skipBelow, skipBelow = _options$skipBelow === void 0 ? DEFAULT_SKIP_BELOW : _options$skipBelow;
46
+ if (!(!isCompressible(file) || file.type === 'image/gif' || file.size <= skipBelow)) {
47
+ _context.next = 4;
48
+ break;
49
+ }
50
+ return _context.abrupt("return", file);
51
+ case 4:
52
+ _context.prev = 4;
53
+ _context.next = 7;
54
+ return createImageBitmap(file);
55
+ case 7:
56
+ bitmap = _context.sent;
57
+ width = bitmap.width, height = bitmap.height;
58
+ if (maxWidth && width > maxWidth) {
59
+ height = Math.round(height * maxWidth / width);
60
+ width = maxWidth;
61
+ }
62
+ if (maxHeight && height > maxHeight) {
63
+ width = Math.round(width * maxHeight / height);
64
+ height = maxHeight;
65
+ }
66
+ canvas = document.createElement('canvas');
67
+ canvas.width = width;
68
+ canvas.height = height;
69
+ ctx = canvas.getContext('2d');
70
+ if (ctx) {
71
+ _context.next = 17;
72
+ break;
73
+ }
74
+ return _context.abrupt("return", file);
75
+ case 17:
76
+ ctx.drawImage(bitmap, 0, 0, width, height);
77
+ (_bitmap$close = bitmap.close) === null || _bitmap$close === void 0 || _bitmap$close.call(bitmap);
78
+
79
+ // png 带透明通道,转 jpeg 会丢透明,统一按原类型输出(png 走无损重编码)
80
+ outputType = file.type === 'image/png' ? 'image/png' : 'image/jpeg';
81
+ _context.next = 22;
82
+ return new Promise(function (resolve) {
83
+ return canvas.toBlob(resolve, outputType, quality);
84
+ });
85
+ case 22:
86
+ blob = _context.sent;
87
+ if (!(!blob || blob.size >= file.size)) {
88
+ _context.next = 25;
89
+ break;
90
+ }
91
+ return _context.abrupt("return", file);
92
+ case 25:
93
+ return _context.abrupt("return", new File([blob], file.name, {
94
+ type: outputType,
95
+ lastModified: Date.now()
96
+ }));
97
+ case 28:
98
+ _context.prev = 28;
99
+ _context.t0 = _context["catch"](4);
100
+ return _context.abrupt("return", file);
101
+ case 31:
102
+ case "end":
103
+ return _context.stop();
104
+ }
105
+ }, _callee, null, [[4, 28]]);
106
+ }));
107
+ return _compressImage.apply(this, arguments);
108
+ }