summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/toolbar_link_button.vue
blob: ff525e528734b25b8b27d95fc99047539390482f (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 {
  GlDropdown,
  GlDropdownForm,
  GlButton,
  GlFormInputGroup,
  GlDropdownDivider,
  GlDropdownItem,
  GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import Link from '../extensions/link';
import { hasSelection } from '../services/utils';
import EditorStateObserver from './editor_state_observer.vue';

export default {
  components: {
    GlDropdown,
    GlDropdownForm,
    GlFormInputGroup,
    GlDropdownDivider,
    GlDropdownItem,
    GlButton,
    EditorStateObserver,
  },
  directives: {
    GlTooltip,
  },
  inject: ['tiptapEditor'],
  data() {
    return {
      linkHref: '',
      isActive: false,
    };
  },
  methods: {
    resetFields() {
      this.imgSrc = '';
      this.$refs.fileSelector.value = '';
    },
    openFileUpload() {
      this.$refs.fileSelector.click();
    },
    updateLinkState({ editor }) {
      const { canonicalSrc, href } = editor.getAttributes(Link.name);

      this.isActive = editor.isActive(Link.name);
      this.linkHref = canonicalSrc || href;
    },
    updateLink() {
      this.tiptapEditor
        .chain()
        .focus()
        .unsetLink()
        .setLink({
          href: this.linkHref,
          canonicalSrc: this.linkHref,
        })
        .run();

      this.$emit('execute', { contentType: Link.name });
    },
    selectLink() {
      const { tiptapEditor } = this;

      // a selection has already been made by the user, so do nothing
      if (!hasSelection(tiptapEditor)) {
        tiptapEditor.chain().focus().extendMarkRange(Link.name).run();
      }
    },
    removeLink() {
      this.tiptapEditor.chain().focus().unsetLink().run();

      this.$emit('execute', { contentType: Link.name });
    },
    onFileSelect(e) {
      this.tiptapEditor
        .chain()
        .focus()
        .uploadAttachment({
          file: e.target.files[0],
        })
        .run();

      this.resetFields();
      this.$emit('execute', { contentType: Link.name });
    },
  },
};
</script>
<template>
  <editor-state-observer @transaction="updateLinkState">
    <gl-dropdown
      v-gl-tooltip
      :aria-label="__('Insert link')"
      :title="__('Insert link')"
      :toggle-class="{ active: isActive }"
      size="small"
      category="tertiary"
      icon="link"
      @show="selectLink()"
    >
      <gl-dropdown-form class="gl-px-3!">
        <gl-form-input-group v-model="linkHref" :placeholder="__('Link URL')">
          <template #append>
            <gl-button variant="confirm" @click="updateLink">{{ __('Apply') }}</gl-button>
          </template>
        </gl-form-input-group>
      </gl-dropdown-form>
      <gl-dropdown-divider />
      <gl-dropdown-item v-if="isActive" @click="removeLink">
        {{ __('Remove link') }}
      </gl-dropdown-item>
      <gl-dropdown-item v-else @click="openFileUpload">
        {{ __('Upload file') }}
      </gl-dropdown-item>

      <input
        ref="fileSelector"
        type="file"
        name="content_editor_attachment"
        class="gl-display-none"
        @change="onFileSelect"
      />
    </gl-dropdown>
  </editor-state-observer>
</template>