All files / app/assets/javascripts/snippets/components snippet_blob_view.vue

100% Statements 17/17
83.33% Branches 5/6
100% Functions 12/12
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 109 110 111 112 113                      2x                     25x             24x     25x         19x                             19x                   49x     25x 25x     25x         7x     1x 1x     24x     24x 24x 24x                                                    
<script>
import GetBlobContent from 'shared_queries/snippet/snippet_blob_content.query.graphql';
 
import BlobContent from '~/blob/components/blob_content.vue';
import BlobHeader from '~/blob/components/blob_header.vue';
 
import {
  SIMPLE_BLOB_VIEWER,
  RICH_BLOB_VIEWER,
  BLOB_RENDER_EVENT_LOAD,
  BLOB_RENDER_EVENT_SHOW_SOURCE,
} from '~/blob/components/constants';
 
export default {
  components: {
    BlobHeader,
    BlobContent,
  },
  apollo: {
    blobContent: {
      query: GetBlobContent,
      variables() {
        return {
          ids: [this.snippet.id],
          rich: this.activeViewerType === RICH_BLOB_VIEWER,
          paths: [this.blob.path],
        };
      },
      update(data) {
        return this.onContentUpdate(data);
      },
      skip() {
        return this.viewer.renderError;
      },
    },
  },
  provide() {
    return {
      blobHash: Math.random().toString().split('.')[1],
    };
  },
  props: {
    snippet: {
      type: Object,
      required: true,
    },
    blob: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      blobContent: '',
      activeViewerType:
        this.blob?.richViewer && !window.location.hash?.startsWith('#LC')
          ? RICH_BLOB_VIEWER
          : SIMPLE_BLOB_VIEWER,
    };
  },
  computed: {
    isContentLoading() {
      return this.$apollo.queries.blobContent.loading;
    },
    viewer() {
      const { richViewer, simpleViewer } = this.blob;
      return this.activeViewerType === RICH_BLOB_VIEWER ? richViewer : simpleViewer;
    },
    hasRenderError() {
      return Boolean(this.viewer.renderError);
    },
  },
  methods: {
    switchViewer(newViewer) {
      this.activeViewerType = newViewer || SIMPLE_BLOB_VIEWER;
    },
    forceQuery() {
      this.$apollo.queries.blobContent.skip = false;
      this.$apollo.queries.blobContent.refetch();
    },
    onContentUpdate(data) {
      const { path: blobPath } = this.blob;
      const {
        blobs: { nodes: dataBlobs },
      } = data.snippets.nodes[0];
      const updatedBlobData = dataBlobs.find((blob) => blob.path === blobPath);
      return updatedBlobData.richData || updatedBlobData.plainData;
    },
  },
  BLOB_RENDER_EVENT_LOAD,
  BLOB_RENDER_EVENT_SHOW_SOURCE,
};
</script>
<template>
  <figure class="file-holder snippet-file-content" :aria-label="__('Code snippet')">
    <blob-header
      :blob="blob"
      :active-viewer-type="viewer.type"
      :has-render-error="hasRenderError"
      @viewer-changed="switchViewer"
    />
    <blob-content
      is-blame-link-hidden
      :loading="isContentLoading"
      :content="blobContent"
      :active-viewer="viewer"
      :blob="blob"
      @[$options.BLOB_RENDER_EVENT_LOAD]="forceQuery"
      @[$options.BLOB_RENDER_EVENT_SHOW_SOURCE]="switchViewer"
    />
  </figure>
</template>