-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathcode.ts
More file actions
109 lines (95 loc) · 2.71 KB
/
code.ts
File metadata and controls
109 lines (95 loc) · 2.71 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { retrieveTailwindText } from "./tailwind/retrieveUI/retrieveTexts";
import {
retrieveGenericLinearGradients,
retrieveGenericSolidUIColors,
} from "./common/retrieveUI/retrieveColors";
import { htmlMain } from "./html/htmlMain";
import { swiftuiMain } from "./swiftui/swiftuiMain";
import { tailwindMain } from "./tailwind/tailwindMain";
import { flutterMain } from "./flutter/flutterMain";
import { convertIntoAltNodes } from "./altNodes/altConversion";
let parentId: string;
let isJsx = false;
let layerName = false;
let material = true;
let mode: "flutter" | "swiftui" | "html" | "tailwind";
figma.showUI(__html__, { width: 450, height: 550 });
const run = () => {
// ignore when nothing was selected
if (figma.currentPage.selection.length === 0) {
figma.ui.postMessage({
type: "empty",
});
return;
}
// check [ignoreStackParent] description
if (figma.currentPage.selection.length > 0) {
parentId = figma.currentPage.selection[0].parent?.id ?? "";
}
let result = "";
const convertedSelection = convertIntoAltNodes(
figma.currentPage.selection,
null
);
// @ts-ignore
if (mode === "flutter") {
result = flutterMain(convertedSelection, parentId, material);
} else if (mode === "tailwind") {
result = tailwindMain(convertedSelection, parentId, isJsx, layerName);
} else if (mode === "swiftui") {
result = swiftuiMain(convertedSelection, parentId);
} else if (mode === "html") {
result = htmlMain(convertedSelection, parentId, isJsx, layerName);
}
console.log(result);
figma.ui.postMessage({
type: "result",
data: result,
});
if (
mode === "tailwind" ||
mode === "flutter" ||
mode === "html" ||
mode === "swiftui"
) {
figma.ui.postMessage({
type: "colors",
data: retrieveGenericSolidUIColors(convertedSelection, mode),
});
figma.ui.postMessage({
type: "gradients",
data: retrieveGenericLinearGradients(convertedSelection, mode),
});
}
if (mode === "tailwind") {
figma.ui.postMessage({
type: "text",
data: retrieveTailwindText(convertedSelection),
});
}
};
figma.on("selectionchange", () => {
run();
});
// efficient? No. Works? Yes.
// todo pass data instead of relying in types
figma.ui.onmessage = (msg) => {
if (
msg.type === "tailwind" ||
msg.type === "flutter" ||
msg.type === "swiftui" ||
msg.type === "html"
) {
mode = msg.type;
run();
} else if (msg.type === "jsx" && msg.data !== isJsx) {
isJsx = msg.data;
run();
} else if (msg.type === "layerName" && msg.data !== layerName) {
layerName = msg.data;
run();
} else if (msg.type === "material" && msg.data !== material) {
material = msg.data;
run();
}
};