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 _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 cx from 'classnames';
import { Schema } from 'prosemirror-model';
import { EditorState, Transaction } from 'prosemirror-state';
import { Transform } from 'prosemirror-transform';
import { EditorView } from 'prosemirror-view';
import * as React from 'react';
import webfontloader from 'webfontloader';
import 'prosemirror-gapcursor/style/gapcursor.css';
import 'prosemirror-view/style/prosemirror.css';
import { exportJSON, registerEditorView, registeryKeys, releaseEditorView } from '../CZIProseMirror.js';
import { BOOKMARK, LIST_ITEM, MATH } from '../NodeNames.js';
import WebFontLoader from '../WebFontLoader.js';
import { preLoadFonts } from '../FontTypeMarkSpec.js';
import createEmptyEditorState from '../createEmptyEditorState.js';
import normalizeHTML from '../normalizeHTML.js';
import BookmarkNodeView from './BookmarkNodeView.js';
import CustomEditorView from './CustomEditorView.js';
import CustomNodeView from './CustomNodeView.js';
import ListItemNodeView from './ListItemNodeView.js';
import MathNodeView from './MathNodeView.js';
import handleEditorDrop from './handleEditorDrop.js';
import handleEditorKeyDown from './handleEditorKeyDown.js';
import handleEditorPaste from './handleEditorPaste.js';
import uuid from './uuid.js';
import './czi-editor.css';
var bpfrpt_proptype_EditorProps = {
  "autoFocus": PropTypes.bool,
  "disabled": PropTypes.bool,
  "dispatchTransaction": PropTypes.func,
  "editorState": function editorState() {
    return (typeof EditorState === "function" ? PropTypes.instanceOf(EditorState) : PropTypes.any).apply(this, arguments);
  },
  "embedded": PropTypes.bool,
  "onBlur": PropTypes.func,
  "onChange": PropTypes.func,
  "onReady": PropTypes.func,
  // Mapping for custom node views.
  "nodeViews": PropTypes.objectOf(function () {
    return (typeof CustomNodeView === "function" ? PropTypes.instanceOf(CustomNodeView).isRequired : PropTypes.any.isRequired).apply(this, arguments);
  }),
  "placeholder": PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  "readOnly": PropTypes.bool,
  "runtime": function runtime() {
    return (typeof bpfrpt_proptype_EditorRuntime === "function" ? bpfrpt_proptype_EditorRuntime : PropTypes.shape(bpfrpt_proptype_EditorRuntime)).apply(this, arguments);
  },
  "transformPastedHTML": PropTypes.func
};
// Export utilities for debugging.
window.CZIProseMirror = {
  exportJSON: exportJSON,
  registeryKeys: registeryKeys
};
var AUTO_FOCUS_DELAY = 350;

// Default custom node views.
export var DEFAULT_NODE_VIEWS = Object.freeze(_defineProperty(_defineProperty(_defineProperty({}, MATH, MathNodeView), BOOKMARK, BookmarkNodeView), LIST_ITEM, ListItemNodeView));
var EDITOR_EMPTY_STATE = Object.freeze(createEmptyEditorState());

// Monkey patch the `scrollIntoView` mathod of 'Transaction'.
// Why this is necessary?
// It appears that promse-mirror does call `scrollIntoView` extensively
// from many of the built-in transformations, thus cause unwanted page
// scrolls. To make the behavior more manageable, this patched method asks
// developer to explicitly use `scrollIntoView(true)` to enforce page scroll.
var scrollIntoView = Transaction.prototype.scrollIntoView;
var scrollIntoViewPatched = function scrollIntoViewPatched(forced) {
  if (forced === true && scrollIntoView) {
    return scrollIntoView.call(this);
  } else {
    return this;
  }
};
Transaction.prototype.scrollIntoView = scrollIntoViewPatched;

// Sets the implementation so that `FontTypeMarkSpec` can load custom fonts.
WebFontLoader.setImplementation(webfontloader);
// FS IRAD-988 2020-06-18
preLoadFonts();
var handleDOMEvents = {
  drop: handleEditorDrop,
  keydown: handleEditorKeyDown,
  paste: handleEditorPaste
};
function bindNodeView(NodeView) {
  return function (node, view, getPos, decorations) {
    return new NodeView(node, view, getPos, decorations);
  };
}
function getSchema(editorState) {
  return editorState ? editorState.schema : EDITOR_EMPTY_STATE.schema;
}
var Editor = /*#__PURE__*/function (_React$PureComponent) {
  function Editor() {
    var _this;
    _classCallCheck(this, Editor);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, Editor, [].concat(args));
    _defineProperty(_this, "_autoFocusTimer", 0);
    _defineProperty(_this, "_id", uuid());
    _defineProperty(_this, "_editorView", null);
    _defineProperty(_this, "props", void 0);
    _defineProperty(_this, "state", {
      isPrinting: false
    });
    _defineProperty(_this, "_onBlur", function () {
      var onBlur = _this.props.onBlur;
      var view = _this._editorView;
      if (view && !view.disabled && !view.readOnly && onBlur) {
        onBlur();
      }
    });
    _defineProperty(_this, "focus", function () {
      var view = _this._editorView;
      if (view && !view.disabled && !view.readOnly) {
        view.focus();
      }
    });
    // [FS-AFQ][20-FEB-2020]
    // Collaboration
    _defineProperty(_this, "_dispatchTransaction", function (transaction) {
      var _this$props = _this.props,
        editorState = _this$props.editorState,
        readOnly = _this$props.readOnly,
        onChange = _this$props.onChange;
      if (readOnly === true || !onChange) {
        return;
      }
      onChange({
        transaction: transaction,
        state: editorState || EDITOR_EMPTY_STATE
      });
    });
    _defineProperty(_this, "_isEditable", function () {
      var _this$props2 = _this.props,
        disabled = _this$props2.disabled,
        readOnly = _this$props2.readOnly;
      var isPrinting = _this.state.isPrinting;
      return !isPrinting && !!_this._editorView && !readOnly && !disabled;
    });
    _defineProperty(_this, "_onPrintStart", function () {
      _this.setState({
        isPrinting: true
      });
    });
    _defineProperty(_this, "_onPrintEnd", function () {
      _this.setState({
        isPrinting: false
      });
    });
    return _this;
  }
  _inherits(Editor, _React$PureComponent);
  return _createClass(Editor, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props3 = this.props,
        onReady = _this$props3.onReady,
        editorState = _this$props3.editorState,
        readOnly = _this$props3.readOnly,
        runtime = _this$props3.runtime,
        placeholder = _this$props3.placeholder,
        disabled = _this$props3.disabled,
        dispatchTransaction = _this$props3.dispatchTransaction,
        nodeViews = _this$props3.nodeViews,
        transformPastedHTML = _this$props3.transformPastedHTML;
      var editorNode = document.getElementById(this._id);
      if (editorNode) {
        var effectiveNodeViews = Object.assign({}, DEFAULT_NODE_VIEWS, nodeViews);
        var boundNodeViews = {};
        var schema = getSchema(editorState);
        var nodes = schema.nodes;
        Object.keys(effectiveNodeViews).forEach(function (nodeName) {
          var nodeView = effectiveNodeViews[nodeName];
          // Only valid and supported node views should be used.
          if (nodes[nodeName]) {
            boundNodeViews[nodeName] = bindNodeView(nodeView);
          }
        });

        // Reference: http://prosemirror.net/examples/basic/
        var _view = this._editorView = new CustomEditorView(editorNode, {
          dispatchTransaction: dispatchTransaction,
          editable: this._isEditable,
          nodeViews: boundNodeViews,
          state: editorState || EDITOR_EMPTY_STATE,
          transformPastedHTML: transformPastedHTML,
          handleDOMEvents: handleDOMEvents
        });
        _view.runtime = runtime;
        _view.placeholder = placeholder;
        _view.readOnly = !!readOnly;
        _view.disabled = !!disabled;
        _view.updateState(editorState || EDITOR_EMPTY_STATE);

        // Expose the view to CZIProseMirror so developer could debug it.
        registerEditorView(this._id, _view);
        this.onEditorReady(_view, onReady);
      }
      window.addEventListener('beforeprint', this._onPrintStart, false);
      window.addEventListener('afterprint', this._onPrintEnd, false);
    }
  }, {
    key: "onEditorReady",
    value: function onEditorReady(view, onReady) {
      onReady && onReady(view);
      this._autoFocusTimer && clearTimeout(this._autoFocusTimer);
      this._autoFocusTimer = this.props.autoFocus ? setTimeout(this.focus, AUTO_FOCUS_DELAY) : 0;
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var view = this._editorView;
      if (view) {
        var prevSchema = getSchema(prevProps.editorState);
        var currSchema = getSchema(this.props.editorState);
        if (prevSchema !== currSchema) {
          // schema should never change.
          // TODO: re-create the editor view if schema changed.
          console.error('editor schema changed.');
        }
        var _this$props4 = this.props,
          runtime = _this$props4.runtime,
          editorState = _this$props4.editorState,
          placeholder = _this$props4.placeholder,
          readOnly = _this$props4.readOnly,
          disabled = _this$props4.disabled;
        var isPrinting = this.state.isPrinting;
        var _state = editorState || EDITOR_EMPTY_STATE;
        view.runtime = runtime;
        view.placeholder = placeholder;
        view.readOnly = !!readOnly || isPrinting;
        view.disabled = !!disabled;
        view.updateState(_state);
        this._autoFocusTimer && clearTimeout(this._autoFocusTimer);
        this._autoFocusTimer = !prevProps.autoFocus && this.props.autoFocus ? setTimeout(this.focus, AUTO_FOCUS_DELAY) : 0;
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this._autoFocusTimer && clearTimeout(this._autoFocusTimer);
      this._editorView && this._editorView.destroy();
      this._editorView = null;
      releaseEditorView(this._id);
      window.removeEventListener('beforeprint', this._onPrintStart, false);
      window.removeEventListener('afterprint', this._onPrintEnd, false);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props5 = this.props,
        embedded = _this$props5.embedded,
        readOnly = _this$props5.readOnly;
      var className = cx('prosemirror-editor-wrapper', {
        embedded: embedded,
        readOnly: readOnly
      });
      return /*#__PURE__*/React.createElement("div", {
        className: className,
        "data-czi-prosemirror-editor-id": this._id,
        id: this._id,
        onBlur: this._onBlur
      });
    }
  }]);
}(React.PureComponent);
_defineProperty(Editor, "EDITOR_EMPTY_STATE", EDITOR_EMPTY_STATE);
_defineProperty(Editor, "defaultProps", {
  transformPastedHTML: normalizeHTML
});
export default Editor;
import { bpfrpt_proptype_EditorRuntime } from "../Types.js";
import PropTypes from "prop-types";
export { bpfrpt_proptype_EditorProps };