UNPKG

react-mathlive

Version:

An interactive math input for react.

124 lines 5.17 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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.MathfieldComponent = exports.combineConfig = void 0; var React = __importStar(require("react")); // @ts-ignore var mathlive_1 = __importDefault(require("mathlive")); require("mathlive/dist/mathlive-fonts.css"); function combineConfig(props) { var combinedConfiguration = __assign({}, props.mathfieldConfig); var onChange = props.onChange; if (onChange) { if (props.mathfieldConfig && props.mathfieldConfig.onContentDidChange) { var fromConfig_1 = props.mathfieldConfig.onContentDidChange; combinedConfiguration.onContentDidChange = function (mf) { onChange(mf.$latex()); fromConfig_1(mf); }; } else { combinedConfiguration.onContentDidChange = function (mf) { return onChange(mf.$latex()); }; } } return combinedConfiguration; } exports.combineConfig = combineConfig; /** A react-control that hosts a mathlive-mathfield in it. */ var MathfieldComponent = /** @class */ (function (_super) { __extends(MathfieldComponent, _super); function MathfieldComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.insertElement = null; _this.combinedConfiguration = combineConfig(_this.props); return _this; } MathfieldComponent.prototype.componentDidUpdate = function (prevProps) { if (!this.mathfield) { throw new Error("Component was not correctly initialized."); } if (prevProps.latex !== undefined) { if (this.props.latex === undefined) { throw new Error("Cannot change from controlled to uncontrolled state!"); } if (this.props.latex !== prevProps.latex) { if (this.props.latex === "") { this.mathfield.$perform("deleteAll"); } else { this.mathfield.$latex(this.props.latex, { suppressChangeNotifications: true, }); } } } }; MathfieldComponent.prototype.render = function () { var _this = this; return React.createElement("div", { ref: function (instance) { return (_this.insertElement = instance); } }); }; MathfieldComponent.prototype.componentDidMount = function () { var _a; if (!this.insertElement) { throw new Error("React did apparently not mount the insert point correctly."); } var initialValue = (_a = this.props.initialLatex) !== null && _a !== void 0 ? _a : this.props.latex; this.mathfield = mathlive_1.default.makeMathField(this.insertElement, this.combinedConfiguration); this.mathfield.$latex(initialValue, { suppressChangeNotifications: true, }); if (this.props.mathfieldRef) { this.props.mathfieldRef(this.mathfield); } }; return MathfieldComponent; }(React.Component)); exports.MathfieldComponent = MathfieldComponent; //# sourceMappingURL=MathfieldComponent.js.map