-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
mergedTableRemoveCol.js
149 lines (125 loc) 路 4.67 KB
/
mergedTableRemoveCol.js
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
/**
* @fileoverview Implements mergedTableRemoveCol. Remove col to selected table
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import $ from 'jquery';
import util from 'tui-code-snippet';
import Editor from '../editorProxy';
import dataHandler from './tableDataHandler';
import tableRangeHandler from './tableRangeHandler';
import tableRenderer from './tableRenderer';
const {CommandManager} = Editor;
let RemoveCol;
if (CommandManager) {
RemoveCol = CommandManager.command('wysiwyg', /** @lends RemoveCol */{
name: 'RemoveCol',
/**
* Command handler.
* @param {WysiwygEditor} wwe - wysiwygEditor instance
*/
exec(wwe) {
const sq = wwe.getEditor();
const range = sq.getSelection().cloneRange();
wwe.focus();
if (!sq.hasFormat('TABLE')) {
return;
}
const $startContainer = $(range.startContainer);
const $table = $startContainer.closest('table');
const tableData = dataHandler.createTableData($table);
const $selectedCells = wwe.componentManager.getManager('tableSelection').getSelectedCells();
const tableRange = tableRangeHandler.getTableSelectionRange(tableData, $selectedCells, $startContainer);
const beforeCellLength = tableData[0].length;
sq.saveUndoState(range);
_removeColumns(tableData, tableRange);
if (tableData[0].length === 0) {
$table.remove();
} else if (beforeCellLength !== tableData[0].length) {
const $newTable = tableRenderer.replaceTable($table, tableData);
const startColIndex = tableRange.start.colIndex;
const focusColIndex = startColIndex >= tableData[0].length ? startColIndex - 1 : startColIndex;
const focusCell = _findFocusCell($newTable, tableRange.start.rowIndex, focusColIndex);
tableRenderer.focusToCell(sq, range, focusCell);
}
}
});
}
/**
* Update colspan to col merger.
* @param {Array.<Array.<object>>} tableData - table data
* @param {number} startColIndex - start col index
* @param {number} endColIndex - end col index
* @private
*/
function _updateColspan(tableData, startColIndex, endColIndex) {
tableData.forEach(rowData => {
util.range(startColIndex, endColIndex + 1).forEach(colIndex => {
const cellData = rowData [colIndex];
if (util.isExisty(cellData.colMergeWith)) {
const merger = rowData [cellData.colMergeWith];
if (merger.colspan) {
merger.colspan -= 1;
}
} else if (cellData.colspan > 1) {
const lastMergedCellIndex = colIndex + cellData.colspan - 1;
cellData.colspan -= (endColIndex - colIndex + 1);
if (lastMergedCellIndex > endColIndex) {
rowData [endColIndex + 1] = util.extend({}, cellData);
}
}
});
});
}
/**
* Update row merge start index to merged cell.
* @param {Array.<Array.<object>>} tableData - table data
* @param {number} startColIndex - start col index
* @param {number} endColIndex - end col index
* @private
*/
function _updateMergeStartIndex(tableData, startColIndex, endColIndex) {
tableData.forEach(rowData => {
rowData.slice(endColIndex + 1).forEach(cellData => {
if (util.isExisty(cellData.colMergeWith) && cellData.colMergeWith >= startColIndex) {
cellData.colMergeWith = endColIndex + 1;
}
});
});
}
/**
* Remove columns.
* @param {Array.<Array.<object>>} tableData - table data
* @param {{
* start: {rowIndex: number, colIndex: number},
* end: {rowIndex: number, colIndex: number}
* }} tableRange - table selection range
* @private
*/
export function _removeColumns(tableData, tableRange) {
const startColIndex = tableRange.start.colIndex;
const endRange = tableRange.end;
const endColIndex = dataHandler.findColMergedLastIndex(tableData, endRange.rowIndex, endRange.colIndex);
const removeCount = endColIndex - startColIndex + 1;
_updateColspan(tableData, startColIndex, endColIndex);
_updateMergeStartIndex(tableData, startColIndex, endColIndex);
tableData.forEach(row => {
row.splice(startColIndex, removeCount);
});
}
/**
* Find focus cell element like td or th.
* @param {jQuery} $newTable - changed table jQuery element
* @param {number} rowIndex - row index of table data
* @param {number} colIndex - column index of tabld data
* @returns {HTMLElement}
* @private
*/
function _findFocusCell($newTable, rowIndex, colIndex) {
const tableData = dataHandler.createTableData($newTable);
if (tableData[0].length - 1 < colIndex) {
colIndex -= 1;
}
const cellElementIndex = dataHandler.findElementIndex(tableData, rowIndex, colIndex);
return $newTable.find('tr').eq(cellElementIndex.rowIndex).find('td, th')[cellElementIndex.colIndex];
}
export default RemoveCol;