UNPKG

rich-text-editor

Version:
104 lines (103 loc) 5.13 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useMathQuill; const MathQuill = __importStar(require("@digabi/mathquill")); const react_1 = require("react"); const use_rerender_1 = __importDefault(require("./use-rerender")); const MQ = MathQuill.getInterface(2); function useMathQuill(opts) { // We use `MathField.latex()` to check for errors below, but setting the // `MathField` latex value does not cause a rerender as React's not aware // of the update. This means that when we set the latex value inside the // `MathField` object, we need to request a rerender from React. The more // "correct" way to do this would probably be to sync React with the object // through `React.useSyncExternalStore` but we pinky-promise to always // manually call the `rerender` function every time after calling the // `MathField.latex(…)` setter, so React's in-sync with MathQuill. const rerender = (0, use_rerender_1.default)(); const [mqHandle, setMqHandle] = (0, react_1.useState)(null); const [lastLatex, setLastLatex] = (0, react_1.useState)(opts.latex); // These refs are needed to access the current state values inside event handler const mqRef = (0, react_1.useRef)(null); const lastLatexRef = (0, react_1.useRef)(lastLatex); // React `ref` objects aren't valid as `useEffect` deps; the official // workaround is to use a memoised callback as the `ref` prop to an element: const ref = (0, react_1.useCallback)(function spawnMathQuillOnElement(node) { if (!node) return; function edit(field) { var _a, _b; // We don't want to signal an event edit that didn't happen from user input *on the // `MathField`*, i.e. we don't want to call `onChange` when it's caused by the raw latex // changing (we'd end up in an infinite loop). The actual focused event is a hidden // textbox inside `field.el()` so we need to use the `.contains()` method. if (field.el().contains(document.activeElement)) { const newValue = (_a = mqRef.current) === null || _a === void 0 ? void 0 : _a.latex(); if (newValue !== undefined) { (_b = opts.onChange) === null || _b === void 0 ? void 0 : _b.call(opts, lastLatexRef.current, newValue); setLastLatex(newValue); lastLatexRef.current = newValue; } } } function enter() { var _a, _b; opts.onEnter((_b = (_a = mqRef.current) === null || _a === void 0 ? void 0 : _a.latex()) !== null && _b !== void 0 ? _b : ''); } const field = MQ.MathField(node, { handlers: { edit, enter } }); field.latex(opts.latex); setMqHandle(field); mqRef.current = field; }, []); (0, react_1.useEffect)(function updateMathQuillField() { if (mqHandle && mqHandle.latex() != opts.latex) { mqHandle === null || mqHandle === void 0 ? void 0 : mqHandle.latex(opts.latex); rerender(); } }, [mqHandle, opts.latex]); return { /** A `ref` that must be attached to the `div` tag that should be controlled by MathQuill */ ref, /** A MathQuill `MathField` that's attached to the `ref` */ mq: mqHandle, /** Boolean representing if the current `latex` code is parseable by MathQuill */ isError: (mqHandle === null || mqHandle === void 0 ? void 0 : mqHandle.latex()) === '' && opts.latex !== '', lastValue: lastLatex, }; }