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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
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(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
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 { EditorState, TextSelection, Plugin } from 'prosemirror-state';
import { Node, Schema } from 'prosemirror-model';
import { Transform } from 'prosemirror-transform';
import { EditorView } from 'prosemirror-view';
import * as React from 'react';
import { stringify } from 'flatted';
import convertFromJSON from '../convertFromJSON.js';
import RichTextEditor from '../ui/RichTextEditor.js';
import uuid from '../uuid.js';
import SimpleConnector from './SimpleConnector.js';
import CollabConnector from './CollabConnector.js';
import { EMPTY_DOC_JSON } from '../createEmptyEditorState.js';
import { createPopUp, atViewportCenter } from '@modusoperandi/licit-ui-commands';
import AlertInfo from '../ui/AlertInfo.js';
import { SetDocAttrStep } from '@modusoperandi/licit-doc-attrs-step';
import DefaultEditorPlugins from '../buildEditorPlugins.js';
import EditorMarks from '../EditorMarks.js';
import EditorNodes from '../EditorNodes.js';
import convertFromHTML from '../convertFromHTML.js';
import DocLayoutCommand from '../DocLayoutCommand.js';
export var DataType = Object.freeze({
  JSON: Symbol('json'),
  HTML: Symbol('html')
});

/**
 * LICIT properties:
 *  docID {string} [] Collaborative Doument ID
 *  collabServiceURL {string} [/collaboration-service] Collaboration Service URL
 *  debug {boolean} [false] To enable/disable ProseMirror Debug Tools, available only in development.
 *  width {string} [100%] Width of the editor.
 *  height {string} [100%] Height of the editor.
 *  readOnly {boolean} [false] To enable/disable editing mode.
 *  onChange {@callback} [null] Fires after each significant change.
 *      @param data {JSON} Modified document data.
 *  onReady {@callback} [null] Fires when the editor is fully ready.
 *      @param ref {LICIT} Rerefence of the editor.
 *  data {JSON|HTML} [null] Document data to be loaded into the editor.
 *  dataType {JSON|HTML} [JSON] Document data to be loaded into the editor.
 *  disabled {boolean} [false] Disable the editor.
 *  embedded {boolean} [false] Disable/Enable inline behaviour.
 *  plugins [plugins] External Plugins into the editor.
 */
var Licit = /*#__PURE__*/function (_React$Component) {
  function Licit(_props, context) {
    var _this;
    _classCallCheck(this, Licit);
    _this = _callSuper(this, Licit, [_props, context]);
    _defineProperty(_this, "_runtime", void 0);
    _defineProperty(_this, "_connector", void 0);
    _defineProperty(_this, "_clientID", void 0);
    _defineProperty(_this, "_editorView", void 0);
    // This will be handy in updating document's content.
    _defineProperty(_this, "_skipSCU", void 0);
    // Flag to decide whether to skip shouldComponentUpdate
    _defineProperty(_this, "_defaultEditorSchema", void 0);
    _defineProperty(_this, "_defaultEditorPlugins", void 0);
    _defineProperty(_this, "_pasteJSONPlugin", void 0);
    _defineProperty(_this, "_popUp", null);
    _defineProperty(_this, "insertJSON", function (json) {
      var _this$_pasteJSONPlugi;
      if ((_this$_pasteJSONPlugi = _this._pasteJSONPlugin) !== null && _this$_pasteJSONPlugi !== void 0 && _this$_pasteJSONPlugi.insert) {
        _this._pasteJSONPlugin.insert(json, _this._editorView);
      }
    });
    _defineProperty(_this, "setContent", function () {
      var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dataType = arguments.length > 1 ? arguments[1] : undefined;
      _this.skipDataTypeCheck = false;
      // [FS] IRAD-1571 2021-09-27
      // dispatch a transaction that MUST start from the views current state;
      var editorState = _this._editorView.state;
      var tr = editorState.tr;
      var document = _this.getDocument(content, editorState, dataType);
      _this.skipDataTypeCheck = true;

      // [FS] IRAD-1593 2021-10-12
      // Reset lastKeyCode since the content is set dynamically and so lastKeyCode is invalid now.
      _this._editorView.lastKeyCode = null;

      // [FS] IRAD-1092 2020-12-03
      // set the value for object metadata  and objectId
      // Should update all document attributes.
      Object.keys(document.attrs).forEach(function (attr) {
        tr = tr.step(new SetDocAttrStep(attr, document.attrs[attr]));
      });
      _this._skipSCU = true;
      _this._editorView.dispatch(tr);
    });
    _defineProperty(_this, "_onChange", function (data) {
      var transaction = data.transaction;

      /*
       ** ProseMirror Debug Tool's Snapshot creates a new state and sets that to editor view's state.
       ** This results in the connector's state as an orphan and thus transaction mismatch error.
       ** To resolve check and update the connector's state to keep in sync.
       */

      if (_this._editorView) {
        var isSameState = _this._connector._editorState == _this._editorView.state;
        var invokeOnEdit = false;
        if (!isSameState) {
          _this._connector._editorState = _this._editorView.state;
          invokeOnEdit = true;
        } else {
          // [FS] IRAD-1264 2021-03-19
          // check if in non-collab mode.
          if (!(_this._connector instanceof CollabConnector)) {
            invokeOnEdit = true;
          }
        }
        if (invokeOnEdit) {
          // [FS] IRAD-1236 2020-03-05
          // Only need to call if there is any difference in collab mode OR always in non-collab mode.
          _this._connector.onEdit(transaction, _this._editorView);
        }
        if (transaction.docChanged || !isSameState && !transaction.getMeta('suppressOnChange')) {
          var _this$_editorView, _docJson$attrs;
          var docJson = (_this$_editorView = _this._editorView) === null || _this$_editorView === void 0 || (_this$_editorView = _this$_editorView.state) === null || _this$_editorView === void 0 || (_this$_editorView = _this$_editorView.doc) === null || _this$_editorView === void 0 ? void 0 : _this$_editorView.toJSON();
          if (!Object.isFrozen(docJson === null || docJson === void 0 || (_docJson$attrs = docJson.attrs) === null || _docJson$attrs === void 0 ? void 0 : _docJson$attrs.objectMetaData)) {
            docJson.attrs.objectMetaData = _objectSpread(_objectSpread({}, docJson.attrs.objectMetaData), {}, {
              lastEditedOn: new Date().toISOString().replace('Z', '+00:00')
            });
          }
          var isEmpty = false;
          if (docJson.content && docJson.content.length === 1) {
            var _docJson$content, _docJson$content2;
            if (!docJson.content[0].content || (_docJson$content = docJson.content) !== null && _docJson$content !== void 0 && (_docJson$content = _docJson$content[0]) !== null && _docJson$content !== void 0 && (_docJson$content = _docJson$content.content) !== null && _docJson$content !== void 0 && (_docJson$content = _docJson$content[0]) !== null && _docJson$content !== void 0 && _docJson$content.text && '' === ((_docJson$content2 = docJson.content) === null || _docJson$content2 === void 0 || (_docJson$content2 = _docJson$content2[0]) === null || _docJson$content2 === void 0 || (_docJson$content2 = _docJson$content2.content) === null || _docJson$content2 === void 0 || (_docJson$content2 = _docJson$content2[0]) === null || _docJson$content2 === void 0 ? void 0 : _docJson$content2.text.trim())) {
              isEmpty = true;
            }
          }

          // setCFlags is/was always the opposite of isEmpty.
          if (isEmpty) {
            _this.resetCounters(transaction);
          } else {
            _this.setCounterFlags(transaction, false);
          }

          // Changing 2nd parameter from boolean to object was not in any way
          // backwards compatible. Any conditional logic placed on isEmpty was
          // broken. Reverting that change, then adding view as a 3rd parameter.
          _this.state.onChangeCB(docJson, isEmpty, _this._editorView);
          _this.closeOpenedPopupModels();
        }
      }
    });
    _defineProperty(_this, "_onReady", function (editorView) {
      // [FS][06-APR-2020][IRAD-922]
      // Showing focus in the editor.
      var state = editorView.state,
        dispatch = editorView.dispatch;
      _this._editorView = editorView;
      var tr = state.tr;
      dispatch(tr.scrollIntoView());
      if (!_this._editorView.editable) {
        var _tr = _this._editorView.state.tr;
        var modified = false;
        _this._editorView.state.doc.descendants(function (node, pos) {
          if (node.type.name === 'table') {
            var _tr2;
            // Apply setNodeMarkup with same type and attrs to force a refresh
            _tr = (_tr2 = _tr) === null || _tr2 === void 0 ? void 0 : _tr2.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
              Id: pos
            }));
            modified = true;
          }
        });
        if (modified) {
          _this._editorView.dispatch(_tr);
        }
      }
      // [FS] IRAD-1578 2021-09-27
      // In collab mode, fire onRead only after getting the response from collab server.
      if (_this.state.onReadyCB && _this.state.docID === '') {
        editorView.focus();
        _this.state.onReadyCB(_this);
      }
    });
    /**
     * LICIT properties:
     *  docID {number} [0] Collaborative Doument ID
     *  debug {boolean} [false] To enable/disable ProseMirror Debug Tools, available only in development.
     *  width {string} [100%] Width of the editor.
     *  height {height} [100%] Height of the editor.
     *  readOnly {boolean} [false] To enable/disable editing mode.
     *  onChange {@callback} [null] Fires after each significant change.
     *      @param data {JSON} Modified document data.
     *  onReady {@callback} [null] Fires when the editor is fully ready.
     *      @param ref {LICIT} Rerefence of the editor.
     *  data {JSON} [null] Document data to be loaded into the editor.
     *  disabled {boolean} [false] Disable the editor.
     *  embedded {boolean} [false] Disable/Enable inline behaviour.
     */
    _defineProperty(_this, "setProps", function (props) {
      if (_this.state.readOnly) {
        // It should be possible to load content into the editor in readonly as well.
        // It should not be necessary to make the component writable any time during the process
        var propsCopy = {};
        _this._skipSCU = true;
        Object.assign(propsCopy, props);
        // make writable without content change
        propsCopy.readOnly = false;
        delete propsCopy.data;
        _this.setState(propsCopy);
      }
      _this.skipDataTypeCheck = false;
      // Need to go through shouldComponentUpdate lifecycle here, when updated from outside,
      // so that content is modified gracefully using transaction so that undo/redo works too.
      _this._skipSCU = false;
      _this.setState(props);
    });
    _defineProperty(_this, "goToEnd", function () {
      // Return focus to the editor with cursor at end of document.
      var view = _this.editorView;
      var tr = view.state.tr;
      view.dispatch(tr.setSelection(TextSelection.atEnd(view.state.doc)).scrollIntoView());
      view.focus();
    });
    /**
     * Method to open page layout
     */
    _defineProperty(_this, "pageLayout", function () {
      var DOC_LAYOUT = new DocLayoutCommand();
      DOC_LAYOUT.waitForUserInput(_this._editorView.state, _this._editorView.dispatch, _this._editorView).then(function (inputs) {
        DOC_LAYOUT.executeWithUserInput(_this._editorView.state, _this._editorView.dispatch, _this._editorView, inputs);
      });
    });
    _this.initialize(_props);
    return _this;
  }
  _inherits(Licit, _React$Component);
  return _createClass(Licit, [{
    key: "editorView",
    get:
    /**
     * Provides access to prosemirror view.
     */
    function get() {
      return this._editorView;
    }
  }, {
    key: "initialize",
    value: function initialize(props) {
      this._clientID = uuid();
      this._editorView = null;
      this._skipSCU = true;
      var noop = function noop() {};

      // [FS] IRAD-981 2020-06-10
      // Component's configurations.
      // [FS] IRAD-1552 2021-08-26
      // Collaboration server / client should allow string values for document identifiers.
      var docID = props.docID || ''; // Empty means collaborative.
      var collaborative = docID !== '';
      // [FS] IRAD-1553 2021-08-26
      // Configurable Collaboration Service URL.
      var collabServiceURL = props.collabServiceURL || '/collaboration-service';
      var debug = props.debug || false;
      // Default width and height to undefined
      var width = props.width || undefined;
      var height = props.height || undefined;
      var onChangeCB = typeof props.onChange === 'function' ? props.onChange : noop;
      var onReadyCB = typeof props.onReady === 'function' ? props.onReady : noop;
      var readOnly = props.readOnly || false;
      var data = props.data || null;
      var dataType = props.dataType || DataType.JSON;
      var disabled = props.disabled || false;
      var embedded = props.embedded || false; // [FS] IRAD-996 2020-06-30
      // [FS] 2020-07-03
      // Handle Image Upload from Angular App
      var runtime = props.runtime || null;
      var plugins = props.plugins || null;
      // This flag decides whether DataType.HTML check is needed when
      // changing document. If it forcefully done, it is not needed, otherwise needed.
      this.skipDataTypeCheck = false;
      this._defaultEditorSchema = new Schema({
        nodes: EditorNodes,
        marks: EditorMarks
      });
      this._defaultEditorPlugins = new DefaultEditorPlugins(this._defaultEditorSchema).get();
      this._pasteJSONPlugin = null;
      var editorState = this.initEditorState(plugins, dataType, data);
      data = editorState.doc;
      var setState = this.setState.bind(this);
      this._connector = collaborative ? new CollabConnector(editorState, setState, {
        docID: docID,
        collabServiceURL: collabServiceURL
      }, this._defaultEditorSchema, this._defaultEditorPlugins,
      // [FS] IRAD-1578 2021-09-27
      this.onReady.bind(this)) : new SimpleConnector(editorState, setState);
      this._connector._dataDefined = !!props.data;

      // FS IRAD-989 2020-18-06
      // updating properties should automatically render the changes

      this.state = {
        docID: docID,
        collabServiceURL: collabServiceURL,
        data: data,
        editorState: editorState,
        width: width,
        height: height,
        readOnly: readOnly,
        onChangeCB: onChangeCB,
        onReadyCB: onReadyCB,
        debug: debug,
        disabled: disabled,
        embedded: embedded,
        runtime: runtime,
        dataType: dataType
      };

      // FS IRAD-1040 2020-26-08
      // Get the modified schema from editorstate and send it to collab server
      if (this._connector.updateSchema) {
        // Use known editorState to update schema.
        this._connector.updateSchema(editorState.schema, data);
      }
    }
  }, {
    key: "initEditorState",
    value: function initEditorState(plugins, dataType, data) {
      var editorState = null;
      var effectivePlugins = this.getEffectivePlugins(this._defaultEditorSchema, this._defaultEditorPlugins, plugins);
      this._pasteJSONPlugin = effectivePlugins.pasteJSONPlugin;
      if (DataType.JSON === dataType) {
        editorState = convertFromJSON(data, null, effectivePlugins.schema, effectivePlugins.plugins);
        // [FS] IRAD-1067 2020-09-19
        // The editorState will return null if the doc Json is mal-formed
        if (null === editorState) {
          editorState = convertFromJSON(EMPTY_DOC_JSON, null, effectivePlugins.schema, effectivePlugins.plugins);
          this.showAlert();
        }
      } else {
        editorState = convertFromHTML(data, effectivePlugins.schema, effectivePlugins.plugins);
      }
      return editorState;
    }
  }, {
    key: "getEffectivePlugins",
    value: function getEffectivePlugins(schema, defaultPlugins, plugins) {
      var effectivePlugins = _toConsumableArray(defaultPlugins);
      var pasteJSONPlugin = null;
      if (plugins) {
        var _iterator = _createForOfIteratorHelper(plugins),
          _step;
        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var p = _step.value;
            if (!effectivePlugins.includes(p)) {
              effectivePlugins.push(p);
              if (p.getEffectiveSchema) {
                schema = p.getEffectiveSchema(schema);
              }
              if (p.initKeyCommands) {
                var keyCommandPlugins = p.initKeyCommands();
                if (Array.isArray(keyCommandPlugins)) {
                  effectivePlugins.push.apply(effectivePlugins, _toConsumableArray(keyCommandPlugins));
                } else if (keyCommandPlugins) {
                  effectivePlugins.push(keyCommandPlugins);
                }
              }
              if (p.insert) {
                pasteJSONPlugin = p;
              }
            }
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      }
      return {
        plugins: effectivePlugins,
        schema: schema,
        pasteJSONPlugin: pasteJSONPlugin
      };
    }

    // [FS] IRAD-1578 2021-09-27
  }, {
    key: "onReady",
    value: function onReady(state) {
      var collabEditing = this.state.docID !== '';
      if (collabEditing) {
        this._editorView && this._editorView.focus();
        if (this.state.onReadyCB) {
          this.state.onReadyCB(this);
        }
      }
    }

    // [FS] IRAD-1067 2020-09-19
    // Alert funtion to show document is corrupted
  }, {
    key: "showAlert",
    value: function showAlert() {
      var _this2 = this;
      var anchor = null;
      this._popUp = createPopUp(AlertInfo, null, {
        anchor: anchor,
        position: atViewportCenter,
        onClose: function onClose(val) {
          if (_this2._popUp) {
            _this2._popUp = null;
          }
        }
      });
    }
  }, {
    key: "resetCounters",
    value: function resetCounters(transaction) {
      for (var index = 1; index <= 10; index++) {
        var counterVar = 'set-cust-style-counter-' + index;
        var setCounterVal = window[counterVar];
        if (setCounterVal) {
          delete window[counterVar];
        }
      }
      this.setCounterFlags(transaction, true);
    }
  }, {
    key: "setCounterFlags",
    value: function setCounterFlags(transaction, reset) {
      var modified = false;
      var counterFlags = null;
      var existingCFlags = transaction.doc.attrs.counterFlags;
      if (reset && !existingCFlags) {
        return;
      }
      for (var index = 1; index <= 10; index++) {
        var counterVar = 'set-cust-style-counter-' + index;
        var setCounterVal = window[counterVar];
        if (setCounterVal) {
          if (!counterFlags) {
            counterFlags = {};
          }
          counterFlags[counterVar] = true;
          if (!existingCFlags) {
            modified = true;
          }
        }
        if (!modified) {
          if (existingCFlags) {
            if (setCounterVal) {
              modified = undefined == existingCFlags[counterVar];
            } else {
              modified = undefined != existingCFlags[counterVar];
            }
          } else {
            modified = setCounterVal;
          }
        }
      }
      if (modified) {
        var tr = this._editorView.state.tr.step(new SetDocAttrStep('counterFlags', counterFlags));
        this._editorView.dispatch(tr);
      }
    }
  }, {
    key: "getDeletedArtifactIds",
    value: function getDeletedArtifactIds() {
      if (this._connector.getDeletedArtifactIds) {
        this._connector.getDeletedArtifactIds(this.state.editorState.schema);
      }
    }
  }, {
    key: "isNodeHasAttribute",
    value: function isNodeHasAttribute(node, attrName) {
      var _node$attrs;
      return (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs[attrName];
    }
  }, {
    key: "getDocument",
    value: function getDocument(content, editorState, dataType) {
      var document = null;
      var schema = editorState.schema;
      if (DataType.JSON === dataType || this.skipDataTypeCheck) {
        document = schema.nodeFromJSON(content || EMPTY_DOC_JSON);
      } else {
        var tempEState = convertFromHTML(content || '', schema, editorState.plugins);
        document = tempEState.doc;
      }
      return document;
    }
  }, {
    key: "hasDataChanged",
    value: function hasDataChanged(nextData, nextDataType) {
      var dataChanged = false;

      // [FS] IRAD-1571 2021-09-27
      // dispatch a transaction that MUST start from the views current state;
      // [FS] IRAD-1589 2021-10-04
      // Do a proper circular JSON comparison.
      if (stringify(this.state.data) !== stringify(nextData)) {
        var editorState = this._editorView.state;
        var nextDoc = this.getDocument(nextData, editorState, nextDataType);
        dataChanged = !nextDoc.eq(editorState.doc);
      }
      return dataChanged;
    }
  }, {
    key: "changeContent",
    value: function changeContent(data, dataType) {
      if (this.hasDataChanged(data, dataType)) {
        // FS IRAD-1592 2021-11-10
        // Release here quickly, so that update doesn't care about at this point.
        // data changed, so update document content
        setTimeout(this.setContent.bind(this, data, dataType), 1);
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      // Only interested if properties are set from outside.
      if (!this._skipSCU) {
        this._skipSCU = false;
        this.changeContent(nextState.data, nextState.dataType);
        if (this.state.docID !== nextState.docID) {
          setTimeout(this.setDocID.bind(this, nextState), 1);
        }
      }
      this.skipDataTypeCheck = true;
      return true;
    }
  }, {
    key: "setDocID",
    value: function setDocID(nextState) {
      // Collaborative mode changed
      var collabEditing = nextState.docID !== '';
      var editorState = this._editorView.state;
      var setState = this.setState.bind(this);
      var docID = nextState.docID || '';
      var collabServiceURL = nextState.collabServiceURL || '/collaboration-service';
      if (this._connector) {
        this._connector.cleanUp();
      }
      // create new connector
      this._connector = collabEditing ? new CollabConnector(editorState, setState, {
        docID: docID,
        collabServiceURL: collabServiceURL
      }, this._defaultEditorSchema, this._defaultEditorPlugins,
      // [FS] IRAD-1578 2021-09-27
      this.onReady.bind(this)) : new SimpleConnector(editorState, setState);

      // FS IRAD-1592 2021-11-10
      // Notify collab server
      if (this._connector.updateSchema) {
        // Use known editorState to update schema.
        this._connector.updateSchema(editorState.schema);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
        editorState = _this$state.editorState,
        width = _this$state.width,
        height = _this$state.height,
        readOnly = _this$state.readOnly,
        disabled = _this$state.disabled,
        embedded = _this$state.embedded,
        runtime = _this$state.runtime;
      // [FS] IRAD-978 2020-06-05
      // Using 100vw & 100vh (100% viewport) is not ideal for a component which is expected to be a part of a page,
      // so changing it to 100%  width & height which will occupy the area relative to its parent.
      return /*#__PURE__*/React.createElement(RichTextEditor, {
        disabled: disabled,
        editorState: editorState,
        embedded: embedded,
        height: height,
        onChange: this._onChange,
        onReady: this._onReady,
        readOnly: readOnly,
        runtime: runtime,
        width: width
      });
    }
  }, {
    key: "closeOpenedPopupModels",
    value:
    // [FS] IRAD-1173 2021-02-25
    // Bug fix: Transaction mismatch error when a dialog is opened and keep typing.
    function closeOpenedPopupModels() {
      var element = document.getElementsByClassName('czi-pop-up-element')[0];
      if (element !== null && element !== void 0 && element.parentElement) {
        element.parentElement.removeChild(element);
      }
    }
  }]);
}(React.Component);
export default Licit;
import { bpfrpt_proptype_EditorRuntime } from "../Types.js";