summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/blob_viewers/simple_viewer.vue
blob: 84770dbac6f54abf5846370ad5d07a8744757445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script>
import { GlIcon } from '@gitlab/ui';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { HIGHLIGHT_CLASS_NAME } from './constants';
import ViewerMixin from './mixins';

export default {
  name: 'SimpleViewer',
  components: {
    GlIcon,
    SourceEditor: () =>
      import(/* webpackChunkName: 'SourceEditor' */ '~/vue_shared/components/source_editor.vue'),
  },
  mixins: [ViewerMixin, glFeatureFlagsMixin()],
  inject: ['blobHash'],
  data() {
    return {
      highlightedLine: null,
    };
  },
  computed: {
    lineNumbers() {
      return this.content.split('\n').length;
    },
    refactorBlobViewerEnabled() {
      return this.glFeatures.refactorBlobViewer;
    },
  },
  mounted() {
    const { hash } = window.location;
    if (hash) this.scrollToLine(hash, true);
  },
  methods: {
    scrollToLine(hash, scroll = false) {
      const lineToHighlight = hash && this.$el.querySelector(hash);
      const currentlyHighlighted = this.highlightedLine;
      if (lineToHighlight) {
        if (currentlyHighlighted) {
          currentlyHighlighted.classList.remove(HIGHLIGHT_CLASS_NAME);
        }

        lineToHighlight.classList.add(HIGHLIGHT_CLASS_NAME);
        this.highlightedLine = lineToHighlight;
        if (scroll) {
          lineToHighlight.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }
      }
    },
  },
  userColorScheme: window.gon.user_color_scheme,
};
</script>
<template>
  <div>
    <source-editor
      v-if="isRawContent && refactorBlobViewerEnabled"
      :value="content"
      :file-name="fileName"
      :editor-options="{ readOnly: true }"
    />
    <div v-else class="file-content code js-syntax-highlight" :class="$options.userColorScheme">
      <div class="line-numbers">
        <a
          v-for="line in lineNumbers"
          :id="`L${line}`"
          :key="line"
          class="diff-line-num js-line-number"
          :href="`#LC${line}`"
          :data-line-number="line"
          @click="scrollToLine(`#LC${line}`)"
        >
          <gl-icon :size="12" name="link" />
          {{ line }}
        </a>
      </div>
      <div class="blob-content">
        <pre
          class="code highlight"
        ><code :data-blob-hash="blobHash" v-html="content /* eslint-disable-line vue/no-v-html */"></code></pre>
      </div>
    </div>
  </div>
</template>