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 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 _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); }
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 _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 _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; }
import { Node } from 'prosemirror-model';
import { Decoration, EditorView } from 'prosemirror-view';
import * as React from 'react';
import ReactDOM from 'react-dom/client';
import SelectionObserver from './SelectionObserver.js';
var bpfrpt_proptype_NodeViewProps = {
  "editorView": function editorView() {
    return (typeof EditorView === "function" ? PropTypes.instanceOf(EditorView).isRequired : PropTypes.any.isRequired).apply(this, arguments);
  },
  "getPos": PropTypes.func.isRequired,
  "node": function node() {
    return (typeof Node === "function" ? PropTypes.instanceOf(Node).isRequired : PropTypes.any.isRequired).apply(this, arguments);
  },
  "selected": PropTypes.bool.isRequired,
  "focused": PropTypes.bool.isRequired
};
// Standard className for selected node.
var SELECTED_NODE_CLASS_NAME = 'ProseMirror-selectednode';
var mountedViews = new Set();
var pendingViews = new Set();
function onMutation(mutations, observer) {
  var root = document.body;
  if (!root) {
    return;
  }
  var mountingViews = [];
  var _iterator = _createForOfIteratorHelper(pendingViews),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var view = _step.value;
      var el = view.dom;
      if (root.contains(el)) {
        pendingViews["delete"](view);
        mountingViews.push(view);
        view.__renderReactComponent();
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  var _iterator2 = _createForOfIteratorHelper(mountedViews),
    _step2;
  try {
    for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
      var _view = _step2.value;
      var _el = _view.dom;
      if (!root.contains(_el)) {
        mountedViews["delete"](_el);
        _view.unmountReactComponent();
      }
    }
  } catch (err) {
    _iterator2.e(err);
  } finally {
    _iterator2.f();
  }
  mountingViews.forEach(function (view) {
    return mountedViews.add(view);
  });
  if (mountedViews.size === 0) {
    observer.disconnect();
  }
}

// Workaround to get in-selection views selected.
// See https://discuss.prosemirror.net/t/copy-selection-issue-with-the-image-node/1673/2;
function onSelection(entries, observer) {
  if (!window.getSelection) {
    console.warn('window.getSelection() is not supported');
    observer.disconnect();
    return;
  }
  var selection = window.getSelection();
  if (!selection.containsNode) {
    console.warn('selection.containsNode() is not supported');
    observer.disconnect();
    return;
  }
  var _iterator3 = _createForOfIteratorHelper(mountedViews),
    _step3;
  try {
    for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
      var view = _step3.value;
      var el = view.dom;
      if (selection.containsNode(el)) {
        view.selectNode();
      } else {
        view.deselectNode();
      }
    }
  } catch (err) {
    _iterator3.e(err);
  } finally {
    _iterator3.f();
  }
  if (mountedViews.size === 0) {
    observer.disconnect();
  }
}
var selectionObserver = new SelectionObserver(onSelection);
var mutationObserver = new MutationObserver(onMutation);

// This implements the `NodeView` interface and renders a Node with a react
// Component.
// https://prosemirror.net/docs/ref/#view.NodeView
// https://github.com/ProseMirror/prosemirror-view/blob/master/src/viewdesc.js#L429
var CustomNodeView = /*#__PURE__*/function () {
  function CustomNodeView(node, editorView, getPos, decorations) {
    _classCallCheck(this, CustomNodeView);
    _defineProperty(this, "dom", void 0);
    _defineProperty(this, "props", void 0);
    _defineProperty(this, "reactRoot", null);
    _defineProperty(this, "_selected", void 0);
    this.props = {
      decorations: decorations,
      editorView: editorView,
      getPos: getPos,
      node: node,
      selected: false,
      focused: false
    };
    pendingViews.add(this);

    // The editor will use this as the node's DOM representation
    var dom = this.createDOMElement();
    this.dom = dom;
    dom.onClick = this._onClick;
    if (pendingViews.size === 1) {
      // [FS] IRAD-1060 2020-09-10
      // Observe the editorview's dom insteadof root document so that
      // if multiple instances of editor in a page shouldn't cross-talk
      mutationObserver.observe(/*document*/editorView.dom, {
        childList: true,
        subtree: true
      });
      selectionObserver.observe(/*document*/editorView.dom);
    }
  }
  return _createClass(CustomNodeView, [{
    key: "update",
    value: function update(node, decorations) {
      this.props = _objectSpread(_objectSpread({}, this.props), {}, {
        node: node
      });
      this.__renderReactComponent();
      return true;
    }
  }, {
    key: "stopEvent",
    value: function stopEvent() {
      return false;
    }

    // Mark this node as being the selected node.
  }, {
    key: "selectNode",
    value: function selectNode() {
      this._selected = true;
      this.dom.classList.add(SELECTED_NODE_CLASS_NAME);
      this.__renderReactComponent();
    }

    // Remove selected node marking from this node.
  }, {
    key: "deselectNode",
    value: function deselectNode() {
      this._selected = false;
      this.dom.classList.remove(SELECTED_NODE_CLASS_NAME);
      this.__renderReactComponent();
    }

    // This should be overwrite by subclass.
  }, {
    key: "createDOMElement",
    value: function createDOMElement() {
      // The editor will use this as the node's DOM representation.
      // return document.createElement('span');
      throw new Error('not implemented');
    }

    // This should be overwrite by subclass.
  }, {
    key: "renderReactComponent",
    value: function renderReactComponent() {
      throw new Error('not implemented');
    }
  }, {
    key: "destroy",
    value: function destroy() {
      // Called when the node view is removed from the editor or the whole
      // editor is destroyed.
      // sub-class may override this method.
      // [FS] IRAD-1555 2021-09-13
      // When destroying the node view, remove from the set.
      // FIX: This solves the image missing issue.
      pendingViews["delete"](this);
      this.unmountReactComponent();
    }
  }, {
    key: "__renderReactComponent",
    value: function __renderReactComponent() {
      var _editorView$state;
      var _this$props = this.props,
        editorView = _this$props.editorView,
        getPos = _this$props.getPos;
      if ((_editorView$state = editorView.state) !== null && _editorView$state !== void 0 && _editorView$state.selection) {
        var from = editorView.state.selection.from;
        var pos = getPos();
        this.props.selected = this._selected;
        this.props.focused = editorView.focused && pos === from;
      } else {
        this.props.selected = false;
        this.props.focused = false;
      }
      if (!this.reactRoot) {
        this.reactRoot = ReactDOM.createRoot(this.dom);
      }
      this.reactRoot.render(this.renderReactComponent());
    }
  }, {
    key: "unmountReactComponent",
    value: function unmountReactComponent() {
      if (this.reactRoot) {
        this.reactRoot.unmount();
        this.reactRoot = null;
      }
    }
  }]);
}();
export default CustomNodeView;
import PropTypes from "prop-types";
export { bpfrpt_proptype_NodeViewProps };