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

108 lines
2.8 KiB
Vue
Raw Normal View History

2020-04-08 14:13:33 +05:30
<script>
import BlobHeaderEdit from '~/blob/components/blob_edit_header.vue';
import BlobContentEdit from '~/blob/components/blob_edit_content.vue';
2020-04-22 19:07:51 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
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';
import Flash from '~/flash';
import { sprintf } from '~/locale';
function localId() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
2020-04-08 14:13:33 +05:30
export default {
components: {
BlobHeaderEdit,
BlobContentEdit,
2020-04-22 19:07:51 +05:30
GlLoadingIcon,
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-04-22 19:07:51 +05:30
required: false,
2020-07-28 23:09:34 +05:30
default: null,
validator: ({ rawPath }) => Boolean(rawPath),
2020-04-08 14:13:33 +05:30
},
2020-07-28 23:09:34 +05:30
},
data() {
return {
id: localId(),
filePath: this.blob?.path || '',
previousPath: '',
originalPath: this.blob?.path || '',
content: this.blob?.content || '',
originalContent: '',
isContentLoading: this.blob,
};
},
watch: {
filePath(filePath, previousPath) {
this.previousPath = previousPath;
this.notifyAboutUpdates({ previousPath });
2020-04-22 19:07:51 +05:30
},
2020-07-28 23:09:34 +05:30
content() {
this.notifyAboutUpdates();
},
},
mounted() {
if (this.blob) {
this.fetchBlobContent();
}
},
methods: {
notifyAboutUpdates(args = {}) {
const { filePath, previousPath } = args;
this.$emit('blob-updated', {
filePath: filePath || this.filePath,
previousPath: previousPath || this.previousPath,
content: this.content,
_constants: {
originalPath: this.originalPath,
originalContent: this.originalContent,
id: this.id,
},
});
},
fetchBlobContent() {
const baseUrl = getBaseURL();
const url = joinPaths(baseUrl, this.blob.rawPath);
axios
.get(url)
.then(res => {
this.originalContent = res.data;
this.content = res.data;
})
.catch(e => this.flashAPIFailure(e))
.finally(() => {
this.isContentLoading = false;
});
},
flashAPIFailure(err) {
Flash(sprintf(SNIPPET_BLOB_CONTENT_FETCH_ERROR, { err }));
this.isContentLoading = false;
2020-04-08 14:13:33 +05:30
},
},
};
</script>
<template>
<div class="form-group file-editor">
<label>{{ s__('Snippets|File') }}</label>
<div class="file-holder snippet">
2020-07-28 23:09:34 +05:30
<blob-header-edit v-model="filePath" data-qa-selector="file_name_field" />
2020-04-22 19:07:51 +05:30
<gl-loading-icon
2020-07-28 23:09:34 +05:30
v-if="isContentLoading"
2020-04-22 19:07:51 +05:30
:label="__('Loading snippet')"
size="lg"
class="loading-animation prepend-top-20 append-bottom-20"
/>
2020-07-28 23:09:34 +05:30
<blob-content-edit v-else v-model="content" :file-name="filePath" />
2020-04-08 14:13:33 +05:30
</div>
</div>
</template>