Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figma codegen for Slint in dev-mode and vscode extension #7555

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions tools/figma-inspector/backend/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ import {
dispatchTS,
getStatus,
updateUI,
getSlintSnippet,
} from "./utils/code-utils";

figma.showUI(__html__, {
themeColors: true,
width: 400,
height: 320,
});

listenTS("copyToClipboard", () => {
figma.notify("Copied!");
});

figma.on("selectionchange", () => {
updateUI();
});

// init
updateUI();
if (figma.editorType === "dev" && figma.mode === "codegen") {
figma.codegen.on("generate", async ({ node }) => {
const slintSnippet = await getSlintSnippet();
return [
{
title: "Slint Code: " + node.name,
language: "CSS",
code: slintSnippet,
},
];
});
}
17 changes: 15 additions & 2 deletions tools/figma-inspector/backend/utils/code-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ function transformStyle(styleObj: StyleObject): string {
}

if (value.includes("linear-gradient")) {
return `${finalKey}: @${finalValue}`;
return ` ${finalKey}: @${finalValue}`;
}

return `${finalKey}: ${finalValue}`;
return ` ${finalKey}: ${finalValue}`;
});

return filteredEntries.length > 0 ? `${filteredEntries.join(";\n")};` : "";
Expand All @@ -117,3 +117,16 @@ export async function updateUI() {

dispatchTS("updatePropertiesCallback", { title, slintProperties });
}

export async function getSlintSnippet(): Promise<string> {
const cssProperties = await figma.currentPage.selection[0].getCSSAsync();
const slintProperties = transformStyle(cssProperties);

let elementName = "Rectangle";
const node = figma.currentPage.selection[0].type;
if (node === "TEXT") {
elementName = "Text";
}

return `${elementName} {\n${slintProperties}\n}`;
}
5 changes: 4 additions & 1 deletion tools/figma-inspector/figma.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export const manifest: PluginManifest = {
api: "1.0.0",
main: "code.js",
ui: "index.html",
editorType: ["figma", "dev"],
editorType: ["dev"],
capabilities: ["codegen", "vscode"],
codegenLanguages: [{ label: "Slint", value: "slint" }],
codegenPreferences: [],
documentAccess: "dynamic-page",
networkAccess: {
allowedDomains: ["*"],
Expand Down
Loading