-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
156 lines (123 loc) · 3.79 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
153
154
155
156
interface NodeLog {
id: string;
type: string;
compliant: boolean;
}
const log: Array<NodeLog> = [];
const selection = figma.currentPage.selection;
const menu = {
add: "add",
remove: "remove"
}
function isFrame(object: SceneNode) {
if (object.type === "FRAME" || object.type === "COMPONENT") return true;
return false;
}
function hasAutoLayout(object: SceneNode) {
if (isFrame(object) && object.layoutMode !== "NONE") return true;
return false;
}
function isCompliant(command: String, object: SceneNode) {
if (object.type !== "INSTANCE") {
if (command === menu.remove) return !hasAutoLayout(object);
if (command === menu.add) return hasAutoLayout(object);
}
return false;
}
function frameIt(object: SceneNode) {
const frame = figma.createFrame();
const objectIndex = object.parent.children.indexOf(object);
object.parent.insertChild(objectIndex, frame);
frame.resizeWithoutConstraints(object.width, object.height);
frame.name = object.name;
frame.x = object.x;
frame.y = object.y;
frame.fills = [];
// Moving content to frame
object.type === "GROUP"
? object.children.forEach((child) => frame.appendChild(child))
: frame.appendChild(object);
return frame;
}
function removeAutoLayout(object: SceneNode) {
if (hasAutoLayout(object)) {
object.layoutMode = "NONE";
}
return object;
}
function addAutoLayout(object: SceneNode) {
if (isFrame(object)) {
// Add auto layout to frame
if (!hasAutoLayout(object)) object.layoutMode = "VERTICAL";
return object;
}
// Add frame and auto layout
return addAutoLayout(frameIt(object));
}
function checkSum() {
const compliantCount = log.filter((object) => object.compliant).length;
return {
validated: compliantCount === selection.length ? true : false,
compliant: compliantCount,
};
}
function updateSelection(command: String) {
const toSelect = log.map((object) => {
const node = figma.getNodeById(object.id);
const type = object.type;
switch (command) {
case menu.add:
if (
type !== "FRAME" &&
type !== "COMPONENT" &&
type !== "INSTANCE" &&
type !== "GROUP"
)
return figma.getNodeById(node.parent.id);
case menu.remove:
default:
return node;
}
});
figma.currentPage.selection = toSelect;
}
function main() {
// When nothing is selected on page
if (selection.length === 0) {
if (figma.command === menu.remove) figma.notify(`Please select frames or components if you wish to remove auto layout`);
if (figma.command === menu.add) figma.notify(`Please select at least one object if you wish to apply auto layout`);
figma.closePlugin();
return;
}
for (const node of selection) {
// Loop through each selected node on the current page
const object: SceneNode = (() => {
if (figma.command === menu.remove) return removeAutoLayout(node);
if (figma.command === menu.add) return addAutoLayout(node);
})();
// Write result to check sum later
log.push({
id: object.id,
type: object.type,
compliant: isCompliant(figma.command, object)
});
}
updateSelection(figma.command);
// Notification
figma.notify(
(() => {
const sum = checkSum();
if (sum.validated) {
if (figma.command === menu.remove) return `✓ Auto layout has been removed`;
if (figma.command === menu.add) return `✓ Auto layout has been applied`;
}
if (figma.command === menu.remove)
return `Auto layout was removed from ${sum.compliant} of the selected objects`;
if (figma.command === menu.add)
return `Auto layout was applied to ${sum.compliant} of the selected objects`;
return `Something went wrong. Please contact plugin author`
})()
);
figma.closePlugin();
}
main();