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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
5.5 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';
2021-03-11 19:13:27 +05:30
import { clickCopyToClipboardButton } from '~/behaviors/copy_to_clipboard';
2019-02-15 15:39:39 +05:30
import { getSelectedFragment } from '~/lib/utils/common_utils';
2021-01-29 00:20:46 +05:30
import { isElementVisible } from '~/lib/utils/dom_utils';
2022-01-26 12:08:38 +05:30
import { DEBOUNCE_DROPDOWN_DELAY } from '~/vue_shared/components/sidebar/labels_select_widget/constants';
2022-06-21 17:19:12 +05:30
import Sidebar from '~/right_sidebar';
2021-03-11 19:13:27 +05:30
import { CopyAsGFM } from '../markdown/copy_as_gfm';
2021-04-29 21:17:54 +05:30
import {
keysFor,
ISSUE_MR_CHANGE_ASSIGNEE,
ISSUE_MR_CHANGE_MILESTONE,
ISSUABLE_CHANGE_LABEL,
ISSUABLE_COMMENT_OR_REPLY,
ISSUABLE_EDIT_DESCRIPTION,
MR_COPY_SOURCE_BRANCH_NAME,
} from './keybindings';
2021-03-11 19:13:27 +05:30
import Shortcuts from './shortcuts';
2018-03-17 18:26:18 +05:30
export default class ShortcutsIssuable extends Shortcuts {
2019-12-04 20:38:33 +05:30
constructor() {
2018-03-17 18:26:18 +05:30
super();
2021-04-29 21:17:54 +05:30
Mousetrap.bind(keysFor(ISSUE_MR_CHANGE_ASSIGNEE), () =>
ShortcutsIssuable.openSidebarDropdown('assignee'),
);
Mousetrap.bind(keysFor(ISSUE_MR_CHANGE_MILESTONE), () =>
ShortcutsIssuable.openSidebarDropdown('milestone'),
);
Mousetrap.bind(keysFor(ISSUABLE_CHANGE_LABEL), () =>
ShortcutsIssuable.openSidebarDropdown('labels'),
);
Mousetrap.bind(keysFor(ISSUABLE_COMMENT_OR_REPLY), ShortcutsIssuable.replyWithSelectedText);
Mousetrap.bind(keysFor(ISSUABLE_EDIT_DESCRIPTION), ShortcutsIssuable.editIssue);
Mousetrap.bind(keysFor(MR_COPY_SOURCE_BRANCH_NAME), ShortcutsIssuable.copyBranchName);
2022-06-21 17:19:12 +05:30
/**
* We're attaching a global focus event listener on document for
* every markdown input field.
*/
$(document).on(
'focus',
'.js-vue-markdown-field .js-gfm-input',
ShortcutsIssuable.handleMarkdownFieldFocus,
);
}
/**
* This event handler preserves last focused markdown input field.
* @param {Object} event
*/
static handleMarkdownFieldFocus({ currentTarget }) {
ShortcutsIssuable.$lastFocusedReplyField = $(currentTarget);
2018-03-17 18:26:18 +05:30
}
2018-11-08 19:23:39 +05:30
static replyWithSelectedText() {
2022-06-21 17:19:12 +05:30
let $replyField = $('.js-main-target-form .js-vue-comment-form');
// Ensure that markdown input is still present in the DOM
// otherwise fall back to main comment input field.
if (
ShortcutsIssuable.$lastFocusedReplyField &&
isElementVisible(ShortcutsIssuable.$lastFocusedReplyField?.get(0))
) {
$replyField = ShortcutsIssuable.$lastFocusedReplyField;
}
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
if (!$replyField.length || $replyField.is(':hidden') /* Other tab selected in MR */) {
2018-11-08 19:23:39 +05:30
return false;
}
2019-02-15 15:39:39 +05:30
const documentFragment = getSelectedFragment(document.querySelector('#content-body'));
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
}
2019-02-15 15:39:39 +05:30
// Sanity check: Make sure the selected text comes from a discussion : it can either contain a message...
2019-09-04 21:01:54 +05:30
let foundMessage = Boolean(documentFragment.querySelector('.md'));
2019-02-15 15:39:39 +05:30
// ... Or come from a message
if (!foundMessage) {
if (documentFragment.originalNodes) {
2021-03-08 18:12:59 +05:30
documentFragment.originalNodes.forEach((e) => {
2019-02-15 15:39:39 +05:30
let node = e;
do {
// Text nodes don't define the `matches` method
2019-07-07 11:18:12 +05:30
if (node.matches && node.matches('.md')) {
2019-02-15 15:39:39 +05:30
foundMessage = true;
}
node = node.parentNode;
} while (node && !foundMessage);
});
}
// If there is no message, just select the reply field
if (!foundMessage) {
$replyField.focus();
return false;
}
}
2018-03-17 18:26:18 +05:30
const el = CopyAsGFM.transformGFMSelection(documentFragment.cloneNode(true));
2019-03-02 22:35:43 +05:30
const blockquoteEl = document.createElement('blockquote');
blockquoteEl.appendChild(el);
CopyAsGFM.nodeToGFM(blockquoteEl)
2021-03-08 18:12:59 +05:30
.then((text) => {
2019-03-02 22:35:43 +05:30
if (text.trim() === '') {
return false;
}
// If replyField already has some content, add a newline before our quote
const separator = ($replyField.val().trim() !== '' && '\n\n') || '';
$replyField
.val((a, current) => `${current}${separator}${text}\n\n`)
.trigger('input')
.trigger('change');
// Trigger autosize
const event = document.createEvent('Event');
event.initEvent('autosize:update', true, false);
$replyField.get(0).dispatchEvent(event);
// Focus the input field
$replyField.focus();
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
return false;
})
.catch(() => {});
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);
2022-01-26 12:08:38 +05:30
// Wait for the sidebar to trigger('click') open
// so it doesn't cause our dropdown to close preemptively
setTimeout(() => {
const editBtn =
document.querySelector(`.block.${name} .shortcut-sidebar-dropdown-toggle`) ||
document.querySelector(`.block.${name} .edit-link`);
editBtn.click();
}, DEBOUNCE_DROPDOWN_DELAY);
2018-03-17 18:26:18 +05:30
return false;
}
2021-01-29 00:20:46 +05:30
static copyBranchName() {
// There are two buttons - one that is shown when the sidebar
// is expanded, and one that is shown when it's collapsed.
2021-04-29 21:17:54 +05:30
const allCopyBtns = Array.from(document.querySelectorAll('.js-sidebar-source-branch button'));
2021-01-29 00:20:46 +05:30
// Select whichever button is currently visible so that
// the "Copied" tooltip is shown when a click is simulated.
const visibleBtn = allCopyBtns.find(isElementVisible);
if (visibleBtn) {
clickCopyToClipboardButton(visibleBtn);
}
}
2018-03-17 18:26:18 +05:30
}