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
|
<script>
import { GlDisclosureDropdown } from '@gitlab/ui';
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2';
import { selectedRect as getSelectedRect } from '@tiptap/pm/tables';
import { __, n__ } from '~/locale';
const TABLE_CELL_HEADER = 'th';
const TABLE_CELL_BODY = 'td';
function getDropdownItems({ selectedRect, cellType, rowspan = 1, colspan = 1 }) {
const totalRows = selectedRect?.map.height;
const totalCols = selectedRect?.map.width;
const isTableBodyCell = cellType === TABLE_CELL_BODY;
const selectedRows = selectedRect ? selectedRect.bottom - selectedRect.top : 0;
const selectedCols = selectedRect ? selectedRect.right - selectedRect.left : 0;
const showSplitCellOption =
selectedRows === rowspan && selectedCols === colspan && (rowspan > 1 || colspan > 1);
const showMergeCellsOption = selectedRows !== rowspan || selectedCols !== colspan;
const numCellsToMerge = (selectedRows - rowspan + 1) * (selectedCols - colspan + 1);
const showDeleteRowOption = totalRows > selectedRows + 1 && isTableBodyCell;
const showDeleteColumnOption = totalCols > selectedCols;
return [
{
items: [
{ text: __('Insert column before'), value: 'addColumnBefore' },
{ text: __('Insert column after'), value: 'addColumnAfter' },
isTableBodyCell && { text: __('Insert row before'), value: 'addRowBefore' },
{ text: __('Insert row after'), value: 'addRowAfter' },
].filter(Boolean),
},
{
items: [
showSplitCellOption && { text: __('Split cell'), value: 'splitCell' },
showMergeCellsOption && {
text: n__('Merge %d cell', 'Merge %d cells', numCellsToMerge),
value: 'mergeCells',
},
].filter(Boolean),
},
{
items: [
showDeleteRowOption && {
text: n__('Delete row', 'Delete %d rows', selectedRows),
value: 'deleteRow',
},
showDeleteColumnOption && {
text: n__('Delete column', 'Delete %d columns', selectedCols),
value: 'deleteColumn',
},
{ text: __('Delete table'), value: 'deleteTable' },
].filter(Boolean),
},
].filter(({ items }) => items.length);
}
export default {
name: 'TableCellBaseWrapper',
components: {
NodeViewWrapper,
NodeViewContent,
GlDisclosureDropdown,
},
props: {
getPos: {
type: Function,
required: true,
},
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,
selectedRect: null,
};
},
computed: {
dropdownItems() {
return getDropdownItems({
selectedRect: this.selectedRect,
cellType: this.cellType,
rowspan: this.node.attrs.rowspan,
colspan: this.node.attrs.colspan,
});
},
},
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;
try {
this.selectedRect = getSelectedRect(state);
} catch (e) {
// ignore error if the selection is not in a table
return;
}
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;
}
}
},
runCommand({ value: command }) {
this.hideDropdown();
this.editor.chain()[command]().run();
},
hideDropdown() {
this.$refs.dropdown?.close();
},
},
};
</script>
<template>
<node-view-wrapper
:as="cellType"
:rowspan="node.attrs.rowspan || 1"
:colspan="node.attrs.colspan || 1"
dir="auto"
class="gl-m-0! gl-p-0! gl-relative"
@click="hideDropdown"
>
<span
v-if="displayActionsDropdown"
contenteditable="false"
class="gl-absolute gl-right-0 gl-top-0 gl-pr-1 gl-pt-1"
>
<gl-disclosure-dropdown
ref="dropdown"
dropup
icon="chevron-down"
size="small"
category="tertiary"
boundary="viewport"
no-caret
text-sr-only
:items="dropdownItems"
:toggle-text="__('Edit table')"
positioning-strategy="fixed"
@action="runCommand"
/>
</span>
<node-view-content as="div" class="gl-p-5 gl-min-w-10" />
</node-view-wrapper>
</template>
|