Files
KaTeX/contrib/copy-tex/copy-tex.js
ylemkimon f628ca142b Generate ECMAScript module for contrib (#1624)
* Create separate entry point of copy-tex for webpack

* Update katex.webpack.js comment

* Generate ECMAScript for contrib

* Update dependencies

* Comment out documentations
2018-10-31 11:37:54 +09:00

25 lines
943 B
JavaScript

import katexReplaceWithTex from './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();
});