debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/markdown/suggestions.vue

130 lines
3.4 KiB
Vue
Raw Normal View History

2019-02-15 15:39:39 +05:30
<script>
import Vue from 'vue';
2020-11-24 15:15:51 +05:30
import { GlSafeHtmlDirective as SafeHtml } from '@gitlab/ui';
2019-09-30 21:07:59 +05:30
import { __ } from '~/locale';
2019-02-15 15:39:39 +05:30
import SuggestionDiff from './suggestion_diff.vue';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as Flash } from '~/flash';
2019-02-15 15:39:39 +05:30
export default {
2020-11-24 15:15:51 +05:30
directives: {
SafeHtml,
},
2019-02-15 15:39:39 +05:30
props: {
lineType: {
type: String,
required: false,
default: '',
},
suggestions: {
type: Array,
required: false,
default: () => [],
},
2020-06-23 00:09:42 +05:30
batchSuggestionsInfo: {
type: Array,
required: false,
default: () => [],
},
2019-02-15 15:39:39 +05:30
noteHtml: {
type: String,
required: true,
},
disabled: {
type: Boolean,
required: false,
default: false,
},
helpPagePath: {
type: String,
required: true,
},
2021-01-03 14:25:43 +05:30
suggestionsCount: {
type: Number,
required: false,
default: 0,
},
2019-02-15 15:39:39 +05:30
},
data() {
return {
isRendered: false,
};
},
watch: {
suggestions() {
this.reset();
},
noteHtml() {
this.reset();
},
},
mounted() {
this.renderSuggestions();
},
methods: {
renderSuggestions() {
// swaps out suggestion(s) markdown with rich diff components
// (while still keeping non-suggestion markdown in place)
if (!this.noteHtml) return;
const { container } = this.$refs;
const suggestionElements = container.querySelectorAll('.js-render-suggestion');
if (this.lineType === 'old') {
2019-09-30 21:07:59 +05:30
Flash(__('Unable to apply suggestions to a deleted line.'), 'alert', this.$el);
2019-02-15 15:39:39 +05:30
}
suggestionElements.forEach((suggestionEl, i) => {
const suggestionParentEl = suggestionEl.parentElement;
2019-07-07 11:18:12 +05:30
const diffComponent = this.generateDiff(i);
2019-02-15 15:39:39 +05:30
diffComponent.$mount(suggestionParentEl);
});
this.isRendered = true;
},
2019-07-07 11:18:12 +05:30
generateDiff(suggestionIndex) {
2021-01-03 14:25:43 +05:30
const { suggestions, disabled, batchSuggestionsInfo, helpPagePath, suggestionsCount } = this;
2019-02-15 15:39:39 +05:30
const suggestion =
suggestions && suggestions[suggestionIndex] ? suggestions[suggestionIndex] : {};
const SuggestionDiffComponent = Vue.extend(SuggestionDiff);
const suggestionDiff = new SuggestionDiffComponent({
2021-01-03 14:25:43 +05:30
propsData: { disabled, suggestion, batchSuggestionsInfo, helpPagePath, suggestionsCount },
2019-02-15 15:39:39 +05:30
});
suggestionDiff.$on('apply', ({ suggestionId, callback }) => {
this.$emit('apply', { suggestionId, callback, flashContainer: this.$el });
});
2020-06-23 00:09:42 +05:30
suggestionDiff.$on('applyBatch', () => {
this.$emit('applyBatch', { flashContainer: this.$el });
});
suggestionDiff.$on('addToBatch', suggestionId => {
this.$emit('addToBatch', suggestionId);
});
suggestionDiff.$on('removeFromBatch', suggestionId => {
this.$emit('removeFromBatch', suggestionId);
});
2019-02-15 15:39:39 +05:30
return suggestionDiff;
},
reset() {
// resets the container HTML (replaces it with the updated noteHTML)
// calls `renderSuggestions` once the updated noteHTML is added to the DOM
this.$refs.container.innerHTML = this.noteHtml;
this.isRendered = false;
this.renderSuggestions();
this.$nextTick(() => this.renderSuggestions());
},
},
};
</script>
<template>
<div>
2019-03-02 22:35:43 +05:30
<div class="flash-container js-suggestions-flash"></div>
2020-11-24 15:15:51 +05:30
<div v-show="isRendered" ref="container" v-safe-html="noteHtml" class="md"></div>
2019-02-15 15:39:39 +05:30
</div>
</template>