mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 11:48:41 +00:00
* copy-tex contrib module * Factor out replacement code * Fix lint * Support for more browsers, in particular Firefox * Use for loop instead of Array.forEach * Use replaceChild if replaceWith is unavailable * Browserify to remove let etc. * Fix HTML handling, in particular for Edge * Convert DocumentFragment to HTML directly (via children' outerHTML) * Set HTML content *before* text content; Edge takes the last only * Handle .katex-html and .katex-mathml separately * Implement option 2: CSS user-select: all Also fix auto-render.js build location * Revise documentation according to @kevinbarabash's comments * Split copy-tex.js into it + katex2tex.js This supports re-use of code in a custom copy handler. * Document custom copy handler * Add missing file
25 lines
948 B
JavaScript
25 lines
948 B
JavaScript
const katexReplaceWithTex = require('./katex2tex');
|
|
|
|
// Global copy handler to modify behavior on .katex elements.
|
|
document.addEventListener('copy', function(event) {
|
|
const selection = window.getSelection();
|
|
if (selection.isCollapsed) {
|
|
return; // default action OK if selection is empty
|
|
}
|
|
const fragment = selection.getRangeAt(0).cloneContents();
|
|
if (!fragment.querySelector('.katex-mathml')) {
|
|
return; // default action OK if no .katex-mathml elements
|
|
}
|
|
// Preserve usual HTML copy/paste behavior.
|
|
const html = [];
|
|
for (let i = 0; i < fragment.childNodes.length; i++) {
|
|
html.push(fragment.childNodes[i].outerHTML);
|
|
}
|
|
event.clipboardData.setData('text/html', html.join(''));
|
|
// Rewrite plain-text version.
|
|
event.clipboardData.setData('text/plain',
|
|
katexReplaceWithTex(fragment).textContent);
|
|
// Prevent normal copy handling.
|
|
event.preventDefault();
|
|
});
|