import { handleCommand } from "./handlers";
// Plugin state
const state = {
serverPort: 3055,
};
// Show UI
figma.showUI(__html__, { width: 350, height: 450, themeColors: true });
// Plugin commands from UI
figma.ui.onmessage = async (msg) => {
switch (msg.type) {
case "update-settings":
updateSettings(msg);
break;
case "notify":
figma.notify(msg.message);
break;
case "close-plugin":
figma.closePlugin();
break;
case "execute-command":
// Execute commands received from UI (which gets them from WebSocket)
try {
const result = await handleCommand(msg.command, msg.params || {});
// Send result back to UI
figma.ui.postMessage({
type: "command-result",
id: msg.id,
result,
});
} catch (error) {
figma.ui.postMessage({
type: "command-error",
id: msg.id,
error:
error instanceof Error ? error.message : "Error executing command",
});
}
break;
}
};
// Listen for plugin commands from menu
figma.on("run", ({ command: _command }) => {
figma.ui.postMessage({ type: "auto-connect" });
});
// Update plugin settings
function updateSettings(settings: { serverPort?: number }) {
if (settings.serverPort) {
state.serverPort = settings.serverPort;
}
figma.clientStorage.setAsync("settings", {
serverPort: state.serverPort,
});
}
// Load saved settings on startup
async function loadSettings() {
const savedSettings = await figma.clientStorage.getAsync("settings");
if (savedSettings) {
if (savedSettings.serverPort) {
state.serverPort = savedSettings.serverPort;
}
}
figma.ui.postMessage({
type: "settings-loaded",
settings: state,
});
}
loadSettings();