-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (63 loc) · 2.2 KB
/
script.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
/* Set Variables */
let button = document.querySelector('.emojifize__action.button');
let form = document.querySelector('.emojifize__form');
let textInput = document.querySelector('.emojifize__area');
let emojiInput = document.querySelector('.emojifize__emoji');
let result = document.querySelector('.emojifize__output');
let resultOutput = document.querySelector('.emojifize__result');
let clear = document.querySelector('.emojifize__action.text');
/* Action that triggers everything */
button.onclick = function(event) {
// Pwease don't submit
event.preventDefault();
// Set variables
let textContent;
let emojiContent;
let spaceRegex = /\s/g;
let errorMessage = 'Please enter some text 👍'
// Default emoji is clapping unless other emoji are present
if ( emojiInput.value == ''){
emojiContent = '👏';
} else {
emojiContent = emojiInput.value;
}
// Text must be present
if ( textInput.value == ''){
textContent = textInput.value = errorMessage;
textInput.classList.add('error');
return false;
} else if ( textInput.value == errorMessage ) {
return false;
} else {
textContent = textInput.value;
}
// Change to ALL CAPS + replace spaces with emoji
let emojifizedText = textContent
.toUpperCase()
.replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, '')
.replace(spaceRegex, ' ' + emojiContent + ' ');
// Bookend with emoji
emojifizedText = emojiContent + ' ' + emojifizedText + ' ' + emojiContent;
resultOutput.value = emojifizedText;
resultOutput.focus();
result.style.display = 'block';
}
/* Clear Error in text area when interacted with */
textInput.onclick = function(){
if ( this.classList.contains('error') ) {
this.classList.remove('error');
this.value = '';
}
}
/* Select all text when clicking into results */
resultOutput.onclick = function(event){
this.focus();
this.select();
}
/* Action that clears form */
clear.onclick = function(event) {
// Pwease don't click
event.preventDefault();
// Clear That Form!
form.reset();
}