@contrail/documents 1.0.108 → 1.0.109

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.
@@ -9,6 +9,6 @@ class DocumentAction extends actions_1.Action {
9
9
  }
10
10
  exports.DocumentAction = DocumentAction;
11
11
  function uniqueElementsToUpdate(elementsToUpdate, mainElementsToUpdate) {
12
- return elementsToUpdate.filter(({ change }) => mainElementsToUpdate.findIndex(elementToUpdate => elementToUpdate.change.id === change.id) === -1);
12
+ return elementsToUpdate.filter(({ change }) => mainElementsToUpdate.findIndex((elementToUpdate) => elementToUpdate.change.id === change.id) === -1);
13
13
  }
14
14
  exports.uniqueElementsToUpdate = uniqueElementsToUpdate;
@@ -1,5 +1,6 @@
1
1
  import { ComponentDefinition } from './components/components';
2
2
  import { DocumentElement } from './types/document-element';
3
+ import { PositionDefinition } from './types';
3
4
  export declare class DocumentElementFactory {
4
5
  static initNewElement(type: string): DocumentElement;
5
6
  static createTextElement(text: string, options?: DocumentElement): DocumentElement;
@@ -9,4 +10,9 @@ export declare class DocumentElementFactory {
9
10
  static createFrameElement(options: DocumentElement): DocumentElement;
10
11
  static createComponent(componentType: string, model: any, modelBindings: any, options?: DocumentElement): DocumentElement;
11
12
  static createComponentFromComponentDefinition(componentDefinition: ComponentDefinition, model: any, modelBindings: any, options?: DocumentElement): DocumentElement;
13
+ static copyElements(elements: DocumentElement[], commonOptions?: DocumentElement, documentId?: string, positionOffset?: PositionDefinition, lastFrameNumber?: number): {
14
+ oldIdToNewId: Map<string, string>;
15
+ newElements: DocumentElement[];
16
+ };
17
+ static isMask(element: DocumentElement): boolean;
12
18
  }
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DocumentElementFactory = void 0;
4
+ const util_1 = require("@contrail/util");
4
5
  const nanoid_1 = require("nanoid");
5
6
  const components_1 = require("./components/components");
6
7
  const document_element_property_binding_handler_1 = require("./document-element-property-binding-handler");
8
+ const document_table_1 = require("@contrail/document-table");
7
9
  class DocumentElementFactory {
8
10
  static initNewElement(type) {
9
11
  const element = {
@@ -33,7 +35,7 @@ class DocumentElementFactory {
33
35
  let element = this.initNewElement(type);
34
36
  element = Object.assign(element, options);
35
37
  if (((_a = element === null || element === void 0 ? void 0 : element.elements) === null || _a === void 0 ? void 0 : _a.length) > 0) {
36
- element.elements = element.elements.map((e) => {
38
+ element.elements = element.elements.map(e => {
37
39
  e.id = (0, nanoid_1.nanoid)(16);
38
40
  return e;
39
41
  });
@@ -66,12 +68,104 @@ class DocumentElementFactory {
66
68
  element.elements = elements;
67
69
  element.modelBindings = modelBindings;
68
70
  if (((_a = element === null || element === void 0 ? void 0 : element.elements) === null || _a === void 0 ? void 0 : _a.length) > 0) {
69
- element.elements = element.elements.map((e) => {
71
+ element.elements = element.elements.map(e => {
70
72
  e.id = (0, nanoid_1.nanoid)(16);
71
73
  return e;
72
74
  });
73
75
  }
74
76
  return element;
75
77
  }
78
+ static copyElements(elements, commonOptions = {}, documentId, positionOffset, lastFrameNumber) {
79
+ var _a;
80
+ const oldIdToNewId = new Map();
81
+ const newElements = [];
82
+ const groupElements = [];
83
+ const maskElements = [];
84
+ let frameNumber = lastFrameNumber;
85
+ for (const element of elements) {
86
+ if (['row', 'column', 'cell'].indexOf(element.type) !== -1)
87
+ continue;
88
+ let newElement = util_1.ObjectUtil.mergeDeep({}, element, commonOptions);
89
+ if (frameNumber != undefined && newElement.type === 'frame') {
90
+ newElement.name = `Frame ${frameNumber}`;
91
+ frameNumber = frameNumber + 1;
92
+ }
93
+ delete newElement.id;
94
+ delete newElement.specifiedId;
95
+ if (((_a = newElement === null || newElement === void 0 ? void 0 : newElement.elements) === null || _a === void 0 ? void 0 : _a.length) > 0) {
96
+ for (const e of newElement.elements) {
97
+ delete e.id;
98
+ delete e.specifiedId;
99
+ }
100
+ }
101
+ let newId = (0, nanoid_1.nanoid)(16);
102
+ if (newElement.type === 'table') {
103
+ const oldChildElements = elements.filter(e => e.tableId === element.id && ['cell', 'column', 'row'].indexOf(e.type) !== -1);
104
+ const [newTableElement, ...newChildElements] = document_table_1.TableCopyService.copy(newElement, oldChildElements);
105
+ if (oldChildElements.length !== newChildElements.length) {
106
+ console.log(`Invalid table element to copy`, newElement, oldChildElements, newChildElements);
107
+ continue;
108
+ }
109
+ newElement = newTableElement;
110
+ newId = newTableElement.id;
111
+ newChildElements.forEach(newChildElement => {
112
+ var _a;
113
+ let oldChildId;
114
+ if (newChildElement.type === 'row') {
115
+ oldChildId = element.rowIds[newTableElement.rowIds.indexOf(newChildElement.id)];
116
+ }
117
+ else if (newChildElement.type === 'column') {
118
+ oldChildId = element.columnIds[newTableElement.columnIds.indexOf(newChildElement.id)];
119
+ }
120
+ else if (newChildElement.type === 'cell') {
121
+ const oldRowId = element.rowIds[newTableElement.rowIds.indexOf(newChildElement.rowId)];
122
+ const oldColumnId = element.columnIds[newTableElement.columnIds.indexOf(newChildElement.columnId)];
123
+ oldChildId = (_a = elements.find(e => e.type === 'cell' && e.rowId === oldRowId && e.columnId === oldColumnId)) === null || _a === void 0 ? void 0 : _a.id;
124
+ }
125
+ if (oldChildId) {
126
+ oldIdToNewId[oldChildId] = newChildElement.id;
127
+ newElements.push(newChildElement);
128
+ }
129
+ });
130
+ }
131
+ if (positionOffset) {
132
+ if (newElement.lineDefinition) {
133
+ newElement.lineDefinition.x1 = newElement.lineDefinition.x1 + positionOffset.x;
134
+ newElement.lineDefinition.y1 = newElement.lineDefinition.y1 + positionOffset.y;
135
+ newElement.lineDefinition.x2 = newElement.lineDefinition.x2 + positionOffset.x;
136
+ newElement.lineDefinition.y2 = newElement.lineDefinition.y2 + positionOffset.y;
137
+ }
138
+ else if (newElement.position) {
139
+ newElement.position.x = newElement.position.x + positionOffset.x;
140
+ newElement.position.y = newElement.position.y + positionOffset.y;
141
+ }
142
+ }
143
+ newElement.documentId = documentId;
144
+ newElement.specifiedId = newId;
145
+ oldIdToNewId[element.id] = newId;
146
+ newElement = DocumentElementFactory.createElement(element.type, newElement);
147
+ newElements.push(newElement);
148
+ if (newElement.type === 'group')
149
+ groupElements.push(newElement);
150
+ if (DocumentElementFactory.isMask(newElement))
151
+ maskElements.push(newElement);
152
+ }
153
+ groupElements.forEach(groupElement => {
154
+ groupElement.elementIds = groupElement.elementIds
155
+ .map(oldElementId => oldIdToNewId.get(oldElementId))
156
+ .filter(id => !!id);
157
+ });
158
+ maskElements.forEach(maskElement => {
159
+ const newElementIds = maskElement.elementIds
160
+ .map(oldElementId => oldIdToNewId.get(oldElementId))
161
+ .filter(id => !!id);
162
+ maskElement.elementIds = (newElementIds === null || newElementIds === void 0 ? void 0 : newElementIds.length) > 0 ? newElementIds : null;
163
+ });
164
+ return { oldIdToNewId, newElements };
165
+ }
166
+ static isMask(element) {
167
+ var _a;
168
+ return ['rectangle', 'circle'].indexOf(element === null || element === void 0 ? void 0 : element.type) !== -1 && ((_a = element === null || element === void 0 ? void 0 : element.elementIds) === null || _a === void 0 ? void 0 : _a.length) > 0;
169
+ }
76
170
  }
77
171
  exports.DocumentElementFactory = DocumentElementFactory;
@@ -8,6 +8,8 @@ export interface AlternateUrls {
8
8
  }
9
9
  export interface DocumentElement extends Document {
10
10
  id?: string;
11
+ specifiedId?: string;
12
+ documentId?: string;
11
13
  type?: string;
12
14
  position?: PositionDefinition;
13
15
  rotate?: RotationDefinition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/documents",
3
- "version": "1.0.108",
3
+ "version": "1.0.109",
4
4
  "description": "Documents library for contrail platform",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -39,6 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@contrail/actions": "^1.0.14",
42
+ "@contrail/document-table": "^1.0.1",
42
43
  "@contrail/types": "^3.0.71",
43
44
  "@contrail/util": "^1.0.19",
44
45
  "reflect-metadata": "^0.1.13"