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 _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 _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 _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 _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); }
import cx from 'classnames';
import * as React from 'react';
import ReactDOM from 'react-dom';
import { clamp } from '@modusoperandi/licit-ui-commands';
import htmlElementToRect from './htmlElementToRect.js';
import { fromHTMlElement, fromXY, isIntersected } from '@modusoperandi/licit-ui-commands';
import './czi-table-grid-size-editor.css';
var bpfrpt_proptype_TableGridSizeEditorValue = {
  "cols": PropTypes.number.isRequired,
  "rows": PropTypes.number.isRequired
};
var GUTTER_SIZE = 5;
var CELL_SIZE = 16;
// [FS] IRAD-1012 2020-07-14
// Fix: Limited Table Grid size from 20 to 7
var MAX_SIZE = 7;
var GridCell = /*#__PURE__*/function (_React$PureComponent) {
  function GridCell() {
    _classCallCheck(this, GridCell);
    return _callSuper(this, GridCell, arguments);
  }
  _inherits(GridCell, _React$PureComponent);
  return _createClass(GridCell, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
        x = _this$props.x,
        y = _this$props.y,
        selected = _this$props.selected;
      var style = {
        left: x + 'px',
        top: y + 'px',
        width: CELL_SIZE + 'px',
        height: CELL_SIZE + 'px'
      };
      var className = cx('czi-table-grid-size-editor-cell', {
        selected: selected
      });
      return /*#__PURE__*/React.createElement("div", {
        className: className,
        style: style
      });
    }
  }]);
}(React.PureComponent);
var TableGridSizeEditor = /*#__PURE__*/function (_React$PureComponent2) {
  function TableGridSizeEditor() {
    var _this;
    _classCallCheck(this, TableGridSizeEditor);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, TableGridSizeEditor, [].concat(args));
    _defineProperty(_this, "_ex", 0);
    _defineProperty(_this, "_ey", 0);
    _defineProperty(_this, "_mx", 0);
    _defineProperty(_this, "_my", 0);
    _defineProperty(_this, "_rafID", 0);
    _defineProperty(_this, "_ref", null);
    _defineProperty(_this, "_entered", false);
    _defineProperty(_this, "props", void 0);
    _defineProperty(_this, "state", {
      rows: 1,
      cols: 1
    });
    _defineProperty(_this, "_onRef", function (ref) {
      _this._ref = ref;
    });
    _defineProperty(_this, "_onMouseEnter", function (e) {
      var node = e.currentTarget;
      if (node instanceof HTMLElement) {
        var rect = fromHTMlElement(node);
        var mx = Math.round(e.clientX);
        var my = Math.round(e.clientY);
        _this._ex = rect.x;
        _this._ey = rect.y;
        _this._mx = mx;
        _this._my = my;
        if (!_this._entered) {
          _this._entered = true;
          document.addEventListener('mousemove', _this._onMouseMove, true);
        }
      }
    });
    _defineProperty(_this, "_onMouseMove", function (e) {
      var el = _this._ref && ReactDOM.findDOMNode(_this._ref);
      var elRect = el ? htmlElementToRect(el) : null;
      var mouseRect = fromXY(e.screenX, e.screenY, 10);
      if (elRect && mouseRect && isIntersected(elRect, mouseRect, 50)) {
        // This prevents `PopUpManager` from collapsing the editor.
        e.preventDefault();
        e.stopImmediatePropagation();
      }
      var mx = Math.round(e.clientX);
      var my = Math.round(e.clientY);
      if (mx !== _this._mx || my !== _this._my) {
        _this._mx = mx;
        _this._my = my;
        _this._rafID && cancelAnimationFrame(_this._rafID);
        _this._rafID = requestAnimationFrame(_this._updateGridSize);
      }
    });
    _defineProperty(_this, "_updateGridSize", function () {
      _this._rafID = 0;
      var mx = _this._mx;
      var my = _this._my;
      var x = mx - _this._ex;
      var y = my - _this._ey;
      var rr = clamp(1, Math.ceil(y / (CELL_SIZE + GUTTER_SIZE)), MAX_SIZE);
      var cc = clamp(1, Math.ceil(x / (CELL_SIZE + GUTTER_SIZE)), MAX_SIZE);
      var _this$state = _this.state,
        rows = _this$state.rows,
        cols = _this$state.cols;
      if (rows !== rr || cols !== cc) {
        _this.setState({
          rows: rr,
          cols: cc
        });
      }
    });
    _defineProperty(_this, "_onMouseDown", function (e) {
      e.preventDefault();
      _this.props.close(_this.state);
    });
    return _this;
  }
  _inherits(TableGridSizeEditor, _React$PureComponent2);
  return _createClass(TableGridSizeEditor, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this._entered) {
        document.removeEventListener('mousemove', this._onMouseMove, true);
      }
      this._rafID && cancelAnimationFrame(this._rafID);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state2 = this.state,
        rows = _this$state2.rows,
        cols = _this$state2.cols;
      var rr = Math.max(5, rows);
      var cc = Math.max(5, cols);
      if (rr === rows) {
        rr = Math.min(MAX_SIZE, rr + 1);
      }
      if (cc === cols) {
        cc = Math.min(MAX_SIZE, cc + 1);
      }
      var cells = [];
      var ii = 0;
      var y = 0;
      var w = 0;
      var h = 0;
      while (ii < rr) {
        y += GUTTER_SIZE;
        var jj = 0;
        var x = 0;
        while (jj < cc) {
          x += GUTTER_SIZE;
          var selected = ii < rows && jj < cols;
          cells.push( /*#__PURE__*/React.createElement(GridCell, {
            key: "".concat(String(ii), "-").concat(String(jj)),
            selected: selected,
            x: x,
            y: y
          }));
          x += CELL_SIZE;
          w = x + GUTTER_SIZE;
          jj++;
        }
        y += CELL_SIZE;
        h = y + GUTTER_SIZE;
        ii++;
      }
      var bodyStyle = {
        width: w + 'px',
        height: h + 'px'
      };
      return /*#__PURE__*/React.createElement("div", {
        className: "czi-table-grid-size-editor",
        ref: this._onRef
      }, /*#__PURE__*/React.createElement("div", {
        className: "czi-table-grid-size-editor-body",
        onMouseDown: this._onMouseDown,
        onMouseEnter: this._onMouseEnter,
        style: bodyStyle
      }, cells), /*#__PURE__*/React.createElement("div", {
        className: "czi-table-grid-size-editor-footer"
      }, rows, " X ", cols));
    }
  }]);
}(React.PureComponent);
export default TableGridSizeEditor;
import PropTypes from "prop-types";
export { bpfrpt_proptype_TableGridSizeEditorValue };