@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.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -2
  3. package/dist/constant/index.d.ts +51 -0
  4. package/dist/constant/index.js +58 -0
  5. package/dist/core/context.d.ts +21 -0
  6. package/dist/core/context.js +78 -0
  7. package/dist/core/lazy.d.ts +43 -0
  8. package/dist/core/lazy.js +175 -0
  9. package/dist/core/source.d.ts +20 -0
  10. package/dist/core/source.js +118 -0
  11. package/dist/core/style.d.ts +8 -0
  12. package/dist/core/style.js +20 -0
  13. package/dist/index.d.ts +18 -0
  14. package/dist/index.js +17 -0
  15. package/dist/pdf/index.d.ts +8 -0
  16. package/dist/pdf/index.js +364 -0
  17. package/dist/pdf/load.d.ts +15 -0
  18. package/dist/pdf/load.js +78 -0
  19. package/dist/previewFile.d.ts +18 -0
  20. package/dist/previewFile.js +54 -0
  21. package/dist/text/index.d.ts +8 -0
  22. package/dist/text/index.js +118 -0
  23. package/dist/types/common.d.ts +68 -0
  24. package/dist/types/common.js +1 -0
  25. package/dist/types/pdf.d.ts +23 -0
  26. package/dist/types/pdf.js +1 -0
  27. package/dist/types/text.d.ts +14 -0
  28. package/dist/types/text.js +1 -0
  29. package/dist/types/word.d.ts +57 -0
  30. package/dist/types/word.js +1 -0
  31. package/dist/types/xlsx.d.ts +45 -0
  32. package/dist/types/xlsx.js +1 -0
  33. package/dist/vendor/pdf.worker.min.mjs +21 -0
  34. package/dist/vendor/xlsx.full.min.js +22 -0
  35. package/dist/word/font.d.ts +20 -0
  36. package/dist/word/font.js +132 -0
  37. package/dist/word/index.d.ts +8 -0
  38. package/dist/word/index.js +237 -0
  39. package/dist/word/layout.d.ts +56 -0
  40. package/dist/word/layout.js +365 -0
  41. package/dist/word/parse.d.ts +6 -0
  42. package/dist/word/parse.js +315 -0
  43. package/dist/word/render.d.ts +17 -0
  44. package/dist/word/render.js +210 -0
  45. package/dist/xlsx/index.d.ts +8 -0
  46. package/dist/xlsx/index.js +195 -0
  47. package/dist/xlsx/parse.d.ts +14 -0
  48. package/dist/xlsx/parse.js +177 -0
  49. package/dist/xlsx/render.d.ts +17 -0
  50. package/dist/xlsx/render.js +144 -0
  51. package/dist/xlsx/workerSource.d.ts +7 -0
  52. package/dist/xlsx/workerSource.js +8 -0
  53. package/package.json +28 -3
@@ -0,0 +1,315 @@
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
4
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
5
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
7
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
8
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
9
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
10
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
11
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
12
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
14
+ 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); } }
15
+ 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); }); }; }
16
+ 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; }
17
+ 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; }
18
+ 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; }
19
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
20
+ 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); }
21
+ /**
22
+ * Word(docx)解析:mammoth 转 HTML,再归一化成块模型。
23
+ */
24
+
25
+ import mammoth from 'mammoth';
26
+ var HEADING_TAGS = new Set(['H1', 'H2', 'H3', 'H4', 'H5', 'H6']);
27
+ var collectRuns = function collectRuns(node) {
28
+ var style = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
29
+ if (node.nodeType === Node.TEXT_NODE) {
30
+ var _node$textContent;
31
+ var text = (_node$textContent = node.textContent) !== null && _node$textContent !== void 0 ? _node$textContent : '';
32
+ return text ? [_objectSpread({
33
+ text: text
34
+ }, style)] : [];
35
+ }
36
+ if (!(node instanceof HTMLElement)) return [];
37
+ if (node.tagName === 'BR') {
38
+ return [_objectSpread({
39
+ text: '\n'
40
+ }, style)];
41
+ }
42
+ var next = _objectSpread({}, style);
43
+ if (node.tagName === 'STRONG' || node.tagName === 'B') next.bold = true;
44
+ if (node.tagName === 'EM' || node.tagName === 'I') next.italic = true;
45
+ if (node.tagName === 'U') next.underline = true;
46
+ return Array.from(node.childNodes).flatMap(function (child) {
47
+ return collectRuns(child, next);
48
+ });
49
+ };
50
+ var loadImageSize = function loadImageSize(src) {
51
+ return new Promise(function (resolve) {
52
+ var image = new Image();
53
+ image.onload = function () {
54
+ return resolve({
55
+ width: image.naturalWidth || 320,
56
+ height: image.naturalHeight || 240
57
+ });
58
+ };
59
+ image.onerror = function () {
60
+ return resolve({
61
+ width: 320,
62
+ height: 240
63
+ });
64
+ };
65
+ image.src = src;
66
+ });
67
+ };
68
+ var toBlocks = /*#__PURE__*/function () {
69
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(element) {
70
+ var tag, image, size, runs, _blocks, items, _iterator, _step, _step$value, index, item, inlineRuns, _i, _Array$from, child, nested, rows, source, _size, children, blocks, _i2, _children, _child;
71
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
72
+ while (1) switch (_context.prev = _context.next) {
73
+ case 0:
74
+ tag = element.tagName;
75
+ if (!HEADING_TAGS.has(tag)) {
76
+ _context.next = 3;
77
+ break;
78
+ }
79
+ return _context.abrupt("return", [{
80
+ type: 'paragraph',
81
+ runs: collectRuns(element),
82
+ heading: Number(tag.slice(1))
83
+ }]);
84
+ case 3:
85
+ if (!(tag === 'P' || tag === 'DIV')) {
86
+ _context.next = 12;
87
+ break;
88
+ }
89
+ image = element.querySelector('img');
90
+ if (!(image !== null && image !== void 0 && image.src)) {
91
+ _context.next = 10;
92
+ break;
93
+ }
94
+ _context.next = 8;
95
+ return loadImageSize(image.src);
96
+ case 8:
97
+ size = _context.sent;
98
+ return _context.abrupt("return", [{
99
+ type: 'image',
100
+ src: image.src,
101
+ width: size.width,
102
+ height: size.height
103
+ }]);
104
+ case 10:
105
+ runs = collectRuns(element);
106
+ return _context.abrupt("return", runs.length > 0 ? [{
107
+ type: 'paragraph',
108
+ runs: runs
109
+ }] : []);
110
+ case 12:
111
+ if (!(tag === 'UL' || tag === 'OL')) {
112
+ _context.next = 45;
113
+ break;
114
+ }
115
+ _blocks = [];
116
+ items = Array.from(element.children).filter(function (child) {
117
+ return child.tagName === 'LI';
118
+ });
119
+ _iterator = _createForOfIteratorHelper(items.entries());
120
+ _context.prev = 16;
121
+ _iterator.s();
122
+ case 18:
123
+ if ((_step = _iterator.n()).done) {
124
+ _context.next = 36;
125
+ break;
126
+ }
127
+ _step$value = _slicedToArray(_step.value, 2), index = _step$value[0], item = _step$value[1];
128
+ inlineRuns = Array.from(item.childNodes).filter(function (child) {
129
+ return !(child instanceof HTMLElement && (child.tagName === 'UL' || child.tagName === 'OL'));
130
+ }).flatMap(function (child) {
131
+ return collectRuns(child);
132
+ });
133
+ if (inlineRuns.length > 0) {
134
+ _blocks.push({
135
+ type: 'paragraph',
136
+ runs: inlineRuns,
137
+ listLevel: 1,
138
+ listIndex: tag === 'OL' ? index + 1 : 0
139
+ });
140
+ }
141
+ _i = 0, _Array$from = Array.from(item.children);
142
+ case 23:
143
+ if (!(_i < _Array$from.length)) {
144
+ _context.next = 34;
145
+ break;
146
+ }
147
+ child = _Array$from[_i];
148
+ if (!(child.tagName !== 'UL' && child.tagName !== 'OL')) {
149
+ _context.next = 27;
150
+ break;
151
+ }
152
+ return _context.abrupt("continue", 31);
153
+ case 27:
154
+ _context.next = 29;
155
+ return toBlocks(child);
156
+ case 29:
157
+ nested = _context.sent;
158
+ _blocks.push.apply(_blocks, _toConsumableArray(nested.map(function (block) {
159
+ return block.type === 'paragraph' ? _objectSpread(_objectSpread({}, block), {}, {
160
+ listLevel: 2
161
+ }) : block;
162
+ })));
163
+ case 31:
164
+ _i++;
165
+ _context.next = 23;
166
+ break;
167
+ case 34:
168
+ _context.next = 18;
169
+ break;
170
+ case 36:
171
+ _context.next = 41;
172
+ break;
173
+ case 38:
174
+ _context.prev = 38;
175
+ _context.t0 = _context["catch"](16);
176
+ _iterator.e(_context.t0);
177
+ case 41:
178
+ _context.prev = 41;
179
+ _iterator.f();
180
+ return _context.finish(41);
181
+ case 44:
182
+ return _context.abrupt("return", _blocks);
183
+ case 45:
184
+ if (!(tag === 'TABLE')) {
185
+ _context.next = 48;
186
+ break;
187
+ }
188
+ rows = Array.from(element.querySelectorAll('tr')).map(function (row) {
189
+ return Array.from(row.querySelectorAll('th,td')).map(function (cell) {
190
+ return {
191
+ runs: collectRuns(cell),
192
+ header: cell.tagName === 'TH'
193
+ };
194
+ });
195
+ });
196
+ return _context.abrupt("return", rows.length > 0 ? [{
197
+ type: 'table',
198
+ rows: rows
199
+ }] : []);
200
+ case 48:
201
+ if (!(tag === 'IMG')) {
202
+ _context.next = 56;
203
+ break;
204
+ }
205
+ source = element.src;
206
+ if (source) {
207
+ _context.next = 52;
208
+ break;
209
+ }
210
+ return _context.abrupt("return", []);
211
+ case 52:
212
+ _context.next = 54;
213
+ return loadImageSize(source);
214
+ case 54:
215
+ _size = _context.sent;
216
+ return _context.abrupt("return", [{
217
+ type: 'image',
218
+ src: source,
219
+ width: _size.width,
220
+ height: _size.height
221
+ }]);
222
+ case 56:
223
+ children = Array.from(element.children);
224
+ blocks = [];
225
+ _i2 = 0, _children = children;
226
+ case 59:
227
+ if (!(_i2 < _children.length)) {
228
+ _context.next = 72;
229
+ break;
230
+ }
231
+ _child = _children[_i2];
232
+ _context.t1 = blocks.push;
233
+ _context.t2 = blocks;
234
+ _context.t3 = _toConsumableArray;
235
+ _context.next = 66;
236
+ return toBlocks(_child);
237
+ case 66:
238
+ _context.t4 = _context.sent;
239
+ _context.t5 = (0, _context.t3)(_context.t4);
240
+ _context.t1.apply.call(_context.t1, _context.t2, _context.t5);
241
+ case 69:
242
+ _i2++;
243
+ _context.next = 59;
244
+ break;
245
+ case 72:
246
+ return _context.abrupt("return", blocks);
247
+ case 73:
248
+ case "end":
249
+ return _context.stop();
250
+ }
251
+ }, _callee, null, [[16, 38, 41, 44]]);
252
+ }));
253
+ return function toBlocks(_x) {
254
+ return _ref.apply(this, arguments);
255
+ };
256
+ }();
257
+
258
+ /** 解析 docx 为块模型,图片尺寸在主线程预读 */
259
+ export var parseWord = /*#__PURE__*/function () {
260
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(buffer) {
261
+ var fontSize,
262
+ result,
263
+ document,
264
+ blocks,
265
+ _i3,
266
+ _Array$from2,
267
+ child,
268
+ _args2 = arguments;
269
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
270
+ while (1) switch (_context2.prev = _context2.next) {
271
+ case 0:
272
+ fontSize = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : 16;
273
+ _context2.next = 3;
274
+ return mammoth.convertToHtml({
275
+ arrayBuffer: buffer
276
+ });
277
+ case 3:
278
+ result = _context2.sent;
279
+ document = new DOMParser().parseFromString(result.value, 'text/html');
280
+ blocks = [];
281
+ _i3 = 0, _Array$from2 = Array.from(document.body.children);
282
+ case 7:
283
+ if (!(_i3 < _Array$from2.length)) {
284
+ _context2.next = 20;
285
+ break;
286
+ }
287
+ child = _Array$from2[_i3];
288
+ _context2.t0 = blocks.push;
289
+ _context2.t1 = blocks;
290
+ _context2.t2 = _toConsumableArray;
291
+ _context2.next = 14;
292
+ return toBlocks(child);
293
+ case 14:
294
+ _context2.t3 = _context2.sent;
295
+ _context2.t4 = (0, _context2.t2)(_context2.t3);
296
+ _context2.t0.apply.call(_context2.t0, _context2.t1, _context2.t4);
297
+ case 17:
298
+ _i3++;
299
+ _context2.next = 7;
300
+ break;
301
+ case 20:
302
+ return _context2.abrupt("return", {
303
+ blocks: blocks,
304
+ fontSize: fontSize
305
+ });
306
+ case 21:
307
+ case "end":
308
+ return _context2.stop();
309
+ }
310
+ }, _callee2);
311
+ }));
312
+ return function parseWord(_x2) {
313
+ return _ref2.apply(this, arguments);
314
+ };
315
+ }();
@@ -0,0 +1,17 @@
1
+ /**
2
+ * 把排版后的页面盒模型绘制到 Canvas。
3
+ */
4
+ import type { LayoutPage } from './layout';
5
+ import type { TextMetricsProvider } from './font';
6
+ export interface PaintOptions {
7
+ pixelRatio: number;
8
+ paper: string;
9
+ color: string;
10
+ metrics: TextMetricsProvider;
11
+ }
12
+ /** 在已设置好坐标系与背景的 ctx 上绘制一页 */
13
+ export declare const paintPage: (page: LayoutPage, ctx: CanvasRenderingContext2D, options: PaintOptions) => Promise<void>;
14
+ /** 渲染到指定 canvas,按设备像素比缩放 */
15
+ export declare const renderPageToCanvas: (page: LayoutPage, canvas: HTMLCanvasElement, options: PaintOptions) => Promise<void>;
16
+ /** 生成缩略图,按目标宽度等比绘制 */
17
+ export declare const renderThumbnail: (page: LayoutPage, canvas: HTMLCanvasElement, options: PaintOptions, targetWidth: number) => Promise<void>;
@@ -0,0 +1,210 @@
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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function _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; }
6
+ 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); } }
7
+ 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); }); }; }
8
+ /**
9
+ * 把排版后的页面盒模型绘制到 Canvas。
10
+ */
11
+
12
+ var imageCache = new Map();
13
+ var loadImage = function loadImage(src) {
14
+ var cached = imageCache.get(src);
15
+ if (cached) return cached;
16
+ var promise = new Promise(function (resolve, reject) {
17
+ var image = new Image();
18
+ image.onload = function () {
19
+ return resolve(image);
20
+ };
21
+ image.onerror = function () {
22
+ return reject(new Error('[preview] 图片加载失败'));
23
+ };
24
+ image.src = src;
25
+ });
26
+ imageCache.set(src, promise);
27
+ return promise;
28
+ };
29
+ var drawText = function drawText(ctx, box, options) {
30
+ var _box$color, _box$color2;
31
+ var baseline = box.y + box.fontSize * 0.82;
32
+ ctx.fillStyle = (_box$color = box.color) !== null && _box$color !== void 0 ? _box$color : options.color;
33
+ options.metrics.draw(ctx, box.text, box.x, baseline, {
34
+ fontSize: box.fontSize,
35
+ bold: box.bold,
36
+ italic: box.italic
37
+ });
38
+ if (!box.underline) return;
39
+ ctx.strokeStyle = (_box$color2 = box.color) !== null && _box$color2 !== void 0 ? _box$color2 : options.color;
40
+ ctx.lineWidth = 1;
41
+ ctx.beginPath();
42
+ ctx.moveTo(box.x, baseline + 2);
43
+ ctx.lineTo(box.x + box.width, baseline + 2);
44
+ ctx.stroke();
45
+ };
46
+ var drawRect = function drawRect(ctx, box) {
47
+ if (box.fill) {
48
+ ctx.fillStyle = box.fill;
49
+ ctx.fillRect(box.x, box.y, box.width, box.height);
50
+ }
51
+ if (box.stroke) {
52
+ var _box$lineWidth;
53
+ ctx.strokeStyle = box.stroke;
54
+ ctx.lineWidth = (_box$lineWidth = box.lineWidth) !== null && _box$lineWidth !== void 0 ? _box$lineWidth : 1;
55
+ ctx.strokeRect(box.x + 0.5, box.y + 0.5, Math.max(0, box.width - 1), Math.max(0, box.height - 1));
56
+ }
57
+ };
58
+ var drawImage = /*#__PURE__*/function () {
59
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ctx, box) {
60
+ var image;
61
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
62
+ while (1) switch (_context.prev = _context.next) {
63
+ case 0:
64
+ _context.prev = 0;
65
+ _context.next = 3;
66
+ return loadImage(box.src);
67
+ case 3:
68
+ image = _context.sent;
69
+ ctx.drawImage(image, box.x, box.y, box.width, box.height);
70
+ _context.next = 10;
71
+ break;
72
+ case 7:
73
+ _context.prev = 7;
74
+ _context.t0 = _context["catch"](0);
75
+ console.warn('[preview] Word 图片绘制失败', _context.t0);
76
+ case 10:
77
+ case "end":
78
+ return _context.stop();
79
+ }
80
+ }, _callee, null, [[0, 7]]);
81
+ }));
82
+ return function drawImage(_x, _x2) {
83
+ return _ref.apply(this, arguments);
84
+ };
85
+ }();
86
+
87
+ /** 在已设置好坐标系与背景的 ctx 上绘制一页 */
88
+ export var paintPage = /*#__PURE__*/function () {
89
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(page, ctx, options) {
90
+ var _iterator, _step, box;
91
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
92
+ while (1) switch (_context2.prev = _context2.next) {
93
+ case 0:
94
+ ctx.fillStyle = options.paper;
95
+ ctx.fillRect(0, 0, page.width, page.height);
96
+ _iterator = _createForOfIteratorHelper(page.boxes);
97
+ _context2.prev = 3;
98
+ _iterator.s();
99
+ case 5:
100
+ if ((_step = _iterator.n()).done) {
101
+ _context2.next = 19;
102
+ break;
103
+ }
104
+ box = _step.value;
105
+ if (!(box.type === 'text')) {
106
+ _context2.next = 11;
107
+ break;
108
+ }
109
+ drawText(ctx, box, options);
110
+ _context2.next = 17;
111
+ break;
112
+ case 11:
113
+ if (!(box.type === 'rect')) {
114
+ _context2.next = 15;
115
+ break;
116
+ }
117
+ drawRect(ctx, box);
118
+ _context2.next = 17;
119
+ break;
120
+ case 15:
121
+ _context2.next = 17;
122
+ return drawImage(ctx, box);
123
+ case 17:
124
+ _context2.next = 5;
125
+ break;
126
+ case 19:
127
+ _context2.next = 24;
128
+ break;
129
+ case 21:
130
+ _context2.prev = 21;
131
+ _context2.t0 = _context2["catch"](3);
132
+ _iterator.e(_context2.t0);
133
+ case 24:
134
+ _context2.prev = 24;
135
+ _iterator.f();
136
+ return _context2.finish(24);
137
+ case 27:
138
+ case "end":
139
+ return _context2.stop();
140
+ }
141
+ }, _callee2, null, [[3, 21, 24, 27]]);
142
+ }));
143
+ return function paintPage(_x3, _x4, _x5) {
144
+ return _ref2.apply(this, arguments);
145
+ };
146
+ }();
147
+
148
+ /** 渲染到指定 canvas,按设备像素比缩放 */
149
+ export var renderPageToCanvas = /*#__PURE__*/function () {
150
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(page, canvas, options) {
151
+ var ratio, ctx;
152
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
153
+ while (1) switch (_context3.prev = _context3.next) {
154
+ case 0:
155
+ ratio = Math.max(1, Math.min(options.pixelRatio, 2));
156
+ canvas.width = Math.floor(page.width * ratio);
157
+ canvas.height = Math.floor(page.height * ratio);
158
+ canvas.style.width = "".concat(page.width, "px");
159
+ canvas.style.height = "".concat(page.height, "px");
160
+ ctx = canvas.getContext('2d');
161
+ if (ctx) {
162
+ _context3.next = 8;
163
+ break;
164
+ }
165
+ return _context3.abrupt("return");
166
+ case 8:
167
+ ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
168
+ _context3.next = 11;
169
+ return paintPage(page, ctx, options);
170
+ case 11:
171
+ case "end":
172
+ return _context3.stop();
173
+ }
174
+ }, _callee3);
175
+ }));
176
+ return function renderPageToCanvas(_x6, _x7, _x8) {
177
+ return _ref3.apply(this, arguments);
178
+ };
179
+ }();
180
+
181
+ /** 生成缩略图,按目标宽度等比绘制 */
182
+ export var renderThumbnail = /*#__PURE__*/function () {
183
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(page, canvas, options, targetWidth) {
184
+ var scale, ctx;
185
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
186
+ while (1) switch (_context4.prev = _context4.next) {
187
+ case 0:
188
+ scale = targetWidth / page.width;
189
+ canvas.width = Math.floor(page.width * scale);
190
+ canvas.height = Math.floor(page.height * scale);
191
+ ctx = canvas.getContext('2d');
192
+ if (ctx) {
193
+ _context4.next = 6;
194
+ break;
195
+ }
196
+ return _context4.abrupt("return");
197
+ case 6:
198
+ ctx.setTransform(scale, 0, 0, scale, 0, 0);
199
+ _context4.next = 9;
200
+ return paintPage(page, ctx, options);
201
+ case 9:
202
+ case "end":
203
+ return _context4.stop();
204
+ }
205
+ }, _callee4);
206
+ }));
207
+ return function renderThumbnail(_x9, _x10, _x11, _x12) {
208
+ return _ref4.apply(this, arguments);
209
+ };
210
+ }();
@@ -0,0 +1,8 @@
1
+ /**
2
+ * XLSX 预览:解析后渲染虚拟表格,多 sheet 用底部/顶部标签切换。
3
+ */
4
+ import type { ResolvedSource } from '../core/source';
5
+ import type { PreviewHandle, PreviewInput } from '../types/common';
6
+ import type { XlsxPreviewOptions } from '../types/xlsx';
7
+ export declare const renderXlsx: (source: ResolvedSource, options: XlsxPreviewOptions) => Promise<PreviewHandle<'xlsx'>>;
8
+ export declare const previewXlsx: (input: PreviewInput, options: XlsxPreviewOptions) => Promise<PreviewHandle<'xlsx'>>;