shortcuts_issuable.js 3.33 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, max-len, no-var, one-var, no-restricted-syntax, vars-on-top, no-use-before-define, no-param-reassign, new-cap, no-underscore-dangle, wrap-iife, one-var-declaration-per-line, quotes, prefer-arrow-callback, consistent-return, prefer-template, no-mixed-operators */
2 3 4
/* global Mousetrap */
/* global ShortcutsNavigation */
/* global sidebar */
Fatih Acet's avatar
Fatih Acet committed
5

6 7
require('mousetrap');
require('./shortcuts_navigation');
Fatih Acet's avatar
Fatih Acet committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

(function() {
  var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    hasProp = {}.hasOwnProperty;

  this.ShortcutsIssuable = (function(superClass) {
    extend(ShortcutsIssuable, superClass);

    function ShortcutsIssuable(isMergeRequest) {
      ShortcutsIssuable.__super__.constructor.call(this);
      Mousetrap.bind('a', this.openSidebarDropdown.bind(this, 'assignee'));
      Mousetrap.bind('m', this.openSidebarDropdown.bind(this, 'milestone'));
      Mousetrap.bind('r', (function(_this) {
        return function() {
          _this.replyWithSelectedText();
          return false;
        };
      })(this));
      Mousetrap.bind('e', (function(_this) {
        return function() {
          _this.editIssue();
          return false;
        };
      })(this));
      Mousetrap.bind('l', this.openSidebarDropdown.bind(this, 'labels'));
      if (isMergeRequest) {
        this.enabledHelp.push('.hidden-shortcut.merge_requests');
      } else {
        this.enabledHelp.push('.hidden-shortcut.issues');
      }
    }

    ShortcutsIssuable.prototype.replyWithSelectedText = function() {
41 42
      var quote, documentFragment, selected, separator;
      var replyField = $('.js-main-target-form #note_note');
43

Douwe Maan's avatar
Douwe Maan committed
44
      documentFragment = window.gl.utils.getSelectedFragment();
45 46 47 48
      if (!documentFragment) {
        replyField.focus();
        return;
      }
49

50 51 52
      // If the documentFragment contains more than just Markdown, don't copy as GFM.
      if (documentFragment.querySelector('.md, .wiki')) return;

53
      selected = window.gl.CopyAsGFM.nodeToGFM(documentFragment);
54 55 56

      if (selected.trim() === "") {
        return;
Fatih Acet's avatar
Fatih Acet committed
57
      }
58
      quote = _.map(selected.split("\n"), function(val) {
59
        return ("> " + val).trim() + "\n";
60 61
      });
      // If replyField already has some content, add a newline before our quote
62
      separator = replyField.val().trim() !== "" && "\n\n" || '';
63 64 65
      replyField.val(function(_, current) {
        return current + separator + quote.join('') + "\n";
      });
Douwe Maan's avatar
Douwe Maan committed
66

67
      // Trigger autosave
68
      replyField.trigger('input');
69 70 71 72 73 74

      // Trigger autosize
      var event = document.createEvent('Event');
      event.initEvent('autosize:update', true, false);
      replyField.get(0).dispatchEvent(event);

75 76
      // Focus the input field
      return replyField.focus();
Fatih Acet's avatar
Fatih Acet committed
77 78 79 80 81
    };

    ShortcutsIssuable.prototype.editIssue = function() {
      var $editBtn;
      $editBtn = $('.issuable-edit');
Bryce Johnson's avatar
Bryce Johnson committed
82
      return gl.utils.visitUrl($editBtn.attr('href'));
Fatih Acet's avatar
Fatih Acet committed
83 84 85 86 87 88 89 90 91 92
    };

    ShortcutsIssuable.prototype.openSidebarDropdown = function(name) {
      sidebar.openDropdown(name);
      return false;
    };

    return ShortcutsIssuable;
  })(ShortcutsNavigation);
}).call(this);