issue_note_form.vue 3.9 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 36
    data() {
      return {
        initialNote: this.noteBody,
        note: this.noteBody,
        conflictWhileEditing: false,
      };
37
    },
38 39
    components: {
      markdownField,
40
    },
41
    computed: {
42 43
      ...mapGetters([
        'getDiscussionLastNote',
44 45 46
        'getIssueDataByProp',
        'getNotesDataByProp',
        'getUserDataByProp',
47
      ]),
48 49 50
      noteHash() {
        return `#note_${this.noteId}`;
      },
51 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
    methods: {
      handleUpdate() {
66
        this.$emit('handleFormUpdate', this.note);
67 68 69
      },
      editMyLastNote() {
        if (this.note === '') {
70
          const lastNoteInDiscussion = this.getDiscussionLastNote(this.discussion, this.currentUserId);
71

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

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