2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-12-19 05:12:32 -05:00
|
|
|
import Mousetrap from 'mousetrap';
|
2017-08-03 16:31:53 -04:00
|
|
|
import _ from 'underscore';
|
2017-12-15 07:57:08 -05:00
|
|
|
import Sidebar from './right_sidebar';
|
2018-01-12 08:18:53 -05:00
|
|
|
import Shortcuts from './shortcuts';
|
2018-03-20 00:01:17 -04:00
|
|
|
import { CopyAsGFM } from './behaviors/markdown/copy_as_gfm';
|
2017-10-10 05:10:11 -04:00
|
|
|
|
2018-01-12 08:18:53 -05:00
|
|
|
export default class ShortcutsIssuable extends Shortcuts {
|
2017-10-10 05:10:11 -04:00
|
|
|
constructor(isMergeRequest) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.$replyField = isMergeRequest ? $('.js-main-target-form #note_note') : $('.js-main-target-form .js-vue-comment-form');
|
|
|
|
|
|
|
|
Mousetrap.bind('a', () => ShortcutsIssuable.openSidebarDropdown('assignee'));
|
|
|
|
Mousetrap.bind('m', () => ShortcutsIssuable.openSidebarDropdown('milestone'));
|
|
|
|
Mousetrap.bind('l', () => ShortcutsIssuable.openSidebarDropdown('labels'));
|
|
|
|
Mousetrap.bind('r', this.replyWithSelectedText.bind(this));
|
2018-02-26 09:12:38 -05:00
|
|
|
Mousetrap.bind('e', ShortcutsIssuable.editIssue);
|
2017-10-10 05:10:11 -04:00
|
|
|
|
|
|
|
if (isMergeRequest) {
|
|
|
|
this.enabledHelp.push('.hidden-shortcut.merge_requests');
|
|
|
|
} else {
|
|
|
|
this.enabledHelp.push('.hidden-shortcut.issues');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-10-10 05:10:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
replyWithSelectedText() {
|
|
|
|
const documentFragment = window.gl.utils.getSelectedFragment();
|
|
|
|
|
|
|
|
if (!documentFragment) {
|
|
|
|
this.$replyField.focus();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-24 03:34:28 -04:00
|
|
|
const el = CopyAsGFM.transformGFMSelection(documentFragment.cloneNode(true));
|
|
|
|
const selected = CopyAsGFM.nodeToGFM(el);
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
if (selected.trim() === '') {
|
2016-07-24 16:45:11 -04:00
|
|
|
return false;
|
2017-10-10 05:10:11 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 08:01:36 -04:00
|
|
|
const quote = _.map(selected.split('\n'), val => `${(`> ${val}`).trim()}\n`);
|
2017-10-10 05:10:11 -04:00
|
|
|
|
|
|
|
// If replyField already has some content, add a newline before our quote
|
|
|
|
const separator = (this.$replyField.val().trim() !== '' && '\n\n') || '';
|
|
|
|
this.$replyField.val((a, current) => `${current}${separator}${quote.join('')}\n`)
|
|
|
|
.trigger('input')
|
|
|
|
.trigger('change');
|
|
|
|
|
|
|
|
// Trigger autosize
|
|
|
|
const event = document.createEvent('Event');
|
|
|
|
event.initEvent('autosize:update', true, false);
|
|
|
|
this.$replyField.get(0).dispatchEvent(event);
|
|
|
|
|
|
|
|
// Focus the input field
|
|
|
|
this.$replyField.focus();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 09:12:38 -05:00
|
|
|
static editIssue() {
|
2017-10-10 05:10:11 -04:00
|
|
|
// Need to click the element as on issues, editing is inline
|
|
|
|
// on merge request, editing is on a different page
|
2018-02-26 09:12:38 -05:00
|
|
|
document.querySelector('.js-issuable-edit').click();
|
2017-10-10 05:10:11 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-10-10 05:10:11 -04:00
|
|
|
static openSidebarDropdown(name) {
|
2017-12-15 07:57:08 -05:00
|
|
|
Sidebar.instance.openDropdown(name);
|
2017-10-10 05:10:11 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|