function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import './czi-math-view.css';
import CustomNodeView from './CustomNodeView.js';
import MathInlineEditor from './MathInlineEditor.js';
import * as React from 'react';
import { createPopUp } from '@modusoperandi/licit-ui-commands';
import cx from 'classnames';
import renderLaTeXAsHTML from './renderLaTeXAsHTML.js';
import uuid from './uuid.js';
import { Decoration } from 'prosemirror-view';
import { FRAMESET_BODY_CLASSNAME } from './EditorFrameset.js';
import { Node } from 'prosemirror-model';
import { atAnchorBottomCenter } from '@modusoperandi/licit-ui-commands';
import { NodeSelection } from 'prosemirror-state';
var EMPTY_SRC = 'data:image/gif;base64,' + 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
var MathViewBody = /*#__PURE__*/function (_React$PureComponent) {
  function MathViewBody() {
    var _this;
    _classCallCheck(this, MathViewBody);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, MathViewBody, [].concat(args));
    _defineProperty(_this, "props", void 0);
    _defineProperty(_this, "state", {
      isEditing: false
    });
    _defineProperty(_this, "_inlineEditor", null);
    _defineProperty(_this, "_id", uuid());
    _defineProperty(_this, "_mounted", false);
    _defineProperty(_this, "_onEditStart", function () {
      _this.setState({
        isEditing: true
      });
    });
    _defineProperty(_this, "_onEditEnd", function () {
      _this.setState({
        isEditing: false
      });
    });
    _defineProperty(_this, "_onChange", function (value) {
      if (!_this._mounted) {
        return;
      }
      var align = value ? value.align : null;
      var latex = value ? value.latex : null;
      var _this$props = _this.props,
        getPos = _this$props.getPos,
        node = _this$props.node,
        editorView = _this$props.editorView;
      var pos = getPos();
      var attrs = _objectSpread(_objectSpread({}, node.attrs), {}, {
        latex: latex,
        align: align
      });
      var tr = editorView.state.tr;
      var selection = editorView.state.selection;
      tr = tr.setNodeMarkup(pos, null, attrs);
      // [FS] IRAD-1005 2020-07-23
      // Upgrade outdated packages.
      // reset selection to original using the latest doc.
      var origSelection = NodeSelection.create(tr.doc, selection.from);
      tr = tr.setSelection(origSelection);
      editorView.dispatch(tr);
    });
    return _this;
  }
  _inherits(MathViewBody, _React$PureComponent);
  return _createClass(MathViewBody, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this._mounted = true;
      this._renderInlineEditor();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this._mounted = false;
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      this._renderInlineEditor();
    }
  }, {
    key: "render",
    value: function render() {
      // TODO: Resolve `readOnly`;
      var readOnly = false;
      var _this$props2 = this.props,
        node = _this$props2.node,
        selected = _this$props2.selected,
        focused = _this$props2.focused;
      var attrs = node.attrs;
      var latex = attrs.latex;
      var isEditing = this.state.isEditing;
      var active = (focused || isEditing) && !readOnly;
      var className = cx('czi-math-view-body', {
        active: active,
        selected: selected
      });
      var html = renderLaTeXAsHTML(latex);
      return /*#__PURE__*/React.createElement("span", {
        className: className,
        "data-active": active ? 'true' : null,
        "data-latex": latex || '',
        id: this._id,
        title: latex
      }, /*#__PURE__*/React.createElement("img", {
        alt: latex,
        className: "czi-math-view-body-img",
        src: EMPTY_SRC,
        title: latex
      }), /*#__PURE__*/React.createElement("span", {
        className: "czi-math-view-body-content",
        dangerouslySetInnerHTML: {
          __html: html
        }
      }));
    }
  }, {
    key: "_renderInlineEditor",
    value: function _renderInlineEditor() {
      var _this2 = this;
      var el = document.getElementById(this._id);
      if (!el || el.getAttribute('data-active') !== 'true') {
        this._inlineEditor && this._inlineEditor.close();
        return;
      }
      var node = this.props.node;
      var editorProps = {
        value: node.attrs,
        onSelect: this._onChange,
        onEditStart: this._onEditStart,
        onEditEnd: this._onEditEnd
      };
      if (this._inlineEditor) {
        this._inlineEditor.update(editorProps);
      } else {
        this._inlineEditor = createPopUp(MathInlineEditor, editorProps, {
          anchor: el,
          autoDismiss: false,
          container: el.closest(".".concat(FRAMESET_BODY_CLASSNAME)),
          position: atAnchorBottomCenter,
          onClose: function onClose() {
            _this2._inlineEditor = null;
          }
        });
      }
    }
  }]);
}(React.PureComponent);
var MathNodeView = /*#__PURE__*/function (_CustomNodeView) {
  function MathNodeView() {
    _classCallCheck(this, MathNodeView);
    return _callSuper(this, MathNodeView, arguments);
  }
  _inherits(MathNodeView, _CustomNodeView);
  return _createClass(MathNodeView, [{
    key: "createDOMElement",
    value:
    // @override
    function createDOMElement() {
      var el = document.createElement('span');
      el.className = 'czi-math-view';
      this._updateDOM(el);
      return el;
    }

    // @override
  }, {
    key: "update",
    value: function update(node, decorations) {
      _get(_getPrototypeOf(MathNodeView.prototype), "update", this).call(this, node, decorations);
      this._updateDOM(this.dom);
      return true;
    }

    // @override
  }, {
    key: "renderReactComponent",
    value: function renderReactComponent() {
      return /*#__PURE__*/React.createElement(MathViewBody, this.props);
    }
  }, {
    key: "_updateDOM",
    value: function _updateDOM(el) {
      var align = this.props.node.attrs.align;
      var className = 'czi-math-view';
      if (align) {
        className += ' align-' + align;
      }
      el.className = className;
    }
  }]);
}(CustomNodeView);
export default MathNodeView;
import { bpfrpt_proptype_NodeViewProps } from "./CustomNodeView.js";