Skip to content

Commit 2886e64

Browse files
authored
Warning log (#144)
* Added a method for recording and displaying warnings when doing conversions. * Added icon and changed color to yellow. * added dark theme (only slightly darker) * Accidentally missed several merge conflicts in rebase * more merge fixes * Fixed issue with empty selection causing weird errors with syntax highlighter
1 parent ba4b9c3 commit 2886e64

File tree

13 files changed

+130
-41
lines changed

13 files changed

+130
-41
lines changed

apps/debug/pages/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export default function Web() {
66
const [selectedFramework, setSelectedFramework] =
77
React.useState<FrameworkTypes>("HTML");
88

9+
const testWarnings = ["This is an example of a conversion warning message."];
10+
911
return (
1012
<div className="flex flex-col p-8 space-y-2">
1113
<p className="text-3xl font-bold">Debug Mode</p>
@@ -19,14 +21,14 @@ export default function Web() {
1921
code={
2022
"backend:dev: CJS dist/index.js 105.74 KB backend:dev: CJS ⚡️ Build success in 419ms plugin:dev: warn - Port 3000 is in use, trying 3001 instead. plugin:dev: warn - Port 3001 is in use, trying 3002 instead."
2123
}
22-
emptySelection={false}
2324
selectedFramework={selectedFramework}
2425
setSelectedFramework={setSelectedFramework}
2526
htmlPreview={null}
2627
settings={undefined}
2728
onPreferenceChanged={() => {}}
2829
colors={[]}
2930
gradients={[]}
31+
warnings={testWarnings}
3032
/>
3133
</div>
3234
</div>
@@ -38,12 +40,12 @@ export default function Web() {
3840
code={"code goes hereeeee"}
3941
selectedFramework={selectedFramework}
4042
setSelectedFramework={setSelectedFramework}
41-
emptySelection={false}
4243
htmlPreview={null}
4344
settings={undefined}
4445
onPreferenceChanged={() => {}}
4546
colors={[]}
4647
gradients={[]}
48+
warnings={testWarnings}
4749
/>
4850
</div>
4951
</div>

apps/plugin/ui-src/App.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SolidColorConversion,
1111
ErrorMessage,
1212
SettingsChangedMessage,
13+
Warning,
1314
} from "types";
1415
import { postUISettingsChangingMessage } from "./messaging";
1516

@@ -21,6 +22,7 @@ interface AppState {
2122
settings: PluginSettings | null;
2223
colors: SolidColorConversion[];
2324
gradients: LinearGradientConversion[];
25+
warnings: Warning[];
2426
}
2527

2628
const emptyPreview = { size: { width: 0, height: 0 }, content: "" };
@@ -33,6 +35,7 @@ export default function App() {
3335
settings: null,
3436
colors: [],
3537
gradients: [],
38+
warnings: [],
3639
});
3740

3841
const rootStyles = getComputedStyle(document.documentElement);
@@ -68,8 +71,9 @@ export default function App() {
6871
// const emptyMessage = untypedMessage as EmptyMessage;
6972
setState((prevState) => ({
7073
...prevState,
71-
code: "// No layer is selected.",
74+
code: "",
7275
htmlPreview: emptyPreview,
76+
warnings: [],
7377
colors: [],
7478
gradients: [],
7579
}));
@@ -131,7 +135,7 @@ export default function App() {
131135
<div className={`${figmaColorBgValue === "#ffffff" ? "" : "dark"}`}>
132136
<PluginUI
133137
code={state.code}
134-
emptySelection={false}
138+
warnings={state.warnings}
135139
selectedFramework={state.selectedFramework}
136140
setSelectedFramework={handleFrameworkChange}
137141
htmlPreview={state.htmlPreview}

packages/backend/src/code.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import { generateHTMLPreview, htmlMain } from "./html/htmlMain";
88
import { postConversionComplete, postEmptyMessage } from "./messaging";
99
import { swiftuiMain } from "./swiftui/swiftuiMain";
1010
import { tailwindMain } from "./tailwind/tailwindMain";
11+
import { clearWarnings, warnings } from "./common/commonConversionWarnings";
1112
import { PluginSettings } from "types";
1213

1314
export const run = (settings: PluginSettings) => {
15+
clearWarnings();
1416
const selection = figma.currentPage.selection;
15-
1617
const convertedSelection = convertIntoNodes(selection, null);
1718

1819
// ignore when nothing was selected
@@ -47,6 +48,7 @@ export const run = (settings: PluginSettings) => {
4748
colors,
4849
gradients,
4950
settings,
51+
warnings: [...warnings],
5052
});
5153
};
5254

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Warning } from "types";
2+
3+
export const warnings = new Set<Warning>();
4+
export const addWarning = (warning: Warning) => {
5+
if (warnings.has(warning) === false) {
6+
console.warn(warning);
7+
}
8+
warnings.add(warning);
9+
};
10+
export const clearWarnings = () => warnings.clear();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { rgbTo8hex, gradientAngle } from "../../common/color";
2+
import { addWarning } from "../../common/commonConversionWarnings";
23
import { generateWidgetCode, sliceNum } from "../../common/numToAutoFixed";
34
import { retrieveTopFill } from "../../common/retrieveFill";
45
import { nearestValue } from "../../tailwind/conversionTables";
@@ -50,6 +51,7 @@ export const flutterBoxDecorationColor = (
5051
};
5152

5253
export const flutterDecorationImage = (node: SceneNode, fill: ImagePaint) => {
54+
addWarning("Image fills are replaced with placeholders");
5355
return generateWidgetCode("DecorationImage", {
5456
image: `NetworkImage("https://via.placeholder.com/${node.width.toFixed(
5557
0,
@@ -82,7 +84,7 @@ export const flutterGradient = (fill: GradientPaint): string => {
8284
case "GRADIENT_ANGULAR":
8385
return flutterAngularGradient(fill);
8486
default:
85-
// Diamond gradient is unsupported.
87+
addWarning("Diamond dradients are not supported in Flutter");
8688
return "";
8789
}
8890
};

packages/backend/src/flutter/flutterMain.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "./builderImpl/flutterAutoLayout";
1414
import { commonSortChildrenWhenInferredAutoLayout } from "../common/commonChildrenOrder";
1515
import { PluginSettings } from "types";
16+
import { addWarning } from "../common/commonConversionWarnings";
1617

1718
let localSettings: PluginSettings;
1819
let previousExecutionCache: string[];
@@ -113,6 +114,9 @@ const flutterWidgetGenerator = (
113114
case "TEXT":
114115
comp.push(flutterText(node));
115116
break;
117+
case "VECTOR":
118+
addWarning("VectorNodes are not supported in Flutter");
119+
break;
116120
default:
117121
// do nothing
118122
}
@@ -143,6 +147,7 @@ const flutterContainer = (node: SceneNode, child: string): string => {
143147

144148
let image = "";
145149
if ("fills" in node && retrieveTopFill(node.fills)?.type === "IMAGE") {
150+
addWarning("Image fills are replaced with placeholders");
146151
image = `Image.network("https://via.placeholder.com/${node.width.toFixed(
147152
0,
148153
)}x${node.height.toFixed(0)}")`;

packages/backend/src/html/htmlMain.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { htmlAutoLayoutProps } from "./builderImpl/htmlAutoLayout";
66
import { formatWithJSX } from "../common/parseJSX";
77
import { commonSortChildrenWhenInferredAutoLayout } from "../common/commonChildrenOrder";
88
import { PluginSettings } from "types";
9+
import { addWarning } from "../common/commonConversionWarnings";
910

1011
let showLayerNames = false;
1112

@@ -87,6 +88,8 @@ const htmlWidgetGenerator = (
8788
break;
8889
case "VECTOR":
8990
comp += htmlAsset(node, isJsx);
91+
addWarning("VectorNodes are not fully supported in HTML");
92+
break;
9093
}
9194
});
9295

@@ -208,6 +211,7 @@ export const htmlAsset = (node: SceneNode, isJsx: boolean = false): string => {
208211
let tag = "div";
209212
let src = "";
210213
if (retrieveTopFill(node.fills)?.type === "IMAGE") {
214+
addWarning("Image fills are replaced with placeholders");
211215
tag = "img";
212216
src = ` src="https://via.placeholder.com/${node.width.toFixed(
213217
0,
@@ -249,6 +253,7 @@ export const htmlContainer = (
249253
let tag = "div";
250254
let src = "";
251255
if (retrieveTopFill(node.fills)?.type === "IMAGE") {
256+
addWarning("Image fills are replaced with placeholders");
252257
if (!("children" in node) || node.children.length === 0) {
253258
tag = "img";
254259
src = ` src="https://via.placeholder.com/${node.width.toFixed(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { retrieveTopFill } from "../../common/retrieveFill";
22
import { gradientAngle } from "../../common/color";
33
import { nearestValue } from "../../tailwind/conversionTables";
44
import { sliceNum } from "../../common/numToAutoFixed";
5+
import { addWarning } from "../../common/commonConversionWarnings";
56

67
export const swiftUISolidColor = (fill: Paint): string => {
78
if (fill && fill.type === "SOLID") {
@@ -58,6 +59,7 @@ export const swiftuiBackground = (
5859
} else if (fill?.type === "GRADIENT_LINEAR") {
5960
return swiftuiGradient(fill);
6061
} else if (fill?.type === "IMAGE") {
62+
addWarning("Image fills are replaced with placeholders");
6163
return `AsyncImage(url: URL(string: "https://via.placeholder.com/${node.width.toFixed(
6264
0,
6365
)}x${node.height.toFixed(0)}"))`;

packages/backend/src/swiftui/swiftuiMain.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SwiftuiTextBuilder } from "./swiftuiTextBuilder";
44
import { SwiftuiDefaultBuilder } from "./swiftuiDefaultBuilder";
55
import { commonSortChildrenWhenInferredAutoLayout } from "../common/commonChildrenOrder";
66
import { PluginSettings } from "types";
7+
import { addWarning } from "../common/commonConversionWarnings";
78

89
let localSettings: PluginSettings;
910
let previousExecutionCache: string[];
@@ -85,6 +86,9 @@ const swiftuiWidgetGenerator = (
8586
case "TEXT":
8687
comp.push(swiftuiText(node));
8788
break;
89+
case "VECTOR":
90+
addWarning("VectorNodes are not supported in SwiftUI");
91+
break;
8892
default:
8993
break;
9094
}

packages/backend/src/tailwind/builderImpl/tailwindColor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
nearestValue,
77
} from "../conversionTables";
88
import { TailwindColorType } from "types";
9+
import { addWarning } from "../../common/commonConversionWarnings";
910

1011
/**
1112
* Get a tailwind color value object
@@ -90,6 +91,10 @@ export const tailwindGradientFromFills = (
9091
return tailwindGradient(fill);
9192
}
9293

94+
addWarning(
95+
"Gradients are not fully supported in Tailwind except for Linear Gradients.",
96+
);
97+
9398
return "";
9499
};
95100

0 commit comments

Comments
 (0)