Skip to content

Commit eb06109

Browse files
committed
description to plugin data
1 parent f0f638b commit eb06109

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

code-snippet-saver/code.js

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ if (figma.mode === "codegen") {
2121
figma.on("selectionchange", () => {
2222
handleCurrentSelection();
2323
});
24-
2524
figma.codegen.on("generate", async (event) => {
2625
handleCurrentSelection();
2726
const pluginDataArray = findPluginDataArrayForSelection();
27+
console.log(pluginDataArray);
2828
const snippets = [];
2929
pluginDataArray.forEach((pluginData) =>
3030
JSON.parse(pluginData).forEach((a) => snippets.push(a))
@@ -40,31 +40,54 @@ if (figma.mode === "codegen") {
4040

4141
return snippets;
4242
});
43+
} else if (figma.command === "description-to-plugin-data") {
44+
descriptionToPluginData();
45+
}
46+
47+
function descriptionToPluginData() {
48+
let count = 0;
49+
figma.currentPage.selection.forEach((node) => {
50+
if (node.description) {
51+
count++;
52+
node.setPluginData(
53+
PLUGIN_DATA_KEY,
54+
JSON.stringify([
55+
{
56+
language: "JAVASCRIPT",
57+
code: node.description.replace(/\n/g, "\n"),
58+
title: node.name,
59+
},
60+
])
61+
);
62+
}
63+
});
64+
figma.notify(`Updated snippet to description for ${count} nodes`);
65+
figma.closePlugin();
66+
}
4367

44-
function findPluginDataArrayForSelection() {
45-
const data = [];
46-
function pluginDataForNode(node) {
47-
const pluginData = node.getPluginData(PLUGIN_DATA_KEY);
48-
// skipping duplicates. why?
49-
// component instances have same pluginData as mainComponent, unless they have override pluginData.
50-
if (pluginData && data.indexOf(pluginData) === -1) {
51-
data.push(pluginData);
52-
}
68+
function findPluginDataArrayForSelection() {
69+
const data = [];
70+
function pluginDataForNode(node) {
71+
const pluginData = node.getPluginData(PLUGIN_DATA_KEY);
72+
// skipping duplicates. why?
73+
// component instances have same pluginData as mainComponent, unless they have override pluginData.
74+
if (pluginData && data.indexOf(pluginData) === -1) {
75+
data.push(pluginData);
76+
}
77+
}
78+
const currentNode = figma.currentPage.selection[0];
79+
pluginDataForNode(currentNode);
80+
if (currentNode.type === "INSTANCE") {
81+
pluginDataForNode(currentNode.mainComponent);
82+
if (currentNode.mainComponent.parent.type === "COMPONENT_SET") {
83+
pluginDataForNode(currentNode.mainComponent.parent);
5384
}
54-
const currentNode = figma.currentPage.selection[0];
55-
pluginDataForNode(currentNode);
56-
if (currentNode.type === "INSTANCE") {
57-
pluginDataForNode(currentNode.mainComponent);
58-
if (currentNode.mainComponent.parent.type === "COMPONENT_SET") {
59-
pluginDataForNode(currentNode.mainComponent.parent);
60-
}
61-
} else if (currentNode.type === "COMPONENT") {
62-
if (currentNode.parent.type === "COMPONENT_SET") {
63-
pluginDataForNode(currentNode.parent);
64-
}
85+
} else if (currentNode.type === "COMPONENT") {
86+
if (currentNode.parent.type === "COMPONENT_SET") {
87+
pluginDataForNode(currentNode.parent);
6588
}
66-
return data;
6789
}
90+
return data;
6891
}
6992

7093
function handleCurrentSelection() {

code-snippet-saver/manifest.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Code Snippet Saver",
33
"id": "239174719128739123",
44
"api": "1.0.0",
5-
"editorType": ["dev"],
5+
"editorType": ["dev", "figma"],
66
"capabilities": ["codegen"],
77
"permissions": [],
88
"codegenLanguages": [{ "label": "Snippets", "value": "snippets" }],
@@ -13,6 +13,12 @@
1313
"label": "Snippet Editor"
1414
}
1515
],
16+
"menu": [
17+
{
18+
"command": "description-to-plugin-data",
19+
"name": "Description to pluginData"
20+
}
21+
],
1622
"main": "code.js",
1723
"ui": "ui.html",
1824
"networkAccess": { "allowedDomains": ["none"] }

code-snippet-saver/ui.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Document</title>
77
<style>
8+
body {
9+
margin: 0;
10+
}
811
* {
912
box-sizing: border-box;
1013
}
@@ -14,10 +17,15 @@
1417
display: block;
1518
width: 100%;
1619
}
20+
textarea {
21+
flex: 1;
22+
}
1723
main {
1824
display: flex;
1925
flex-direction: column;
2026
gap: 0.5rem;
27+
height: 100vh;
28+
padding: 0.5rem;
2129
}
2230
</style>
2331
</head>

0 commit comments

Comments
 (0)