-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
183 lines (156 loc) · 7.1 KB
/
Copy pathindex.html
File metadata and controls
183 lines (156 loc) · 7.1 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NaturalMath2LaTeX</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/LaTeX@0.16.11/dist/LaTeX.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/LaTeX@0.16.11/dist/LaTeX.min.js"></script>
</head>
<body class="h-screen overflow-hidden bg-slate-100 text-slate-900">
<main class="mx-auto flex h-full max-w-7xl flex-col p-4 sm:p-6 lg:p-8">
<header class="mb-6">
<a href="https://github.com/PEXEL2002/NaturalMath2LaTeX" target="_blank"><h1 class="text-3xl font-bold tracking-tight">NaturalMath2LaTeX</h1></a>
<p class="mt-2 text-sm text-slate-600">Przekształć zapis matematyczny na kod LaTeX z wizualizacją wyniku.</p>
</header>
<section class="grid flex-1 min-h-0 gap-4 md:grid-cols-2">
<div class="flex h-full min-h-0 flex-col rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
<div class="mb-3 flex items-center justify-between gap-3">
<h2 class="text-lg font-semibold">1. Wejście</h2>
</div>
<label for="input" class="mb-2 block text-sm font-medium text-slate-700">Treść do konwersji</label>
<textarea id="input" rows="10" class="min-h-0 w-full flex-1 rounded-xl border border-slate-300 bg-slate-50 p-3 font-mono text-sm outline-none transition focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200" placeholder="Np. f(x) = x dla x >=0; -x dla x <0">f(x) = x dla x >=0; -x dla x <0</textarea>
<div class="mt-4 flex flex-wrap items-center gap-3">
<button id="convertBtn" class="inline-flex items-center rounded-xl bg-indigo-600 px-4 py-2.5 text-sm font-semibold text-white transition hover:bg-indigo-700 disabled:cursor-not-allowed disabled:opacity-60">
Konwertuj
</button>
<button id="exampleBtn" class="inline-flex items-center rounded-xl border border-slate-300 bg-white px-4 py-2.5 text-sm font-semibold text-slate-700 transition hover:bg-slate-50">
Wstaw przykład
</button>
<span id="status" class="text-sm text-slate-500"></span>
</div>
<div id="errorBox" class="mt-4 hidden rounded-xl border border-red-200 bg-red-50 p-3 text-sm text-red-700"></div>
</div>
<div class="flex h-full min-h-0 flex-col rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
<div class="mb-3 flex items-center justify-between gap-3">
<h2 class="text-lg font-semibold">2. Surowy LaTeX</h2>
<div class="flex items-center gap-2">
<button id="copyBtn" class="inline-flex items-center rounded-xl border border-slate-300 bg-white px-3 py-2 text-xs font-semibold text-slate-700 transition hover:bg-slate-50 disabled:cursor-not-allowed disabled:opacity-60">
Kopiuj LaTeX
</button>
</div>
</div>
<pre id="latexRaw" class="min-h-0 flex-1 overflow-auto rounded-xl bg-slate-950 p-4 font-mono text-sm whitespace-pre-wrap break-words text-slate-100">Tutaj pojawi się surowy tekst LaTeX...</pre>
</div>
</section>
<section class="mt-4 flex min-h-0 flex-[0.9] flex-col rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
<div class="mb-3 flex items-center justify-between gap-3">
<h2 class="text-lg font-semibold">3. Podgląd LaTeX</h2>
</div>
<div id="LaTeXPreview" class="min-h-0 flex-1 overflow-x-auto rounded-xl bg-slate-50 p-4"></div>
</section>
</main>
<script>
const API_URL = window.location.protocol.startsWith('http')
? `${window.location.origin}/api/convert`
: 'http://localhost:7070/api/convert';
const inputEl = document.getElementById('input');
const convertBtn = document.getElementById('convertBtn');
const exampleBtn = document.getElementById('exampleBtn');
const copyBtn = document.getElementById('copyBtn');
const statusEl = document.getElementById('status');
const errorBox = document.getElementById('errorBox');
const latexRaw = document.getElementById('latexRaw');
const LaTeXPreview = document.getElementById('LaTeXPreview');
const exampleText = 'f(x) = x dla x >=0; -x dla x <0';
function setStatus(message) {
statusEl.textContent = message || '';
}
function showError(message) {
if (!message) {
errorBox.classList.add('hidden');
errorBox.textContent = '';
return;
}
errorBox.textContent = message;
errorBox.classList.remove('hidden');
}
function renderLaTeX(latex) {
LaTeXPreview.innerHTML = '';
if (!latex) {
LaTeXPreview.innerHTML = '<p class="text-sm text-slate-500">Podgląd pojawi się tutaj po konwersji.</p>';
return;
}
try {
LaTeX.render(latex, LaTeXPreview, {
throwOnError: false,
displayMode: true
});
} catch (err) {
LaTeXPreview.innerHTML = '<p class="text-sm text-red-600">Nie udało się wyrenderować LaTeX.</p>';
}
}
async function copyLatex() {
const latex = latexRaw.textContent || '';
if (!latex || latex === 'Tutaj pojawi się surowy tekst LaTeX...' || latex === 'Brak danych.') {
showError('Brak LaTeX do skopiowania. Najpierw wykonaj konwersję.');
return;
}
try {
await navigator.clipboard.writeText(latex);
setStatus('Skopiowano LaTeX');
showError('');
} catch (err) {
showError('Nie udało się skopiować do schowka.');
}
}
async function convert() {
const expression = inputEl.value.trim();
if (!expression) {
showError('Wpisz treść do konwersji.');
return;
}
convertBtn.disabled = true;
setStatus('Konwertowanie...');
showError('');
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ expression })
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || 'Błąd konwersji.');
}
latexRaw.textContent = data.latex || '';
renderLaTeX(data.latex || '');
setStatus('Gotowe');
} catch (err) {
showError(err.message || 'Nie udało się połączyć z API.');
latexRaw.textContent = 'Brak danych.';
LaTeXPreview.innerHTML = '<p class="text-sm text-slate-500">Brak podglądu z powodu błędu.</p>';
setStatus('Błąd');
} finally {
convertBtn.disabled = false;
}
}
convertBtn.addEventListener('click', convert);
copyBtn.addEventListener('click', copyLatex);
exampleBtn.addEventListener('click', () => {
inputEl.value = exampleText;
showError('');
setStatus('Wstawiono przykład');
});
inputEl.addEventListener('keydown', (event) => {
if ((event.ctrlKey || event.metaKey) && event.key === 'Enter') {
convert();
}
});
renderLaTeX('');
</script>
</body>
</html>