-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
115 lines (97 loc) · 3.09 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
let active: boolean = false;
let preCount: number = 0;
let rowGap: number = 0;
let columnGap: number = 0;
let limit: number = 0;
let $target: FrameNode;
figma.showUI(__uiFiles__.notice, { width: 462, height: 160 });
figma.on("selectionchange", () => {
const selection: Readonly<SceneNode[]> = figma.currentPage.selection;
if (selection.length === 1) {
const node = selection[0];
if (node.type === "FRAME") {
if (node.children.length == 1) {
$target = node;
active = true;
} else {
active = false;
}
} else {
active = false;
}
} else {
active = false;
}
if (active === true) {
const child = $target.children[0];
const data = {
row: 0,
col: 0,
};
if ($target.layoutMode === "NONE") {
data.col = child.x;
data.row = child.y;
} else {
data.col = $target.itemSpacing;
data.row = $target.counterAxisSpacing ?? 0;
}
figma.showUI(__uiFiles__.main, { width: 420, height: 354 });
figma.ui.postMessage(data);
} else {
figma.showUI(__uiFiles__.notice, { width: 462, height: 160 });
}
});
figma.on("documentchange", () => {
sortChildObject();
});
figma.ui.onmessage = (msg) => {
if (msg.type === "apply") {
rowGap = msg.data.row;
columnGap = msg.data.col;
limit = msg.data.limit;
sortChildObject();
}
};
// 자식 요소 복제
function sortChildObject() {
if (active === true) {
const child = $target.children[0];
const width = $target.width;
const height = $target.height;
const childWidth = child.width;
const childHeight = child.height;
if ($target.layoutMode === "NONE") {
$target.horizontalPadding = child.x;
$target.verticalPadding = child.y;
}
$target.layoutMode = "HORIZONTAL";
$target.layoutWrap = "WRAP";
$target.itemSpacing = columnGap;
$target.counterAxisSpacing = rowGap;
const x = Math.floor((width - $target.horizontalPadding * 2 + columnGap) / (childWidth + columnGap));
const y = Math.floor((height - $target.verticalPadding * 2 + rowGap) / (childHeight + rowGap));
// 이전 값과 다를경우
// if (preCount !== x * y) {
for (let i = 0; i < x * y; i += 1) {
if (limit !== 0 && limit < i) {
break;
}
if ($target.children[i] === undefined) {
let copyObject = child.clone();
$target.appendChild(copyObject);
}
}
$target.children.forEach((item, i) => {
if (i !== 0) {
if (i >= x * y) {
item.remove();
}
if (limit !== 0 && limit < i) {
item.remove();
}
}
});
// }
preCount = x * y;
}
}