@contrail/document-generation 2.0.9 → 2.0.11

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.
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ComponentGenerator = void 0;
4
4
  const documents_1 = require("@contrail/documents");
5
5
  const nanoid_1 = require("nanoid");
6
- const util_1 = require("@contrail/util");
7
6
  const document_property_util_1 = require("../util/document-property-util");
8
7
  const component_util_1 = require("./component-util");
9
8
  class ComponentGenerator {
@@ -25,16 +24,8 @@ class ComponentGenerator {
25
24
  }
26
25
  let yPosition = 0;
27
26
  const imageDimension = template.imageDimension || {};
28
- const imageHeight = imageDimension.height || imageDimension.width || 125;
29
27
  const imageWidth = imageDimension.width || 125;
30
- const imageUrl = util_1.ObjectUtil.getByPath(data, `viewable.mediumViewableDownloadUrl`);
31
- const elements = [{
32
- type: "image",
33
- size: { width: imageWidth, height: imageHeight },
34
- propertyBindings: { url: 'viewable.mediumViewableDownloadUrl' },
35
- url: imageUrl,
36
- }];
37
- yPosition += imageHeight;
28
+ const elements = [];
38
29
  const textOptions = {
39
30
  style: {
40
31
  text: {
@@ -45,7 +36,7 @@ class ComponentGenerator {
45
36
  width: imageWidth
46
37
  }
47
38
  };
48
- elements.push(...document_property_util_1.DocumentPropertyUtil.generatePropertyTextElements(data, template.properties, textOptions, { y: yPosition }, true));
39
+ elements.push(...document_property_util_1.DocumentPropertyUtil.generatePropertyElements(data, template.properties, textOptions, { y: yPosition }, imageDimension, true));
49
40
  const modelBindings = component_util_1.ComponentUtil.generatedModelBindings(data);
50
41
  componentElement.elements = elements;
51
42
  componentElement.modelBindings = modelBindings;
@@ -44,7 +44,7 @@ class InfoPanelGenerator {
44
44
  },
45
45
  modelBindings,
46
46
  };
47
- const textElements = document_property_util_1.DocumentPropertyUtil.generatePropertyTextElements(data, template.propertyTemplate.properties, textOptions, { x: panel.position.x, y: panel.position.y }, false);
47
+ const textElements = document_property_util_1.DocumentPropertyUtil.generatePropertyElements(data, template.propertyTemplate.properties, textOptions, { x: panel.position.x, y: panel.position.y }, {}, false);
48
48
  elements.push(...textElements);
49
49
  return elements;
50
50
  }
@@ -53,4 +53,5 @@ export interface DocumentPropertyDefinition {
53
53
  slug: string;
54
54
  typeRootSlug: string;
55
55
  includeLabel?: boolean;
56
+ isHidden?: boolean;
56
57
  }
@@ -1,7 +1,8 @@
1
1
  import { DocumentPropertyDefinition } from "../interfaces";
2
- import { DocumentElement, PositionDefinition } from "@contrail/documents";
2
+ import { DocumentElement, PositionDefinition, SizeDefinition } from "@contrail/documents";
3
3
  import { PropertyValueFormatter } from "@contrail/types";
4
4
  export declare class DocumentPropertyUtil {
5
- static generatePropertyTextElements(data: any, properties: Array<DocumentPropertyDefinition>, options: DocumentElement, startingPosition: PositionDefinition, isComponent?: boolean): Array<DocumentElement>;
5
+ static generatePropertyElements(data: any, properties: Array<DocumentPropertyDefinition>, options: DocumentElement, startingPosition: PositionDefinition, imageDimension: SizeDefinition, isComponent?: boolean): Array<DocumentElement>;
6
6
  static generatePropertyTextElement(model: any, property: DocumentPropertyDefinition, options: DocumentElement, formatter: PropertyValueFormatter, position: PositionDefinition, isComponent?: boolean): DocumentElement;
7
+ static generatePropertyImageElement(data: any, property: DocumentPropertyDefinition, imageDimension: SizeDefinition, position: PositionDefinition): DocumentElement;
7
8
  }
@@ -4,23 +4,33 @@ exports.DocumentPropertyUtil = void 0;
4
4
  const util_1 = require("@contrail/util");
5
5
  const types_1 = require("@contrail/types");
6
6
  const document_text_element_util_1 = require("./document-text-element-util");
7
+ const text_util_1 = require("./text-util");
7
8
  class DocumentPropertyUtil {
8
- static generatePropertyTextElements(data, properties, options, startingPosition, isComponent = false) {
9
+ static generatePropertyElements(data, properties, options, startingPosition, imageDimension, isComponent = false) {
9
10
  var _a, _b;
10
11
  let yPosition = startingPosition.y || 0;
11
12
  const xPosition = startingPosition.x || 0;
12
13
  const elements = [];
13
14
  const formatter = new types_1.PropertyValueFormatter();
14
15
  for (let property of properties) {
15
- const fontSize = ((_b = (_a = property.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.size) || 12;
16
- const PT_TO_PX = 1.3;
17
- const LINE_HEIGHT = 1.4;
18
- const PADDING = 1.6;
19
- let height = fontSize * PT_TO_PX * LINE_HEIGHT * PADDING;
20
- options.size = options.size || {};
21
- options.size.height = height;
22
- let element = DocumentPropertyUtil.generatePropertyTextElement(data, property, options, formatter, { x: xPosition, y: yPosition }, isComponent);
23
- yPosition += height;
16
+ let element;
17
+ if (property.propertyDefinition) {
18
+ const fontSize = ((_b = (_a = property.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.size) || 12;
19
+ const PT_TO_PX = 1.3;
20
+ const LINE_HEIGHT = 1.4;
21
+ const PADDING = 1.6;
22
+ let height = fontSize * PT_TO_PX * LINE_HEIGHT * PADDING;
23
+ options.size = options.size || {};
24
+ options.size.height = height;
25
+ element = DocumentPropertyUtil.generatePropertyTextElement(data, property, options, formatter, { x: xPosition, y: yPosition }, isComponent);
26
+ yPosition += element.size.height - 10;
27
+ }
28
+ else {
29
+ const imageHeight = imageDimension.height || imageDimension.width || 125;
30
+ const imageWidth = imageDimension.width || 125;
31
+ element = DocumentPropertyUtil.generatePropertyImageElement(data, property, { width: imageWidth, height: imageHeight }, { x: xPosition, y: yPosition });
32
+ yPosition += imageHeight;
33
+ }
24
34
  elements.push(element);
25
35
  }
26
36
  return elements;
@@ -30,6 +40,10 @@ class DocumentPropertyUtil {
30
40
  const modelKey = `${util_1.StringUtil.convertToCamelCase(property.typeRootSlug)}.${property.propertyDefinition.slug}`;
31
41
  const value = util_1.ObjectUtil.getByPath(model, modelKey);
32
42
  let display = formatter.formatValueForProperty(value, property.propertyDefinition);
43
+ if (!isComponent) {
44
+ const height = text_util_1.TextUtil.getHeightByTextWidth(display, property, options.size.width);
45
+ options.size.height = height;
46
+ }
33
47
  if (!isComponent && property.includeLabel) {
34
48
  display = property.propertyDefinition.label + ': ' + display;
35
49
  }
@@ -43,5 +57,19 @@ class DocumentPropertyUtil {
43
57
  element.propertyBindings = { text: modelKey };
44
58
  return element;
45
59
  }
60
+ static generatePropertyImageElement(data, property, imageDimension, position) {
61
+ const imageUrl = util_1.ObjectUtil.getByPath(data, `viewable.mediumViewableDownloadUrl`);
62
+ const element = {
63
+ type: "image",
64
+ size: imageDimension,
65
+ propertyBindings: { url: 'viewable.mediumViewableDownloadUrl' },
66
+ url: imageUrl,
67
+ position
68
+ };
69
+ if (property.isHidden) {
70
+ element.isHidden = property.isHidden;
71
+ }
72
+ return element;
73
+ }
46
74
  }
47
75
  exports.DocumentPropertyUtil = DocumentPropertyUtil;
@@ -0,0 +1,4 @@
1
+ import { DocumentPropertyDefinition } from "../interfaces";
2
+ export declare class TextUtil {
3
+ static getHeightByTextWidth(text: string, property: DocumentPropertyDefinition, width: number): number;
4
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TextUtil = void 0;
4
+ class TextUtil {
5
+ static getHeightByTextWidth(text, property, width) {
6
+ var _a, _b, _c, _d;
7
+ let height = 0;
8
+ let div = document.createElement('DIV');
9
+ div.id = '__textMeasure';
10
+ div.innerHTML = text;
11
+ div.style.position = 'absolute';
12
+ div.style.top = '-500px';
13
+ div.style.left = '0';
14
+ div.style.width = width + 'px';
15
+ div.style.lineHeight = 'normal';
16
+ div.style.fontFamily = ((_b = (_a = property.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.family) || 'Roboto';
17
+ div.style.fontWeight = property.style.font.weight ? 'bold' : 'normal';
18
+ div.style.fontSize = (((_d = (_c = property.style) === null || _c === void 0 ? void 0 : _c.font) === null || _d === void 0 ? void 0 : _d.size) || 12) + 'pt';
19
+ div.style.padding = '11px';
20
+ document.body.appendChild(div);
21
+ height = div.offsetHeight + 5;
22
+ document.body.removeChild(div);
23
+ return height;
24
+ }
25
+ }
26
+ exports.TextUtil = TextUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/document-generation",
3
- "version": "2.0.9",
3
+ "version": "2.0.11",
4
4
  "description": "Utilities for automatic generation of documents.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@contrail/data-grouping": "^1.0.20",
46
- "@contrail/documents": "^1.0.44",
46
+ "@contrail/documents": "^1.0.59",
47
47
  "@contrail/types": "^3.0.27",
48
48
  "@contrail/util": "^1.0.27"
49
49
  }