-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathexport-button.business.ts
More file actions
261 lines (214 loc) · 7.03 KB
/
export-button.business.ts
File metadata and controls
261 lines (214 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import {
FieldVm,
TableVm,
TABLE_CONST,
NoteVm,
NOTE_CONST,
} from '@/core/providers';
import { doTablesOverlap } from './export-coordinate.helpers';
import { calculateNoteAutoHeight } from '@/pods/canvas/components/note';
export const getMaxPositionXFromTables = (tables: TableVm[]): number =>
tables.length === 0 ? 0 : Math.max(...tables.map(table => table.x));
export const getMaxEndPositionXFromTables = (tables: TableVm[]): number =>
tables.length === 0
? 0
: Math.max(
...tables.map(
table => table.x + (table.width ?? TABLE_CONST.DEFAULT_TABLE_WIDTH)
)
);
export const getMinPositionXFromTables = (tables: TableVm[]): number =>
tables.length === 0 ? 0 : Math.min(...tables.map(table => table.x));
export const getTotalCanvasWidthFromTables = (tables: TableVm[]): number => {
if (tables.length === 0) return 0;
const minX = getMinPositionXFromTables(tables);
const maxEndX = getMaxEndPositionXFromTables(tables);
// Si hay tablas en posiciones negativas, calculamos el ancho total
// desde la posición más a la izquierda hasta la más a la derecha
return maxEndX - Math.min(minX, 0);
};
export const normalizeTablesForExport = (tables: TableVm[]): TableVm[] => {
if (tables.length === 0) return tables;
const minX = getMinPositionXFromTables(tables);
// Si hay tablas en posiciones negativas, las desplazamos todas hacia la derecha
const offsetX =
minX < 0
? Math.abs(minX) + TABLE_CONST.CANVAS_PADDING
: TABLE_CONST.CANVAS_PADDING;
return tables.map(table => ({
...table,
x: table.x + offsetX,
}));
};
export const getFieldsCount = (fields: FieldVm[]): number =>
fields.reduce((acc, field) => {
if (field.children && field.children.length > 0 && !field.isCollapsed) {
return acc + 1 + getFieldsCount(field.children);
}
return acc + 1;
}, 0);
export const calculateTableHeight = (fields: FieldVm[]): number => {
const countFieldInTable = getFieldsCount(fields);
const rowHeightTotal = countFieldInTable * TABLE_CONST.ROW_HEIGHT;
const headerHeight = TABLE_CONST.HEADER_HEIGHT;
const headerTitleGap = TABLE_CONST.HEADER_TITLE_GAP;
const bottomPadding = TABLE_CONST.ROW_PADDING;
return rowHeightTotal + headerHeight + headerTitleGap + bottomPadding;
};
export const calculateTableEndYPosition = (table: TableVm): number => {
const tableHeight = calculateTableHeight(table.fields);
return table.y + tableHeight;
};
export const doesTableOverlap = (
tableA: TableVm,
tables: TableVm[]
): boolean => {
if (tables.length === 0) {
return false;
}
const tablesMinusOriginTableCollection = tables.filter(
t => t.id !== tableA.id
);
return tablesMinusOriginTableCollection.some(tableB =>
doTablesOverlap(tableA, tableB)
);
};
export const placeTableWithoutOverlap = (
table: TableVm,
tables: TableVm[]
): TableVm => {
let newTable = table;
let attempts = 0;
while (
doesTableOverlap(newTable, tables) &&
attempts < TABLE_CONST.MAX_PLACEMENT_ATTEMPTS
) {
newTable = {
...newTable,
x: newTable.x - TABLE_CONST.TABLE_SHIFT_DISTANCE,
};
if (newTable.x < 0) {
newTable = {
...newTable,
x: TABLE_CONST.TABLE_SHIFT_DISTANCE,
y: newTable.y + TABLE_CONST.TABLE_SHIFT_DISTANCE,
};
}
attempts++;
}
return newTable;
};
export const placeAllTablesWithoutOverlap = (tables: TableVm[]): TableVm[] => {
const placedTables: TableVm[] = [];
for (const table of tables) {
const newTable = placeTableWithoutOverlap(table, placedTables);
placedTables.push(newTable);
}
return placedTables;
};
export const getMaxPositionYFromTables = (tables: TableVm[]): number =>
tables.length === 0 ? 0 : Math.max(...tables.map(calculateTableEndYPosition));
export const expandTableFields = (fields: FieldVm[]): FieldVm[] =>
fields.map(field => ({
...field,
children: field.children ? expandTableFields(field.children) : [],
isCollapsed: false,
}));
export const expandAllFieldsInTables = (table: TableVm[]) =>
table.map(table => ({
...table,
fields: expandTableFields(table.fields),
}));
// Export Schema functions
export const getPropertyJsonSchema = (field: FieldVm): string => {
if (field.isArray) {
return `"${field.name}": { bsonType: "array", items: ${getItemType(field)} }`;
}
return `"${field.name}": ${getItemType(field)}`;
};
export const getItemType = (field: FieldVm): string => {
if (field.children && field.children.length > 0) {
const properties = getPropertiesJsonSchema(field.children, false);
return `{ bsonType: "object", title: "${field.name}", properties: { ${properties}, }, }`;
// Perhaps to be changed for this later.
//return `{ bsonType: "object", title: "${field.name}", required: [${getRequiredFields(field.children)}], properties: {${properties}} } `;
}
return `{ bsonType: "${field.type}" }`;
}
export const getPropertiesJsonSchema = (
fields: FieldVm[],
useTab = true
): string => {
const separator = useTab ? ',\n ' : ', ';
return fields.map(getPropertyJsonSchema).join(separator);
};
export const getRequiredFields = (fields: FieldVm[]): string => {
return fields
.filter(field => field.isNN)
.map(field => `"${field.name}"`)
.join(', ');
};
export const getSchemaScriptFromTableVm = (table: TableVm): string => {
const properties = getPropertiesJsonSchema(table.fields);
const schemaScript = `db.createCollection("${table.tableName}", {
validator: {
$jsonSchema: {
bsonType: "object",
title: "${table.tableName}",
required: [${getRequiredFields(table.fields)}],
properties: {
${properties},
},
},
},
});`;
return schemaScript;
};
export const getSchemaScriptFromTableVmArray = (tables: TableVm[]): string => {
return tables.map(getSchemaScriptFromTableVm).join('\n\n');
};
export const getMaxEndPositionXFromNotes = (notes: NoteVm[]): number =>
notes.length === 0
? 0
: Math.max(
...notes.map(
note => note.x + (note.width ?? NOTE_CONST.DEFAULT_NOTE_WIDTH)
)
);
export const getMaxPositionYFromNotes = (notes: NoteVm[]): number => {
if (notes.length === 0) return 0;
return Math.max(
...notes.map(note => {
const noteHeight = calculateNoteAutoHeight(
note.description,
note.width ?? NOTE_CONST.DEFAULT_NOTE_WIDTH
);
return note.y + noteHeight;
})
);
};
export const normalizeNotesForExport = (
notes: NoteVm[],
offsetX: number
): NoteVm[] => {
return notes.map(note => ({
...note,
x: note.x + offsetX,
}));
};
export const getTotalCanvasWidthFromSchema = (
tables: TableVm[],
notes: NoteVm[]
): number => {
const tableWidth = getTotalCanvasWidthFromTables(tables);
const noteMaxX = getMaxEndPositionXFromNotes(notes);
return Math.max(tableWidth, noteMaxX);
};
export const getMaxPositionYFromSchema = (
tables: TableVm[],
notes: NoteVm[]
): number => {
const tableMaxY = getMaxPositionYFromTables(tables);
const noteMaxY = getMaxPositionYFromNotes(notes);
return Math.max(tableMaxY, noteMaxY);
};