gitlab-org--gitlab-foss/app/assets/javascripts/issue_show/components/description.vue

159 lines
3.5 KiB
Vue
Raw Normal View History

<script>
import { GlSafeHtmlDirective as SafeHtml } from '@gitlab/ui';
import $ from 'jquery';
import createFlash from '~/flash';
import { s__, sprintf } from '~/locale';
2018-10-10 02:23:54 -04:00
import TaskList from '../../task_list';
import animateMixin from '../mixins/animate';
2018-10-10 02:23:54 -04:00
export default {
directives: {
SafeHtml,
},
mixins: [animateMixin],
2018-10-10 02:23:54 -04:00
props: {
canUpdate: {
type: Boolean,
required: true,
},
2018-10-10 02:23:54 -04:00
descriptionHtml: {
type: String,
required: true,
},
2018-10-10 02:23:54 -04:00
descriptionText: {
type: String,
required: false,
default: '',
2018-10-10 02:23:54 -04:00
},
taskStatus: {
type: String,
required: false,
default: '',
},
issuableType: {
type: String,
required: false,
default: 'issue',
},
updateUrl: {
type: String,
required: false,
default: null,
},
lockVersion: {
type: Number,
required: false,
2019-01-22 15:53:08 -05:00
default: 0,
},
2018-10-10 02:23:54 -04:00
},
data() {
return {
preAnimation: false,
pulseAnimation: false,
initialUpdate: true,
2018-10-10 02:23:54 -04:00
};
},
watch: {
descriptionHtml(newDescription, oldDescription) {
if (!this.initialUpdate && newDescription !== oldDescription) {
this.animateChange();
} else {
this.initialUpdate = false;
}
2018-10-10 02:23:54 -04:00
this.$nextTick(() => {
this.renderGFM();
});
},
2018-10-10 02:23:54 -04:00
taskStatus() {
2018-01-05 09:31:01 -05:00
this.updateTaskStatusText();
},
2018-10-10 02:23:54 -04:00
},
mounted() {
this.renderGFM();
this.updateTaskStatusText();
},
methods: {
renderGFM() {
$(this.$refs['gfm-content']).renderGFM();
2018-10-10 02:23:54 -04:00
if (this.canUpdate) {
// eslint-disable-next-line no-new
new TaskList({
dataType: this.issuableType,
fieldName: 'description',
lockVersion: this.lockVersion,
2018-10-10 02:23:54 -04:00
selector: '.detail-page-description',
onError: this.taskListUpdateError.bind(this),
2018-10-10 02:23:54 -04:00
});
}
},
2019-01-22 15:53:08 -05:00
taskListUpdateError() {
createFlash({
message: sprintf(
s__(
'Someone edited this %{issueType} at the same time you did. The description has been updated and you will need to make your changes again.',
),
{
issueType: this.issuableType,
},
2019-01-26 09:49:49 -05:00
),
});
this.$emit('taskListUpdateFailed');
},
2018-10-10 02:23:54 -04:00
updateTaskStatusText() {
const taskRegexMatches = this.taskStatus.match(/(\d+) of ((?!0)\d+)/);
const $issuableHeader = $('.issuable-meta');
const $tasks = $('#task_status', $issuableHeader);
const $tasksShort = $('#task_status_short', $issuableHeader);
2018-10-10 02:23:54 -04:00
if (taskRegexMatches) {
$tasks.text(this.taskStatus);
$tasksShort.text(
`${taskRegexMatches[1]}/${taskRegexMatches[2]} task${taskRegexMatches[2] > 1 ? 's' : ''}`,
);
} else {
$tasks.text('');
$tasksShort.text('');
}
},
2018-10-10 02:23:54 -04:00
},
};
</script>
<template>
<div
2017-05-16 05:42:17 -04:00
v-if="descriptionHtml"
:class="{
2018-11-16 15:07:38 -05:00
'js-task-list-container': canUpdate,
2018-01-05 09:31:01 -05:00
}"
2018-06-11 05:49:47 -04:00
class="description"
2018-01-05 09:31:01 -05:00
>
<div
2018-06-11 05:49:47 -04:00
ref="gfm-content"
v-safe-html="descriptionHtml"
:class="{
'issue-realtime-pre-pulse': preAnimation,
2018-11-16 15:07:38 -05:00
'issue-realtime-trigger-pulse': pulseAnimation,
}"
class="md"
2018-11-16 15:07:38 -05:00
></div>
<!-- eslint-disable vue/no-mutating-props -->
<textarea
v-if="descriptionText"
ref="textarea"
2017-11-29 04:29:06 -05:00
v-model="descriptionText"
:data-update-url="updateUrl"
2018-06-11 05:49:47 -04:00
class="hidden js-task-list-field"
dir="auto"
2017-11-29 04:29:06 -05:00
>
</textarea>
<!-- eslint-enable vue/no-mutating-props -->
</div>
</template>