-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
unmergeCell.js
133 lines (112 loc) 路 4.61 KB
/
unmergeCell.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
/**
* @fileoverview Implements UnmergeCell
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import $ from 'jquery';
import util from 'tui-code-snippet';
import Editor from '../editor';
import dataHandler from './tableDataHandler';
import tableRangeHandler from './tableRangeHandler';
import tableRenderer from './tableRenderer';
const {CommandManager} = Editor;
let UnmergeCell;
if (CommandManager) {
UnmergeCell = CommandManager.command('wysiwyg', /** @lends UnmergeCell */{
name: 'UnmergeCells',
/**
* 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);
if (!_hasMergedCell(tableData, tableRange)) {
return;
}
_unmergeCells(tableData, tableRange);
const $newTable = tableRenderer.replaceTable($table, tableData);
const focusCell = _findFocusCell($newTable, tableRange.start.rowIndex, tableRange.start.colIndex);
tableRenderer.focusToCell(sq, range, focusCell);
}
});
}
/**
* Whether has merged cell.
* @param {Array.<Array.<object>>} tableData - table data
* @param {{rowIndex: number, colIndex: number}} startRange - start table selection range
* @param {{rowIndex: number, colIndex: number}} endRange - end table selection range
* @returns {boolean}
* @private
*/
export function _hasMergedCell(tableData, {start: startRange, end: endRange}) {
const startColIndex = startRange.colIndex;
const limitColIndex = endRange.colIndex + 1;
const targetRows = tableData.slice(startRange.rowIndex, endRange.rowIndex + 1);
const targetCells = targetRows.map(rowData => rowData.slice(startColIndex, limitColIndex));
return !![].concat(...targetCells).filter(cellData => (cellData.colspan > 1 || cellData.rowspan > 1)).length;
}
/**
* Update merged cell data to basic cell data.
* @param {Array.<Array.<object>>} tableData - table data
* @param {number} startRowIndex - start row index
* @param {number} startColIndex - start col index
* @param {number} rowspan - rowspan property of merger cell
* @param {number} colspan - colspan property of merger cell
* @private
*/
function _updateMergedCells(tableData, startRowIndex, startColIndex, rowspan, colspan) {
const limitRowIndex = startRowIndex + rowspan;
const limitColIndex = startColIndex + colspan;
const colRange = util.range(startColIndex, limitColIndex);
util.range(startRowIndex, limitRowIndex).forEach(rowIndex => {
const rowData = tableData[rowIndex];
const startIndex = (rowIndex === startRowIndex) ? 1 : 0;
colRange.slice(startIndex).forEach(colIndex => {
rowData[colIndex] = dataHandler.createBasicCell(rowIndex, colIndex, rowData[colIndex].nodeName);
});
});
}
/**
* Unmerge selected cells.
* @param {Array.<Array.<object>>} tableData - table data
* @param {{rowIndex: number, colIndex: number}} startRange - start table selection range
* @param {{rowIndex: number, colIndex: number}} endRange - end table selection range
* @private
*/
export function _unmergeCells(tableData, {start: startRange, end: endRange}) {
const colRange = util.range(startRange.colIndex, endRange.colIndex + 1);
util.range(startRange.rowIndex, endRange.rowIndex + 1).forEach(rowIndex => {
colRange.forEach(colIndex => {
const cellData = tableData[rowIndex][colIndex];
const {colspan, rowspan} = cellData;
if (colspan > 1 || rowspan > 1) {
cellData.colspan = 1;
cellData.rowspan = 1;
_updateMergedCells(tableData, rowIndex, colIndex, rowspan, colspan);
}
});
});
}
/**
* 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);
const cellElementIndex = dataHandler.findElementIndex(tableData, rowIndex, colIndex);
return $newTable.find('tr').eq(cellElementIndex.rowIndex).find('td, th')[cellElementIndex.colIndex];
}
export default UnmergeCell;