summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/rich_content_editor/modals/add_image_modal.vue
blob: 400630659260ffa3151fe32187634e5be391cce2 (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
<script>
import { isSafeURL } from '~/lib/utils/url_utility';
import { GlModal, GlFormGroup, GlFormInput } from '@gitlab/ui';
import { __ } from '~/locale';

export default {
  components: {
    GlModal,
    GlFormGroup,
    GlFormInput,
  },
  data() {
    return {
      error: null,
      imageUrl: null,
      altText: null,
      modalTitle: __('Image Details'),
      okTitle: __('Insert'),
      urlLabel: __('Image URL'),
      descriptionLabel: __('Description'),
    };
  },
  methods: {
    show() {
      this.error = null;
      this.imageUrl = null;
      this.altText = null;

      this.$refs.modal.show();
    },
    onOk(event) {
      if (!this.isValid()) {
        event.preventDefault();
        return;
      }

      const { imageUrl, altText } = this;

      this.$emit('addImage', { imageUrl, altText: altText || __('image') });
    },
    isValid() {
      if (!isSafeURL(this.imageUrl)) {
        this.error = __('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="modalTitle"
    :ok-title="okTitle"
    @ok="onOk"
  >
    <gl-form-group
      :label="urlLabel"
      label-for="url-input"
      :state="!Boolean(error)"
      :invalid-feedback="error"
    >
      <gl-form-input id="url-input" ref="urlInput" v-model="imageUrl" />
    </gl-form-group>

    <gl-form-group :label="descriptionLabel" label-for="description-input">
      <gl-form-input id="description-input" ref="descriptionInput" v-model="altText" />
    </gl-form-group>
  </gl-modal>
</template>