Spaces:
Sleeping
Sleeping
File size: 5,450 Bytes
6981282 |
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
const DEFAULT_LATEX = `\\documentclass{article}
\\usepackage[utf8]{inputenc}
\\usepackage{amsmath}
\\usepackage{graphicx}
\\title{My Document}
\\author{Author Name}
\\date{\\today}
\\begin{document}
\\maketitle
\\section{Introduction}
Welcome to the \\LaTeX\\ editor! Start typing to see instant preview.
\\section{Mathematics}
Here's a famous equation:
\\[
E = mc^2
\\]
And an inline equation: $a^2 + b^2 = c^2$
\\section{Lists}
\\begin{itemize}
\\item First item
\\item Second item
\\item Third item
\\end{itemize}
\\end{document}`;
const editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'stex',
theme: 'dracula',
lineNumbers: true,
lineWrapping: true,
autofocus: true,
tabSize: 4,
indentUnit: 4,
matchBrackets: true,
autoCloseBrackets: true
});
editor.setValue(DEFAULT_LATEX);
const statusEl = document.getElementById('status');
const pdfCanvas = document.getElementById('pdf-canvas');
const pdfContainer = document.getElementById('pdf-container');
const placeholder = document.getElementById('placeholder');
const ctx = pdfCanvas.getContext('2d');
let currentPdf = null;
let currentPage = 1;
let scale = 1.5;
let debounceTimer = null;
let ws = null;
let reconnectAttempts = 0;
const MAX_RECONNECT_ATTEMPTS = 5;
function setStatus(text, type = 'info') {
statusEl.textContent = text;
statusEl.className = 'status ' + type;
}
function connectWebSocket() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
ws = new WebSocket(`${protocol}//${window.location.host}/ws`);
ws.onopen = () => {
setStatus('Connected', 'success');
reconnectAttempts = 0;
compile();
};
ws.onmessage = async (event) => {
const data = JSON.parse(event.data);
if (data.type === 'compiling') {
setStatus('Compiling...', 'info');
} else if (data.type === 'pdf') {
setStatus('Ready', 'success');
await renderPdf(data.data);
} else if (data.type === 'error') {
setStatus('Compilation Error', 'error');
showError(data.log);
}
};
ws.onclose = () => {
setStatus('Disconnected', 'error');
if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
reconnectAttempts++;
setTimeout(connectWebSocket, 1000 * reconnectAttempts);
}
};
ws.onerror = () => {
setStatus('Connection Error', 'error');
};
}
function compile() {
if (ws && ws.readyState === WebSocket.OPEN) {
const latex = editor.getValue();
ws.send(JSON.stringify({ type: 'compile', latex }));
}
}
function debounceCompile() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(compile, 500);
}
async function renderPdf(base64Data) {
try {
const pdfData = atob(base64Data);
const uint8Array = new Uint8Array(pdfData.length);
for (let i = 0; i < pdfData.length; i++) {
uint8Array[i] = pdfData.charCodeAt(i);
}
currentPdf = await pdfjsLib.getDocument({ data: uint8Array }).promise;
await renderPage(currentPage);
placeholder.style.display = 'none';
pdfCanvas.style.display = 'block';
} catch (e) {
console.error('PDF render error:', e);
}
}
async function renderPage(pageNum) {
if (!currentPdf) return;
const page = await currentPdf.getPage(pageNum);
const viewport = page.getViewport({ scale });
pdfCanvas.height = viewport.height;
pdfCanvas.width = viewport.width;
await page.render({
canvasContext: ctx,
viewport: viewport
}).promise;
}
function showError(log) {
placeholder.innerHTML = `<pre class="error-log">${escapeHtml(log)}</pre>`;
placeholder.style.display = 'block';
pdfCanvas.style.display = 'none';
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
document.getElementById('zoom-in').addEventListener('click', async () => {
scale = Math.min(scale + 0.25, 3);
document.getElementById('zoom-level').textContent = Math.round(scale / 1.5 * 100) + '%';
await renderPage(currentPage);
});
document.getElementById('zoom-out').addEventListener('click', async () => {
scale = Math.max(scale - 0.25, 0.5);
document.getElementById('zoom-level').textContent = Math.round(scale / 1.5 * 100) + '%';
await renderPage(currentPage);
});
const resizer = document.getElementById('resizer');
const editorPanel = document.querySelector('.editor-panel');
let isResizing = false;
resizer.addEventListener('mousedown', (e) => {
isResizing = true;
document.body.style.cursor = 'col-resize';
document.body.style.userSelect = 'none';
});
document.addEventListener('mousemove', (e) => {
if (!isResizing) return;
const container = document.querySelector('.main');
const containerRect = container.getBoundingClientRect();
const percentage = ((e.clientX - containerRect.left) / containerRect.width) * 100;
const clamped = Math.min(Math.max(percentage, 20), 80);
editorPanel.style.width = clamped + '%';
});
document.addEventListener('mouseup', () => {
isResizing = false;
document.body.style.cursor = '';
document.body.style.userSelect = '';
});
editor.on('change', debounceCompile);
connectWebSocket();
|