@docx4j/core-ts 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.
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +126 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +10 -0
- package/dist/model/content/Body.d.mts +107 -0
- package/dist/model/content/Body.mjs +363 -0
- package/dist/model/content/Font.d.mts +46 -0
- package/dist/model/content/Font.mjs +50 -0
- package/dist/model/content/Paragraph.d.mts +99 -0
- package/dist/model/content/Paragraph.mjs +364 -0
- package/dist/model/content/Range.d.mts +34 -0
- package/dist/model/content/Range.mjs +94 -0
- package/dist/model/content/index.d.mts +7 -0
- package/dist/model/content/index.mjs +9 -0
- package/dist/model/content/search.d.mts +12 -0
- package/dist/model/content/search.mjs +68 -0
- package/dist/model/content/tree.d.mts +40 -0
- package/dist/model/content/tree.mjs +120 -0
- package/dist/model/index.d.mts +1 -0
- package/dist/model/index.mjs +2 -0
- package/dist/opc/ContentTypeManager.d.mts +37 -0
- package/dist/opc/ContentTypeManager.mjs +126 -0
- package/dist/opc/ContentTypes.d.mts +118 -0
- package/dist/opc/ContentTypes.mjs +139 -0
- package/dist/opc/FlatOpcPartStore.d.mts +42 -0
- package/dist/opc/FlatOpcPartStore.mjs +151 -0
- package/dist/opc/Load.d.mts +18 -0
- package/dist/opc/Load.mjs +123 -0
- package/dist/opc/PartName.d.mts +46 -0
- package/dist/opc/PartName.mjs +132 -0
- package/dist/opc/PartStore.d.mts +45 -0
- package/dist/opc/PartStore.mjs +54 -0
- package/dist/opc/Save.d.mts +9 -0
- package/dist/opc/Save.mjs +52 -0
- package/dist/opc/ZipPartStore.d.mts +32 -0
- package/dist/opc/ZipPartStore.mjs +117 -0
- package/dist/opc/exceptions.d.mts +17 -0
- package/dist/opc/exceptions.mjs +22 -0
- package/dist/opc/index.d.mts +12 -0
- package/dist/opc/index.mjs +13 -0
- package/dist/opc/mce/preprocessor.d.mts +15 -0
- package/dist/opc/mce/preprocessor.mjs +74 -0
- package/dist/opc/mce/understood.d.mts +1 -0
- package/dist/opc/mce/understood.mjs +98 -0
- package/dist/packages/OpcPackage.d.mts +62 -0
- package/dist/packages/OpcPackage.mjs +133 -0
- package/dist/packages/PresentationMLPackage.d.mts +12 -0
- package/dist/packages/PresentationMLPackage.mjs +39 -0
- package/dist/packages/SpreadsheetMLPackage.d.mts +12 -0
- package/dist/packages/SpreadsheetMLPackage.mjs +38 -0
- package/dist/packages/WordprocessingMLPackage.d.mts +49 -0
- package/dist/packages/WordprocessingMLPackage.mjs +130 -0
- package/dist/packages/index.d.mts +5 -0
- package/dist/packages/index.mjs +6 -0
- package/dist/packages/registry.d.mts +7 -0
- package/dist/packages/registry.mjs +18 -0
- package/dist/parts/BinaryPart.d.mts +38 -0
- package/dist/parts/BinaryPart.mjs +72 -0
- package/dist/parts/DefaultXmlPart.d.mts +29 -0
- package/dist/parts/DefaultXmlPart.mjs +63 -0
- package/dist/parts/Namespaces.d.mts +99 -0
- package/dist/parts/Namespaces.mjs +99 -0
- package/dist/parts/Part.d.mts +50 -0
- package/dist/parts/Part.mjs +68 -0
- package/dist/parts/PartRegistry.d.mts +20 -0
- package/dist/parts/PartRegistry.mjs +142 -0
- package/dist/parts/Parts.d.mts +13 -0
- package/dist/parts/Parts.mjs +28 -0
- package/dist/parts/RelationshipsPart.d.mts +66 -0
- package/dist/parts/RelationshipsPart.mjs +224 -0
- package/dist/parts/XmlPart.d.mts +46 -0
- package/dist/parts/XmlPart.mjs +87 -0
- package/dist/parts/customXml/index.d.mts +9 -0
- package/dist/parts/customXml/index.mjs +11 -0
- package/dist/parts/dml/index.d.mts +54 -0
- package/dist/parts/dml/index.mjs +83 -0
- package/dist/parts/docProps/index.d.mts +14 -0
- package/dist/parts/docProps/index.mjs +18 -0
- package/dist/parts/index.d.mts +14 -0
- package/dist/parts/index.mjs +15 -0
- package/dist/parts/pml/index.d.mts +53 -0
- package/dist/parts/pml/index.mjs +90 -0
- package/dist/parts/sml/index.d.mts +61 -0
- package/dist/parts/sml/index.mjs +107 -0
- package/dist/parts/wml/defaultStyles.d.mts +1 -0
- package/dist/parts/wml/defaultStyles.mjs +500 -0
- package/dist/parts/wml/index.d.mts +102 -0
- package/dist/parts/wml/index.mjs +213 -0
- package/dist/xml/dom.d.mts +22 -0
- package/dist/xml/dom.mjs +92 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
@docx4j/core-ts
|
|
2
|
+
Copyright 2026 Plutext Pty Ltd
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
This package is a TypeScript counterpart of docx4j's docx4j-core module
|
|
7
|
+
(https://github.com/plutext/docx4j, Apache License 2.0): the Open Packaging Conventions layer
|
|
8
|
+
(packages, parts, relationships, content types), the typed parts, and the style, numbering and font
|
|
9
|
+
resolution utilities follow docx4j's design and names. It depends on @docx4j/generated-objects-ts
|
|
10
|
+
(https://github.com/plutext/docx4j-generated-objects-ts) for the Office Open XML object model and on
|
|
11
|
+
@docx4j/jsonix (https://github.com/plutext/jsonix) for XML binding; see their NOTICE files for the
|
|
12
|
+
material they derive from (ECMA-376, Microsoft Open Specifications, Jsonix).
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @docx4j/core-ts
|
|
2
|
+
|
|
3
|
+
docx4j-core for TypeScript: Open Packaging (docx, pptx and xlsx as zip, and the flat OPC packages
|
|
4
|
+
that Office JS `getOoxml()` returns), typed parts and relationships, and docx4j's style, numbering
|
|
5
|
+
and font resolution, over the Office Open XML object model of
|
|
6
|
+
[`@docx4j/generated-objects-ts`](https://github.com/plutext/docx4j-generated-objects-ts). Node,
|
|
7
|
+
browsers and Word add-ins.
|
|
8
|
+
|
|
9
|
+
Status: the Open Packaging layer, the typed parts and the packages (Phase A of
|
|
10
|
+
[CR-001](docs/change-requests/CR-001-engine.md)) are implemented; the resolution utilities
|
|
11
|
+
(`PropertyResolver`, list numbering, fonts: Phase B) are next.
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install @docx4j/core-ts
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { WordprocessingMLPackage, ImagePart, ContentTypes } from '@docx4j/core-ts';
|
|
19
|
+
|
|
20
|
+
// a zip (Uint8Array), a flat OPC string from Office JS getOoxml(), or a PartStore
|
|
21
|
+
const pkg = await WordprocessingMLPackage.load(bytes);
|
|
22
|
+
const body = await pkg.getBody(); // Office JS Word.Body, over the typed tree
|
|
23
|
+
body.insertParagraph('Hello World', 'End');
|
|
24
|
+
for (const range of body.search('draft', { matchWholeWord: true })) range.insertText('final', 'Replace');
|
|
25
|
+
body.paragraphs[0].font.bold = true;
|
|
26
|
+
|
|
27
|
+
const main = pkg.getMainDocumentPart();
|
|
28
|
+
const document = main.contents; // org_docx4j_wml.Document: the tree behind the views
|
|
29
|
+
const styles = await main.styleDefinitionsPart.getContents();
|
|
30
|
+
|
|
31
|
+
const image = new ImagePart('/word/media/image1.png', ContentTypes.IMAGE_PNG);
|
|
32
|
+
image.setBytes(pngBytes);
|
|
33
|
+
const rel = main.addTargetPart(image); // rel.id is the r:embed to use in the document
|
|
34
|
+
|
|
35
|
+
const docx = await pkg.save(); // zip: only the parts you unmarshalled are re-marshalled
|
|
36
|
+
const ooxml = await pkg.saveFlatOpc(); // pkg:package, for insertOoxml()
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
A part that is never touched is written back byte for byte; `getContents()` (or a view over
|
|
40
|
+
it) marks a part for re-marshalling. `mc:AlternateContent` is resolved when a part is
|
|
41
|
+
unmarshalled, as Word does on open (`{ mcePreprocess: false }` to keep it). Subpaths
|
|
42
|
+
`@docx4j/core-ts/opc`, `/parts`, `/packages` and `/model` give the layers separately.
|
|
43
|
+
|
|
44
|
+
### The content API
|
|
45
|
+
|
|
46
|
+
`Body`, `Paragraph`, `Range` and `Font` follow Office JS's `Word.*` shapes (a compile-time
|
|
47
|
+
check keeps them assignable to a subset of those types), so add-in code runs against a
|
|
48
|
+
package with its `load` and `sync` lines removed. Hello World, from nothing to a `.docx`:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { writeFile } from 'node:fs/promises';
|
|
52
|
+
import { WordprocessingMLPackage } from '@docx4j/core-ts';
|
|
53
|
+
|
|
54
|
+
const pkg = await WordprocessingMLPackage.createPackage({ pageSize: 'A4' });
|
|
55
|
+
pkg.body.insertParagraph('Hello World', 'End');
|
|
56
|
+
await writeFile('hello.docx', await pkg.save());
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Editing what is there:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
const body = await pkg.getBody();
|
|
63
|
+
const title = body.insertParagraph('Report', 'Start');
|
|
64
|
+
title.styleBuiltIn = 'Heading 1';
|
|
65
|
+
title.alignment = 'Centered';
|
|
66
|
+
const [hit] = body.search('quick brown fox'); // across runs
|
|
67
|
+
hit.font.italic = true; // runs split at the boundaries
|
|
68
|
+
hit.insertText('slow red fox', 'Replace'); // keeps the first replaced character's formatting
|
|
69
|
+
body.paragraphs.at(-1)?.insertParagraph('The end.', 'After');
|
|
70
|
+
body.text; // a paragraph per line
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
For agents, every paragraph has an address (`body/3`, or the `w14:paraId` Word wrote), and
|
|
74
|
+
`outline()` lists them all:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const outline = await pkg.outline(); // { paragraphs: [{ address: 'body/0', paraId, style, text }, ...], tables, headers, footers }
|
|
78
|
+
const p = await pkg.paragraphAt('body/3');
|
|
79
|
+
p?.insertParagraph('Inserted after the fourth block', 'After');
|
|
80
|
+
(await pkg.paragraphAt({ contains: 'Chapter 2' }))?.delete();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Docx4j's names are there as aliases (`addParagraphOfText`, `addStyledParagraphOfText`,
|
|
84
|
+
`addObject`, `getContent`), and the tree stays reachable: `paragraph.p` is the `P`,
|
|
85
|
+
`body.content` the live array.
|
|
86
|
+
|
|
87
|
+
### XML in
|
|
88
|
+
|
|
89
|
+
A whole `word/document.xml` becomes the main document part's contents with `setXml`, and a
|
|
90
|
+
fragment (one or more `w:p`, `w:tbl`, ... as written inside `document.xml`, standard prefixes
|
|
91
|
+
declared for you) goes in through `insertXml`:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
main.setXml(`<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
|
95
|
+
<w:body><w:p><w:r><w:t>Hello World</w:t></w:r></w:p><w:sectPr/></w:body>
|
|
96
|
+
</w:document>`);
|
|
97
|
+
const body = await main.getBody();
|
|
98
|
+
await body.insertXml('<w:p><w:pPr><w:pStyle w:val="Heading1"/></w:pPr><w:r><w:t>From XML</w:t></w:r></w:p>', 'Start');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Typed content is built with the objects package's generated factories and appended with
|
|
102
|
+
`addObject` (docx4j) or `insertElement`, which links parent pointers and rejects what a body
|
|
103
|
+
cannot hold:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import * as el from '@docx4j/generated-objects-ts/el/org_docx4j_wml';
|
|
107
|
+
body.addObject(el.p({ content: [el.r({ content: [el.t({ value: 'Hello World' })] })] }));
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Tables, pictures, `insertOoxml` from a `pkg:package`, custom XML parts, XML mapping and typed
|
|
111
|
+
content controls are the next phases of
|
|
112
|
+
[CR-002](docs/change-requests/CR-002-content-api.md); effective formatting (styles resolved,
|
|
113
|
+
as Office JS reports it) comes with [CR-001](docs/change-requests/CR-001-engine.md) Phase B.
|
|
114
|
+
|
|
115
|
+
## Development
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
npm ci # the locked dependencies from npm
|
|
119
|
+
npm run typecheck && npm test # test = build, then node --test test/*.test.mjs
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Releases are published to npm from GitHub Actions; see `RELEASING.md`.
|
|
123
|
+
|
|
124
|
+
## Licence
|
|
125
|
+
|
|
126
|
+
Apache-2.0, as docx4j. See NOTICE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from '@docx4j/generated-objects-ts';
|
|
2
|
+
export type { Part as FlatOpcPart } from '@docx4j/generated-objects-ts';
|
|
3
|
+
export { Part } from './parts/Part.mjs';
|
|
4
|
+
export * from './opc/index.mjs';
|
|
5
|
+
export * from './parts/index.mjs';
|
|
6
|
+
export * from './packages/index.mjs';
|
|
7
|
+
export * from './model/index.mjs';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @docx4j/core-ts: the counterpart of docx4j-core. The Open Packaging layer, the typed parts and
|
|
2
|
+
// the packages are specified in docs/change-requests/CR-001-engine.md. The object model's facade
|
|
3
|
+
// (docx4j's XmlUtils names) is re-exported so that code written against
|
|
4
|
+
// @docx4j/generated-objects-ts reads the same here.
|
|
5
|
+
export * from '@docx4j/generated-objects-ts';
|
|
6
|
+
export { Part } from './parts/Part.mjs';
|
|
7
|
+
export * from './opc/index.mjs';
|
|
8
|
+
export * from './parts/index.mjs';
|
|
9
|
+
export * from './packages/index.mjs';
|
|
10
|
+
export * from './model/index.mjs';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type * as wml from '@docx4j/generated-objects-ts/modules/org_docx4j_wml';
|
|
2
|
+
import type { XmlPart } from '../../parts/XmlPart.mjs';
|
|
3
|
+
import type { OpcPackage } from '../../packages/OpcPackage.mjs';
|
|
4
|
+
import { type Element } from './tree.mjs';
|
|
5
|
+
import { Paragraph } from './Paragraph.mjs';
|
|
6
|
+
import { Range } from './Range.mjs';
|
|
7
|
+
import type { SearchOptions } from './search.mjs';
|
|
8
|
+
/** Where a paragraph or table is, for agents and across tool calls (CR-002 section 3.3). */
|
|
9
|
+
export type Address = string | {
|
|
10
|
+
contains: string;
|
|
11
|
+
} | {
|
|
12
|
+
paraId: string;
|
|
13
|
+
};
|
|
14
|
+
/** A view over a block-level element that is not a paragraph (a table, a content control), for insert targets and outlines. */
|
|
15
|
+
export interface BlockElement {
|
|
16
|
+
element: Element;
|
|
17
|
+
container: Element[];
|
|
18
|
+
}
|
|
19
|
+
export interface OutlineParagraph {
|
|
20
|
+
address: string;
|
|
21
|
+
paraId?: string;
|
|
22
|
+
style: string;
|
|
23
|
+
text: string;
|
|
24
|
+
}
|
|
25
|
+
export interface OutlineTable {
|
|
26
|
+
address: string;
|
|
27
|
+
rows: number;
|
|
28
|
+
cols: number;
|
|
29
|
+
}
|
|
30
|
+
export interface Outline {
|
|
31
|
+
paragraphs: OutlineParagraph[];
|
|
32
|
+
tables: OutlineTable[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A subset of Office JS `Word.Body` over any container with block-level content: the main
|
|
36
|
+
* document's body, a header, a footer, a footnote, a cell or a content control. A view: the
|
|
37
|
+
* tree is the state; `content` is the live array.
|
|
38
|
+
*/
|
|
39
|
+
export declare class Body {
|
|
40
|
+
/** The part whose tree this is; marks it for re-marshalling by being unmarshalled. */
|
|
41
|
+
readonly part: XmlPart<unknown> | undefined;
|
|
42
|
+
/** The container value: a Body, Hdr, Ftr, Tc, SdtContentBlock, ... */
|
|
43
|
+
readonly container: {
|
|
44
|
+
content?: Element[];
|
|
45
|
+
};
|
|
46
|
+
/** The address prefix: 'body', 'header:rId3', 'footer:rId4', 'footnote:2'. */
|
|
47
|
+
readonly prefix: string;
|
|
48
|
+
readonly package_?: OpcPackage | undefined;
|
|
49
|
+
constructor(
|
|
50
|
+
/** The part whose tree this is; marks it for re-marshalling by being unmarshalled. */
|
|
51
|
+
part: XmlPart<unknown> | undefined,
|
|
52
|
+
/** The container value: a Body, Hdr, Ftr, Tc, SdtContentBlock, ... */
|
|
53
|
+
container: {
|
|
54
|
+
content?: Element[];
|
|
55
|
+
},
|
|
56
|
+
/** The address prefix: 'body', 'header:rId3', 'footer:rId4', 'footnote:2'. */
|
|
57
|
+
prefix?: string, package_?: OpcPackage | undefined);
|
|
58
|
+
/** The live content array (docx4j getContent()); block-level elements only, sectPr excluded. */
|
|
59
|
+
get content(): Element[];
|
|
60
|
+
/** Every paragraph in document order, descending into tables and content controls. */
|
|
61
|
+
get paragraphs(): Paragraph[];
|
|
62
|
+
/** The tables directly in this body (as elements; `Table` views are CR-002 Phase C). */
|
|
63
|
+
get tables(): BlockElement[];
|
|
64
|
+
/** The text, a paragraph per line. */
|
|
65
|
+
get text(): string;
|
|
66
|
+
insertParagraph(text: string, location: 'Start' | 'End'): Paragraph;
|
|
67
|
+
/** Text at the start of the first paragraph, the end of the last, or replacing everything with one paragraph. */
|
|
68
|
+
insertText(text: string, location: 'Start' | 'End' | 'Replace'): Range;
|
|
69
|
+
insertBreak(type: 'Page' | 'Line', location: 'Start' | 'End'): void;
|
|
70
|
+
/**
|
|
71
|
+
* Inserts block-level elements (docx4j addObject): at the start or end of this body, or
|
|
72
|
+
* before or after a target paragraph, element or address. Links PARENT. Rejects anything the
|
|
73
|
+
* body cannot hold, naming what was passed. Returns the view of the first inserted element.
|
|
74
|
+
*/
|
|
75
|
+
insertElement(element: Element | Element[], location: 'Start' | 'End' | 'Before' | 'After', target?: Paragraph | BlockElement | Address): Paragraph | BlockElement;
|
|
76
|
+
/** Inserts a WML fragment (one or more w:p / w:tbl / ... as inside document.xml); the standard prefixes are declared for it. */
|
|
77
|
+
insertXml(xml: string, location: 'Start' | 'End' | 'Before' | 'After', target?: Paragraph | BlockElement | Address): Promise<(Paragraph | BlockElement)[]>;
|
|
78
|
+
/** Matches over every paragraph, in document order. */
|
|
79
|
+
search(text: string, options?: SearchOptions): Range[];
|
|
80
|
+
/** Removes everything (section properties stay). */
|
|
81
|
+
clear(): void;
|
|
82
|
+
/** The part's XML (extension; Office JS getOoxml wraps it in a package). */
|
|
83
|
+
getXml(): Promise<string>;
|
|
84
|
+
/** The element at an address: 'body/3' (fourth block), 'body/3/1/0/2' (table row 1, cell 0, block 2), a paraId, or a text match. */
|
|
85
|
+
elementAt(address: Address): BlockElement | undefined;
|
|
86
|
+
paragraphAt(address: Address): Paragraph | undefined;
|
|
87
|
+
/** The ordinal address of a paragraph or element in this body, from PARENT links; undefined when it is not here. */
|
|
88
|
+
addressOf(target: Paragraph | BlockElement): string | undefined;
|
|
89
|
+
/** What an agent asks for first: every paragraph and table with its address, style and text. */
|
|
90
|
+
outline(): Outline;
|
|
91
|
+
/** docx4j MainDocumentPart.addParagraphOfText. */
|
|
92
|
+
addParagraphOfText(text: string): wml.P;
|
|
93
|
+
/** docx4j MainDocumentPart.addStyledParagraphOfText. */
|
|
94
|
+
addStyledParagraphOfText(styleId: string, text: string): wml.P;
|
|
95
|
+
/** docx4j addObject: appends a block-level element. */
|
|
96
|
+
addObject(element: Element): void;
|
|
97
|
+
/** docx4j getContent(). */
|
|
98
|
+
getContent(): Element[];
|
|
99
|
+
private resolveTarget;
|
|
100
|
+
/** The typed object whose content array this is, for PARENT links. */
|
|
101
|
+
private ownerOf;
|
|
102
|
+
private containerOf;
|
|
103
|
+
private pathOf;
|
|
104
|
+
/** Gives a new paragraph a w14:paraId when the document already uses them. */
|
|
105
|
+
private assignParaId;
|
|
106
|
+
private usesParaIds;
|
|
107
|
+
}
|