-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
164 lines (142 loc) · 4.69 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
function $(selector) {
return document.querySelector(selector);
}
function getHSLArray(str) {
const noLineBreaks = str.replace(/(\r\n|\n|\r)/gm, ''); // removes all 3 types of newline characters
const hslArray = noLineBreaks.match(/hsl\(\d+,( | )\d+%, \d+%\)/g); // hsl values from string to array
const noSpaceHSLArray = [];
try {
for (let el of hslArray) {
noSpaceHSLArray.push(el.replaceAll(' ', '')); // 'deletes' space to use in URL
}
$('.error-msg').classList.add('invisible');
} catch (err) {
$('.error-msg').classList.remove('invisible');
$(
'.error-msg'
).innerHTML = `Required format: <span>Color name: hsl(x, x%, x%)</span>`;
}
return noSpaceHSLArray;
}
async function fetchAColor(color) {
const url = `https://www.thecolorapi.com/id?hsl=${color}`;
try {
const response = await fetch(url);
const data = await response.json();
$('.error-msg').classList.add('invisible');
return data;
} catch (err) {
console.log(err);
displayOfflineErrorMsg();
}
}
function setObjectColors(object, key, value) {
const newKey = key.replace(/\s+/g, ''); // deletes any number of white space from fetched color name
// modify output prefix based on scss.status being 1 or 0
if ($('.scss-con').dataset.status === '1') {
object['$' + newKey] = value + ';';
}
if ($('.scss-con').dataset.status === '0') {
object['--clr-' + newKey] = value + ';';
}
}
async function getColors() {
const inputFieldValue = $('#unformatted-string').value;
const hslArray = getHSLArray(inputFieldValue);
const colorObject = {};
const fetches = [];
for (let i = 0; i < hslArray.length; i++) {
const fetch = fetchAColor(hslArray[i]).then(
data => setObjectColors(colorObject, data?.name.value, hslArray[i]),
e => {
console.log("Something's strange in the neighborhood: " + e);
}
);
fetches.push(fetch);
}
await Promise.all(fetches);
return colorObject;
}
function renderColors(colorObject) {
const stringifiedObj = JSON.stringify(colorObject);
const deobjectifiedObj = stringifiedObj
.replaceAll('"', '')
.replaceAll('{', '')
.replaceAll('}', '');
const regex = /(,(?=\$))|((?<=;),)/g;
const result = deobjectifiedObj.replaceAll(regex, ''); // delete every comma besides the ones within the paranthesis
$('#unformatted-string').value = result; // insert formatted result
navigator.clipboard.writeText(result); // copy to clipboard
displayMsgOnSuccess(); // display copied to your clipboard
}
async function startConversion() {
const inputFieldValue = $('#unformatted-string');
const obj = await getColors(inputFieldValue);
if (obj !== undefined) {
renderColors(obj);
}
}
function removeOfflineErrorMsg() {
$('.error-msg').classList.add('invisible');
}
function displayOfflineErrorMsg() {
$(
'.error-msg'
).innerHTML = `No internet connection.<span>Please try again later.</span>`;
$('.error-msg').classList.remove('invisible');
}
function displayAlreadyFormattedErrorMsg() {
$('.error-msg').innerHTML = `<span>Formatting is already done.</span>`;
$('.error-msg').classList.remove('invisible');
}
function removeAlreadyFormattedErrorMsg() {
$('.error-msg').classList.remove('invisible');
}
function displayMsgOnSuccess() {
$('.success-msg-con').id = '';
$('.success-msg-con').setAttribute('closing', '');
$('.success-msg-con').setAttribute('open', '');
$('.success-msg-con').addEventListener('animationend', () => {
$('.success-msg-con').id = 'invisible';
$('.success-msg-con').removeAttribute('closing');
});
}
window.addEventListener('DOMContentLoaded', () => {
// listening click event on SCSS radio
$('.scss-con').addEventListener('click', () => {
$('.scss-con').dataset.status = 1;
});
// listening click event on CSS radio
$('.css-con').addEventListener('click', () => {
$('.scss-con').dataset.status = 0;
});
// convert string on click
$('button').addEventListener('click', () => {
const includesSCSS = $('#unformatted-string').value.includes('$');
const includesCSS = $('#unformatted-string').value.includes('--clr-');
if ($('.scss-con').dataset.status === '1' && !includesSCSS) {
startConversion();
}
if ($('.scss-con').dataset.status === '1' && includesSCSS) {
displayAlreadyFormattedErrorMsg();
}
if ($('.scss-con').dataset.status === '0' && !includesCSS) {
startConversion();
}
if ($('.scss-con').dataset.status === '0' && includesCSS) {
displayAlreadyFormattedErrorMsg();
}
});
// log promise rejection
window.addEventListener('unhandledrejection', promiseRejectionEvent => {
console.log(promiseRejectionEvent);
});
// displays error msg when offline
window.addEventListener('offline', () => {
displayOfflineErrorMsg();
});
// removes error msg when online
window.addEventListener('online', () => {
removeOfflineErrorMsg();
});
});