debian-mirror-gitlab/app/assets/javascripts/snippets/components/snippet_blob_view.vue

110 lines
2.7 KiB
Vue
Raw Normal View History

2020-03-13 15:44:24 +05:30
<script>
2021-01-29 00:20:46 +05:30
import GetBlobContent from 'shared_queries/snippet/snippet_blob_content.query.graphql';
2020-03-13 15:44:24 +05:30
import BlobContent from '~/blob/components/blob_content.vue';
2021-03-11 19:13:27 +05:30
import BlobHeader from '~/blob/components/blob_header.vue';
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
import {
SIMPLE_BLOB_VIEWER,
RICH_BLOB_VIEWER,
BLOB_RENDER_EVENT_LOAD,
BLOB_RENDER_EVENT_SHOW_SOURCE,
} from '~/blob/components/constants';
2020-03-13 15:44:24 +05:30
export default {
components: {
BlobHeader,
BlobContent,
},
apollo: {
blobContent: {
query: GetBlobContent,
variables() {
return {
2021-01-29 00:20:46 +05:30
ids: [this.snippet.id],
2020-03-13 15:44:24 +05:30
rich: this.activeViewerType === RICH_BLOB_VIEWER,
2021-01-03 14:25:43 +05:30
paths: [this.blob.path],
2020-03-13 15:44:24 +05:30
};
},
2020-10-24 23:57:45 +05:30
update(data) {
return this.onContentUpdate(data);
},
2020-05-24 23:13:21 +05:30
skip() {
return this.viewer.renderError;
},
2020-03-13 15:44:24 +05:30
},
},
2021-03-08 18:12:59 +05:30
provide() {
return {
blobHash: Math.random().toString().split('.')[1],
};
},
2020-03-13 15:44:24 +05:30
props: {
snippet: {
type: Object,
required: true,
},
2020-07-28 23:09:34 +05:30
blob: {
type: Object,
required: true,
},
2020-03-13 15:44:24 +05:30
},
data() {
return {
blobContent: '',
2020-04-22 19:07:51 +05:30
activeViewerType:
2020-07-28 23:09:34 +05:30
this.blob?.richViewer && !window.location.hash ? RICH_BLOB_VIEWER : SIMPLE_BLOB_VIEWER,
2020-03-13 15:44:24 +05:30
};
},
computed: {
isContentLoading() {
return this.$apollo.queries.blobContent.loading;
},
viewer() {
const { richViewer, simpleViewer } = this.blob;
return this.activeViewerType === RICH_BLOB_VIEWER ? richViewer : simpleViewer;
},
2020-06-23 00:09:42 +05:30
hasRenderError() {
return Boolean(this.viewer.renderError);
},
2020-03-13 15:44:24 +05:30
},
methods: {
2020-04-22 19:07:51 +05:30
switchViewer(newViewer) {
2020-05-24 23:13:21 +05:30
this.activeViewerType = newViewer || SIMPLE_BLOB_VIEWER;
},
forceQuery() {
this.$apollo.queries.blobContent.skip = false;
this.$apollo.queries.blobContent.refetch();
2020-03-13 15:44:24 +05:30
},
2020-10-24 23:57:45 +05:30
onContentUpdate(data) {
const { path: blobPath } = this.blob;
2021-01-03 14:25:43 +05:30
const {
blobs: { nodes: dataBlobs },
} = data.snippets.nodes[0];
2021-03-08 18:12:59 +05:30
const updatedBlobData = dataBlobs.find((blob) => blob.path === blobPath);
2020-10-24 23:57:45 +05:30
return updatedBlobData.richData || updatedBlobData.plainData;
},
2020-03-13 15:44:24 +05:30
},
2020-05-24 23:13:21 +05:30
BLOB_RENDER_EVENT_LOAD,
BLOB_RENDER_EVENT_SHOW_SOURCE,
2020-03-13 15:44:24 +05:30
};
</script>
<template>
2022-01-26 12:08:38 +05:30
<figure class="file-holder snippet-file-content" :aria-label="__('Code snippet')">
2020-07-28 23:09:34 +05:30
<blob-header
:blob="blob"
:active-viewer-type="viewer.type"
:has-render-error="hasRenderError"
@viewer-changed="switchViewer"
2020-10-24 23:57:45 +05:30
/>
2020-07-28 23:09:34 +05:30
<blob-content
:loading="isContentLoading"
:content="blobContent"
:active-viewer="viewer"
:blob="blob"
@[$options.BLOB_RENDER_EVENT_LOAD]="forceQuery"
@[$options.BLOB_RENDER_EVENT_SHOW_SOURCE]="switchViewer"
/>
2022-01-26 12:08:38 +05:30
</figure>
2020-03-13 15:44:24 +05:30
</template>