summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/rich_content_editor/modals/add_image/add_image_modal.vue
blob: e1652f5498210e32dca1132517df0162f983e40d (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
<script>
import { GlModal, GlFormGroup, GlFormInput, GlTabs, GlTab } from '@gitlab/ui';
import { isSafeURL } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { IMAGE_TABS } from '../../constants';
import UploadImageTab from './upload_image_tab.vue';

export default {
  components: {
    UploadImageTab,
    GlModal,
    GlFormGroup,
    GlFormInput,
    GlTabs,
    GlTab,
  },
  mixins: [glFeatureFlagMixin()],
  props: {
    imageRoot: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      file: null,
      urlError: null,
      imageUrl: null,
      description: null,
      tabIndex: IMAGE_TABS.UPLOAD_TAB,
      uploadImageTab: null,
    };
  },
  modalTitle: __('Image details'),
  okTitle: __('Insert image'),
  urlTabTitle: __('By URL'),
  urlLabel: __('Image URL'),
  descriptionLabel: __('Description'),
  uploadTabTitle: __('Upload file'),
  computed: {
    altText() {
      return this.description;
    },
  },
  methods: {
    show() {
      this.file = null;
      this.urlError = null;
      this.imageUrl = null;
      this.description = null;
      this.tabIndex = IMAGE_TABS.UPLOAD_TAB;

      this.$refs.modal.show();
    },
    onOk(event) {
      if (this.glFeatures.sseImageUploads && this.tabIndex === IMAGE_TABS.UPLOAD_TAB) {
        this.submitFile(event);
        return;
      }
      this.submitURL(event);
    },
    setFile(file) {
      this.file = file;
    },
    submitFile(event) {
      const { file, altText } = this;
      const { uploadImageTab } = this.$refs;

      uploadImageTab.validateFile();

      if (uploadImageTab.fileError) {
        event.preventDefault();
        return;
      }

      const imageUrl = `${this.imageRoot}${file.name}`;

      this.$emit('addImage', { imageUrl, file, altText: altText || file.name });
    },
    submitURL(event) {
      if (!this.validateUrl()) {
        event.preventDefault();
        return;
      }

      const { imageUrl, altText } = this;

      this.$emit('addImage', { imageUrl, altText: altText || imageUrl });
    },
    validateUrl() {
      if (!isSafeURL(this.imageUrl)) {
        this.urlError = __('Please provide a valid URL');
        this.$refs.urlInput.$el.focus();
        return false;
      }

      return true;
    },
  },
};
</script>
<template>
  <gl-modal
    ref="modal"
    modal-id="add-image-modal"
    :title="$options.modalTitle"
    :ok-title="$options.okTitle"
    @ok="onOk"
  >
    <gl-tabs v-if="glFeatures.sseImageUploads" v-model="tabIndex">
      <!-- Upload file Tab -->
      <gl-tab :title="$options.uploadTabTitle">
        <upload-image-tab ref="uploadImageTab" @input="setFile" />
      </gl-tab>

      <!-- By URL Tab -->
      <gl-tab :title="$options.urlTabTitle">
        <gl-form-group
          class="gl-mt-5 gl-mb-3"
          :label="$options.urlLabel"
          label-for="url-input"
          :state="!Boolean(urlError)"
          :invalid-feedback="urlError"
        >
          <gl-form-input id="url-input" ref="urlInput" v-model="imageUrl" />
        </gl-form-group>
      </gl-tab>
    </gl-tabs>

    <gl-form-group
      v-else
      class="gl-mt-5 gl-mb-3"
      :label="$options.urlLabel"
      label-for="url-input"
      :state="!Boolean(urlError)"
      :invalid-feedback="urlError"
    >
      <gl-form-input id="url-input" ref="urlInput" v-model="imageUrl" />
    </gl-form-group>

    <!-- Description Input -->
    <gl-form-group :label="$options.descriptionLabel" label-for="description-input">
      <gl-form-input id="description-input" ref="descriptionInput" v-model="description" />
    </gl-form-group>
  </gl-modal>
</template>