-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode.ts
148 lines (122 loc) · 4.32 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
const step = 64;
const sortByDepth = (a: SceneNode, b: SceneNode) => a.parent.children.indexOf(a) - b.parent.children.indexOf(b);
const getValidParent = (node: SceneNode) => {
let parent = node.parent;
while (parent) {
if (parent.type == "INSTANCE" || parent.type == "COMPONENT" || parent.type == "COMPONENT_SET") return parent;
parent = parent.parent;
}
return null;
}
const isAutoLayout = (node: BaseNode & ChildrenMixin) => {
const al = node as AutoLayoutMixin;
return (al.layoutMode && al.layoutMode != "NONE") || (al.layoutWrap && al.layoutWrap == "WRAP");
}
const selection = figma.currentPage.selection;
let note = null;
const select = [];
if (selection.length > 0) {
const instances: string[] = [];
const groups: Map<string, SceneNode[]> = new Map();
const positions: Map<string, { x: number, y: number }> = new Map();
selection.forEach(node => {
const pn = node.parent.id;
const sets = groups.has(pn) ? groups.get(pn) : groups.set(pn, []).get(pn);
sets.push(node);
});
groups.forEach(nodes => {
nodes.sort(sortByDepth);
const [first] = nodes;
const validParent = getValidParent(first);
if (validParent?.type == "INSTANCE") {
instances.push(validParent.name);
return;
}
let posParent = (validParent ?? first.parent) as SceneNode;
let parent = validParent?.parent ?? first.parent;
if (parent.type == "COMPONENT_SET") {
posParent = parent as SceneNode;
parent = parent.parent;
}
if (posParent && !positions.has(posParent.id)) {
positions.set(posParent.id, {x: posParent.x + posParent.width + step, y: posParent.y});
}
const nodeParent = first.parent as SceneNode & ChildrenMixin;
let groupSaver: RectangleNode;
if (nodeParent.type == "GROUP") {
groupSaver = figma.createRectangle();
groupSaver.resize(0.01, 0.01);
nodeParent.appendChild(groupSaver);
}
nodes.forEach(node => {
if (!validParent) {
if (node.type == "COMPONENT" || node.type == "COMPONENT_SET")
{
note = `Cannot create a component from a ${node.type == "COMPONENT" ? "component" : "component set"}.`;
return;
}
figma.createComponentFromNode(node);
return;
}
const alParent = nodeParent as AutoLayoutMixin;
let component: ComponentNode;
const settings: Vector & ConstraintMixin = {
x: node.x,
y: node.y,
constraints: (node as ConstraintMixin).constraints,
};
const rotation = (node as LayoutMixin).rotation;
(node as LayoutMixin).rotation = 0;
if (alParent.layoutMode != "NONE" || alParent.layoutWrap == "WRAP") {
const alChild = node as AutoLayoutChildrenMixin;
const alSettings: AutoLayoutChildrenMixin = {
layoutAlign: alChild.layoutAlign,
layoutGrow: alChild.layoutGrow,
layoutPositioning: alChild.layoutPositioning,
};
parent.appendChild(node);
component = figma.createComponentFromNode(node);
const instance = component.createInstance();
nodeParent.appendChild(instance);
instance.layoutAlign = alSettings.layoutAlign;
instance.layoutGrow = alSettings.layoutGrow;
instance.layoutPositioning = alSettings.layoutPositioning;
if (instance.layoutPositioning == "ABSOLUTE") {
instance.x = settings.x;
instance.y = settings.y;
if (settings.constraints)
instance.constraints = settings.constraints;
}
instance.rotation = rotation;
} else {
parent.appendChild(node);
component = figma.createComponentFromNode(node);
const instance = component.createInstance();
nodeParent.appendChild(instance);
instance.x = settings.x;
instance.y = settings.y;
if (settings.constraints)
instance.constraints = settings.constraints;
instance.rotation = rotation;
}
parent.insertChild(parent.children.indexOf(posParent) + 1, component);
if (!isAutoLayout(parent)) {
const pos = positions.get(posParent.id);
component.x = pos.x;
component.y = pos.y;
pos.x += component.width + step;
}
select.push(component);
});
if (groupSaver) {
groupSaver.remove();
}
});
if (instances.length > 0) {
const instanceNames = instances.join(", ");
note = `Skipping node${instances.length > 1 ? `s "${instanceNames}"` : ` "${instanceNames}"`} inside an instance, try again in the main component.`;
}
}
if (note) figma.notify(note, {timeout: note.length * 50});
if (select.length > 0) figma.currentPage.selection = select;
figma.closePlugin();