summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/toolbar_text_style_dropdown.vue
blob: 473fc472c1b52193829b2da66b13b0622858c6c9 (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
<script>
import { GlDropdown, GlDropdownItem, GlTooltipDirective as GlTooltip } from '@gitlab/ui';
import { Editor as TiptapEditor } from '@tiptap/vue-2';
import { __ } from '~/locale';
import { TEXT_STYLE_DROPDOWN_ITEMS } from '../constants';

export default {
  components: {
    GlDropdown,
    GlDropdownItem,
  },
  directives: {
    GlTooltip,
  },
  props: {
    tiptapEditor: {
      type: TiptapEditor,
      required: true,
    },
  },
  computed: {
    activeItem() {
      return TEXT_STYLE_DROPDOWN_ITEMS.find((item) =>
        this.tiptapEditor.isActive(item.contentType, item.commandParams),
      );
    },
    activeItemLabel() {
      const { activeItem } = this;

      return activeItem ? activeItem.label : this.$options.i18n.placeholder;
    },
  },
  methods: {
    execute(item) {
      const { editorCommand, contentType, commandParams } = item;
      const value = commandParams?.level;

      if (editorCommand) {
        this.tiptapEditor
          .chain()
          .focus()
          [editorCommand](commandParams || {})
          .run();
      }

      this.$emit('execute', { contentType, value });
    },
    isActive(item) {
      return this.tiptapEditor.isActive(item.contentType, item.commandParams);
    },
  },
  items: TEXT_STYLE_DROPDOWN_ITEMS,
  i18n: {
    placeholder: __('Text style'),
  },
};
</script>
<template>
  <gl-dropdown
    v-gl-tooltip="$options.i18n.placeholder"
    size="small"
    :disabled="!activeItem"
    :text="activeItemLabel"
  >
    <gl-dropdown-item
      v-for="(item, index) in $options.items"
      :key="index"
      is-check-item
      :is-checked="isActive(item)"
      @click="execute(item)"
    >
      {{ item.label }}
    </gl-dropdown-item>
  </gl-dropdown>
</template>