issue_note_form.vue 3.8 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
  export default {
    props: {
      noteBody: {
        type: String,
        required: false,
        default: '',
      },
      noteId: {
        type: Number,
        required: false,
      },
      saveButtonTitle: {
        type: String,
        required: false,
        default: 'Save comment',
      },
21 22 23 24
      discussion: {
        type: Array,
        required: false,
      }
25
    },
26
    data() {
27 28
      const { getIssueData, getNotesData } =  this.$store.getters;

29 30 31
      return {
        initialNote: this.noteBody,
        note: this.noteBody,
32 33
        markdownPreviewUrl: getIssueData.preview_note_path,
        markdownDocsUrl: getNotesData.markdownDocs,
34 35
        conflictWhileEditing: false,
      };
36
    },
37 38
    components: {
      markdownField,
39
    },
40 41 42 43
    computed: {
      noteHash() {
        return `#note_${this.noteId}`;
      },
44
    },
45 46
    methods: {
      handleUpdate() {
47
        this.$emit('handleFormUpdate', note);
48 49 50
      },
      editMyLastNote() {
        if (this.note === '') {
51 52
          // TODO: HANDLE THIS WITHOUTH JQUERY OR QUERYING THE DOM
          // FIND the discussion we are in and the last comment on that discussion
53 54
          const discussion = $(this.$el).closest('.discussion-notes');
          const myLastNoteId = discussion.find('.js-my-note').last().attr('id');
55

56 57 58
          debugger;
          const lastNoteInDiscussion = this.$store.getters.getDiscussionLastNote(this.discussion);

59 60 61 62 63
          if (myLastNoteId) {
            eventHub.$emit('enterEditMode', {
              noteId: parseInt(myLastNoteId.replace('note_', ''), 10),
            });
          }
64
        }
65
      },
66 67 68 69
      cancelHandler(shouldConfirm = false) {
        // Sends information about confirm message and if the textarea has changed
        this.$emit('cancelFormEdition', shouldConfirm, this.initialNote !== this.note);
      }
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
    <form
      @submit="handleUpdate"
      class="edit-note common-note-form">
101 102 103 104 105 106
      <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
        </textarea>
      </markdown-field>
      <div class="note-form-actions clearfix">
        <button
123
          type="submit"
124
          class="btn btn-nr btn-save">
125
          {{saveButtonTitle}}
126 127
        </button>
        <button
128
          @click="cancelHandler()"
129
          class="btn btn-nr btn-cancel note-edit-cancel"
130 131 132 133 134 135 136
          type="button">
          Cancel
        </button>
      </div>
    </form>
  </div>
</template>