"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.backspace = exports.altPaste = exports.addRowBefore = exports.addRowAfter = exports.addLink = exports.addColumnBefore = exports.addColumnAfter = exports.addAltText = exports.UP = exports.RIGHT = exports.LEFT = exports.KEY_6 = exports.KEY_5 = exports.KEY_4 = exports.KEY_3 = exports.KEY_2 = exports.KEY_1 = exports.KEY_0 = exports.HEADING_KEYS = exports.DOWN = void 0;
exports.bindKeymapWithCommand = bindKeymapWithCommand;
exports.escape = exports.enter = exports.deleteKey = exports.cut = exports.copy = exports.clearFormatting = void 0;
exports.findKeyMapForBrowser = findKeyMapForBrowser;
exports.findKeymapByDescription = findKeymapByDescription;
exports.findShortcutByDescription = findShortcutByDescription;
exports.findShortcutByKeymap = findShortcutByKeymap;
exports.undo = exports.toggleUnderline = exports.toggleTable = exports.toggleStrikethrough = exports.toggleOrderedList = exports.toggleItalic = exports.toggleHeading6 = exports.toggleHeading5 = exports.toggleHeading4 = exports.toggleHeading3 = exports.toggleHeading2 = exports.toggleHeading1 = exports.toggleCode = exports.toggleBulletList = exports.toggleBold = exports.toggleBlockQuote = exports.tab = exports.submit = exports.splitListItem = exports.splitCodeBlock = exports.space = exports.shiftBackspace = exports.setNormalText = exports.redoBarred = exports.redo = exports.previousCell = exports.pastePlainText = exports.paste = exports.outdentList = exports.outdent = exports.openHelp = exports.nextCell = exports.moveUp = exports.moveRight = exports.moveLeft = exports.moveDown = exports.insertRule = exports.insertNewLine = exports.indentList = exports.indent = void 0;

var _browser = _interopRequireDefault(require("../utils/browser"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var addAltText = makeKeyMapWithCommon('Add Alt Text', 'Mod-Alt-y');
exports.addAltText = addAltText;
var toggleBold = makeKeyMapWithCommon('Bold', 'Mod-b');
exports.toggleBold = toggleBold;
var toggleItalic = makeKeyMapWithCommon('Italic', 'Mod-i');
exports.toggleItalic = toggleItalic;
var toggleUnderline = makeKeyMapWithCommon('Underline', 'Mod-u');
exports.toggleUnderline = toggleUnderline;
var toggleStrikethrough = makeKeyMapWithCommon('Strikethrough', 'Mod-Shift-s');
exports.toggleStrikethrough = toggleStrikethrough;
var toggleCode = makeKeyMapWithCommon('Code', 'Mod-Shift-m');
exports.toggleCode = toggleCode;
var pastePlainText = makeKeyMapWithCommon('Paste Plain Text', 'Mod-Shift-v');
exports.pastePlainText = pastePlainText;
var clearFormatting = makeKeyMapWithCommon('Clear formatting', 'Mod-\\');
exports.clearFormatting = clearFormatting;
var setNormalText = makeKeyMapWithCommon('Normal text', 'Mod-Alt-0');
exports.setNormalText = setNormalText;
var toggleHeading1 = makeKeyMapWithCommon('Heading 1', 'Mod-Alt-1');
exports.toggleHeading1 = toggleHeading1;
var toggleHeading2 = makeKeyMapWithCommon('Heading 2', 'Mod-Alt-2');
exports.toggleHeading2 = toggleHeading2;
var toggleHeading3 = makeKeyMapWithCommon('Heading 3', 'Mod-Alt-3');
exports.toggleHeading3 = toggleHeading3;
var toggleHeading4 = makeKeyMapWithCommon('Heading 4', 'Mod-Alt-4');
exports.toggleHeading4 = toggleHeading4;
var toggleHeading5 = makeKeyMapWithCommon('Heading 5', 'Mod-Alt-5');
exports.toggleHeading5 = toggleHeading5;
var toggleHeading6 = makeKeyMapWithCommon('Heading 6', 'Mod-Alt-6');
exports.toggleHeading6 = toggleHeading6;
var toggleOrderedList = makeKeyMapWithCommon('Numbered list', 'Mod-Shift-7');
exports.toggleOrderedList = toggleOrderedList;
var toggleBulletList = makeKeyMapWithCommon('Bullet list', 'Mod-Shift-8');
exports.toggleBulletList = toggleBulletList;
var toggleBlockQuote = makeKeymap('Quote', '', 'Cmd-Alt-9');
exports.toggleBlockQuote = toggleBlockQuote;
var insertNewLine = makeKeyMapWithCommon('Insert new line', 'Shift-Enter');
exports.insertNewLine = insertNewLine;
var shiftBackspace = makeKeyMapWithCommon('Shift Backspace', 'Shift-Backspace');
exports.shiftBackspace = shiftBackspace;
var splitCodeBlock = makeKeyMapWithCommon('Split code block', 'Enter');
exports.splitCodeBlock = splitCodeBlock;
var splitListItem = makeKeyMapWithCommon('Split list item', 'Enter');
exports.splitListItem = splitListItem;
var insertRule = makeKeyMapWithCommon('Insert horizontal rule', 'Mod-Shift--');
exports.insertRule = insertRule;
var undo = makeKeyMapWithCommon('Undo', 'Mod-z');
exports.undo = undo;
var moveUp = makeKeyMapWithCommon('Move up', 'ArrowUp');
exports.moveUp = moveUp;
var moveDown = makeKeyMapWithCommon('Move down', 'ArrowDown');
exports.moveDown = moveDown;
var moveLeft = makeKeyMapWithCommon('Move left', 'ArrowLeft');
exports.moveLeft = moveLeft;
var moveRight = makeKeyMapWithCommon('Move right', 'ArrowRight');
exports.moveRight = moveRight;
var indentList = makeKeyMapWithCommon('Indent List', 'Tab');
exports.indentList = indentList;
var outdentList = makeKeyMapWithCommon('Outdent List', 'Shift-Tab');
exports.outdentList = outdentList;
var redo = makeKeymap('Redo', 'Ctrl-y', 'Cmd-Shift-z');
exports.redo = redo;
var redoBarred = makeKeymap('Redo Barred', 'Ctrl-Shift-z', 'Cmd-y');
exports.redoBarred = redoBarred;
var openHelp = makeKeyMapWithCommon('Open Help', 'Mod-/');
exports.openHelp = openHelp;
var addLink = makeKeyMapWithCommon('Link', 'Mod-k');
exports.addLink = addLink;
var submit = makeKeyMapWithCommon('Submit Content', 'Mod-Enter');
exports.submit = submit;
var enter = makeKeyMapWithCommon('Enter', 'Enter');
exports.enter = enter;
var tab = makeKeyMapWithCommon('Tab', 'Tab');
exports.tab = tab;
var indent = makeKeyMapWithCommon('Indent', 'Tab');
exports.indent = indent;
var outdent = makeKeyMapWithCommon('Outdent', 'Shift-Tab');
exports.outdent = outdent;
var backspace = makeKeyMapWithCommon('Backspace', 'Backspace');
exports.backspace = backspace;
var deleteKey = makeKeyMapWithCommon('Delete', 'Delete');
exports.deleteKey = deleteKey;
var space = makeKeyMapWithCommon('Space', 'Space');
exports.space = space;
var escape = makeKeyMapWithCommon('Escape', 'Escape');
exports.escape = escape;
var nextCell = makeKeyMapWithCommon('Next cell', 'Tab');
exports.nextCell = nextCell;
var previousCell = makeKeyMapWithCommon('Previous cell', 'Shift-Tab');
exports.previousCell = previousCell;
var toggleTable = makeKeyMapWithCommon('Table', 'Shift-Alt-t');
exports.toggleTable = toggleTable;
var addRowBefore = makeKeyMapWithCommon('Add Row Above', 'Ctrl-Alt-ArrowUp');
exports.addRowBefore = addRowBefore;
var addRowAfter = makeKeyMapWithCommon('Add Row Below', 'Ctrl-Alt-ArrowDown');
exports.addRowAfter = addRowAfter;
var addColumnAfter = makeKeyMapWithCommon('Add Column After', 'Ctrl-Alt-ArrowRight');
exports.addColumnAfter = addColumnAfter;
var addColumnBefore = makeKeyMapWithCommon('Add Column Before', 'Ctrl-Alt-ArrowLeft');
exports.addColumnBefore = addColumnBefore;
var cut = makeKeyMapWithCommon('Cut', 'Mod-x');
exports.cut = cut;
var copy = makeKeyMapWithCommon('Copy', 'Mod-c');
exports.copy = copy;
var paste = makeKeyMapWithCommon('Paste', 'Mod-v');
exports.paste = paste;
var altPaste = makeKeyMapWithCommon('Paste', 'Mod-Shift-v');
exports.altPaste = altPaste;
var arrowKeysMap = {
  // for reference: https://wincent.com/wiki/Unicode_representations_of_modifier_keys
  ARROWLEFT: "\u2190",
  ARROWRIGHT: "\u2192",
  ARROWUP: "\u2191",
  ARROWDOWN: "\u2193"
};

function formatShortcut(keymap) {
  var shortcut;

  if (_browser["default"].mac) {
    // for reference: https://wincent.com/wiki/Unicode_representations_of_modifier_keys
    shortcut = keymap.mac.replace(/Cmd/i, "\u2318").replace(/Shift/i, "\u21E7").replace(/Ctrl/i, "\u2303").replace(/Alt/i, "\u2325").replace(/Backspace/i, "\u232B");
  } else {
    shortcut = keymap.windows.replace(/Backspace/i, "\u232B");
  }

  var keys = shortcut.split('-');
  var lastKey = keys[keys.length - 1].toUpperCase();
  keys[keys.length - 1] = arrowKeysMap[lastKey] || lastKey;
  return keys.join(_browser["default"].mac ? '' : '+');
}

function findKeymapByDescription(description) {
  var matches = ALL.filter(function (keymap) {
    return keymap.description.toUpperCase() === description.toUpperCase();
  });
  return matches[0];
}

function findShortcutByDescription(description) {
  var keymap = findKeymapByDescription(description);

  if (keymap) {
    return findShortcutByKeymap(keymap);
  }

  return;
}

function findShortcutByKeymap(keymap) {
  if (_browser["default"].mac) {
    return keymap.mac;
  }

  return keymap.windows;
}

var ALL = [toggleOrderedList, toggleBulletList, toggleBold, toggleItalic, toggleUnderline, toggleStrikethrough, toggleCode, setNormalText, toggleHeading1, toggleHeading2, toggleHeading3, toggleHeading4, toggleHeading5, toggleHeading6, toggleBlockQuote, insertNewLine, insertRule, splitCodeBlock, splitListItem, redo, undo];

function makeKeymap(description, windows, mac, common) {
  return {
    description: description,
    windows: windows,
    mac: mac,
    common: common
  };
}

function makeKeyMapWithCommon(description, common) {
  var windows = common.replace(/Mod/i, 'Ctrl');
  var mac = common.replace(/Mod/i, 'Cmd');
  return makeKeymap(description, windows, mac, common);
}

function bindKeymapWithCommand(shortcut, cmd, keymap) {
  var oldCmd = keymap[shortcut];
  var newCmd = cmd;

  if (keymap[shortcut]) {
    newCmd = function newCmd(state, dispatch, editorView) {
      return oldCmd(state, dispatch) || cmd(state, dispatch, editorView);
    };
  }

  keymap[shortcut] = newCmd;
}

function findKeyMapForBrowser(keyMap) {
  if (keyMap) {
    if (_browser["default"].mac) {
      return keyMap.mac;
    }

    return keyMap.windows;
  }

  return;
}

var LEFT = 37;
exports.LEFT = LEFT;
var RIGHT = 39;
exports.RIGHT = RIGHT;
var UP = 38;
exports.UP = UP;
var DOWN = 40;
exports.DOWN = DOWN;
var KEY_0 = 48;
exports.KEY_0 = KEY_0;
var KEY_1 = 49;
exports.KEY_1 = KEY_1;
var KEY_2 = 50;
exports.KEY_2 = KEY_2;
var KEY_3 = 51;
exports.KEY_3 = KEY_3;
var KEY_4 = 52;
exports.KEY_4 = KEY_4;
var KEY_5 = 53;
exports.KEY_5 = KEY_5;
var KEY_6 = 54;
exports.KEY_6 = KEY_6;
var HEADING_KEYS = [KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6];
exports.HEADING_KEYS = HEADING_KEYS;