-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
150 lines (130 loc) · 5.58 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
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
function showNotificationModal(message) {
document.getElementById("notificationMessage").innerText = message;
const modal = document.getElementById("notificationModal");
modal.style.display = "block";
}
function closeNotificationModal() {
const modal = document.getElementById("notificationModal");
modal.style.display = "none";
}
window.onclick = function(event) {
const modal = document.getElementById("notificationModal");
if (event.target == modal) {
closeNotificationModal();
}
}
async function fetchFontNames() {
try {
const response = await fetch('https://api.github.com/repos/mskian/ascii-art/contents/fonts');
if (!response.ok) {
throw new Error('Failed to fetch fonts');
}
const fonts = await response.json();
return fonts.map(font => font.name.replace('.flf', ''));
} catch (error) {
console.error('Error fetching fonts:', error.message);
return [];
}
}
async function populateFontDropdown() {
const fontSelect = document.getElementById('fontSelect');
const fontList = await fetchFontNames();
fontList.forEach(font => {
const option = document.createElement('option');
option.textContent = font;
option.value = font;
fontSelect.appendChild(option);
});
}
function generateASCII() {
const inputText = document.getElementById("textInput").value.trim();
const inputLength = inputText.length;
if (inputLength < 2) {
showNotificationModal('Text must be at least 2 characters long.');
return;
} else if (inputLength > 40) {
showNotificationModal('Text must not exceed 40 characters.');
return;
}
const selectedFont = document.getElementById("fontSelect").value;
const artType = document.getElementById("artType").value;
const asciiOutput = document.getElementById("asciiOutput");
asciiOutput.innerText = '';
asciiOutput.style.display = "block";
if (artType === 'figlet') {
figlet(inputText, { font: selectedFont }, function(err, figletData) {
if (err) {
console.error('Error generating Figlet ASCII art:', err);
asciiOutput.innerText = 'Error: Failed to generate ASCII art';
return;
}
if (figletData.trim()) {
asciiOutput.innerHTML = `<section class="section"><div class="container content"><div class="columns is-centered"><div class="column is-full"><section id="terminal__bar">
<div class="fakeButtons fakeClose"></div>
<div class="fakeButtons fakeMinimize"></div>
<div class="fakeButtons fakeZoom"></div>
</section><pre>${figletData}</pre>`;
}
const buttonContainer = document.createElement('div');
buttonContainer.className = 'has-text-centered';
asciiOutput.appendChild(buttonContainer);
const clearButton = document.createElement('button');
clearButton.innerHTML = '<span class="button-text">Clear</span>';
clearButton.className = 'button is-danger mr-2';
clearButton.onclick = clearText;
buttonContainer.appendChild(clearButton);
const copyButton = document.createElement('button');
copyButton.innerHTML = '<span class="button-text">Copy</span>';
copyButton.className = 'button is-info';
copyButton.onclick = copyToClipboard;
buttonContainer.appendChild(copyButton);
});
} else if (artType === 'cowsay') {
const cowsayData = `
__________________
< ${inputText} >
------------------
\\ ^__^
\\ (oo)\\_______
(__)\\ )\\/\\
||----w |
|| ||
`;
asciiOutput.innerHTML += `<section class="section"><div class="container content"><div class="columns is-centered"><div class="column is-full"><section id="terminal__bar">
<div class="fakeButtons fakeClose"></div>
<div class="fakeButtons fakeMinimize"></div>
<div class="fakeButtons fakeZoom"></div>
</section><pre>${cowsayData}</pre>`;
const buttonContainer = document.createElement('div');
buttonContainer.className = 'has-text-centered';
asciiOutput.appendChild(buttonContainer);
const clearButton = document.createElement('button');
clearButton.innerHTML = '<span class="button-text">Clear</span>';
clearButton.className = 'button is-danger mr-2';
clearButton.onclick = clearText;
buttonContainer.appendChild(clearButton);
const copyButton = document.createElement('button');
copyButton.innerHTML = '<span class="button-text">Copy</span>';
copyButton.className = 'button is-info';
copyButton.onclick = copyToClipboard;
buttonContainer.appendChild(copyButton);
}
}
function clearText() {
document.getElementById("textInput").value = "";
document.getElementById("asciiOutput").innerText = "";
document.getElementById("fontSelect").value = "Standard";
document.getElementById("asciiOutput").style.display = "none";
}
populateFontDropdown();
async function copyToClipboard() {
const asciiOutput = document.getElementById("asciiOutput");
const asciiText = asciiOutput.innerText.replace(/Clear|Copy/g, '');
try {
await navigator.clipboard.writeText(asciiText);
showNotificationModal('ASCII art copied to clipboard!');
} catch (error) {
console.error('Error copying to clipboard:', error);
showNotificationModal('Failed to copy ASCII art to clipboard');
}
}