issue_note_form.vue 3.6 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 31 32 33 34 35 36 37
    data() {
      return {
        initialNote: this.noteBody,
        note: this.noteBody,
        markdownPreviewUrl: gl.issueData.preview_note_path,
        markdownDocsUrl: '',
        conflictWhileEditing: false,
      };
38
    },
39 40
    components: {
      markdownField,
41
    },
42 43 44 45 46 47 48
    computed: {
      isDirty() {
        return this.initialNote !== this.note;
      },
      noteHash() {
        return `#note_${this.noteId}`;
      },
49
    },
50 51 52 53 54 55 56 57 58 59
    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');
60

61 62 63 64 65
          if (myLastNoteId) {
            eventHub.$emit('enterEditMode', {
              noteId: parseInt(myLastNoteId.replace('note_', ''), 10),
            });
          }
66
        }
67
      },
68
    },
69 70 71
    mounted() {
      const issuableDataEl = document.getElementById('js-issuable-app-initial-data');
      const issueData = JSON.parse(issuableDataEl.innerHTML.replace(/&quot;/g, '"'));
72

73 74 75 76 77 78 79 80 81 82 83
      this.markdownDocsUrl = issueData.markdownDocs;
      this.$refs.textarea.focus();
    },
    watch: {
      noteBody() {
        if (this.note === this.initialNote) {
          this.note = this.noteBody;
        } else {
          this.conflictWhileEditing = true;
        }
      },
84
    },
85
  };
86 87 88
</script>

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