"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.stateKey = exports.plugin = exports["default"] = exports.ReactNodeViewState = void 0;

var _prosemirrorState = require("prosemirror-state");

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, 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; }

var ReactNodeViewState = /*#__PURE__*/function () {
  function ReactNodeViewState() {
    _classCallCheck(this, ReactNodeViewState);

    this.changeHandlers = [];
    this.changeHandlers = [];
  }

  _createClass(ReactNodeViewState, [{
    key: "subscribe",
    value: function subscribe(cb) {
      this.changeHandlers.push(cb);
    }
  }, {
    key: "unsubscribe",
    value: function unsubscribe(cb) {
      this.changeHandlers = this.changeHandlers.filter(function (ch) {
        return ch !== cb;
      });
    }
  }, {
    key: "notifyNewSelection",
    value: function notifyNewSelection(fromPos, toPos) {
      this.changeHandlers.forEach(function (cb) {
        return cb(fromPos, toPos);
      });
    }
  }]);

  return ReactNodeViewState;
}();

exports.ReactNodeViewState = ReactNodeViewState;
var stateKey = new _prosemirrorState.PluginKey('reactNodeView');
exports.stateKey = stateKey;
var plugin = new _prosemirrorState.Plugin({
  state: {
    init: function init() {
      return new ReactNodeViewState();
    },
    apply: function apply(_tr, pluginState) {
      return pluginState;
    }
  },
  key: stateKey,
  view: function view(_view) {
    var pluginState = stateKey.getState(_view.state);
    return {
      update: function update(view) {
        var _view$state$selection = view.state.selection,
            from = _view$state$selection.from,
            to = _view$state$selection.to;
        pluginState.notifyNewSelection(from, to);
      }
    };
  }
});
exports.plugin = plugin;

var plugins = function plugins() {
  return [plugin];
};

var _default = plugins;
exports["default"] = _default;