UNPKG

mathpix-markdown-it

Version:

Mathpix-markdown-it is an open source implementation of the mathpix-markdown spec written in Typescript. It relies on the following open source libraries: MathJax v3 (to render math with SVGs), markdown-it (for standard Markdown parsing)

96 lines 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeListenerCopyToClipboardEvents = exports.addListenerCopyToClipboardEvents = exports.clicked = exports.clickInsideCopyElement = void 0; var tslib_1 = require("tslib"); var clipboard_1 = require("./clipboard"); var CLIPBOARD_COPY_TIMER_DURATION = 2000; var showSVG = function (svg) { svg.style.display = 'inline-block'; }; var hideSVG = function (svg) { svg.style.display = 'none'; }; var clickInsideCopyElement = function (e) { var el = e.target; if (el.tagName === "CLIPBOARD-COPY") { return el; } else { var elParent = null; while (el = el.parentNode) { if (el.tagName === "CLIPBOARD-COPY") { elParent = el; } } return elParent; } }; exports.clickInsideCopyElement = clickInsideCopyElement; function copy(button) { return tslib_1.__awaiter(this, void 0, void 0, function () { function trigger() { button.dispatchEvent(new CustomEvent('clipboard-copy', { bubbles: true })); } var text; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: text = button.getAttribute('value'); if (button.getAttribute('aria-disabled') === 'true') { return [2 /*return*/]; } if (!text) return [3 /*break*/, 2]; return [4 /*yield*/, (0, clipboard_1.copyText)(text)]; case 1: _a.sent(); trigger(); _a.label = 2; case 2: return [2 /*return*/]; } }); }); } var clicked = function (event) { var button = (0, exports.clickInsideCopyElement)(event); if (button instanceof HTMLElement) { copy(button); } }; exports.clicked = clicked; // Toggle a copy button. var showCopy = function (button) { var _a = tslib_1.__read(button.querySelectorAll('.mmd-clipboard-icon'), 2), copyIcon = _a[0], checkIcon = _a[1]; if (!copyIcon || !checkIcon) return; button.setAttribute('aria-label', 'Copy'); showSVG(copyIcon); hideSVG(checkIcon); }; // Toggle a copy button. var showCheck = function (button) { var _a = tslib_1.__read(button.querySelectorAll('.mmd-clipboard-icon'), 2), copyIcon = _a[0], checkIcon = _a[1]; if (!copyIcon || !checkIcon) return; button.setAttribute('aria-label', 'Copied'); hideSVG(copyIcon); showSVG(checkIcon); }; var handleClipboardCopy = function (event) { var el = event.target; showCheck(el); setTimeout(function () { el.setAttribute('aria-label', ''); showCopy(el); }, CLIPBOARD_COPY_TIMER_DURATION); }; var addListenerCopyToClipboardEvents = function () { document.addEventListener('click', exports.clicked); document.addEventListener('clipboard-copy', handleClipboardCopy); }; exports.addListenerCopyToClipboardEvents = addListenerCopyToClipboardEvents; var removeListenerCopyToClipboardEvents = function () { document.removeEventListener('click', exports.clicked); document.removeEventListener('clipboard-copy', handleClipboardCopy); }; exports.removeListenerCopyToClipboardEvents = removeListenerCopyToClipboardEvents; //# sourceMappingURL=index.js.map