From 3941b21e7ec818389d3490969e029c83bde23f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=A4=9A=E7=9B=8A?= Date: Thu, 23 Mar 2023 20:53:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20office-view=20=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=86=85=E5=B5=8C=E5=AD=97=E4=BD=93=EF=BC=9B=E6=95=B0=E5=AD=A6?= =?UTF-8?q?=E5=85=AC=E5=BC=8F=EF=BC=9B=E5=88=9D=E6=AD=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20textbox=EF=BC=9B=E4=BF=AE=E5=A4=8D=20highlight=20=E5=92=8C?= =?UTF-8?q?=E5=8A=A0=E7=B2=97=E6=B8=B2=E6=9F=93=E4=B8=8D=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#6459)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 初步支持 textbox * 开始实现内嵌字体 * 支持内嵌字体 * 避免 style 导致可能的 xss * 支持公式渲染 * 修复 highlight 和加粗渲染不正确问题 * 删掉不用的文件 * 修复编译报错 --- .dockerignore | 1 + docs/zh-CN/components/office-viewer.md | 45 +- packages/office-viewer/README.md | 7 +- .../office-viewer/__tests__/OpenXML.test.ts | 2 +- .../__tests__/docx/dedocx/README.md | 3 + .../docx/dedocx/broken/corrupt-zip.docx | Bin 0 -> 74992 bytes .../__tests__/docx/dedocx/broken/doc.docx | Bin 0 -> 46080 bytes .../docx/dedocx/broken/empty.docx} | 0 .../__tests__/docx/dedocx/broken/excel.docx | Bin 0 -> 16196 bytes .../__tests__/docx/dedocx/broken/html.docx | 28 + .../dedocx/broken/malformed-document-xml.docx | Bin 0 -> 47065 bytes .../docx/dedocx/broken/no-document-xml.docx | Bin 0 -> 20048 bytes .../__tests__/docx/dedocx/broken/png.docx | Bin 0 -> 35247 bytes .../broken/stupid name with spaces.docx | Bin 0 -> 31168 bytes .../dedocx/citation-container-author.docx | Bin 0 -> 153937 bytes .../__tests__/docx/dedocx/code.docx | Bin 0 -> 15045 bytes .../__tests__/docx/dedocx/custom-list.docx | Bin 0 -> 13862 bytes .../__tests__/docx/dedocx/doc-props.docx | Bin 0 -> 26193 bytes .../__tests__/docx/dedocx/equations.docx | Bin 0 -> 15997 bytes .../docx/dedocx/fake-test-docx.ds3.docx | 1 + .../docx/dedocx/fields/fields-bib.xml | 111 + .../docx/dedocx/fields/fields-single.xml | 23 + .../docx/dedocx/fields/fields-three.xml | 101 + .../dedocx/fields/fields-trivial-instr.xml | 38 + .../docx/dedocx/fields/fields-trivial.xml | 38 + ...tnotes-and-endnotes-multiple-repeated.docx | Bin 0 -> 17977 bytes .../docx/dedocx/footnotes-repeated.docx | Bin 0 -> 16432 bytes .../__tests__/docx/dedocx/footnotes.docx | Bin 0 -> 49638 bytes .../__tests__/docx/dedocx/full-tables.docx | Bin 0 -> 81146 bytes .../dedocx/inline-and-block-image-styles.docx | Bin 0 -> 38950 bytes .../docx/dedocx/list-of-citations.docx | Bin 0 -> 37187 bytes .../__tests__/docx/dedocx/list-test.docx | Bin 0 -> 19086 bytes .../__tests__/docx/dedocx/lists.docx | Bin 0 -> 14540 bytes .../docx/dedocx/minimum-test-document.docx | Bin 0 -> 2770434 bytes .../__tests__/docx/dedocx/picture-grid.docx | Bin 0 -> 321625 bytes .../__tests__/docx/dedocx/playground.docx | Bin 0 -> 25497 bytes .../__tests__/docx/dedocx/runs.docx | Bin 0 -> 31445 bytes .../__tests__/docx/dedocx/section-links.docx | Bin 0 -> 203908 bytes .../__tests__/docx/dedocx/symbols.docx | Bin 0 -> 37134 bytes .../docx/dedocx/tables-with-equations.docx | Bin 0 -> 23912 bytes .../__tests__/docx/dedocx/tables.docx | Bin 0 -> 83883 bytes .../__tests__/docx/dedocx/textboxes.docx | Bin 0 -> 1193767 bytes .../__tests__/docx/dedocx/tooltip.docx | Bin 0 -> 29396 bytes .../docx/dedocx/uncaptioned-images.ds3.docx | Bin 0 -> 920997 bytes .../docx/dedocx/undefined-bookmarks.docx | Bin 0 -> 17838 bytes .../__tests__/docx/simple/bold.docx | Bin 0 -> 24050 bytes .../__tests__/docx/simple/em.docx | Bin 0 -> 10594 bytes .../__tests__/docx/simple/embed-font.docx | Bin 0 -> 18200 bytes .../__tests__/docx/simple/helloworld.docx | Bin 30957 -> 0 bytes .../__tests__/docx/simple/highlight.docx | Bin 0 -> 10860 bytes .../__tests__/docx/simple/image.docx | Bin 0 -> 22490 bytes .../__tests__/docx/simple/link.docx | Bin 0 -> 12457 bytes .../__tests__/docx/simple/math.docx | Bin 0 -> 12719 bytes .../__tests__/docx/simple/shape-ellipse.docx | Bin 0 -> 15035 bytes .../__tests__/docx/simple/textbox.docx | Bin 0 -> 17805 bytes .../__tests__/docx/simple/w.docx | Bin 0 -> 10258 bytes packages/office-viewer/examples/app.ts | 10 +- .../office-viewer/examples/static/css/app.css | 4 +- packages/office-viewer/package.json | 1 + packages/office-viewer/src/OpenXML.ts | 41 + packages/office-viewer/src/Word.ts | 142 +- packages/office-viewer/src/openxml/Style.ts | 13 +- packages/office-viewer/src/openxml/Theme.ts | 22 +- packages/office-viewer/src/openxml/Types.ts | 4417 ++++++----------- .../src/openxml/math/OMML2MML.XSL | 2068 ++++++++ .../office-viewer/src/openxml/math/OMath.ts | 10 + .../office-viewer/src/openxml/math/README.md | 1 + .../src/openxml/math/convertOOML.ts | 11 + .../office-viewer/src/openxml/math/xsl.ts | 2077 ++++++++ .../office-viewer/src/openxml/word/Body.ts | 4 +- .../office-viewer/src/openxml/word/Break.ts | 2 +- .../office-viewer/src/openxml/word/Font.ts | 78 + .../src/openxml/word/FontTable.ts | 20 + .../src/openxml/word/Hyperlink.ts | 2 +- .../src/openxml/word/Paragraph.ts | 8 +- .../office-viewer/src/openxml/word/Pict.ts | 7 +- .../office-viewer/src/openxml/word/Run.ts | 7 + .../office-viewer/src/openxml/word/Section.ts | 15 +- .../office-viewer/src/openxml/word/Table.ts | 277 +- .../src/openxml/word/drawing/Blip.ts | 4 +- .../src/openxml/word/drawing/Drawing.ts | 160 +- .../src/openxml/word/drawing/Geom.ts | 17 + .../openxml/word/drawing/ShapeProperties.ts | 119 +- .../src/openxml/word/numbering/Lvl.ts | 9 +- .../src/openxml/word/table/Tc.ts | 222 +- .../src/openxml/word/table/Tr.ts | 96 +- .../office-viewer/src/openxml/word/wps/WPG.ts | 5 + .../office-viewer/src/openxml/word/wps/WPS.ts | 64 + .../src/package/PackageParser.ts | 5 +- .../src/package/ZipPackageParser.ts | 4 +- .../office-viewer/src/parse/parseBorder.ts | 24 +- .../src/parse/parseCellMargin.ts | 28 + .../office-viewer/src/parse/parseColor.ts | 93 +- .../src/parse/parseInsideBorders.ts | 25 + packages/office-viewer/src/parse/parsePr.ts | 62 +- .../office-viewer/src/parse/parseTable.ts | 45 + .../office-viewer/src/parse/parseTablePr.ts | 207 + .../office-viewer/src/parse/parseTblWidth.ts | 19 + packages/office-viewer/src/parse/parseTc.ts | 65 + packages/office-viewer/src/parse/parseTcPr.ts | 105 + .../src/parse/parseTextDirection.ts | 8 +- packages/office-viewer/src/parse/parseTr.ts | 46 + packages/office-viewer/src/parse/parseTrPr.ts | 57 + .../office-viewer/src/render/renderDrawing.ts | 93 +- .../office-viewer/src/render/renderFont.ts | 32 + .../office-viewer/src/render/renderMath.ts | 7 + .../src/render/renderNumbering.ts | 17 +- .../src/render/renderParagraph.ts | 8 +- .../office-viewer/src/render/renderPict.ts | 10 +- .../office-viewer/src/render/renderRun.ts | 2 +- .../office-viewer/src/render/renderStyle.ts | 64 +- .../office-viewer/src/render/renderTab.ts | 2 +- .../office-viewer/src/render/renderTable.ts | 32 +- .../src/render/setElementStyle.ts | 4 +- packages/office-viewer/src/util/dom.ts | 10 +- packages/office-viewer/tools/xsd2ts.ts | 73 +- 116 files changed, 7801 insertions(+), 3677 deletions(-) create mode 100644 packages/office-viewer/__tests__/docx/dedocx/README.md create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/corrupt-zip.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/doc.docx rename packages/office-viewer/{src/openxml/word/AlternateContent.ts => __tests__/docx/dedocx/broken/empty.docx} (100%) mode change 100644 => 100755 create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/excel.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/html.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/malformed-document-xml.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/no-document-xml.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/png.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/broken/stupid name with spaces.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/citation-container-author.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/code.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/custom-list.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/doc-props.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/equations.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/fake-test-docx.ds3.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/fields/fields-bib.xml create mode 100755 packages/office-viewer/__tests__/docx/dedocx/fields/fields-single.xml create mode 100755 packages/office-viewer/__tests__/docx/dedocx/fields/fields-three.xml create mode 100755 packages/office-viewer/__tests__/docx/dedocx/fields/fields-trivial-instr.xml create mode 100755 packages/office-viewer/__tests__/docx/dedocx/fields/fields-trivial.xml create mode 100755 packages/office-viewer/__tests__/docx/dedocx/footnotes-and-endnotes-multiple-repeated.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/footnotes-repeated.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/footnotes.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/full-tables.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/inline-and-block-image-styles.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/list-of-citations.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/list-test.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/lists.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/minimum-test-document.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/picture-grid.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/playground.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/runs.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/section-links.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/symbols.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/tables-with-equations.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/tables.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/textboxes.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/tooltip.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/uncaptioned-images.ds3.docx create mode 100755 packages/office-viewer/__tests__/docx/dedocx/undefined-bookmarks.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/bold.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/em.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/embed-font.docx delete mode 100644 packages/office-viewer/__tests__/docx/simple/helloworld.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/highlight.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/image.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/link.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/math.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/shape-ellipse.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/textbox.docx create mode 100644 packages/office-viewer/__tests__/docx/simple/w.docx create mode 100755 packages/office-viewer/src/openxml/math/OMML2MML.XSL create mode 100644 packages/office-viewer/src/openxml/math/OMath.ts create mode 100644 packages/office-viewer/src/openxml/math/README.md create mode 100644 packages/office-viewer/src/openxml/math/convertOOML.ts create mode 100644 packages/office-viewer/src/openxml/math/xsl.ts create mode 100644 packages/office-viewer/src/openxml/word/Font.ts create mode 100644 packages/office-viewer/src/openxml/word/FontTable.ts create mode 100644 packages/office-viewer/src/openxml/word/drawing/Geom.ts create mode 100644 packages/office-viewer/src/openxml/word/wps/WPG.ts create mode 100644 packages/office-viewer/src/openxml/word/wps/WPS.ts create mode 100644 packages/office-viewer/src/parse/parseCellMargin.ts create mode 100644 packages/office-viewer/src/parse/parseInsideBorders.ts create mode 100644 packages/office-viewer/src/parse/parseTable.ts create mode 100644 packages/office-viewer/src/parse/parseTablePr.ts create mode 100644 packages/office-viewer/src/parse/parseTblWidth.ts create mode 100644 packages/office-viewer/src/parse/parseTc.ts create mode 100644 packages/office-viewer/src/parse/parseTcPr.ts create mode 100644 packages/office-viewer/src/parse/parseTr.ts create mode 100644 packages/office-viewer/src/parse/parseTrPr.ts create mode 100644 packages/office-viewer/src/render/renderFont.ts create mode 100644 packages/office-viewer/src/render/renderMath.ts diff --git a/.dockerignore b/.dockerignore index 711869696..288b41209 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,3 +5,4 @@ npm/ .git/ .github/ .vscode/ +.rollup.cache diff --git a/docs/zh-CN/components/office-viewer.md b/docs/zh-CN/components/office-viewer.md index 4503457a1..c4488924c 100644 --- a/docs/zh-CN/components/office-viewer.md +++ b/docs/zh-CN/components/office-viewer.md @@ -18,14 +18,10 @@ order: 23 - 列表 - 注音 - 链接 +- 文本框 +- 数学公式(依赖 MathML,需要比较新的浏览器,或者试试 [polyfill](https://github.com/w3c/mathml-polyfills)) -不支持的功能: - -- 分页符 -- 形状 -- 艺术字 -- 域 -- 对象 +不支持的功能:分页符、形状、艺术字、域、对象、目录 ## 基本用法 @@ -34,7 +30,8 @@ order: 23 "type": "office-viewer", "src": "/examples/static/simple.docx", "wordOptions": { - "padding": "8px" + "padding": "8px", + "ignoreWidth": false } } ``` @@ -50,25 +47,37 @@ order: 23 "type": "office-viewer", "wordOptions": { "padding": "8px", - "classPrefix": "docx" + "ignoreWidth": false } } ``` -| 属性名 | 类型 | 默认值 | 说明 | -| ----------------- | --------- | ------------- | ------------------------------------------ | -| classPrefix | `string` | 'docx-viewer' | 渲染的 class 类前缀 | -| bulletUseFont | `boolean` | true | 列表使用字体渲染,请参考下面的乱码说明 | -| fontMapping | `object` | | 字体映射,是个键值对,用于替换文档中的字体 | -| forceLineHeight | `string` | | 设置段落行高,忽略文档中的设置 | -| padding | `string` | | 设置页面间距,忽略文档中的设置 | -| enableReplaceText | `boolean` | true | 是否开启变量替换功能 | +| 属性名 | 类型 | 默认值 | 说明 | +| ----------------- | --------- | ------------- | ---------------------------------------------------------- | +| classPrefix | `string` | 'docx-viewer' | 渲染的 class 类前缀 | +| ignoreWidth | `boolean` | false | 忽略文档里的宽度设置,用于更好嵌入到页面里,但会减低还原度 | +| padding | `string` | | 设置页面间距,忽略文档中的设置 | +| bulletUseFont | `boolean` | true | 列表使用字体渲染,请参考下面的乱码说明 | +| fontMapping | `object` | | 字体映射,是个键值对,用于替换文档中的字体 | +| forceLineHeight | `string` | | 设置段落行高,忽略文档中的设置 | +| enableReplaceText | `boolean` | true | 是否开启变量替换功能 | + +### 关于渲染效果差异 + +目前的实现难以保证和本地 Word 渲染完全一致,会遇到以下问题: + +1. 字体大小不一致 +1. 单元格宽度不一致,表格完全依赖浏览器渲染 +1. 分页显示,目前的渲染不会分页,而是内容有多长就有多高 +1. 分栏显示,这个是因为没有分页导致的,不限制高度没法分栏 + +如果追求完整效果打印,目前只能使用下载文件的方式用本地 Word 进行打印。 ## 列表符号出现乱码问题 默认情况下列表左侧的符号使用字体渲染,这样能做到最接近 Word 渲染效果,但如果用户的系统中没有这些字体就会显示乱码,为了解决这个问题需要手动在 amis 渲染的页面里导入对应的字体,比如 -``` +```html