summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/components/note_body.vue
blob: ac4e1ffe53a9d0f9016cd31cc603e83558938bf2 (plain)
1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<script>
  import noteEditedText from './note_edited_text.vue';
  import noteAwardsList from './note_awards_list.vue';
  import noteAttachment from './note_attachment.vue';
  import noteForm from './note_form.vue';
  import TaskList from '../../task_list';
  import autosave from '../mixins/autosave';

  export default {
    props: {
      note: {
        type: Object,
        required: true,
      },
      canEdit: {
        type: Boolean,
        required: true,
      },
      isEditing: {
        type: Boolean,
        required: false,
        default: false,
      },
    },
    mixins: [
      autosave,
    ],
    components: {
      noteEditedText,
      noteAwardsList,
      noteAttachment,
      noteForm,
    },
    computed: {
      noteBody() {
        return this.note.note;
      },
    },
    methods: {
      renderGFM() {
        $(this.$refs['note-body']).renderGFM();
      },
      initTaskList() {
        if (this.canEdit) {
          this.taskList = new TaskList({
            dataType: 'note',
            fieldName: 'note',
            selector: '.notes',
          });
        }
      },
      handleFormUpdate(note, parentElement, callback) {
        this.$emit('handleFormUpdate', note, parentElement, callback);
      },
      formCancelHandler(shouldConfirm, isDirty) {
        this.$emit('cancelFormEdition', shouldConfirm, isDirty);
      },
    },
    mounted() {
      this.renderGFM();
      this.initTaskList();

      if (this.isEditing) {
        this.initAutoSave();
      }
    },
    updated() {
      this.initTaskList();
      this.renderGFM();

      if (this.isEditing) {
        if (!this.autosave) {
          this.initAutoSave();
        } else {
          this.setAutoSave();
        }
      }
    },
  };
</script>

<template>
  <div
    :class="{ 'js-task-list-container': canEdit }"
    ref="note-body"
    class="note-body">
    <div
      v-html="note.note_html"
      class="note-text md"></div>
    <note-form
      v-if="isEditing"
      ref="noteForm"
      @handleFormUpdate="handleFormUpdate"
      @cancelFormEdition="formCancelHandler"
      :is-editing="isEditing"
      :note-body="noteBody"
      :note-id="note.id"
      />
    <textarea
      v-if="canEdit"
      v-model="note.note"
      :data-update-url="note.path"
      class="hidden js-task-list-field"></textarea>
    <note-edited-text
      v-if="note.last_edited_at"
      :edited-at="note.last_edited_at"
      :edited-by="note.last_edited_by"
      action-text="Edited"
      />
    <note-awards-list
      v-if="note.award_emoji.length"
      :note-id="note.id"
      :note-author-id="note.author.id"
      :awards="note.award_emoji"
      :toggle-award-path="note.toggle_award_path"
      />
    <note-attachment
      v-if="note.attachment"
      :attachment="note.attachment"
      />
  </div>
</template>