Commit 3df36a8f authored by Jacques Erasmus's avatar Jacques Erasmus Committed by David O'Regan

Fix scroll to line number

Fixes the scroll to line number bug in the blob viewer

Changelog: fixed
parent cbdfced2
...@@ -12,6 +12,7 @@ import { redirectTo } from '~/lib/utils/url_utility'; ...@@ -12,6 +12,7 @@ import { redirectTo } from '~/lib/utils/url_utility';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import WebIdeLink from '~/vue_shared/components/web_ide_link.vue'; import WebIdeLink from '~/vue_shared/components/web_ide_link.vue';
import CodeIntelligence from '~/code_navigation/components/app.vue'; import CodeIntelligence from '~/code_navigation/components/app.vue';
import LineHighlighter from '~/blob/line_highlighter';
import getRefMixin from '../mixins/get_ref'; import getRefMixin from '../mixins/get_ref';
import blobInfoQuery from '../queries/blob_info.query.graphql'; import blobInfoQuery from '../queries/blob_info.query.graphql';
import userInfoQuery from '../queries/user_info.query.graphql'; import userInfoQuery from '../queries/user_info.query.graphql';
...@@ -192,6 +193,7 @@ export default { ...@@ -192,6 +193,7 @@ export default {
window.requestIdleCallback(() => { window.requestIdleCallback(() => {
this.isRenderingLegacyTextViewer = false; this.isRenderingLegacyTextViewer = false;
new LineHighlighter(); // eslint-disable-line no-new
}); });
} else { } else {
this.legacyRichViewer = html; this.legacyRichViewer = html;
......
<script> <script>
import { GlIcon, GlSafeHtmlDirective } from '@gitlab/ui'; import { GlIcon, GlSafeHtmlDirective } from '@gitlab/ui';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import LineHighlighter from '~/blob/line_highlighter';
import { HIGHLIGHT_CLASS_NAME } from './constants'; import { HIGHLIGHT_CLASS_NAME } from './constants';
import ViewerMixin from './mixins'; import ViewerMixin from './mixins';
...@@ -13,7 +11,7 @@ export default { ...@@ -13,7 +11,7 @@ export default {
directives: { directives: {
SafeHtml: GlSafeHtmlDirective, SafeHtml: GlSafeHtmlDirective,
}, },
mixins: [ViewerMixin, glFeatureFlagsMixin()], mixins: [ViewerMixin],
inject: ['blobHash'], inject: ['blobHash'],
data() { data() {
return { return {
...@@ -21,21 +19,14 @@ export default { ...@@ -21,21 +19,14 @@ export default {
}; };
}, },
computed: { computed: {
refactorBlobViewerEnabled() {
return this.glFeatures.refactorBlobViewer;
},
lineNumbers() { lineNumbers() {
return this.content.split('\n').length; return this.content.split('\n').length;
}, },
}, },
mounted() { mounted() {
if (this.refactorBlobViewerEnabled) { const { hash } = window.location;
// This line will be removed once we start using highlight.js on the frontend (https://gitlab.com/groups/gitlab-org/-/epics/7146) if (hash) {
new LineHighlighter(); // eslint-disable-line no-new this.scrollToLine(hash, true);
} else {
const { hash } = window.location;
if (hash) this.scrollToLine(hash, true);
} }
}, },
methods: { methods: {
......
...@@ -25,6 +25,7 @@ import { redirectTo } from '~/lib/utils/url_utility'; ...@@ -25,6 +25,7 @@ import { redirectTo } from '~/lib/utils/url_utility';
import { isLoggedIn } from '~/lib/utils/common_utils'; import { isLoggedIn } from '~/lib/utils/common_utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper'; import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import httpStatusCodes from '~/lib/utils/http_status'; import httpStatusCodes from '~/lib/utils/http_status';
import LineHighlighter from '~/blob/line_highlighter';
import { import {
simpleViewerMock, simpleViewerMock,
richViewerMock, richViewerMock,
...@@ -39,6 +40,7 @@ import { ...@@ -39,6 +40,7 @@ import {
jest.mock('~/repository/components/blob_viewers'); jest.mock('~/repository/components/blob_viewers');
jest.mock('~/lib/utils/url_utility'); jest.mock('~/lib/utils/url_utility');
jest.mock('~/lib/utils/common_utils'); jest.mock('~/lib/utils/common_utils');
jest.mock('~/blob/line_highlighter');
let wrapper; let wrapper;
let mockResolver; let mockResolver;
...@@ -173,20 +175,30 @@ describe('Blob content viewer component', () => { ...@@ -173,20 +175,30 @@ describe('Blob content viewer component', () => {
}); });
describe('legacy viewers', () => { describe('legacy viewers', () => {
const legacyViewerUrl = 'some_file.js?format=json&viewer=simple';
const fileType = 'text';
const highlightJs = false;
it('loads a legacy viewer when a the fileType is text and the highlightJs feature is turned off', async () => { it('loads a legacy viewer when a the fileType is text and the highlightJs feature is turned off', async () => {
await createComponent({ await createComponent({
blob: { ...simpleViewerMock, fileType: 'text', highlightJs: false }, blob: { ...simpleViewerMock, fileType, highlightJs },
}); });
expect(mockAxios.history.get).toHaveLength(1); expect(mockAxios.history.get).toHaveLength(1);
expect(mockAxios.history.get[0].url).toEqual('some_file.js?format=json&viewer=simple'); expect(mockAxios.history.get[0].url).toBe(legacyViewerUrl);
}); });
it('loads a legacy viewer when a viewer component is not available', async () => { it('loads a legacy viewer when a viewer component is not available', async () => {
await createComponent({ blob: { ...simpleViewerMock, fileType: 'unknown' } }); await createComponent({ blob: { ...simpleViewerMock, fileType: 'unknown' } });
expect(mockAxios.history.get).toHaveLength(1); expect(mockAxios.history.get).toHaveLength(1);
expect(mockAxios.history.get[0].url).toEqual('some_file.js?format=json&viewer=simple'); expect(mockAxios.history.get[0].url).toBe(legacyViewerUrl);
});
it('loads the LineHighlighter', async () => {
mockAxios.onGet(legacyViewerUrl).replyOnce(httpStatusCodes.OK, 'test');
await createComponent({ blob: { ...simpleViewerMock, fileType, highlightJs } });
expect(LineHighlighter).toHaveBeenCalled();
}); });
}); });
}); });
......
...@@ -2,9 +2,6 @@ import { shallowMount } from '@vue/test-utils'; ...@@ -2,9 +2,6 @@ import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import { HIGHLIGHT_CLASS_NAME } from '~/vue_shared/components/blob_viewers/constants'; import { HIGHLIGHT_CLASS_NAME } from '~/vue_shared/components/blob_viewers/constants';
import SimpleViewer from '~/vue_shared/components/blob_viewers/simple_viewer.vue'; import SimpleViewer from '~/vue_shared/components/blob_viewers/simple_viewer.vue';
import LineHighlighter from '~/blob/line_highlighter';
jest.mock('~/blob/line_highlighter');
describe('Blob Simple Viewer component', () => { describe('Blob Simple Viewer component', () => {
let wrapper; let wrapper;
...@@ -30,20 +27,6 @@ describe('Blob Simple Viewer component', () => { ...@@ -30,20 +27,6 @@ describe('Blob Simple Viewer component', () => {
wrapper.destroy(); wrapper.destroy();
}); });
describe('refactorBlobViewer feature flag', () => {
it('loads the LineHighlighter if refactorBlobViewer is enabled', () => {
createComponent('', false, { refactorBlobViewer: true });
expect(LineHighlighter).toHaveBeenCalled();
});
it('does not load the LineHighlighter if refactorBlobViewer is disabled', () => {
createComponent('', false, { refactorBlobViewer: false });
expect(LineHighlighter).not.toHaveBeenCalled();
});
});
it('does not fail if content is empty', () => { it('does not fail if content is empty', () => {
const spy = jest.spyOn(window.console, 'error'); const spy = jest.spyOn(window.console, 'error');
createComponent(''); createComponent('');
......
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