"use strict";

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

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

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

var _FormatBold = _interopRequireDefault(require("@material-ui/icons/FormatBold"));

var _FormatListBulleted = _interopRequireDefault(require("@material-ui/icons/FormatListBulleted"));

var _FormatListNumbered = _interopRequireDefault(require("@material-ui/icons/FormatListNumbered"));

var _image = _interopRequireDefault(require("./image"));

var _media = _interopRequireDefault(require("./media"));

var _characters = _interopRequireDefault(require("./characters"));

var _FormatItalic = _interopRequireDefault(require("@material-ui/icons/FormatItalic"));

var _math = _interopRequireDefault(require("./math"));

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

var _FormatStrikethrough = _interopRequireDefault(require("@material-ui/icons/FormatStrikethrough"));

var _toolbar = _interopRequireDefault(require("./toolbar"));

var _FormatUnderlined = _interopRequireDefault(require("@material-ui/icons/FormatUnderlined"));

var _compact = _interopRequireDefault(require("lodash/compact"));

var _slateSoftBreak = _interopRequireDefault(require("slate-soft-break"));

var _debug = _interopRequireDefault(require("debug"));

var _list = _interopRequireDefault(require("./list"));

var _table = _interopRequireDefault(require("./table"));

var _respArea = _interopRequireDefault(require("./respArea"));

var _html = _interopRequireDefault(require("./html"));

//import Code from '@material-ui/icons/Code';
var log = (0, _debug["default"])('@pie-lib:editable-html:plugins');

function MarkHotkey(options) {
  var type = options.type,
      key = options.key,
      icon = options.icon,
      tag = options.tag; // Return our "plugin" object, containing the `onKeyDown` handler.

  return {
    toolbar: {
      isMark: true,
      type: type,
      icon: icon,
      onToggle: function onToggle(change) {
        log('[onToggleMark] type: ', type);
        return change.toggleMark(type);
      }
    },
    renderMark: function renderMark(props) {
      if (props.mark.type === type) {
        var K = tag || type;
        return /*#__PURE__*/_react["default"].createElement(K, null, props.children);
      }
    },
    onKeyDown: function onKeyDown(event, change) {
      // Check that the key pressed matches our `key` option.
      if (!event.metaKey || event.key != key) return; // Prevent the default characters from being inserted.

      event.preventDefault(); // Toggle the mark `type`.

      change.toggleMark(type);
      return true;
    }
  };
}

var ALL_PLUGINS = ['bold', // 'code',
'html', 'italic', 'underline', 'strikethrough', 'bulleted-list', 'numbered-list', 'image', 'math', 'languageCharacters', 'table', 'video', 'audio', 'responseArea'];
exports.ALL_PLUGINS = ALL_PLUGINS;
var DEFAULT_PLUGINS = ALL_PLUGINS.filter(function (plug) {
  return plug !== 'responseArea';
});
exports.DEFAULT_PLUGINS = DEFAULT_PLUGINS;

var buildPlugins = function buildPlugins(activePlugins, opts) {
  log('[buildPlugins] opts: ', opts);
  activePlugins = activePlugins || DEFAULT_PLUGINS;

  var addIf = function addIf(key, p) {
    return activePlugins.includes(key) && p;
  };

  var imagePlugin = opts.image && opts.image.onDelete && (0, _image["default"])(opts.image);
  var mathPlugin = (0, _math["default"])(opts.math);
  var respAreaPlugin = opts.responseArea && opts.responseArea.type && (0, _respArea["default"])(opts.responseArea, (0, _compact["default"])([mathPlugin]));
  return (0, _compact["default"])([addIf('table', (0, _table["default"])(opts.table, (0, _compact["default"])([imagePlugin, mathPlugin, respAreaPlugin]))), addIf('bold', MarkHotkey({
    key: 'b',
    type: 'bold',
    icon: /*#__PURE__*/_react["default"].createElement(_FormatBold["default"], null),
    tag: 'strong'
  })), // addIf('code', MarkHotkey({ key: '`', type: 'code', icon: <Code /> })),
  addIf('italic', MarkHotkey({
    key: 'i',
    type: 'italic',
    icon: /*#__PURE__*/_react["default"].createElement(_FormatItalic["default"], null),
    tag: 'em'
  })), addIf('strikethrough', MarkHotkey({
    key: '~',
    type: 'strikethrough',
    icon: /*#__PURE__*/_react["default"].createElement(_FormatStrikethrough["default"], null),
    tag: 'del'
  })), addIf('underline', MarkHotkey({
    key: 'u',
    type: 'underline',
    icon: /*#__PURE__*/_react["default"].createElement(_FormatUnderlined["default"], null),
    tag: 'u'
  })), addIf('image', imagePlugin), addIf('video', (0, _media["default"])('video', opts.media)), addIf('audio', (0, _media["default"])('audio', opts.media)), addIf('math', mathPlugin)].concat((0, _toConsumableArray2["default"])(opts.languageCharacters.map(function (config) {
    return addIf('languageCharacters', (0, _characters["default"])(config));
  })), [addIf('bulleted-list', (0, _list["default"])({
    key: 'l',
    type: 'ul_list',
    icon: /*#__PURE__*/_react["default"].createElement(_FormatListBulleted["default"], null)
  })), addIf('numbered-list', (0, _list["default"])({
    key: 'n',
    type: 'ol_list',
    icon: /*#__PURE__*/_react["default"].createElement(_FormatListNumbered["default"], null)
  })), (0, _toolbar["default"])(opts.toolbar), (0, _slateSoftBreak["default"])({
    shift: true
  }), addIf('responseArea', respAreaPlugin), addIf('html', (0, _html["default"])(opts.html))]));
};

exports.buildPlugins = buildPlugins;
//# sourceMappingURL=index.js.map