-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.js
More file actions
executable file
·199 lines (199 loc) · 6.96 KB
/
code.js
File metadata and controls
executable file
·199 lines (199 loc) · 6.96 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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());
});
};
const delay = ms => new Promise(res => setTimeout(res, ms));
var totalTimers = 0;
const secondsSet = [86400, 3600, 60, 1];
var pause = false;
var reset = false;
var userSetSeconds = 0;
const uiWindow = {
minHeight: 60,
maxHeight: 300,
helptextHeight: 200,
width: 220,
};
var timerUIHeight = 50;
if (figma.command === 'timer') {
figma.showUI(__html__, { width: uiWindow.width, height: uiWindow.minHeight });
}
figma.showUI(__html__, { width: uiWindow.width, height: uiWindow.minHeight });
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;
totalTimers = 0;
figma.ui.resize(uiWindow.width, uiWindow.minHeight);
break;
case 'helpon':
figma.ui.resize(uiWindow.width, uiWindow.helptextHeight);
break;
case 'helpoff':
figma.ui.resize(uiWindow.width, uiWindow.minHeight);
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() {
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() {
const nodes = figma.currentPage.findAll(node => node.type === "TEXT" && node.characters.startsWith("Timer:"));
nodes.forEach(start);
return nodes.length > 0;
}
function start(node) {
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);
//set plugin relaunch data for easy launching/installing of plugin
node.setRelaunchData({ timer: '' });
}
function getRemainingSeconds(timeString) {
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) {
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, template) {
const trimmedTemplate = template.substring(0, template.length - timeString.length);
return trimmedTemplate + timeString;
}
function secondsToInterval(seconds) {
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;
}
/**
* Code that updates all timers on the Figma stage
* will also send updates / messages to UI.html, so we can show timers there
*/
function startTimer(node, seconds, template, startsWithTimer) {
return __awaiter(this, void 0, void 0, function* () {
yield figma.loadFontAsync(node.fontName);
totalTimers += 1;
console.log("Timer started / became active");
var timerID = totalTimers;
var keepItRunning = true;
var secondsToGo = seconds;
var eventType = "start timer";
var newText = "";
adjustUIWindowHeight();
postMessageToUIWindow(eventType, newText, timerID, secondsToGo, seconds);
// this loop updates all timers every second
while (keepItRunning) {
// checking if reset was clicked by user and if so resetting all timers
if (reset) {
newText = fillUpTimeStringWithTemplate(secondsToInterval(seconds), template);
keepItRunning = false;
updateTimerText(startsWithTimer, newText, node);
}
else if (!pause) {
if (secondsToGo > 0) {
newText = fillUpTimeStringWithTemplate(secondsToInterval(secondsToGo), template);
eventType = "counting";
}
else {
newText = "Done";
eventType = "timer done";
}
postMessageToUIWindow(eventType, newText, timerID, secondsToGo, seconds);
updateTimerText(startsWithTimer, newText, node);
secondsToGo -= 1;
}
yield delay(1000);
}
console.log("Timer finished / became in-active");
});
}
function postMessageToUIWindow(eventType, timerText, timerID, secondsToGo, secondsToStart) {
figma.ui.postMessage([eventType, timerText, timerID, secondsToGo, secondsToStart]);
}
function updateTimerText(startsWithTimer, newText, node) {
if (startsWithTimer) {
newText = "Timer: " + newText;
}
node.characters = newText;
}
// adjusting height of UI windows depending on amount of timers
function adjustUIWindowHeight() {
var newUIHeight = 100 + totalTimers * 50;
if (newUIHeight > uiWindow.maxHeight) {
newUIHeight = uiWindow.maxHeight;
}
figma.ui.resize(uiWindow.width, newUIHeight);
}