Skip to content

Fixed code.js #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions dev-mode/code.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
if (figma.editorType === "dev") {
if (figma.mode === "inspect") {
figma.showUI("<h1>This is Dev Mode!</h1>");
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("<h1>This is Figma!</h1>");
figma.showUI(__html__); // Ensure you reference a proper HTML file
}
19 changes: 12 additions & 7 deletions document-change/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
}