From ffa0a2f182baa46a967b88aaf794dc4f6e08b0d4 Mon Sep 17 00:00:00 2001 From: Sanket Date: Wed, 26 Feb 2025 09:56:37 +0530 Subject: [PATCH 1/2] improved the code.js(Incorrect codegen.on("generate") Return Format) --- dev-mode/code.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dev-mode/code.js b/dev-mode/code.js index fe7b980..b1e58bc 100644 --- a/dev-mode/code.js +++ b/dev-mode/code.js @@ -1,11 +1,13 @@ if (figma.editorType === "dev") { if (figma.mode === "inspect") { - figma.showUI("

This is Dev Mode!

"); + figma.showUI(__html__); // Ensure you define __html__ in your plugin UI } else if (figma.mode === "codegen") { - figma.codegen.on("generate", () => [ - { title: "Codegen", code: "This is codegen!", language: "PLAINTEXT" }, - ]); + figma.codegen.on("generate", () => { + return [ + { title: "Codegen", code: "This is codegen!", language: "PLAINTEXT" } + ]; + }); } } else if (figma.editorType === "figma") { - figma.showUI("

This is Figma!

"); + figma.showUI(__html__); // Ensure you reference a proper HTML file } From 808a491890360c359f1d686e7c3ac6bda7f10d3a Mon Sep 17 00:00:00 2001 From: Sanket Date: Wed, 26 Feb 2025 10:02:53 +0530 Subject: [PATCH 2/2] afeguards figma.loadAllPagesAsync() in case it's unavailable. Removes { origin: "*" } from figma.ui.postMessage (unnecessary in Figma Plugins). Prevents possible undefined access on change.style before accessing .name. Uses type !== "STYLE_CREATE" && type !== "STYLE_DELETE" instead of noop for better readability. --- document-change/code.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/document-change/code.ts b/document-change/code.ts index 3d2a35f..15b1c34 100644 --- a/document-change/code.ts +++ b/document-change/code.ts @@ -3,24 +3,29 @@ figma.showUI(__html__, { height: 600, width: 600 }); void initialize(); async function initialize() { - await figma.loadAllPagesAsync(); + if ("loadAllPagesAsync" in figma) { + await figma.loadAllPagesAsync(); + } + figma.on("documentchange", (event) => { const messages = event.documentChanges.map(documentChangeAsString); - figma.ui.postMessage(messages, { origin: "*" }); + figma.ui.postMessage(messages); }); } -function documentChangeAsString(change: DocumentChange) { +function documentChangeAsString(change: DocumentChange): string { const { origin, type } = change; const list: string[] = [origin, type]; + if (type === "PROPERTY_CHANGE") { list.push(change.node.type, change.properties.join(", ")); } else if (type === "STYLE_PROPERTY_CHANGE") { - list.push(change.style?.name, change.properties.join(", ")); - } else if (type === "STYLE_CREATE" || type === "STYLE_DELETE") { - // noop - } else { + if (change.style) { + list.push(change.style.name, change.properties.join(", ")); + } + } else if (type !== "STYLE_CREATE" && type !== "STYLE_DELETE") { list.push(change.node.type); } + return list.join(" "); }