-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
152 lines (127 loc) · 4.9 KB
/
code.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// This plugin will open a window to prompt the user to enter a number, and
// it will then create that many rectangles on the screen.
// This file holds the main code for the plugins. It has access to the *document*.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser environment (see documentation).
// This shows the HTML page in "ui.html".
figma.showUI(__html__, { width: 480, height: 400 });
function handleSelectionChange() {
if (
figma.currentPage.selection.length === 1 &&
figma.currentPage.selection[0].type === "TEXT"
) {
const textNode = figma.currentPage.selection[0] as TextNode;
figma.ui.postMessage({ type: "state:set-text", text: textNode.characters });
} else {
// If the selection is not a single TextNode, clear the input
figma.ui.postMessage({ type: "state:set-text", text: "" });
}
}
figma.on("selectionchange", handleSelectionChange);
// Calls to "parent.postMessage" from within the HTML page will trigger this
// callback. The callback will be passed the "pluginMessage" property of the
// posted message.
figma.ui.onmessage = async (message) => {
await figma.loadFontAsync({ family: "Inter", style: "Regular" });
let data = "";
// One way of distinguishing between different types of messages sent from
// your HTML page is to use an object with a "type" property like this.
if (message.type === "app:generate") {
const format = message.format;
if (message.text) {
data = encodeURIComponent(message.text);
} else {
if (
figma.currentPage.selection.length === 0 ||
!(figma.currentPage.selection[0].type === "TEXT")
) {
figma.ui.postMessage({ type: "state:no-text-selected" });
return;
}
const textNode = figma.currentPage.selection[0] as TextNode;
data = encodeURIComponent(textNode.characters);
}
try {
const response = await fetch(
`https://zebra-code.p.rapidapi.com/?data=${data}&type=${format}`,
{
method: "GET",
headers: {
"X-RapidAPI-Key": "9177ead490msha2000b73cf6ac13p1545c0jsn8e9e83d0fed7", // Default API Key
"X-RapidAPI-Host": "zebra-code.p.rapidapi.com",
},
}
);
const imageData = (await response.json()).image;
if (!imageData) {
if (response.status === 403) {
throw new Error("API Key is invalid or expired");
} else if (response.status === 429) {
throw new Error(
"API Request exceeded capacity."
);
} else {
throw new Error(
"Oops! Barcode generation failed, please try again later."
);
}
}
const formatName = format.replace(/\-/gm, " ");
const frame = figma.createFrame();
frame.name = formatName + " - " + decodeURIComponent(data);
frame.resize(512, 640);
const label = figma.createText();
label.characters = decodeURIComponent(message.text);
label.fontSize = 36;
label.lineHeight = { value: 48, unit: "PIXELS" };
label.textAlignHorizontal = "CENTER";
label.textAlignVertical = "CENTER";
label.x = frame.width / 2 - label.width / 2;
label.y = frame.height - 128 + label.height;
frame.appendChild(label);
const rectangle = figma.createNodeFromSvg(imageData);
rectangle.x = 20;
if (format === "QR_CODE") {
rectangle.y = 20;
rectangle.resize(frame.width - 40, frame.width - 40);
} else {
rectangle.y = frame.height / 2 - rectangle.height / 2;
rectangle.resize(frame.width - 40, rectangle.height);
const labelFormat = figma.createText();
labelFormat.characters = formatName;
labelFormat.fontSize = 48;
labelFormat.lineHeight = { value: 58, unit: "PIXELS" };
labelFormat.textAlignHorizontal = "CENTER";
labelFormat.textAlignVertical = "CENTER";
labelFormat.x = frame.width / 2 - labelFormat.width / 2;
labelFormat.y = 124;
frame.appendChild(labelFormat);
}
frame.appendChild(rectangle);
figma.currentPage.appendChild(frame);
const frames = figma.currentPage.children.filter(
(node) => node.type === "FRAME"
) as FrameNode[];
const lastFrame = frames[frames.length - 2];
if (lastFrame) {
frame.x = lastFrame.x + lastFrame.width + 20;
frame.y = lastFrame.y;
}
figma.ui.postMessage({ type: "state:finish" });
} catch (e: any) {
figma.ui.postMessage({ type: "state:finish" });
figma.notify(
e.message
? e.message
: "Oops! Barcode generation failed, please try again later.",
{
error: true,
timeout: 3000,
}
);
}
}
// Make sure to close the plugin when you're done. Otherwise the plugin will
// keep running, which shows the cancel button at the bottom of the screen.
// figma.closePlugin();
};