Commit 5a9adb66 authored by Jacques's avatar Jacques

Fix line selection in the refactored blob viewer

Fixes the line selection on the refactored blob viewer
parent 3d6de92f
<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 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';
...@@ -20,13 +21,22 @@ export default { ...@@ -20,13 +21,22 @@ 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) {
// This line will be removed once we start using highlight.js on the frontend (https://gitlab.com/groups/gitlab-org/-/epics/7146)
new LineHighlighter(); // eslint-disable-line no-new
} else {
const { hash } = window.location; const { hash } = window.location;
if (hash) this.scrollToLine(hash, true); if (hash) this.scrollToLine(hash, true);
}
}, },
methods: { methods: {
scrollToLine(hash, scroll = false) { scrollToLine(hash, scroll = false) {
......
...@@ -2,16 +2,20 @@ import { shallowMount } from '@vue/test-utils'; ...@@ -2,16 +2,20 @@ 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;
const contentMock = `<span id="LC1">First</span>\n<span id="LC2">Second</span>\n<span id="LC3">Third</span>`; const contentMock = `<span id="LC1">First</span>\n<span id="LC2">Second</span>\n<span id="LC3">Third</span>`;
const blobHash = 'foo-bar'; const blobHash = 'foo-bar';
function createComponent(content = contentMock, isRawContent = false) { function createComponent(content = contentMock, isRawContent = false, glFeatures = {}) {
wrapper = shallowMount(SimpleViewer, { wrapper = shallowMount(SimpleViewer, {
provide: { provide: {
blobHash, blobHash,
glFeatures,
}, },
propsData: { propsData: {
content, content,
...@@ -26,6 +30,20 @@ describe('Blob Simple Viewer component', () => { ...@@ -26,6 +30,20 @@ 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