forked from nirsky/figma-plugin-react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.ts
85 lines (79 loc) · 2.76 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
/** @format */
const HEIGHT_PLUGIN = 860;
const WIDTH_PLUGIN = 1024;
const HEIGHT_MINIFY_PLUGIN = 132;
const WIDTH_MINIFY_PLUGIN = 132;
const STEP_RESIZE = 30;
figma.showUI(__html__, { width: WIDTH_PLUGIN, height: HEIGHT_PLUGIN });
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
// figma.clientStorage
// .getAsync('size')
// .then((size) => {
// if (size)
// {
// figma.ui.resize(size.w, size.h);
// }
// })
// .catch((err) => {
// console.error(err);
// });
figma.ui.onmessage = async (msg) => {
switch (msg.type) {
case "check-onboarding":
const onBoardingDone = await figma.clientStorage.getAsync("onBoardingDone");
figma.ui.postMessage({
type: "onboarding-status",
// onBoardingDone: !!onBoardingDone,
onBoardingDone: false
});
break;
case "onboarding-done":
figma.clientStorage.setAsync("onBoardingDone", true);
break;
case "onboarding-undone":
figma.clientStorage.setAsync("onBoardingDone", false);
break;
case "minify-plugin":
const adjustWidth= Math.round(STEP_RESIZE*1.2);
let widthPlugin = WIDTH_PLUGIN;
for(let i=HEIGHT_PLUGIN; i > HEIGHT_MINIFY_PLUGIN; i-=STEP_RESIZE)
{
widthPlugin -= adjustWidth;
figma.ui.resize(widthPlugin, i);
await sleep(8);
}
figma.ui.resize(WIDTH_MINIFY_PLUGIN, HEIGHT_MINIFY_PLUGIN);
break;
case "unminify-plugin":
const adjustWidthMinify = Math.round(STEP_RESIZE*1.2);
let widthPluginMinify = WIDTH_MINIFY_PLUGIN;
for(let i=HEIGHT_MINIFY_PLUGIN; i < HEIGHT_PLUGIN; i+=STEP_RESIZE)
{
widthPluginMinify += adjustWidthMinify;
figma.ui.resize(widthPluginMinify, i);
await sleep(8);
}
figma.ui.resize(WIDTH_PLUGIN, HEIGHT_PLUGIN);
break;
case "paste-stage":
const rect = figma.createRectangle();
const { imageData } = msg;
const img = figma.createImage(imageData);
const { width, height } = await img.getSizeAsync();
rect.resize(width, height);
rect.fills = [
{
type: "IMAGE",
imageHash: img.hash,
scaleMode: "FILL",
},
];
figma.currentPage.appendChild(rect);
const nodes = [rect];
figma.viewport.scrollAndZoomIntoView(nodes);
break;
default:
break;
}
// figma.closePlugin();
};