issue_note_form.vue 3.5 KB
Newer Older
1
<script>
2 3
  import markdownField from '../../vue_shared/components/markdown/field.vue';
  import eventHub from '../event_hub';
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  export default {
    props: {
      noteBody: {
        type: String,
        required: false,
        default: '',
      },
      noteId: {
        type: Number,
        required: false,
      },
      updateHandler: {
        type: Function,
        required: true,
      },
      cancelHandler: {
        type: Function,
        required: true,
      },
      saveButtonTitle: {
        type: String,
        required: false,
        default: 'Save comment',
      },
29
    },
30
    data() {
31 32
      const { getIssueData, getNotesData } =  this.$store.getters;

33 34 35
      return {
        initialNote: this.noteBody,
        note: this.noteBody,
36 37
        markdownPreviewUrl: getIssueData.preview_note_path,
        markdownDocsUrl: getNotesData.markdownDocs,
38 39
        conflictWhileEditing: false,
      };
40
    },
41 42
    components: {
      markdownField,
43
    },
44 45 46 47 48 49 50
    computed: {
      isDirty() {
        return this.initialNote !== this.note;
      },
      noteHash() {
        return `#note_${this.noteId}`;
      },
51
    },
52 53 54 55 56 57 58 59 60 61
    methods: {
      handleUpdate() {
        this.updateHandler({
          note: this.note,
        });
      },
      editMyLastNote() {
        if (this.note === '') {
          const discussion = $(this.$el).closest('.discussion-notes');
          const myLastNoteId = discussion.find('.js-my-note').last().attr('id');
62

63 64 65 66 67
          if (myLastNoteId) {
            eventHub.$emit('enterEditMode', {
              noteId: parseInt(myLastNoteId.replace('note_', ''), 10),
            });
          }
68
        }
69
      },
70
    },
71 72 73 74 75 76 77 78 79 80 81
    mounted() {
      this.$refs.textarea.focus();
    },
    watch: {
      noteBody() {
        if (this.note === this.initialNote) {
          this.note = this.noteBody;
        } else {
          this.conflictWhileEditing = true;
        }
      },
82
    },
83
  };
84 85 86
</script>

<template>
87
  <div class="note-edit-form current-note-edit-form">
88 89 90 91 92 93 94 95 96 97
    <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>
98 99 100 101 102 103 104
    <form class="edit-note common-note-form">
      <markdown-field
        :markdown-preview-url="markdownPreviewUrl"
        :markdown-docs="markdownDocsUrl"
        :addSpacingClasses="false">
        <textarea
          id="note-body"
F
Fatih Acet 已提交
105
          name="note[note]"
106
          class="note-textarea js-gfm-input js-autosize markdown-area js-note-text"
107 108
          data-supports-slash-commands="true"
          data-supports-quick-actions="true"
109 110 111 112 113
          aria-label="Description"
          v-model="note"
          ref="textarea"
          slot="textarea"
          placeholder="Write a comment or drag your files here..."
114
          @keydown.meta.enter="handleUpdate"
115
          @keydown.up="editMyLastNote"
116
          @keydown.esc="cancelHandler(true)">
117 118 119 120 121 122 123
        </textarea>
      </markdown-field>
      <div class="note-form-actions clearfix">
        <button
          @click="handleUpdate"
          type="button"
          class="btn btn-nr btn-save">
124
          {{saveButtonTitle}}
125 126
        </button>
        <button
127
          @click="cancelHandler"
128
          class="btn btn-nr btn-cancel note-edit-cancel"
129 130 131 132 133 134 135
          type="button">
          Cancel
        </button>
      </div>
    </form>
  </div>
</template>