From 46d5d085afbf9d2a5a93eb85a716e54e1c1beb63 Mon Sep 17 00:00:00 2001 From: Samantha Ming Date: Thu, 21 Jan 2021 18:01:01 -0800 Subject: [PATCH 1/2] Append targetPath as a string to skip encording If we used "searchParams.set()", the hash L1, it would be changed to percentage L1. Which triggers the bug. To solve this, we append the targetPath as a string. This will bypass the encoding. --- app/assets/javascripts/pages/projects/project.js | 9 ++++++--- .../296667-fix-branch-change-with-line-selected.yml | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/296667-fix-branch-change-with-line-selected.yml diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index ef6953db83b257..89e8fc91a21527 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -121,21 +121,24 @@ export default class Project { if (shouldVisit) { const selectedUrl = new URL(e.target.href); const loc = window.location.href; + let targetPathStringParam = ''; if (loc.includes('/-/')) { const refs = this.fullData.Branches.concat(this.fullData.Tags); const currentRef = refs.find((ref) => loc.indexOf(ref) > -1); if (currentRef) { const targetPath = loc.split(currentRef)[1].slice(1); - selectedUrl.searchParams.set('path', targetPath); + + targetPathStringParam = `&path=${targetPath}`; + selectedUrl.searchParams.delete('path'); } } // Open in new window if "meta" key is pressed if (e.metaKey) { - window.open(selectedUrl.href, '_blank'); + window.open(selectedUrl.href + targetPathStringParam, '_blank'); } else { - window.location.href = selectedUrl.href; + window.location.href = selectedUrl.href + targetPathStringParam; } } }, diff --git a/changelogs/unreleased/296667-fix-branch-change-with-line-selected.yml b/changelogs/unreleased/296667-fix-branch-change-with-line-selected.yml new file mode 100644 index 00000000000000..f42e04a193c88c --- /dev/null +++ b/changelogs/unreleased/296667-fix-branch-change-with-line-selected.yml @@ -0,0 +1,5 @@ +--- +title: Fix bug branch change with line selected +merge_request: 52285 +author: +type: fixed -- GitLab From 76fb479e26a871ecdb0adf2c7484ed724e604f30 Mon Sep 17 00:00:00 2001 From: Samantha Ming Date: Wed, 27 Jan 2021 20:53:57 -0800 Subject: [PATCH 2/2] Adjust to use window.location.hash to set hash --- app/assets/javascripts/pages/projects/project.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index 89e8fc91a21527..7bb740465c80ae 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -121,24 +121,22 @@ export default class Project { if (shouldVisit) { const selectedUrl = new URL(e.target.href); const loc = window.location.href; - let targetPathStringParam = ''; if (loc.includes('/-/')) { const refs = this.fullData.Branches.concat(this.fullData.Tags); const currentRef = refs.find((ref) => loc.indexOf(ref) > -1); if (currentRef) { - const targetPath = loc.split(currentRef)[1].slice(1); - - targetPathStringParam = `&path=${targetPath}`; - selectedUrl.searchParams.delete('path'); + const targetPath = loc.split(currentRef)[1].slice(1).split('#')[0]; + selectedUrl.searchParams.set('path', targetPath); + selectedUrl.hash = window.location.hash; } } // Open in new window if "meta" key is pressed if (e.metaKey) { - window.open(selectedUrl.href + targetPathStringParam, '_blank'); + window.open(selectedUrl.href, '_blank'); } else { - window.location.href = selectedUrl.href + targetPathStringParam; + window.location.href = selectedUrl.href; } } }, -- GitLab