@contrail/document-generation 1.0.25 → 1.0.27

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.
@@ -1,59 +1,103 @@
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
- class DocumentTextElementUtil {
7
- static generateTextElement(textValue, options, applyInlineFormatting = true) {
8
- var _a;
9
- const defaultStyle = {
10
- font: {
11
- size: 8,
12
- },
13
- color: 'rgba(0,0,0,.5)',
14
- };
15
- let style = util_1.ObjectUtil.mergeDeep(defaultStyle, options.style || {});
16
- if ((_a = options === null || options === void 0 ? void 0 : options.style) === null || _a === void 0 ? void 0 : _a.text) {
17
- style.text = options.style.text;
18
- }
19
- let position = options.position || { y: 0, x: 0 };
20
- position.x = position.x || 0;
21
- position.y = position.y || 0;
22
- let size = options.size || { height: 25, width: 125 };
23
- size.height = size.height || 25;
24
- size.width = size.width || 125;
25
- const defaultedOptions = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(options), { position, size, style });
26
- let element = documents_1.DocumentElementFactory.createTextElement(textValue);
27
- element = Object.assign(element, defaultedOptions);
28
- if (applyInlineFormatting) {
29
- element.text = this.applyInLineStyling(element.text, element.style);
30
- }
31
- return element;
32
- }
33
- static applyInLineStyling(text, style) {
34
- var _a, _b;
35
- let formattedText = text;
36
- if ((style === null || style === void 0 ? void 0 : style.font.weight) === 'bold') {
37
- formattedText = `<strong>${formattedText}</strong>`;
38
- }
39
- if (((_a = style === null || style === void 0 ? void 0 : style.text) === null || _a === void 0 ? void 0 : _a.decoration) === 'underline') {
40
- formattedText = `<span style="text-decoration: underline;">${formattedText}</span>`;
41
- }
42
- let fontStyles = '';
43
- if ((_b = style === null || style === void 0 ? void 0 : style.font) === null || _b === void 0 ? void 0 : _b.size) {
44
- fontStyles += `font-size: ${style.font.size}pt; `;
45
- }
46
- if (style === null || style === void 0 ? void 0 : style.color) {
47
- fontStyles += `color: ${style.color}; `;
48
- }
49
- if (fontStyles.length) {
50
- formattedText = `<span style="${fontStyles}">${formattedText}</span>`;
51
- }
52
- formattedText = `<p>${formattedText}</p>`;
53
- return formattedText;
54
- }
55
- static applyBold(text) {
56
- return;
57
- }
58
- }
59
- 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
+ class DocumentTextElementUtil {
7
+ static generateTextElement(textValue, options, applyInlineFormatting = true) {
8
+ var _a;
9
+ const defaultStyle = {
10
+ font: {
11
+ size: 8,
12
+ },
13
+ color: 'rgba(0,0,0,.5)',
14
+ };
15
+ let style = util_1.ObjectUtil.mergeDeep(defaultStyle, options.style || {});
16
+ if ((_a = options === null || options === void 0 ? void 0 : options.style) === null || _a === void 0 ? void 0 : _a.text) {
17
+ style.text = options.style.text;
18
+ }
19
+ let position = options.position || { y: 0, x: 0 };
20
+ position.x = position.x || 0;
21
+ position.y = position.y || 0;
22
+ let size = options.size || { height: 25, width: 125 };
23
+ size.height = size.height || 25;
24
+ size.width = size.width || 125;
25
+ const defaultedOptions = util_1.ObjectUtil.mergeDeep(util_1.ObjectUtil.cloneDeep(options), { position, size, style });
26
+ let element = documents_1.DocumentElementFactory.createTextElement(textValue);
27
+ element = Object.assign(element, defaultedOptions);
28
+ if (applyInlineFormatting) {
29
+ element.text = this.applyInLineStyling(element.text, element.style);
30
+ }
31
+ return element;
32
+ }
33
+ static applyInLineStyling(text, style) {
34
+ var _a, _b;
35
+ let formattedText = text;
36
+ if ((style === null || style === void 0 ? void 0 : style.font.weight) === 'bold') {
37
+ formattedText = `<strong>${formattedText}</strong>`;
38
+ }
39
+ if (((_a = style === null || style === void 0 ? void 0 : style.text) === null || _a === void 0 ? void 0 : _a.decoration) === 'underline') {
40
+ formattedText = `<span style="text-decoration: underline;">${formattedText}</span>`;
41
+ }
42
+ let fontStyles = '';
43
+ if ((_b = style === null || style === void 0 ? void 0 : style.font) === null || _b === void 0 ? void 0 : _b.size) {
44
+ fontStyles += `font-size: ${style.font.size}pt; `;
45
+ }
46
+ if (style === null || style === void 0 ? void 0 : style.color) {
47
+ fontStyles += `color: ${style.color}; `;
48
+ }
49
+ if (fontStyles.length) {
50
+ formattedText = `<span style="${fontStyles}">${formattedText}</span>`;
51
+ }
52
+ formattedText = `<p>${formattedText}</p>`;
53
+ return formattedText;
54
+ }
55
+ static extractStyleFromInline(text) {
56
+ if (!text) {
57
+ return null;
58
+ }
59
+ const style = { font: {}, text: {} };
60
+ if (text.indexOf('<strong>') > -1 || text.indexOf("bold") > -1) {
61
+ style.font.weight = 'bold';
62
+ }
63
+ const fontSize = this.extractFontSizeFromHTML(text);
64
+ if (fontSize) {
65
+ style.font.size = fontSize;
66
+ }
67
+ const color = this.extractColorFromHTML(text);
68
+ if (color) {
69
+ style.color = color;
70
+ }
71
+ const decoration = this.extractTextDecorationFromHTML(text);
72
+ if (decoration) {
73
+ style.text.decoration = decoration;
74
+ }
75
+ return style;
76
+ }
77
+ static extractFontSizeFromHTML(text) {
78
+ let regex = /font-size: (.*?)pt;/;
79
+ let match = text.match(regex);
80
+ if ((match === null || match === void 0 ? void 0 : match.length) && !isNaN(parseInt(match[1]))) {
81
+ return parseInt(match[1]);
82
+ }
83
+ }
84
+ static extractColorFromHTML(text) {
85
+ let regex = /color: (.*?);/;
86
+ let match = text.match(regex);
87
+ if (match === null || match === void 0 ? void 0 : match.length) {
88
+ let color = match[1];
89
+ color = color.replace(/['"]+/g, '');
90
+ return color;
91
+ }
92
+ }
93
+ static extractTextDecorationFromHTML(text) {
94
+ let regex = /text-decoration: (.*?);/;
95
+ let match = text.match(regex);
96
+ if (match === null || match === void 0 ? void 0 : match.length) {
97
+ let decoration = match[1];
98
+ decoration = decoration.replace(/['"]+/g, '');
99
+ return decoration;
100
+ }
101
+ }
102
+ }
103
+ exports.DocumentTextElementUtil = DocumentTextElementUtil;
@@ -1,7 +1,7 @@
1
- import { PositionDefinition } from "@contrail/documents";
2
- export declare class DocumentUtil {
3
- static getRelativePosition(position1: PositionDefinition, position2: PositionDefinition): {
4
- x: number;
5
- y: number;
6
- };
7
- }
1
+ import { PositionDefinition } from "@contrail/documents";
2
+ export declare class DocumentUtil {
3
+ static getRelativePosition(position1: PositionDefinition, position2: PositionDefinition): {
4
+ x: number;
5
+ y: number;
6
+ };
7
+ }
@@ -1,13 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DocumentUtil = void 0;
4
- class DocumentUtil {
5
- static getRelativePosition(position1, position2) {
6
- const relativePosition = {
7
- x: position2.x - position1.x,
8
- y: position2.y - position1.y,
9
- };
10
- return relativePosition;
11
- }
12
- }
13
- exports.DocumentUtil = DocumentUtil;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DocumentUtil = void 0;
4
+ class DocumentUtil {
5
+ static getRelativePosition(position1, position2) {
6
+ const relativePosition = {
7
+ x: position2.x - position1.x,
8
+ y: position2.y - position1.y,
9
+ };
10
+ return relativePosition;
11
+ }
12
+ }
13
+ exports.DocumentUtil = DocumentUtil;
@@ -0,0 +1,3 @@
1
+ export * from './document-property-util';
2
+ export * from './document-text-element-util';
3
+ export * from './document-util';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./document-property-util"), exports);
18
+ __exportStar(require("./document-text-element-util"), exports);
19
+ __exportStar(require("./document-util"), exports);
package/package.json CHANGED
@@ -1,50 +1,50 @@
1
- {
2
- "name": "@contrail/document-generation",
3
- "version": "1.0.25",
4
- "description": "Utilities for automatic generation of documents.",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "scripts": {
8
- "build": "tsc",
9
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
- "lint": "tslint -p tsconfig.json",
11
- "test": "jest"
12
- },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
- "devDependencies": {
17
- "@contrail/aggregates": "^1.0.14",
18
- "@contrail/sdk": "^1.2.3",
19
- "@types/jest": "^29.5.2",
20
- "@types/node": "^18.16.0",
21
- "jest": "^29.5.0",
22
- "nanoid": "^3.3.6",
23
- "npm": "^9.6.5",
24
- "prettier": "^1.19.1",
25
- "ts-jest": "^29.1.1",
26
- "tslint": "^5.11.0",
27
- "tslint-config-prettier": "^1.18.0",
28
- "typescript": "^4.0.0"
29
- },
30
- "jest": {
31
- "moduleFileExtensions": [
32
- "js",
33
- "json",
34
- "ts"
35
- ],
36
- "rootDir": "src",
37
- "testRegex": ".spec.ts$",
38
- "transform": {
39
- "^.+\\.(t|j)s$": "ts-jest"
40
- },
41
- "coverageDirectory": "../coverage",
42
- "testEnvironment": "node"
43
- },
44
- "dependencies": {
45
- "@contrail/data-grouping": "^1.0.20",
46
- "@contrail/documents": "^1.0.44",
47
- "@contrail/types": "^3.0.27",
48
- "@contrail/util": "^1.0.27"
49
- }
50
- }
1
+ {
2
+ "name": "@contrail/document-generation",
3
+ "version": "1.0.27",
4
+ "description": "Utilities for automatic generation of documents.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
+ "lint": "tslint -p tsconfig.json",
11
+ "test": "jest"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@contrail/aggregates": "^1.0.14",
18
+ "@contrail/sdk": "^1.2.3",
19
+ "@types/jest": "^23.3.14",
20
+ "@types/node": "^18.16.0",
21
+ "jest": "^23.6.0",
22
+ "nanoid": "^3.3.6",
23
+ "npm": "^9.6.5",
24
+ "prettier": "^1.19.1",
25
+ "ts-jest": "^23.10.5",
26
+ "tslint": "^5.11.0",
27
+ "tslint-config-prettier": "^1.18.0",
28
+ "typescript": "^4.0.0"
29
+ },
30
+ "jest": {
31
+ "moduleFileExtensions": [
32
+ "js",
33
+ "json",
34
+ "ts"
35
+ ],
36
+ "rootDir": "src",
37
+ "testRegex": ".spec.ts$",
38
+ "transform": {
39
+ "^.+\\.(t|j)s$": "ts-jest"
40
+ },
41
+ "coverageDirectory": "../coverage",
42
+ "testEnvironment": "node"
43
+ },
44
+ "dependencies": {
45
+ "@contrail/data-grouping": "^1.0.20",
46
+ "@contrail/documents": "^1.0.44",
47
+ "@contrail/types": "^3.0.27",
48
+ "@contrail/util": "^1.0.27"
49
+ }
50
+ }