summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/source_viewer/components/chunk.vue
blob: 9683288f93788b35c23fd776759a9eea8ab45ca7 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<script>
import { GlIntersectionObserver, GlSafeHtmlDirective } from '@gitlab/ui';
import ChunkLine from './chunk_line.vue';

/*
 * We only highlight the chunk that is currently visible to the user.
 * By making use of the Intersection Observer API we can determine when a chunk becomes visible and highlight it accordingly.
 *
 * Content that is not visible to the user (i.e. not highlighted) do not need to look nice,
 * so by making text transparent and rendering raw (non-highlighted) text,
 * the browser spends less resources on painting content that is not immediately relevant.
 *
 * Why use transparent text as opposed to hiding content entirely?
 * 1. If content is hidden entirely, native find text (⌘ + F) won't work.
 * 2. When URL contains line numbers, the browser needs to be able to jump to the correct line.
 */
export default {
  components: {
    ChunkLine,
    GlIntersectionObserver,
  },
  directives: {
    SafeHtml: GlSafeHtmlDirective,
  },
  props: {
    chunkIndex: {
      type: Number,
      required: false,
      default: 0,
    },
    isHighlighted: {
      type: Boolean,
      required: true,
    },
    content: {
      type: String,
      required: true,
    },
    startingFrom: {
      type: Number,
      required: false,
      default: 0,
    },
    totalLines: {
      type: Number,
      required: false,
      default: 0,
    },
    language: {
      type: String,
      required: false,
      default: null,
    },
    blamePath: {
      type: String,
      required: true,
    },
  },
  computed: {
    lines() {
      return this.content.split('\n');
    },
  },
  methods: {
    handleChunkAppear() {
      if (!this.isHighlighted) {
        this.$emit('appear', this.chunkIndex);
      }
    },
  },
};
</script>
<template>
  <div>
    <gl-intersection-observer @appear="handleChunkAppear">
      <div v-if="isHighlighted">
        <chunk-line
          v-for="(line, index) in lines"
          :key="index"
          :number="startingFrom + index + 1"
          :content="line"
          :language="language"
          :blame-path="blamePath"
        />
      </div>
      <div v-else class="gl-display-flex">
        <div class="gl-display-flex gl-flex-direction-column">
          <a
            v-for="(n, index) in totalLines"
            :id="`L${startingFrom + index + 1}`"
            :key="index"
            class="gl-ml-5 gl-text-transparent"
            :href="`#L${startingFrom + index + 1}`"
            :data-line-number="startingFrom + index + 1"
            data-testid="line-number"
          >
            {{ startingFrom + index + 1 }}
          </a>
        </div>
        <div
          class="gl-white-space-pre-wrap! gl-text-transparent"
          data-testid="content"
          v-text="content"
        ></div>
      </div>
    </gl-intersection-observer>
  </div>
</template>