Skip to content

Commit 71a8d8d

Browse files
committed
Save settings.
1 parent 6f3095e commit 71a8d8d

File tree

3 files changed

+146
-157
lines changed

3 files changed

+146
-157
lines changed

apps/plugin/plugin-src/code.ts

Lines changed: 137 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const defaultPluginSettings: PluginSettings = {
2424
responsiveRoot: false,
2525
flutterGenerationMode: "snippet",
2626
swiftUIGenerationMode: "snippet",
27+
roundTailwind: false,
2728
};
2829

2930
// A helper type guard to ensure the key belongs to the PluginSettings type
@@ -98,169 +99,148 @@ const standardMode = async () => {
9899
};
99100
};
100101

102+
const codegenMode = async () => {
103+
// figma.showUI(__html__, { visible: false });
104+
await getUserSettings();
105+
106+
figma.codegen.on("generate", ({ language, node }) => {
107+
const convertedSelection = convertIntoNodes([node], null);
108+
109+
switch (language) {
110+
case "html":
111+
return [
112+
{
113+
title: `Code`,
114+
code: htmlMain(
115+
convertedSelection,
116+
{ ...userPluginSettings, jsx: false },
117+
true
118+
),
119+
language: "HTML",
120+
},
121+
{
122+
title: `Text Styles`,
123+
code: htmlCodeGenTextStyles(false),
124+
language: "HTML",
125+
},
126+
];
127+
case "html_jsx":
128+
return [
129+
{
130+
title: `Code`,
131+
code: htmlMain(
132+
convertedSelection,
133+
{ ...userPluginSettings, jsx: true },
134+
true
135+
),
136+
language: "HTML",
137+
},
138+
{
139+
title: `Text Styles`,
140+
code: htmlCodeGenTextStyles(true),
141+
language: "HTML",
142+
},
143+
];
144+
case "tailwind":
145+
return [
146+
{
147+
title: `Code`,
148+
code: tailwindMain(convertedSelection, {
149+
...userPluginSettings,
150+
jsx: false,
151+
}),
152+
language: "HTML",
153+
},
154+
{
155+
title: `Colors`,
156+
code: retrieveGenericSolidUIColors("Tailwind")
157+
.map((d) => `#${d.hex} <- ${d.colorName}`)
158+
.join("\n"),
159+
language: "HTML",
160+
},
161+
{
162+
title: `Text Styles`,
163+
code: tailwindCodeGenTextStyles(),
164+
language: "HTML",
165+
},
166+
];
167+
case "tailwind_jsx":
168+
return [
169+
{
170+
title: `Code`,
171+
code: tailwindMain(convertedSelection, {
172+
...userPluginSettings,
173+
jsx: true,
174+
}),
175+
language: "HTML",
176+
},
177+
// {
178+
// title: `Style`,
179+
// code: tailwindMain(convertedSelection, defaultPluginSettings),
180+
// language: "HTML",
181+
// },
182+
{
183+
title: `Colors`,
184+
code: retrieveGenericSolidUIColors("Tailwind")
185+
.map((d) => `#${d.hex} <- ${d.colorName}`)
186+
.join("\n"),
187+
language: "HTML",
188+
},
189+
{
190+
title: `Text Styles`,
191+
code: tailwindCodeGenTextStyles(),
192+
language: "HTML",
193+
},
194+
];
195+
case "flutter":
196+
return [
197+
{
198+
title: `Code`,
199+
code: flutterMain(convertedSelection, {
200+
...userPluginSettings,
201+
flutterGenerationMode: "snippet",
202+
}),
203+
language: "SWIFT",
204+
},
205+
{
206+
title: `Text Styles`,
207+
code: flutterCodeGenTextStyles(),
208+
language: "SWIFT",
209+
},
210+
];
211+
case "swiftUI":
212+
return [
213+
{
214+
title: `SwiftUI`,
215+
code: swiftuiMain(convertedSelection, {
216+
...userPluginSettings,
217+
swiftUIGenerationMode: "snippet",
218+
}),
219+
language: "SWIFT",
220+
},
221+
{
222+
title: `Text Styles`,
223+
code: swiftUICodeGenTextStyles(),
224+
language: "SWIFT",
225+
},
226+
];
227+
default:
228+
break;
229+
}
230+
231+
const blocks: CodegenResult[] = [];
232+
return blocks;
233+
});
234+
};
235+
101236
switch (figma.mode) {
102237
case "default":
103238
case "inspect":
104239
standardMode();
105240
break;
106241
case "codegen":
107-
initSettings();
108-
// figma.codegen.on("preferenceschange", (preferences) => {
109-
110-
// });
111-
figma.codegen.on("generate", ({ language, node }) => {
112-
const convertedSelection = convertIntoNodes([node], null);
113-
114-
switch (language) {
115-
case "html":
116-
return [
117-
{
118-
title: `Code`,
119-
code: htmlMain(
120-
convertedSelection,
121-
{ ...defaultPluginSettings, jsx: false },
122-
true
123-
),
124-
language: "HTML",
125-
},
126-
{
127-
title: `Text Styles`,
128-
code: htmlCodeGenTextStyles(false),
129-
language: "HTML",
130-
},
131-
];
132-
case "html_jsx":
133-
return [
134-
{
135-
title: `Code`,
136-
code: htmlMain(
137-
convertedSelection,
138-
{ ...defaultPluginSettings, jsx: true },
139-
true
140-
),
141-
language: "HTML",
142-
},
143-
{
144-
title: `Text Styles`,
145-
code: htmlCodeGenTextStyles(true),
146-
language: "HTML",
147-
},
148-
];
149-
case "tailwind":
150-
return [
151-
{
152-
title: `Code`,
153-
code: tailwindMain(convertedSelection, {
154-
...defaultPluginSettings,
155-
jsx: false,
156-
}),
157-
language: "HTML",
158-
},
159-
{
160-
title: `Colors`,
161-
code: retrieveGenericSolidUIColors("Tailwind")
162-
.map((d) => `#${d.hex} <- ${d.colorName}`)
163-
.join("\n"),
164-
language: "HTML",
165-
},
166-
{
167-
title: `Text Styles`,
168-
code: tailwindCodeGenTextStyles(),
169-
language: "HTML",
170-
},
171-
];
172-
case "tailwind_jsx":
173-
return [
174-
{
175-
title: `Code`,
176-
code: tailwindMain(convertedSelection, {
177-
...defaultPluginSettings,
178-
jsx: true,
179-
}),
180-
language: "HTML",
181-
},
182-
// {
183-
// title: `Style`,
184-
// code: tailwindMain(convertedSelection, defaultPluginSettings),
185-
// language: "HTML",
186-
// },
187-
{
188-
title: `Colors`,
189-
code: retrieveGenericSolidUIColors("Tailwind")
190-
.map((d) => `#${d.hex} <- ${d.colorName}`)
191-
.join("\n"),
192-
language: "HTML",
193-
},
194-
{
195-
title: `Text Styles`,
196-
code: tailwindCodeGenTextStyles(),
197-
language: "HTML",
198-
},
199-
];
200-
case "flutter":
201-
return [
202-
{
203-
title: `Code`,
204-
code: flutterMain(convertedSelection, {
205-
...defaultPluginSettings,
206-
flutterGenerationMode: "snippet",
207-
}),
208-
language: "SWIFT",
209-
},
210-
{
211-
title: `Text Styles`,
212-
code: flutterCodeGenTextStyles(),
213-
language: "SWIFT",
214-
},
215-
];
216-
case "swiftUI":
217-
return [
218-
{
219-
title: `SwiftUI`,
220-
code: swiftuiMain(convertedSelection, defaultPluginSettings),
221-
language: "SWIFT",
222-
},
223-
{
224-
title: `Text Styles`,
225-
code: swiftUICodeGenTextStyles(),
226-
language: "SWIFT",
227-
},
228-
];
229-
default:
230-
break;
231-
}
232-
233-
const blocks: CodegenResult[] = [
234-
// {
235-
// title: `Code`,
236-
// code: tailwindMain(convertedSelection, defaultPluginSettings),
237-
// language: "HTML",
238-
// },
239-
// {
240-
// title: `Flutter`,
241-
// code: flutterMain(convertedSelection, defaultPluginSettings),
242-
// language: "SWIFT",
243-
// },
244-
// {
245-
// title: `SwiftUI`,
246-
// code: swiftuiMain(convertedSelection, node.parent?.id),
247-
// language: "SWIFT",
248-
// },
249-
// {
250-
// title: `Settings`,
251-
// code: "To change settings, export to\n CodeSandbox, and see a preview,\n click in the 'Plugins' tab above",
252-
// language: "JSON",
253-
// },
254-
// {
255-
// title: `Tailwind Colors`,
256-
// code: JSON.stringify(colors).split(", ").join(",\n"),
257-
// language: "JSON",
258-
// },
259-
];
260-
261-
figma.showUI(__html__, { visible: false });
262-
return blocks;
263-
});
242+
codegenMode();
243+
break;
264244
default:
265245
break;
266246
}

packages/backend/src/code.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type PluginSettings = {
1919
responsiveRoot: boolean;
2020
flutterGenerationMode: string;
2121
swiftUIGenerationMode: string;
22+
roundTailwind: boolean;
2223
};
2324

2425
export const run = (settings: PluginSettings) => {

packages/plugin-ui/src/PluginUI.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type PluginSettings = {
1515
responsiveRoot: boolean;
1616
flutterGenerationMode: string;
1717
swiftUIGenerationMode: string;
18+
roundTailwind: boolean;
1819
};
1920

2021
type PluginUIProps = {
@@ -201,6 +202,13 @@ export const preferenceOptions: LocalCodegenPreference[] = [
201202
isDefault: false,
202203
includedLanguages: ["HTML", "Tailwind"],
203204
},
205+
{
206+
itemType: "individual_select",
207+
propertyName: "roundTailwind",
208+
label: "Round to Tailwind",
209+
isDefault: false,
210+
includedLanguages: ["Tailwind"],
211+
},
204212
// Add your preferences data here
205213
];
206214

0 commit comments

Comments
 (0)