var _excluded = ["format", "icon", "title"];

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React from 'react';
import { Button } from './Button';
import { useSlate, Editor } from '../slate';

var isMarkActive = function isMarkActive(editor, format) {
  var marks = Editor.marks(editor);
  return marks ? marks[format] === true : false;
};

var toggleMark = function toggleMark(editor, format) {
  var isActive = isMarkActive(editor, format);

  if (isActive) {
    Editor.removeMark(editor, format);
  } else {
    Editor.addMark(editor, format, true);
  }
};

export var MarkButton = function MarkButton(_ref) {
  var format = _ref.format,
      icon = _ref.icon,
      title = _ref.title,
      btnProps = _objectWithoutProperties(_ref, _excluded);

  var editor = useSlate();
  return /*#__PURE__*/React.createElement(Button, Object.assign({}, btnProps, {
    title: title,
    active: isMarkActive(editor, format),
    onMouseDown: function onMouseDown(event) {
      event.preventDefault();
      toggleMark(editor, format);
    }
  }), icon);
};
MarkButton.toggleMark = toggleMark;