@contrail/documents 1.3.21-0 → 1.3.24

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,10 +1,26 @@
1
1
  import { DocumentElement } from '../types';
2
2
  export declare const DEFAULT_THUMBNAIL_WIDTH = 200;
3
3
  export declare const MAX_CARD_WIDTH = 600;
4
+ export interface ItemCardDimensions {
5
+ thumbnailWidth: number;
6
+ thumbnailHeight: number;
7
+ cardWidth: number;
8
+ cardHeight: number;
9
+ }
10
+ export interface ScaledItemCardResult {
11
+ thumbnailWidth: number;
12
+ thumbnailHeight: number;
13
+ cardWidth: number;
14
+ }
4
15
  export declare class ItemComponentService {
5
16
  static isItemComponent(element: DocumentElement): boolean;
6
17
  static isThumbnailOnlyComponent(element: DocumentElement): boolean;
7
18
  static updateSizeAndPosition(propertyElements: any[], componentElement: DocumentElement, annotatedElements?: any[]): any;
8
19
  private static getYMargin;
9
20
  private static calculateAnnotationHeight;
21
+ static getImageElement(element: DocumentElement): DocumentElement | undefined;
22
+ static getItemCardDimensions(element: DocumentElement, visualHeight?: number): ItemCardDimensions;
23
+ static calculateScaledThumbnailHeight(thumbnailWidth: number, thumbnailHeight: number, newThumbnailWidth: number): number;
24
+ static calculateScaledCardWidth(thumbnailWidth: number, cardWidth: number, newThumbnailWidth: number): number;
25
+ static calculateScaledDimensions(dimensions: ItemCardDimensions, newThumbnailWidth: number): ScaledItemCardResult;
10
26
  }
@@ -13,8 +13,8 @@ class ItemComponentService {
13
13
  var _a;
14
14
  if (!((_a = element === null || element === void 0 ? void 0 : element.elements) === null || _a === void 0 ? void 0 : _a.length) || element.elements.length > 2)
15
15
  return false;
16
- const annotationElement = element.elements.find((e) => e.type == 'annotation');
17
- const imageElement = element.elements.find((e) => e.type == 'image');
16
+ const annotationElement = element.elements.find(e => e.type == 'annotation');
17
+ const imageElement = element.elements.find(e => e.type == 'image');
18
18
  return (!annotationElement || annotationElement.isHidden) && imageElement && !imageElement.isHidden;
19
19
  }
20
20
  static updateSizeAndPosition(propertyElements, componentElement, annotatedElements = []) {
@@ -22,7 +22,7 @@ class ItemComponentService {
22
22
  const newElements = [];
23
23
  let lastYPosition = 0;
24
24
  let width = 200;
25
- const imageElements = propertyElements.filter((element) => {
25
+ const imageElements = propertyElements.filter(element => {
26
26
  return element.type === 'image';
27
27
  });
28
28
  if (imageElements.length > 0) {
@@ -73,7 +73,7 @@ class ItemComponentService {
73
73
  newElements.push(element);
74
74
  });
75
75
  const cardWidth = (_a = componentElement === null || componentElement === void 0 ? void 0 : componentElement.size) === null || _a === void 0 ? void 0 : _a.width;
76
- const imageElement = newElements === null || newElements === void 0 ? void 0 : newElements.find((e) => e.type === 'image');
76
+ const imageElement = newElements === null || newElements === void 0 ? void 0 : newElements.find(e => e.type === 'image');
77
77
  const thumbnailWidth = ((_b = imageElement === null || imageElement === void 0 ? void 0 : imageElement.size) === null || _b === void 0 ? void 0 : _b.width) || exports.DEFAULT_THUMBNAIL_WIDTH;
78
78
  const align = ((_d = (_c = imageElement === null || imageElement === void 0 ? void 0 : imageElement.style) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.align) || 'center';
79
79
  if (cardWidth && cardWidth > thumbnailWidth) {
@@ -116,14 +116,14 @@ class ItemComponentService {
116
116
  const annotationSize = ((_b = (_a = element.style) === null || _a === void 0 ? void 0 : _a.font) === null || _b === void 0 ? void 0 : _b.size) || document_element_constants_1.ANNOTATION_IMG_SIZE;
117
117
  let height = annotationSize + document_element_constants_1.ANNOTATION_PADDING_Y;
118
118
  let xPos = 0;
119
- const annotatedElement = annotatedElements.find((elem) => elem.id === componentElement.id);
119
+ const annotatedElement = annotatedElements.find(elem => elem.id === componentElement.id);
120
120
  if (!annotatedElement || element.isHidden) {
121
121
  return height;
122
122
  }
123
- const annotations = annotatedElement.annotations.filter((annotation) => annotation.category === 'property');
123
+ const annotations = annotatedElement.annotations.filter(annotation => annotation.category === 'property');
124
124
  if (annotations.length > 0) {
125
125
  let count = 0;
126
- annotations === null || annotations === void 0 ? void 0 : annotations.forEach((annotation) => {
126
+ annotations === null || annotations === void 0 ? void 0 : annotations.forEach(annotation => {
127
127
  if ((count + 1) * annotationSize * 1.25 > width) {
128
128
  count = 0;
129
129
  xPos = -width * 0.5;
@@ -135,5 +135,46 @@ class ItemComponentService {
135
135
  }
136
136
  return height;
137
137
  }
138
+ static getImageElement(element) {
139
+ var _a;
140
+ return (_a = element === null || element === void 0 ? void 0 : element.elements) === null || _a === void 0 ? void 0 : _a.find(e => e.type === 'image');
141
+ }
142
+ static getItemCardDimensions(element, visualHeight) {
143
+ var _a, _b, _c;
144
+ const imageElement = this.getImageElement(element);
145
+ const thumbnailWidth = ((_a = imageElement === null || imageElement === void 0 ? void 0 : imageElement.size) === null || _a === void 0 ? void 0 : _a.width) || exports.DEFAULT_THUMBNAIL_WIDTH;
146
+ const thumbnailHeight = ((_b = imageElement === null || imageElement === void 0 ? void 0 : imageElement.size) === null || _b === void 0 ? void 0 : _b.height) || thumbnailWidth;
147
+ const cardWidth = ((_c = element === null || element === void 0 ? void 0 : element.size) === null || _c === void 0 ? void 0 : _c.width) || thumbnailWidth;
148
+ const cardHeight = visualHeight || thumbnailHeight;
149
+ return {
150
+ thumbnailWidth,
151
+ thumbnailHeight,
152
+ cardWidth,
153
+ cardHeight,
154
+ };
155
+ }
156
+ static calculateScaledThumbnailHeight(thumbnailWidth, thumbnailHeight, newThumbnailWidth) {
157
+ const aspectRatio = thumbnailWidth / thumbnailHeight;
158
+ if (aspectRatio === 1) {
159
+ return newThumbnailWidth;
160
+ }
161
+ else if (aspectRatio > 1) {
162
+ return Math.ceil(newThumbnailWidth * (3 / 4));
163
+ }
164
+ else {
165
+ return Math.ceil(newThumbnailWidth * (4 / 3));
166
+ }
167
+ }
168
+ static calculateScaledCardWidth(thumbnailWidth, cardWidth, newThumbnailWidth) {
169
+ const scaleFactor = newThumbnailWidth / thumbnailWidth;
170
+ return Math.max(newThumbnailWidth, Math.round(cardWidth * scaleFactor));
171
+ }
172
+ static calculateScaledDimensions(dimensions, newThumbnailWidth) {
173
+ return {
174
+ thumbnailWidth: newThumbnailWidth,
175
+ thumbnailHeight: this.calculateScaledThumbnailHeight(dimensions.thumbnailWidth, dimensions.thumbnailHeight, newThumbnailWidth),
176
+ cardWidth: this.calculateScaledCardWidth(dimensions.thumbnailWidth, dimensions.cardWidth, newThumbnailWidth),
177
+ };
178
+ }
138
179
  }
139
180
  exports.ItemComponentService = ItemComponentService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.3.21-0",
3
+ "version": "1.3.24",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",