LingoSynth/lingosynth/app/static/js/index.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-05-11 21:48:16 +02:00
function stopRephrasing() {
rephraseButton.classList.remove('loading');
textWrapper.classList.remove('loading');
rephraseIcon.className = 'ti ti-language';
2023-05-13 14:29:15 +02:00
copyButton.classList.remove('done');
copyIcon.className = 'ti ti-clipboard';
copyText.innerText = 'Copy';
2023-05-11 21:48:16 +02:00
}
function rephrase() {
if (textBox.classList.contains('loading')) {
return;
}
rephraseButton.classList.add('loading');
textWrapper.classList.add('loading');
rephraseIcon.className = 'ti ti-loader-2';
const data = {
text: textBox.value
};
fetch('/api/rephrase', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': navigator.userAgent
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
textBox.value = data.text;
stopRephrasing();
})
.catch((error) => {
stopRephrasing();
console.error('Error:', error);
2023-05-11 23:28:39 +02:00
popups.innerHTML = '';
2023-05-11 21:48:16 +02:00
const errorDiv = document.createElement('div');
errorDiv.className = 'error alert';
2023-05-13 14:29:15 +02:00
errorDiv.innerText = 'Sorry, an error has occurred. Please try again later.';
2023-05-11 21:48:16 +02:00
popups.appendChild(errorDiv);
2023-05-11 23:28:39 +02:00
const closeButton = document.createElement('i');
closeButton.className = 'ti ti-x';
closeButton.addEventListener('click', function() {
errorDiv.remove();
}
);
errorDiv.appendChild(closeButton);
2023-05-11 21:48:16 +02:00
});
};
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.keyCode === 13) {
rephrase();
}
});
2023-05-13 14:29:15 +02:00
function copy() {
textBox.select();
document.execCommand('copy');
textBox.setSelectionRange(0, 0);
textBox.blur();
copyButton.classList.add('done');
copyIcon.className = 'ti ti-clipboard-check';
copyText.innerText = 'Copied';
}
const easymde = new EasyMDE({
element: textBox,
});