forked from bernaferrari/FigmaToCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
235 lines (219 loc) · 6.13 KB
/
Copy pathcode.ts
File metadata and controls
235 lines (219 loc) · 6.13 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
import { tailwindCodeGenTextStyles } from "./../../../packages/backend/src/tailwind/tailwindMain";
import {
run,
flutterMain,
tailwindMain,
swiftuiMain,
convertIntoNodes,
htmlMain,
PluginSettings,
} from "backend";
import { retrieveGenericSolidUIColors } from "backend/src/common/retrieveUI/retrieveColors";
import { flutterCodeGenTextStyles } from "backend/src/flutter/flutterMain";
import { htmlCodeGenTextStyles } from "backend/src/html/htmlMain";
import { swiftUICodeGenTextStyles } from "backend/src/swiftui/swiftuiMain";
let userPluginSettings: PluginSettings;
const defaultPluginSettings: PluginSettings = {
framework: "HTML",
jsx: false,
optimizeLayout: true,
layerName: false,
inlineStyle: true,
responsiveRoot: false,
flutterGenerationMode: "snippet",
swiftUIGenerationMode: "snippet",
roundTailwindValues: false,
roundTailwindColors: false,
customTailwindColors: false,
};
// A helper type guard to ensure the key belongs to the PluginSettings type
function isKeyOfPluginSettings(key: string): key is keyof PluginSettings {
return key in defaultPluginSettings;
}
const getUserSettings = async () => {
const possiblePluginSrcSettings =
(await figma.clientStorage.getAsync("userPluginSettings")) ?? {};
const updatedPluginSrcSettings = {
...defaultPluginSettings,
...Object.keys(defaultPluginSettings).reduce((validSettings, key) => {
if (
isKeyOfPluginSettings(key) &&
key in possiblePluginSrcSettings &&
typeof possiblePluginSrcSettings[key] ===
typeof defaultPluginSettings[key]
) {
validSettings[key] = possiblePluginSrcSettings[key] as any;
}
return validSettings;
}, {} as Partial<PluginSettings>),
};
userPluginSettings = updatedPluginSrcSettings as PluginSettings;
};
const initSettings = async () => {
await getUserSettings();
figma.ui.postMessage({
type: "pluginSettingChanged",
data: userPluginSettings,
});
safeRun(userPluginSettings);
};
const safeRun = (settings: PluginSettings) => {
try {
run(settings);
} catch (e) {
if (e && typeof e === "object" && "message" in e) {
console.log("error: ", (e as any).stack);
figma.ui.postMessage({
type: "error",
data: e.message,
});
}
}
};
const standardMode = async () => {
figma.showUI(__html__, { width: 450, height: 550, themeColors: true });
await initSettings();
figma.on("selectionchange", () => {
safeRun(userPluginSettings);
});
figma.ui.onmessage = (msg) => {
console.log("[node] figma.ui.onmessage", msg);
if (msg.type === "pluginSettingChanged") {
(userPluginSettings as any)[msg.key] = msg.value;
figma.clientStorage.setAsync("userPluginSettings", userPluginSettings);
// figma.ui.postMessage({
// type: "pluginSettingChanged",
// data: userPluginSettings,
// });
safeRun(userPluginSettings);
}
};
};
const codegenMode = async () => {
// figma.showUI(__html__, { visible: false });
await getUserSettings();
figma.codegen.on("generate", ({ language, node }) => {
const convertedSelection = convertIntoNodes([node], null);
switch (language) {
case "html":
return [
{
title: `Code`,
code: htmlMain(
convertedSelection,
{ ...userPluginSettings, jsx: false },
true
),
language: "HTML",
},
{
title: `Text Styles`,
code: htmlCodeGenTextStyles(false),
language: "HTML",
},
];
case "html_jsx":
return [
{
title: `Code`,
code: htmlMain(
convertedSelection,
{ ...userPluginSettings, jsx: true },
true
),
language: "HTML",
},
{
title: `Text Styles`,
code: htmlCodeGenTextStyles(true),
language: "HTML",
},
];
case "tailwind":
case "tailwind_jsx":
return [
{
title: `Code`,
code: tailwindMain(convertedSelection, {
...userPluginSettings,
jsx: language === 'tailwind_jsx',
}),
language: "HTML",
},
// {
// title: `Style`,
// code: tailwindMain(convertedSelection, defaultPluginSettings),
// language: "HTML",
// },
{
title: `Tailwind Colors`,
code: retrieveGenericSolidUIColors("Tailwind")
.map((d) => {
let str = `${d.hex};`
if (d.colorName !== d.hex) {
str += ` // ${d.colorName}`
}
if (d.meta) {
str += ` (${d.meta})`
}
return str;
})
.join("\n"),
language: "JAVASCRIPT",
},
{
title: `Text Styles`,
code: tailwindCodeGenTextStyles(),
language: "HTML",
},
];
case "flutter":
return [
{
title: `Code`,
code: flutterMain(convertedSelection, {
...userPluginSettings,
flutterGenerationMode: "snippet",
}),
language: "SWIFT",
},
{
title: `Text Styles`,
code: flutterCodeGenTextStyles(),
language: "SWIFT",
},
];
case "swiftUI":
return [
{
title: `SwiftUI`,
code: swiftuiMain(convertedSelection, {
...userPluginSettings,
swiftUIGenerationMode: "snippet",
}),
language: "SWIFT",
},
{
title: `Text Styles`,
code: swiftUICodeGenTextStyles(),
language: "SWIFT",
},
];
default:
break;
}
const blocks: CodegenResult[] = [];
return blocks;
});
};
switch (figma.mode) {
case "default":
case "inspect":
standardMode();
break;
case "codegen":
codegenMode();
break;
default:
break;
}