UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

128 lines (125 loc) 4.9 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/RichEditor/components/MathNode.tsx var _core = require('@tiptap/core'); var _react = require('@tiptap/react'); var _katex = require('katex'); var _katex2 = _interopRequireDefault(_katex); var _react3 = require('react'); var _jsxruntime = require('react/jsx-runtime'); function MathNodeView({ node, editor, getPos }) { const latex = typeof node.attrs.latex === "string" ? node.attrs.latex : ""; const renderedHtml = _react3.useMemo.call(void 0, () => { try { return _katex2.default.renderToString(latex, { throwOnError: false, displayMode: false }); } catch (e) { return `<span class="text-error-600">${latex}</span>`; } }, [latex]); const handleClick = () => { const pos = getPos(); if (typeof pos !== "number" || !Number.isFinite(pos)) return; editor.chain().focus().deleteRange({ from: pos, to: pos + node.nodeSize }).insertContent(`$${latex}$`).run(); const docSize = editor.state.doc.content.size; const newPos = Math.min(Math.max(pos + latex.length + 1, 0), docSize); editor.commands.setTextSelection(newPos); }; return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.NodeViewWrapper, { as: "span", className: "inline-math cursor-pointer hover:bg-primary-50 rounded px-0.5 transition-colors", onClick: handleClick, title: "Clique para editar", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { dangerouslySetInnerHTML: { __html: renderedHtml }, className: "inline" } ) } ); } var MathNode = _core.Node.create({ name: "mathInline", group: "inline", inline: true, atom: true, selectable: true, addAttributes() { return { latex: { default: "" } }; }, parseHTML() { return [ { tag: 'span[data-type="math-inline"]', getAttrs: (dom) => { return { latex: _nullishCoalesce(dom.dataset.latex, () => ( "")) }; } } ]; }, renderHTML({ node, HTMLAttributes }) { return [ "span", _core.mergeAttributes.call(void 0, HTMLAttributes, { "data-type": "math-inline", "data-latex": node.attrs.latex }) ]; }, addNodeView() { return _react.ReactNodeViewRenderer.call(void 0, MathNodeView); }, // Disable default input rules addInputRules() { return []; }, addKeyboardShortcuts() { return { // When space is pressed after closing $, convert to math node Space: ({ editor }) => { const { state } = editor; const { selection } = state; const { $from } = selection; const textBefore = $from.parent.textBetween( Math.max(0, $from.parentOffset - 200), $from.parentOffset, "\n" ); const regex = /\$([^$]+)\$$/; const match = regex.exec(textBefore); if (_optionalChain([match, 'optionalAccess', _ => _[1], 'optionalAccess', _2 => _2.trim, 'call', _3 => _3()])) { const latex = match[1]; const matchStart = $from.pos - match[0].length; editor.chain().deleteRange({ from: matchStart, to: $from.pos }).insertContent([ { type: "mathInline", attrs: { latex } }, { type: "text", text: " " } ]).run(); return true; } return false; }, // When backspace is pressed on a math node, convert it back to text for editing Backspace: ({ editor }) => { const { selection } = editor.state; const { $from } = selection; const nodeBefore = $from.nodeBefore; if (_optionalChain([nodeBefore, 'optionalAccess', _4 => _4.type, 'access', _5 => _5.name]) === "mathInline") { const pos = $from.pos - nodeBefore.nodeSize; const latex = nodeBefore.attrs.latex; editor.chain().deleteRange({ from: pos, to: $from.pos }).insertContent(`$${latex}`).run(); return true; } return false; } }; } }); exports.MathNode = MathNode; //# sourceMappingURL=chunk-ULCHG6PK.js.map