88 htmlMain ,
99 postSettingsChanged ,
1010} from "backend" ;
11+ import { nodesToJSON } from "backend/src/code" ;
1112import { retrieveGenericSolidUIColors } from "backend/src/common/retrieveUI/retrieveColors" ;
1213import { flutterCodeGenTextStyles } from "backend/src/flutter/flutterMain" ;
1314import { htmlCodeGenTextStyles } from "backend/src/html/htmlMain" ;
@@ -30,6 +31,7 @@ export const defaultPluginSettings: PluginSettings = {
3031 customTailwindColors : false ,
3132 customTailwindPrefix : "" ,
3233 embedImages : false ,
34+ embedVectors : false ,
3335} ;
3436
3537// A helper type guard to ensure the key belongs to the PluginSettings type
@@ -38,8 +40,13 @@ function isKeyOfPluginSettings(key: string): key is keyof PluginSettings {
3840}
3941
4042const getUserSettings = async ( ) => {
43+ console . log ( "[DEBUG] getUserSettings - Starting to fetch user settings" ) ;
4144 const possiblePluginSrcSettings =
4245 ( await figma . clientStorage . getAsync ( "userPluginSettings" ) ) ?? { } ;
46+ console . log (
47+ "[DEBUG] getUserSettings - Raw settings from storage:" ,
48+ possiblePluginSrcSettings ,
49+ ) ;
4350
4451 const updatedPluginSrcSettings = {
4552 ...defaultPluginSettings ,
@@ -57,46 +64,72 @@ const getUserSettings = async () => {
5764 } ;
5865
5966 userPluginSettings = updatedPluginSrcSettings as PluginSettings ;
67+ console . log ( "[DEBUG] getUserSettings - Final settings:" , userPluginSettings ) ;
68+ return userPluginSettings ;
6069} ;
6170
6271const initSettings = async ( ) => {
72+ console . log ( "[DEBUG] initSettings - Initializing plugin settings" ) ;
6373 await getUserSettings ( ) ;
6474 postSettingsChanged ( userPluginSettings ) ;
75+ console . log ( "[DEBUG] initSettings - Calling safeRun with settings" ) ;
6576 safeRun ( userPluginSettings ) ;
6677} ;
6778
6879// Used to prevent running from happening again.
6980let isLoading = false ;
7081const safeRun = async ( settings : PluginSettings ) => {
82+ console . log (
83+ "[DEBUG] safeRun - Called with isLoading =" ,
84+ isLoading ,
85+ "selection =" ,
86+ figma . currentPage . selection ,
87+ ) ;
7188 if ( isLoading === false ) {
7289 try {
7390 isLoading = true ;
91+ console . log ( "[DEBUG] safeRun - Starting run execution" ) ;
7492 await run ( settings ) ;
93+ console . log ( "[DEBUG] safeRun - Run execution completed" ) ;
7594 // hack to make it not immediately set to false when complete. (executes on next frame)
7695 setTimeout ( ( ) => {
96+ console . log ( "[DEBUG] safeRun - Resetting isLoading to false" ) ;
7797 isLoading = false ;
7898 } , 1 ) ;
7999 } catch ( e ) {
100+ console . log ( "[DEBUG] safeRun - Error caught in execution" ) ;
101+ isLoading = false ; // Make sure to reset the flag on error
80102 if ( e && typeof e === "object" && "message" in e ) {
81103 const error = e as Error ;
82104 console . log ( "error: " , error . stack ) ;
83105 figma . ui . postMessage ( { type : "error" , error : error . message } ) ;
84106 }
85107 }
108+ } else {
109+ console . log (
110+ "[DEBUG] safeRun - Skipping execution because isLoading =" ,
111+ isLoading ,
112+ ) ;
86113 }
87114} ;
88115
89116const standardMode = async ( ) => {
117+ console . log ( "[DEBUG] standardMode - Starting standard mode initialization" ) ;
90118 figma . showUI ( __html__ , { width : 450 , height : 700 , themeColors : true } ) ;
91119 await initSettings ( ) ;
92120
93121 // Listen for selection changes
94122 figma . on ( "selectionchange" , ( ) => {
123+ console . log (
124+ "[DEBUG] selectionchange event - New selection:" ,
125+ figma . currentPage . selection ,
126+ ) ;
95127 safeRun ( userPluginSettings ) ;
96128 } ) ;
97129
98130 // Listen for document changes
99131 figma . on ( "documentchange" , ( ) => {
132+ console . log ( "[DEBUG] documentchange event triggered" ) ;
100133 // Node: This was causing an infinite load when you try to export a background image from a group that contains children.
101134 // The reason for this is that the code will temporarily hide the children of the group in order to export a clean image
102135 // then restores the visibility of the children. This constitutes a document change so it's restarting the whole conversion.
@@ -105,10 +138,11 @@ const standardMode = async () => {
105138 } ) ;
106139
107140 figma . ui . onmessage = ( msg ) => {
108- console . log ( "[node ] figma.ui.onmessage" , msg ) ;
141+ console . log ( "[DEBUG ] figma.ui.onmessage" , msg ) ;
109142
110143 if ( msg . type === "pluginSettingWillChange" ) {
111144 const { key, value } = msg as SettingWillChangeMessage < unknown > ;
145+ console . log ( `[DEBUG] Setting changed: ${ key } = ${ value } ` ) ;
112146 ( userPluginSettings as any ) [ key ] = value ;
113147 figma . clientStorage . setAsync ( "userPluginSettings" , userPluginSettings ) ;
114148 safeRun ( userPluginSettings ) ;
@@ -117,13 +151,24 @@ const standardMode = async () => {
117151} ;
118152
119153const codegenMode = async ( ) => {
154+ console . log ( "[DEBUG] codegenMode - Starting codegen mode initialization" ) ;
120155 // figma.showUI(__html__, { visible: false });
121156 await getUserSettings ( ) ;
122157
123158 figma . codegen . on (
124159 "generate" ,
125160 async ( { language, node } : CodegenEvent ) : Promise < CodegenResult [ ] > => {
126- const convertedSelection = convertIntoNodes ( [ node ] , null ) ;
161+ console . log (
162+ `[DEBUG] codegen.generate - Language: ${ language } , Node:` ,
163+ node ,
164+ ) ;
165+
166+ const nodeJson = await nodesToJSON ( [ node ] ) ;
167+ const convertedSelection = await convertIntoNodes ( nodeJson , null ) ;
168+ console . log (
169+ "[DEBUG] codegen.generate - Converted selection:" ,
170+ convertedSelection ,
171+ ) ;
127172
128173 switch ( language ) {
129174 case "html" :
@@ -243,11 +288,14 @@ const codegenMode = async () => {
243288switch ( figma . mode ) {
244289 case "default" :
245290 case "inspect" :
291+ console . log ( "[DEBUG] Starting plugin in" , figma . mode , "mode" ) ;
246292 standardMode ( ) ;
247293 break ;
248294 case "codegen" :
295+ console . log ( "[DEBUG] Starting plugin in codegen mode" ) ;
249296 codegenMode ( ) ;
250297 break ;
251298 default :
299+ console . log ( "[DEBUG] Unknown plugin mode:" , figma . mode ) ;
252300 break ;
253301}
0 commit comments