"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableControl = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _core = require("@material-ui/core");

var _icons = require("@material-ui/icons");

var _react = _interopRequireWildcard(require("react"));

var _hooks = require("../../hooks");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const useStyles = (0, _core.makeStyles)(() => {
  return (0, _core.createStyles)({
    textField: {
      marginRight: '2rem'
    },
    checkbox: {
      marginTop: '1rem'
    }
  });
});

const TableControl = ({
  editor,
  buttonProps
}) => {
  const [open, setOpen] = (0, _react.useState)(false);
  const [rows, setRows] = (0, _react.useState)();
  const [cols, setCols] = (0, _react.useState)();
  const [hasHeader, setHasHeader] = (0, _react.useState)(true);
  const classes = useStyles();
  const localization = (0, _hooks.useLocalization)();
  const form = (0, _react.useRef)(null);

  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  const insertTable = () => {
    if (rows && cols) {
      editor.chain().focus().insertTable({
        rows,
        cols,
        withHeaderRow: hasHeader
      }).run();
    }
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
    title: localization.menubar.table
  }, /*#__PURE__*/_react.default.createElement(_core.IconButton, (0, _extends2.default)({
    onClick: handleClickOpen,
    color: editor.isActive('link') ? 'primary' : 'default'
  }, buttonProps), /*#__PURE__*/_react.default.createElement(_icons.TableChartOutlined, null))), /*#__PURE__*/_react.default.createElement(_core.Dialog, {
    open: open,
    onClose: handleClose,
    "aria-labelledby": "table-control-title",
    maxWidth: "sm",
    onExited: () => {
      setRows(undefined);
      setCols(undefined);
      setHasHeader(true);
    }
  }, /*#__PURE__*/_react.default.createElement(_core.DialogTitle, {
    id: "table-control-title"
  }, localization.tableControl.title), /*#__PURE__*/_react.default.createElement(_core.DialogContent, null, /*#__PURE__*/_react.default.createElement("form", {
    ref: form
  }, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_core.TextField, {
    autoFocus: true,
    margin: "dense",
    label: localization.tableControl.rows,
    type: "number",
    required: true,
    InputProps: {
      inputProps: {
        min: 1
      }
    },
    value: rows !== null && rows !== void 0 ? rows : '',
    onChange: ev => setRows(parseInt(ev.target.value, 10)),
    className: classes.textField
  }), /*#__PURE__*/_react.default.createElement(_core.TextField, {
    margin: "dense",
    label: localization.tableControl.cols,
    type: "number",
    required: true,
    InputProps: {
      inputProps: {
        min: 1
      }
    },
    value: cols !== null && cols !== void 0 ? cols : '',
    onChange: ev => setCols(parseInt(ev.target.value, 10))
  })), /*#__PURE__*/_react.default.createElement(_core.FormControlLabel, {
    control: /*#__PURE__*/_react.default.createElement(_core.Checkbox, {
      checked: hasHeader,
      onChange: event => setHasHeader(event.target.checked),
      color: "primary"
    }),
    label: localization.tableControl.hasHeader,
    className: classes.checkbox
  }))), /*#__PURE__*/_react.default.createElement(_core.DialogActions, null, /*#__PURE__*/_react.default.createElement(_core.Button, {
    onClick: handleClose
  }, localization.common.cancel), /*#__PURE__*/_react.default.createElement(_core.Button, {
    type: "button",
    onClick: () => {
      var _form$current;

      if ((_form$current = form.current) !== null && _form$current !== void 0 && _form$current.reportValidity()) {
        handleClose();
        insertTable();
      }
    },
    color: "primary"
  }, localization.tableControl.submit))));
};

exports.TableControl = TableControl;
//# sourceMappingURL=table-control.js.map