Welcome to mirror list, hosted at ThFree Co, Russian Federation.

work_item_comment_form.vue « notes « components « work_items « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd407fd9d9f4890b7d86d970463c8fd02e68eefe (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
123
124
125
126
<script>
import { GlButton } from '@gitlab/ui';
import { helpPagePath } from '~/helpers/help_page_helper';
import { s__, __ } from '~/locale';
import { joinPaths } from '~/lib/utils/url_utility';
import { getDraft, clearDraft, updateDraft } from '~/lib/utils/autosave';
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
import MarkdownEditor from '~/vue_shared/components/markdown/markdown_editor.vue';

export default {
  constantOptions: {
    markdownDocsPath: helpPagePath('user/markdown'),
  },
  components: {
    GlButton,
    MarkdownEditor,
  },
  inject: ['fullPath'],
  props: {
    workItemType: {
      type: String,
      required: true,
    },
    ariaLabel: {
      type: String,
      required: true,
    },
    autosaveKey: {
      type: String,
      required: true,
    },
    isSubmitting: {
      type: Boolean,
      required: false,
      default: false,
    },
    initialValue: {
      type: String,
      required: false,
      default: '',
    },
    commentButtonText: {
      type: String,
      required: false,
      default: __('Comment'),
    },
  },
  data() {
    return {
      commentText: getDraft(this.autosaveKey) || this.initialValue || '',
    };
  },
  computed: {
    markdownPreviewPath() {
      return joinPaths(
        '/',
        gon.relative_url_root || '',
        this.fullPath,
        `/preview_markdown?target_type=${this.workItemType}`,
      );
    },
    formFieldProps() {
      return {
        'aria-label': this.ariaLabel,
        placeholder: __('Write a comment or drag your files here…'),
        id: 'work-item-add-or-edit-comment',
        name: 'work-item-add-or-edit-comment',
      };
    },
  },
  methods: {
    setCommentText(newText) {
      this.commentText = newText;
      updateDraft(this.autosaveKey, this.commentText);
    },
    async cancelEditing() {
      if (this.commentText && this.commentText !== this.initialValue) {
        const msg = s__('WorkItem|Are you sure you want to cancel editing?');

        const confirmed = await confirmAction(msg, {
          primaryBtnText: __('Discard changes'),
          cancelBtnText: __('Continue editing'),
          primaryBtnVariant: 'danger',
        });

        if (!confirmed) {
          return;
        }
      }

      this.$emit('cancelEditing');
      clearDraft(this.autosaveKey);
    },
  },
};
</script>

<template>
  <form class="common-note-form gfm-form js-main-target-form gl-flex-grow-1">
    <markdown-editor
      :value="commentText"
      :render-markdown-path="markdownPreviewPath"
      :markdown-docs-path="$options.constantOptions.markdownDocsPath"
      :form-field-props="formFieldProps"
      data-testid="work-item-add-comment"
      class="gl-mb-3"
      autofocus
      use-bottom-toolbar
      @input="setCommentText"
      @keydown.meta.enter="$emit('submitForm', commentText)"
      @keydown.ctrl.enter="$emit('submitForm', commentText)"
      @keydown.esc.stop="cancelEditing"
    />
    <gl-button
      category="primary"
      variant="confirm"
      data-testid="confirm-button"
      :loading="isSubmitting"
      @click="$emit('submitForm', commentText)"
      >{{ commentButtonText }}
    </gl-button>
    <gl-button data-testid="cancel-button" category="primary" class="gl-ml-3" @click="cancelEditing"
      >{{ __('Cancel') }}
    </gl-button>
  </form>
</template>