summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/bubble_menus/code_block.vue
blob: 210f259b20ff4e517c781c5a1d15d6745182b4b6 (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
<script>
import {
  GlButton,
  GlButtonGroup,
  GlDropdown,
  GlDropdownItem,
  GlSearchBoxByType,
  GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import { BubbleMenu } from '@tiptap/vue-2';
import { getParentByTagName } from '~/lib/utils/dom_utils';
import codeBlockLanguageLoader from '../../services/code_block_language_loader';
import CodeBlockHighlight from '../../extensions/code_block_highlight';
import Diagram from '../../extensions/diagram';
import Frontmatter from '../../extensions/frontmatter';
import EditorStateObserver from '../editor_state_observer.vue';

const CODE_BLOCK_NODE_TYPES = [CodeBlockHighlight.name, Diagram.name, Frontmatter.name];

export default {
  components: {
    BubbleMenu,
    GlButton,
    GlButtonGroup,
    GlDropdown,
    GlDropdownItem,
    GlSearchBoxByType,
    EditorStateObserver,
  },
  directives: {
    GlTooltip,
  },
  inject: ['tiptapEditor'],
  data() {
    return {
      codeBlockType: undefined,
      selectedLanguage: {},
      filterTerm: '',
      filteredLanguages: [],
    };
  },
  watch: {
    filterTerm: {
      handler(val) {
        this.filteredLanguages = codeBlockLanguageLoader.filterLanguages(val);
      },
      immediate: true,
    },
  },
  methods: {
    shouldShow: ({ editor }) => {
      return CODE_BLOCK_NODE_TYPES.some((type) => editor.isActive(type));
    },

    updateSelectedLanguage() {
      this.codeBlockType = CODE_BLOCK_NODE_TYPES.find((type) => this.tiptapEditor.isActive(type));

      if (this.codeBlockType) {
        const { language } = this.tiptapEditor.getAttributes(this.codeBlockType);
        this.selectedLanguage = codeBlockLanguageLoader.findLanguageBySyntax(language);
      }
    },

    copyCodeBlockText() {
      const { view } = this.tiptapEditor;
      const { from } = this.tiptapEditor.state.selection;
      const node = getParentByTagName(view.domAtPos(from).node, 'pre');

      navigator.clipboard.writeText(node?.textContent || '');
    },

    async applySelectedLanguage(language) {
      this.selectedLanguage = language;

      await codeBlockLanguageLoader.loadLanguages([language.syntax]);

      this.tiptapEditor.commands.setCodeBlock({ language: this.selectedLanguage.syntax });
    },

    getReferenceClientRect() {
      const { view } = this.tiptapEditor;
      const { from } = this.tiptapEditor.state.selection;
      const node = getParentByTagName(view.domAtPos(from).node, 'pre');
      return node?.getBoundingClientRect() || new DOMRect(-1000, -1000, 0, 0);
    },

    deleteCodeBlock() {
      this.tiptapEditor.chain().focus().deleteNode(this.codeBlockType).run();
    },
  },
};
</script>
<template>
  <bubble-menu
    data-testid="code-block-bubble-menu"
    class="gl-shadow gl-rounded-base gl-bg-white"
    :editor="tiptapEditor"
    plugin-key="bubbleMenuCodeBlock"
    :should-show="shouldShow"
    :tippy-options="/* eslint-disable @gitlab/vue-no-new-non-primitive-in-template */ {
      getReferenceClientRect,
    } /* eslint-enable @gitlab/vue-no-new-non-primitive-in-template */"
  >
    <editor-state-observer @transaction="updateSelectedLanguage">
      <gl-button-group>
        <gl-dropdown
          category="tertiary"
          contenteditable="false"
          boundary="viewport"
          :text="selectedLanguage.label"
        >
          <template #header>
            <gl-search-box-by-type
              v-model="filterTerm"
              :clear-button-title="__('Clear')"
              :placeholder="__('Search')"
            />
          </template>

          <template #highlighted-items>
            <gl-dropdown-item :key="selectedLanguage.syntax" is-check-item :is-checked="true">
              {{ selectedLanguage.label }}
            </gl-dropdown-item>
          </template>

          <gl-dropdown-item
            v-for="language in filteredLanguages"
            v-show="selectedLanguage.syntax !== language.syntax"
            :key="language.syntax"
            @click="applySelectedLanguage(language)"
          >
            {{ language.label }}
          </gl-dropdown-item>
        </gl-dropdown>
        <gl-button
          v-gl-tooltip
          variant="default"
          category="tertiary"
          size="medium"
          data-testid="copy-code-block"
          :aria-label="__('Copy code')"
          :title="__('Copy code')"
          icon="copy-to-clipboard"
          @click="copyCodeBlockText"
        />
        <gl-button
          v-gl-tooltip
          variant="default"
          category="tertiary"
          size="medium"
          data-testid="delete-code-block"
          :aria-label="__('Delete code block')"
          :title="__('Delete code block')"
          icon="remove"
          @click="deleteCodeBlock"
        />
      </gl-button-group>
    </editor-state-observer>
  </bubble-menu>
</template>