"use strict";

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

var _prosemirrorCommands = require("prosemirror-commands");

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

var _prosemirrorHistory = require("prosemirror-history");

var _prosemirrorInputrules = require("prosemirror-inputrules");

var mac = typeof navigator != "undefined" ? /Mac/.test(navigator.platform) : false; // :: (Schema, ?Object) → Object
// Inspect the given schema looking for marks and nodes from the
// basic schema, and if found, add key bindings related to them.
// This will add:
//
// * **Mod-b** for toggling [strong](#schema-basic.StrongMark)
// * **Mod-i** for toggling [emphasis](#schema-basic.EmMark)
// * **Mod-`** for toggling [code font](#schema-basic.CodeMark)
// * **Ctrl-Shift-0** for making the current textblock a paragraph
// * **Ctrl-Shift-1** to **Ctrl-Shift-Digit6** for making the current
//   textblock a heading of the corresponding level
// * **Ctrl-Shift-Backslash** to make the current textblock a code block
// * **Ctrl-Shift-8** to wrap the selection in an ordered list
// * **Ctrl-Shift-9** to wrap the selection in a bullet list
// * **Ctrl->** to wrap the selection in a block quote
// * **Enter** to split a non-empty textblock in a list item while at
//   the same time splitting the list item
// * **Mod-Enter** to insert a hard break
// * **Mod-_** to insert a horizontal rule
// * **Backspace** to undo an input rule
// * **Alt-ArrowUp** to `joinUp`
// * **Alt-ArrowDown** to `joinDown`
// * **Mod-BracketLeft** to `lift`
// * **Escape** to `selectParentNode`
//
// You can suppress or map these bindings by passing a `mapKeys`
// argument, which maps key names (say `"Mod-B"` to either `false`, to
// remove the binding, or a new key name string.

function buildKeymap(schema, mapKeys) {
  var keys = {},
      type;

  function bind(key, cmd) {
    if (mapKeys) {
      var mapped = mapKeys[key];
      if (mapped === false) return;
      if (mapped) key = mapped;
    }

    keys[key] = cmd;
  }

  bind("Mod-z", _prosemirrorHistory.undo);
  bind("Shift-Mod-z", _prosemirrorHistory.redo);
  bind("Backspace", _prosemirrorInputrules.undoInputRule);
  if (!mac) bind("Mod-y", _prosemirrorHistory.redo);
  bind("Alt-ArrowUp", _prosemirrorCommands.joinUp);
  bind("Alt-ArrowDown", _prosemirrorCommands.joinDown);
  bind("Mod-BracketLeft", _prosemirrorCommands.lift);
  bind("Escape", _prosemirrorCommands.selectParentNode);

  if (type = schema.marks.strong) {
    bind("Mod-b", (0, _prosemirrorCommands.toggleMark)(type));
    bind("Mod-B", (0, _prosemirrorCommands.toggleMark)(type));
  }

  if (type = schema.marks.em) {
    bind("Mod-i", (0, _prosemirrorCommands.toggleMark)(type));
    bind("Mod-I", (0, _prosemirrorCommands.toggleMark)(type));
  }

  if (type = schema.marks.code) bind("Mod-`", (0, _prosemirrorCommands.toggleMark)(type));
  if (type = schema.nodes.bullet_list) bind("Shift-Ctrl-8", (0, _list.wrapInList)(type));
  if (type = schema.nodes.ordered_list) bind("Shift-Ctrl-9", (0, _list.wrapInList)(type));
  if (type = schema.nodes.blockquote) bind("Ctrl->", (0, _prosemirrorCommands.wrapIn)(type));

  if (type = schema.nodes.hard_break) {
    var br = type,
        cmd = (0, _prosemirrorCommands.chainCommands)(_prosemirrorCommands.exitCode, function (state, dispatch) {
      dispatch === null || dispatch === void 0 ? void 0 : dispatch(state.tr.replaceSelectionWith(br.create()).scrollIntoView());
      return true;
    });
    bind("Mod-Enter", cmd);
    bind("Shift-Enter", cmd);
    if (mac) bind("Ctrl-Enter", cmd);
  }

  if (type = schema.nodes.list_item) {
    bind("Enter", (0, _list.splitListItem)(type));
    bind("Tab", (0, _list.sinkListItem)(type));
    bind("Shift-Tab", (0, _list.liftListItem)(type));
    bind("Mod-[", (0, _list.liftListItem)(type));
    bind("Mod-]", (0, _list.sinkListItem)(type));
  }

  if (type = schema.nodes.paragraph) bind("Shift-Ctrl-0", (0, _prosemirrorCommands.setBlockType)(type));
  if (type = schema.nodes.code_block) bind("Shift-Ctrl-\\", (0, _prosemirrorCommands.setBlockType)(type));
  if (type = schema.nodes.heading) for (var i = 1; i <= 6; i++) {
    bind("Shift-Ctrl-" + i, (0, _prosemirrorCommands.setBlockType)(type, {
      level: i
    }));
  }

  if (type = schema.nodes.horizontal_rule) {
    var hr = type;
    bind("Mod-_", function (state, dispatch) {
      dispatch(state.tr.replaceSelectionWith(hr.create()).scrollIntoView());
      return true;
    });
  }

  return keys;
}