@contrail/document-generation 2.0.44 → 2.0.45

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 (45) hide show
  1. package/README.md +2 -2
  2. package/lib/board-document-generator.d.ts +23 -23
  3. package/lib/board-document-generator.js +241 -227
  4. package/lib/components/component-generator.d.ts +6 -6
  5. package/lib/components/component-generator.js +55 -55
  6. package/lib/components/component-grid-generator.d.ts +7 -7
  7. package/lib/components/component-grid-generator.js +84 -84
  8. package/lib/components/component-util.d.ts +5 -5
  9. package/lib/components/component-util.js +43 -43
  10. package/lib/document-generator.d.ts +6 -6
  11. package/lib/document-generator.js +11 -11
  12. package/lib/frames/frame-generator.d.ts +12 -12
  13. package/lib/frames/frame-generator.js +141 -135
  14. package/lib/frames/frame.d.ts +16 -16
  15. package/lib/frames/frame.js +67 -67
  16. package/lib/frames/index.d.ts +2 -2
  17. package/lib/frames/index.js +18 -18
  18. package/lib/index.d.ts +6 -6
  19. package/lib/index.js +22 -22
  20. package/lib/info-panel/info-panel-generator.d.ts +6 -6
  21. package/lib/info-panel/info-panel-generator.js +56 -52
  22. package/lib/interfaces.d.ts +72 -72
  23. package/lib/interfaces.js +8 -8
  24. package/lib/scripts/test-board.d.ts +1 -1
  25. package/lib/scripts/test-board.js +230 -230
  26. package/lib/scripts/test-showcase.d.ts +1 -1
  27. package/lib/scripts/test-showcase.js +257 -257
  28. package/lib/showcase-frame-generator.d.ts +8 -8
  29. package/lib/showcase-frame-generator.js +44 -44
  30. package/lib/test-data.d.ts +72 -72
  31. package/lib/test-data.js +187 -187
  32. package/lib/util/document-dynamic-text-element-util.d.ts +8 -8
  33. package/lib/util/document-dynamic-text-element-util.js +71 -71
  34. package/lib/util/document-property-util.d.ts +9 -9
  35. package/lib/util/document-property-util.js +104 -101
  36. package/lib/util/document-text-element-util.d.ts +10 -10
  37. package/lib/util/document-text-element-util.js +124 -124
  38. package/lib/util/document-util.d.ts +8 -8
  39. package/lib/util/document-util.js +22 -22
  40. package/lib/util/index.d.ts +2 -2
  41. package/lib/util/index.js +18 -18
  42. package/lib/util/text-util.d.ts +4 -4
  43. package/lib/util/text-util.js +28 -28
  44. package/package.json +50 -50
  45. package/testdocument.json +25972 -25972
@@ -1,71 +1,71 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DocumentDynamicTextElementUtil = void 0;
4
- const documents_1 = require("@contrail/documents");
5
- const util_1 = require("@contrail/util");
6
- const document_text_element_util_1 = require("./document-text-element-util");
7
- const HARD_CODED_PROPS = {
8
- createdBy: 'Created by',
9
- updatedBy: 'Updated by',
10
- count: 'Count',
11
- };
12
- class DocumentDynamicTextElementUtil {
13
- static isDynamicText(element) {
14
- return element.type === 'text' && element.propertyBindings && element.propertyBindingsMetaData;
15
- }
16
- static handleDynamicText(element, dataGroup) {
17
- var _a;
18
- const displayFunction = element.propertyBindingsMetaData.displayFunction;
19
- const displayLabel = element.propertyBindingsMetaData.displayLabel;
20
- const propertyBindingsText = element.propertyBindings.text;
21
- let text;
22
- const entityType = propertyBindingsText.split('.')[0];
23
- const propertySlug = propertyBindingsText.split('.')[1];
24
- let property;
25
- let type;
26
- if (!['showcase', 'board', 'project'].includes(entityType)) {
27
- if (entityType === 'frame') {
28
- if (propertySlug === 'count') {
29
- text = dataGroup.data.length;
30
- }
31
- else {
32
- text = dataGroup.name;
33
- }
34
- }
35
- else {
36
- if (dataGroup.data.length === 0) {
37
- return propertyBindingsText;
38
- }
39
- type = this.typeMap[entityType];
40
- property = type.typeProperties.find((p) => p.slug === propertySlug);
41
- if (displayFunction === documents_1.DynamicTextDisplayFunction.LABEL) {
42
- text = (property === null || property === void 0 ? void 0 : property.label) || HARD_CODED_PROPS[propertySlug] || `${type.label} name`;
43
- }
44
- else {
45
- const elementValues = this.getPropertyValuesForElements(dataGroup.data, propertyBindingsText);
46
- text = documents_1.DynamicTextUtil.getTextFromFrameMemberValues(displayFunction, property, elementValues);
47
- }
48
- }
49
- if (text === undefined || text === null) {
50
- text = 'n/a';
51
- }
52
- if (displayLabel && displayFunction !== documents_1.DynamicTextDisplayFunction.LABEL) {
53
- text = `${(property === null || property === void 0 ? void 0 : property.label) || `${type === null || type === void 0 ? void 0 : type.label} name` || HARD_CODED_PROPS[propertySlug]}: ${text}`;
54
- }
55
- }
56
- if (((_a = element.text) === null || _a === void 0 ? void 0 : _a.toString().indexOf('</')) > -1) {
57
- const styleDefinition = document_text_element_util_1.DocumentTextElementUtil.extractStyleFromInline(element.text);
58
- text = document_text_element_util_1.DocumentTextElementUtil.applyInLineStyling(text, styleDefinition);
59
- }
60
- element.text = text;
61
- }
62
- static getPropertyValuesForElements(elements, propertyKey) {
63
- const values = [];
64
- elements.forEach((el) => {
65
- const propertyValue = util_1.ObjectUtil.getByPath(el, propertyKey);
66
- values.push(propertyValue);
67
- });
68
- return values;
69
- }
70
- }
71
- exports.DocumentDynamicTextElementUtil = DocumentDynamicTextElementUtil;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentDynamicTextElementUtil = void 0;
4
+ const documents_1 = require("@contrail/documents");
5
+ const util_1 = require("@contrail/util");
6
+ const document_text_element_util_1 = require("./document-text-element-util");
7
+ const HARD_CODED_PROPS = {
8
+ createdBy: 'Created by',
9
+ updatedBy: 'Updated by',
10
+ count: 'Count',
11
+ };
12
+ class DocumentDynamicTextElementUtil {
13
+ static isDynamicText(element) {
14
+ return element.type === 'text' && element.propertyBindings && element.propertyBindingsMetaData;
15
+ }
16
+ static handleDynamicText(element, dataGroup) {
17
+ var _a;
18
+ const displayFunction = element.propertyBindingsMetaData.displayFunction;
19
+ const displayLabel = element.propertyBindingsMetaData.displayLabel;
20
+ const propertyBindingsText = element.propertyBindings.text;
21
+ let text;
22
+ const entityType = propertyBindingsText.split('.')[0];
23
+ const propertySlug = propertyBindingsText.split('.')[1];
24
+ let property;
25
+ let type;
26
+ if (!['showcase', 'board', 'project'].includes(entityType)) {
27
+ if (entityType === 'frame') {
28
+ if (propertySlug === 'count') {
29
+ text = dataGroup.data.length;
30
+ }
31
+ else {
32
+ text = dataGroup.name;
33
+ }
34
+ }
35
+ else {
36
+ if (dataGroup.data.length === 0) {
37
+ return propertyBindingsText;
38
+ }
39
+ type = this.typeMap[entityType];
40
+ property = type.typeProperties.find((p) => p.slug === propertySlug);
41
+ if (displayFunction === documents_1.DynamicTextDisplayFunction.LABEL) {
42
+ text = (property === null || property === void 0 ? void 0 : property.label) || HARD_CODED_PROPS[propertySlug] || `${type.label} name`;
43
+ }
44
+ else {
45
+ const elementValues = this.getPropertyValuesForElements(dataGroup.data, propertyBindingsText);
46
+ text = documents_1.DynamicTextUtil.getTextFromFrameMemberValues(displayFunction, property, elementValues);
47
+ }
48
+ }
49
+ if (text === undefined || text === null) {
50
+ text = 'n/a';
51
+ }
52
+ if (displayLabel && displayFunction !== documents_1.DynamicTextDisplayFunction.LABEL) {
53
+ text = `${(property === null || property === void 0 ? void 0 : property.label) || `${type === null || type === void 0 ? void 0 : type.label} name` || HARD_CODED_PROPS[propertySlug]}: ${text}`;
54
+ }
55
+ }
56
+ if (((_a = element.text) === null || _a === void 0 ? void 0 : _a.toString().indexOf('</')) > -1) {
57
+ const styleDefinition = document_text_element_util_1.DocumentTextElementUtil.extractStyleFromInline(element.text);
58
+ text = document_text_element_util_1.DocumentTextElementUtil.applyInLineStyling(text, styleDefinition);
59
+ }
60
+ element.text = text;
61
+ }
62
+ static getPropertyValuesForElements(elements, propertyKey) {
63
+ const values = [];
64
+ elements.forEach((el) => {
65
+ const propertyValue = util_1.ObjectUtil.getByPath(el, propertyKey);
66
+ values.push(propertyValue);
67
+ });
68
+ return values;
69
+ }
70
+ }
71
+ exports.DocumentDynamicTextElementUtil = DocumentDynamicTextElementUtil;
@@ -1,9 +1,9 @@
1
- import { DocumentPropertyDefinition } from '../interfaces';
2
- import { DocumentElement, PositionDefinition, SizeDefinition } from '@contrail/documents';
3
- import { PropertyValueFormatter } from '@contrail/types';
4
- export declare class DocumentPropertyUtil {
5
- static generatePropertyElements(data: any, properties: Array<DocumentPropertyDefinition>, options: DocumentElement, startingPosition: PositionDefinition, imageDimension: SizeDefinition, isComponent?: boolean): Array<DocumentElement>;
6
- static generatePropertyTextElement(model: any, property: DocumentPropertyDefinition, options: DocumentElement, formatter: PropertyValueFormatter, position: PositionDefinition, isComponent?: boolean): DocumentElement;
7
- static generatePropertyAnnotationElement(data: any, property: DocumentPropertyDefinition, size: SizeDefinition, position: PositionDefinition): DocumentElement;
8
- static generatePropertyImageElement(data: any, property: DocumentPropertyDefinition, imageDimension: SizeDefinition, position: PositionDefinition): DocumentElement;
9
- }
1
+ import { DocumentPropertyDefinition } from '../interfaces';
2
+ import { DocumentElement, PositionDefinition, SizeDefinition } from '@contrail/documents';
3
+ import { PropertyValueFormatter } from '@contrail/types';
4
+ export declare class DocumentPropertyUtil {
5
+ static generatePropertyElements(data: any, properties: Array<DocumentPropertyDefinition>, options: DocumentElement, startingPosition: PositionDefinition, imageDimension: SizeDefinition, isComponent?: boolean): Array<DocumentElement>;
6
+ static generatePropertyTextElement(model: any, property: DocumentPropertyDefinition, options: DocumentElement, formatter: PropertyValueFormatter, position: PositionDefinition, isComponent?: boolean): DocumentElement;
7
+ static generatePropertyAnnotationElement(data: any, property: DocumentPropertyDefinition, size: SizeDefinition, position: PositionDefinition): DocumentElement;
8
+ static generatePropertyImageElement(data: any, property: DocumentPropertyDefinition, imageDimension: SizeDefinition, position: PositionDefinition): DocumentElement;
9
+ }
@@ -1,101 +1,104 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DocumentPropertyUtil = void 0;
4
- const util_1 = require("@contrail/util");
5
- const documents_1 = require("@contrail/documents");
6
- const types_1 = require("@contrail/types");
7
- const document_text_element_util_1 = require("./document-text-element-util");
8
- const text_util_1 = require("./text-util");
9
- class DocumentPropertyUtil {
10
- static generatePropertyElements(data, properties, options, startingPosition, imageDimension, isComponent = false) {
11
- var _a, _b;
12
- let yPosition = startingPosition.y || 0;
13
- const xPosition = startingPosition.x || 0;
14
- const elements = [];
15
- const formatter = new types_1.PropertyValueFormatter();
16
- let index = 0;
17
- for (let property of properties) {
18
- let element;
19
- if (index === 0 && property.slug === 'annotation') {
20
- yPosition = -15;
21
- }
22
- if (property.propertyDefinition) {
23
- const fontSize = ((_b = (_a = property.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.size) || 12;
24
- const PT_TO_PX = 1.3;
25
- const LINE_HEIGHT = 1.4;
26
- const PADDING = 1.6;
27
- let height = fontSize * PT_TO_PX * LINE_HEIGHT * PADDING;
28
- options.size = options.size || {};
29
- options.size.height = height;
30
- element = DocumentPropertyUtil.generatePropertyTextElement(data, property, options, formatter, { x: xPosition, y: yPosition }, isComponent);
31
- yPosition += element.size.height - 10;
32
- }
33
- else if (property.slug === 'annotation') {
34
- element = DocumentPropertyUtil.generatePropertyAnnotationElement(data, property, property.size, {
35
- x: xPosition,
36
- y: yPosition,
37
- });
38
- yPosition += element.size.height - 10;
39
- }
40
- else {
41
- const imageHeight = imageDimension.height || imageDimension.width || 125;
42
- const imageWidth = imageDimension.width || 125;
43
- element = DocumentPropertyUtil.generatePropertyImageElement(data, property, { width: imageWidth, height: imageHeight }, { x: xPosition, y: yPosition });
44
- yPosition += imageHeight;
45
- }
46
- elements.push(element);
47
- index++;
48
- }
49
- return elements;
50
- }
51
- static generatePropertyTextElement(model, property, options, formatter, position, isComponent = false) {
52
- var _a;
53
- const modelKey = `${util_1.StringUtil.convertToCamelCase(property.typeRootSlug)}.${property.propertyDefinition.slug}`;
54
- const value = util_1.ObjectUtil.getByPath(model, modelKey);
55
- let display = formatter.formatValueForProperty(value, property.propertyDefinition);
56
- if (!isComponent) {
57
- const height = text_util_1.TextUtil.getHeightByTextWidth(display, property, options.size.width);
58
- options.size.height = height;
59
- }
60
- if (!isComponent && property.includeLabel) {
61
- display = property.propertyDefinition.label + ': ' + display;
62
- }
63
- const mergedOptions = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(options), { style: property.style || {} });
64
- mergedOptions.position = position;
65
- const useInlineFormatting = isComponent === false;
66
- let element = document_text_element_util_1.DocumentTextElementUtil.generateTextElement(display, mergedOptions, useInlineFormatting, !isComponent);
67
- if (property.includeLabel) {
68
- element.label = (_a = property.propertyDefinition) === null || _a === void 0 ? void 0 : _a.label;
69
- }
70
- element.propertyBindings = { text: modelKey };
71
- return element;
72
- }
73
- static generatePropertyAnnotationElement(data, property, size, position) {
74
- const element = {
75
- type: 'annotation',
76
- size: size,
77
- propertyBindings: { annotation: 'annotation' },
78
- position,
79
- };
80
- if (property.isHidden) {
81
- element.isHidden = property.isHidden;
82
- }
83
- return element;
84
- }
85
- static generatePropertyImageElement(data, property, imageDimension, position) {
86
- const imageUrl = util_1.ObjectUtil.getByPath(data, `viewable.mediumViewableDownloadUrl`);
87
- const element = {
88
- type: 'image',
89
- size: imageDimension,
90
- propertyBindings: { url: 'viewable.mediumViewableDownloadUrl' },
91
- url: imageUrl,
92
- position,
93
- };
94
- if (property.isHidden) {
95
- element.isHidden = property.isHidden;
96
- }
97
- documents_1.DocumentElementPropertyBindingHandler.bindPropertiesToElement(element, data);
98
- return element;
99
- }
100
- }
101
- exports.DocumentPropertyUtil = DocumentPropertyUtil;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentPropertyUtil = void 0;
4
+ const util_1 = require("@contrail/util");
5
+ const documents_1 = require("@contrail/documents");
6
+ const types_1 = require("@contrail/types");
7
+ const document_text_element_util_1 = require("./document-text-element-util");
8
+ const text_util_1 = require("./text-util");
9
+ class DocumentPropertyUtil {
10
+ static generatePropertyElements(data, properties, options, startingPosition, imageDimension, isComponent = false) {
11
+ var _a, _b;
12
+ let yPosition = startingPosition.y || 0;
13
+ const xPosition = startingPosition.x || 0;
14
+ const elements = [];
15
+ const formatter = new types_1.PropertyValueFormatter();
16
+ let index = 0;
17
+ for (let property of properties) {
18
+ let element;
19
+ if (index === 0 && property.slug === 'annotation') {
20
+ yPosition = -15;
21
+ }
22
+ if (property.propertyDefinition) {
23
+ const fontSize = ((_b = (_a = property.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.size) || 12;
24
+ const PT_TO_PX = 1.3;
25
+ const LINE_HEIGHT = 1.4;
26
+ const PADDING = 1.6;
27
+ let height = fontSize * PT_TO_PX * LINE_HEIGHT * PADDING;
28
+ options.size = options.size || {};
29
+ options.size.height = height;
30
+ element = DocumentPropertyUtil.generatePropertyTextElement(data, property, options, formatter, { x: xPosition, y: yPosition }, isComponent);
31
+ yPosition += element.size.height - 10;
32
+ }
33
+ else if (property.slug === 'annotation') {
34
+ element = DocumentPropertyUtil.generatePropertyAnnotationElement(data, property, property.size, {
35
+ x: xPosition,
36
+ y: yPosition,
37
+ });
38
+ yPosition += element.size.height - 10;
39
+ }
40
+ else {
41
+ const imageHeight = imageDimension.height || imageDimension.width || 125;
42
+ const imageWidth = imageDimension.width || 125;
43
+ element = DocumentPropertyUtil.generatePropertyImageElement(data, property, { width: imageWidth, height: imageHeight }, { x: xPosition, y: yPosition });
44
+ yPosition += imageHeight;
45
+ }
46
+ elements.push(element);
47
+ index++;
48
+ }
49
+ return elements;
50
+ }
51
+ static generatePropertyTextElement(model, property, options, formatter, position, isComponent = false) {
52
+ var _a;
53
+ const rootSlug = util_1.StringUtil.convertToCamelCase(property.typeRootSlug);
54
+ const slug = property.propertyDefinition.slug;
55
+ const modelKey = `${rootSlug}.${slug}`;
56
+ const value = util_1.ObjectUtil.getBySlugs(model, rootSlug, slug);
57
+ let display = formatter.formatValueForProperty(value, property.propertyDefinition);
58
+ if (!isComponent) {
59
+ const height = text_util_1.TextUtil.getHeightByTextWidth(display, property, options.size.width);
60
+ options.size.height = height;
61
+ }
62
+ if (!isComponent && property.includeLabel) {
63
+ display = property.propertyDefinition.label + ': ' + display;
64
+ }
65
+ const mergedOptions = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(options), { style: property.style || {} });
66
+ mergedOptions.position = position;
67
+ const useInlineFormatting = isComponent === false;
68
+ let element = document_text_element_util_1.DocumentTextElementUtil.generateTextElement(display, mergedOptions, useInlineFormatting, !isComponent);
69
+ if (property.includeLabel) {
70
+ element.label = (_a = property.propertyDefinition) === null || _a === void 0 ? void 0 : _a.label;
71
+ }
72
+ element.propertyBindings = { text: modelKey };
73
+ return element;
74
+ }
75
+ static generatePropertyAnnotationElement(data, property, size, position) {
76
+ const element = {
77
+ type: 'annotation',
78
+ size: size,
79
+ propertyBindings: { annotation: 'annotation' },
80
+ position,
81
+ };
82
+ if (property.isHidden) {
83
+ element.isHidden = property.isHidden;
84
+ }
85
+ return element;
86
+ }
87
+ static generatePropertyImageElement(data, property, imageDimension, position) {
88
+ var _a;
89
+ const imageUrl = (_a = data === null || data === void 0 ? void 0 : data.viewable) === null || _a === void 0 ? void 0 : _a.mediumViewableDownloadUrl;
90
+ const element = {
91
+ type: 'image',
92
+ size: imageDimension,
93
+ propertyBindings: { url: 'viewable.mediumViewableDownloadUrl' },
94
+ url: imageUrl,
95
+ position,
96
+ };
97
+ if (property.isHidden) {
98
+ element.isHidden = property.isHidden;
99
+ }
100
+ documents_1.DocumentElementPropertyBindingHandler.bindPropertiesToElement(element, data);
101
+ return element;
102
+ }
103
+ }
104
+ exports.DocumentPropertyUtil = DocumentPropertyUtil;
@@ -1,10 +1,10 @@
1
- import { DocumentElement, StyleDefinition } from '@contrail/documents';
2
- export declare class DocumentTextElementUtil {
3
- static generateTextElement(textValue: any, options: any, applyInlineFormatting?: boolean, sanitizeHTML?: boolean): DocumentElement;
4
- static applyInLineStyling(text: any, style: StyleDefinition): any;
5
- static extractStyleFromInline(text: string): StyleDefinition;
6
- static extractFontSizeFromHTML(text: any): number;
7
- static extractColorFromHTML(text: any): string;
8
- static extractTextDecorationFromHTML(text: any): string;
9
- static extractFontFromHTML(text: any): any;
10
- }
1
+ import { DocumentElement, StyleDefinition } from '@contrail/documents';
2
+ export declare class DocumentTextElementUtil {
3
+ static generateTextElement(textValue: any, options: any, applyInlineFormatting?: boolean, sanitizeHTML?: boolean): DocumentElement;
4
+ static applyInLineStyling(text: any, style: StyleDefinition): any;
5
+ static extractStyleFromInline(text: string): StyleDefinition;
6
+ static extractFontSizeFromHTML(text: any): number;
7
+ static extractColorFromHTML(text: any): string;
8
+ static extractTextDecorationFromHTML(text: any): string;
9
+ static extractFontFromHTML(text: any): any;
10
+ }
@@ -1,124 +1,124 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DocumentTextElementUtil = void 0;
4
- const documents_1 = require("@contrail/documents");
5
- const util_1 = require("@contrail/util");
6
- const document_util_1 = require("./document-util");
7
- class DocumentTextElementUtil {
8
- static generateTextElement(textValue, options, applyInlineFormatting = true, sanitizeHTML = true) {
9
- var _a;
10
- const defaultStyle = {
11
- font: {
12
- size: 8,
13
- },
14
- color: 'rgba(0,0,0,.5)',
15
- };
16
- let style = util_1.ObjectUtil.mergeDeep(defaultStyle, options.style || {});
17
- if ((_a = options === null || options === void 0 ? void 0 : options.style) === null || _a === void 0 ? void 0 : _a.text) {
18
- style.text = options.style.text;
19
- }
20
- let position = options.position || { y: 0, x: 0 };
21
- position.x = position.x || 0;
22
- position.y = position.y || 0;
23
- let size = options.size || { height: 25, width: 125 };
24
- size.height = size.height || 25;
25
- size.width = size.width || 125;
26
- const defaultedOptions = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(options), { position, size, style });
27
- let textVal = sanitizeHTML ? document_util_1.DocumentUtil.sanitizeHTML(textValue) : textValue;
28
- if (textVal === undefined || textVal === null) {
29
- textVal = '';
30
- }
31
- let element = documents_1.DocumentElementFactory.createTextElement(textVal);
32
- element = Object.assign(element, defaultedOptions);
33
- if (applyInlineFormatting) {
34
- element.text = this.applyInLineStyling(element.text, element.style);
35
- }
36
- return element;
37
- }
38
- static applyInLineStyling(text, style) {
39
- var _a, _b, _c;
40
- let formattedText = text;
41
- if ((style === null || style === void 0 ? void 0 : style.font.weight) === 'bold') {
42
- formattedText = `<strong>${formattedText}</strong>`;
43
- }
44
- if (((_a = style === null || style === void 0 ? void 0 : style.text) === null || _a === void 0 ? void 0 : _a.decoration) === 'underline') {
45
- formattedText = `<span style="text-decoration: underline;">${formattedText}</span>`;
46
- }
47
- let fontStyles = '';
48
- if ((_b = style === null || style === void 0 ? void 0 : style.font) === null || _b === void 0 ? void 0 : _b.size) {
49
- fontStyles += `font-size: ${style.font.size}pt; `;
50
- }
51
- if (style === null || style === void 0 ? void 0 : style.color) {
52
- fontStyles += `color: ${style.color}; `;
53
- }
54
- if ((_c = style === null || style === void 0 ? void 0 : style.font) === null || _c === void 0 ? void 0 : _c.family) {
55
- fontStyles += `font-family: '${style.font.family}'; `;
56
- }
57
- if (fontStyles.length) {
58
- formattedText = `<span style="${fontStyles}">${formattedText}</span>`;
59
- }
60
- formattedText = `<p>${formattedText}</p>`;
61
- return formattedText;
62
- }
63
- static extractStyleFromInline(text) {
64
- if (!text) {
65
- return null;
66
- }
67
- const style = { font: {}, text: {} };
68
- if (text.indexOf('<strong>') > -1 || text.indexOf('bold') > -1) {
69
- style.font.weight = 'bold';
70
- }
71
- const fontSize = this.extractFontSizeFromHTML(text);
72
- if (fontSize) {
73
- style.font.size = fontSize;
74
- }
75
- const color = this.extractColorFromHTML(text);
76
- if (color) {
77
- style.color = color;
78
- }
79
- const decoration = this.extractTextDecorationFromHTML(text);
80
- if (decoration) {
81
- style.text.decoration = decoration;
82
- }
83
- const font = this.extractFontFromHTML(text);
84
- if (font) {
85
- style.font.family = font;
86
- }
87
- return style;
88
- }
89
- static extractFontSizeFromHTML(text) {
90
- let regex = /font-size: (.*?)pt;/;
91
- let match = text.match(regex);
92
- if ((match === null || match === void 0 ? void 0 : match.length) && !isNaN(parseInt(match[1]))) {
93
- return parseInt(match[1]);
94
- }
95
- }
96
- static extractColorFromHTML(text) {
97
- let regex = /color: (.*?);/;
98
- let match = text.match(regex);
99
- if (match === null || match === void 0 ? void 0 : match.length) {
100
- let color = match[1];
101
- color = color.replace(/['"]+/g, '');
102
- return color;
103
- }
104
- }
105
- static extractTextDecorationFromHTML(text) {
106
- let regex = /text-decoration: (.*?);/;
107
- let match = text.match(regex);
108
- if (match === null || match === void 0 ? void 0 : match.length) {
109
- let decoration = match[1];
110
- decoration = decoration.replace(/['"]+/g, '');
111
- return decoration;
112
- }
113
- }
114
- static extractFontFromHTML(text) {
115
- let regex = /font-family: (.*?);/;
116
- let match = text.match(regex);
117
- if (match === null || match === void 0 ? void 0 : match.length) {
118
- let font = match[1];
119
- font = font.replace(/['"]+/g, '');
120
- return font;
121
- }
122
- }
123
- }
124
- exports.DocumentTextElementUtil = DocumentTextElementUtil;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentTextElementUtil = void 0;
4
+ const documents_1 = require("@contrail/documents");
5
+ const util_1 = require("@contrail/util");
6
+ const document_util_1 = require("./document-util");
7
+ class DocumentTextElementUtil {
8
+ static generateTextElement(textValue, options, applyInlineFormatting = true, sanitizeHTML = true) {
9
+ var _a;
10
+ const defaultStyle = {
11
+ font: {
12
+ size: 8,
13
+ },
14
+ color: 'rgba(0,0,0,.5)',
15
+ };
16
+ let style = util_1.ObjectUtil.mergeDeep(defaultStyle, options.style || {});
17
+ if ((_a = options === null || options === void 0 ? void 0 : options.style) === null || _a === void 0 ? void 0 : _a.text) {
18
+ style.text = options.style.text;
19
+ }
20
+ let position = options.position || { y: 0, x: 0 };
21
+ position.x = position.x || 0;
22
+ position.y = position.y || 0;
23
+ let size = options.size || { height: 25, width: 125 };
24
+ size.height = size.height || 25;
25
+ size.width = size.width || 125;
26
+ const defaultedOptions = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(options), { position, size, style });
27
+ let textVal = sanitizeHTML ? document_util_1.DocumentUtil.sanitizeHTML(textValue) : textValue;
28
+ if (textVal === undefined || textVal === null) {
29
+ textVal = '';
30
+ }
31
+ let element = documents_1.DocumentElementFactory.createTextElement(textVal);
32
+ element = Object.assign(element, defaultedOptions);
33
+ if (applyInlineFormatting) {
34
+ element.text = this.applyInLineStyling(element.text, element.style);
35
+ }
36
+ return element;
37
+ }
38
+ static applyInLineStyling(text, style) {
39
+ var _a, _b, _c;
40
+ let formattedText = text;
41
+ if ((style === null || style === void 0 ? void 0 : style.font.weight) === 'bold') {
42
+ formattedText = `<strong>${formattedText}</strong>`;
43
+ }
44
+ if (((_a = style === null || style === void 0 ? void 0 : style.text) === null || _a === void 0 ? void 0 : _a.decoration) === 'underline') {
45
+ formattedText = `<span style="text-decoration: underline;">${formattedText}</span>`;
46
+ }
47
+ let fontStyles = '';
48
+ if ((_b = style === null || style === void 0 ? void 0 : style.font) === null || _b === void 0 ? void 0 : _b.size) {
49
+ fontStyles += `font-size: ${style.font.size}pt; `;
50
+ }
51
+ if (style === null || style === void 0 ? void 0 : style.color) {
52
+ fontStyles += `color: ${style.color}; `;
53
+ }
54
+ if ((_c = style === null || style === void 0 ? void 0 : style.font) === null || _c === void 0 ? void 0 : _c.family) {
55
+ fontStyles += `font-family: '${style.font.family}'; `;
56
+ }
57
+ if (fontStyles.length) {
58
+ formattedText = `<span style="${fontStyles}">${formattedText}</span>`;
59
+ }
60
+ formattedText = `<p>${formattedText}</p>`;
61
+ return formattedText;
62
+ }
63
+ static extractStyleFromInline(text) {
64
+ if (!text) {
65
+ return null;
66
+ }
67
+ const style = { font: {}, text: {} };
68
+ if (text.indexOf('<strong>') > -1 || text.indexOf('bold') > -1) {
69
+ style.font.weight = 'bold';
70
+ }
71
+ const fontSize = this.extractFontSizeFromHTML(text);
72
+ if (fontSize) {
73
+ style.font.size = fontSize;
74
+ }
75
+ const color = this.extractColorFromHTML(text);
76
+ if (color) {
77
+ style.color = color;
78
+ }
79
+ const decoration = this.extractTextDecorationFromHTML(text);
80
+ if (decoration) {
81
+ style.text.decoration = decoration;
82
+ }
83
+ const font = this.extractFontFromHTML(text);
84
+ if (font) {
85
+ style.font.family = font;
86
+ }
87
+ return style;
88
+ }
89
+ static extractFontSizeFromHTML(text) {
90
+ let regex = /font-size: (.*?)pt;/;
91
+ let match = text.match(regex);
92
+ if ((match === null || match === void 0 ? void 0 : match.length) && !isNaN(parseInt(match[1]))) {
93
+ return parseInt(match[1]);
94
+ }
95
+ }
96
+ static extractColorFromHTML(text) {
97
+ let regex = /color: (.*?);/;
98
+ let match = text.match(regex);
99
+ if (match === null || match === void 0 ? void 0 : match.length) {
100
+ let color = match[1];
101
+ color = color.replace(/['"]+/g, '');
102
+ return color;
103
+ }
104
+ }
105
+ static extractTextDecorationFromHTML(text) {
106
+ let regex = /text-decoration: (.*?);/;
107
+ let match = text.match(regex);
108
+ if (match === null || match === void 0 ? void 0 : match.length) {
109
+ let decoration = match[1];
110
+ decoration = decoration.replace(/['"]+/g, '');
111
+ return decoration;
112
+ }
113
+ }
114
+ static extractFontFromHTML(text) {
115
+ let regex = /font-family: (.*?);/;
116
+ let match = text.match(regex);
117
+ if (match === null || match === void 0 ? void 0 : match.length) {
118
+ let font = match[1];
119
+ font = font.replace(/['"]+/g, '');
120
+ return font;
121
+ }
122
+ }
123
+ }
124
+ exports.DocumentTextElementUtil = DocumentTextElementUtil;