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

97 lines
2.3 KiB
Vue
Raw Normal View History

2020-04-08 14:13:33 +05:30
<script>
2020-10-24 23:57:45 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
2020-04-08 14:13:33 +05:30
import BlobHeaderEdit from '~/blob/components/blob_edit_header.vue';
2021-01-03 14:25:43 +05:30
import EditorLite from '~/vue_shared/components/editor_lite.vue';
2020-07-28 23:09:34 +05:30
import { getBaseURL, joinPaths } from '~/lib/utils/url_utility';
import axios from '~/lib/utils/axios_utils';
import { SNIPPET_BLOB_CONTENT_FETCH_ERROR } from '~/snippets/constants';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as Flash } from '~/flash';
2020-07-28 23:09:34 +05:30
import { sprintf } from '~/locale';
2020-04-08 14:13:33 +05:30
export default {
components: {
BlobHeaderEdit,
2020-04-22 19:07:51 +05:30
GlLoadingIcon,
2021-01-03 14:25:43 +05:30
EditorLite,
2020-04-08 14:13:33 +05:30
},
2020-04-22 19:07:51 +05:30
inheritAttrs: false,
2020-04-08 14:13:33 +05:30
props: {
2020-07-28 23:09:34 +05:30
blob: {
type: Object,
2020-10-24 23:57:45 +05:30
required: true,
},
canDelete: {
type: Boolean,
2020-04-22 19:07:51 +05:30
required: false,
2020-10-24 23:57:45 +05:30
default: true,
2020-04-08 14:13:33 +05:30
},
2020-10-24 23:57:45 +05:30
showDelete: {
type: Boolean,
required: false,
2021-01-03 14:25:43 +05:30
default: true,
2020-04-22 19:07:51 +05:30
},
2020-10-24 23:57:45 +05:30
},
computed: {
inputId() {
return `${this.blob.id}_file_path`;
2020-07-28 23:09:34 +05:30
},
},
mounted() {
2020-10-24 23:57:45 +05:30
if (!this.blob.isLoaded) {
2020-07-28 23:09:34 +05:30
this.fetchBlobContent();
}
},
methods: {
2020-10-24 23:57:45 +05:30
onDelete() {
this.$emit('delete');
},
2020-07-28 23:09:34 +05:30
notifyAboutUpdates(args = {}) {
2020-10-24 23:57:45 +05:30
this.$emit('blob-updated', args);
2020-07-28 23:09:34 +05:30
},
fetchBlobContent() {
const baseUrl = getBaseURL();
const url = joinPaths(baseUrl, this.blob.rawPath);
axios
2020-11-24 15:15:51 +05:30
.get(url, {
// This prevents axios from automatically JSON.parse response
transformResponse: [f => f],
})
2020-07-28 23:09:34 +05:30
.then(res => {
2020-10-24 23:57:45 +05:30
this.notifyAboutUpdates({ content: res.data });
2020-07-28 23:09:34 +05:30
})
2020-10-24 23:57:45 +05:30
.catch(e => this.flashAPIFailure(e));
2020-07-28 23:09:34 +05:30
},
flashAPIFailure(err) {
Flash(sprintf(SNIPPET_BLOB_CONTENT_FETCH_ERROR, { err }));
2020-04-08 14:13:33 +05:30
},
},
};
</script>
<template>
2021-01-03 14:25:43 +05:30
<div class="file-holder snippet" data-qa-selector="file_holder_container">
2020-10-24 23:57:45 +05:30
<blob-header-edit
:id="inputId"
:value="blob.path"
data-qa-selector="file_name_field"
:can-delete="canDelete"
:show-delete="showDelete"
@input="notifyAboutUpdates({ path: $event })"
@delete="onDelete"
/>
<gl-loading-icon
v-if="!blob.isLoaded"
:label="__('Loading snippet')"
size="lg"
2020-11-24 15:15:51 +05:30
class="loading-animation prepend-top-20 gl-mb-6"
2020-10-24 23:57:45 +05:30
/>
2021-01-03 14:25:43 +05:30
<editor-lite
2020-10-24 23:57:45 +05:30
v-else
:value="blob.content"
:file-global-id="blob.id"
:file-name="blob.path"
@input="notifyAboutUpdates({ content: $event })"
/>
2020-04-08 14:13:33 +05:30
</div>
</template>