-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode.ts
116 lines (103 loc) · 2.93 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
// I really don't care about quality of the code, but you can make pull request to make it bettor!
// 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: NotificationHandler
let selection: ReadonlyArray<SceneNode>
let working: boolean
let count: number = 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: string) {
if (notification != null)
notification.cancel()
notification = figma.notify(text)
}
function cancel() {
if (notification != null)
notification.cancel()
if (working)
notify("Plugin work have been interrupted")
}
async function post(action, value = 1, rewrite = false, plugin = 'no-numbers') {
if (!sendStats) return
await 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',
},
})
}