This repository was archived by the owner on Mar 23, 2026. It is now read-only.
forked from lennet/Figma-Timer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
executable file
·184 lines (153 loc) · 4.45 KB
/
code.ts
File metadata and controls
executable file
·184 lines (153 loc) · 4.45 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const delay = ms => new Promise(res => setTimeout(res, ms));
var activeTimer = 0;
const secondsSet = [86400, 3600, 60, 1];
var pause = false;
var reset = false;
var userSetSeconds = 0;
figma.showUI(__html__, { width: 220, height: 50 })
figma.ui.onmessage = msg => {
switch (msg.type) {
case 'start':
pause = false;
reset = false;
checkAndStart();
break;
case 'pause':
pause = true;
break;
case 'continue':
pause = false;
break;
case 'reset':
reset = true;
pause = true;
break;
case 'helpon':
figma.ui.resize(220, 200);
break;
case 'helpoff':
figma.ui.resize(220, 50);
break;
default:
console.log("no code for msg.type: " + msg.type);
break;
}
};
function checkAndStart() {
if (checkForSelectedNodes() == false) {
if (checkForNodesThatBeginWithTimer() == false) {
throw new Error("Type the time to start Timer");
}
}
}
function checkForSelectedNodes(): boolean {
const regex = new RegExp("[0-9]{1,2}(:[0-9]{1,2})*");
const selectedNodes = figma.currentPage.selection.filter(node => node.type == "TEXT" && regex.test(node.characters));
selectedNodes.forEach(start);
return selectedNodes.length > 0;
}
function checkForNodesThatBeginWithTimer(): boolean {
const nodes = figma.currentPage.findAll(node => node.type === "TEXT" && node.characters.startsWith("Timer:"));
nodes.forEach(start);
return nodes.length > 0;
}
function start(node: TextNode) {
var timeString = node.characters;
var startsWithTimer = false;
if (timeString.startsWith("Timer:")) {
timeString = timeString.replace("Timer: ", "");
startsWithTimer = true;
}
var seconds = getRemainingSeconds(timeString);
var template = getTemplateFromString(timeString);
startTimer(node, seconds, template, startsWithTimer);
}
function getRemainingSeconds(timeString: string): number {
var seconds = 0;
var components = timeString.split(":");
secondsSet.reverse();
components.reverse().forEach((element, index) => {
var factor = secondsSet[index];
seconds += factor * Number(element);
});
secondsSet.reverse();
return seconds;
}
/**
* Generates a template string from a timeString
* e.g. converts 5:00 into 0:00
*/
function getTemplateFromString(timeString: string): string {
var result = "";
for (const c of timeString) {
if (c == ":") {
result += c;
} else {
result += "0";
}
}
return result;
}
/**
* Creates a new time string that conforms to the templates format
* e.g. 5:00 (timeString) and 00:00:00 (template) will return 00:05:00
*/
function fillUpTimeStringWithTemplate(timeString: string, template: string): string {
const trimmedTemplate = template.substring(0, template.length - timeString.length)
return trimmedTemplate + timeString;
}
function secondsToInterval(seconds: number): string {
var result = "";
var secondsToGo = seconds;
secondsSet.forEach((element) => {
var count = Math.floor(secondsToGo / element);
if (count > 0 || result.length > 0) {
secondsToGo -= count * element;
if (result.length > 0) {
result += ":";
if (count < 10) {
result += "0";
}
}
result += String(count);
}
})
return result;
}
async function startTimer(node: TextNode, seconds: number, template: string, startsWithTimer: boolean) {
await figma.loadFontAsync(node.fontName as FontName);
activeTimer += 1;
console.log("Timer started / became active");
var timerID = activeTimer;
var keepItRunning = true;
var secondsToGo = seconds;
var newText = "";
while (keepItRunning) {
// checking if reset was clicked by user and if so resetting all timers
if (reset) {
secondsToGo = seconds;
newText = fillUpTimeStringWithTemplate(secondsToInterval(secondsToGo), template);
if (startsWithTimer) {
newText = "Timer: " + newText;
}
node.characters = newText;
keepItRunning = false;
};
// checking if pause was NOT clicked
if (!pause) {
if (secondsToGo > 0) {
newText = fillUpTimeStringWithTemplate(secondsToInterval(secondsToGo), template);
if (startsWithTimer) {
newText = "Timer: " + newText;
}
node.characters = newText;
secondsToGo -= 1;
} else if (secondsToGo < 1) {
node.characters = "Done";
}
}
await delay(1000);
}
console.log("Timer finished / became in-active");
activeTimer -= 1;
}