debian-mirror-gitlab/app/assets/javascripts/notes/components/comment_form.vue

434 lines
14 KiB
Vue
Raw Normal View History

2018-03-17 18:26:18 +05:30
<script>
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
import { mapActions, mapGetters, mapState } from 'vuex';
2020-03-13 15:44:24 +05:30
import { isEmpty } from 'lodash';
2018-05-09 12:01:36 +05:30
import Autosize from 'autosize';
2021-02-22 17:27:13 +05:30
import { GlButton, GlIcon } from '@gitlab/ui';
2018-05-09 12:01:36 +05:30
import { __, sprintf } from '~/locale';
2019-02-15 15:39:39 +05:30
import TimelineEntryItem from '~/vue_shared/components/notes/timeline_entry_item.vue';
2021-02-22 17:27:13 +05:30
import { deprecatedCreateFlash as Flash } from '~/flash';
import Autosave from '~/autosave';
2018-12-05 23:21:45 +05:30
import {
capitalizeFirstCharacter,
convertToCamelCase,
splitCamelCase,
2019-07-31 22:56:46 +05:30
slugifyWithUnderscore,
2021-02-22 17:27:13 +05:30
} from '~/lib/utils/text_utility';
2019-09-30 21:07:59 +05:30
import { refreshUserMergeRequestCounts } from '~/commons/nav/user_merge_requests';
2018-05-09 12:01:36 +05:30
import * as constants from '../constants';
import eventHub from '../event_hub';
2021-02-22 17:27:13 +05:30
import markdownField from '~/vue_shared/components/markdown/field.vue';
import userAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
2021-03-08 18:12:59 +05:30
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
2018-05-09 12:01:36 +05:30
import noteSignedOutWidget from './note_signed_out_widget.vue';
import discussionLockedWidget from './discussion_locked_widget.vue';
import issuableStateMixin from '../mixins/issuable_state';
2021-03-08 18:12:59 +05:30
import CommentFieldLayout from './comment_field_layout.vue';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export default {
name: 'CommentForm',
components: {
noteSignedOutWidget,
discussionLockedWidget,
markdownField,
userAvatarLink,
2020-11-24 15:15:51 +05:30
GlButton,
2019-02-15 15:39:39 +05:30
TimelineEntryItem,
2021-01-29 00:20:46 +05:30
GlIcon,
2021-03-08 18:12:59 +05:30
CommentFieldLayout,
2018-05-09 12:01:36 +05:30
},
2021-03-08 18:12:59 +05:30
mixins: [glFeatureFlagsMixin(), issuableStateMixin],
2018-05-09 12:01:36 +05:30
props: {
noteableType: {
type: String,
required: true,
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
},
data() {
return {
note: '',
noteType: constants.COMMENT,
isSubmitting: false,
isSubmitButtonDisabled: true,
};
},
computed: {
...mapGetters([
'getCurrentUserLastNote',
'getUserData',
'getNoteableData',
2020-06-23 00:09:42 +05:30
'getNoteableDataByProp',
2018-05-09 12:01:36 +05:30
'getNotesData',
'openState',
]),
2021-02-22 17:27:13 +05:30
...mapState(['isToggleStateButtonLoading']),
2018-05-09 12:01:36 +05:30
noteableDisplayName() {
2018-11-08 19:23:39 +05:30
return splitCamelCase(this.noteableType).toLowerCase();
2018-03-27 19:54:05 +05:30
},
2018-05-09 12:01:36 +05:30
isLoggedIn() {
return this.getUserData.id;
},
commentButtonTitle() {
2019-09-30 21:07:59 +05:30
return this.noteType === constants.COMMENT ? __('Comment') : __('Start thread');
2018-11-08 19:23:39 +05:30
},
startDiscussionDescription() {
2019-09-30 21:07:59 +05:30
return this.getNoteableData.noteableType === constants.MERGE_REQUEST_NOTEABLE_TYPE
? __('Discuss a specific suggestion or question that needs to be resolved.')
: __('Discuss a specific suggestion or question.');
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
isOpen() {
2018-11-08 19:23:39 +05:30
return this.openState === constants.OPENED || this.openState === constants.REOPENED;
2018-05-09 12:01:36 +05:30
},
canCreateNote() {
return this.getNoteableData.current_user.can_create_note;
},
issueActionButtonTitle() {
const openOrClose = this.isOpen ? 'close' : 'reopen';
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (this.note.length) {
return sprintf(__('%{actionText} & %{openOrClose} %{noteable}'), {
actionText: this.commentButtonTitle,
openOrClose,
noteable: this.noteableDisplayName,
});
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
return sprintf(__('%{openOrClose} %{noteable}'), {
openOrClose: capitalizeFirstCharacter(openOrClose),
noteable: this.noteableDisplayName,
});
2018-03-17 18:26:18 +05:30
},
2020-11-24 15:15:51 +05:30
buttonVariant() {
return this.isOpen ? 'warning' : 'default';
},
2018-05-09 12:01:36 +05:30
actionButtonClassNames() {
return {
'btn-reopen': !this.isOpen,
'btn-close': this.isOpen,
'js-note-target-close': this.isOpen,
'js-note-target-reopen': !this.isOpen,
};
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
markdownDocsPath() {
return this.getNotesData.markdownDocsPath;
},
quickActionsDocsPath() {
return this.getNotesData.quickActionsDocsPath;
},
markdownPreviewPath() {
return this.getNoteableData.preview_note_path;
},
author() {
return this.getUserData;
},
2019-07-31 22:56:46 +05:30
canToggleIssueState() {
return (
this.getNoteableData.current_user.can_update &&
2020-07-28 23:09:34 +05:30
this.getNoteableData.state !== constants.MERGED &&
!this.closedAndLocked
2019-07-31 22:56:46 +05:30
);
2018-05-09 12:01:36 +05:30
},
2020-07-28 23:09:34 +05:30
closedAndLocked() {
return !this.isOpen && this.isLocked(this.getNoteableData);
},
2018-05-09 12:01:36 +05:30
endpoint() {
return this.getNoteableData.create_note_path;
},
2018-11-08 19:23:39 +05:30
issuableTypeTitle() {
2018-12-05 23:21:45 +05:30
return this.noteableType === constants.MERGE_REQUEST_NOTEABLE_TYPE
2019-09-30 21:07:59 +05:30
? __('merge request')
: __('issue');
2018-11-08 19:23:39 +05:30
},
2021-02-22 17:27:13 +05:30
isIssue() {
2020-06-23 00:09:42 +05:30
return this.noteableDisplayName === constants.ISSUE_NOTEABLE_TYPE;
},
2019-07-31 22:56:46 +05:30
trackingLabel() {
return slugifyWithUnderscore(`${this.commentButtonTitle} button`);
},
2018-05-09 12:01:36 +05:30
},
watch: {
note(newNote) {
this.setIsSubmitButtonDisabled(newNote, this.isSubmitting);
},
isSubmitting(newValue) {
this.setIsSubmitButtonDisabled(this.note, newValue);
},
},
mounted() {
// jQuery is needed here because it is a custom event being dispatched with jQuery.
$(document).on('issuable:change', (e, isClosed) => {
2018-11-08 19:23:39 +05:30
this.toggleIssueLocalState(isClosed ? constants.CLOSED : constants.REOPENED);
2018-05-09 12:01:36 +05:30
});
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.initAutoSave();
},
methods: {
...mapActions([
'saveNote',
'stopPolling',
'restartPolling',
'removePlaceholderNotes',
2021-02-22 17:27:13 +05:30
'closeIssuable',
'reopenIssuable',
2018-05-09 12:01:36 +05:30
'toggleIssueLocalState',
]),
setIsSubmitButtonDisabled(note, isSubmitting) {
2020-03-13 15:44:24 +05:30
if (!isEmpty(note) && !isSubmitting) {
2018-05-09 12:01:36 +05:30
this.isSubmitButtonDisabled = false;
} else {
this.isSubmitButtonDisabled = true;
}
2018-03-17 18:26:18 +05:30
},
2018-05-09 12:01:36 +05:30
handleSave(withIssueAction) {
if (this.note.length) {
const noteData = {
endpoint: this.endpoint,
flashContainer: this.$el,
data: {
note: {
noteable_type: this.noteableType,
noteable_id: this.getNoteableData.id,
note: this.note,
2018-03-17 18:26:18 +05:30
},
2018-11-08 19:23:39 +05:30
merge_request_diff_head_sha: this.getNoteableData.diff_head_sha,
2018-05-09 12:01:36 +05:30
},
};
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (this.noteType === constants.DISCUSSION) {
noteData.data.note.type = constants.DISCUSSION_NOTE;
}
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
this.note = ''; // Empty textarea while being requested. Repopulate in catch
this.resizeTextarea();
this.stopPolling();
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
this.isSubmitting = true;
2018-05-09 12:01:36 +05:30
this.saveNote(noteData)
2020-01-01 13:55:28 +05:30
.then(() => {
2018-05-09 12:01:36 +05:30
this.restartPolling();
2020-01-01 13:55:28 +05:30
this.discard();
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (withIssueAction) {
this.toggleIssueState();
}
})
.catch(() => {
this.discard(false);
2019-09-30 21:07:59 +05:30
const msg = __(
'Your comment could not be submitted! Please check your network connection and try again.',
);
2018-05-09 12:01:36 +05:30
Flash(msg, 'alert', this.$el);
this.note = noteData.data.note.note; // Restore textarea content.
this.removePlaceholderNotes();
2021-02-22 17:27:13 +05:30
})
.finally(() => {
this.isSubmitting = false;
2018-05-09 12:01:36 +05:30
});
} else {
this.toggleIssueState();
}
},
toggleIssueState() {
2021-02-22 17:27:13 +05:30
if (this.isIssue) {
// We want to invoke the close/reopen logic in the issue header
// since that is where the blocked-by issues modal logic is also defined
eventHub.$emit('toggle.issuable.state');
2020-05-24 23:13:21 +05:30
return;
}
2019-02-15 15:39:39 +05:30
2021-02-22 17:27:13 +05:30
const toggleState = this.isOpen ? this.closeIssuable : this.reopenIssuable;
2019-02-15 15:39:39 +05:30
2021-02-22 17:27:13 +05:30
toggleState()
.then(refreshUserMergeRequestCounts)
.catch(() => Flash(constants.toggleStateErrorMessage[this.noteableType][this.openState]));
2020-05-24 23:13:21 +05:30
},
2018-05-09 12:01:36 +05:30
discard(shouldClear = true) {
// `blur` is needed to clear slash commands autocomplete cache if event fired.
// `focus` is needed to remain cursor in the textarea.
this.$refs.textarea.blur();
this.$refs.textarea.focus();
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (shouldClear) {
this.note = '';
this.resizeTextarea();
this.$refs.markdownField.previewMarkdown = false;
}
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
this.autosave.reset();
},
setNoteType(type) {
this.noteType = type;
},
editCurrentUserLastNote() {
if (this.note === '') {
const lastNote = this.getCurrentUserLastNote;
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if (lastNote) {
eventHub.$emit('enterEditMode', {
noteId: lastNote.id,
});
2018-03-17 18:26:18 +05:30
}
2018-05-09 12:01:36 +05:30
}
},
initAutoSave() {
if (this.isLoggedIn) {
2018-11-08 19:23:39 +05:30
const noteableType = capitalizeFirstCharacter(convertToCamelCase(this.noteableType));
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
this.autosave = new Autosave($(this.$refs.textarea), [
2019-09-30 21:07:59 +05:30
__('Note'),
2018-05-09 12:01:36 +05:30
noteableType,
this.getNoteableData.id,
]);
}
},
resizeTextarea() {
this.$nextTick(() => {
Autosize.update(this.$refs.textarea);
});
2018-03-17 18:26:18 +05:30
},
2021-03-08 18:12:59 +05:30
hasEmailParticipants() {
return this.getNoteableData.issue_email_participants?.length;
},
2018-05-09 12:01:36 +05:30
},
};
2018-03-17 18:26:18 +05:30
</script>
<template>
<div>
<note-signed-out-widget v-if="!isLoggedIn" />
2019-02-15 15:39:39 +05:30
<discussion-locked-widget v-else-if="!canCreateNote" :issuable-type="issuableTypeTitle" />
<ul v-else-if="canCreateNote" class="notes notes-form timeline">
<timeline-entry-item class="note-form">
<div class="flash-container error-alert timeline-content"></div>
2021-01-29 00:20:46 +05:30
<div class="timeline-icon d-none d-md-block">
2019-02-15 15:39:39 +05:30
<user-avatar-link
v-if="author"
:link-href="author.path"
:img-src="author.avatar_url"
:img-alt="author.name"
:img-size="40"
/>
</div>
<div class="timeline-content timeline-content-form">
<form ref="commentForm" class="new-note common-note-form gfm-form js-main-target-form">
2021-03-08 18:12:59 +05:30
<comment-field-layout
:with-alert-container="true"
:noteable-data="getNoteableData"
2020-07-28 23:09:34 +05:30
:noteable-type="noteableType"
2019-02-15 15:39:39 +05:30
>
2021-03-08 18:12:59 +05:30
<markdown-field
ref="markdownField"
:is-submitting="isSubmitting"
:markdown-preview-path="markdownPreviewPath"
:markdown-docs-path="markdownDocsPath"
:quick-actions-docs-path="quickActionsDocsPath"
:add-spacing-classes="false"
:textarea-value="note"
>
<template #textarea>
<textarea
id="note-body"
ref="textarea"
v-model="note"
dir="auto"
:disabled="isSubmitting"
name="note[note]"
class="note-textarea js-vue-comment-form js-note-text js-gfm-input js-autosize markdown-area"
data-qa-selector="comment_field"
data-testid="comment-field"
:data-supports-quick-actions="!glFeatures.tributeAutocomplete"
:aria-label="__('Description')"
:placeholder="__('Write a comment or drag your files here…')"
@keydown.up="editCurrentUserLastNote()"
@keydown.meta.enter="handleSave()"
@keydown.ctrl.enter="handleSave()"
></textarea>
</template>
</markdown-field>
</comment-field-layout>
2019-02-15 15:39:39 +05:30
<div class="note-form-actions">
<div
2020-07-28 23:09:34 +05:30
class="btn-group gl-mr-3 comment-type-dropdown js-comment-type-dropdown droplab-dropdown"
2019-02-15 15:39:39 +05:30
>
2020-11-24 15:15:51 +05:30
<gl-button
2019-02-15 15:39:39 +05:30
:disabled="isSubmitButtonDisabled"
2021-01-03 14:25:43 +05:30
class="js-comment-button js-comment-submit-button"
data-qa-selector="comment_button"
2021-02-22 17:27:13 +05:30
data-testid="comment-button"
2019-02-15 15:39:39 +05:30
type="submit"
2020-11-24 15:15:51 +05:30
category="primary"
variant="success"
2019-07-31 22:56:46 +05:30
:data-track-label="trackingLabel"
data-track-event="click_button"
2019-03-02 22:35:43 +05:30
@click.prevent="handleSave()"
2020-11-24 15:15:51 +05:30
>{{ commentButtonTitle }}</gl-button
2019-02-15 15:39:39 +05:30
>
2020-11-24 15:15:51 +05:30
<gl-button
2019-02-15 15:39:39 +05:30
:disabled="isSubmitButtonDisabled"
name="button"
2020-11-24 15:15:51 +05:30
category="primary"
variant="success"
2021-01-03 14:25:43 +05:30
class="note-type-toggle js-note-new-discussion dropdown-toggle"
data-qa-selector="note_dropdown"
2019-02-15 15:39:39 +05:30
data-display="static"
data-toggle="dropdown"
2020-11-24 15:15:51 +05:30
icon="chevron-down"
2019-09-30 21:07:59 +05:30
:aria-label="__('Open comment type dropdown')"
2020-11-24 15:15:51 +05:30
/>
2019-02-15 15:39:39 +05:30
<ul class="note-type-dropdown dropdown-open-top dropdown-menu">
<li :class="{ 'droplab-item-selected': noteType === 'comment' }">
<button
type="button"
class="btn btn-transparent"
2019-03-02 22:35:43 +05:30
@click.prevent="setNoteType('comment')"
2019-02-15 15:39:39 +05:30
>
2021-01-29 00:20:46 +05:30
<gl-icon name="check" class="icon" />
2019-02-15 15:39:39 +05:30
<div class="description">
2019-09-30 21:07:59 +05:30
<strong>{{ __('Comment') }}</strong>
<p>
{{
sprintf(__('Add a general comment to this %{noteableDisplayName}.'), {
noteableDisplayName,
})
}}
</p>
2019-02-15 15:39:39 +05:30
</div>
</button>
</li>
<li class="divider droplab-item-ignore"></li>
<li :class="{ 'droplab-item-selected': noteType === 'discussion' }">
2021-01-03 14:25:43 +05:30
<button
data-qa-selector="discussion_menu_item"
@click.prevent="setNoteType('discussion')"
>
2021-01-29 00:20:46 +05:30
<gl-icon name="check" class="icon" />
2019-02-15 15:39:39 +05:30
<div class="description">
2019-09-30 21:07:59 +05:30
<strong>{{ __('Start thread') }}</strong>
2019-02-15 15:39:39 +05:30
<p>{{ startDiscussionDescription }}</p>
</div>
</button>
</li>
</ul>
2018-03-17 18:26:18 +05:30
</div>
2019-02-15 15:39:39 +05:30
2020-11-24 15:15:51 +05:30
<gl-button
2021-02-22 17:27:13 +05:30
v-if="canToggleIssueState"
2019-02-15 15:39:39 +05:30
:loading="isToggleStateButtonLoading"
2020-11-24 15:15:51 +05:30
category="secondary"
:variant="buttonVariant"
2021-02-22 17:27:13 +05:30
:class="[actionButtonClassNames, 'btn-comment btn-comment-and-close']"
:disabled="isSubmitting"
data-testid="close-reopen-button"
2019-03-02 22:35:43 +05:30
@click="handleSave(true)"
2020-11-24 15:15:51 +05:30
>{{ issueActionButtonTitle }}</gl-button
>
2019-02-15 15:39:39 +05:30
</div>
</form>
2018-03-17 18:26:18 +05:30
</div>
2019-02-15 15:39:39 +05:30
</timeline-entry-item>
</ul>
2018-03-17 18:26:18 +05:30
</div>
</template>