Files
inbraille/public/modules/stl.js
T
Lucas RayanandClaude Opus 5.5 07aaf8e1a7
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
feat: novo site e interface; tradução Braille no navegador (1.4.0)
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>
2026-11-15 19:51:53 -03:00

243 lines
9.6 KiB
JavaScript

import { state, updatePlateWidth } from './state.js';
import { getTranslation } from './i18n.js';
import { showToast, highlightInput, announce } from './ui.js';
import { convert, updateActionStates } from './converter.js';
import { buildClientSTL } from './client_stl_generator.js';
let dom = {};
export function initStlDOM(elements) {
dom = elements;
}
let realtimeDebounceTimer = null;
let firstStlDone = false;
export function triggerRealtimePreview(delay = 80) {
if (!dom.stl_preview || dom.stl_preview.classList.contains("hidden")) {
return;
}
clearTimeout(realtimeDebounceTimer);
realtimeDebounceTimer = setTimeout(() => {
updateRealtimeModel();
}, delay);
}
export function updateRealtimeModel() {
if (!dom.stl_preview || dom.stl_preview.classList.contains("hidden")) {
return;
}
let braille = dom.output_textbox ? dom.output_textbox.value : "";
if (state.conversion_mode === "braille-to-text" && dom.input_textbox) {
braille = dom.input_textbox.value;
}
if (!braille || braille.trim().length === 0) {
return;
}
try {
const arrayBuffer = buildClientSTL(braille, {
resolution: state.resolution,
plate_thickness: state.plate_thickness,
unique_plate: state.unique_plate,
symbols_per_line: state.symbols_per_line,
text_alignment: state.text_alignment,
rounded: state.rounded,
points_only: state.points_only
});
state.last_stl_blob = new Blob([arrayBuffer], { type: "model/stl" });
updateActionStates();
if (typeof window.renderSTLPreview === "function") {
window.renderSTLPreview(arrayBuffer.slice(0), false);
}
} catch (err) {
console.warn("[InBraille] Real-time preview update skipped:", err);
}
}
export function updateParameters() {
if (!dom.input_resolution || !dom.input_plate_thickness || !dom.input_separate_plates || !dom.input_symbols_per_line || !dom.select_text_alignment || !dom.input_rounded || !dom.input_points_only) {
return;
}
state.resolution = parseInt(dom.input_resolution.value) || 20;
state.plate_thickness = parseInt(dom.input_plate_thickness.value) || 3;
state.unique_plate = !dom.input_separate_plates.checked;
state.symbols_per_line = parseInt(dom.input_symbols_per_line.value) || 22;
state.text_alignment = dom.select_text_alignment.value || "center";
state.rounded = dom.input_rounded.checked;
state.points_only = dom.input_points_only.checked;
updatePlateWidth();
triggerRealtimePreview(60);
}
export function applyPointsOnlyState() {
if (!dom.input_separate_plates || !dom.input_rounded || !dom.input_plate_thickness) return;
const disabled_inputs = [dom.input_separate_plates, dom.input_rounded, dom.input_plate_thickness];
disabled_inputs.forEach((input) => {
input.disabled = state.points_only;
const card = input.closest(".option-card, .bg-white");
if (card) {
card.classList.toggle("opacity-50", state.points_only);
card.classList.toggle("pointer-events-none", state.points_only);
if (state.points_only) card.setAttribute("aria-disabled", "true");
else card.removeAttribute("aria-disabled");
}
});
}
function setFieldError(inputEl, message) {
if (!inputEl) return;
const err = document.getElementById(`${inputEl.id}_error`);
if (message) {
inputEl.setAttribute("aria-invalid", "true");
if (err) { err.textContent = message; err.classList.remove("hidden"); }
} else {
inputEl.removeAttribute("aria-invalid");
if (err) { err.textContent = ""; err.classList.add("hidden"); }
}
}
export function updateInputs() {
if (!dom.input_resolution || !dom.input_plate_thickness || !dom.input_separate_plates || !dom.input_symbols_per_line || !dom.select_text_alignment || !dom.input_rounded || !dom.input_points_only || !dom.output_plate_width) {
return;
}
const clamp = (key, min, max, inputEl) => {
const original = parseInt(inputEl.value);
let outOfRange = !isNaN(original) && (original < min || original > max);
if (state[key] < min) { state[key] = min; outOfRange = true; }
if (state[key] > max) { state[key] = max; outOfRange = true; }
setFieldError(inputEl, outOfRange
? (getTranslation("field_range_error") || "{min}-{max}").replace("{min}", min).replace("{max}", max)
: "");
};
clamp("symbols_per_line", 8, 50, dom.input_symbols_per_line);
clamp("plate_thickness", 2, 100, dom.input_plate_thickness);
clamp("resolution", 15, 50, dom.input_resolution);
dom.input_resolution.value = state.resolution;
dom.input_plate_thickness.value = state.plate_thickness;
dom.input_separate_plates.checked = !state.unique_plate;
dom.input_symbols_per_line.value = state.symbols_per_line;
dom.select_text_alignment.value = state.text_alignment;
dom.input_rounded.checked = state.rounded;
dom.input_points_only.checked = state.points_only;
dom.output_plate_width.innerHTML = state.plate_width.toFixed(1);
}
export async function generateSTL() {
const input_textbox = dom.input_textbox;
const output_textbox = dom.output_textbox;
const stl_button = dom.stl_button;
if (!input_textbox || !output_textbox || !stl_button) return;
if (state.conversion_mode === "braille-to-text") {
if (input_textbox.value.trim().length === 0) {
showToast(getTranslation("toast_enter_braille"), "warning");
highlightInput(input_textbox);
return;
}
} else if (output_textbox.value.trim().length === 0) {
if (input_textbox.value.trim().length === 0) {
showToast(getTranslation("toast_enter_text"), "warning");
highlightInput(input_textbox);
return;
}
showToast(getTranslation("toast_converting"), "info");
await convert();
if (output_textbox.value.trim().length === 0) {
return;
}
}
let braille = output_textbox.value;
if (state.conversion_mode === "braille-to-text") {
braille = input_textbox.value;
}
if (braille.length === 0) { return; }
const stlText = document.getElementById("stl_button_text");
stl_button.disabled = true;
stl_button.setAttribute("aria-busy", "true");
if (stlText) stlText.textContent = getTranslation("generating_loader") || "Generating...";
if (dom.stl_preview) dom.stl_preview.classList.add("is-loading");
try {
const optionsPayload = {
braille: braille,
resolution: state.resolution,
plate_thickness: state.plate_thickness,
unique_plate: state.unique_plate,
symbols_per_line: state.symbols_per_line,
text_alignment: state.text_alignment,
rounded: state.rounded,
points_only: state.points_only
};
// Geracao 100% no navegador; qualquer erro cai no catch abaixo (toast).
const t0 = performance.now();
const arrayBuffer = buildClientSTL(braille, optionsPayload);
console.log(`[InBraille] Client STL generated in ${(performance.now() - t0).toFixed(1)}ms`);
state.last_stl_blob = new Blob([arrayBuffer], { type: "model/stl" });
const empty = document.getElementById("stl_empty");
if (empty) empty.classList.add("hidden");
if (dom.stl_preview) {
dom.stl_preview.classList.remove("hidden");
}
if (typeof window.renderSTLPreview === "function") {
window.renderSTLPreview(arrayBuffer.slice(0), true);
}
updateActionStates();
if (dom.stl_preview) {
const reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
dom.stl_preview.scrollIntoView({ behavior: reduce ? "auto" : "smooth", block: "nearest" });
}
announce(getTranslation("status_stl_ready"));
if (!firstStlDone) {
firstStlDone = true;
const title = document.getElementById("stl_preview_title");
if (title) title.focus({ preventScroll: true });
}
} catch (error) {
console.error(error);
showToast(getTranslation("alert_stl_error"), "error");
} finally {
stl_button.disabled = false;
stl_button.removeAttribute("aria-busy");
if (stlText) stlText.textContent = getTranslation("stl_button_default") || "Generate & Preview STL";
if (dom.stl_preview) dom.stl_preview.classList.remove("is-loading");
}
}
export function downloadSTL() {
if (!state.last_stl_blob) {
showToast(getTranslation("toast_generate_first"), "info");
return;
}
const url = window.URL.createObjectURL(state.last_stl_blob);
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'output.stl');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
}
export function toggleAdvancedOptions() {
if (dom.advanced_options && dom.advanced_options_arrow) {
const nowHidden = dom.advanced_options.classList.toggle("hidden");
dom.advanced_options_arrow.classList.toggle("rotate-180", !nowHidden);
if (dom.advanced_options_toggle) dom.advanced_options_toggle.setAttribute("aria-expanded", String(!nowHidden));
}
}
export function openAdvancedOptionsOnDesktop() {
if (!dom.advanced_options || !window.matchMedia || !window.matchMedia("(min-width: 1024px)").matches) return;
if (dom.advanced_options.classList.contains("hidden")) toggleAdvancedOptions();
}