-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode.js
114 lines (114 loc) · 3.86 KB
/
code.js
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
// I really don't care about quality of the code, but you can make pull request to make it bettor!
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
// Constants
const confirmMsgs = ["Done!", "You got it!", "Aye!", "Is that all?", "My job here is done.", "Gotcha!", "It wasn't hard.", "Got it! What's next?"];
const renameMsgs = ["Renamed", "Affected", "Made it with", "No numbered", "Cleared"];
const idleMsgs = ["No numbers, already", "I see no layers with numbers", "Any default numbers? I can't see it", "Nothing to do, your layers are great"];
const regex = /^\D\w+(?= \d+)/g;
// Affected layers
const dict = [
"Frame",
"Group",
"Slice",
"Rectangle",
"Line",
"Arrow",
"Ellipse",
"Polygon",
"Star",
"Vector",
"Component",
"Section",
"image",
];
// Stats
const sendStats = false;
// Variables
let notification;
let selection;
let working;
let count = 0;
figma.on("currentpagechange", cancel);
// Main + Elements Check
post("started");
const start = Date.now();
working = true;
selection = figma.currentPage.selection;
console.log(selection.length + " selected");
if (selection.length)
for (const node of selection)
recursiveRename(node);
else
recursiveRename(figma.currentPage);
finish();
function recursiveRename(node) {
if (node.type !== "PAGE") {
const match = node.name.match(regex);
const index = (match && match.length > 0) ? dict.indexOf(match[0]) : -1;
if (index >= 0) {
count++;
node.name = dict[index];
}
}
if ("children" in node) {
for (const child of node.children) {
recursiveRename(child);
}
}
}
function finish() {
working = false;
figma.root.setRelaunchData({ relaunch: '' });
// Notification
if (count > 0) {
notify(confirmMsgs[Math.floor(Math.random() * confirmMsgs.length)] +
" " + renameMsgs[Math.floor(Math.random() * renameMsgs.length)] +
" " + ((count === 1) ? "only one layer" : (count + " layers")));
const time = (Date.now() - start) / 1000;
console.log("Renamed " + count + " layers in " + time + " seconds");
post("renamed", count);
post("runned-for", time).then(() => { figma.closePlugin(); });
setTimeout(() => { console.log("Timeouted"), figma.closePlugin(); }, 5000);
}
else {
notify(idleMsgs[Math.floor(Math.random() * idleMsgs.length)]);
figma.closePlugin();
}
}
function notify(text) {
if (notification != null)
notification.cancel();
notification = figma.notify(text);
}
function cancel() {
if (notification != null)
notification.cancel();
if (working)
notify("Plugin work have been interrupted");
}
function post(action, value = 1, rewrite = false, plugin = 'no-numbers') {
return __awaiter(this, void 0, void 0, function* () {
if (!sendStats)
return;
yield fetch("https://new.qurle.net/api/plugins", {
method: 'PATCH',
body: JSON.stringify({
'key': plugin,
'field': action,
'value': value,
'rewrite': rewrite
}),
headers: {
'Content-Type': 'application/json',
},
});
});
}