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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.4 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';
2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
import { getBaseURL, joinPaths } from '~/lib/utils/url_utility';
2020-07-28 23:09:34 +05:30
import { sprintf } from '~/locale';
2021-03-11 19:13:27 +05:30
import { SNIPPET_BLOB_CONTENT_FETCH_ERROR } from '~/snippets/constants';
2021-09-30 23:02:18 +05:30
import SourceEditor from '~/vue_shared/components/source_editor.vue';
2020-07-28 23:09:34 +05:30
2020-04-08 14:13:33 +05:30
export default {
components: {
BlobHeaderEdit,
2020-04-22 19:07:51 +05:30
GlLoadingIcon,
2021-09-30 23:02:18 +05:30
SourceEditor,
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
2021-03-08 18:12:59 +05:30
transformResponse: [(f) => f],
2020-11-24 15:15:51 +05:30
})
2021-03-08 18:12:59 +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
})
2021-03-08 18:12:59 +05:30
.catch((e) => this.flashAPIFailure(e));
2020-07-28 23:09:34 +05:30
},
flashAPIFailure(err) {
2022-11-25 23:54:43 +05:30
createAlert({ message: 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-09-30 23:02:18 +05:30
<source-editor
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>