summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/wrappers/table_cell_base.vue
blob: 6456540a0dd7225179ed0d8ebdad99702afdb9d8 (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
<script>
import { GlDropdown, GlDropdownItem, GlDropdownDivider } from '@gitlab/ui';
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2';
import { selectedRect as getSelectedRect } from '@_ueberdosis/prosemirror-tables';
import { __ } from '~/locale';

const TABLE_CELL_HEADER = 'th';
const TABLE_CELL_BODY = 'td';

export default {
  name: 'TableCellBaseWrapper',
  components: {
    NodeViewWrapper,
    NodeViewContent,
    GlDropdown,
    GlDropdownItem,
    GlDropdownDivider,
  },
  props: {
    cellType: {
      type: String,
      validator: (type) => [TABLE_CELL_HEADER, TABLE_CELL_BODY].includes(type),
      required: true,
    },
    editor: {
      type: Object,
      required: true,
    },
    node: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      displayActionsDropdown: false,
      preventHide: true,
      selectedRect: null,
    };
  },
  computed: {
    totalRows() {
      return this.selectedRect?.map.height;
    },
    totalCols() {
      return this.selectedRect?.map.width;
    },
    isTableBodyCell() {
      return this.cellType === TABLE_CELL_BODY;
    },
  },
  mounted() {
    this.editor.on('selectionUpdate', this.handleSelectionUpdate);
    this.handleSelectionUpdate();
  },
  beforeDestroy() {
    this.editor.off('selectionUpdate', this.handleSelectionUpdate);
  },
  methods: {
    handleSelectionUpdate() {
      const { state } = this.editor;
      const { $cursor } = state.selection;

      if (!$cursor) return;

      this.displayActionsDropdown = false;

      for (let level = 0; level < $cursor.depth; level += 1) {
        if ($cursor.node(level) === this.node) {
          this.displayActionsDropdown = true;
          break;
        }
      }

      if (this.displayActionsDropdown) {
        this.selectedRect = getSelectedRect(state);
      }
    },
    runCommand(command) {
      this.editor.chain()[command]().run();
      this.hideDropdown();
    },
    handleHide($event) {
      if (this.preventHide) {
        $event.preventDefault();
      }
      this.preventHide = true;
    },
    hideDropdown() {
      this.preventHide = false;
      this.$refs.dropdown?.hide();
    },
  },
  i18n: {
    insertColumnBefore: __('Insert column before'),
    insertColumnAfter: __('Insert column after'),
    insertRowBefore: __('Insert row before'),
    insertRowAfter: __('Insert row after'),
    deleteRow: __('Delete row'),
    deleteColumn: __('Delete column'),
    deleteTable: __('Delete table'),
    editTableActions: __('Edit table'),
  },
  dropdownPopperOpts: {
    positionFixed: true,
  },
};
</script>
<template>
  <node-view-wrapper
    class="gl-relative gl-padding-5 gl-min-w-10"
    :as="cellType"
    @click="hideDropdown"
  >
    <span
      v-if="displayActionsDropdown"
      contenteditable="false"
      class="gl-absolute gl-right-0 gl-top-0"
    >
      <gl-dropdown
        ref="dropdown"
        dropup
        icon="chevron-down"
        size="small"
        category="tertiary"
        boundary="viewport"
        no-caret
        text-sr-only
        :text="$options.i18n.editTableActions"
        :popper-opts="$options.dropdownPopperOpts"
        @hide="handleHide($event)"
      >
        <gl-dropdown-item @click="runCommand('addColumnBefore')">
          {{ $options.i18n.insertColumnBefore }}
        </gl-dropdown-item>
        <gl-dropdown-item @click="runCommand('addColumnAfter')">
          {{ $options.i18n.insertColumnAfter }}
        </gl-dropdown-item>
        <gl-dropdown-item v-if="isTableBodyCell" @click="runCommand('addRowBefore')">
          {{ $options.i18n.insertRowBefore }}
        </gl-dropdown-item>
        <gl-dropdown-item @click="runCommand('addRowAfter')">
          {{ $options.i18n.insertRowAfter }}
        </gl-dropdown-item>
        <gl-dropdown-divider />
        <gl-dropdown-item v-if="totalRows > 2 && isTableBodyCell" @click="runCommand('deleteRow')">
          {{ $options.i18n.deleteRow }}
        </gl-dropdown-item>
        <gl-dropdown-item v-if="totalCols > 1" @click="runCommand('deleteColumn')">
          {{ $options.i18n.deleteColumn }}
        </gl-dropdown-item>
        <gl-dropdown-item @click="runCommand('deleteTable')">
          {{ $options.i18n.deleteTable }}
        </gl-dropdown-item>
      </gl-dropdown>
    </span>
    <node-view-content />
  </node-view-wrapper>
</template>