Skip to content
Navigation Menu
{{ message }}
forked from bernaferrari/FigmaToCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeWidthHeight.ts
More file actions
321 lines (281 loc) · 9.52 KB
/
Copy pathnodeWidthHeight.ts
File metadata and controls
321 lines (281 loc) · 9.52 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { AltSceneNode } from "../altNodes/altMixins";
export const magicMargin = 32;
type SizeResult = {
readonly width: responsive | number | null;
readonly height: responsive | number | null;
};
export const nodeWidthHeight = (
node: AltSceneNode,
allowRelative: boolean
): SizeResult => {
/// WIDTH AND HEIGHT
// if parent is a page, width can't get past w-64, therefore let it be free
// if (node.parent?.type === "PAGE" && node.width > 256) {
// return "";
// }
if (node.layoutAlign === "STRETCH" && node.layoutGrow === 1) {
return {
width: "full",
height: "full",
};
}
const [nodeWidth, nodeHeight] = getNodeSizeWithStrokes(node);
let propWidth: responsive | number | null = nodeWidth;
let propHeight: responsive | number | null = nodeHeight;
if (node.parent && "layoutMode" in node.parent) {
// Stretch means the opposite direction
if (node.layoutAlign === "STRETCH") {
switch (node.parent.layoutMode) {
case "HORIZONTAL":
propHeight = "full";
break;
case "VERTICAL":
propWidth = "full";
break;
}
}
// Grow means the same direction
if (node.layoutGrow === 1) {
if (node.parent.layoutMode === "HORIZONTAL") {
propWidth = "full";
} else {
propHeight = "full";
}
}
}
// avoid relative width when parent is relative (therefore, child is probably absolute, which doesn't work nice)
// ignore for root layer
// todo should this be kept this way? The issue is w-full which doesn't work well with absolute position.
if (allowRelative && node.parent?.isRelative !== true) {
// don't calculate again if it was already calculated
if (propWidth !== "full") {
const rW = calculateResponsiveWH(node, nodeWidth, "x");
if (rW) {
propWidth = rW;
}
}
if (propHeight !== "full") {
const rH = calculateResponsiveWH(node, nodeHeight, "y");
if (rH && node.parent) {
propHeight = rH;
}
}
}
// when any child has a relative width and parent is HORIZONTAL,
// parent must have a defined width, which wouldn't otherwise.
// todo check if the performance impact of this is worth it.
// const hasRelativeChildW =
// allowRelative &&
// "children" in node &&
// node.children.find((d) =>
// calculateResponsiveWH(d, getNodeSizeWithStrokes(d)[0], "x")
// ) !== undefined;
// when the child has the same size as the parent, don't set the size of the parent (twice)
if ("children" in node && node.children && node.children.length === 1) {
const child = node.children[0];
// detect if Frame's width is same as Child when Frame has Padding.
let hPadding = 0;
let vPadding = 0;
if ("layoutMode" in node) {
hPadding = node.paddingLeft + node.paddingRight;
vPadding = node.paddingTop + node.paddingBottom;
}
// set them independently, in case w is equal but h isn't
if (child.width === nodeWidth - hPadding) {
// propWidth = null;
}
if (child.height === nodeHeight - vPadding) {
// propHeight = null;
}
}
if ("layoutMode" in node) {
if (
(node.layoutMode === "HORIZONTAL" &&
node.counterAxisSizingMode === "AUTO") ||
(node.layoutMode === "VERTICAL" && node.primaryAxisSizingMode === "AUTO")
) {
propHeight = null;
}
if (
(node.layoutMode === "VERTICAL" &&
node.counterAxisSizingMode === "AUTO") ||
(node.layoutMode === "HORIZONTAL" &&
node.primaryAxisSizingMode === "AUTO")
) {
propWidth = null;
}
}
// On Tailwind, do not let the size be larger than 384.
if (allowRelative) {
if (
(node.type !== "RECTANGLE" && nodeHeight > 384) ||
childLargerThanMaxSize(node, "y")
) {
propHeight = null;
} else if (
(node.type !== "RECTANGLE" && nodeWidth > 384) ||
childLargerThanMaxSize(node, "x")
) {
propWidth = null;
}
}
if ("layoutMode" in node && node.layoutMode !== "NONE") {
// there is an edge case: frame with no children, layoutMode !== NONE and counterAxis = AUTO, but:
// in [altConversions] it is already solved: Frame without children becomes a Rectangle.
switch (node.layoutMode) {
case "HORIZONTAL":
return {
width: node.primaryAxisSizingMode === "FIXED" ? propWidth : null,
height: node.counterAxisSizingMode === "FIXED" ? propHeight : null,
};
case "VERTICAL":
return {
width: node.counterAxisSizingMode === "FIXED" ? propWidth : null,
height: node.primaryAxisSizingMode === "FIXED" ? propHeight : null,
};
}
} else {
return {
width: propWidth,
height: propHeight,
};
}
};
// makes the view size bigger when there is a stroke
const getNodeSizeWithStrokes = (node: AltSceneNode): Array<number> => {
let nodeHeight = node.height;
let nodeWidth = node.width;
// tailwind doesn't support OUTSIDE or CENTER, only INSIDE.
// Therefore, to give the same feeling, the height and width will be slighly increased.
// node.strokes.lenght is necessary because [strokeWeight] can exist even without strokes.
if ("strokes" in node && node.strokes && node.strokes.length) {
if (node.strokeAlign === "OUTSIDE") {
nodeHeight += node.strokeWeight * 2;
nodeWidth += node.strokeWeight * 2;
} else if (node.strokeAlign === "CENTER") {
nodeHeight += node.strokeWeight;
nodeWidth += node.strokeWeight;
}
}
if ("children" in node) {
// if any children has an OUTSIDE or CENTER stroke and, with that stroke,
// the child gets a size bigger than parent, adjust parent to be larger
node.children.forEach((d) => {
if ("strokeWeight" in d && d.strokes?.length > 0) {
if (d.strokeAlign === "OUTSIDE") {
if (nodeWidth < d.width + d.strokeWeight * 2) {
nodeWidth += d.strokeWeight * 2;
}
if (nodeHeight < d.height + d.strokeWeight * 2) {
nodeHeight += d.strokeWeight * 2;
}
} else if (d.strokeAlign === "CENTER") {
if (nodeWidth < d.width + d.strokeWeight) {
nodeWidth += d.strokeWeight;
}
if (nodeHeight < d.height + d.strokeWeight) {
nodeHeight += d.strokeWeight;
}
}
}
});
}
return [nodeWidth, nodeHeight];
};
const childLargerThanMaxSize = (node: AltSceneNode, axis: "x" | "y") => {
if ("children" in node && node.children.length > 0) {
const widthHeight: "width" | "height" = axis === "x" ? "width" : "height";
const lastChild = node.children[node.children.length - 1];
const maxLen =
lastChild[axis] + lastChild[widthHeight] - node.children[0][axis];
return maxLen > 384;
}
return false;
};
type responsive =
| ""
| "full"
| "1/2"
| "1/3"
| "2/3"
| "1/4"
| "3/4"
| "1/5"
| "1/6"
| "5/6";
const calculateResponsiveWH = (
node: AltSceneNode,
nodeWidthHeight: number,
axis: "x" | "y"
): responsive => {
let returnValue: responsive = "";
if (nodeWidthHeight > 384 || childLargerThanMaxSize(node, axis)) {
returnValue = "full";
}
if (!node.parent) {
return returnValue;
}
let parentWidthHeight;
if ("layoutMode" in node.parent && node.parent.layoutMode !== "NONE") {
if (axis === "x") {
// subtract padding from the layout width, so it can be full when compared with parent.
parentWidthHeight =
node.parent.width - node.parent.paddingLeft - node.parent.paddingRight;
} else {
// subtract padding from the layout height, so it can be full when compared with parent.
parentWidthHeight =
node.parent.height - node.parent.paddingTop - node.parent.paddingBottom;
}
} else {
parentWidthHeight = axis === "x" ? node.parent.width : node.parent.height;
}
// 0.01 of tolerance is enough for 5% of diff, i.e.: 804 / 400
const dividedWidth = nodeWidthHeight / parentWidthHeight;
const calculateResp = (div: number, str: responsive) => {
if (Math.abs(dividedWidth - div) < 0.01) {
returnValue = str;
return true;
}
return false;
};
// they will try to set the value, and if false keep calculating
const checkList: Array<[number, responsive]> = [
[1, "full"],
[1 / 2, "1/2"],
[1 / 3, "1/3"],
[2 / 3, "2/3"],
[1 / 4, "1/4"],
[3 / 4, "3/4"],
[1 / 5, "1/5"],
[1 / 6, "1/6"],
[5 / 6, "5/6"],
];
// exit the for when result is found.
let resultFound = false;
for (let i = 0; i < checkList.length && !resultFound; i++) {
const [div, resp] = checkList[i];
resultFound = calculateResp(div, resp);
}
// todo this was commented because it is almost never used. Should it be uncommented?
// if (!resultFound && isWidthFull(node, nodeWidth, parentWidth)) {
// propWidth = "full";
// }
return returnValue;
};
// set the width to max if the view is near the corner
// export const isWidthFull = (
// node: AltSceneNode,
// nodeWidth: number,
// parentWidth: number
// ): boolean => {
// // check if initial and final positions are within a magic number (currently 32)
// // this will only be reached when parent is FRAME, so node.parent.x is always 0.
// const betweenValueMargins =
// node.x <= magicMargin && parentWidth - (node.x + nodeWidth) <= magicMargin;
// // check if total width is at least 80% of the parent. This number is also a magic number and has worked fine so far.
// const betweenPercentMargins = nodeWidth / parentWidth >= 0.8;
// if (betweenValueMargins && betweenPercentMargins) {
// return true;
// }
// return false;
// };
You can’t perform that action at this time.
