diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-27 18:08:39 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-27 18:08:39 +0300 |
commit | 2b1e7f7dac0fa5d7bb3bdf415cec1b3c67ed77b0 (patch) | |
tree | 725ae8200573957bff6fa03aee237f738dadf1d7 /app/assets/javascripts/behaviors | |
parent | eb004dc626d3a1c9497e8b9dc0f3f578afd05fd9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/behaviors')
-rw-r--r-- | app/assets/javascripts/behaviors/copy_to_clipboard.js | 30 | ||||
-rw-r--r-- | app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js | 17 |
2 files changed, 47 insertions, 0 deletions
diff --git a/app/assets/javascripts/behaviors/copy_to_clipboard.js b/app/assets/javascripts/behaviors/copy_to_clipboard.js index 430a8c38387..e822072d669 100644 --- a/app/assets/javascripts/behaviors/copy_to_clipboard.js +++ b/app/assets/javascripts/behaviors/copy_to_clipboard.js @@ -79,3 +79,33 @@ export default function initCopyToClipboard() { clipboardData.setData('text/x-gfm', json.gfm); }); } + +/** + * Programmatically triggers a click event on a + * "copy to clipboard" button, causing its + * contents to be copied. Handles some of the messiniess + * around managing the button's tooltip. + * @param {HTMLElement} btnElement + */ +export function clickCopyToClipboardButton(btnElement) { + const $btnElement = $(btnElement); + + // Ensure the button has already been tooltip'd. + // If the use hasn't yet interacted (i.e. hovered or clicked) + // with the button, Bootstrap hasn't yet initialized + // the tooltip, and its `data-original-title` will be `undefined`. + // This value is used in the functions above. + $btnElement.tooltip(); + btnElement.dispatchEvent(new MouseEvent('mouseover')); + + btnElement.click(); + + // Manually trigger the necessary events to hide the + // button's tooltip and allow the button to perform its + // tooltip cleanup (updating the title from "Copied" back + // to its original title, "Copy branch name"). + setTimeout(() => { + btnElement.dispatchEvent(new MouseEvent('mouseout')); + $btnElement.tooltip('hide'); + }, 2000); +} diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js index f7b327b2af1..5a5a67334d3 100644 --- a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js @@ -4,6 +4,8 @@ import Sidebar from '../../right_sidebar'; import Shortcuts from './shortcuts'; import { CopyAsGFM } from '../markdown/copy_as_gfm'; import { getSelectedFragment } from '~/lib/utils/common_utils'; +import { isElementVisible } from '~/lib/utils/dom_utils'; +import { clickCopyToClipboardButton } from '~/behaviors/copy_to_clipboard'; export default class ShortcutsIssuable extends Shortcuts { constructor() { @@ -14,6 +16,7 @@ export default class ShortcutsIssuable extends Shortcuts { Mousetrap.bind('l', () => ShortcutsIssuable.openSidebarDropdown('labels')); Mousetrap.bind('r', ShortcutsIssuable.replyWithSelectedText); Mousetrap.bind('e', ShortcutsIssuable.editIssue); + Mousetrap.bind('b', ShortcutsIssuable.copyBranchName); } static replyWithSelectedText() { @@ -98,4 +101,18 @@ export default class ShortcutsIssuable extends Shortcuts { Sidebar.instance.openDropdown(name); return false; } + + static copyBranchName() { + // There are two buttons - one that is shown when the sidebar + // is expanded, and one that is shown when it's collapsed. + const allCopyBtns = Array.from(document.querySelectorAll('.sidebar-source-branch button')); + + // 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); + } + } } |