issue_note_form.vue 4.2 KB
Newer Older
1
<script>
2
  import { mapGetters } from 'vuex';
3 4
  import markdownField from '../../vue_shared/components/markdown/field.vue';
  import eventHub from '../event_hub';
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  export default {
    props: {
      noteBody: {
        type: String,
        required: false,
        default: '',
      },
      noteId: {
        type: Number,
        required: false,
      },
      saveButtonTitle: {
        type: String,
        required: false,
        default: 'Save comment',
      },
22
      discussion: {
23
        type: Object,
24
        required: false,
25
        default: () => ({}),
26 27 28 29 30
      },
      isEditing: {
        type: Boolean,
        required: true,
      },
31
    },
32 33 34 35
    data() {
      return {
        note: this.noteBody,
        conflictWhileEditing: false,
36
        isSubmitting: false,
37
      };
38
    },
39 40
    components: {
      markdownField,
41
    },
42
    computed: {
43 44
      ...mapGetters([
        'getDiscussionLastNote',
45 46 47
        'getIssueDataByProp',
        'getNotesDataByProp',
        'getUserDataByProp',
48
      ]),
49 50 51
      noteHash() {
        return `#note_${this.noteId}`;
      },
52 53 54 55 56 57 58
      markdownPreviewUrl() {
        return this.getIssueDataByProp('preview_note_path');
      },
      markdownDocsUrl() {
        return this.getNotesDataByProp('markdownDocs');
      },
      quickActionsDocsUrl() {
59
        return !this.isEditing ? this.getNotesDataByProp('quickActionsDocs') : undefined;
60 61 62
      },
      currentUserId() {
        return this.getUserDataByProp('id');
63 64 65 66
      },
      isDisabled() {
        return !this.note.length || this.isSubmitting;
      },
67
    },
68 69
    methods: {
      handleUpdate() {
70
        this.isSubmitting = true;
71
        this.$emit('handleFormUpdate', this.note, this.$refs.editNoteForm);
72 73 74
      },
      editMyLastNote() {
        if (this.note === '') {
75
          const lastNoteInDiscussion = this.getDiscussionLastNote(this.discussion);
76

77
          if (lastNoteInDiscussion) {
78
            eventHub.$emit('enterEditMode', {
79
              noteId: lastNoteInDiscussion.id,
80 81
            });
          }
82
        }
83
      },
84 85
      cancelHandler(shouldConfirm = false) {
        // Sends information about confirm message and if the textarea has changed
86
        this.$emit('cancelFormEdition', shouldConfirm, this.noteBody !== this.note);
87
      },
88
    },
89 90 91 92 93
    mounted() {
      this.$refs.textarea.focus();
    },
    watch: {
      noteBody() {
94
        if (this.note === this.noteBody) {
95 96 97 98 99
          this.note = this.noteBody;
        } else {
          this.conflictWhileEditing = true;
        }
      },
100
    },
101
  };
102 103 104
</script>

<template>
105
  <div ref="editNoteForm" class="note-edit-form current-note-edit-form">
106 107 108 109 110 111 112 113 114 115
    <div
      v-if="conflictWhileEditing"
      class="js-conflict-edit-warning alert alert-danger">
      This comment has changed since you started editing, please review the
      <a
        :href="noteHash"
        target="_blank"
        rel="noopener noreferrer">updated comment</a>
        to ensure information is not lost.
    </div>
116
    <div class="flash-container timeline-content"></div>
117
    <form
118
      class="edit-note common-note-form">
119 120 121
      <markdown-field
        :markdown-preview-url="markdownPreviewUrl"
        :markdown-docs="markdownDocsUrl"
122 123
        :quick-actions-docs="quickActionsDocsUrl"
        :add-spacing-classes="false">
124 125
        <textarea
          id="note-body"
F
Fatih Acet 已提交
126
          name="note[note]"
127
          class="note-textarea js-gfm-input js-autosize markdown-area js-vue-issue-note-form"
128
          :data-supports-quick-actions="!isEditing"
129 130 131 132 133
          aria-label="Description"
          v-model="note"
          ref="textarea"
          slot="textarea"
          placeholder="Write a comment or drag your files here..."
134 135
          @keydown.meta.enter="handleUpdate()"
          @keydown.up="editMyLastNote()"
136
          @keydown.esc="cancelHandler(true)">
137 138 139 140
        </textarea>
      </markdown-field>
      <div class="note-form-actions clearfix">
        <button
F
Filipa Lacerda 已提交
141
          type="submit"
142
          @click="handleUpdate()"
143
          :disabled="isDisabled"
144
          class="js-vue-issue-save btn btn-save">
145
          {{saveButtonTitle}}
146 147
        </button>
        <button
148
          @click="cancelHandler()"
149
          class="btn btn-nr btn-cancel note-edit-cancel"
150 151 152 153 154 155 156
          type="button">
          Cancel
        </button>
      </div>
    </form>
  </div>
</template>
157