-
Notifications
You must be signed in to change notification settings - Fork 3
/
code.ts
124 lines (123 loc) · 3.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
figma.showUI(__html__);
figma.ui.resize(308, 282);
const notifiedErrors = new Set<string>();
figma.ui.onmessage = (msg) => {
if (msg.type === "randomise") {
const isPositionEnbaled = msg.positionEnabled;
const isSizeEnabled = msg.sizeEnabled;
const isColorEnabled = msg.colorEnabled;
try {
const frame: FrameNode = figma.currentPage.selection[0] as FrameNode;
if (frame.type == "FRAME") {
const children = frame.children;
for (const child of children) {
if (isPositionEnbaled) {
randomisePosition(child, frame);
}
if (isSizeEnabled) {
resizeChild(child, frame);
}
if (isColorEnabled) {
colorChange(child);
}
if (!isPositionEnbaled && !isSizeEnabled && !isColorEnabled) {
figma.notify("Enable any property to randomise");
}
}
frame.resizeWithoutConstraints(frame.width, frame.height);
} else {
figma.notify("Selection is not a frame");
}
} catch (error) {
handleError(error, notifiedErrors);
}
}
if (msg.type === "close") {
figma.closePlugin();
}
function randomisePosition(child: SceneNode, frame: FrameNode) {
child.x = Math.max(
0,
Math.min(Math.random() * frame.width, frame.width - child.width)
);
child.y = Math.max(
0,
Math.min(Math.random() * frame.height, frame.height - child.height)
);
}
function resizeChild(child: SceneNode, frame: FrameNode) {
if (
child.type == "BOOLEAN_OPERATION" ||
child.type == "COMPONENT" ||
child.type == "COMPONENT_SET" ||
child.type == "ELLIPSE" ||
child.type == "FRAME" ||
child.type == "GROUP" ||
child.type == "HIGHLIGHT" ||
child.type == "INSTANCE" ||
child.type == "LINE" ||
child.type == "POLYGON" ||
child.type == "RECTANGLE" ||
child.type == "SLICE" ||
child.type == "STAMP" ||
child.type == "STAR" ||
child.type == "TEXT" ||
child.type == "VECTOR" ||
child.type == "WASHI_TAPE"
) {
const newWidth = (Math.random() * frame.width) / 4;
const newHeight = (newWidth * child.height) / child.width;
child.resize(
child.width != 0 ? newWidth : 0,
child.height != 0 ? newHeight : 0
);
}
}
function colorChange(child: SceneNode) {
if (
child.type == "BOOLEAN_OPERATION" ||
child.type == "COMPONENT" ||
child.type == "COMPONENT_SET" ||
child.type == "ELLIPSE" ||
child.type == "FRAME" ||
child.type == "HIGHLIGHT" ||
child.type == "INSTANCE" ||
child.type == "LINE" ||
child.type == "POLYGON" ||
child.type == "RECTANGLE" ||
child.type == "STAMP" ||
child.type == "STAR" ||
child.type == "TEXT" ||
child.type == "VECTOR" ||
child.type == "WASHI_TAPE"
) {
const fills = clone(child.fills);
fills[0].color.r = parseFloat(Math.random().toFixed(1));
fills[0].color.g = parseFloat(Math.random().toFixed(1));
fills[0].color.b = parseFloat(Math.random().toFixed(1));
child.fills = fills;
}
function clone(val: readonly Paint[] | typeof figma.mixed) {
return JSON.parse(JSON.stringify(val));
}
}
function handleError(error: unknown, notifiedErrors: Set<string>) {
if (error instanceof TypeError) {
if (error.message.includes("color")) {
notifyOnce("One or more shapes have no fill color", notifiedErrors);
} else if (error.message.includes("frame")) {
notifyOnce("Selection is not a frame", notifiedErrors);
} else {
notifyOnce("Something went wrong: " + error.message, notifiedErrors);
}
} else {
throw error;
}
}
function notifyOnce(message: string, notifiedErrors: Set<string>) {
if (!notifiedErrors.has(message)) {
figma.notify(message);
notifiedErrors.add(message);
}
}
};