@@ -21,10 +21,10 @@ if (figma.mode === "codegen") {
21
21
figma . on ( "selectionchange" , ( ) => {
22
22
handleCurrentSelection ( ) ;
23
23
} ) ;
24
-
25
24
figma . codegen . on ( "generate" , async ( event ) => {
26
25
handleCurrentSelection ( ) ;
27
26
const pluginDataArray = findPluginDataArrayForSelection ( ) ;
27
+ console . log ( pluginDataArray ) ;
28
28
const snippets = [ ] ;
29
29
pluginDataArray . forEach ( ( pluginData ) =>
30
30
JSON . parse ( pluginData ) . forEach ( ( a ) => snippets . push ( a ) )
@@ -40,31 +40,54 @@ if (figma.mode === "codegen") {
40
40
41
41
return snippets ;
42
42
} ) ;
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
+ }
43
67
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 ) ;
53
84
}
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 ) ;
65
88
}
66
- return data ;
67
89
}
90
+ return data ;
68
91
}
69
92
70
93
function handleCurrentSelection ( ) {
0 commit comments