office-viewer 0.1.3 → 0.1.5

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 (40) hide show
  1. package/README.md +1 -1
  2. package/esm/Word.js +16 -10
  3. package/esm/openxml/drawing/Drawing.d.ts +2 -0
  4. package/esm/openxml/drawing/Drawing.js +4 -0
  5. package/esm/openxml/drawing/Transform.d.ts +2 -0
  6. package/esm/openxml/drawing/Transform.js +41 -12
  7. package/esm/openxml/word/Tab.d.ts +1 -1
  8. package/esm/openxml/word/wps/WPG.d.ts +10 -1
  9. package/esm/openxml/word/wps/WPG.js +56 -0
  10. package/esm/openxml/word/wps/WPS.js +1 -0
  11. package/esm/render/renderDrawing.js +183 -71
  12. package/esm/render/renderParagraph.js +22 -0
  13. package/esm/render/renderRun.js +7 -2
  14. package/esm/render/renderStyle.js +1 -1
  15. package/esm/render/renderTab.d.ts +1 -1
  16. package/esm/render/renderTab.js +7 -1
  17. package/esm/util/get.d.ts +4 -0
  18. package/esm/util/get.js +18 -0
  19. package/esm/util/mergeRun.js +7 -1
  20. package/esm/util/replaceVar.js +2 -1
  21. package/lib/Word.js +16 -10
  22. package/lib/openxml/drawing/Drawing.d.ts +2 -0
  23. package/lib/openxml/drawing/Drawing.js +4 -0
  24. package/lib/openxml/drawing/Transform.d.ts +2 -0
  25. package/lib/openxml/drawing/Transform.js +41 -12
  26. package/lib/openxml/word/Tab.d.ts +1 -1
  27. package/lib/openxml/word/wps/WPG.d.ts +10 -1
  28. package/lib/openxml/word/wps/WPG.js +60 -0
  29. package/lib/openxml/word/wps/WPS.js +1 -0
  30. package/lib/render/renderDrawing.js +183 -71
  31. package/lib/render/renderParagraph.js +22 -0
  32. package/lib/render/renderRun.js +7 -2
  33. package/lib/render/renderStyle.js +1 -1
  34. package/lib/render/renderTab.d.ts +1 -1
  35. package/lib/render/renderTab.js +7 -1
  36. package/lib/util/get.d.ts +4 -0
  37. package/lib/util/get.js +22 -0
  38. package/lib/util/mergeRun.js +7 -1
  39. package/lib/util/replaceVar.js +2 -1
  40. package/package.json +2 -3
@@ -12,6 +12,7 @@ var renderHyperLink = require('./renderHyperLink.js');
12
12
  var renderBookmark = require('./renderBookmark.js');
13
13
  var renderNumbering = require('./renderNumbering.js');
14
14
  var setElementStyle = require('./setElementStyle.js');
15
+ var renderTab = require('./renderTab.js');
15
16
  var OMath = require('../openxml/math/OMath.js');
16
17
  var renderMath = require('./renderMath.js');
17
18
 
@@ -35,6 +36,27 @@ function renderParagraph(word, paragraph, renderEmptySpace, inHeader) {
35
36
  dom.appendChild(p, renderNumbering.renderNumbering(p, word, properties.numPr));
36
37
  }
37
38
  var inFldChar = false;
39
+ if (properties.tabs && properties.tabs.length) {
40
+ // 目前只支持渲染第一个,因为第二个位置取决于前面内容位置,挺麻烦
41
+ // 虽然目前这个实现很 hack,但可以支持常见情况
42
+ dom.appendChild(p, renderTab.renderTab(word, properties.tabs[0], true));
43
+ // 同时删掉第一个 run 中的 tab
44
+ // let done = false;
45
+ // for (const child of paragraph.children) {
46
+ // if (done) {
47
+ // break;
48
+ // }
49
+ // if (child instanceof Run) {
50
+ // for (const runChild of child.children) {
51
+ // if (runChild instanceof Tab) {
52
+ // child.children.splice(child.children.indexOf(runChild), 1);
53
+ // done = true;
54
+ // break;
55
+ // }
56
+ // }
57
+ // }
58
+ // }
59
+ }
38
60
  try {
39
61
  for (var _b = tslib.__values(paragraph.children), _c = _b.next(); !_c.done; _c = _b.next()) {
40
62
  var child = _c.value;
@@ -39,12 +39,14 @@ function renderText(span, word, text, paragraph) {
39
39
  var _a;
40
40
  // 简单过滤一下提升性能
41
41
  if (text.indexOf('{{') === -1) {
42
+ var finalText = void 0;
42
43
  if ((_a = paragraph === null || paragraph === void 0 ? void 0 : paragraph.properties) === null || _a === void 0 ? void 0 : _a.autoSpace) {
43
- span.textContent = autoSpace.cjkspace(text.split(''));
44
+ finalText = autoSpace.cjkspace(text.split(''));
44
45
  }
45
46
  else {
46
- span.textContent = text;
47
+ finalText = text;
47
48
  }
49
+ span.textContent = finalText;
48
50
  }
49
51
  else {
50
52
  span.dataset.originText = text;
@@ -52,6 +54,9 @@ function renderText(span, word, text, paragraph) {
52
54
  span.classList.add(VARIABLE_CLASS_NAME);
53
55
  span.textContent = word.replaceText(text);
54
56
  }
57
+ // 大于两个空格才转成 nbsp
58
+ var html = span.innerHTML.split(' ').join('  ');
59
+ span.innerHTML = html;
55
60
  }
56
61
  /**
57
62
  * 更新文档里的所有变量
@@ -25,7 +25,7 @@ function generateDefaultStyle(word) {
25
25
  }
26
26
  var hyphens = ((_a = word.settings) === null || _a === void 0 ? void 0 : _a.autoHyphenation) ? 'hyphens: auto;' : '';
27
27
  var classPrefix = word.getClassPrefix();
28
- return "\n\n\n /** docDefaults **/\n .".concat(classPrefix, " {\n --docx-theme-font-minorHAnsi: Calibri, Helvetica, Arial, 'Helvetica Neue';\n --docx-theme-font-minorEastAsia: 'PingFang SC', 'Microsoft YaHei', 'Hiragino Sans GB', 'STHeiti',\n 'Microsoft YaHei';\n }\n\n .").concat(classPrefix, " p {\n margin: 0;\n padding: 0;\n line-height: 1.5;\n ").concat(hyphens, "\n }\n\n .").concat(classPrefix, " .justify:after {\n content: \"\";\n display: inline-block;\n width: 100%;\n }\n\n .").concat(classPrefix, " table {\n border-spacing: 0;\n }\n\n .").concat(classPrefix, " .").concat(classPrefix, "-p {\n ").concat(defaultPStyle, "\n }\n\n .").concat(classPrefix, " .").concat(classPrefix, "-r {\n white-space: pre-wrap;\n overflow-wrap: break-word;\n ").concat(defaultRStyle, "\n }\n ");
28
+ return "\n\n\n /** docDefaults **/\n .".concat(classPrefix, " {\n --docx-theme-font-minorHAnsi: Calibri, Helvetica, Arial, 'Helvetica Neue';\n --docx-theme-font-minorEastAsia: 'PingFang SC', 'Microsoft YaHei', 'Hiragino Sans GB', 'STHeiti',\n 'Microsoft YaHei';\n }\n\n .").concat(classPrefix, " p {\n margin: 0;\n padding: 0;\n line-height: 1.5;\n ").concat(hyphens, "\n }\n\n .").concat(classPrefix, " .justify:after {\n content: \"\";\n display: inline-block;\n width: 100%;\n }\n\n .").concat(classPrefix, " table {\n border-spacing: 0;\n }\n\n .").concat(classPrefix, " .").concat(classPrefix, "-p {\n ").concat(defaultPStyle, "\n }\n\n .").concat(classPrefix, " .").concat(classPrefix, "-r {\n overflow-wrap: break-word;\n ").concat(defaultRStyle, "\n }\n ");
29
29
  }
30
30
  /**
31
31
  * 生成表格级别样式
@@ -5,4 +5,4 @@ import Word from '../Word';
5
5
  * 不支持 tabs 里的自定义宽度,因为要算渲染后的宽度,比较麻烦
6
6
  * http://officeopenxml.com/WPtab.php
7
7
  */
8
- export declare function renderTab(word: Word, tab: Tab): HTMLElement;
8
+ export declare function renderTab(word: Word, tab: Tab, renderWidth?: boolean): HTMLElement;
@@ -9,12 +9,18 @@ var dom = require('../util/dom.js');
9
9
  * 不支持 tabs 里的自定义宽度,因为要算渲染后的宽度,比较麻烦
10
10
  * http://officeopenxml.com/WPtab.php
11
11
  */
12
- function renderTab(word, tab) {
12
+ function renderTab(word, tab, renderWidth) {
13
+ if (renderWidth === void 0) { renderWidth = false; }
13
14
  var tabElement = dom.createElement('span');
15
+ tabElement.style.display = 'inline-block';
16
+ tabElement.style.width = '2em';
14
17
  tabElement.innerHTML = ' ';
15
18
  if (tab.leader === 'dot') {
16
19
  tabElement.style.borderBottom = '1pt dotted';
17
20
  }
21
+ if (renderWidth && tab.pos && (tab.type === 'start' || tab.type == 'left')) {
22
+ tabElement.style.width = tab.pos;
23
+ }
18
24
  return tabElement;
19
25
  }
20
26
 
@@ -0,0 +1,4 @@
1
+ /**
2
+ * https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get
3
+ */
4
+ export declare const get: (obj: any, path: string, defaultValue?: undefined) => any;
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get
7
+ */
8
+ var get = function (obj, path, defaultValue) {
9
+ if (defaultValue === void 0) { defaultValue = undefined; }
10
+ var travel = function (regexp) {
11
+ return String.prototype.split
12
+ .call(path, regexp)
13
+ .filter(Boolean)
14
+ .reduce(function (res, key) {
15
+ return res !== null && res !== undefined ? res[key] : res;
16
+ }, obj);
17
+ };
18
+ var result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
19
+ return result === undefined || result === obj ? defaultValue : result;
20
+ };
21
+
22
+ exports.get = get;
@@ -39,7 +39,13 @@ function canMerge(element) {
39
39
  if (childChild.tagName === 'w:t') {
40
40
  hasText = true;
41
41
  textHasSpace = childChild.getAttribute('xml:space') === 'preserve';
42
- break;
42
+ if (textHasSpace) {
43
+ break;
44
+ }
45
+ }
46
+ // 有 tab 的情况下不能合并
47
+ if (childChild.tagName === 'w:tab') {
48
+ return false;
43
49
  }
44
50
  }
45
51
  }
@@ -28,6 +28,7 @@ function replaceText(word, text, data) {
28
28
  return String(result);
29
29
  }
30
30
  else {
31
+ console.warn('var error: [', text, '] not found in data');
31
32
  return '';
32
33
  }
33
34
  }
@@ -129,7 +130,7 @@ function replaceTableRow(word, tr) {
129
130
  }
130
131
  finally { if (e_5) throw e_5.error; }
131
132
  }
132
- table.appendChild(newTr);
133
+ table.insertBefore(newTr, tr);
133
134
  }
134
135
  }
135
136
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "office-viewer",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "office 文档在线预览",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -65,7 +65,6 @@
65
65
  },
66
66
  "jest": {
67
67
  "testEnvironment": "jsdom",
68
- "collectCoverage": true,
69
68
  "coverageReporters": [
70
69
  "text",
71
70
  "cobertura"
@@ -99,4 +98,4 @@
99
98
  "printBasicPrototype": false
100
99
  }
101
100
  }
102
- }
101
+ }