@courtifyai/docx-render 1.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/src/index.ts ADDED
@@ -0,0 +1,137 @@
1
+ /**
2
+ * DOCX Render
3
+ * 自研 DOCX 渲染库,参考 docx-preview 架构
4
+ * 支持文档和评论一体化渲染,评论连接线
5
+ */
6
+
7
+ import { DocumentParser } from './parser'
8
+ import { DocumentRenderer } from './renderer'
9
+ import { IRendererOptions, IDocxDocument, ICommentElement } from './types'
10
+
11
+ // 样式
12
+ import './styles/index.css'
13
+
14
+ // 导出类型
15
+ export type {
16
+ IDocxDocument,
17
+ ICommentElement,
18
+ ICommentExtended,
19
+ IRendererOptions,
20
+ IOpenXmlElement,
21
+ IParagraphElement,
22
+ IRunElement,
23
+ IRunProperties,
24
+ ITextElement,
25
+ ITableElement,
26
+ ITheme,
27
+ IColorScheme,
28
+ IFontScheme,
29
+ IThemeColors,
30
+ IThemeColorRef,
31
+ IFootnoteElement,
32
+ IEndnoteElement,
33
+ IFootnoteReference,
34
+ IEndnoteReference,
35
+ // 书签类型
36
+ IBookmarkStartElement,
37
+ IBookmarkEndElement,
38
+ // Symbol 元素
39
+ ISymbolElement,
40
+ // 字体表类型
41
+ IFontTable,
42
+ IFontDeclaration,
43
+ IEmbedFontRef,
44
+ IFontSignature,
45
+ ILoadedEmbedFont,
46
+ TEmbedFontType,
47
+ // 下划线样式类型
48
+ TUnderlineStyle,
49
+ } from './types'
50
+
51
+ // 导出常量
52
+ export { DomType, DOCX_PARTS, XML_NS } from './types'
53
+
54
+ // 导出解析器
55
+ export { DocumentParser } from './parser'
56
+
57
+ // 导出渲染器
58
+ export { DocumentRenderer } from './renderer'
59
+
60
+ // 导出主题工具
61
+ export { parseTheme, resolveThemeColor, applyTintShade, resolveThemeFont } from './theme'
62
+
63
+ // 导出字体表工具
64
+ export {
65
+ parseFontTable,
66
+ getSubstituteFontName,
67
+ getFontDeclaration,
68
+ hasEmbeddedFont,
69
+ getEmbedFontRefs,
70
+ buildFontFamily,
71
+ loadEmbeddedFonts,
72
+ cleanupFontStyles,
73
+ } from './font-table'
74
+
75
+ // 导出评论工具
76
+ export { parseCommentsExtended, buildCommentTree } from './comments'
77
+
78
+ /**
79
+ * DOCX 渲染器类
80
+ * 整合解析器和渲染器,提供完整的渲染能力
81
+ */
82
+ export class DocxRender {
83
+ private parser: DocumentParser
84
+ private renderer: DocumentRenderer
85
+ private document: IDocxDocument | null = null
86
+
87
+ constructor(options: IRendererOptions) {
88
+ this.parser = new DocumentParser()
89
+ this.renderer = new DocumentRenderer(options)
90
+ }
91
+
92
+ /**
93
+ * 渲染 DOCX 文件
94
+ */
95
+ async render(file: File | ArrayBuffer | Blob): Promise<void> {
96
+ this.document = await this.parser.parse(file)
97
+ this.renderer.render(this.document)
98
+ }
99
+
100
+ /**
101
+ * 获取文档对象
102
+ */
103
+ getDocument(): IDocxDocument | null {
104
+ return this.document
105
+ }
106
+
107
+ /**
108
+ * 获取所有评论
109
+ */
110
+ getComments(): ICommentElement[] {
111
+ return this.document?.comments || []
112
+ }
113
+
114
+ /**
115
+ * 获取解析器(用于保存修改)
116
+ */
117
+ getParser(): DocumentParser {
118
+ return this.parser
119
+ }
120
+ }
121
+
122
+ /**
123
+ * 便捷方法:快速渲染 DOCX 文件
124
+ */
125
+ export async function renderDocx(
126
+ file: File | ArrayBuffer | Blob,
127
+ container: HTMLElement | string,
128
+ options?: Partial<IRendererOptions>
129
+ ): Promise<DocxRender> {
130
+ const render = new DocxRender({
131
+ container,
132
+ ...options,
133
+ })
134
+
135
+ await render.render(file)
136
+ return render
137
+ }