copy-tex.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory();
  4. else if(typeof define === 'function' && define.amd)
  5. define([], factory);
  6. else {
  7. var a = factory();
  8. for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
  9. }
  10. })((typeof self !== 'undefined' ? self : this), function() {
  11. return /******/ (function() { // webpackBootstrap
  12. /******/ "use strict";
  13. var __webpack_exports__ = {};
  14. ;// CONCATENATED MODULE: ./contrib/copy-tex/katex2tex.js
  15. // Set these to how you want inline and display math to be delimited.
  16. var defaultCopyDelimiters = {
  17. inline: ['$', '$'],
  18. // alternative: ['\(', '\)']
  19. display: ['$$', '$$'] // alternative: ['\[', '\]']
  20. }; // Replace .katex elements with their TeX source (<annotation> element).
  21. // Modifies fragment in-place. Useful for writing your own 'copy' handler,
  22. // as in copy-tex.js.
  23. function katexReplaceWithTex(fragment, copyDelimiters) {
  24. if (copyDelimiters === void 0) {
  25. copyDelimiters = defaultCopyDelimiters;
  26. }
  27. // Remove .katex-html blocks that are preceded by .katex-mathml blocks
  28. // (which will get replaced below).
  29. var katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
  30. for (var i = 0; i < katexHtml.length; i++) {
  31. var element = katexHtml[i];
  32. if (element.remove) {
  33. element.remove();
  34. } else if (element.parentNode) {
  35. element.parentNode.removeChild(element);
  36. }
  37. } // Replace .katex-mathml elements with their annotation (TeX source)
  38. // descendant, with inline delimiters.
  39. var katexMathml = fragment.querySelectorAll('.katex-mathml');
  40. for (var _i = 0; _i < katexMathml.length; _i++) {
  41. var _element = katexMathml[_i];
  42. var texSource = _element.querySelector('annotation');
  43. if (texSource) {
  44. if (_element.replaceWith) {
  45. _element.replaceWith(texSource);
  46. } else if (_element.parentNode) {
  47. _element.parentNode.replaceChild(texSource, _element);
  48. }
  49. texSource.innerHTML = copyDelimiters.inline[0] + texSource.innerHTML + copyDelimiters.inline[1];
  50. }
  51. } // Switch display math to display delimiters.
  52. var displays = fragment.querySelectorAll('.katex-display annotation');
  53. for (var _i2 = 0; _i2 < displays.length; _i2++) {
  54. var _element2 = displays[_i2];
  55. _element2.innerHTML = copyDelimiters.display[0] + _element2.innerHTML.substr(copyDelimiters.inline[0].length, _element2.innerHTML.length - copyDelimiters.inline[0].length - copyDelimiters.inline[1].length) + copyDelimiters.display[1];
  56. }
  57. return fragment;
  58. }
  59. /* harmony default export */ var katex2tex = (katexReplaceWithTex);
  60. ;// CONCATENATED MODULE: ./contrib/copy-tex/copy-tex.js
  61. // Return <div class="katex"> element containing node, or null if not found.
  62. function closestKatex(node) {
  63. // If node is a Text Node, for example, go up to containing Element,
  64. // where we can apply the `closest` method.
  65. var element = node instanceof Element ? node : node.parentElement;
  66. return element && element.closest('.katex');
  67. } // Global copy handler to modify behavior on/within .katex elements.
  68. document.addEventListener('copy', function (event) {
  69. var selection = window.getSelection();
  70. if (selection.isCollapsed || !event.clipboardData) {
  71. return; // default action OK if selection is empty or unchangeable
  72. }
  73. var clipboardData = event.clipboardData;
  74. var range = selection.getRangeAt(0); // When start point is within a formula, expand to entire formula.
  75. var startKatex = closestKatex(range.startContainer);
  76. if (startKatex) {
  77. range.setStartBefore(startKatex);
  78. } // Similarly, when end point is within a formula, expand to entire formula.
  79. var endKatex = closestKatex(range.endContainer);
  80. if (endKatex) {
  81. range.setEndAfter(endKatex);
  82. }
  83. var fragment = range.cloneContents();
  84. if (!fragment.querySelector('.katex-mathml')) {
  85. return; // default action OK if no .katex-mathml elements
  86. }
  87. var htmlContents = Array.prototype.map.call(fragment.childNodes, function (el) {
  88. return el instanceof Text ? el.textContent : el.outerHTML;
  89. }).join(''); // Preserve usual HTML copy/paste behavior.
  90. clipboardData.setData('text/html', htmlContents); // Rewrite plain-text version.
  91. clipboardData.setData('text/plain', katex2tex(fragment).textContent); // Prevent normal copy handling.
  92. event.preventDefault();
  93. });
  94. __webpack_exports__ = __webpack_exports__["default"];
  95. /******/ return __webpack_exports__;
  96. /******/ })()
  97. ;
  98. });