CI / Trivy (dependências, segredos e configuração) (push) Successful in 1m2s
CI / Testes e imagem Docker (linux/arm64) (push) Successful in 4m24s
Release / Plano (reaproveitar a imagem?) (push) Successful in 5s
Release / ci (push) Skipped
Release / Publica a imagem (linux/arm64) (push) Successful in 18s
Release / Cria a release no Gitea (push) Successful in 4s
Interface reescrita (design próprio, tema claro/escuro, a11y WCAG 2.2 AA), seções do site e artigo IDEIA @ CSBC 2025. Tradutor portado para JS com paridade 100% contra o Python; sem Tailwind nem axios. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
286 lines
11 KiB
JavaScript
286 lines
11 KiB
JavaScript
import { state } from './state.js';
|
||
import { getTranslation } from './i18n.js';
|
||
import { triggerRealtimePreview } from './stl.js';
|
||
import { showToast, announce } from './ui.js';
|
||
import { encode as translateEncode, decode as translateDecode } from './translator.js';
|
||
|
||
let dom = {};
|
||
|
||
export function initConverterDOM(elements) {
|
||
dom = elements;
|
||
}
|
||
|
||
export function getAlphabet() {
|
||
const checkedInput = document.querySelector('input[name="alphabet"]:checked');
|
||
return checkedInput ? checkedInput.value : "North American";
|
||
}
|
||
|
||
function setAriaDisabled(el, disabled) {
|
||
if (!el) return;
|
||
el.setAttribute("aria-disabled", disabled ? "true" : "false");
|
||
}
|
||
|
||
// Keeps aria-disabled of copy/save/download in sync with the output and STL state.
|
||
export function updateActionStates() {
|
||
const hasOutput = !!(dom.output_textbox && dom.output_textbox.value.length > 0);
|
||
setAriaDisabled(dom.copy_button, !hasOutput);
|
||
setAriaDisabled(dom.save_button, !hasOutput);
|
||
setAriaDisabled(dom.download_stl_button, !state.last_stl_blob);
|
||
}
|
||
|
||
export function switchConversionMode(mode) {
|
||
state.conversion_mode = mode ? mode : state.conversion_mode;
|
||
|
||
const text_to_braille_tab = dom.text_to_braille_tab;
|
||
const braille_to_text_tab = dom.braille_to_text_tab;
|
||
const input_label = dom.input_label;
|
||
const input_textbox = dom.input_textbox;
|
||
const output_label = dom.output_label;
|
||
const output_textbox = dom.output_textbox;
|
||
const warning = dom.warning;
|
||
|
||
if (!text_to_braille_tab || !braille_to_text_tab || !input_label || !input_textbox || !output_label || !output_textbox) {
|
||
return;
|
||
}
|
||
|
||
const toBraille = state.conversion_mode === "text-to-braille";
|
||
const active = toBraille ? text_to_braille_tab : braille_to_text_tab;
|
||
const inactive = toBraille ? braille_to_text_tab : text_to_braille_tab;
|
||
active.classList.add("tab-active");
|
||
inactive.classList.remove("tab-active");
|
||
active.setAttribute("aria-selected", "true");
|
||
inactive.setAttribute("aria-selected", "false");
|
||
active.setAttribute("tabindex", "0");
|
||
inactive.setAttribute("tabindex", "-1");
|
||
const workbench = document.getElementById("workbench");
|
||
if (workbench) workbench.setAttribute("aria-labelledby", active.id);
|
||
|
||
const brailleGlyph = '<span class="label-icon" aria-hidden="true">⠿</span> ';
|
||
if (toBraille) {
|
||
input_label.innerHTML = `<i class="fas fa-pen-alt label-icon" aria-hidden="true"></i> `;
|
||
input_label.append(getTranslation("input_label_text_to_braille"));
|
||
input_textbox.placeholder = getTranslation("input_placeholder_text_to_braille");
|
||
output_label.innerHTML = brailleGlyph;
|
||
output_label.append(getTranslation("output_label_text_to_braille"));
|
||
output_textbox.placeholder = getTranslation("output_placeholder_text_to_braille");
|
||
} else {
|
||
input_label.innerHTML = brailleGlyph;
|
||
input_label.append(getTranslation("input_label_braille_to_text"));
|
||
input_textbox.placeholder = getTranslation("input_placeholder_braille_to_text");
|
||
output_label.innerHTML = `<i class="fas fa-text-width label-icon" aria-hidden="true"></i> `;
|
||
output_label.append(getTranslation("output_label_braille_to_text"));
|
||
output_textbox.placeholder = getTranslation("output_placeholder_braille_to_text");
|
||
}
|
||
// The field that holds Braille uses the Braille font/size.
|
||
input_textbox.classList.toggle("braille-text", !toBraille);
|
||
output_textbox.classList.toggle("braille-text", toBraille);
|
||
|
||
if (warning) {
|
||
if (!toBraille && getAlphabet() === "Brazilian") {
|
||
warning.classList.remove("hidden");
|
||
} else {
|
||
warning.classList.add("hidden");
|
||
}
|
||
}
|
||
|
||
const chineseTip = document.getElementById("chinese_input_tip_container");
|
||
if (chineseTip) {
|
||
chineseTip.classList.toggle("hidden", getAlphabet() !== "Chinese");
|
||
}
|
||
|
||
if (mode) {
|
||
clearInput();
|
||
}
|
||
updateCharCount();
|
||
updateActionStates();
|
||
}
|
||
|
||
export function clearInput() {
|
||
if (dom.input_textbox) dom.input_textbox.value = "";
|
||
if (dom.output_textbox) dom.output_textbox.value = "";
|
||
updateCharCount();
|
||
updateActionStates();
|
||
}
|
||
|
||
export function updateCharCount() {
|
||
if (!dom.char_count || !dom.input_textbox) return;
|
||
const count = dom.input_textbox.value.length;
|
||
const tpl = getTranslation(count === 1 ? "char_count_one" : "char_count_other") || "{n}";
|
||
dom.char_count.textContent = tpl.replace("{n}", count);
|
||
}
|
||
|
||
export function copyOutput() {
|
||
const output_textbox = dom.output_textbox;
|
||
const copy_button = dom.copy_button;
|
||
if (!output_textbox || !copy_button) return;
|
||
|
||
const output = output_textbox.value;
|
||
if (output.length === 0) {
|
||
showToast(getTranslation("toast_nothing_to_copy"), "info");
|
||
return;
|
||
}
|
||
|
||
const labelEl = document.getElementById("copy_button_text") || copy_button;
|
||
const iconEl = copy_button.querySelector("i");
|
||
const showCopied = () => {
|
||
const copyText = getTranslation("copy_button_feedback") || "Copied!";
|
||
const originalIcon = iconEl ? iconEl.className : "";
|
||
const originalText = getTranslation("copy_button") || labelEl.textContent;
|
||
labelEl.textContent = copyText;
|
||
if (iconEl) iconEl.className = "fas fa-check";
|
||
announce(copyText);
|
||
setTimeout(() => {
|
||
labelEl.textContent = getTranslation("copy_button") || originalText;
|
||
if (iconEl) iconEl.className = originalIcon;
|
||
}, 2000);
|
||
};
|
||
const fallback = () => {
|
||
try {
|
||
output_textbox.select();
|
||
if (document.execCommand("copy")) { showCopied(); return; }
|
||
} catch (e) { console.error(e); }
|
||
showToast(getTranslation("console_copy_error") || "Error", "error");
|
||
};
|
||
|
||
if (!navigator.clipboard) {
|
||
fallback();
|
||
} else {
|
||
navigator.clipboard.writeText(output).then(showCopied).catch(err => {
|
||
console.error(getTranslation("console_copy_error") || "Error", err);
|
||
fallback();
|
||
});
|
||
}
|
||
}
|
||
|
||
export function saveOutput() {
|
||
if (!dom.output_textbox) return;
|
||
const text = dom.output_textbox.value;
|
||
if (text.length === 0) {
|
||
showToast(getTranslation("toast_nothing_to_save"), "info");
|
||
return;
|
||
}
|
||
|
||
const blob = new Blob([text], { type: 'text/plain' });
|
||
const url = URL.createObjectURL(blob);
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.download = `inbraille_output.txt`;
|
||
document.body.appendChild(a);
|
||
a.click();
|
||
document.body.removeChild(a);
|
||
URL.revokeObjectURL(url);
|
||
showToast(getTranslation("toast_saved"), "success");
|
||
}
|
||
|
||
function adjustBrailleSize(braille) {
|
||
const initial_string = String(braille).replaceAll("\n", "⠀");
|
||
let adjusted_braille = "";
|
||
let actual_phrase = "";
|
||
|
||
const words = initial_string.split("⠀");
|
||
const number_of_words = words.length;
|
||
for (let index = 0; index < number_of_words; index++) {
|
||
const word = words[index];
|
||
const word_length = word.length;
|
||
if (word_length > state.symbols_per_line) { return word_length; }
|
||
|
||
if ((word_length + actual_phrase.length) > state.symbols_per_line) {
|
||
adjusted_braille += actual_phrase.slice(0, -1) + "\n";
|
||
actual_phrase = "";
|
||
}
|
||
|
||
actual_phrase += word + "⠀";
|
||
}
|
||
|
||
if (actual_phrase.length > 0) {
|
||
adjusted_braille += actual_phrase.slice(0, -1);
|
||
} else {
|
||
adjusted_braille = adjusted_braille.slice(0, -1);
|
||
}
|
||
|
||
return adjusted_braille;
|
||
}
|
||
|
||
async function checkBrailleSize(braille) {
|
||
const phrases = braille.split("\n");
|
||
const number_of_phrases = phrases.length;
|
||
for (let index = 0; index < number_of_phrases; index++) {
|
||
const phrase = phrases[index];
|
||
const phrase_length = phrase.length;
|
||
if (phrase_length > state.symbols_per_line) {
|
||
const adjust = confirm(getTranslation("confirm_line_exceeds"));
|
||
|
||
if (adjust) {
|
||
const adjusted = adjustBrailleSize(braille);
|
||
if (isNaN(adjusted)) {
|
||
if (state.conversion_mode === "text-to-braille") {
|
||
const response = await translateDecode({
|
||
braille: adjusted,
|
||
alphabet: getAlphabet()
|
||
});
|
||
if (dom.input_textbox) {
|
||
dom.input_textbox.value = response.decoded;
|
||
}
|
||
}
|
||
alert(getTranslation("alert_auto_adjust_success"));
|
||
} else {
|
||
alert(`${getTranslation("alert_word_exceeds_prefix")} ${adjusted}`);
|
||
return -1;
|
||
}
|
||
return adjusted;
|
||
}
|
||
return phrase_length;
|
||
}
|
||
}
|
||
return phrases.join("\n");
|
||
}
|
||
|
||
export async function convert() {
|
||
if (!dom.input_textbox || !dom.output_textbox) return;
|
||
const text = dom.input_textbox.value;
|
||
if (text.length === 0) { return; }
|
||
|
||
const alphabet = getAlphabet();
|
||
try {
|
||
if (state.conversion_mode === "text-to-braille") {
|
||
const response = await translateEncode({
|
||
text: text,
|
||
alphabet: alphabet
|
||
});
|
||
const braille = await checkBrailleSize(response.encoded);
|
||
if (braille === -1) {
|
||
// Alert already handled inside checkBrailleSize
|
||
} else if (!isNaN(braille)) {
|
||
dom.output_textbox.value = "";
|
||
alert(`${getTranslation("alert_manual_correct_prefix")} ${braille}`);
|
||
} else {
|
||
dom.output_textbox.value = braille;
|
||
const cells = (String(braille).match(/[\u2800-\u28FF]/g) || []).length;
|
||
announce((getTranslation("status_converted") || "").replace("{n}", cells));
|
||
updateActionStates();
|
||
triggerRealtimePreview(60);
|
||
}
|
||
} else {
|
||
const adjusted_braille = await checkBrailleSize(text);
|
||
if (adjusted_braille === -1) {
|
||
// Already handled
|
||
} else if (!isNaN(adjusted_braille)) {
|
||
dom.output_textbox.value = "";
|
||
alert(`${getTranslation("alert_manual_correct_prefix")} ${adjusted_braille}`);
|
||
} else {
|
||
const response = await translateDecode({
|
||
braille: text,
|
||
alphabet: alphabet
|
||
});
|
||
dom.output_textbox.value = response.decoded;
|
||
announce(getTranslation("status_text_converted"));
|
||
updateActionStates();
|
||
triggerRealtimePreview(60);
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error(error);
|
||
showToast(getTranslation("alert_general_error"), "error");
|
||
}
|
||
}
|