-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller.ts
240 lines (227 loc) · 8.3 KB
/
controller.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
figma.skipInvisibleInstanceChildren = true;
const pluginWidth = 800;
const pluginHeight = 560;
const mapBoundary = 560;
const markerSVG: string = `<svg width="16" height="20" viewBox="0 0 16 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.56215 19.3143C7.95734 19.3143 15.0186 11.7697 15.0186 7.65163C15.0186 3.53355 11.6802 0.19519 7.56215 0.19519C3.44407 0.19519 0.105713 3.53355 0.105713 7.65163C0.105713 11.7697 7.16697 19.3143 7.56215 19.3143ZM7.56216 11.3811C9.65003 11.3811 11.3426 9.68851 11.3426 7.60064C11.3426 5.51277 9.65003 3.82022 7.56216 3.82022C5.4743 3.82022 3.78175 5.51277 3.78175 7.60064C3.78175 9.68851 5.4743 11.3811 7.56216 11.3811Z" fill="#1DAEEF"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M0.105713 7.65163C0.105713 11.7697 7.16697 19.3143 7.56215 19.3143L7.56215 11.3811C5.47429 11.381 3.78175 9.6885 3.78175 7.60064C3.78175 5.51278 5.47429 3.82023 7.56215 3.82022L7.56215 0.19519C3.44407 0.19519 0.105713 3.53355 0.105713 7.65163Z" fill="#3EC3FF"/>
</svg>`;
const allComponents = figma.currentPage.findAllWithCriteria({
types: ["COMPONENT"]
});
const allComponentsName = allComponents.map(component => component.name);
figma.showUI(__html__, {
width: pluginWidth,
height: pluginHeight
});
figma.ui.onmessage = msg => {
if (msg.type === "draw-map") {
let mapWidthOffset = (mapBoundary - msg.width) / 2;
let mapHeightOffset = (mapBoundary - msg.height) / 2;
if (figma.editorType == "figma") {
// Figma drawing
/////////
// Map //
/////////
const nodes = [];
let imageHash = figma.createImage(msg.data).hash;
const rect = figma.createRectangle();
rect.name = "Basemap image";
rect.resize(msg.width, msg.height);
rect.fills = [{ type: "IMAGE", scaleMode: "FIT", imageHash }];
figma.currentPage.appendChild(rect);
// select the rectangle and focus the viewport
figma.currentPage.selection = [rect];
figma.viewport.scrollAndZoomIntoView([rect]);
nodes.push(rect);
/////////////
// Markers //
/////////////
if (msg.markerImg == 1) {
// default marker option
// Create Master component
const markerComponent = figma.createComponent();
markerComponent.resize(16, 20);
markerComponent.name = "Default marker component";
let nodeSVG = figma.createNodeFromSvg(markerSVG);
markerComponent.appendChild(nodeSVG);
markerComponent.x = -100;
markerComponent.y = -100;
figma.currentPage.appendChild(markerComponent);
// Instances from master component
msg.markers.map(marker => {
let instanceMarker = markerComponent.createInstance();
instanceMarker.x = marker.x - mapWidthOffset;
instanceMarker.y = marker.y - mapHeightOffset;
instanceMarker.x -= instanceMarker.width / 2;
instanceMarker.y -= instanceMarker.height / 2;
figma.currentPage.appendChild(instanceMarker);
nodes.push(instanceMarker);
});
} else {
//Selected custom component
let selectedComponent = figma.root.findAll(
c => c.name === msg.markerImg && c.type === "COMPONENT"
);
msg.markers.map(marker => {
// @ts-ignore: Unreachable code error
let instanceMarker = selectedComponent[0].createInstance();
instanceMarker.x = marker.x - mapWidthOffset;
instanceMarker.y = marker.y - mapHeightOffset;
instanceMarker.x -= instanceMarker.width / 2;
instanceMarker.y -= instanceMarker.height / 2;
if (marker.name) {
for (var customProperty of instanceMarker.componentProperties) {
if (customProperty["type"] == "TEXT") {
instanceMarker.setProperties({
customProperty: marker.name
});
break;
}
}
}
figma.currentPage.appendChild(instanceMarker);
nodes.push(instanceMarker);
});
}
figma.currentPage.selection = nodes;
figma.viewport.scrollAndZoomIntoView(nodes);
const groupContainer = figma.group(nodes, figma.currentPage);
groupContainer.name = "Figmap";
////////////////////
// Figma response //
////////////////////
figma.ui.postMessage({
type: "map-drawed",
message: `Map drawed in Figma`
});
//console.log("allComponents ", allComponents);
//////////////////
// Close plugin //
//////////////////
figma.closePlugin();
} else {
// FigJam drawing
const nodes = [];
let imageHash = figma.createImage(msg.data).hash;
const rect = figma.createRectangle();
rect.name = "Basemap image";
rect.resize(msg.width, msg.height);
rect.fills = [{ type: "IMAGE", scaleMode: "FIT", imageHash }];
nodes.push(rect);
figma.currentPage.appendChild(rect);
// select the rectangle and focus the viewport
figma.currentPage.selection = [rect];
figma.viewport.scrollAndZoomIntoView([rect]);
msg.markers.map(marker => {
let nodeSVG = figma.createNodeFromSvg(markerSVG);
figma.currentPage.appendChild(nodeSVG);
nodeSVG.x = marker.x - mapWidthOffset;
nodeSVG.y = marker.y - mapHeightOffset;
nodeSVG.x -= 10;
nodeSVG.y -= 8;
nodes.push(nodeSVG);
});
figma.group(nodes, figma.currentPage);
figma.closePlugin();
}
}
if (msg.type === "get-components") {
////////////////////
// Figma response //
////////////////////
figma.ui.postMessage({
type: "components-response",
message: allComponentsName
});
}
if (msg.type === "notification") {
const notificationHandler = figma.notify("GPX file loaded", {
timeout: 6000
});
return function(): void {
notificationHandler.cancel();
};
}
if (msg.type === "update-storage") {
figma.clientStorage.setAsync("_username", msg.user);
figma.clientStorage.setAsync("_customStyleID", msg.style);
}
if (msg.type === "update-accessToken-storage") {
figma.clientStorage.setAsync("_accessToken", msg.accessToken);
}
if (msg.type === "read-storage") {
figma.clientStorage.getAsync("_username").then(result => {
figma.ui.postMessage({
type: "fetched username",
storage: result
});
});
figma.clientStorage.getAsync("_customStyleID").then(result => {
figma.ui.postMessage({
type: "fetched custom style",
storage: result
});
});
figma.clientStorage.getAsync("_accessToken").then(result => {
figma.ui.postMessage({
type: "fetched custom access token",
storage: result
});
});
// figma.notify("We restored your map. Default values were: 'ergum' 'ckg6ps8s62b5e19nrr67wqw9u'", {
// timeout: 6000
// });
}
if (msg.type === "notify-storage") {
figma.notify("💾 We restored your Mapbox user, Style ID or API token", {
timeout: 6000
});
}
if (msg.type === "ask-editorType") {
figma.ui.postMessage({
type: "sending-editor",
storage: figma.editorType
});
}
};
///////////////////
// Figma payment //
///////////////////
// Force payment status to be unpaid for development
type _PaymentStatus = {
type: "UNPAID" | "PAID";
};
const paymentStatus: _PaymentStatus = { type: "PAID" };
figma.payments.setPaymentStatusInDevelopment(paymentStatus);
//
async function runPaymentDetect() {
if (figma.payments.status.type === "UNPAID") {
const usageCount = (await figma.clientStorage.getAsync("usage-count")) || 0;
const maxUsages = 15;
if (usageCount >= maxUsages) {
await figma.payments.initiateCheckoutAsync({
interstitial: "TRIAL_ENDED"
});
if (figma.payments.status.type === "UNPAID") {
figma.notify(
"You have run out of free usages of this plugin. Buy me a ☕️ to continue using it.",
{
timeout: 6000
}
);
figma.closePlugin();
return;
} else {
figma.notify("🎉 Thanks for purchasing it.");
}
} else {
await figma.clientStorage.setAsync("usage-count", usageCount + 1);
figma.notify("ℹ️ " + usageCount + "/" + maxUsages + " free trials.", {
timeout: 6000
});
}
}
}
runPaymentDetect();
export {};