debian-mirror-gitlab/app/assets/javascripts/shortcuts_issuable.js

78 lines
2.3 KiB
JavaScript
Raw Normal View History

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