Welcome to mirror list, hosted at ThFree Co, Russian Federation.

link.vue « bubble_menus « components « content_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dae0bc63b5adca4ffbc6a75dd389822ee7cfec19 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<script>
import {
  GlLink,
  GlForm,
  GlFormGroup,
  GlFormInput,
  GlButton,
  GlButtonGroup,
  GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import { BubbleMenu } from '@tiptap/vue-2';
import Link from '../../extensions/link';
import EditorStateObserver from '../editor_state_observer.vue';

export default {
  components: {
    BubbleMenu,
    GlForm,
    GlFormGroup,
    GlFormInput,
    GlLink,
    GlButton,
    GlButtonGroup,
    EditorStateObserver,
  },
  directives: {
    GlTooltip,
  },
  inject: ['tiptapEditor', 'contentEditor'],
  data() {
    return {
      linkHref: undefined,
      linkCanonicalSrc: undefined,
      linkTitle: undefined,

      isEditing: false,
    };
  },
  watch: {
    linkCanonicalSrc(value) {
      if (!value) this.isEditing = true;
    },
  },
  methods: {
    shouldShow() {
      const shouldShow = this.tiptapEditor.isActive(Link.name);

      if (!shouldShow) this.isEditing = false;

      return shouldShow;
    },

    startEditingLink() {
      // select the entire link
      this.tiptapEditor.chain().focus().extendMarkRange(Link.name).run();

      this.isEditing = true;
    },

    async endEditingLink() {
      this.isEditing = false;

      this.linkHref = await this.contentEditor.resolveUrl(this.linkCanonicalSrc);

      if (!this.linkCanonicalSrc && !this.linkHref) {
        this.removeLink();
      }
    },

    cancelEditingLink() {
      this.endEditingLink();
      this.updateLinkToState();
    },

    async saveEditedLink() {
      if (!this.linkCanonicalSrc) {
        this.removeLink();
      } else {
        this.tiptapEditor
          .chain()
          .focus()
          .extendMarkRange(Link.name)
          .updateAttributes(Link.name, {
            href: this.linkCanonicalSrc,
            canonicalSrc: this.linkCanonicalSrc,
            title: this.linkTitle,
          })
          .run();
      }

      this.endEditingLink();
    },

    updateLinkToState() {
      if (!this.tiptapEditor.isActive(Link.name)) return;

      const { href, title, canonicalSrc } = this.tiptapEditor.getAttributes(Link.name);

      this.linkTitle = title;
      this.linkHref = href;
      this.linkCanonicalSrc = canonicalSrc || href;
    },

    copyLinkHref() {
      navigator.clipboard.writeText(this.linkCanonicalSrc);
    },

    removeLink() {
      this.tiptapEditor.chain().focus().extendMarkRange(Link.name).unsetLink().run();
    },
  },
};
</script>
<template>
  <bubble-menu
    data-testid="link-bubble-menu"
    class="gl-shadow gl-rounded-base gl-bg-white"
    :editor="tiptapEditor"
    plugin-key="bubbleMenuLink"
    :should-show="() => shouldShow()"
    :tippy-options="/* eslint-disable @gitlab/vue-no-new-non-primitive-in-template */ {
      placement: 'bottom',
    } /* eslint-enable @gitlab/vue-no-new-non-primitive-in-template */"
  >
    <editor-state-observer @transaction="updateLinkToState">
      <gl-button-group v-if="!isEditing" class="gl-display-flex gl-align-items-center">
        <gl-link
          v-gl-tooltip
          :href="linkHref"
          :aria-label="linkCanonicalSrc"
          :title="linkCanonicalSrc"
          target="_blank"
          class="gl-px-3 gl-overflow-hidden gl-white-space-nowrap gl-text-overflow-ellipsis"
        >
          {{ linkCanonicalSrc }}
        </gl-link>
        <gl-button
          v-gl-tooltip
          variant="default"
          category="tertiary"
          size="medium"
          data-testid="copy-link-url"
          :aria-label="__('Copy link URL')"
          :title="__('Copy link URL')"
          icon="copy-to-clipboard"
          @click="copyLinkHref"
        />
        <gl-button
          v-gl-tooltip
          variant="default"
          category="tertiary"
          size="medium"
          data-testid="edit-link"
          :aria-label="__('Edit link')"
          :title="__('Edit link')"
          icon="pencil"
          @click="startEditingLink"
        />
        <gl-button
          v-gl-tooltip
          variant="default"
          category="tertiary"
          size="medium"
          data-testid="remove-link"
          :aria-label="__('Remove link')"
          :title="__('Remove link')"
          icon="unlink"
          @click="removeLink"
        />
      </gl-button-group>
      <gl-form v-else class="bubble-menu-form gl-p-4 gl-w-100" @submit.prevent="saveEditedLink">
        <gl-form-group :label="__('URL')" label-for="link-href">
          <gl-form-input id="link-href" v-model="linkCanonicalSrc" data-testid="link-href" />
        </gl-form-group>
        <gl-form-group :label="__('Title')" label-for="link-title">
          <gl-form-input id="link-title" v-model="linkTitle" data-testid="link-title" />
        </gl-form-group>
        <div class="gl-display-flex gl-justify-content-end">
          <gl-button class="gl-mr-3" data-testid="cancel-link" @click="cancelEditingLink">
            {{ __('Cancel') }}
          </gl-button>
          <gl-button variant="confirm" type="submit">
            {{ __('Apply') }}
          </gl-button>
        </div>
      </gl-form>
    </editor-state-observer>
  </bubble-menu>
</template>