@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,365 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
5
+ 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."); }
6
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
7
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
8
+ 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; } } }; }
9
+ 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); }
10
+ 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; }
11
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
13
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
14
+ 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; }
15
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
16
+ 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); }
17
+ /**
18
+ * Word 精简排版引擎:断行、表格布局、分页,产出可绘制到 Canvas 的盒模型。
19
+ *
20
+ * 字体度量来自 opentype.js(内嵌字体)或 Canvas measureText,两者都通过 TextMetricsProvider 抽象。
21
+ */
22
+
23
+ var HEADING_SCALE = {
24
+ 1: 2,
25
+ 2: 1.6,
26
+ 3: 1.4,
27
+ 4: 1.2,
28
+ 5: 1.1,
29
+ 6: 1
30
+ };
31
+ var isCjk = function isCjk(char) {
32
+ return /[\u2e80-\u9fff\uf900-\ufaff\uff00-\uffef]/.test(char);
33
+ };
34
+ var Pager = /*#__PURE__*/function () {
35
+ function Pager(config) {
36
+ _classCallCheck(this, Pager);
37
+ _defineProperty(this, "pages", []);
38
+ _defineProperty(this, "contentWidth", void 0);
39
+ _defineProperty(this, "y", void 0);
40
+ _defineProperty(this, "boxes", []);
41
+ this.config = config;
42
+ this.contentWidth = config.pageWidth - config.padding * 2;
43
+ this.y = config.padding;
44
+ }
45
+ _createClass(Pager, [{
46
+ key: "x",
47
+ get: function get() {
48
+ return this.config.padding;
49
+ }
50
+ }, {
51
+ key: "bottom",
52
+ get: function get() {
53
+ return this.config.pageHeight - this.config.padding;
54
+ }
55
+ }, {
56
+ key: "add",
57
+ value: function add(box) {
58
+ this.boxes.push(box);
59
+ }
60
+ }, {
61
+ key: "ensure",
62
+ value: function ensure(height) {
63
+ if (this.y + height <= this.bottom) return;
64
+ if (this.boxes.length === 0) return;
65
+ this.newPage();
66
+ }
67
+ }, {
68
+ key: "newPage",
69
+ value: function newPage() {
70
+ this.pages.push({
71
+ width: this.config.pageWidth,
72
+ height: this.config.pageHeight,
73
+ boxes: this.boxes
74
+ });
75
+ this.boxes = [];
76
+ this.y = this.config.padding;
77
+ }
78
+ }, {
79
+ key: "finish",
80
+ value: function finish() {
81
+ if (this.boxes.length > 0) {
82
+ this.pages.push({
83
+ width: this.config.pageWidth,
84
+ height: this.config.pageHeight,
85
+ boxes: this.boxes
86
+ });
87
+ this.boxes = [];
88
+ }
89
+ if (this.pages.length === 0) {
90
+ return [{
91
+ width: this.config.pageWidth,
92
+ height: this.config.pageHeight,
93
+ boxes: []
94
+ }];
95
+ }
96
+ return this.pages;
97
+ }
98
+ }]);
99
+ return Pager;
100
+ }();
101
+ var styleOf = function styleOf(run, baseSize) {
102
+ var _run$fontSize;
103
+ return {
104
+ fontSize: (_run$fontSize = run.fontSize) !== null && _run$fontSize !== void 0 ? _run$fontSize : baseSize,
105
+ bold: run.bold,
106
+ italic: run.italic
107
+ };
108
+ };
109
+ var wrapRuns = function wrapRuns(runs, maxWidth, baseSize, metrics) {
110
+ var chars = [];
111
+ var widthCache = new Map();
112
+ runs.forEach(function (run) {
113
+ var style = styleOf(run, baseSize);
114
+ var _iterator = _createForOfIteratorHelper(run.text),
115
+ _step;
116
+ try {
117
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
118
+ var char = _step.value;
119
+ chars.push({
120
+ char: char,
121
+ run: run,
122
+ style: style
123
+ });
124
+ }
125
+ } catch (err) {
126
+ _iterator.e(err);
127
+ } finally {
128
+ _iterator.f();
129
+ }
130
+ });
131
+ var charWidth = function charWidth(item) {
132
+ var key = "".concat(item.style.fontSize, "|").concat(item.style.bold ? 1 : 0, "|").concat(item.char);
133
+ var cached = widthCache.get(key);
134
+ if (cached !== undefined) return cached;
135
+ var width = metrics.measure(item.char, item.style);
136
+ widthCache.set(key, width);
137
+ return width;
138
+ };
139
+ var lines = [];
140
+ var current = [];
141
+ var width = 0;
142
+ var lastSpace = -1;
143
+ for (var _i = 0, _chars = chars; _i < _chars.length; _i++) {
144
+ var item = _chars[_i];
145
+ if (item.char === '\n') {
146
+ lines.push(current);
147
+ current = [];
148
+ width = 0;
149
+ lastSpace = -1;
150
+ continue;
151
+ }
152
+ var itemWidth = charWidth(item);
153
+ if (width + itemWidth > maxWidth && current.length > 0) {
154
+ if (lastSpace > 0 && !isCjk(item.char)) {
155
+ var head = current.slice(0, lastSpace);
156
+ var rest = current.slice(lastSpace + 1);
157
+ lines.push(head);
158
+ current = rest;
159
+ width = rest.reduce(function (sum, next) {
160
+ return sum + charWidth(next);
161
+ }, 0);
162
+ } else {
163
+ lines.push(current);
164
+ current = [];
165
+ width = 0;
166
+ }
167
+ lastSpace = -1;
168
+ }
169
+ current.push(item);
170
+ width += itemWidth;
171
+ if (item.char === ' ') lastSpace = current.length - 1;
172
+ }
173
+ if (current.length > 0) lines.push(current);
174
+ return lines.length > 0 ? lines : [[]];
175
+ };
176
+ var lineWidth = function lineWidth(line, metrics) {
177
+ return line.reduce(function (sum, item) {
178
+ return sum + metrics.measure(item.char, item.style);
179
+ }, 0);
180
+ };
181
+
182
+ /** 把一行按样式切分成多个 text box */
183
+ var pushLine = function pushLine(pager, line, x, maxWidth, lineHeight, metrics) {
184
+ var align = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 'left';
185
+ if (line.length === 0) return;
186
+ var total = lineWidth(line, metrics);
187
+ var cursor = x;
188
+ if (align === 'center') cursor += Math.max(0, (maxWidth - total) / 2);
189
+ if (align === 'right') cursor += Math.max(0, maxWidth - total);
190
+ var buffer = '';
191
+ var style = line[0].style;
192
+ var run = line[0].run;
193
+ var flush = function flush() {
194
+ if (!buffer) return;
195
+ var width = metrics.measure(buffer, style);
196
+ pager.add({
197
+ type: 'text',
198
+ x: cursor,
199
+ y: pager.y,
200
+ width: width,
201
+ height: lineHeight,
202
+ text: buffer,
203
+ fontSize: style.fontSize,
204
+ bold: style.bold,
205
+ italic: style.italic,
206
+ underline: run.underline
207
+ });
208
+ cursor += width;
209
+ buffer = '';
210
+ };
211
+ var _iterator2 = _createForOfIteratorHelper(line),
212
+ _step2;
213
+ try {
214
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
215
+ var item = _step2.value;
216
+ if (item.run !== run || item.style !== style) {
217
+ flush();
218
+ run = item.run;
219
+ style = item.style;
220
+ }
221
+ buffer += item.char;
222
+ }
223
+ } catch (err) {
224
+ _iterator2.e(err);
225
+ } finally {
226
+ _iterator2.f();
227
+ }
228
+ flush();
229
+ };
230
+ var layoutParagraph = function layoutParagraph(pager, block, config) {
231
+ var _HEADING_SCALE$block$, _block$listLevel;
232
+ var headingScale = block.heading ? (_HEADING_SCALE$block$ = HEADING_SCALE[block.heading]) !== null && _HEADING_SCALE$block$ !== void 0 ? _HEADING_SCALE$block$ : 1 : 1;
233
+ var fontSize = config.fontSize * headingScale;
234
+ var lineHeight = fontSize * (block.heading ? 1.35 : config.lineHeight);
235
+ var listLevel = (_block$listLevel = block.listLevel) !== null && _block$listLevel !== void 0 ? _block$listLevel : 0;
236
+ var indent = listLevel > 0 ? 18 * listLevel : 0;
237
+ var prefix = listLevel > 0 ? block.listIndex ? "".concat(block.listIndex, ". ") : '• ' : '';
238
+ var prefixWidth = prefix ? config.metrics.measure(prefix, {
239
+ fontSize: fontSize,
240
+ bold: Boolean(block.heading)
241
+ }) : 0;
242
+ var textWidth = pager.contentWidth - indent - prefixWidth;
243
+ if (block.heading) {
244
+ pager.y += fontSize * 0.4;
245
+ }
246
+ var lines = wrapRuns(block.runs, Math.max(40, textWidth), fontSize, config.metrics);
247
+ lines.forEach(function (line, index) {
248
+ var _block$align;
249
+ pager.ensure(lineHeight);
250
+ if (index === 0 && prefix) {
251
+ pager.add({
252
+ type: 'text',
253
+ x: pager.x + indent,
254
+ y: pager.y,
255
+ width: prefixWidth,
256
+ height: lineHeight,
257
+ text: prefix,
258
+ fontSize: fontSize,
259
+ bold: Boolean(block.heading)
260
+ });
261
+ }
262
+ pushLine(pager, line, pager.x + indent + prefixWidth, textWidth, lineHeight, config.metrics, (_block$align = block.align) !== null && _block$align !== void 0 ? _block$align : 'left');
263
+ pager.y += lineHeight;
264
+ });
265
+ pager.y += block.heading ? fontSize * 0.3 : 6;
266
+ };
267
+ var layoutTable = function layoutTable(pager, block, config) {
268
+ var colCount = Math.max.apply(Math, _toConsumableArray(block.rows.map(function (row) {
269
+ return row.length;
270
+ })).concat([1]));
271
+ var cellPadding = 6;
272
+ var fontSize = config.fontSize;
273
+ var lineHeight = fontSize * config.lineHeight;
274
+ var colWidth = pager.contentWidth / colCount;
275
+ var prepared = block.rows.map(function (row) {
276
+ var cells = Array.from({
277
+ length: colCount
278
+ }, function (_, index) {
279
+ var _row$index;
280
+ return (_row$index = row[index]) !== null && _row$index !== void 0 ? _row$index : {
281
+ runs: []
282
+ };
283
+ });
284
+ var wrapped = cells.map(function (cell) {
285
+ var runs = cell.header ? cell.runs.map(function (run) {
286
+ return _objectSpread(_objectSpread({}, run), {}, {
287
+ bold: true
288
+ });
289
+ }) : cell.runs;
290
+ return wrapRuns(runs, Math.max(20, colWidth - cellPadding * 2), fontSize, config.metrics);
291
+ });
292
+ var height = Math.max.apply(Math, [1].concat(_toConsumableArray(wrapped.map(function (lines) {
293
+ return lines.length;
294
+ })))) * lineHeight + cellPadding * 2;
295
+ return {
296
+ wrapped: wrapped,
297
+ height: height
298
+ };
299
+ });
300
+ prepared.forEach(function (row) {
301
+ pager.ensure(row.height);
302
+ var top = pager.y;
303
+ for (var index = 0; index < colCount; index += 1) {
304
+ pager.add({
305
+ type: 'rect',
306
+ x: pager.x + index * colWidth,
307
+ y: top,
308
+ width: colWidth,
309
+ height: row.height,
310
+ stroke: '#d0d5dd',
311
+ lineWidth: 1
312
+ });
313
+ }
314
+ row.wrapped.forEach(function (lines, columnIndex) {
315
+ var cellX = pager.x + columnIndex * colWidth + cellPadding;
316
+ pager.y = top + cellPadding;
317
+ lines.forEach(function (line) {
318
+ pushLine(pager, line, cellX, colWidth - cellPadding * 2, lineHeight, config.metrics);
319
+ pager.y += lineHeight;
320
+ });
321
+ });
322
+ pager.y = top + row.height;
323
+ });
324
+ pager.y += 8;
325
+ };
326
+ var layoutImage = function layoutImage(pager, block) {
327
+ var maxWidth = pager.contentWidth;
328
+ var scale = Math.min(1, maxWidth / Math.max(1, block.width));
329
+ var width = block.width * scale;
330
+ var height = block.height * scale;
331
+ pager.ensure(height);
332
+ pager.add({
333
+ type: 'image',
334
+ x: pager.x + Math.max(0, (maxWidth - width) / 2),
335
+ y: pager.y,
336
+ width: width,
337
+ height: height,
338
+ src: block.src
339
+ });
340
+ pager.y += height + 8;
341
+ };
342
+
343
+ /** 把文档模型排版成页面盒模型 */
344
+ export var layoutDocument = function layoutDocument(model, config) {
345
+ var pager = new Pager(config);
346
+ var _iterator3 = _createForOfIteratorHelper(model.blocks),
347
+ _step3;
348
+ try {
349
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
350
+ var block = _step3.value;
351
+ if (block.type === 'paragraph') {
352
+ layoutParagraph(pager, block, config);
353
+ } else if (block.type === 'table') {
354
+ layoutTable(pager, block, config);
355
+ } else {
356
+ layoutImage(pager, block);
357
+ }
358
+ }
359
+ } catch (err) {
360
+ _iterator3.e(err);
361
+ } finally {
362
+ _iterator3.f();
363
+ }
364
+ return pager.finish();
365
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Word(docx)解析:mammoth 转 HTML,再归一化成块模型。
3
+ */
4
+ import type { WordDocumentModel } from '../types/word';
5
+ /** 解析 docx 为块模型,图片尺寸在主线程预读 */
6
+ export declare const parseWord: (buffer: ArrayBuffer, fontSize?: number) => Promise<WordDocumentModel>;