-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
62 lines (54 loc) · 1.96 KB
/
main.ts
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
enum SupportedFonts {
Arial = 'arial',
Roboto = 'roboto',
OpenSans = 'open-sans',
Lexend = 'lexend'
}
const inputField: HTMLTextAreaElement = document.querySelector("#input-field");
const outputField: HTMLDivElement = document.querySelector("#output-container");
const fontSelector: HTMLSelectElement = document.querySelector('#font-selector')
const charsSelector: HTMLInputElement = document.querySelector('#chars-selector')
const charsLabel: HTMLSpanElement = document.querySelector('#chars-label')
const documentBody: HTMLBodyElement = 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: string = 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: HTMLSelectElement) => {
const fontName: string = selectElement.value
const isSupported = Object.values(SupportedFonts).includes(fontName as SupportedFonts)
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()
})