summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/toolbar_attachment_button.vue
blob: 1f18090e7d74c0151abe61fcaf7eda83cba9e57c (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
<script>
import { GlButton, GlTooltipDirective as GlTooltip } from '@gitlab/ui';
import Link from '../extensions/link';

export default {
  components: {
    GlButton,
  },
  directives: {
    GlTooltip,
  },
  inject: ['tiptapEditor'],
  data() {
    return {
      linkHref: '',
    };
  },
  methods: {
    emitExecute(source = 'url') {
      this.$emit('execute', { contentType: Link.name, value: source });
    },
    openFileUpload() {
      this.$refs.fileSelector.click();
    },
    onFileSelect(e) {
      this.tiptapEditor
        .chain()
        .focus()
        .uploadAttachment({
          file: e.target.files[0],
        })
        .run();

      // Reset the file input so that the same file can be uploaded again
      this.$refs.fileSelector.value = '';
      this.emitExecute('upload');
    },
  },
};
</script>
<template>
  <span class="gl-display-inline-flex">
    <gl-button
      v-gl-tooltip
      :text="__('Attach a file or image')"
      :title="__('Attach a file or image')"
      category="tertiary"
      icon="paperclip"
      size="small"
      lazy
      @click="openFileUpload"
    />
    <input
      ref="fileSelector"
      type="file"
      name="content_editor_image"
      class="gl-display-none"
      data-qa-selector="file_upload_field"
      @change="onFileSelect"
    />
  </span>
</template>