-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
wwMergedTableSelectionManager.js
138 lines (116 loc) 路 4.01 KB
/
wwMergedTableSelectionManager.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
/**
* @fileoverview Implements wysiwyg merged table selection manager
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import $ from 'jquery';
import util from 'tui-code-snippet';
import Editor from '../editorProxy';
import tableDataHandler from './tableDataHandler';
import tableRangeHandler from './tableRangeHandler';
const {WwTableSelectionManager} = Editor;
const TABLE_CELL_SELECTED_CLASS_NAME = 'te-cell-selected';
/**
* Class WwMergedTableSelectionManager
*/
class WwMergedTableSelectionManager extends WwTableSelectionManager {
/**
* Creates an instance of WwMergedTableSelectionManager.
* @param {WysiwygEditor} wwe - WysiwygEditor instance
* @memberof WwMergedTableSelectionManager
*/
constructor(wwe) {
super(wwe);
/**
* table cache data
* @type {Array.<Array.<Object>>}
* @private
*/
this._tableData = null;
this.mergedTableSelectionManager = true;
}
/**
* Add css class for selected cells.
* @param {jQuery} $table - table jQuery element
* @param {Array.<Array.<object>>} tableData - table data
* @param {{
* start: {rowIndex: number, colIndex: number},
* end: {rowIndex: number, colIndex: number}
* }} tableRange - table selected range
* @private
*/
_addClassToSelectedCells($table, tableData, tableRange) {
const startRange = tableRange.start;
const endRange = tableRange.end;
const cellIndexRange = util.range(startRange.colIndex, endRange.colIndex + 1);
const $trs = $table.find('tr');
util.range(startRange.rowIndex, endRange.rowIndex + 1).forEach(rowIndex => {
const rowData = tableData[rowIndex];
const $cells = $trs.eq(rowIndex).find('td, th');
return cellIndexRange.forEach(colIndex => {
const cellData = rowData[colIndex];
if (cellData.elementIndex) {
$cells.eq(rowData[colIndex].elementIndex.colIndex).addClass(TABLE_CELL_SELECTED_CLASS_NAME);
}
});
});
}
/**
* cache table data on drag start
* @param {HTMLElement} selectionStart - start element
*/
onDragStart(selectionStart) {
const $table = $(selectionStart).closest('[contenteditable=true] table');
this._tableData = tableDataHandler.createTableData($table);
}
/**
* clear table data in cache on drag end
*/
onDragEnd() {
this._tableData = null;
}
/**
* Highlight selected table cells
* @param {HTMLElement} selectionStart start element
* @param {HTMLElement} selectionEnd end element
* @override
*/
highlightTableCellsBy(selectionStart, selectionEnd) {
const $start = $(selectionStart);
const $end = $(selectionEnd);
const $table = $start.closest('[contenteditable=true] table');
const tableRange = tableRangeHandler.findSelectionRange(this._tableData, $start, $end);
this.removeClassAttrbuteFromAllCellsIfNeed();
this._addClassToSelectedCells($table, this._tableData, tableRange);
}
/**
* Style to selected cells.
* @param {function} onStyle - function for styling
*/
styleToSelectedCells(onStyle) {
const sq = this.wwe.getEditor();
const range = sq.getSelection().cloneRange();
const $table = $(range.startContainer).closest('[contenteditable=true] table');
$table.find('tr').get().forEach(tr => {
const $cells = $(tr).find(`.${TABLE_CELL_SELECTED_CLASS_NAME}`);
const firstSelectedCell = $cells.first().get(0);
const lastSelectedCell = $cells.last().get(0);
if (!$cells.length) {
return;
}
range.setStart(firstSelectedCell, 0);
range.setEnd(lastSelectedCell, lastSelectedCell.childNodes.length);
sq.setSelection(range);
onStyle(sq);
});
}
/**
* Whether has selected both TH and TD.
* @param {jQuery} $selectedCells - selected cells jQuery element
* @returns {boolean}
*/
hasSelectedBothThAndTd($selectedCells) {
$selectedCells = $selectedCells || this.getSelectedCells();
return $selectedCells.first()[0].nodeName !== $selectedCells.last()[0].nodeName;
}
}
export default WwMergedTableSelectionManager;