Skip to content

Commit 5885e50

Browse files
committed
Fix size when layout is optimized.
1 parent 288dfaa commit 5885e50

File tree

15 files changed

+103
-122
lines changed

15 files changed

+103
-122
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"eslint": "^8.42.0",
1212
"eslint-config-custom": "workspace:*",
1313
"prettier": "^2.8.8",
14-
"turbo": "^1.10.2"
14+
"turbo": "^1.10.3"
1515
},
1616
"packageManager": "[email protected]"
1717
}

packages/backend/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
"react-dom": "18.2.0"
2020
},
2121
"devDependencies": {
22-
"@types/react": "^18.2.6",
22+
"@types/react": "^18.2.9",
2323
"@types/react-dom": "^18.2.4",
24-
"eslint": "^8.36.0",
24+
"eslint": "^8.42.0",
2525
"eslint-config-custom": "workspace:*",
2626
"tsconfig": "workspace:*",
2727
"tsup": "^6.7.0",
28-
"typescript": "^5.0.2"
28+
"typescript": "^5.1.3"
2929
}
3030
}

packages/backend/src/common/nodeWidthHeight.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,33 @@ type SizeResult = {
33
readonly height: "fill" | number | null;
44
};
55

6-
export const nodeSize = (node: SceneNode): SizeResult => {
6+
export const nodeSize = (
7+
node: SceneNode,
8+
optimizeLayout: boolean
9+
): SizeResult => {
710
const hasLayout =
811
"layoutAlign" in node && node.parent && "layoutMode" in node.parent;
912

1013
if (!hasLayout || ("layoutMode" in node && node.layoutMode === "NONE")) {
1114
return { width: node.width, height: node.height };
1215
}
1316

14-
const parentLayoutMode = node.parent.layoutMode;
15-
// const parentLayoutMode = optimizeLayout
16-
// ? node.parent.inferredAutoLayout?.layoutMode
17-
// : null ?? node.parent.layoutMode;
17+
// const parentLayoutMode = node.parent.layoutMode;
18+
const parentLayoutMode = optimizeLayout
19+
? node.parent.inferredAutoLayout?.layoutMode
20+
: null ?? node.parent.layoutMode;
1821

19-
// const nodeAuto =
20-
// (optimizeLayout && "inferredAutoLayout" in node
21-
// ? node.inferredAutoLayout
22-
// : null) ?? node;
22+
const nodeAuto =
23+
(optimizeLayout && "inferredAutoLayout" in node
24+
? node.inferredAutoLayout
25+
: null) ?? node;
2326

2427
const isWidthFill =
25-
(parentLayoutMode === "HORIZONTAL" && node.layoutGrow === 1) ||
26-
(parentLayoutMode === "VERTICAL" && node.layoutAlign === "STRETCH");
28+
(parentLayoutMode === "HORIZONTAL" && nodeAuto.layoutGrow === 1) ||
29+
(parentLayoutMode === "VERTICAL" && nodeAuto.layoutAlign === "STRETCH");
2730
const isHeightFill =
28-
(parentLayoutMode === "HORIZONTAL" && node.layoutAlign === "STRETCH") ||
29-
(parentLayoutMode === "VERTICAL" && node.layoutGrow === 1);
31+
(parentLayoutMode === "HORIZONTAL" && nodeAuto.layoutAlign === "STRETCH") ||
32+
(parentLayoutMode === "VERTICAL" && nodeAuto.layoutGrow === 1);
3033
const modesSwapped = parentLayoutMode === "HORIZONTAL";
3134
const primaryAxisMode = modesSwapped
3235
? "counterAxisSizingMode"
@@ -38,12 +41,12 @@ export const nodeSize = (node: SceneNode): SizeResult => {
3841
return {
3942
width: isWidthFill
4043
? "fill"
41-
: "layoutMode" in node && node[primaryAxisMode] === "AUTO"
44+
: "layoutMode" in nodeAuto && nodeAuto[primaryAxisMode] === "AUTO"
4245
? null
4346
: node.width,
4447
height: isHeightFill
4548
? "fill"
46-
: "layoutMode" in node && node[counterAxisMode] === "AUTO"
49+
: "layoutMode" in nodeAuto && nodeAuto[counterAxisMode] === "AUTO"
4750
? null
4851
: node.height,
4952
};

packages/backend/src/flutter/builderImpl/flutterSize.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { sliceNum } from "../../common/numToAutoFixed";
33

44
// Used in tests.
55
export const flutterSizeWH = (node: SceneNode): string => {
6-
const fSize = flutterSize(node);
6+
const fSize = flutterSize(node, false);
77
const size = fSize.width + fSize.height;
88
return size;
99
};
1010

1111
export const flutterSize = (
12-
node: SceneNode
12+
node: SceneNode,
13+
optimizeLayout: boolean
1314
): { width: string; height: string; isExpanded: boolean } => {
14-
const size = nodeSize(node);
15+
const size = nodeSize(node, optimizeLayout);
1516
let isExpanded: boolean = false;
1617

1718
// this cast will always be true, since nodeWidthHeight was called with false to relative.

packages/backend/src/flutter/flutterContainer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const flutterContainer = (
2828

2929
// ignore for Groups
3030
const propBoxDecoration = getDecoration(node);
31-
const { width, height, isExpanded } = flutterSize(node);
31+
const { width, height, isExpanded } = flutterSize(node, optimizeLayout);
3232
const clipBehavior =
3333
"clipsContent" in node && node.clipsContent === true
3434
? "Clip.antiAlias"

packages/backend/src/flutter/flutterMain.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,9 @@ const flutterContainer = (node: SceneNode, child: string): string => {
142142
)}x${node.height.toFixed(0)}")`;
143143
}
144144

145-
// if (child.length > 0 && image.length > 0) {
146-
// const prop1 = generateWidgetCode("Positioned.fill", {
147-
// child: child,
148-
// });
149-
// const prop2 = generateWidgetCode("Positioned.fill", {
150-
// child: image,
151-
// });
152-
153-
// propChild = generateWidgetCode("Stack", {
154-
// children: [prop1, prop2],
155-
// });
156-
// }
157145
if (child.length > 0) {
158146
propChild = child;
159147
}
160-
// else if (image.length > 0) {
161-
// propChild = image;
162-
// }
163148

164149
const builder = new FlutterDefaultBuilder(propChild)
165150
.createContainer(node, localSettings.optimizeLayout)

packages/backend/src/html/builderImpl/htmlSize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { isPreviewGlobal } from "../htmlMain";
44

55
export const htmlSizePartial = (
66
node: SceneNode,
7-
isJsx: boolean
7+
isJsx: boolean,
8+
optimizeLayout: boolean
89
): { width: string; height: string } => {
910
if (isPreviewGlobal && node.parent === undefined) {
1011
return {
@@ -13,7 +14,7 @@ export const htmlSizePartial = (
1314
};
1415
}
1516

16-
const size = nodeSize(node);
17+
const size = nodeSize(node, optimizeLayout);
1718

1819
let w = "";
1920
if (typeof size.width === "number") {

packages/backend/src/html/htmlDefaultBuilder.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class HtmlDefaultBuilder {
3939
node: SceneNode & LayoutMixin & MinimalBlendMixin,
4040
optimizeLayout: boolean
4141
): this {
42-
this.size(node);
42+
this.size(node, optimizeLayout);
4343
this.autoLayoutPadding(node, optimizeLayout);
4444
this.position(node, optimizeLayout);
4545
this.blend(node);
@@ -219,8 +219,12 @@ export class HtmlDefaultBuilder {
219219
return this;
220220
}
221221

222-
size(node: SceneNode): this {
223-
const { width, height } = htmlSizePartial(node, this.isJSX);
222+
size(node: SceneNode, optimizedLayout: boolean): this {
223+
const { width, height } = htmlSizePartial(
224+
node,
225+
this.isJSX,
226+
optimizedLayout
227+
);
224228

225229
if (node.type === "TEXT") {
226230
switch (node.textAutoResize) {

packages/backend/src/html/htmlMain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const htmlGroup = (node: GroupNode, isJsx: boolean = false): string => {
8484
// while technically it shouldn't get less than 0, due to rounding errors,
8585
// it can get to values like: -0.000004196293048153166
8686
// also ignore if there are no children inside, which makes no sense
87-
if (node.width <= 0 || node.height <= 0 || node.children.length === 0) {
87+
if (node.width < 0 || node.height <= 0 || node.children.length === 0) {
8888
return "";
8989
}
9090

@@ -197,7 +197,7 @@ export const htmlContainer = (
197197
// ignore the view when size is zero or less
198198
// while technically it shouldn't get less than 0, due to rounding errors,
199199
// it can get to values like: -0.000004196293048153166
200-
if (node.width <= 0 || node.height <= 0) {
200+
if (node.width < 0 || node.height <= 0) {
201201
return children;
202202
}
203203

@@ -247,7 +247,7 @@ export const htmlSection = (
247247
): string => {
248248
const childrenStr = htmlWidgetGenerator(node.children, isJsx);
249249
const builder = new HtmlDefaultBuilder(node, showLayerName, isJsx)
250-
.size(node)
250+
.size(node, localSettings.optimizeLayout)
251251
.position(node, localSettings.optimizeLayout)
252252
.applyFillsToStyle(node.fills, "background");
253253

packages/backend/src/swiftui/builderImpl/swiftuiSize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { nodeSize } from "../../common/nodeWidthHeight";
22
import { sliceNum } from "../../common/numToAutoFixed";
33

44
export const swiftuiSize = (
5-
node: SceneNode
5+
node: SceneNode,
6+
optimizeLayout: boolean
67
): { width: string; height: string } => {
7-
const size = nodeSize(node);
8+
const size = nodeSize(node, optimizeLayout);
89

910
// if width is set as maxWidth, height must also be set as maxHeight (not height)
1011
const shouldExtend = size.height === "fill" || size.width === "fill";

0 commit comments

Comments
 (0)