Commit fd601dc6 authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera

Merge branch 'defect/jump-to-next-overscroll' into 'master'

Scroll exactly to the top of a discussion on the MR Overview tab

See merge request gitlab-org/gitlab!47970
parents 2cfc665e 8d8e8426
...@@ -218,23 +218,36 @@ export const isMetaKey = e => e.metaKey || e.ctrlKey || e.altKey || e.shiftKey; ...@@ -218,23 +218,36 @@ export const isMetaKey = e => e.metaKey || e.ctrlKey || e.altKey || e.shiftKey;
export const isMetaClick = e => e.metaKey || e.ctrlKey || e.which === 2; export const isMetaClick = e => e.metaKey || e.ctrlKey || e.which === 2;
export const contentTop = () => { export const contentTop = () => {
const perfBar = $('#js-peek').outerHeight() || 0; const heightCalculators = [
const mrTabsHeight = $('.merge-request-tabs').outerHeight() || 0; () => $('#js-peek').outerHeight(),
const headerHeight = $('.navbar-gitlab').outerHeight() || 0; () => $('.navbar-gitlab').outerHeight(),
const diffFilesChanged = $('.js-diff-files-changed').outerHeight() || 0; () => $('.merge-request-tabs').outerHeight(),
() => $('.js-diff-files-changed').outerHeight(),
() => {
const isDesktop = breakpointInstance.isDesktop(); const isDesktop = breakpointInstance.isDesktop();
const diffFileTitleBar = const diffsTabIsActive = window.mrTabs?.currentAction === 'diffs';
(isDesktop && $('.diff-file .file-title-flex-parent:visible').outerHeight()) || 0; let size;
const compareVersionsHeaderHeight = (isDesktop && $('.mr-version-controls').outerHeight()) || 0;
return ( if (isDesktop && diffsTabIsActive) {
perfBar + size = $('.diff-file .file-title-flex-parent:visible').outerHeight();
mrTabsHeight + }
headerHeight +
diffFilesChanged + return size;
diffFileTitleBar + },
compareVersionsHeaderHeight () => {
); let size;
if (breakpointInstance.isDesktop()) {
size = $('.mr-version-controls').outerHeight();
}
return size;
},
];
return heightCalculators.reduce((totalHeight, calculator) => {
return totalHeight + (calculator() || 0);
}, 0);
}; };
export const scrollToElement = (element, options = {}) => { export const scrollToElement = (element, options = {}) => {
......
import { mapGetters, mapActions, mapState } from 'vuex'; import { mapGetters, mapActions, mapState } from 'vuex';
import { scrollToElementWithContext } from '~/lib/utils/common_utils'; import { scrollToElementWithContext, scrollToElement } from '~/lib/utils/common_utils';
import eventHub from '../event_hub'; import eventHub from '../event_hub';
/** /**
* @param {string} selector * @param {string} selector
* @returns {boolean} * @returns {boolean}
*/ */
function scrollTo(selector) { function scrollTo(selector, { withoutContext = false } = {}) {
const el = document.querySelector(selector); const el = document.querySelector(selector);
const scrollFunction = withoutContext ? scrollToElement : scrollToElementWithContext;
if (el) { if (el) {
scrollToElementWithContext(el); scrollFunction(el);
return true; return true;
} }
...@@ -35,7 +36,7 @@ function diffsJump({ expandDiscussion }, id) { ...@@ -35,7 +36,7 @@ function diffsJump({ expandDiscussion }, id) {
function discussionJump({ expandDiscussion }, id) { function discussionJump({ expandDiscussion }, id) {
const selector = `div.discussion[data-discussion-id="${id}"]`; const selector = `div.discussion[data-discussion-id="${id}"]`;
expandDiscussion({ discussionId: id }); expandDiscussion({ discussionId: id });
return scrollTo(selector); return scrollTo(selector, { withoutContext: true });
} }
/** /**
......
---
title: Scroll exactly to the top of a discussion on the MR Overview tab
merge_request: 47970
author:
type: fixed
...@@ -220,6 +220,7 @@ describe('common_utils', () => { ...@@ -220,6 +220,7 @@ describe('common_utils', () => {
beforeEach(() => { beforeEach(() => {
elem = document.createElement('div'); elem = document.createElement('div');
window.innerHeight = windowHeight; window.innerHeight = windowHeight;
window.mrTabs = { currentAction: 'show' };
jest.spyOn($.fn, 'animate'); jest.spyOn($.fn, 'animate');
jest.spyOn($.fn, 'offset').mockReturnValue({ top: elemTop }); jest.spyOn($.fn, 'offset').mockReturnValue({ top: elemTop });
}); });
......
...@@ -42,6 +42,7 @@ describe('Discussion navigation mixin', () => { ...@@ -42,6 +42,7 @@ describe('Discussion navigation mixin', () => {
); );
jest.spyOn(utils, 'scrollToElementWithContext'); jest.spyOn(utils, 'scrollToElementWithContext');
jest.spyOn(utils, 'scrollToElement');
expandDiscussion = jest.fn(); expandDiscussion = jest.fn();
const { actions, ...notesRest } = notesModule(); const { actions, ...notesRest } = notesModule();
...@@ -133,7 +134,7 @@ describe('Discussion navigation mixin', () => { ...@@ -133,7 +134,7 @@ describe('Discussion navigation mixin', () => {
}); });
it('scrolls to element', () => { it('scrolls to element', () => {
expect(utils.scrollToElementWithContext).toHaveBeenCalledWith( expect(utils.scrollToElement).toHaveBeenCalledWith(
findDiscussion('div.discussion', expected), findDiscussion('div.discussion', expected),
); );
}); });
...@@ -200,7 +201,7 @@ describe('Discussion navigation mixin', () => { ...@@ -200,7 +201,7 @@ describe('Discussion navigation mixin', () => {
}); });
it('scrolls to discussion', () => { it('scrolls to discussion', () => {
expect(utils.scrollToElementWithContext).toHaveBeenCalledWith( expect(utils.scrollToElement).toHaveBeenCalledWith(
findDiscussion('div.discussion', expected), findDiscussion('div.discussion', expected),
); );
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment