-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
74 lines (62 loc) · 1.88 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
//
// Bingo
// So far only one method: `shuffleSelection`
//
// Swaps positions randomly of the selected node's children's children
// assumes cells are grouped into rows or columns on the bingo board
// To use, select the frame that contains rows/cols, e.g.
// Bingo <Selection>
// Row
// Cell 1
// Cell 2...
// If you name a node as "FREE" it'll be put in the middle
// assumes those rows/columns are in order (to support freeNode)
const shuffleChildren = (freeNodeName: string) => {
const children = [];
const rows = [];
const wrap = [];
// Idk why I have to push a single selection into an array first
// But typescript cries if I don't do this
figma.currentPage.selection.forEach(n => {
wrap.push(n);
});
let freeNode = null;
wrap.forEach(w => {
w.children.forEach(r => {
r.children.forEach(c => {
rows.push(r);
if (c.name !== freeNodeName) {
children.push(c)
} else {
freeNode = c;
}
});
});
});
const newOrder = shuffle(children.slice());
if (freeNode !== null) {
newOrder.splice(Math.floor(newOrder.length/2), 0, freeNode);
}
rows.forEach((r, i) => {
r.appendChild(newOrder[i]);
});
}
shuffleChildren("FREE");
figma.closePlugin()
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}