@contrail/documents 1.3.6 → 1.3.8

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/lib/index.d.ts CHANGED
@@ -6,3 +6,5 @@ export * from './document-action';
6
6
  export * from './util/measure-text/measure-text';
7
7
  export * from './util/dynamic-text/dynamic-text-util';
8
8
  export * from './document-element-constants';
9
+ export * from './util/curved-line/point';
10
+ export * from './util/curved-line/curved-line';
package/lib/index.js CHANGED
@@ -22,3 +22,5 @@ __exportStar(require("./document-action"), exports);
22
22
  __exportStar(require("./util/measure-text/measure-text"), exports);
23
23
  __exportStar(require("./util/dynamic-text/dynamic-text-util"), exports);
24
24
  __exportStar(require("./document-element-constants"), exports);
25
+ __exportStar(require("./util/curved-line/point"), exports);
26
+ __exportStar(require("./util/curved-line/curved-line"), exports);
@@ -0,0 +1,16 @@
1
+ import { LineDefinition, ViewBox, LinkedEdgeDefinition, PositionDefinition, SizeDefinition } from '../../types';
2
+ export declare class Callout {
3
+ static getCalloutLocation({ x1, y1, x2, y2 }: LineDefinition): 'auto' | 'left' | 'right' | 'top' | 'bottom';
4
+ static getCalloutStart({ x, y }: {
5
+ x: any;
6
+ y: any;
7
+ }, elementId?: string, elementDimensions?: ViewBox): LinkedEdgeDefinition;
8
+ static getCalloutEnd({ x, y }: {
9
+ x: any;
10
+ y: any;
11
+ }, elementId?: string): LinkedEdgeDefinition;
12
+ static getCalloutLineEndPosition(location: 'auto' | 'left' | 'right' | 'top' | 'bottom', position: PositionDefinition, size: SizeDefinition): PositionDefinition;
13
+ static getCalloutEndElementPosition(location: 'auto' | 'left' | 'right' | 'top' | 'bottom', position: PositionDefinition, size: SizeDefinition): PositionDefinition;
14
+ private static rotate;
15
+ private static roundTo;
16
+ }
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Callout = void 0;
4
+ class Callout {
5
+ static getCalloutLocation({ x1, y1, x2, y2 }) {
6
+ let location;
7
+ const endCenterRelativeToStart = {
8
+ x: x2 - x1,
9
+ y: y2 - y1,
10
+ };
11
+ const rotatedPosition = this.rotate(endCenterRelativeToStart, -45);
12
+ if (rotatedPosition.x >= 0) {
13
+ if (rotatedPosition.y >= 0)
14
+ location = 'top';
15
+ else
16
+ location = 'left';
17
+ }
18
+ else {
19
+ if (rotatedPosition.y >= 0)
20
+ location = 'right';
21
+ else
22
+ location = 'bottom';
23
+ }
24
+ return location;
25
+ }
26
+ static getCalloutStart({ x, y }, elementId, elementDimensions) {
27
+ if (!elementId) {
28
+ return {
29
+ id: null,
30
+ position: {
31
+ x,
32
+ y,
33
+ },
34
+ };
35
+ }
36
+ return {
37
+ id: elementId,
38
+ position: {
39
+ x: this.roundTo(((x - elementDimensions.x) * 100) / elementDimensions.width),
40
+ y: this.roundTo(((y - elementDimensions.y) * 100) / elementDimensions.height),
41
+ },
42
+ };
43
+ }
44
+ static getCalloutEnd({ x, y }, elementId) {
45
+ if (!elementId) {
46
+ return {
47
+ id: null,
48
+ position: {
49
+ x,
50
+ y,
51
+ },
52
+ };
53
+ }
54
+ return {
55
+ id: elementId,
56
+ location: 'auto',
57
+ };
58
+ }
59
+ static getCalloutLineEndPosition(location, position, size) {
60
+ let x, y;
61
+ switch (location) {
62
+ case 'top':
63
+ x = position.x + size.width * 0.5;
64
+ y = position.y;
65
+ break;
66
+ case 'bottom':
67
+ x = position.x + size.width * 0.5;
68
+ y = position.y + size.height;
69
+ break;
70
+ case 'right':
71
+ x = position.x + size.width;
72
+ y = position.y + size.height * 0.5;
73
+ break;
74
+ case 'left':
75
+ default:
76
+ x = position.x;
77
+ y = position.y + size.height * 0.5;
78
+ break;
79
+ }
80
+ return { x, y };
81
+ }
82
+ static getCalloutEndElementPosition(location, position, size) {
83
+ let x, y;
84
+ switch (location) {
85
+ case 'top':
86
+ x = position.x - size.width * 0.5;
87
+ y = position.y;
88
+ break;
89
+ case 'bottom':
90
+ x = position.x - size.width * 0.5;
91
+ y = position.y - size.height;
92
+ break;
93
+ case 'right':
94
+ x = position.x - size.width;
95
+ y = position.y - size.height * 0.5;
96
+ break;
97
+ case 'left':
98
+ default:
99
+ x = position.x;
100
+ y = position.y - size.height * 0.5;
101
+ break;
102
+ }
103
+ return { x, y };
104
+ }
105
+ static rotate(position, angle, center = { x: 0, y: 0 }) {
106
+ const theta = (angle * Math.PI) / 180;
107
+ const dx = position.x - center.x;
108
+ const dy = position.y - center.y;
109
+ return {
110
+ x: dx * Math.cos(theta) - dy * Math.sin(theta) + center.x,
111
+ y: dx * Math.sin(theta) + dy * Math.cos(theta) + center.y,
112
+ };
113
+ }
114
+ static roundTo(v) {
115
+ return Math.round(v * 10000) / 10000;
116
+ }
117
+ }
118
+ exports.Callout = Callout;
@@ -0,0 +1,12 @@
1
+ import { LineDefinition } from '../../types';
2
+ import { Point } from './point';
3
+ export interface BezierCurvePointDefinition {
4
+ cp1: Point;
5
+ cp2: Point;
6
+ p1: Point;
7
+ p2: Point;
8
+ }
9
+ export declare class CurvedLine {
10
+ static getCurvePoints(lineDefinition: LineDefinition, points: [number, number][]): BezierCurvePointDefinition[];
11
+ static lerp(a: number, b: number, t: number): number;
12
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CurvedLine = void 0;
4
+ const point_1 = require("./point");
5
+ class CurvedLine {
6
+ static getCurvePoints(lineDefinition, points) {
7
+ const curves = [];
8
+ for (let i = 0; i <= points.length; i++) {
9
+ const p0 = points[i - 1];
10
+ const p1 = points[i];
11
+ const prevPoint = p0 ? new point_1.Point(p0[0], p0[1]) : new point_1.Point(lineDefinition.x1, lineDefinition.y1);
12
+ const point = p1 ? new point_1.Point(p1[0], p1[1]) : new point_1.Point(lineDefinition.x2, lineDefinition.y2);
13
+ curves.push({
14
+ p1: prevPoint,
15
+ p2: point,
16
+ cp1: prevPoint,
17
+ cp2: point,
18
+ });
19
+ }
20
+ const interpolation = [];
21
+ const distance = [];
22
+ const r = 0.4;
23
+ for (let i = 0; i < curves.length; i++) {
24
+ const c0 = curves[i - 1];
25
+ const { p1, p2 } = curves[i];
26
+ interpolation.push(r * p1.distance(p2));
27
+ if (c0) {
28
+ const diff0 = c0.p2.clone().subtract(c0.p1).normalize();
29
+ const diff1 = p2.clone().subtract(p1).normalize();
30
+ const d = diff0.add(diff1).normalize();
31
+ distance.push(d);
32
+ }
33
+ }
34
+ distance.unshift(new point_1.Point());
35
+ distance.push(new point_1.Point());
36
+ const interpolated = [];
37
+ for (let i = 0; i < curves.length; i++) {
38
+ const d0 = interpolation[i - 1];
39
+ const d1 = interpolation[i];
40
+ const d2 = interpolation[i + 1];
41
+ const i0 = d0 && d0 < d1 ? CurvedLine.lerp(d0, d1, 0.5) : d1;
42
+ const i1 = d2 && d2 < d1 ? CurvedLine.lerp(d1, d2, 0.5) : d1;
43
+ interpolated.push(i0, i1);
44
+ }
45
+ for (let i = 0; i < curves.length; i++) {
46
+ const i1 = interpolated[2 * i];
47
+ const i2 = interpolated[2 * i + 1];
48
+ const d1 = distance[i].clone();
49
+ const d2 = distance[i + 1].clone().invert();
50
+ const c = curves[i];
51
+ c.cp1 = c.p1.clone().add(d1.clone().normalize(i1));
52
+ c.cp2 = c.p2.clone().add(d2.clone().normalize(i2));
53
+ }
54
+ return curves;
55
+ }
56
+ static lerp(a, b, t) {
57
+ return a + (b - a) * t;
58
+ }
59
+ }
60
+ exports.CurvedLine = CurvedLine;
@@ -0,0 +1,13 @@
1
+ export declare class Point {
2
+ x: number;
3
+ y: number;
4
+ constructor(x?: number, y?: number);
5
+ clone(): Point;
6
+ magnitude(): number;
7
+ multiply(a: number): Point;
8
+ invert(): Point;
9
+ add(p: Point): Point;
10
+ subtract(p: Point): Point;
11
+ normalize(n?: number): Point;
12
+ distance(p: Point): number;
13
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Point = void 0;
4
+ class Point {
5
+ constructor(x = 0, y = 0) {
6
+ this.x = x;
7
+ this.y = y;
8
+ }
9
+ clone() {
10
+ return new Point(this.x, this.y);
11
+ }
12
+ magnitude() {
13
+ return Math.hypot(this.x, this.y);
14
+ }
15
+ multiply(a) {
16
+ this.x *= a;
17
+ this.y *= a;
18
+ return this;
19
+ }
20
+ invert() {
21
+ this.x = -this.x;
22
+ this.y = -this.y;
23
+ return this;
24
+ }
25
+ add(p) {
26
+ this.x += p.x;
27
+ this.y += p.y;
28
+ return this;
29
+ }
30
+ subtract(p) {
31
+ this.x -= p.x;
32
+ this.y -= p.y;
33
+ return this;
34
+ }
35
+ normalize(n) {
36
+ if (this.x !== 0 || this.y !== 0) {
37
+ const e = (n !== null && n !== void 0 ? n : 1) / this.magnitude();
38
+ this.x *= e;
39
+ this.y *= e;
40
+ }
41
+ return this;
42
+ }
43
+ distance(p) {
44
+ return Math.hypot(this.x - p.x, this.y - p.y);
45
+ }
46
+ }
47
+ exports.Point = Point;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",