-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
421 lines (362 loc) · 11 KB
/
app.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// User input's DOM elements
const avatar = document.getElementById('avatar');
const fileName = document.getElementById('file-name');
const reset = document.getElementById('reset');
const fullname = document.getElementById('name');
const username = document.getElementById('username');
const message = document.getElementById('message');
const time = document.getElementById('time');
const date = document.getElementById('date');
const client = document.getElementById('client');
const retweets = document.getElementById('retweets');
const quotes = document.getElementById('quotes');
const likes = document.getElementById('likes');
// Capturing all Radio buttons
const themeRadios = document.getElementsByName('theme_radio');
const verifiedRadios = document.getElementsByName('verified_radio');
// Preview's DOM elements
const tweetBox = document.getElementById('tweet_box');
const tweet = document.getElementById('tweet');
const tweetAvatar = document.getElementById('tweet_avatar');
const tweetName = document.getElementById('tweet_name');
const tweetVerified = document.getElementById('tweet_verified');
const tweetUsername = document.getElementById('tweet_username');
const tweetMessage = document.getElementById('tweet_message');
const tweetTime = document.getElementById('tweet_time');
const tweetDate = document.getElementById('tweet_date');
const tweetClient = document.getElementById('tweet_client');
const tweetRetweets = document.getElementById('tweet_retweets');
const tweetQuotes = document.getElementById('tweet_quotes');
const tweetLikes = document.getElementById('tweet_likes');
// Capturing Download button
const download = document.getElementById('download');
// Month names
const MONTHS = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
// Theme
let themeColor = '#ffffff';
// Number Formatter for Retweets, Quote Tweets and Likes
function numberFormatter(num, fixed) {
// terminate early
if (num === null) {
return null;
}
// terminate early
if (num === 0) {
return '0';
}
fixed = !fixed || fixed < 0 ? 0 : fixed; // number of decimal places to show
let b = num.toPrecision(2).split('e'), // get power
k = b.length === 1 ? 0 : Math.floor(Math.min(b[1].slice(1), 14) / 3), // floor at decimals, ceiling at trillions
c =
k < 1
? num.toFixed(0 + fixed)
: (num / Math.pow(10, k * 3)).toFixed(1 + fixed), // divide by power
d = c < 0 ? c : Math.abs(c), // enforce -0 is 0
e = d + ['', 'K', 'M', 'B', 'T'][k]; // append power
return e;
}
// Show the uploaded file's name
function showFileName(name) {
fileName.classList.add('show');
fileName.innerText = name;
}
// Render Profile Picture in Tweet
function renderProfilePicture() {
const [file] = avatar.files;
if (file) {
showFileName(file.name);
tweetAvatar.src = URL.createObjectURL(file);
}
}
// Reset the profile picture to default
function resetProfilePicture() {
fileName.innerText = '';
fileName.classList.remove('show');
tweetAvatar.src = 'assets/silhoutte.png';
}
// Render Name in Tweet
function renderName() {
const nameValue = fullname.value.trim();
if (nameValue === '') {
tweetName.innerText = 'Name';
} else {
tweetName.innerText = nameValue;
}
const characterCountEl = fullname.nextElementSibling.querySelector('.count');
characterCountEl.innerText = nameValue.length;
}
// Render Username in Tweet
function renderUsername() {
const usernameValue = username.value.trim();
if (usernameValue === '') {
tweetUsername.innerText = 'username';
} else {
tweetUsername.innerText = usernameValue;
}
const characterCountEl =
username.parentElement.nextElementSibling.querySelector('.count');
characterCountEl.innerText = usernameValue.length;
}
// Render Message in Tweet
function renderMessage() {
const messageValue = message.value.trim();
if (messageValue === '') {
tweetMessage.innerText = 'Generate convincing fake tweet images';
} else {
tweetMessage.innerText = '';
messageValue.split(' ').forEach((token) => {
if (token.match(/^@(\w){1,20}$/)) {
const spanEl = document.createElement('span');
spanEl.className = 'highlight';
spanEl.innerText = token;
tweetMessage.append(spanEl);
tweetMessage.append(' ');
} else if (token.match(/^@(\w){21,}$/)) {
const spanEl = document.createElement('span');
spanEl.className = 'highlight';
spanEl.innerText = token.slice(0, 21);
tweetMessage.append(spanEl);
tweetMessage.append(token.slice(21));
tweetMessage.append(' ');
} else if (token.match(/^@\w+/)) {
const spanEl = document.createElement('span');
spanEl.className = 'highlight';
spanEl.innerText = token.match(/^@\w+/);
tweetMessage.append(spanEl);
tweetMessage.append(token.match(/(?<=\w)\W+/));
tweetMessage.append(' ');
} else {
tweetMessage.append(token);
tweetMessage.append(' ');
}
});
// To preserve line breaks
tweetMessage.innerHTML = tweetMessage.innerHTML.replace(/\n/g, '<br>\n');
}
let test = `hey there @shashi how are you?`;
const characterCountEl = message.nextElementSibling.querySelector('.count');
characterCountEl.innerText = messageValue.length;
}
// Render Time in Tweet
function renderTime() {
const timeValue = time.value.trim();
if (timeValue === '') {
tweetTime.innerText = getCurrentTime();
} else {
tweetTime.innerText = timeValue;
}
}
// Render Date in Tweet
function renderDate() {
const dateValue = date.value.trim();
if (dateValue === '') {
tweetDate.innerText = getCurrentDate();
} else {
tweetDate.innerText = dateValue;
}
}
// Render Client in Tweet
function renderClient() {
const clientValue = client.value.trim();
if (clientValue === '') {
tweetClient.innerText = 'Twitter Web App';
} else {
tweetClient.innerText = clientValue;
}
}
// Render Retweets in Tweet
function renderRetweets() {
tweetRetweets.parentElement.classList.remove('hide');
let retweetsValue = retweets.value;
if (retweetsValue === '') {
tweetRetweets.innerText = '96';
} else {
retweetsValue = +retweetsValue;
if (retweetsValue >= 0) {
if (retweetsValue === 0) {
tweetRetweets.parentElement.classList.add('hide');
} else {
tweetRetweets.innerText = numberFormatter(retweetsValue);
}
} else {
tweetRetweets.innerText = '96';
}
}
}
// Render Quotes in Tweet
function renderQuotes() {
tweetQuotes.parentElement.classList.remove('hide');
let quotesValue = quotes.value;
if (quotesValue === '') {
tweetQuotes.innerText = '88';
} else {
quotesValue = +quotesValue;
if (quotesValue >= 0) {
if (quotesValue === 0) {
tweetQuotes.parentElement.classList.add('hide');
} else {
tweetQuotes.innerText = numberFormatter(quotesValue);
}
} else {
tweetQuotes.innerText = '88';
}
}
}
// Render Likes in Tweet
function renderLikes() {
tweetLikes.parentElement.classList.remove('hide');
let likesValue = likes.value;
if (likesValue === '') {
tweetLikes.innerText = '153';
} else {
likesValue = +likesValue;
if (likesValue >= 0) {
if (likesValue === 0) {
tweetLikes.parentElement.classList.add('hide');
} else {
tweetLikes.innerText = numberFormatter(likesValue);
}
} else {
tweetLikes.innerText = '153';
}
}
}
// Returns current Time
function getCurrentTime() {
const dateObj = new Date();
let hours = +dateObj.getHours();
let minutes = ('00' + dateObj.getMinutes()).slice(-2);
let suffix;
if (hours > 12) {
hours = hours - 12;
suffix = 'PM';
} else {
if (hours === 0) {
hours = 12;
suffix = 'AM';
} else if (hours === 12) {
suffix = 'PM';
} else {
suffix = 'AM';
}
}
return `${hours}:${minutes} ${suffix}`;
}
// Returns current Date
function getCurrentDate() {
const dateObj = new Date();
const day = dateObj.getDate();
const month = dateObj.getMonth();
const year = dateObj.getFullYear();
return `${MONTHS[month]} ${day}, ${year}`;
}
// Set Theme
function toggleTheme(ev) {
let choice;
for (let i = 0; i < themeRadios.length; i++) {
if (themeRadios[i].checked) {
choice = themeRadios[i].value;
}
}
if (choice === 'dim') {
tweet.className = 'tweet dim';
tweetBox.className = 'tweet_box dim';
themeColor = '#15202b';
} else if (choice === 'dark') {
tweet.className = 'tweet dark';
tweetBox.className = 'tweet_box dark';
themeColor = '#000000';
} else {
tweet.className = 'tweet';
tweetBox.className = 'tweet_box';
themeColor = '#ffffff';
}
}
// Toggle Verified Badge
function toggleVerified() {
let choice;
for (let i = 0; i < verifiedRadios.length; i++) {
if (verifiedRadios[i].checked) {
choice = verifiedRadios[i].value;
}
}
if (choice === 'show') {
tweetVerified.classList.remove('hide');
} else {
tweetVerified.classList.add('hide');
}
}
// Generate filenames for the image which is to be downloaded
function generateFileName() {
return `tweet${Math.floor(Math.random() * 90000) + 10000}`;
}
// Download it to the local machine
function saveAs(uri, filename) {
const link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
//Firefox requires the link to be in the body
document.body.appendChild(link);
//simulate click
link.click();
//remove the link when done
document.body.removeChild(link);
} else {
window.open(uri);
}
}
// Take screenshot of the tweet
function takeScreenshot() {
window.scrollTo(0, 0);
html2canvas(document.querySelector('.tweet'), {
allowTaint: true,
backgroundColor: themeColor,
useCORS: true,
scrollX: -window.scrollX,
scrollY: -window.scrollY,
windowWidth: document.documentElement.offsetWidth,
windowHeight: document.documentElement.offsetHeight,
}).then((canvas) => {
saveAs(canvas.toDataURL(), generateFileName());
});
}
// Set Timestamp when page is loaded
function setTimestamp() {
renderTime();
renderDate();
}
// On load
setTimestamp();
// Event Listeners
avatar.addEventListener('change', renderProfilePicture);
reset.addEventListener('click', resetProfilePicture);
fullname.addEventListener('input', renderName);
username.addEventListener('input', renderUsername);
message.addEventListener('input', renderMessage);
time.addEventListener('input', renderTime);
date.addEventListener('input', renderDate);
client.addEventListener('input', renderClient);
retweets.addEventListener('input', renderRetweets);
quotes.addEventListener('input', renderQuotes);
likes.addEventListener('input', renderLikes);
download.addEventListener('click', takeScreenshot);
for (let i = 0; i < themeRadios.length; i++) {
themeRadios[i].addEventListener('change', toggleTheme);
}
for (let i = 0; i < verifiedRadios.length; i++) {
verifiedRadios[i].addEventListener('change', toggleVerified);
}