issue_note_form.vue 4.1 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 26 27 28 29
      },
      isEditing: {
        type: Boolean,
        required: true,
      },
30
    },
31 32 33 34 35
    data() {
      return {
        initialNote: this.noteBody,
        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 59 60 61 62
      markdownPreviewUrl() {
        return this.getIssueDataByProp('preview_note_path');
      },
      markdownDocsUrl() {
        return this.getNotesDataByProp('markdownDocs');
      },
      quickActionsDocsUrl() {
        return this.getNotesDataByProp('quickActionsDocs');
      },
      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);
72 73 74
      },
      editMyLastNote() {
        if (this.note === '') {
F
Filipa Lacerda 已提交
75 76 77 78
          const lastNoteInDiscussion = this.getDiscussionLastNote(
            this.discussion,
            this.currentUserId,
          );
79

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

<template>
108
  <div class="note-edit-form current-note-edit-form">
109 110 111 112 113 114 115 116 117 118
    <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>
119 120
    <form
      class="edit-note common-note-form">
121 122 123
      <markdown-field
        :markdown-preview-url="markdownPreviewUrl"
        :markdown-docs="markdownDocsUrl"
124 125
        :quick-actions-docs="quickActionsDocsUrl"
        :add-spacing-classes="false">
126 127
        <textarea
          id="note-body"
F
Fatih Acet 已提交
128
          name="note[note]"
129
          class="note-textarea js-gfm-input js-autosize markdown-area"
130
          :data-supports-quick-actions="!isEditing"
131 132 133 134 135
          aria-label="Description"
          v-model="note"
          ref="textarea"
          slot="textarea"
          placeholder="Write a comment or drag your files here..."
136
          @keydown.meta.enter="handleUpdate"
137
          @keydown.up="editMyLastNote"
138
          @keydown.esc="cancelHandler(true)">
139 140 141 142
        </textarea>
      </markdown-field>
      <div class="note-form-actions clearfix">
        <button
143
          type="button"
144 145
          @click="handleUpdate"
          :disabled="isDisabled"
146
          class="btn btn-nr btn-save">
147
          {{saveButtonTitle}}
148 149
        </button>
        <button
150
          @click="cancelHandler()"
151
          class="btn btn-nr btn-cancel note-edit-cancel"
152 153 154 155 156 157 158
          type="button">
          Cancel
        </button>
      </div>
    </form>
  </div>
</template>