-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (55 loc) · 2.05 KB
/
main.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
var SupportedFonts;
(function (SupportedFonts) {
SupportedFonts["Arial"] = "arial";
SupportedFonts["Roboto"] = "roboto";
SupportedFonts["OpenSans"] = "open-sans";
SupportedFonts["Lexend"] = "lexend";
})(SupportedFonts || (SupportedFonts = {}));
const inputField = document.querySelector("#input-field");
const outputField = document.querySelector("#output-container");
const fontSelector = document.querySelector('#font-selector');
const charsSelector = document.querySelector('#chars-selector');
const charsLabel = document.querySelector('#chars-label');
const documentBody = document.querySelector('body');
let numOfBoldChars = 3;
const onInit = () => {
// Reset font to default and keep selection in sync on page refresh
documentBody.className = 'font-arial';
fontSelector.selectedIndex = 0;
charsSelector.value = numOfBoldChars.toString();
charsLabel.innerText = numOfBoldChars.toString();
convertText(); // if text is remembered from previously by the browser
};
const convertText = () => {
const textToConvert = inputField.value;
let matches = textToConvert.match(/[a-zA-Z0-9!?@\-_'\+=;:"]+\n?|\n/g);
let words = matches.map((word) => {
const bold = word.slice(0, numOfBoldChars);
const regular = word.slice(numOfBoldChars);
return `<strong>${bold}</strong>${regular}`;
});
let outputString = "";
words.forEach(w => {
outputString += w;
if (!w.includes('\n'))
outputString += ' ';
});
outputField.innerHTML = outputString;
};
const selectFont = (selectElement) => {
const fontName = selectElement.value;
const isSupported = Object.values(SupportedFonts).includes(fontName);
if (isSupported) {
documentBody.className = `font-${fontName}`;
return;
}
console.error(`${fontName} is not a supported font`);
};
const updateLabel = () => {
numOfBoldChars = charsSelector.valueAsNumber;
charsLabel.innerText = numOfBoldChars.toString();
convertText();
};
document.addEventListener("DOMContentLoaded", () => {
onInit();
});