From ddb2dc5e0843f240c607fa61418446bbb621d7fb Mon Sep 17 00:00:00 2001
From: Chaoyue Zhao <czhao@gitlab.com>
Date: Wed, 12 Mar 2025 14:24:54 -0400
Subject: [PATCH] Fix blob overflow menu permalink shortcut does not get
 triggered

This happens when user navigate to the blob page from repository page.
The `Mousetrap.bind()` in mounted() is getting
ovewritten by the shortcuts_blob.js, which we use to trigger the old
permalink button (when flag is off). This adds a feature flag check
in `shortcuts_blob.js.

Additionally, this also adds a new const under keybindings.js that
better reflects the nature of the new permalink (e.g. copy).

This feature was originally added in
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/181901 and is
behind the `blob_overflow_menu` feature flag.
---
 .../behaviors/shortcuts/keybindings.js        |  5 +-
 .../behaviors/shortcuts/shortcuts_blob.js     |  6 ++
 locale/gitlab.pot                             |  3 +
 .../shortcuts/shortcuts_blob_spec.js          | 56 +++++++++++++++++++
 4 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 spec/frontend/behaviors/shortcuts/shortcuts_blob_spec.js

diff --git a/app/assets/javascripts/behaviors/shortcuts/keybindings.js b/app/assets/javascripts/behaviors/shortcuts/keybindings.js
index 10c22609858cb..c7ee7699d9ca5 100644
--- a/app/assets/javascripts/behaviors/shortcuts/keybindings.js
+++ b/app/assets/javascripts/behaviors/shortcuts/keybindings.js
@@ -388,9 +388,12 @@ export const PROJECT_FILES_GO_BACK = {
   defaultKeys: ['esc'],
 };
 
+const { blobOverflowMenu } = gon.features ?? {};
 export const PROJECT_FILES_GO_TO_PERMALINK = {
   id: 'projectFiles.goToFilePermalink',
-  description: __('Go to file permalink (while viewing a file)'),
+  description: blobOverflowMenu
+    ? __('Copy file permalink')
+    : __('Go to file permalink (while viewing a file)'),
   defaultKeys: ['y'],
 };
 
diff --git a/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js b/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js
index 9f8d7272e5c52..13ecfaf1a5493 100644
--- a/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js
+++ b/app/assets/javascripts/behaviors/shortcuts/shortcuts_blob.js
@@ -3,6 +3,12 @@ import { moveToFilePermalink } from '~/blob/utils';
 
 export default class ShortcutsBlob {
   constructor(shortcuts) {
+    const { blobOverflowMenu } = gon.features ?? {};
+    if (blobOverflowMenu) {
+      // TODO: Remove ShortcutsBlob entirely once these feature flags are removed.
+      return;
+    }
+
     shortcuts.add(PROJECT_FILES_GO_TO_PERMALINK, moveToFilePermalink);
   }
 }
diff --git a/locale/gitlab.pot b/locale/gitlab.pot
index ea82ab0801ce1..8b18df7cde607 100644
--- a/locale/gitlab.pot
+++ b/locale/gitlab.pot
@@ -16860,6 +16860,9 @@ msgstr ""
 msgid "Copy file path"
 msgstr ""
 
+msgid "Copy file permalink"
+msgstr ""
+
 msgid "Copy image URL"
 msgstr ""
 
diff --git a/spec/frontend/behaviors/shortcuts/shortcuts_blob_spec.js b/spec/frontend/behaviors/shortcuts/shortcuts_blob_spec.js
new file mode 100644
index 0000000000000..3bb992636ee07
--- /dev/null
+++ b/spec/frontend/behaviors/shortcuts/shortcuts_blob_spec.js
@@ -0,0 +1,56 @@
+import ShortcutsBlob from '~/behaviors/shortcuts/shortcuts_blob';
+import { PROJECT_FILES_GO_TO_PERMALINK } from '~/behaviors/shortcuts/keybindings';
+import { moveToFilePermalink } from '~/blob/utils';
+
+describe('ShortcutsBlob', () => {
+  const shortcuts = {
+    add: jest.fn(),
+  };
+
+  const init = () => {
+    return new ShortcutsBlob(shortcuts);
+  };
+
+  beforeEach(() => {
+    shortcuts.add.mockClear();
+    window.gon = {};
+  });
+
+  describe('constructor', () => {
+    describe('when shortcuts should be added', () => {
+      it('adds the permalink shortcut when gon.features is undefined', () => {
+        init();
+
+        expect(shortcuts.add).toHaveBeenCalledWith(
+          PROJECT_FILES_GO_TO_PERMALINK,
+          moveToFilePermalink,
+        );
+      });
+
+      it('adds shortcuts when blobOverflowMenu is false', () => {
+        window.gon.features = {
+          blobOverflowMenu: false,
+        };
+
+        init();
+
+        expect(shortcuts.add).toHaveBeenCalledWith(
+          PROJECT_FILES_GO_TO_PERMALINK,
+          moveToFilePermalink,
+        );
+      });
+    });
+
+    describe('when shortcuts should not be added', () => {
+      it('does not add shortcuts when blobOverflowMenu is true', () => {
+        window.gon.features = {
+          blobOverflowMenu: true,
+        };
+
+        init();
+
+        expect(shortcuts.add).not.toHaveBeenCalled();
+      });
+    });
+  });
+});
-- 
GitLab