-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
174 lines (147 loc) · 4.96 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//values
const mtxt={
'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E',
'..-.': 'F', '--.': 'G', '....': 'H', '..': 'I', '.---': 'J',
'-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O',
'.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T',
'..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y',
'--..': 'Z',
'-----': '0', '.----': '1', '..---': '2', '...--': '3', '....-': '4',
'.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9',
'.-.-.-': '.', '--..--': ',', '..--..': '?', '-.-.--': '!', ' ': ' ',
'-..-.': '/', '.-...': '&', '---...': ':', '-.-.-.': ';', '-...-': '=',
'.-.-.': '+', '-....-': '-', '..--.-': '_', '.-..-.': '"', '...-..-': '$',
'.--.-.': '@', '-.--.': '(', '-.--.-': ')', '.----.': '\'','':''
};
const txtm = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..',
// Digits
'0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
// Punctuation
'.': '.-.-.-', ',': '--..--', '?': '..--..', '!': '-.-.--', ' ': ' ',
'/': '-..-.', '&': '.-...', ':': '---...', ';': '-.-.-.', '=': '-...-',
'+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.', '$': '...-..-',
'@': '.--.-.', '(': '-.--.', ')': '-.--.-', '\'': '.----.',
// Spanish
'Á': '.--.-', 'É': '..-..', 'Ó': '---.', 'Ú': '..--', 'Ñ': '--.--',
// German
'Ä': '.-.-', 'Ö': '---.', 'Ü': '..--', 'ẞ': '...--..',
// French
'À': '.--.-', 'È': '.-..-', 'Ù': '..--'
};
//elements
const tm = document.getElementById("tm");
const mt = document.getElementById("mt");
var header=document.getElementById("header");
const submit=document.getElementById("sub");
const input=document.getElementById("inputbox");
const output=document.getElementById("outputbox");
const copybutton = document.getElementById('copy1');
//texts
var text=["Standard Morse Code Translator ",".-- . .-.. -.-. --- -- . "];
var displaytext="";
//global variables for typing animation
var i=0;
var j=0;
var textele=0;
//event to select only one checkbox at a time
tm.addEventListener("change",function(){
if(this.checked){
mt.checked=false;
}
});
mt.addEventListener("change",function(){
if(this.checked){
tm.checked=false;
}
});
//limit resizing
document.addEventListener("DOMContentLoaded", function () {
if (output.style.width ==='350px') {
output.style.resize = "none";
}
});
//typing animation function
function type(){
if(i<text[textele].length && j==0){
displaytext+=(text[textele])[i];
header.innerText=displaytext;
i++;
}
else{
j=1;
}
if(i>0 && j==1){
i--;
displaytext=displaytext.slice(0, -1);
header.innerText=displaytext;
}
else if(i<=0 && j==1){
j=0;
textele++;
if (textele>text.length-1){
textele=0;
}
}
};
const interval= setInterval(type,80); //interval
function out(){
copybutton.innerHTML='<u>copy</u>';
let text1=(input.value).trim();
if (tm.checked){
let outputtext='';
let text2=text1.split(" ");
for (const ele of text2) {
for(const txt of ele){
if(txtm[txt.toUpperCase()]==undefined){
outputtext+="# ";
}
else{
outputtext+=txtm[txt.toUpperCase()]+" ";
}
}
outputtext+='/ ';
}
outputtext = outputtext.replace(/\/\s$/, '');
output.innerHTML=`${outputtext.trim("/")}`;
}
else if(mt.checked){
let outputtext='';
let text2=text1.split("/");
for (const ele of text2) {
const text3=(ele.trim(" ")).split(" ");
for(const txt of text3){
if(mtxt[txt]==undefined){
outputtext+="# ";
}
else{
outputtext+=mtxt[txt];
}
}
outputtext+=" ";
}
output.innerHTML=`${outputtext.trim(" ")}`;
}
};
input.addEventListener("keyup",out)
function copyFromTextarea() {
output.select();
if (navigator.clipboard) {
navigator.clipboard.writeText(output.value)
.then(() => {
copybutton.innerHTML='<u>copied</u>'
})
.catch((error) => {
alert('Copying text to clipboard failed. Please copy manually.');
});
} else {
alert('Copying text to clipboard is not supported in this browser. Please copy manually.');
}
}
copybutton.addEventListener('click',copyFromTextarea);