summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/toolbar_table_button.vue
blob: 49d3006e9bf0d1d2f43dc1e127b628a369c8be9c (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
<script>
import { GlDropdown, GlDropdownDivider, GlDropdownForm, GlButton } from '@gitlab/ui';
import { Editor as TiptapEditor } from '@tiptap/vue-2';
import { __, sprintf } from '~/locale';
import { clamp } from '../services/utils';

export const tableContentType = 'table';

const MIN_ROWS = 3;
const MIN_COLS = 3;
const MAX_ROWS = 8;
const MAX_COLS = 8;

export default {
  components: {
    GlDropdown,
    GlDropdownDivider,
    GlDropdownForm,
    GlButton,
  },
  props: {
    tiptapEditor: {
      type: TiptapEditor,
      required: true,
    },
  },
  data() {
    return {
      maxRows: MIN_ROWS,
      maxCols: MIN_COLS,
      rows: 1,
      cols: 1,
    };
  },
  methods: {
    list(n) {
      return new Array(n).fill().map((_, i) => i + 1);
    },
    setRowsAndCols(rows, cols) {
      this.rows = rows;
      this.cols = cols;
      this.maxRows = clamp(rows + 1, MIN_ROWS, MAX_ROWS);
      this.maxCols = clamp(cols + 1, MIN_COLS, MAX_COLS);
    },
    resetState() {
      this.rows = 1;
      this.cols = 1;
    },
    insertTable() {
      this.tiptapEditor
        .chain()
        .focus()
        .insertTable({
          rows: this.rows,
          cols: this.cols,
          withHeaderRow: true,
        })
        .run();

      this.resetState();

      this.$emit('execute', { contentType: 'table' });
    },
    getButtonLabel(rows, cols) {
      return sprintf(__('Insert a %{rows}x%{cols} table.'), { rows, cols });
    },
  },
};
</script>
<template>
  <gl-dropdown size="small" category="tertiary" icon="table">
    <gl-dropdown-form class="gl-px-3! gl-w-auto!">
      <div class="gl-w-auto!">
        <div v-for="r of list(maxRows)" :key="r" class="gl-display-flex">
          <gl-button
            v-for="c of list(maxCols)"
            :key="c"
            :data-testid="`table-${r}-${c}`"
            :class="{ 'gl-bg-blue-50!': r <= rows && c <= cols }"
            :aria-label="getButtonLabel(r, c)"
            class="gl-display-inline! gl-px-0! gl-w-5! gl-h-5! gl-rounded-0!"
            @mouseover="setRowsAndCols(r, c)"
            @click="insertTable()"
          />
        </div>
        <gl-dropdown-divider />
        {{ getButtonLabel(rows, cols) }}
      </div>
    </gl-dropdown-form>
  </gl-dropdown>
</template>