Skip to content

Commit dfe9356

Browse files
committed
Total refactor part 1.
1 parent f9d42d6 commit dfe9356

31 files changed

+1666
-1289
lines changed

apps/debug/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
},
1212
"dependencies": {
1313
"backend": "workspace:*",
14-
"next": "^14.2.20",
14+
"next": "^14.2.24",
1515
"plugin-ui": "workspace:*",
1616
"react": "^18.3.1",
1717
"react-dom": "^18.3.1"
1818
},
1919
"devDependencies": {
20-
"@types/node": "^20.17.10",
21-
"@types/react": "^18.3.17",
20+
"@types/node": "^20.17.21",
21+
"@types/react": "^18.3.18",
2222
"@types/react-dom": "^18.3.5",
2323
"autoprefixer": "^10.4.20",
2424
"eslint-config-custom": "workspace:*",
25-
"postcss": "^8.4.49",
25+
"postcss": "^8.5.3",
2626
"tailwindcss": "3.4.6",
2727
"tsconfig": "workspace:*",
2828
"types": "workspace:*",
29-
"typescript": "^5.7.2"
29+
"typescript": "^5.8.2"
3030
}
3131
}

apps/plugin/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@
1010
"dev": "pnpm build:watch"
1111
},
1212
"dependencies": {
13-
"@figma/plugin-typings": "^1.105.0",
13+
"@figma/plugin-typings": "^1.108.0",
1414
"backend": "workspace:*",
1515
"plugin-ui": "workspace:*",
1616
"react": "^18.3.1",
1717
"react-dom": "^18.3.1"
1818
},
1919
"devDependencies": {
20-
"@types/node": "^20.17.10",
21-
"@types/react": "^18.3.17",
20+
"@types/node": "^20.17.21",
21+
"@types/react": "^18.3.18",
2222
"@types/react-dom": "^18.3.5",
2323
"@typescript-eslint/eslint-plugin": "^7.18.0",
2424
"@typescript-eslint/parser": "^7.18.0",
2525
"@vitejs/plugin-react": "^4.3.4",
26-
"@vitejs/plugin-react-swc": "^3.7.2",
26+
"@vitejs/plugin-react-swc": "^3.8.0",
2727
"autoprefixer": "^10.4.20",
2828
"concurrently": "^8.2.2",
2929
"esbuild": "^0.23.1",
3030
"eslint-config-custom": "workspace:*",
3131
"eslint-plugin-react-hooks": "^4.6.2",
32-
"eslint-plugin-react-refresh": "^0.4.16",
33-
"postcss": "^8.4.49",
32+
"eslint-plugin-react-refresh": "^0.4.19",
33+
"postcss": "^8.5.3",
3434
"tailwindcss": "3.4.6",
3535
"tsconfig": "workspace:*",
36-
"typescript": "^5.7.2",
3736
"types": "workspace:*",
38-
"vite": "^5.4.11",
37+
"typescript": "^5.8.2",
38+
"vite": "^5.4.14",
3939
"vite-plugin-singlefile": "^2.1.0"
4040
}
4141
}

apps/plugin/plugin-src/code.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
htmlMain,
99
postSettingsChanged,
1010
} from "backend";
11+
import { nodesToJSON } from "backend/src/code";
1112
import { retrieveGenericSolidUIColors } from "backend/src/common/retrieveUI/retrieveColors";
1213
import { flutterCodeGenTextStyles } from "backend/src/flutter/flutterMain";
1314
import { 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

4042
const 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

6271
const 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.
6980
let isLoading = false;
7081
const 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

89116
const 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

119153
const 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 () => {
243288
switch (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
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"format": "prettier --write \"**/*.{ts,tsx,css,md}\""
1111
},
1212
"devDependencies": {
13-
"eslint": "^9.17.0",
13+
"eslint": "^9.21.0",
1414
"eslint-config-custom": "workspace:*",
15-
"prettier": "^3.4.2",
16-
"turbo": "^2.3.3",
17-
"typescript": "^5.7.2"
15+
"prettier": "^3.5.2",
16+
"turbo": "^2.4.4",
17+
"typescript": "^5.8.2"
1818
}
1919
}

packages/backend/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
"lint": "eslint \"src/**/*.ts*\""
1414
},
1515
"dependencies": {
16-
"@figma/plugin-typings": "^1.105.0",
16+
"@figma/plugin-typings": "^1.108.0",
1717
"js-base64": "^3.7.7",
1818
"react": "18.3.1",
1919
"react-dom": "18.3.1",
2020
"types": "workspace:*"
2121
},
2222
"devDependencies": {
23-
"@types/react": "^18.3.17",
23+
"@types/react": "^18.3.18",
2424
"@types/react-dom": "^18.3.5",
25-
"eslint": "^9.17.0",
25+
"eslint": "^9.21.0",
2626
"eslint-config-custom": "workspace:*",
2727
"tsconfig": "workspace:*",
28-
"tsup": "^8.3.5",
29-
"typescript": "^5.7.2"
28+
"tsup": "^8.4.0",
29+
"typescript": "^5.8.2"
3030
}
3131
}

0 commit comments

Comments
 (0)