summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/toolbar_link_button.vue
blob: f706080eaa139ea509e0f98b3742c10d56df6545 (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
<script>
import {
  GlDropdown,
  GlDropdownForm,
  GlButton,
  GlFormInputGroup,
  GlDropdownDivider,
  GlDropdownItem,
  GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import { Editor as TiptapEditor } from '@tiptap/vue-2';
import { hasSelection } from '../services/utils';

export const linkContentType = 'link';

export default {
  components: {
    GlDropdown,
    GlDropdownForm,
    GlFormInputGroup,
    GlDropdownDivider,
    GlDropdownItem,
    GlButton,
  },
  directives: {
    GlTooltip,
  },
  props: {
    tiptapEditor: {
      type: TiptapEditor,
      required: true,
    },
  },
  data() {
    return {
      linkHref: '',
    };
  },
  computed: {
    isActive() {
      return this.tiptapEditor.isActive(linkContentType);
    },
  },
  mounted() {
    this.tiptapEditor.on('selectionUpdate', ({ editor }) => {
      const { href } = editor.getAttributes(linkContentType);

      this.linkHref = href;
    });
  },
  methods: {
    updateLink() {
      this.tiptapEditor.chain().focus().unsetLink().setLink({ href: this.linkHref }).run();

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

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

      this.$emit('execute', { contentType: linkContentType });
    },
  },
};
</script>
<template>
  <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 v-if="isActive" />
    <gl-dropdown-item v-if="isActive" @click="removeLink()">
      {{ __('Remove link') }}
    </gl-dropdown-item>
  </gl-dropdown>
</template>