@wapri/preview 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +78 -2
- package/dist/constant/index.d.ts +51 -0
- package/dist/constant/index.js +58 -0
- package/dist/core/context.d.ts +21 -0
- package/dist/core/context.js +78 -0
- package/dist/core/lazy.d.ts +43 -0
- package/dist/core/lazy.js +175 -0
- package/dist/core/source.d.ts +20 -0
- package/dist/core/source.js +118 -0
- package/dist/core/style.d.ts +8 -0
- package/dist/core/style.js +20 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/pdf/index.d.ts +8 -0
- package/dist/pdf/index.js +364 -0
- package/dist/pdf/load.d.ts +15 -0
- package/dist/pdf/load.js +78 -0
- package/dist/previewFile.d.ts +18 -0
- package/dist/previewFile.js +54 -0
- package/dist/text/index.d.ts +8 -0
- package/dist/text/index.js +118 -0
- package/dist/types/common.d.ts +68 -0
- package/dist/types/common.js +1 -0
- package/dist/types/pdf.d.ts +23 -0
- package/dist/types/pdf.js +1 -0
- package/dist/types/text.d.ts +14 -0
- package/dist/types/text.js +1 -0
- package/dist/types/word.d.ts +57 -0
- package/dist/types/word.js +1 -0
- package/dist/types/xlsx.d.ts +45 -0
- package/dist/types/xlsx.js +1 -0
- package/dist/vendor/pdf.worker.min.mjs +21 -0
- package/dist/vendor/xlsx.full.min.js +22 -0
- package/dist/word/font.d.ts +20 -0
- package/dist/word/font.js +132 -0
- package/dist/word/index.d.ts +8 -0
- package/dist/word/index.js +237 -0
- package/dist/word/layout.d.ts +56 -0
- package/dist/word/layout.js +365 -0
- package/dist/word/parse.d.ts +6 -0
- package/dist/word/parse.js +315 -0
- package/dist/word/render.d.ts +17 -0
- package/dist/word/render.js +210 -0
- package/dist/xlsx/index.d.ts +8 -0
- package/dist/xlsx/index.js +195 -0
- package/dist/xlsx/parse.d.ts +14 -0
- package/dist/xlsx/parse.js +177 -0
- package/dist/xlsx/render.d.ts +17 -0
- package/dist/xlsx/render.js +144 -0
- package/dist/xlsx/workerSource.d.ts +7 -0
- package/dist/xlsx/workerSource.js +8 -0
- package/package.json +28 -3
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
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; }
|
|
2
|
+
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); } }
|
|
3
|
+
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); }); }; }
|
|
4
|
+
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); }
|
|
5
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
6
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
7
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
8
|
+
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); }
|
|
9
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
10
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
11
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
12
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
13
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
14
|
+
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
|
|
15
|
+
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
|
|
16
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
17
|
+
function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
|
|
18
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
19
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
20
|
+
/**
|
|
21
|
+
* 输入来源解析:把 File / Blob / ArrayBuffer / URL 统一成可识别的字节流。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { detectFileType, getFileName, isFileUrl, readFileAsArrayBuffer } from '@wapri/gbase';
|
|
25
|
+
export var PreviewUnsupportedError = /*#__PURE__*/function (_Error) {
|
|
26
|
+
_inherits(PreviewUnsupportedError, _Error);
|
|
27
|
+
var _super = _createSuper(PreviewUnsupportedError);
|
|
28
|
+
function PreviewUnsupportedError(message) {
|
|
29
|
+
var _this;
|
|
30
|
+
_classCallCheck(this, PreviewUnsupportedError);
|
|
31
|
+
_this = _super.call(this, message);
|
|
32
|
+
_this.name = 'PreviewUnsupportedError';
|
|
33
|
+
return _this;
|
|
34
|
+
}
|
|
35
|
+
return _createClass(PreviewUnsupportedError);
|
|
36
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
37
|
+
var isUrlSource = function isUrlSource(input) {
|
|
38
|
+
return _typeof(input) === 'object' && input !== null && !(input instanceof Blob) && !(input instanceof ArrayBuffer) && !ArrayBuffer.isView(input) && 'url' in input;
|
|
39
|
+
};
|
|
40
|
+
var toFileInput = function toFileInput(input) {
|
|
41
|
+
if (isUrlSource(input)) return input.url;
|
|
42
|
+
return input;
|
|
43
|
+
};
|
|
44
|
+
var resolveName = function resolveName(input) {
|
|
45
|
+
if (isUrlSource(input)) {
|
|
46
|
+
return input.fileName || getFileName(input.url);
|
|
47
|
+
}
|
|
48
|
+
return getFileName(toFileInput(input));
|
|
49
|
+
};
|
|
50
|
+
var toPreviewKind = function toPreviewKind(category, extension) {
|
|
51
|
+
switch (category) {
|
|
52
|
+
case 'pdf':
|
|
53
|
+
return 'pdf';
|
|
54
|
+
case 'word':
|
|
55
|
+
if (extension === 'doc') {
|
|
56
|
+
throw new PreviewUnsupportedError('暂不支持 .doc 旧格式,请另存为 .docx 后再预览');
|
|
57
|
+
}
|
|
58
|
+
return 'word';
|
|
59
|
+
case 'xlsx':
|
|
60
|
+
return 'xlsx';
|
|
61
|
+
case 'text':
|
|
62
|
+
return 'text';
|
|
63
|
+
default:
|
|
64
|
+
throw new PreviewUnsupportedError("\u6682\u4E0D\u652F\u6301\u9884\u89C8\u8BE5\u6587\u4EF6\u7C7B\u578B".concat(extension ? "\uFF08.".concat(extension, "\uFF09") : '', "\uFF0C\u5F53\u524D\u652F\u6301\u6587\u672C\u3001PDF\u3001Word\uFF08docx\uFF09\u3001Excel\uFF08xlsx/xls/csv\uFF09"));
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** 解析来源并读取整个文件;远程文件支持自定义请求头 */
|
|
69
|
+
export var resolvePreviewSource = /*#__PURE__*/function () {
|
|
70
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
|
|
71
|
+
var bytes, _buffer, name, init, buffer, head, info, kind;
|
|
72
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
73
|
+
while (1) switch (_context.prev = _context.next) {
|
|
74
|
+
case 0:
|
|
75
|
+
if (!(typeof input === 'string' && !isFileUrl(input))) {
|
|
76
|
+
_context.next = 4;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
bytes = new TextEncoder().encode(input);
|
|
80
|
+
_buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
81
|
+
return _context.abrupt("return", {
|
|
82
|
+
kind: 'text',
|
|
83
|
+
category: 'text',
|
|
84
|
+
name: '',
|
|
85
|
+
extension: 'txt',
|
|
86
|
+
mime: 'text/plain',
|
|
87
|
+
buffer: _buffer
|
|
88
|
+
});
|
|
89
|
+
case 4:
|
|
90
|
+
name = resolveName(input);
|
|
91
|
+
init = isUrlSource(input) && input.headers ? {
|
|
92
|
+
headers: input.headers
|
|
93
|
+
} : undefined;
|
|
94
|
+
_context.next = 8;
|
|
95
|
+
return readFileAsArrayBuffer(toFileInput(input), init);
|
|
96
|
+
case 8:
|
|
97
|
+
buffer = _context.sent;
|
|
98
|
+
head = new Uint8Array(buffer, 0, Math.min(buffer.byteLength, 8192));
|
|
99
|
+
info = detectFileType(head, name);
|
|
100
|
+
kind = toPreviewKind(info.category, info.extension);
|
|
101
|
+
return _context.abrupt("return", {
|
|
102
|
+
kind: kind,
|
|
103
|
+
category: info.category,
|
|
104
|
+
name: name,
|
|
105
|
+
extension: info.extension,
|
|
106
|
+
mime: info.mime,
|
|
107
|
+
buffer: buffer
|
|
108
|
+
});
|
|
109
|
+
case 13:
|
|
110
|
+
case "end":
|
|
111
|
+
return _context.stop();
|
|
112
|
+
}
|
|
113
|
+
}, _callee);
|
|
114
|
+
}));
|
|
115
|
+
return function resolvePreviewSource(_x) {
|
|
116
|
+
return _ref.apply(this, arguments);
|
|
117
|
+
};
|
|
118
|
+
}();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 预览基础样式一次性注入。
|
|
3
|
+
*
|
|
4
|
+
* 不产出独立 css 文件,避免消费方还要额外引样式;所有 class 都带 `wa-preview` 前缀,
|
|
5
|
+
* 业务方可按需覆盖。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { PREVIEW_CLASS, PREVIEW_PREFIX } from "../constant";
|
|
9
|
+
var STYLE_ID = "".concat(PREVIEW_PREFIX, "-style");
|
|
10
|
+
var STYLES = "\n.".concat(PREVIEW_CLASS.root, " {\n position: relative;\n display: flex;\n flex-direction: column;\n width: 100%;\n height: 100%;\n min-height: 0;\n color: var(--wa-preview-color, #1f2329);\n background: var(--wa-preview-background, #f2f3f5);\n font-family: var(--wa-preview-font-family, -apple-system, BlinkMacSystemFont, \"PingFang SC\", \"Microsoft YaHei\", sans-serif);\n font-size: var(--wa-preview-font-size, 14px);\n line-height: var(--wa-preview-line-height, 1.7);\n -webkit-overflow-scrolling: touch;\n}\n.").concat(PREVIEW_CLASS.viewport, " {\n position: relative;\n flex: 1;\n min-height: 0;\n overflow: auto;\n overscroll-behavior: contain;\n}\n.").concat(PREVIEW_CLASS.page, " {\n position: relative;\n margin: 12px auto;\n background: var(--wa-preview-paper, #fff);\n box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);\n}\n.").concat(PREVIEW_CLASS.pageCanvas, " {\n display: block;\n width: 100%;\n height: 100%;\n}\n.").concat(PREVIEW_CLASS.pageLabel, " {\n position: absolute;\n right: 8px;\n bottom: 4px;\n color: var(--wa-preview-muted, #8f959e);\n font-size: 12px;\n line-height: 1;\n pointer-events: none;\n}\n.").concat(PREVIEW_CLASS.thumbnails, " {\n display: flex;\n gap: 8px;\n padding: 8px;\n overflow-x: auto;\n background: rgba(255, 255, 255, 0.86);\n border-top: 1px solid rgba(0, 0, 0, 0.06);\n}\n.").concat(PREVIEW_CLASS.thumbnail, " {\n position: relative;\n flex: 0 0 auto;\n padding: 2px;\n border: 1px solid transparent;\n border-radius: 4px;\n background: var(--wa-preview-paper, #fff);\n cursor: pointer;\n}\n.").concat(PREVIEW_CLASS.thumbnail, " canvas {\n display: block;\n width: 100%;\n height: auto;\n}\n.").concat(PREVIEW_CLASS.text, " {\n margin: 0;\n padding: 16px;\n color: var(--wa-preview-color, #1f2329);\n font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, \"Liberation Mono\", monospace;\n font-size: 13px;\n line-height: 1.7;\n white-space: pre;\n word-break: break-word;\n tab-size: 4;\n}\n.").concat(PREVIEW_CLASS.textChunk, " {\n min-height: 24px;\n}\n.").concat(PREVIEW_CLASS.textLine, " {\n display: block;\n min-height: 1.7em;\n white-space: pre-wrap;\n word-break: break-all;\n}\n.").concat(PREVIEW_CLASS.sheetTabs, " {\n display: flex;\n gap: 4px;\n padding: 6px 8px;\n overflow-x: auto;\n background: var(--wa-preview-paper, #fff);\n border-bottom: 1px solid rgba(0, 0, 0, 0.08);\n}\n.").concat(PREVIEW_CLASS.sheetTab, " {\n flex: 0 0 auto;\n padding: 4px 10px;\n border: 0;\n border-radius: 4px;\n background: transparent;\n color: var(--wa-preview-muted, #8f959e);\n font: inherit;\n font-size: 13px;\n cursor: pointer;\n}\n.").concat(PREVIEW_CLASS.sheetTabActive, " {\n color: var(--wa-preview-accent, #3370ff);\n background: rgba(51, 112, 255, 0.1);\n}\n.").concat(PREVIEW_CLASS.grid, " {\n position: relative;\n background: var(--wa-preview-paper, #fff);\n font-size: 13px;\n line-height: 1.4;\n}\n.").concat(PREVIEW_CLASS.gridHeader, " {\n position: sticky;\n top: 0;\n z-index: 2;\n display: flex;\n background: #f7f8fa;\n border-bottom: 1px solid rgba(0, 0, 0, 0.1);\n}\n.").concat(PREVIEW_CLASS.gridRow, " {\n display: flex;\n}\n.").concat(PREVIEW_CLASS.gridCell, " {\n position: absolute;\n box-sizing: border-box;\n padding: 2px 6px;\n overflow: hidden;\n white-space: pre-wrap;\n word-break: break-word;\n border-right: 1px solid rgba(0, 0, 0, 0.08);\n border-bottom: 1px solid rgba(0, 0, 0, 0.08);\n}\n.").concat(PREVIEW_CLASS.empty, " {\n display: flex;\n align-items: center;\n justify-content: center;\n min-height: 120px;\n color: var(--wa-preview-muted, #8f959e);\n font-size: 13px;\n}\n.").concat(PREVIEW_CLASS.error, " {\n margin: 24px;\n padding: 16px;\n color: #d83931;\n background: #fff1f0;\n border: 1px solid #ffccc7;\n border-radius: 6px;\n font-size: 13px;\n white-space: pre-wrap;\n}\n");
|
|
11
|
+
|
|
12
|
+
/** 保证样式只注入一次;SSR 环境下直接跳过 */
|
|
13
|
+
export var ensurePreviewStyles = function ensurePreviewStyles() {
|
|
14
|
+
if (typeof document === 'undefined') return;
|
|
15
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
16
|
+
var style = document.createElement('style');
|
|
17
|
+
style.id = STYLE_ID;
|
|
18
|
+
style.textContent = STYLES;
|
|
19
|
+
document.head.appendChild(style);
|
|
20
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @wapri/preview 公共入口。
|
|
3
|
+
*/
|
|
4
|
+
export * from './constant';
|
|
5
|
+
export * from './types/common';
|
|
6
|
+
export * from './types/text';
|
|
7
|
+
export * from './types/pdf';
|
|
8
|
+
export * from './types/word';
|
|
9
|
+
export * from './types/xlsx';
|
|
10
|
+
export { previewFile } from './previewFile';
|
|
11
|
+
export type { PreviewFileOptions } from './previewFile';
|
|
12
|
+
export { previewText } from './text';
|
|
13
|
+
export { previewPdf } from './pdf';
|
|
14
|
+
export { previewWord } from './word';
|
|
15
|
+
export { previewXlsx } from './xlsx';
|
|
16
|
+
export { configurePdfjs } from './pdf/load';
|
|
17
|
+
export { PreviewUnsupportedError, resolvePreviewSource } from './core/source';
|
|
18
|
+
export type { ResolvedSource } from './core/source';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @wapri/preview 公共入口。
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from "./constant";
|
|
6
|
+
export * from "./types/common";
|
|
7
|
+
export * from "./types/text";
|
|
8
|
+
export * from "./types/pdf";
|
|
9
|
+
export * from "./types/word";
|
|
10
|
+
export * from "./types/xlsx";
|
|
11
|
+
export { previewFile } from "./previewFile";
|
|
12
|
+
export { previewText } from "./text";
|
|
13
|
+
export { previewPdf } from "./pdf";
|
|
14
|
+
export { previewWord } from "./word";
|
|
15
|
+
export { previewXlsx } from "./xlsx";
|
|
16
|
+
export { configurePdfjs } from "./pdf/load";
|
|
17
|
+
export { PreviewUnsupportedError, resolvePreviewSource } from "./core/source";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PDF 预览:pdf.js 渲染到 canvas,按页进入视口才渲染,缩略图同样懒渲染。
|
|
3
|
+
*/
|
|
4
|
+
import type { ResolvedSource } from '../core/source';
|
|
5
|
+
import type { PreviewHandle, PreviewInput } from '../types/common';
|
|
6
|
+
import type { PdfPreviewOptions } from '../types/pdf';
|
|
7
|
+
export declare const renderPdf: (source: ResolvedSource, options: PdfPreviewOptions) => Promise<PreviewHandle<'pdf'>>;
|
|
8
|
+
export declare const previewPdf: (input: PreviewInput, options: PdfPreviewOptions) => Promise<PreviewHandle<'pdf'>>;
|
|
@@ -0,0 +1,364 @@
|
|
|
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
|
+
* PDF 预览:pdf.js 渲染到 canvas,按页进入视口才渲染,缩略图同样懒渲染。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PDF_THUMBNAIL_WIDTH, PREVIEW_CLASS } from "../constant";
|
|
10
|
+
import { createPreviewContext } from "../core/context";
|
|
11
|
+
import { LazyRenderPool } from "../core/lazy";
|
|
12
|
+
import { resolvePreviewSource } from "../core/source";
|
|
13
|
+
import { loadPdfJs, getPdfjsConfig } from "./load";
|
|
14
|
+
var applySlotSize = function applySlotSize(slot) {
|
|
15
|
+
slot.element.style.width = "".concat(Math.floor(slot.baseWidth * slot.scale), "px");
|
|
16
|
+
slot.element.style.height = "".concat(Math.floor(slot.baseHeight * slot.scale), "px");
|
|
17
|
+
};
|
|
18
|
+
var renderSlot = /*#__PURE__*/function () {
|
|
19
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(slot, tasks) {
|
|
20
|
+
var viewport, ratio, canvas, context2d, task, _slot$canvas;
|
|
21
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
22
|
+
while (1) switch (_context.prev = _context.next) {
|
|
23
|
+
case 0:
|
|
24
|
+
viewport = slot.page.getViewport({
|
|
25
|
+
scale: slot.scale
|
|
26
|
+
});
|
|
27
|
+
ratio = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
28
|
+
canvas = document.createElement('canvas');
|
|
29
|
+
canvas.className = PREVIEW_CLASS.pageCanvas;
|
|
30
|
+
canvas.width = Math.floor(viewport.width * ratio);
|
|
31
|
+
canvas.height = Math.floor(viewport.height * ratio);
|
|
32
|
+
canvas.style.width = "".concat(Math.floor(viewport.width), "px");
|
|
33
|
+
canvas.style.height = "".concat(Math.floor(viewport.height), "px");
|
|
34
|
+
context2d = canvas.getContext('2d');
|
|
35
|
+
if (context2d) {
|
|
36
|
+
_context.next = 11;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
throw new Error('[preview] 当前环境不支持 Canvas 2D');
|
|
40
|
+
case 11:
|
|
41
|
+
task = slot.page.render({
|
|
42
|
+
canvasContext: context2d,
|
|
43
|
+
viewport: viewport,
|
|
44
|
+
transform: ratio === 1 ? undefined : [ratio, 0, 0, ratio, 0, 0]
|
|
45
|
+
});
|
|
46
|
+
tasks.add(task);
|
|
47
|
+
_context.prev = 13;
|
|
48
|
+
_context.next = 16;
|
|
49
|
+
return task.promise;
|
|
50
|
+
case 16:
|
|
51
|
+
(_slot$canvas = slot.canvas) === null || _slot$canvas === void 0 || _slot$canvas.remove();
|
|
52
|
+
slot.canvas = canvas;
|
|
53
|
+
slot.element.appendChild(canvas);
|
|
54
|
+
_context.next = 27;
|
|
55
|
+
break;
|
|
56
|
+
case 21:
|
|
57
|
+
_context.prev = 21;
|
|
58
|
+
_context.t0 = _context["catch"](13);
|
|
59
|
+
canvas.remove();
|
|
60
|
+
// 取消渲染属于正常流程,不往上抛
|
|
61
|
+
if (!((_context.t0 === null || _context.t0 === void 0 ? void 0 : _context.t0.name) === 'RenderingCancelledException')) {
|
|
62
|
+
_context.next = 26;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
return _context.abrupt("return");
|
|
66
|
+
case 26:
|
|
67
|
+
throw _context.t0;
|
|
68
|
+
case 27:
|
|
69
|
+
_context.prev = 27;
|
|
70
|
+
tasks.delete(task);
|
|
71
|
+
return _context.finish(27);
|
|
72
|
+
case 30:
|
|
73
|
+
case "end":
|
|
74
|
+
return _context.stop();
|
|
75
|
+
}
|
|
76
|
+
}, _callee, null, [[13, 21, 27, 30]]);
|
|
77
|
+
}));
|
|
78
|
+
return function renderSlot(_x, _x2) {
|
|
79
|
+
return _ref.apply(this, arguments);
|
|
80
|
+
};
|
|
81
|
+
}();
|
|
82
|
+
export var renderPdf = /*#__PURE__*/function () {
|
|
83
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(source, options) {
|
|
84
|
+
var _options$pdfjs;
|
|
85
|
+
var context, pdfjs, _getPdfjsConfig, workerSrc, error, config, loadingTask, pdfDocument, _options$maxScale, _pages$, _options$scale, pageCount, pages, index, containerWidth, maxScale, firstViewport, autoScale, scale, tasks, slots, pool, handle;
|
|
86
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
87
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
88
|
+
case 0:
|
|
89
|
+
context = createPreviewContext(options);
|
|
90
|
+
_context2.next = 3;
|
|
91
|
+
return loadPdfJs(options.pdfjs);
|
|
92
|
+
case 3:
|
|
93
|
+
pdfjs = _context2.sent;
|
|
94
|
+
_getPdfjsConfig = getPdfjsConfig(), workerSrc = _getPdfjsConfig.workerSrc;
|
|
95
|
+
if (workerSrc) {
|
|
96
|
+
_context2.next = 9;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
error = new Error('[preview] 未找到 pdf.js worker:请调用 configurePdfjs({ workerSrc }) 指定可访问的 pdf.worker.min.mjs 地址');
|
|
100
|
+
context.fail(error);
|
|
101
|
+
throw error;
|
|
102
|
+
case 9:
|
|
103
|
+
config = (_options$pdfjs = options.pdfjs) !== null && _options$pdfjs !== void 0 ? _options$pdfjs : {};
|
|
104
|
+
loadingTask = pdfjs.getDocument({
|
|
105
|
+
data: new Uint8Array(source.buffer),
|
|
106
|
+
cMapUrl: config.cMapUrl,
|
|
107
|
+
cMapPacked: true,
|
|
108
|
+
standardFontDataUrl: config.standardFontDataUrl,
|
|
109
|
+
isEvalSupported: false
|
|
110
|
+
});
|
|
111
|
+
_context2.prev = 11;
|
|
112
|
+
context.emitProgress({
|
|
113
|
+
stage: 'parsing',
|
|
114
|
+
loaded: 0,
|
|
115
|
+
total: 0
|
|
116
|
+
});
|
|
117
|
+
_context2.next = 15;
|
|
118
|
+
return loadingTask.promise;
|
|
119
|
+
case 15:
|
|
120
|
+
pdfDocument = _context2.sent;
|
|
121
|
+
pageCount = pdfDocument.numPages;
|
|
122
|
+
pages = [];
|
|
123
|
+
index = 0;
|
|
124
|
+
case 19:
|
|
125
|
+
if (!(index < pageCount)) {
|
|
126
|
+
_context2.next = 28;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
_context2.t0 = pages;
|
|
130
|
+
_context2.next = 23;
|
|
131
|
+
return pdfDocument.getPage(index + 1);
|
|
132
|
+
case 23:
|
|
133
|
+
_context2.t1 = _context2.sent;
|
|
134
|
+
_context2.t0.push.call(_context2.t0, _context2.t1);
|
|
135
|
+
case 25:
|
|
136
|
+
index += 1;
|
|
137
|
+
_context2.next = 19;
|
|
138
|
+
break;
|
|
139
|
+
case 28:
|
|
140
|
+
containerWidth = context.viewport.clientWidth || context.container.clientWidth || 360;
|
|
141
|
+
maxScale = (_options$maxScale = options.maxScale) !== null && _options$maxScale !== void 0 ? _options$maxScale : 3;
|
|
142
|
+
firstViewport = (_pages$ = pages[0]) === null || _pages$ === void 0 ? void 0 : _pages$.getViewport({
|
|
143
|
+
scale: 1
|
|
144
|
+
});
|
|
145
|
+
autoScale = firstViewport ? Math.min(maxScale, Math.max(0.1, (containerWidth - 24) / firstViewport.width)) : 1;
|
|
146
|
+
scale = (_options$scale = options.scale) !== null && _options$scale !== void 0 ? _options$scale : autoScale;
|
|
147
|
+
tasks = new Set();
|
|
148
|
+
slots = pages.map(function (page, index) {
|
|
149
|
+
var size = page.getViewport({
|
|
150
|
+
scale: 1
|
|
151
|
+
});
|
|
152
|
+
var element = document.createElement('div');
|
|
153
|
+
element.className = PREVIEW_CLASS.page;
|
|
154
|
+
element.dataset.page = String(index + 1);
|
|
155
|
+
var label = document.createElement('span');
|
|
156
|
+
label.className = PREVIEW_CLASS.pageLabel;
|
|
157
|
+
label.textContent = "".concat(index + 1, " / ").concat(pageCount);
|
|
158
|
+
element.appendChild(label);
|
|
159
|
+
var slot = {
|
|
160
|
+
element: element,
|
|
161
|
+
page: page,
|
|
162
|
+
baseWidth: size.width,
|
|
163
|
+
baseHeight: size.height,
|
|
164
|
+
scale: scale
|
|
165
|
+
};
|
|
166
|
+
applySlotSize(slot);
|
|
167
|
+
context.viewport.appendChild(element);
|
|
168
|
+
return slot;
|
|
169
|
+
});
|
|
170
|
+
pool = new LazyRenderPool({
|
|
171
|
+
root: context.viewport,
|
|
172
|
+
keepAlive: true,
|
|
173
|
+
maxConcurrent: 1
|
|
174
|
+
});
|
|
175
|
+
context.onDestroy(function () {
|
|
176
|
+
pool.destroy();
|
|
177
|
+
tasks.forEach(function (task) {
|
|
178
|
+
return task.cancel();
|
|
179
|
+
});
|
|
180
|
+
tasks.clear();
|
|
181
|
+
void loadingTask.destroy();
|
|
182
|
+
});
|
|
183
|
+
slots.forEach(function (slot) {
|
|
184
|
+
pool.register({
|
|
185
|
+
element: slot.element,
|
|
186
|
+
render: function render() {
|
|
187
|
+
return renderSlot(slot, tasks);
|
|
188
|
+
},
|
|
189
|
+
release: function release() {
|
|
190
|
+
var _slot$canvas2;
|
|
191
|
+
(_slot$canvas2 = slot.canvas) === null || _slot$canvas2 === void 0 || _slot$canvas2.remove();
|
|
192
|
+
slot.canvas = undefined;
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
if (options.thumbnails) {
|
|
197
|
+
attachThumbnails(context, slots, tasks);
|
|
198
|
+
}
|
|
199
|
+
context.emitProgress({
|
|
200
|
+
stage: 'ready',
|
|
201
|
+
loaded: pageCount,
|
|
202
|
+
total: pageCount
|
|
203
|
+
});
|
|
204
|
+
_context2.t2 = context.container;
|
|
205
|
+
_context2.t3 = function renderAll() {
|
|
206
|
+
return pool.renderAll();
|
|
207
|
+
};
|
|
208
|
+
_context2.t4 = function scrollTo(index) {
|
|
209
|
+
var _slots$index;
|
|
210
|
+
(_slots$index = slots[index]) === null || _slots$index === void 0 || _slots$index.element.scrollIntoView({
|
|
211
|
+
block: 'start'
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
_context2.t5 = function refresh() {
|
|
215
|
+
var width = context.viewport.clientWidth || containerWidth;
|
|
216
|
+
if (!firstViewport || options.scale) return;
|
|
217
|
+
var nextScale = Math.min(maxScale, Math.max(0.1, (width - 24) / firstViewport.width));
|
|
218
|
+
slots.forEach(function (slot) {
|
|
219
|
+
slot.scale = nextScale;
|
|
220
|
+
applySlotSize(slot);
|
|
221
|
+
});
|
|
222
|
+
pool.reset();
|
|
223
|
+
};
|
|
224
|
+
_context2.t6 = function destroy() {
|
|
225
|
+
pool.destroy();
|
|
226
|
+
context.destroy();
|
|
227
|
+
};
|
|
228
|
+
handle = {
|
|
229
|
+
kind: 'pdf',
|
|
230
|
+
container: _context2.t2,
|
|
231
|
+
get unitCount() {
|
|
232
|
+
return slots.length;
|
|
233
|
+
},
|
|
234
|
+
get renderedCount() {
|
|
235
|
+
return pool.renderedCount;
|
|
236
|
+
},
|
|
237
|
+
renderAll: _context2.t3,
|
|
238
|
+
scrollTo: _context2.t4,
|
|
239
|
+
refresh: _context2.t5,
|
|
240
|
+
destroy: _context2.t6
|
|
241
|
+
};
|
|
242
|
+
return _context2.abrupt("return", handle);
|
|
243
|
+
case 49:
|
|
244
|
+
_context2.prev = 49;
|
|
245
|
+
_context2.t7 = _context2["catch"](11);
|
|
246
|
+
_context2.next = 53;
|
|
247
|
+
return loadingTask.destroy();
|
|
248
|
+
case 53:
|
|
249
|
+
context.fail(_context2.t7);
|
|
250
|
+
throw _context2.t7;
|
|
251
|
+
case 55:
|
|
252
|
+
case "end":
|
|
253
|
+
return _context2.stop();
|
|
254
|
+
}
|
|
255
|
+
}, _callee2, null, [[11, 49]]);
|
|
256
|
+
}));
|
|
257
|
+
return function renderPdf(_x3, _x4) {
|
|
258
|
+
return _ref2.apply(this, arguments);
|
|
259
|
+
};
|
|
260
|
+
}();
|
|
261
|
+
var attachThumbnails = function attachThumbnails(context, slots, tasks) {
|
|
262
|
+
var bar = document.createElement('div');
|
|
263
|
+
var thumbPool = new LazyRenderPool({
|
|
264
|
+
root: bar,
|
|
265
|
+
keepAlive: true,
|
|
266
|
+
maxConcurrent: 1
|
|
267
|
+
});
|
|
268
|
+
bar.className = PREVIEW_CLASS.thumbnails;
|
|
269
|
+
context.root.appendChild(bar);
|
|
270
|
+
context.onDestroy(function () {
|
|
271
|
+
return thumbPool.destroy();
|
|
272
|
+
});
|
|
273
|
+
slots.forEach(function (slot, index) {
|
|
274
|
+
var item = document.createElement('button');
|
|
275
|
+
var canvas = document.createElement('canvas');
|
|
276
|
+
item.type = 'button';
|
|
277
|
+
item.className = PREVIEW_CLASS.thumbnail;
|
|
278
|
+
item.title = "\u7B2C ".concat(index + 1, " \u9875");
|
|
279
|
+
canvas.width = PDF_THUMBNAIL_WIDTH;
|
|
280
|
+
canvas.height = Math.max(1, Math.round(PDF_THUMBNAIL_WIDTH * slot.baseHeight / slot.baseWidth));
|
|
281
|
+
item.appendChild(canvas);
|
|
282
|
+
bar.appendChild(item);
|
|
283
|
+
item.addEventListener('click', function () {
|
|
284
|
+
slot.element.scrollIntoView({
|
|
285
|
+
block: 'start'
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
thumbPool.register({
|
|
289
|
+
element: item,
|
|
290
|
+
render: function () {
|
|
291
|
+
var _render = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
292
|
+
var thumbScale, viewport, context2d, task;
|
|
293
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
294
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
295
|
+
case 0:
|
|
296
|
+
thumbScale = PDF_THUMBNAIL_WIDTH / slot.baseWidth;
|
|
297
|
+
viewport = slot.page.getViewport({
|
|
298
|
+
scale: thumbScale
|
|
299
|
+
});
|
|
300
|
+
context2d = canvas.getContext('2d');
|
|
301
|
+
if (context2d) {
|
|
302
|
+
_context3.next = 5;
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
return _context3.abrupt("return");
|
|
306
|
+
case 5:
|
|
307
|
+
canvas.width = Math.floor(viewport.width);
|
|
308
|
+
canvas.height = Math.floor(viewport.height);
|
|
309
|
+
task = slot.page.render({
|
|
310
|
+
canvasContext: context2d,
|
|
311
|
+
viewport: viewport
|
|
312
|
+
});
|
|
313
|
+
tasks.add(task);
|
|
314
|
+
_context3.prev = 9;
|
|
315
|
+
_context3.next = 12;
|
|
316
|
+
return task.promise;
|
|
317
|
+
case 12:
|
|
318
|
+
_context3.next = 17;
|
|
319
|
+
break;
|
|
320
|
+
case 14:
|
|
321
|
+
_context3.prev = 14;
|
|
322
|
+
_context3.t0 = _context3["catch"](9);
|
|
323
|
+
if ((_context3.t0 === null || _context3.t0 === void 0 ? void 0 : _context3.t0.name) !== 'RenderingCancelledException') {
|
|
324
|
+
console.warn('[preview] 缩略图渲染失败', _context3.t0);
|
|
325
|
+
}
|
|
326
|
+
case 17:
|
|
327
|
+
_context3.prev = 17;
|
|
328
|
+
tasks.delete(task);
|
|
329
|
+
return _context3.finish(17);
|
|
330
|
+
case 20:
|
|
331
|
+
case "end":
|
|
332
|
+
return _context3.stop();
|
|
333
|
+
}
|
|
334
|
+
}, _callee3, null, [[9, 14, 17, 20]]);
|
|
335
|
+
}));
|
|
336
|
+
function render() {
|
|
337
|
+
return _render.apply(this, arguments);
|
|
338
|
+
}
|
|
339
|
+
return render;
|
|
340
|
+
}()
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
};
|
|
344
|
+
export var previewPdf = /*#__PURE__*/function () {
|
|
345
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(input, options) {
|
|
346
|
+
var source;
|
|
347
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
348
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
349
|
+
case 0:
|
|
350
|
+
_context4.next = 2;
|
|
351
|
+
return resolvePreviewSource(input);
|
|
352
|
+
case 2:
|
|
353
|
+
source = _context4.sent;
|
|
354
|
+
return _context4.abrupt("return", renderPdf(source, options));
|
|
355
|
+
case 4:
|
|
356
|
+
case "end":
|
|
357
|
+
return _context4.stop();
|
|
358
|
+
}
|
|
359
|
+
}, _callee4);
|
|
360
|
+
}));
|
|
361
|
+
return function previewPdf(_x5, _x6) {
|
|
362
|
+
return _ref3.apply(this, arguments);
|
|
363
|
+
};
|
|
364
|
+
}();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pdf.js 运行时加载与资源定位。
|
|
3
|
+
*
|
|
4
|
+
* 用 legacy 构建,兼容更多 H5 WebView;worker 默认指向包内 `dist/vendor` 的副本,
|
|
5
|
+
* 业务方可用 `pdfjs.workerSrc` 覆盖成自己的 CDN。
|
|
6
|
+
*
|
|
7
|
+
* 注意:webpack 会把 `import.meta.url` 替换成构建机上的 `file://` 路径,浏览器无法 fetch,
|
|
8
|
+
* 因此这里只在 `http(s)` 协议下才采用包内 vendor,其余情况交给调用方显式配置。
|
|
9
|
+
*/
|
|
10
|
+
import type { PdfjsRuntimeConfig } from '../types/pdf';
|
|
11
|
+
export type PdfjsModule = typeof import('pdfjs-dist');
|
|
12
|
+
export declare const configurePdfjs: (config: PdfjsRuntimeConfig) => void;
|
|
13
|
+
export declare const getPdfjsConfig: () => PdfjsRuntimeConfig;
|
|
14
|
+
/** 按需加载 pdf.js,workerSrc 为空时保持 pdf.js 默认行为,并在调用侧给出明确提示 */
|
|
15
|
+
export declare const loadPdfJs: (config?: PdfjsRuntimeConfig) => Promise<PdfjsModule>;
|