"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LinkControl = 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)({
    inNewTab: {
      marginTop: '1rem'
    }
  });
});

const LinkControl = ({
  editor,
  buttonProps
}) => {
  const [open, setOpen] = (0, _react.useState)(false);
  const [link, setLink] = (0, _react.useState)('');
  const [inNewTab, setInNewTab] = (0, _react.useState)(false);
  const classes = useStyles();
  const localization = (0, _hooks.useLocalization)();
  const form = (0, _react.useRef)(null);

  const handleClickOpen = () => {
    if (editor.state.selection.empty) {
      return false;
    }

    setOpen(true);
    const currentLink = editor.getAttributes('link');

    if (currentLink !== null && currentLink !== void 0 && currentLink.href) {
      setLink(currentLink.href);
      setInNewTab(currentLink.target === '_blank');
    }
  };

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

  const addLink = () => {
    if (link) {
      editor.chain().focus().setLink({
        href: link,
        target: inNewTab ? '_blank' : 'self'
      }).run();
    }
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_core.Tooltip, {
    title: localization.menubar.link
  }, /*#__PURE__*/_react.default.createElement(_core.IconButton, (0, _extends2.default)({
    onClick: handleClickOpen,
    color: editor.isActive('link') ? 'primary' : 'default'
  }, buttonProps), /*#__PURE__*/_react.default.createElement(_icons.Link, null))), /*#__PURE__*/_react.default.createElement(_core.Dialog, {
    open: open,
    onClose: handleClose,
    "aria-labelledby": "link-control-title",
    fullWidth: true,
    maxWidth: "sm",
    onExited: () => {
      setLink('');
      setInNewTab(false);
    }
  }, /*#__PURE__*/_react.default.createElement(_core.DialogTitle, {
    id: "link-control-title"
  }, localization.linkControl.title), /*#__PURE__*/_react.default.createElement(_core.DialogContent, null, /*#__PURE__*/_react.default.createElement("form", {
    ref: form
  }, /*#__PURE__*/_react.default.createElement(_core.TextField, {
    autoFocus: true,
    margin: "dense",
    label: localization.linkControl.url,
    type: "url",
    required: true,
    fullWidth: true,
    value: link,
    onChange: ev => setLink(ev.target.value)
  }), /*#__PURE__*/_react.default.createElement(_core.FormControlLabel, {
    control: /*#__PURE__*/_react.default.createElement(_core.Checkbox, {
      checked: inNewTab,
      onChange: event => setInNewTab(event.target.checked),
      color: "primary"
    }),
    label: localization.linkControl.openInNewTab,
    className: classes.inNewTab
  }))), /*#__PURE__*/_react.default.createElement(_core.DialogActions, null, /*#__PURE__*/_react.default.createElement(_core.Button, {
    onClick: handleClose
  }, localization.common.cancel), /*#__PURE__*/_react.default.createElement(_core.Button, {
    onClick: () => {
      var _form$current;

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

exports.LinkControl = LinkControl;
//# sourceMappingURL=link-control.js.map