From 7d1f33a8a31978394680d5399804ca101e1d95d6 Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Wed, 17 Aug 2022 14:04:49 -0600 Subject: [PATCH 1/2] Switch 'b' shortcut to not ever focus or click an in-page element Changelog: fixed --- .../behaviors/shortcuts/shortcuts_issuable.js | 28 ++++++++++++------- locale/gitlab.pot | 6 ++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js index 82229b5aa8ff6d..698a1f80187b1d 100644 --- a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js @@ -1,9 +1,11 @@ import $ from 'jquery'; +import ClipboardJS from 'clipboard'; import Mousetrap from 'mousetrap'; -import { clickCopyToClipboardButton } from '~/behaviors/copy_to_clipboard'; import { getSelectedFragment } from '~/lib/utils/common_utils'; import { isElementVisible } from '~/lib/utils/dom_utils'; import { DEBOUNCE_DROPDOWN_DELAY } from '~/vue_shared/components/sidebar/labels_select_widget/constants'; +import toast from '~/vue_shared/plugins/global_toast'; +import { s__ } from '~/locale'; import Sidebar from '~/right_sidebar'; import { CopyAsGFM } from '../markdown/copy_as_gfm'; import { @@ -153,17 +155,23 @@ export default class ShortcutsIssuable extends Shortcuts { 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('.js-source-branch-copy')); + static async copyBranchName() { + const button = document.querySelector('.js-source-branch-copy'); + const branchName = button?.dataset.clipboardText; - // Select whichever button is currently visible so that - // the "Copied" tooltip is shown when a click is simulated. - const visibleBtn = allCopyBtns.find(isElementVisible); + if (branchName) { + const inMemButton = document.createElement('button'); + inMemButton.dataset.clipboardText = branchName; - if (visibleBtn) { - clickCopyToClipboardButton(visibleBtn); + const clip = new ClipboardJS(inMemButton); + clip.on('success', () => { + toast(s__('GlobalShortcuts|Copied source branch name to clipboard.')); + }); + clip.on('error', () => { + toast(s__('GlobalShortcuts|Unable to copy the source branch name at this time.')); + }); + + inMemButton.dispatchEvent(new CustomEvent('click')); } } } diff --git a/locale/gitlab.pot b/locale/gitlab.pot index bed17f1e9fbf73..c11ebb936d3012 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -18046,6 +18046,12 @@ msgstr "" msgid "GlobalSearch|project" msgstr "" +msgid "GlobalShortcuts|Copied source branch name to clipboard." +msgstr "" + +msgid "GlobalShortcuts|Unable to copy the source branch name at this time." +msgstr "" + msgid "Globally-allowed IP ranges" msgstr "" -- GitLab From c4bf67d271762e2034814ff63b602040cc9182a8 Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Fri, 26 Aug 2022 16:55:09 -0600 Subject: [PATCH 2/2] Create the Clipboard instance when the shortcuts class is constructed --- .../behaviors/shortcuts/shortcuts_issuable.js | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js index 698a1f80187b1d..97ba9e15c0fc71 100644 --- a/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js +++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_issuable.js @@ -23,6 +23,15 @@ export default class ShortcutsIssuable extends Shortcuts { constructor() { super(); + this.inMemoryButton = document.createElement('button'); + this.clipboardInstance = new ClipboardJS(this.inMemoryButton); + this.clipboardInstance.on('success', () => { + toast(s__('GlobalShortcuts|Copied source branch name to clipboard.')); + }); + this.clipboardInstance.on('error', () => { + toast(s__('GlobalShortcuts|Unable to copy the source branch name at this time.')); + }); + Mousetrap.bind(keysFor(ISSUE_MR_CHANGE_ASSIGNEE), () => ShortcutsIssuable.openSidebarDropdown('assignee'), ); @@ -34,7 +43,7 @@ export default class ShortcutsIssuable extends Shortcuts { ); 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); + Mousetrap.bind(keysFor(MR_COPY_SOURCE_BRANCH_NAME), () => this.copyBranchName()); /** * We're attaching a global focus event listener on document for @@ -155,23 +164,14 @@ export default class ShortcutsIssuable extends Shortcuts { return false; } - static async copyBranchName() { + async copyBranchName() { const button = document.querySelector('.js-source-branch-copy'); const branchName = button?.dataset.clipboardText; if (branchName) { - const inMemButton = document.createElement('button'); - inMemButton.dataset.clipboardText = branchName; - - const clip = new ClipboardJS(inMemButton); - clip.on('success', () => { - toast(s__('GlobalShortcuts|Copied source branch name to clipboard.')); - }); - clip.on('error', () => { - toast(s__('GlobalShortcuts|Unable to copy the source branch name at this time.')); - }); - - inMemButton.dispatchEvent(new CustomEvent('click')); + this.inMemoryButton.dataset.clipboardText = branchName; + + this.inMemoryButton.dispatchEvent(new CustomEvent('click')); } } } -- GitLab