-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathui.tsx
30 lines (24 loc) · 863 Bytes
/
ui.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import generate from "@babel/generator";
import { snapshotToReactFigmaAst } from "./transformers/snapshotToReactFigmaAst";
import prettier from "prettier/standalone";
import parserTypescript from "prettier/parser-typescript";
const sendToIFrame = code => {
const iframeEl = document.getElementById("editor-iframe");
if (!iframeEl) {
return;
}
const iframeWin = (iframeEl as HTMLIFrameElement).contentWindow;
iframeWin.postMessage(code, "https://figma-code-gen.now.sh/");
};
onmessage = event => {
console.log(event);
const root = event.data && event.data.pluginMessage;
const output = generate(snapshotToReactFigmaAst(root));
console.log(output.code);
const formattedCode = prettier.format(output.code, {
parser: "typescript",
plugins: [parserTypescript]
});
console.log(formattedCode);
sendToIFrame(formattedCode);
};