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

description_form.vue « design_description « components « design_management « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 413442074f0534037d6bf2cbc6f5dffa621e2988 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<script>
import { GlButton, GlFormGroup, GlAlert, GlTooltipDirective } from '@gitlab/ui';
import SafeHtml from '~/vue_shared/directives/safe_html';
import { __, s__ } from '~/locale';
import { helpPagePath } from '~/helpers/help_page_helper';
import MarkdownEditor from '~/vue_shared/components/markdown/markdown_editor.vue';
import glFeaturesFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { renderGFM } from '~/behaviors/markdown/render_gfm';
import { toggleMarkCheckboxes } from '~/behaviors/markdown/utils';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { trackSavedUsingEditor } from '~/vue_shared/components/markdown/tracking';
import updateDesignDescriptionMutation from '../../graphql/mutations/update_design_description.mutation.graphql';
import { UPDATE_DESCRIPTION_ERROR } from '../../utils/error_messages';

const isCheckbox = (target) => target?.classList.contains('task-list-item-checkbox');

export default {
  components: {
    MarkdownEditor,
    GlAlert,
    GlButton,
    GlFormGroup,
  },
  directives: {
    SafeHtml,
    GlTooltip: GlTooltipDirective,
  },
  i18n: {
    edit: __('Edit'),
    editDescription: s__('DesignManagement|Edit description'),
    descriptionLabel: s__('DesignManagement|Design description'),
  },
  formFieldProps: {
    id: 'design-description',
    name: 'design-description',
    placeholder: s__('DesignManagement|Write a comment or drag your files here…'),
    'aria-label': s__('DesignManagement|Design description'),
  },
  mixins: [glFeaturesFlagMixin()],
  markdownDocsPath: helpPagePath('user/markdown'),
  quickActionsDocsPath: helpPagePath('user/project/quick_actions'),
  props: {
    design: {
      type: Object,
      required: true,
    },
    markdownPreviewPath: {
      type: String,
      required: true,
    },
    designVariables: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      descriptionText: this.design.description || '',
      showEditor: false,
      isSubmitting: false,
      errorMessage: '',
      autosaveKey: `Issue/${getIdFromGraphQLId(this.design.issue.id)}/Design/${getIdFromGraphQLId(
        this.design.id,
      )}`,
    };
  },
  computed: {
    canUpdate() {
      return this.design.issue?.userPermissions?.updateDesign && !this.showEditor;
    },
  },
  watch: {
    'design.descriptionHtml': {
      handler(newDescriptionHtml, oldDescriptionHtml) {
        if (newDescriptionHtml !== oldDescriptionHtml) {
          this.renderGFM();
        }
      },
      immediate: true,
    },
  },
  methods: {
    startEditing() {
      this.showEditor = true;
    },
    closeForm() {
      this.showEditor = false;
    },
    async renderGFM() {
      await this.$nextTick();
      renderGFM(this.$refs['gfm-content']);

      if (this.canUpdate) {
        const checkboxes = this.$el.querySelectorAll('.task-list-item-checkbox');

        // enable boxes, disabled by default in markdown
        checkboxes.forEach((checkbox) => {
          // eslint-disable-next-line no-param-reassign
          checkbox.disabled = false;
        });
      }
    },
    setDescriptionText(newText) {
      // Do not update when cmd+enter is executed
      if (!this.isSubmitting) {
        this.descriptionText = newText;
      }
    },
    async updateDesignDescription() {
      this.isSubmitting = true;

      if (this.$refs.markdownEditor) {
        // eslint-disable-next-line @gitlab/require-i18n-strings
        trackSavedUsingEditor(this.$refs.markdownEditor.isContentEditorActive, 'Design');
      }

      try {
        const designDescriptionInput = { description: this.descriptionText, id: this.design.id };

        await this.$apollo.mutate({
          mutation: updateDesignDescriptionMutation,
          variables: {
            input: designDescriptionInput,
          },
        });

        this.closeForm();
      } catch {
        this.errorMessage = UPDATE_DESCRIPTION_ERROR;
      } finally {
        this.isSubmitting = false;
      }
    },
    toggleCheckboxes(event) {
      const { target } = event;

      if (isCheckbox(target)) {
        target.disabled = true;

        const { sourcepos } = target.parentElement.dataset;

        if (!sourcepos) return;

        // Toggle checkboxes based on user input
        this.descriptionText = toggleMarkCheckboxes({
          rawMarkdown: this.descriptionText,
          checkboxChecked: target.checked,
          sourcepos,
        });

        // Update the desciption text using mutation
        this.updateDesignDescription();
      }
    },
  },
};
</script>

<template>
  <div class="design-description-container">
    <gl-form-group
      v-if="showEditor"
      class="design-description-form common-note-form"
      :label="$options.i18n.descriptionLabel"
    >
      <div v-if="errorMessage" class="gl-pb-3">
        <gl-alert variant="danger" @dismiss="errorMessage = null">
          {{ errorMessage }}
        </gl-alert>
      </div>
      <markdown-editor
        ref="markdownEditor"
        :value="descriptionText"
        :render-markdown-path="markdownPreviewPath"
        :markdown-docs-path="$options.markdownDocsPath"
        :form-field-props="$options.formFieldProps"
        :enable-content-editor="Boolean(glFeatures.contentEditorOnIssues)"
        :quick-actions-docs-path="$options.quickActionsDocsPath"
        :autosave-key="autosaveKey"
        enable-autocomplete
        :supports-quick-actions="false"
        autofocus
        @input="setDescriptionText"
        @keydown.meta.enter="updateDesignDescription"
        @keydown.ctrl.enter="updateDesignDescription"
        @keydown.exact.esc.stop="closeForm"
      />
      <div class="gl-display-flex gl-mt-3">
        <gl-button
          category="primary"
          variant="confirm"
          :loading="isSubmitting"
          data-testid="save-description"
          @click="updateDesignDescription"
          >{{ s__('DesignManagement|Save') }}
        </gl-button>
        <gl-button category="tertiary" class="gl-ml-3" data-testid="cancel" @click="closeForm"
          >{{ s__('DesignManagement|Cancel') }}
        </gl-button>
      </div>
    </gl-form-group>
    <div v-else class="design-description-view">
      <div
        class="design-description-header gl-display-flex gl-justify-content-space-between gl-mb-2"
      >
        <label class="gl-m-0">
          {{ $options.i18n.descriptionLabel }}
        </label>
        <gl-button
          v-if="canUpdate"
          v-gl-tooltip
          class="gl-ml-auto"
          size="small"
          data-testid="edit-description"
          :aria-label="$options.i18n.editDescription"
          @click="startEditing"
        >
          {{ $options.i18n.edit }}
        </gl-button>
      </div>
      <div
        v-if="!design.descriptionHtml"
        data-testid="design-description-none"
        class="gl-text-secondary gl-mb-5"
      >
        {{ s__('DesignManagement|None') }}
      </div>
      <div v-else class="design-description js-task-list-container">
        <div
          ref="gfm-content"
          v-safe-html="design.descriptionHtml"
          class="md gl-mb-4"
          data-testid="design-description-content"
          @change="toggleCheckboxes"
        ></div>
      </div>
    </div>
  </div>
</template>