Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
542 lines (499 loc) · 21.1 KB
/
Copy pathscript.js
File metadata and controls
542 lines (499 loc) · 21.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"use strict";
const COPY_SCALE = 2;
const DOWNLOAD_SCALE = 4;
const DEBOUNCE_MS = 150;
const SVG_NS = "http://www.w3.org/2000/svg";
const presets = {
"rsa-encryption": String.raw`c \equiv m^e \pmod n`,
"rsa-decryption": String.raw`m \equiv c^d \pmod n`,
"euler-totient": String.raw`\phi(n) = (p-1)(q-1)`,
"ecdsa-signature": String.raw`s \equiv k^{-1}(H(m) + dr) \pmod n`,
"elliptic-curve": String.raw`y^2 \equiv x^3 + ax + b \pmod p`,
"modular-inverse": String.raw`a^{-1}a \equiv 1 \pmod n`,
polynomial: String.raw`f(x) = \sum_{i=0}^{n} a_i x^i`,
matrix: String.raw`\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}`
};
function wrapEquation(latex, display) {
const delimiter = display ? "$$" : "$";
return `${delimiter}${latex}${delimiter}`;
}
function readMathEnvironment(source, start = 0) {
const opening = /^\\begin\{(aligned|alignedat|gathered|split|align\*?|gather\*?|equation\*?|[bpBvV]?matrix|cases)\}/.exec(source.slice(start));
if (!opening) return null;
const stack = [opening[1]];
const tokens = /\\(begin|end)\{([^}]+)\}/g;
tokens.lastIndex = start + opening[0].length;
let token;
while ((token = tokens.exec(source))) {
if (token[1] === "begin") stack.push(token[2]);
else {
const expected = stack.pop();
if (token[2] !== expected) {
throw new Error("Mismatched LaTeX environment. Expected \\end{" + expected + "}.");
}
if (!stack.length) return { latex: source.slice(start, tokens.lastIndex), end: tokens.lastIndex };
}
}
throw new Error("Incomplete LaTeX environment. Add \\end{" + stack[stack.length - 1] + "}.");
}
function parseEquationSource(source) {
const value = source.trim();
const environment = readMathEnvironment(value);
if (environment && environment.end === value.length) {
return { latex: environment.latex, display: true };
}
const delimiter = value.startsWith("$$") ? "$$" : value.startsWith("$") ? "$" : null;
if (!delimiter || !value.endsWith(delimiter) ||
(delimiter === "$" && value.endsWith("$$"))) {
throw new Error("Wrap the equation in $$...$$ for Display or $...$ for Inline.");
}
const latex = value.slice(delimiter.length, -delimiter.length).trim();
if (!latex) throw new Error("Enter an equation between the dollar signs.");
return { latex, display: delimiter === "$$" };
}
function extractAiEquations(source) {
if (source.length > 50000) throw new Error("Paste up to 50,000 characters at a time.");
const text = source.replace(/\r\n?/g, "\n");
const pattern = /\x60{3}([^\n\x60]*)\n([\s\S]*?)\x60{3}|(?<!\\)\$\$([\s\S]*?)(?<!\\)\$\$|(?<!\\)\\\[([\s\S]*?)(?<!\\)\\\]|(?<!\\)\\\(([\s\S]*?)(?<!\\)\\\)|(?<![\\$])\$(?![\s$])((?:\\[^\n]|[^$\\\n])+?)(?<!\s)\$(?!\$)|(?<!\\)\\begin\{(aligned|alignedat|gathered|split|align\*?|gather\*?|equation\*?|[bpBvV]?matrix|cases)\}/g;
const equations = [];
let match;
while ((match = pattern.exec(text))) {
if (match[7] !== undefined) {
const environment = readMathEnvironment(text, match.index);
equations.push({ latex: environment.latex, display: true });
pattern.lastIndex = environment.end;
} else if (match[1] !== undefined) {
const language = match[1].trim().toLowerCase();
if (!["", "latex", "tex", "math"].includes(language)) continue;
const body = match[2].trim();
const nested = extractAiEquations(body);
if (nested.length) equations.push(...nested);
else if (language && body) {
if (/\$\$|\\[\[\]()]/.test(body)) {
throw new Error("A LaTeX block has incomplete math delimiters. Check its opening and closing markers.");
}
equations.push({ latex: body, display: true });
}
} else {
const latex = (match[3] ?? match[4] ?? match[5] ?? match[6]).trim();
if (latex) equations.push({ latex, display: match[3] !== undefined || match[4] !== undefined });
}
if (equations.length > 100) throw new Error("Paste up to 100 equations at a time.");
}
return equations;
}
function looksLikeFlattenedMath(source) {
return /[\u200b-\u200d\u2060]/.test(source) ||
(/[∫∑∏∂∇ℏ∞≤]/.test(source) && !/\\(?:[a-zA-Z]+|[\[(])/.test(source));
}
function extractClipboardEquations(html) {
if (!html) return [];
if (html.length > 1000000) throw new Error("Copied response is too large. Copy a smaller selection.");
// Template contents stay inert: pasted markup is never added to the live page.
const template = document.createElement("template");
template.innerHTML = html;
const equations = [];
const seenMath = new Set();
let characters = 0;
for (const annotation of template.content.querySelectorAll("math annotation[encoding]")) {
const encoding = annotation.getAttribute("encoding").trim().toLowerCase();
if (!["application/x-tex", "application/x-latex"].includes(encoding)) continue;
const math = annotation.closest("math");
const latex = annotation.textContent.trim();
if (!latex || seenMath.has(math)) continue;
seenMath.add(math);
characters += latex.length;
if (characters > 50000 || equations.length >= 100) {
throw new Error("Copy up to 100 equations and 50,000 LaTeX characters at a time.");
}
equations.push({
latex,
display: math.getAttribute("display") === "block" || Boolean(math.closest(".katex-display"))
});
}
return equations;
}
const FLATTENED_MATH_MESSAGE = "This looks like copied rendered math: fractions, powers, or duplicated formulas have lost their structure. Paste directly from the original chat with Ctrl+V, or use Copy LaTeX prompt to request the original LaTeX. Bare LaTeX cannot restore missing structure.";
const LATEX_REQUEST = "Please return the equations from your previous response as LaTeX source. Put each complete equation in a separate fenced code block labeled latex. Preserve all fractions, exponents, integral and sum limits, matrices, and line breaks. Do not use rendered equations or duplicated Unicode math text. Preserve the equations as written; do not evaluate or change them.";
const input = document.querySelector("#latex-input");
let isUntouchedExample = input.value === input.defaultValue;
const preview = document.querySelector("#equation-preview");
const errorMessage = document.querySelector("#error-message");
const status = document.querySelector("#status");
const presetSelect = document.querySelector("#preset-select");
const copyImageButton = document.querySelector("#copy-image");
const downloadButton = document.querySelector("#download-png");
const pasteBackgroundInputs = document.querySelectorAll('input[name="paste-background"]');
const aiSource = document.querySelector("#ai-source");
const aiSelect = document.querySelector("#ai-equations");
const aiCode = document.querySelector("#ai-equation-source");
const aiResult = document.querySelector("#ai-result");
const aiError = document.querySelector("#ai-error");
const aiImportButton = document.querySelector("#import-equation");
let aiEquations = [];
function resetAiResults() {
aiEquations = [];
aiSelect.replaceChildren(new Option("No equations extracted yet", ""));
aiSelect.disabled = true;
aiImportButton.disabled = true;
aiCode.textContent = "Your selected LaTeX will appear here.";
aiResult.textContent = "Extract equations to get started.";
aiError.textContent = "";
aiError.hidden = true;
}
function showAiSelection() {
const equation = aiEquations[Number(aiSelect.value)];
aiCode.textContent = equation ? wrapEquation(equation.latex, equation.display) : "";
}
aiSource.addEventListener("input", resetAiResults);
aiSource.addEventListener("paste", event => {
try {
const equations = extractClipboardEquations(event.clipboardData?.getData("text/html"));
if (!equations.length) return;
event.preventDefault();
const start = aiSource.selectionStart;
const end = aiSource.selectionEnd;
const pasted = equations.map(equation => wrapEquation(equation.latex, equation.display)).join("\n\n");
const prefix = start && aiSource.value[start - 1] !== "\n" ? "\n" : "";
const suffix = end < aiSource.value.length && aiSource.value[end] !== "\n" ? "\n" : "";
if (aiSource.value.length - (end - start) + prefix.length + pasted.length + suffix.length > 50000) {
throw new Error("Paste up to 50,000 characters at a time.");
}
aiSource.setRangeText(prefix + pasted + suffix, start, end, "end");
document.querySelector("#ai-raw").checked = false;
resetAiResults();
aiResult.textContent = equations.length + " equation source(s) recovered from rich text. Click Extract equations.";
} catch (error) {
event.preventDefault();
resetAiResults();
aiError.textContent = error.message;
aiError.hidden = false;
}
});
document.querySelector("#copy-latex-prompt").addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(LATEX_REQUEST);
aiResult.textContent = "Prompt copied. Paste it into your AI chat, then copy its LaTeX code blocks here.";
} catch {
aiResult.textContent = LATEX_REQUEST;
}
});
document.querySelector("#ai-raw").addEventListener("change", resetAiResults);
aiSelect.addEventListener("change", showAiSelection);
document.querySelector("#extract-equations").addEventListener("click", () => {
resetAiResults();
try {
if (document.querySelector("#ai-raw").checked) {
if (!aiSource.value.trim()) throw new Error("Paste a LaTeX equation first.");
if (aiSource.value.length > 50000) throw new Error("Paste up to 50,000 characters at a time.");
if (looksLikeFlattenedMath(aiSource.value)) throw new Error(FLATTENED_MATH_MESSAGE);
aiEquations = [{ latex: aiSource.value.trim(), display: true }];
} else {
aiEquations = extractAiEquations(aiSource.value);
}
if (!aiEquations.length) {
if (looksLikeFlattenedMath(aiSource.value)) throw new Error(FLATTENED_MATH_MESSAGE);
throw new Error("No equations found. Paste a latex, tex, or math code block, or enclose the formula in $$...$$, $...$, \\[...\\], or \\(...\\).");
}
aiSelect.replaceChildren(...aiEquations.map((equation, index) =>
new Option((index + 1) + ". " + (equation.display ? "Display" : "Inline") + " — " + equation.latex.replace(/\s+/g, " ").slice(0, 90), String(index))));
aiSelect.disabled = false;
aiImportButton.disabled = false;
aiResult.textContent = aiEquations.length + (aiEquations.length === 1 ? " equation found." : " equations found.") + " Choose one to render.";
showAiSelection();
} catch (error) {
aiError.textContent = error.message;
aiError.hidden = false;
}
});
document.querySelector("#clear-ai").addEventListener("click", () => {
aiSource.value = "";
document.querySelector("#ai-raw").checked = false;
resetAiResults();
aiSource.focus();
});
aiImportButton.addEventListener("click", () => {
const equation = aiEquations[Number(aiSelect.value)];
if (!equation) return;
input.value = wrapEquation(equation.latex, equation.display);
isUntouchedExample = false;
presetSelect.value = "";
queueRender(true);
input.focus();
showStatus("Equation imported from AI response");
});
let changeVersion = 0;
let renderedVersion = -1;
let debounceTimer;
let statusTimer;
let renderQueue = Promise.resolve();
const cachedPngBlobs = new Map();
const pngPromises = new Map();
function equationColor() {
return document.querySelector('input[name="paste-background"]:checked').value === "dark"
? "#ffffff" : "#1f252b";
}
function showStatus(message, isError = false) {
clearTimeout(statusTimer);
status.textContent = message;
status.classList.toggle("is-error", isError);
statusTimer = setTimeout(() => { status.textContent = ""; }, 1800);
}
function setError(message) {
errorMessage.textContent = message;
errorMessage.hidden = !message;
}
function setImageActionsEnabled(enabled) {
copyImageButton.disabled = !enabled;
downloadButton.disabled = !enabled;
}
function queueRender(immediate = false) {
const version = ++changeVersion;
clearTimeout(debounceTimer);
preview.classList.remove("is-selected");
renderedVersion = -1;
cachedPngBlobs.clear();
pngPromises.clear();
setImageActionsEnabled(false);
setError("");
const schedule = () => {
// MathJax 3 conversions share internal state, so they must run in sequence.
renderQueue = renderQueue.catch(() => {}).then(() => renderEquation(version));
};
if (immediate) schedule();
else debounceTimer = setTimeout(schedule, DEBOUNCE_MS);
}
async function renderEquation(version) {
if (version !== changeVersion) return;
const latex = input.value;
if (!latex.trim()) {
preview.classList.remove("inline");
preview.replaceChildren(makePlaceholder("Enter LaTeX to see an equation."));
return;
}
if (!window.MathJax?.tex2svgPromise) {
preview.replaceChildren(makePlaceholder("Equation preview could not load."));
setError("Equation preview could not load. Check your internet connection and reload the page.");
return;
}
try {
const equation = parseEquationSource(latex);
const { display } = equation;
document.querySelector(`input[name="mode"][value="${display ? "display" : "inline"}"]`).checked = true;
const metrics = MathJax.getMetricsFor(preview, display);
const result = await MathJax.tex2svgPromise(equation.latex, { ...metrics, display });
if (version !== changeVersion) return;
const svg = result.querySelector("svg");
if (!svg) throw new Error("Equation renderer did not return an SVG equation.");
preview.classList.toggle("inline", !display);
preview.replaceChildren(result);
renderedVersion = version;
setImageActionsEnabled(true);
setError("");
} catch (error) {
if (version !== changeVersion) return;
preview.classList.remove("inline");
preview.replaceChildren(makePlaceholder("Fix the LaTeX to update the preview."));
setError(error?.message || "The equation could not be rendered.");
}
}
function makePlaceholder(message) {
const span = document.createElement("span");
span.className = "placeholder";
span.textContent = message;
return span;
}
function svgToPng(svg, color, scale) {
const box = svg.getBoundingClientRect();
if (!box.width || !box.height) return Promise.reject(new Error("Equation has no visible size."));
const width = Math.ceil(box.width * scale);
const height = Math.ceil(box.height * scale);
const clone = svg.cloneNode(true);
clone.setAttribute("xmlns", SVG_NS);
clone.setAttribute("width", `${box.width}px`);
clone.setAttribute("height", `${box.height}px`);
clone.setAttribute("color", color);
clone.style.color = color;
const xml = new XMLSerializer().serializeToString(clone);
const svgUrl = URL.createObjectURL(new Blob([xml], { type: "image/svg+xml;charset=utf-8" }));
return new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => {
URL.revokeObjectURL(svgUrl);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
if (!context) return reject(new Error("Canvas is unavailable."));
context.imageSmoothingEnabled = true;
context.imageSmoothingQuality = "high";
context.scale(scale, scale);
context.drawImage(image, 0, 0, box.width, box.height);
canvas.toBlob(blob => {
if (blob) resolve(blob);
else reject(new Error("PNG export failed."));
}, "image/png");
};
image.onerror = () => {
URL.revokeObjectURL(svgUrl);
reject(new Error("Could not convert the equation SVG to PNG."));
};
image.src = svgUrl;
});
}
function generatePngBlob(scale) {
if (cachedPngBlobs.has(scale)) return Promise.resolve(cachedPngBlobs.get(scale));
if (pngPromises.has(scale)) return pngPromises.get(scale);
if (renderedVersion !== changeVersion) return Promise.reject(new Error("The preview is still updating."));
const svg = preview.querySelector("svg");
if (!svg) return Promise.reject(new Error("There is no equation to export."));
const version = renderedVersion;
const pngPromise = svgToPng(svg, equationColor(), scale).then(blob => {
if (version !== changeVersion) throw new Error("The equation changed. Try again.");
cachedPngBlobs.set(scale, blob);
return blob;
}).finally(() => {
if (version === changeVersion) pngPromises.delete(scale);
});
pngPromises.set(scale, pngPromise);
return pngPromise;
}
async function copyEquationImage() {
if (renderedVersion !== changeVersion) {
showStatus("Wait for the equation preview to update.", true);
return;
}
if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined" ||
(ClipboardItem.supports && !ClipboardItem.supports("image/png"))) {
showStatus("Image clipboard is unsupported. Use Download PNG instead.", true);
return;
}
try {
const isWhite = equationColor() === "#ffffff";
// Pass the Blob promise directly so clipboard.write starts during the user gesture.
const png = generatePngBlob(COPY_SCALE);
await navigator.clipboard.write([new ClipboardItem({ "image/png": png })]);
showStatus(isWhite
? "White equation copied as transparent image"
: "Dark equation copied as transparent image");
} catch (error) {
showStatus("Clipboard access failed. Use Download PNG instead.", true);
}
}
async function copyLatex() {
if (!navigator.clipboard?.writeText) {
showStatus("Text clipboard is unavailable in this browser.", true);
return;
}
try {
await navigator.clipboard.writeText(input.value);
showStatus("LaTeX copied");
} catch (error) {
showStatus("Clipboard access failed.", true);
}
}
async function downloadPng() {
try {
const blob = await generatePngBlob(DOWNLOAD_SCALE);
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "equation.png";
document.body.appendChild(link);
link.click();
link.remove();
setTimeout(() => URL.revokeObjectURL(url), 1000);
showStatus("PNG downloaded");
} catch (error) {
showStatus(error?.message || "PNG download failed.", true);
}
}
input.addEventListener("paste", event => {
const pastedText = event.clipboardData?.getData("text/plain");
if (!pastedText) {
if ([...(event.clipboardData?.items || [])].some(item => item.type.startsWith("image/"))) {
showStatus("Clipboard contains an image. Paste LaTeX text here.", true);
}
return;
}
// Let the browser perform its normal paste, replacing only the untouched starter example.
if (isUntouchedExample && input.selectionStart === input.selectionEnd) input.select();
});
input.addEventListener("input", () => {
isUntouchedExample = false;
queueRender();
});
document.querySelectorAll('input[name="mode"]').forEach(radio => {
radio.addEventListener("change", () => {
if (input.value.trim()) {
try {
const { latex } = parseEquationSource(input.value);
input.value = wrapEquation(latex, radio.value === "display");
isUntouchedExample = false;
} catch (error) {
// Keep incomplete input editable; the preview will show the delimiter error.
}
}
queueRender(true);
});
});
presetSelect.addEventListener("change", () => {
if (!presetSelect.value) return;
const display = document.querySelector('input[name="mode"]:checked').value === "display";
input.value = wrapEquation(presets[presetSelect.value], display);
isUntouchedExample = false;
queueRender(true);
input.focus();
});
pasteBackgroundInputs.forEach(radio => {
radio.addEventListener("change", () => {
preview.classList.toggle("dark", radio.value === "dark");
queueRender(true);
});
});
document.querySelector("#copy-latex").addEventListener("click", copyLatex);
copyImageButton.addEventListener("click", copyEquationImage);
downloadButton.addEventListener("click", downloadPng);
document.querySelector("#clear-input").addEventListener("click", () => {
input.value = "";
isUntouchedExample = false;
presetSelect.value = "";
queueRender(true);
input.focus();
});
function selectEquation() {
if (!preview.querySelector("svg")) return;
preview.classList.add("is-selected");
preview.focus({ preventScroll: true });
}
preview.addEventListener("pointerdown", event => {
if (event.button === 0) selectEquation();
});
preview.addEventListener("click", selectEquation);
preview.addEventListener("focusout", event => {
if (!preview.contains(event.relatedTarget)) preview.classList.remove("is-selected");
});
preview.addEventListener("keydown", event => {
if ((event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === "c") {
event.preventDefault();
copyEquationImage();
}
});
if (window.MathJax?.startup?.promise) {
MathJax.startup.promise.then(() => {
// Direct tex2svgPromise conversion does not insert MathJax's SVG stylesheet.
if (!document.querySelector("#MJX-SVG-styles")) {
document.head.appendChild(MathJax.svgStylesheet());
}
queueRender(true);
}).catch(() => {
preview.replaceChildren(makePlaceholder("Equation preview could not load."));
setError("Equation preview could not load. Check your internet connection and reload the page.");
});
} else {
preview.replaceChildren(makePlaceholder("Equation preview could not load."));
setError("Equation preview could not load. Check your internet connection and reload the page.");
}