abstract-document 18.2.6 → 18.2.10

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 (32) hide show
  1. package/lib/abstract-document/atoms/image.d.ts +4 -0
  2. package/lib/abstract-document/atoms/image.d.ts.map +1 -1
  3. package/lib/abstract-document/atoms/image.js.map +1 -1
  4. package/lib/abstract-document-exporters/pdf/measure.d.ts.map +1 -1
  5. package/lib/abstract-document-exporters/pdf/measure.js.map +1 -1
  6. package/lib/abstract-document-exporters/pdf/render-image.d.ts.map +1 -1
  7. package/lib/abstract-document-exporters/pdf/render-image.js +19 -3
  8. package/lib/abstract-document-exporters/pdf/render-image.js.map +1 -1
  9. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.d.ts +5 -1
  10. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.d.ts.map +1 -1
  11. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.js +8 -24
  12. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.js.map +1 -1
  13. package/lib/abstract-document-xml/abstract-doc-of-xml/creator.d.ts.map +1 -1
  14. package/lib/abstract-document-xml/abstract-doc-of-xml/creator.js.map +1 -1
  15. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.d.ts +3 -5
  16. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.d.ts.map +1 -1
  17. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.js +4 -4
  18. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.js.map +1 -1
  19. package/lib/abstract-document-xml/xsd-template/elements.d.ts +1 -1
  20. package/lib/abstract-document-xml/xsd-template/elements.d.ts.map +1 -1
  21. package/lib/abstract-document-xml/xsd-template/elements.js +18 -0
  22. package/lib/abstract-document-xml/xsd-template/elements.js.map +1 -1
  23. package/lib/abstract-document-xml/xsd-template/xsd-template.d.ts +2 -2
  24. package/lib/abstract-document-xml/xsd-template/xsd-template.d.ts.map +1 -1
  25. package/package.json +4 -6
  26. package/src/abstract-document/atoms/image.ts +4 -0
  27. package/src/abstract-document-exporters/pdf/measure.ts +7 -10
  28. package/src/abstract-document-exporters/pdf/render-image.ts +29 -8
  29. package/src/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.ts +32 -35
  30. package/src/abstract-document-xml/abstract-doc-of-xml/creator.ts +36 -35
  31. package/src/abstract-document-xml/abstract-doc-of-xml/custom-elements.tsx +18 -8
  32. package/src/abstract-document-xml/xsd-template/elements.ts +18 -0
@@ -3,7 +3,7 @@ import { AbstractDoc } from "../../abstract-document/index.js";
3
3
  import * as StyleKey from "../../abstract-document/styles/style-key.js";
4
4
  import { Resources } from "../../abstract-document/resources.js";
5
5
  import { ADCreatorFn, creators, propsCreators } from "./creator.js";
6
- import { parseHandlebarsXml, parseMustacheXml, type XmlElement } from "handlebars-xml";
6
+ import { parseHandlebarsXml, type XmlElement } from "handlebars-xml";
7
7
  import { getFontStyleName } from "../../abstract-document-exporters/pdf/font.js";
8
8
  import { Font } from "../../abstract-document/primitives/font.js";
9
9
 
@@ -21,32 +21,27 @@ export type Format = "PDF" | "DOCX";
21
21
 
22
22
  export async function abstractDocsXml(
23
23
  templateInputs: ReadonlyArray<TemplateInput>,
24
- getResources: (imageUrls: Record<string, true>, fontFamilies: Record<string, ReadonlyArray<keyof Font>>) => Promise<Resources>
24
+ getResources: (
25
+ imageUrls: Record<string, true>,
26
+ fontFamilies: Record<string, ReadonlyArray<keyof Font>>
27
+ ) => Promise<Resources>
25
28
  ): Promise<AbstractDoxXmlsResult> {
26
29
  try {
27
30
  const abstractDocs = Array<AbstractDoc.AbstractDoc>();
28
31
  let imageUrls: Record<string, true> = {};
29
- let fontFamilies: Record<string, ReadonlyArray<keyof Font>> = {};
32
+ const fontFamilies: Record<string, Partial<Record<keyof Font, boolean>>> = {};
30
33
  for (const r of templateInputs) {
31
- const [ad, newImageUrls, newFontFamilies] = abstractDocXml(r.template, r.data, r.partials, "Handlebars");
34
+ const [ad, newImageUrls, newFontFamilies] = abstractDocXml(r.template, r.data, r.partials);
32
35
  abstractDocs.push(ad);
33
36
  imageUrls = { ...imageUrls, ...newImageUrls };
34
-
35
- for(const [font, families] of Object.entries(newFontFamilies)) {
36
- if(font in fontFamilies) {
37
- const newFamilies: Array<keyof Font> = [];
38
- for(const newFamily of families) {
39
- if(fontFamilies[font].find((v) => v === newFamily) === undefined) {
40
- newFamilies.push(newFamily);
41
- }
42
- }
43
- fontFamilies[font] = [...fontFamilies[font], ...newFamilies];
44
- } else {
45
- fontFamilies[font] = families;
46
- }
47
- }
37
+ Object.entries(newFontFamilies).forEach(
38
+ ([font, types]) => (fontFamilies[font] = { ...fontFamilies[font], ...types })
39
+ );
48
40
  }
49
- const resources = await getResources(imageUrls, fontFamilies);
41
+ const resources = await getResources(
42
+ imageUrls,
43
+ Object.fromEntries(Object.entries(fontFamilies).map(([f, s]) => [f, Object.keys(s) as Array<keyof Font>]))
44
+ );
50
45
  const combinedReport = addResources(merge(...abstractDocs), resources);
51
46
  return { type: "Ok", value: combinedReport };
52
47
  } catch (e) {
@@ -57,11 +52,13 @@ export async function abstractDocsXml(
57
52
  export function abstractDocXml(
58
53
  template: string,
59
54
  data: any,
60
- partials: Record<string, string>,
61
- rendered: "Mustache" | "Handlebars" = "Handlebars"
62
- ): readonly [AbstractDoc.AbstractDoc, imageUrls: Record<string, true>, fontFamilies: Record<string, ReadonlyArray<keyof Font>>] {
63
- const xml =
64
- rendered === "Mustache" ? parseMustacheXml(template, data, partials) : parseHandlebarsXml(template, data, partials);
55
+ partials: Record<string, string>
56
+ ): readonly [
57
+ AbstractDoc.AbstractDoc,
58
+ imageUrls: Record<string, true>,
59
+ fontFamilies: Record<string, Partial<Record<keyof Font, boolean>>>
60
+ ] {
61
+ const xml = parseHandlebarsXml(template, data, partials);
65
62
  const [imageUrls, fontFamilies, styleNames] = extractImageFontsStyleNames(xml);
66
63
  const doc = abstractDocXmlRecursive(creators(styleNames), xml[0]!);
67
64
  return [doc, imageUrls, fontFamilies];
@@ -116,7 +113,8 @@ function abstractDocXmlRecursive(
116
113
  const obj = creator(allProps, children) as { [k: string]: unknown };
117
114
 
118
115
  for (const propName of Object.keys(allProps).sort((a, b) => a.length - b.length)) {
119
- const propsCreator = allProps[propName] !== undefined && propsCreators[propName] ? propsCreators[propName] : undefined;
116
+ const propsCreator =
117
+ allProps[propName] !== undefined && propsCreators[propName] ? propsCreators[propName] : undefined;
120
118
  if (propsCreator) {
121
119
  const attributeName = getSuffixedAttributeBaseName(propsCreator.name) ?? propsCreator.name;
122
120
  obj[attributeName] = propsCreator(allProps, children);
@@ -169,8 +167,8 @@ function abstractDocXmlRecursive(
169
167
  function getSuffixedAttributeBaseName(attributeName: string): string | undefined {
170
168
  const suffixRemoved = (() => {
171
169
  const suffixes = ["Top", "Bottom", "Left", "Right"];
172
- for(const suffix of suffixes) {
173
- if(attributeName.endsWith(suffix)) {
170
+ for (const suffix of suffixes) {
171
+ if (attributeName.endsWith(suffix)) {
174
172
  return attributeName.slice(0, -suffix.length);
175
173
  }
176
174
  }
@@ -191,19 +189,18 @@ function extractImageFontsStyleNames(
191
189
  xmlElement: ReadonlyArray<XmlElement>,
192
190
  styleNames: Record<string, string> = {},
193
191
  images: Record<string, true> = {},
194
- fonts: Record<string, ReadonlyArray<keyof Font>> = {},
195
- ): readonly [imageUrls: Record<string, true>, fontFamilies: Record<string, ReadonlyArray<keyof Font>>, styleNames: Record<string, string>] {
192
+ fonts: Record<string, Partial<Record<keyof Font, boolean>>> = {}
193
+ ): readonly [
194
+ imageUrls: Record<string, true>,
195
+ fontFamilies: Record<string, Partial<Record<keyof Font, boolean>>>,
196
+ styleNames: Record<string, string>
197
+ ] {
196
198
  xmlElement.forEach((item) => {
197
199
  if (item.tagName.startsWith("Image") && item.attributes?.src) {
198
200
  images[item.attributes.src as string] = true;
199
201
  } else if (item.attributes?.fontFamily) {
200
-
201
202
  const styleName = getFontStyleName(item.attributes);
202
- const currentStyleNames = fonts[item.attributes.fontFamily as string] ?? [];
203
- if(currentStyleNames.findIndex((v) => v === styleName) === -1) {
204
- fonts[item.attributes.fontFamily as string] = [...currentStyleNames, styleName];
205
- }
206
-
203
+ (fonts[item.attributes.fontFamily as string] ??= {})[styleName] = true;
207
204
  if (item.tagName === "StyleName" && item.attributes.name && item.attributes.type) {
208
205
  styleNames[item.attributes.name as string] = item.attributes.type;
209
206
  }
@@ -46,9 +46,10 @@ export const creators: (styleNames: Record<string, string>) => Record<string, AD
46
46
  TextCell: (props: TextCellProps) => TextCell(props, styleNames),
47
47
  TextParagraph: (props: TextParagraphProps) => TextParagraph(props, styleNames),
48
48
  TextRun: (props) => TextRun.create(props as unknown as TextRun.TextRunProps),
49
- ImageRow: (props: ImageRowProps) => ImageRow(imageProps(props) as unknown as ImageRowProps, styleNames),
50
- ImageCell: (props: ImageCellProps) => ImageCell(imageProps(props) as unknown as ImageCellProps, styleNames),
51
- ImageParagraph: (props: ImageParagraphProps) =>
49
+ ImageRow: (props: Record<string, unknown>) => ImageRow(imageProps(props) as unknown as ImageRowProps, styleNames),
50
+ ImageCell: (props: Record<string, unknown>) =>
51
+ ImageCell(imageProps(props) as unknown as ImageCellProps, styleNames),
52
+ ImageParagraph: (props: Record<string, unknown>) =>
52
53
  ImageParagraph(imageProps(props) as unknown as ImageParagraphProps, styleNames),
53
54
  Image: (props: Record<string, unknown>) => Image.create(imageProps(props) as unknown as Image.ImageProps),
54
55
  Table: (props, children: ReadonlyArray<TableRow.TableRow>) =>
@@ -139,27 +140,27 @@ export const propsCreators: Record<string, ADCreatorFn> = {
139
140
  },
140
141
  borderTop: (props: { readonly borderTop: string }): unknown => {
141
142
  const allProps = props as Record<string, unknown>;
142
- const borders: { top?: number, bottom?: number, left?: number, right?: number } = allProps.borders ?? {};
143
+ const borders: { top?: number; bottom?: number; left?: number; right?: number } = allProps.borders ?? {};
143
144
  borders.top = Number(props.borderTop);
144
- return borders;
145
+ return borders;
145
146
  },
146
147
  borderBottom: (props: { readonly borderBottom: string }): unknown => {
147
148
  const allProps = props as Record<string, unknown>;
148
- const borders: { top?: number, bottom?: number, left?: number, right?: number } = allProps.borders ?? {};
149
+ const borders: { top?: number; bottom?: number; left?: number; right?: number } = allProps.borders ?? {};
149
150
  borders.bottom = Number(props.borderBottom);
150
- return borders;
151
+ return borders;
151
152
  },
152
153
  borderLeft: (props: { readonly borderLeft: string }): unknown => {
153
154
  const allProps = props as Record<string, unknown>;
154
- const borders: { top?: number, bottom?: number, left?: number, right?: number } = allProps.borders ?? {};
155
+ const borders: { top?: number; bottom?: number; left?: number; right?: number } = allProps.borders ?? {};
155
156
  borders.left = Number(props.borderLeft);
156
- return borders;
157
+ return borders;
157
158
  },
158
159
  borderRight: (props: { readonly borderRight: string }): unknown => {
159
160
  const allProps = props as Record<string, unknown>;
160
- const borders: { top?: number, bottom?: number, left?: number, right?: number } = allProps.borders ?? {};
161
+ const borders: { top?: number; bottom?: number; left?: number; right?: number } = allProps.borders ?? {};
161
162
  borders.right = Number(props.borderRight);
162
- return borders;
163
+ return borders;
163
164
  },
164
165
  padding: (props: { readonly padding: string }): unknown => {
165
166
  const padding: { [k: string]: number } = { top: 0, right: 0, bottom: 0, left: 0 };
@@ -205,27 +206,27 @@ export const propsCreators: Record<string, ADCreatorFn> = {
205
206
  },
206
207
  paddingTop: (props: { readonly paddingTop: string }): unknown => {
207
208
  const allProps = props as Record<string, unknown>;
208
- const padding: { top?: number, bottom?: number, left?: number, right?: number } = allProps.padding ?? {};
209
+ const padding: { top?: number; bottom?: number; left?: number; right?: number } = allProps.padding ?? {};
209
210
  padding.top = Number(props.paddingTop);
210
- return padding;
211
+ return padding;
211
212
  },
212
213
  paddingBottom: (props: { readonly paddingBottom: string }): unknown => {
213
214
  const allProps = props as Record<string, unknown>;
214
- const padding: { top?: number, bottom?: number, left?: number, right?: number } = allProps.padding ?? {};
215
+ const padding: { top?: number; bottom?: number; left?: number; right?: number } = allProps.padding ?? {};
215
216
  padding.bottom = Number(props.paddingBottom);
216
- return padding;
217
+ return padding;
217
218
  },
218
219
  paddingLeft: (props: { readonly paddingLeft: string }): unknown => {
219
220
  const allProps = props as Record<string, unknown>;
220
- const padding: { top?: number, bottom?: number, left?: number, right?: number } = allProps.padding ?? {};
221
+ const padding: { top?: number; bottom?: number; left?: number; right?: number } = allProps.padding ?? {};
221
222
  padding.left = Number(props.paddingLeft);
222
- return padding;
223
+ return padding;
223
224
  },
224
225
  paddingRight: (props: { readonly paddingRight: string }): unknown => {
225
226
  const allProps = props as Record<string, unknown>;
226
- const padding: { top?: number, bottom?: number, left?: number, right?: number } = allProps.padding ?? {};
227
+ const padding: { top?: number; bottom?: number; left?: number; right?: number } = allProps.padding ?? {};
227
228
  padding.right = Number(props.paddingRight);
228
- return padding;
229
+ return padding;
229
230
  },
230
231
  margins: (props: { readonly margins: string }): unknown => {
231
232
  const margins: { [k: string]: number } = { top: 0, right: 0, bottom: 0, left: 0 };
@@ -269,27 +270,27 @@ export const propsCreators: Record<string, ADCreatorFn> = {
269
270
  },
270
271
  marginTop: (props: { readonly marginTop: string }): unknown => {
271
272
  const allProps = props as Record<string, unknown>;
272
- const margins: { top?: number, bottom?: number, left?: number, right?: number } = allProps.margins ?? {};
273
+ const margins: { top?: number; bottom?: number; left?: number; right?: number } = allProps.margins ?? {};
273
274
  margins.top = Number(props.marginTop);
274
- return margins;
275
+ return margins;
275
276
  },
276
277
  marginBottom: (props: { readonly marginBottom: string }): unknown => {
277
278
  const allProps = props as Record<string, unknown>;
278
- const margins: { top?: number, bottom?: number, left?: number, right?: number } = allProps.margins ?? {};
279
+ const margins: { top?: number; bottom?: number; left?: number; right?: number } = allProps.margins ?? {};
279
280
  margins.bottom = Number(props.marginBottom);
280
- return margins;
281
+ return margins;
281
282
  },
282
283
  marginLeft: (props: { readonly marginLeft: string }): unknown => {
283
284
  const allProps = props as Record<string, unknown>;
284
- const margins: { top?: number, bottom?: number, left?: number, right?: number } = allProps.margins ?? {};
285
+ const margins: { top?: number; bottom?: number; left?: number; right?: number } = allProps.margins ?? {};
285
286
  margins.left = Number(props.marginLeft);
286
- return margins;
287
+ return margins;
287
288
  },
288
289
  marginRight: (props: { readonly marginRight: string }): unknown => {
289
290
  const allProps = props as Record<string, unknown>;
290
- const margins: { top?: number, bottom?: number, left?: number, right?: number } = allProps.margins ?? {};
291
+ const margins: { top?: number; bottom?: number; left?: number; right?: number } = allProps.margins ?? {};
291
292
  margins.right = Number(props.marginRight);
292
- return margins;
293
+ return margins;
293
294
  },
294
295
  borderColors: (props: { readonly borderColors: string }): unknown => {
295
296
  const borderColors: { [k: string]: string } = { top: "", right: "", bottom: "", left: "" };
@@ -315,27 +316,27 @@ export const propsCreators: Record<string, ADCreatorFn> = {
315
316
  },
316
317
  borderColorTop: (props: { readonly borderColorTop: string }): unknown => {
317
318
  const allProps = props as Record<string, unknown>;
318
- const margins: { top?: string, bottom?: string, left?: string, right?: string } = allProps.borderColors ?? {};
319
+ const margins: { top?: string; bottom?: string; left?: string; right?: string } = allProps.borderColors ?? {};
319
320
  margins.top = props.borderColorTop;
320
- return margins;
321
+ return margins;
321
322
  },
322
323
  borderColorBottom: (props: { readonly borderColorBottom: string }): unknown => {
323
324
  const allProps = props as Record<string, unknown>;
324
- const boderColors: { top?: string, bottom?: string, left?: string, right?: string } = allProps.borderColors ?? {};
325
+ const boderColors: { top?: string; bottom?: string; left?: string; right?: string } = allProps.borderColors ?? {};
325
326
  boderColors.bottom = props.borderColorBottom;
326
- return boderColors;
327
+ return boderColors;
327
328
  },
328
329
  borderColorLeft: (props: { readonly borderColorLeft: string }): unknown => {
329
330
  const allProps = props as Record<string, unknown>;
330
- const boderColors: { top?: string, bottom?: string, left?: string, right?: string } = allProps.borderColors ?? {};
331
+ const boderColors: { top?: string; bottom?: string; left?: string; right?: string } = allProps.borderColors ?? {};
331
332
  boderColors.left = props.borderColorLeft;
332
- return boderColors;
333
+ return boderColors;
333
334
  },
334
335
  borderColorRight: (props: { readonly borderColorRight: string }): unknown => {
335
336
  const allProps = props as Record<string, unknown>;
336
- const boderColors: { top?: string, bottom?: string, left?: string, right?: string } = allProps.borderColors ?? {};
337
+ const boderColors: { top?: string; bottom?: string; left?: string; right?: string } = allProps.borderColors ?? {};
337
338
  boderColors.right = props.borderColorRight;
338
- return boderColors;
339
+ return boderColors;
339
340
  },
340
341
  };
341
342
 
@@ -1,3 +1,4 @@
1
+ import { ImageProps } from "../../abstract-document/atoms/image.js";
1
2
  import {
2
3
  TextStyle,
3
4
  ParagraphStyle,
@@ -110,9 +111,20 @@ export type ImageCellProps = Omit<ImageParagraphProps, "style"> & {
110
111
  };
111
112
 
112
113
  export function ImageCell(props: ImageCellProps, styleNameTypes: Record<string, string>): TableCell.TableCell {
113
- const { imageResource, width, height, paragraphStyle, style, columnSpan, rowSpan } = props;
114
+ const {
115
+ imageResource,
116
+ width,
117
+ height,
118
+ horizontalAlignment,
119
+ verticalAlignment,
120
+ paragraphStyle,
121
+ style,
122
+ columnSpan,
123
+ rowSpan,
124
+ } = props;
114
125
  const styleNames = extractStyleNames(props.styleNames, styleNameTypes);
115
- const imageElement = imageResource && Image.create({ imageResource, width, height });
126
+ const imageElement =
127
+ imageResource && Image.create({ imageResource, width, height, horizontalAlignment, verticalAlignment });
116
128
  const pararaphProps = {
117
129
  style: paragraphStyle ? { ...paragraphStyle, type: "ParagraphStyle" } : undefined,
118
130
  styleName: styleNames.ParagraphStyle,
@@ -123,10 +135,7 @@ export function ImageCell(props: ImageCellProps, styleNameTypes: Record<string,
123
135
  return TableCell.create({ columnSpan, rowSpan, style, styleName: styleNames.TableCellStyle }, [paragraph]);
124
136
  }
125
137
 
126
- export type ImageParagraphProps = {
127
- readonly imageResource: ImageResource.ImageResource;
128
- readonly width: number;
129
- readonly height: number;
138
+ export type ImageParagraphProps = ImageProps & {
130
139
  readonly style?: ParagraphStyle.ParagraphStyle;
131
140
  readonly styleNames?: string;
132
141
  };
@@ -135,9 +144,10 @@ export function ImageParagraph(
135
144
  props: ImageParagraphProps,
136
145
  styleNameTypes: Record<string, string>
137
146
  ): Paragraph.Paragraph | undefined {
138
- const { imageResource, width, height, style } = props;
147
+ const { imageResource, width, height, style, horizontalAlignment, verticalAlignment } = props;
139
148
  const styleNames = extractStyleNames(props.styleNames, styleNameTypes);
140
- const imageElement = imageResource && Image.create({ imageResource, width, height });
149
+ const imageElement =
150
+ imageResource && Image.create({ imageResource, width, height, horizontalAlignment, verticalAlignment });
141
151
  const pararaphProps = { style, styleName: styleNames.ParagraphStyle };
142
152
  return imageElement
143
153
  ? Paragraph.create(pararaphProps, [imageElement])
@@ -212,6 +212,24 @@ export const image = `<xs:complexType name="Image">
212
212
  <xs:attribute name="src" type="xs:string" use="required" />
213
213
  <xs:attribute name="width" type="xs:decimal" use="required" />
214
214
  <xs:attribute name="height" type="xs:decimal" use="required" />
215
+ <xs:attribute name="verticalAlignment">
216
+ <xs:simpleType>
217
+ <xs:restriction base="xs:string">
218
+ <xs:enumeration value="Top" />
219
+ <xs:enumeration value="Center" />
220
+ <xs:enumeration value="Bottom" />
221
+ </xs:restriction>
222
+ </xs:simpleType>
223
+ </xs:attribute>
224
+ <xs:attribute name="horizontalAlignment">
225
+ <xs:simpleType>
226
+ <xs:restriction base="xs:string">
227
+ <xs:enumeration value="Left" />
228
+ <xs:enumeration value="Center" />
229
+ <xs:enumeration value="Right" />
230
+ </xs:restriction>
231
+ </xs:simpleType>
232
+ </xs:attribute>
215
233
  </xs:complexType>`;
216
234
 
217
235
  export const pageBreak = `<xs:complexType name="PageBreak" />`;