@wapri/preview 0.0.0-stage → 0.0.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
package/dist/pdf/load.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
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; }
|
|
6
|
+
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; }
|
|
7
|
+
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; }
|
|
8
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
9
|
+
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); }
|
|
10
|
+
/**
|
|
11
|
+
* pdf.js 运行时加载与资源定位。
|
|
12
|
+
*
|
|
13
|
+
* 用 legacy 构建,兼容更多 H5 WebView;worker 默认指向包内 `dist/vendor` 的副本,
|
|
14
|
+
* 业务方可用 `pdfjs.workerSrc` 覆盖成自己的 CDN。
|
|
15
|
+
*
|
|
16
|
+
* 注意:webpack 会把 `import.meta.url` 替换成构建机上的 `file://` 路径,浏览器无法 fetch,
|
|
17
|
+
* 因此这里只在 `http(s)` 协议下才采用包内 vendor,其余情况交给调用方显式配置。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
var runtimeConfig = {};
|
|
21
|
+
var modulePromise;
|
|
22
|
+
|
|
23
|
+
/** 用字符串拼接避免构建器把包内 vendor 路径当成静态资源去解析 */
|
|
24
|
+
var resolveVendorUrl = function resolveVendorUrl(file) {
|
|
25
|
+
try {
|
|
26
|
+
if (typeof import.meta === 'undefined' || !import.meta.url) return undefined;
|
|
27
|
+
var base = import.meta.url;
|
|
28
|
+
if (!/^https?:\/\//i.test(base)) return undefined;
|
|
29
|
+
var dir = base.slice(0, base.lastIndexOf('/'));
|
|
30
|
+
return "".concat(dir, "/../vendor/").concat(file);
|
|
31
|
+
} catch (_unused) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export var configurePdfjs = function configurePdfjs(config) {
|
|
36
|
+
runtimeConfig = _objectSpread(_objectSpread({}, runtimeConfig), config);
|
|
37
|
+
};
|
|
38
|
+
export var getPdfjsConfig = function getPdfjsConfig() {
|
|
39
|
+
var _runtimeConfig$worker;
|
|
40
|
+
return _objectSpread(_objectSpread({}, runtimeConfig), {}, {
|
|
41
|
+
workerSrc: (_runtimeConfig$worker = runtimeConfig.workerSrc) !== null && _runtimeConfig$worker !== void 0 ? _runtimeConfig$worker : resolveVendorUrl('pdf.worker.min.mjs')
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** 按需加载 pdf.js,workerSrc 为空时保持 pdf.js 默认行为,并在调用侧给出明确提示 */
|
|
46
|
+
export var loadPdfJs = /*#__PURE__*/function () {
|
|
47
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
48
|
+
var _modulePromise;
|
|
49
|
+
var config,
|
|
50
|
+
pdfjs,
|
|
51
|
+
_getPdfjsConfig,
|
|
52
|
+
workerSrc,
|
|
53
|
+
_args = arguments;
|
|
54
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
55
|
+
while (1) switch (_context.prev = _context.next) {
|
|
56
|
+
case 0:
|
|
57
|
+
config = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
|
|
58
|
+
configurePdfjs(config);
|
|
59
|
+
(_modulePromise = modulePromise) !== null && _modulePromise !== void 0 ? _modulePromise : modulePromise = import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
60
|
+
_context.next = 5;
|
|
61
|
+
return modulePromise;
|
|
62
|
+
case 5:
|
|
63
|
+
pdfjs = _context.sent;
|
|
64
|
+
_getPdfjsConfig = getPdfjsConfig(), workerSrc = _getPdfjsConfig.workerSrc;
|
|
65
|
+
if (workerSrc) {
|
|
66
|
+
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
|
|
67
|
+
}
|
|
68
|
+
return _context.abrupt("return", pdfjs);
|
|
69
|
+
case 9:
|
|
70
|
+
case "end":
|
|
71
|
+
return _context.stop();
|
|
72
|
+
}
|
|
73
|
+
}, _callee);
|
|
74
|
+
}));
|
|
75
|
+
return function loadPdfJs() {
|
|
76
|
+
return _ref.apply(this, arguments);
|
|
77
|
+
};
|
|
78
|
+
}();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一入口:识别文件类型后分发到具体预览实现。
|
|
3
|
+
*/
|
|
4
|
+
import type { BasePreviewOptions, PreviewHandle, PreviewInput } from './types/common';
|
|
5
|
+
import type { PdfPreviewOptions } from './types/pdf';
|
|
6
|
+
import type { TextPreviewOptions } from './types/text';
|
|
7
|
+
import type { WordPreviewOptions } from './types/word';
|
|
8
|
+
import type { XlsxPreviewOptions } from './types/xlsx';
|
|
9
|
+
type Extra<T extends BasePreviewOptions> = Omit<T, keyof BasePreviewOptions>;
|
|
10
|
+
export interface PreviewFileOptions extends BasePreviewOptions {
|
|
11
|
+
text?: Extra<TextPreviewOptions>;
|
|
12
|
+
pdf?: Extra<PdfPreviewOptions>;
|
|
13
|
+
word?: Extra<WordPreviewOptions>;
|
|
14
|
+
xlsx?: Extra<XlsxPreviewOptions>;
|
|
15
|
+
}
|
|
16
|
+
/** 自动识别文件类型并选择预览器 */
|
|
17
|
+
export declare const previewFile: (input: PreviewInput, options: PreviewFileOptions) => Promise<PreviewHandle>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
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; }
|
|
6
|
+
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; }
|
|
7
|
+
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; }
|
|
8
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
9
|
+
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); }
|
|
10
|
+
/**
|
|
11
|
+
* 统一入口:识别文件类型后分发到具体预览实现。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { resolvePreviewSource } from "./core/source";
|
|
15
|
+
import { renderPdf } from "./pdf";
|
|
16
|
+
import { renderText } from "./text";
|
|
17
|
+
import { renderWord } from "./word";
|
|
18
|
+
import { renderXlsx } from "./xlsx";
|
|
19
|
+
var mergeOptions = function mergeOptions(base, extra) {
|
|
20
|
+
return _objectSpread(_objectSpread({}, base), extra);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** 自动识别文件类型并选择预览器 */
|
|
24
|
+
export var previewFile = /*#__PURE__*/function () {
|
|
25
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input, options) {
|
|
26
|
+
var source;
|
|
27
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
28
|
+
while (1) switch (_context.prev = _context.next) {
|
|
29
|
+
case 0:
|
|
30
|
+
_context.next = 2;
|
|
31
|
+
return resolvePreviewSource(input);
|
|
32
|
+
case 2:
|
|
33
|
+
source = _context.sent;
|
|
34
|
+
_context.t0 = source.kind;
|
|
35
|
+
_context.next = _context.t0 === 'pdf' ? 6 : _context.t0 === 'word' ? 7 : _context.t0 === 'xlsx' ? 8 : _context.t0 === 'text' ? 9 : 9;
|
|
36
|
+
break;
|
|
37
|
+
case 6:
|
|
38
|
+
return _context.abrupt("return", renderPdf(source, mergeOptions(options, options.pdf)));
|
|
39
|
+
case 7:
|
|
40
|
+
return _context.abrupt("return", renderWord(source, mergeOptions(options, options.word)));
|
|
41
|
+
case 8:
|
|
42
|
+
return _context.abrupt("return", renderXlsx(source, mergeOptions(options, options.xlsx)));
|
|
43
|
+
case 9:
|
|
44
|
+
return _context.abrupt("return", renderText(source, mergeOptions(options, options.text)));
|
|
45
|
+
case 10:
|
|
46
|
+
case "end":
|
|
47
|
+
return _context.stop();
|
|
48
|
+
}
|
|
49
|
+
}, _callee);
|
|
50
|
+
}));
|
|
51
|
+
return function previewFile(_x, _x2) {
|
|
52
|
+
return _ref.apply(this, arguments);
|
|
53
|
+
};
|
|
54
|
+
}();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文本预览:按行分块,进入视口才填充内容。
|
|
3
|
+
*/
|
|
4
|
+
import type { ResolvedSource } from '../core/source';
|
|
5
|
+
import type { PreviewHandle, PreviewInput } from '../types/common';
|
|
6
|
+
import type { TextPreviewOptions } from '../types/text';
|
|
7
|
+
export declare const renderText: (source: ResolvedSource, options: TextPreviewOptions) => PreviewHandle<'text'>;
|
|
8
|
+
export declare const previewText: (input: PreviewInput, options: TextPreviewOptions) => Promise<PreviewHandle<'text'>>;
|
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PREVIEW_CLASS, TEXT_CHUNK_LINES } from "../constant";
|
|
10
|
+
import { createPreviewContext } from "../core/context";
|
|
11
|
+
import { LazyRenderPool } from "../core/lazy";
|
|
12
|
+
import { resolvePreviewSource } from "../core/source";
|
|
13
|
+
export var renderText = function renderText(source, options) {
|
|
14
|
+
var _options$chunkLines;
|
|
15
|
+
var context = createPreviewContext(options);
|
|
16
|
+
var chunkLines = Math.max(1, (_options$chunkLines = options.chunkLines) !== null && _options$chunkLines !== void 0 ? _options$chunkLines : TEXT_CHUNK_LINES);
|
|
17
|
+
var text = '';
|
|
18
|
+
try {
|
|
19
|
+
var _options$encoding;
|
|
20
|
+
text = new TextDecoder((_options$encoding = options.encoding) !== null && _options$encoding !== void 0 ? _options$encoding : 'utf-8').decode(source.buffer);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
context.fail(error);
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
var lines = text.split(/\r\n|\r|\n/);
|
|
26
|
+
var chunks = [];
|
|
27
|
+
for (var index = 0; index < lines.length; index += chunkLines) {
|
|
28
|
+
chunks.push(lines.slice(index, index + chunkLines));
|
|
29
|
+
}
|
|
30
|
+
var lineHeight = Math.round(context.theme.fontSize * Math.max(1, context.theme.lineHeight));
|
|
31
|
+
var list = document.createElement('div');
|
|
32
|
+
list.className = PREVIEW_CLASS.text;
|
|
33
|
+
list.style.whiteSpace = options.wrap === false ? 'pre' : 'pre-wrap';
|
|
34
|
+
if (options.monoFontFamily) list.style.fontFamily = options.monoFontFamily;
|
|
35
|
+
var blocks = [];
|
|
36
|
+
var pool = new LazyRenderPool({
|
|
37
|
+
root: context.viewport,
|
|
38
|
+
keepAlive: true
|
|
39
|
+
});
|
|
40
|
+
context.onDestroy(function () {
|
|
41
|
+
return pool.destroy();
|
|
42
|
+
});
|
|
43
|
+
context.emitProgress({
|
|
44
|
+
stage: 'parsing',
|
|
45
|
+
loaded: 0,
|
|
46
|
+
total: chunks.length
|
|
47
|
+
});
|
|
48
|
+
chunks.forEach(function (chunk, index) {
|
|
49
|
+
var block = document.createElement('div');
|
|
50
|
+
block.className = PREVIEW_CLASS.textChunk;
|
|
51
|
+
block.dataset.index = String(index);
|
|
52
|
+
// 先按行数占位,保证滚动条高度稳定
|
|
53
|
+
block.style.minHeight = "".concat(chunk.length * lineHeight, "px");
|
|
54
|
+
list.appendChild(block);
|
|
55
|
+
blocks.push(block);
|
|
56
|
+
var content = chunk.join('\n');
|
|
57
|
+
pool.register({
|
|
58
|
+
element: block,
|
|
59
|
+
render: function render() {
|
|
60
|
+
block.textContent = content;
|
|
61
|
+
block.style.minHeight = '';
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
context.viewport.appendChild(list);
|
|
66
|
+
context.emitProgress({
|
|
67
|
+
stage: 'ready',
|
|
68
|
+
loaded: chunks.length,
|
|
69
|
+
total: chunks.length
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
kind: 'text',
|
|
73
|
+
container: context.container,
|
|
74
|
+
get unitCount() {
|
|
75
|
+
return chunks.length;
|
|
76
|
+
},
|
|
77
|
+
get renderedCount() {
|
|
78
|
+
return pool.renderedCount;
|
|
79
|
+
},
|
|
80
|
+
renderAll: function renderAll() {
|
|
81
|
+
return pool.renderAll();
|
|
82
|
+
},
|
|
83
|
+
scrollTo: function scrollTo(index) {
|
|
84
|
+
var _blocks$index;
|
|
85
|
+
(_blocks$index = blocks[index]) === null || _blocks$index === void 0 || _blocks$index.scrollIntoView({
|
|
86
|
+
block: 'start'
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
refresh: function refresh() {
|
|
90
|
+
// 文本没有画布,尺寸变化交给浏览器重排
|
|
91
|
+
},
|
|
92
|
+
destroy: function destroy() {
|
|
93
|
+
pool.destroy();
|
|
94
|
+
context.destroy();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
export var previewText = /*#__PURE__*/function () {
|
|
99
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(input, options) {
|
|
100
|
+
var source;
|
|
101
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
102
|
+
while (1) switch (_context.prev = _context.next) {
|
|
103
|
+
case 0:
|
|
104
|
+
_context.next = 2;
|
|
105
|
+
return resolvePreviewSource(input);
|
|
106
|
+
case 2:
|
|
107
|
+
source = _context.sent;
|
|
108
|
+
return _context.abrupt("return", renderText(source, options));
|
|
109
|
+
case 4:
|
|
110
|
+
case "end":
|
|
111
|
+
return _context.stop();
|
|
112
|
+
}
|
|
113
|
+
}, _callee);
|
|
114
|
+
}));
|
|
115
|
+
return function previewText(_x, _x2) {
|
|
116
|
+
return _ref.apply(this, arguments);
|
|
117
|
+
};
|
|
118
|
+
}();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 预览包的公共类型:输入来源、统一句柄与进度。
|
|
3
|
+
*/
|
|
4
|
+
export type PreviewKind = 'text' | 'pdf' | 'word' | 'xlsx';
|
|
5
|
+
/** 需要额外请求头的远程文件 */
|
|
6
|
+
export interface PreviewUrlSource {
|
|
7
|
+
url: string;
|
|
8
|
+
/** 用于识别类型与展示的名称,远程地址推导不出来时传 */
|
|
9
|
+
fileName?: string;
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
export type PreviewInput = File | Blob | ArrayBuffer | ArrayBufferView | string | PreviewUrlSource;
|
|
13
|
+
export interface PreviewProgress {
|
|
14
|
+
/** 当前阶段 */
|
|
15
|
+
stage: 'loading' | 'parsing' | 'layout' | 'rendering' | 'ready';
|
|
16
|
+
/** 已完成数量,总量未知时为 0 */
|
|
17
|
+
loaded: number;
|
|
18
|
+
/** 总量,未知时为 0 */
|
|
19
|
+
total: number;
|
|
20
|
+
message?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface PreviewTheme {
|
|
23
|
+
/** 正文颜色 */
|
|
24
|
+
color?: string;
|
|
25
|
+
/** 次要文本 / 边框颜色 */
|
|
26
|
+
mutedColor?: string;
|
|
27
|
+
/** 页面背景 */
|
|
28
|
+
background?: string;
|
|
29
|
+
/** 纸张背景 */
|
|
30
|
+
paper?: string;
|
|
31
|
+
/** 强调色,用于链接、选中 sheet */
|
|
32
|
+
accent?: string;
|
|
33
|
+
/** 正文字体族 */
|
|
34
|
+
fontFamily?: string;
|
|
35
|
+
/** 正文字号,px */
|
|
36
|
+
fontSize?: number;
|
|
37
|
+
/** 行高倍数 */
|
|
38
|
+
lineHeight?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface BasePreviewOptions {
|
|
41
|
+
/** 挂载容器或选择器 */
|
|
42
|
+
container: HTMLElement | string;
|
|
43
|
+
/** 追加到根节点的 class */
|
|
44
|
+
className?: string;
|
|
45
|
+
/** 是否开启可视区域懒渲染,默认 true */
|
|
46
|
+
lazy?: boolean;
|
|
47
|
+
/** 提前渲染的屏幕数,默认 1 */
|
|
48
|
+
overscan?: number;
|
|
49
|
+
/** 主题覆盖 */
|
|
50
|
+
theme?: PreviewTheme;
|
|
51
|
+
onProgress?: (progress: PreviewProgress) => void;
|
|
52
|
+
}
|
|
53
|
+
export interface PreviewHandle<K extends PreviewKind = PreviewKind> {
|
|
54
|
+
readonly kind: K;
|
|
55
|
+
readonly container: HTMLElement;
|
|
56
|
+
/** 总单元数:文本为分块数,PDF / Word 为页数,XLSX 为行数 */
|
|
57
|
+
readonly unitCount: number;
|
|
58
|
+
/** 已经渲染的单元数 */
|
|
59
|
+
readonly renderedCount: number;
|
|
60
|
+
/** 立即渲染全部内容,用于打印或导出 */
|
|
61
|
+
renderAll(): void;
|
|
62
|
+
/** 滚动到指定单元 */
|
|
63
|
+
scrollTo(index: number): void;
|
|
64
|
+
/** 重新计算容器尺寸并重排 */
|
|
65
|
+
refresh(): void;
|
|
66
|
+
/** 释放 canvas、worker、objectURL */
|
|
67
|
+
destroy(): void;
|
|
68
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PDF 预览选项。
|
|
3
|
+
*/
|
|
4
|
+
import type { BasePreviewOptions } from './common';
|
|
5
|
+
/** pdf.js 运行资源,业务方按部署路径注入,包内不写死 CDN */
|
|
6
|
+
export interface PdfjsRuntimeConfig {
|
|
7
|
+
/** `pdf.worker.min.mjs` 地址;默认取包内 vendor 副本 */
|
|
8
|
+
workerSrc?: string;
|
|
9
|
+
/** CMap 目录,预览中文 PDF 建议配置 */
|
|
10
|
+
cMapUrl?: string;
|
|
11
|
+
/** 标准字体目录 */
|
|
12
|
+
standardFontDataUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface PdfPreviewOptions extends BasePreviewOptions {
|
|
15
|
+
/** 固定缩放,未设置时按容器宽度自适应 */
|
|
16
|
+
scale?: number;
|
|
17
|
+
/** 自适应缩放上限,默认 3 */
|
|
18
|
+
maxScale?: number;
|
|
19
|
+
/** 是否展示底部缩略图条,默认 false */
|
|
20
|
+
thumbnails?: boolean;
|
|
21
|
+
/** pdf.js 资源配置 */
|
|
22
|
+
pdfjs?: PdfjsRuntimeConfig;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文本预览选项。
|
|
3
|
+
*/
|
|
4
|
+
import type { BasePreviewOptions } from './common';
|
|
5
|
+
export interface TextPreviewOptions extends BasePreviewOptions {
|
|
6
|
+
/** 是否自动换行,默认 true */
|
|
7
|
+
wrap?: boolean;
|
|
8
|
+
/** 文本编码,默认 utf-8 */
|
|
9
|
+
encoding?: string;
|
|
10
|
+
/** 每个懒渲染块包含的行数,默认 80 */
|
|
11
|
+
chunkLines?: number;
|
|
12
|
+
/** 代码字体族覆盖 */
|
|
13
|
+
monoFontFamily?: string;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Word 预览选项与文档模型。
|
|
3
|
+
*/
|
|
4
|
+
import type { BasePreviewOptions } from './common';
|
|
5
|
+
export interface WordTextRun {
|
|
6
|
+
text: string;
|
|
7
|
+
bold?: boolean;
|
|
8
|
+
italic?: boolean;
|
|
9
|
+
underline?: boolean;
|
|
10
|
+
/** 字号覆盖,未设置时继承块级设置 */
|
|
11
|
+
fontSize?: number;
|
|
12
|
+
}
|
|
13
|
+
export type WordBlock = {
|
|
14
|
+
type: 'paragraph';
|
|
15
|
+
runs: WordTextRun[];
|
|
16
|
+
/** 1 - 6,0 表示普通段落 */
|
|
17
|
+
heading?: number;
|
|
18
|
+
/** 列表层级,0 表示不在列表里 */
|
|
19
|
+
listLevel?: number;
|
|
20
|
+
/** 有序列表序号 */
|
|
21
|
+
listIndex?: number;
|
|
22
|
+
align?: 'left' | 'center' | 'right' | 'justify';
|
|
23
|
+
} | {
|
|
24
|
+
type: 'table';
|
|
25
|
+
rows: WordTableCell[][];
|
|
26
|
+
} | {
|
|
27
|
+
type: 'image';
|
|
28
|
+
src: string;
|
|
29
|
+
width: number;
|
|
30
|
+
height: number;
|
|
31
|
+
};
|
|
32
|
+
export interface WordTableCell {
|
|
33
|
+
runs: WordTextRun[];
|
|
34
|
+
/** 表头行用粗体渲染 */
|
|
35
|
+
header?: boolean;
|
|
36
|
+
}
|
|
37
|
+
export interface WordDocumentModel {
|
|
38
|
+
blocks: WordBlock[];
|
|
39
|
+
/** 默认字号,px */
|
|
40
|
+
fontSize: number;
|
|
41
|
+
}
|
|
42
|
+
export interface WordPreviewOptions extends BasePreviewOptions {
|
|
43
|
+
/** 页面宽度 px,默认 A4 96dpi */
|
|
44
|
+
pageWidth?: number;
|
|
45
|
+
/** 页面高度 px */
|
|
46
|
+
pageHeight?: number;
|
|
47
|
+
/** 页面内边距 px */
|
|
48
|
+
padding?: number;
|
|
49
|
+
/** 内嵌字体(TTF / OTF / WOFF 的 ArrayBuffer),用于精确测量与绘制 */
|
|
50
|
+
fonts?: ArrayBuffer[];
|
|
51
|
+
/** 字号覆盖 */
|
|
52
|
+
fontSize?: number;
|
|
53
|
+
/** 是否展示缩略图条,默认 false */
|
|
54
|
+
thumbnails?: boolean;
|
|
55
|
+
/** 每页渲染的最大设备像素比,默认 2 */
|
|
56
|
+
maxPixelRatio?: number;
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* XLSX 预览选项与解析后的模型。
|
|
3
|
+
*/
|
|
4
|
+
import type { BasePreviewOptions } from './common';
|
|
5
|
+
export interface XlsxMerge {
|
|
6
|
+
s: {
|
|
7
|
+
r: number;
|
|
8
|
+
c: number;
|
|
9
|
+
};
|
|
10
|
+
e: {
|
|
11
|
+
r: number;
|
|
12
|
+
c: number;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export interface XlsxSheet {
|
|
16
|
+
name: string;
|
|
17
|
+
/** 二维文本矩阵,未解析到的单元格为空串 */
|
|
18
|
+
rows: string[][];
|
|
19
|
+
rowCount: number;
|
|
20
|
+
colCount: number;
|
|
21
|
+
merges: XlsxMerge[];
|
|
22
|
+
/** 列宽 px,0 表示使用默认值 */
|
|
23
|
+
colWidths: number[];
|
|
24
|
+
/** 行高 px,0 表示使用默认值 */
|
|
25
|
+
rowHeights: number[];
|
|
26
|
+
/** 单元格数量超过上限被截断 */
|
|
27
|
+
truncated: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface XlsxWorkbook {
|
|
30
|
+
sheets: XlsxSheet[];
|
|
31
|
+
}
|
|
32
|
+
export interface XlsxPreviewOptions extends BasePreviewOptions {
|
|
33
|
+
/** 初始 sheet 名称或下标 */
|
|
34
|
+
sheet?: string | number;
|
|
35
|
+
/** 默认列宽 px */
|
|
36
|
+
colWidth?: number;
|
|
37
|
+
/** 默认行高 px */
|
|
38
|
+
rowHeight?: number;
|
|
39
|
+
/** 单表最大单元格数,默认 20 万 */
|
|
40
|
+
maxCells?: number;
|
|
41
|
+
/** 是否使用 worker 解析,默认 true */
|
|
42
|
+
worker?: boolean;
|
|
43
|
+
/** SheetJS UMD 地址;默认取包内 vendor 副本 */
|
|
44
|
+
workerScript?: string;
|
|
45
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|