CellSelection.js
4.99 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* CellSelection.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This class handles table cell selection by faking it using a css class that gets applied
* to cells when dragging the mouse from one cell to another.
*
* @class tinymce.tableplugin.CellSelection
* @private
*/
define("tinymce/tableplugin/CellSelection", [
"tinymce/tableplugin/TableGrid",
"tinymce/dom/TreeWalker",
"tinymce/util/Tools"
], function(TableGrid, TreeWalker, Tools) {
return function(editor, selectionChange) {
var dom = editor.dom, tableGrid, startCell, startTable, lastMouseOverTarget, hasCellSelection = true, resizing, dragging;
function clear(force) {
// Restore selection possibilities
editor.getBody().style.webkitUserSelect = '';
if (force || hasCellSelection) {
editor.$('td[data-mce-selected],th[data-mce-selected]').removeAttr('data-mce-selected');
hasCellSelection = false;
}
}
var endSelection = function () {
startCell = tableGrid = startTable = lastMouseOverTarget = null;
selectionChange(false);
};
function isCellInTable(table, cell) {
if (!table || !cell) {
return false;
}
return table === dom.getParent(cell, 'table');
}
function cellSelectionHandler(e) {
var sel, target = e.target, currentCell;
if (resizing || dragging) {
return;
}
// Fake mouse enter by keeping track of last mouse over
if (target === lastMouseOverTarget) {
return;
}
lastMouseOverTarget = target;
if (startTable && startCell) {
currentCell = dom.getParent(target, 'td,th');
if (!isCellInTable(startTable, currentCell)) {
currentCell = dom.getParent(startTable, 'td,th');
}
// Selection inside first cell is normal until we have expanted
if (startCell === currentCell && !hasCellSelection) {
return;
}
selectionChange(true);
if (isCellInTable(startTable, currentCell)) {
e.preventDefault();
if (!tableGrid) {
tableGrid = new TableGrid(editor, startTable, startCell);
editor.getBody().style.webkitUserSelect = 'none';
}
tableGrid.setEndCell(currentCell);
hasCellSelection = true;
// Remove current selection
sel = editor.selection.getSel();
try {
if (sel.removeAllRanges) {
sel.removeAllRanges();
} else {
sel.empty();
}
} catch (ex) {
// IE9 might throw errors here
}
}
}
}
editor.on('SelectionChange', function(e) {
if (hasCellSelection) {
e.stopImmediatePropagation();
}
}, true);
// Add cell selection logic
editor.on('MouseDown', function(e) {
if (e.button != 2 && !resizing && !dragging) {
clear();
startCell = dom.getParent(e.target, 'td,th');
startTable = dom.getParent(startCell, 'table');
}
});
editor.on('mouseover', cellSelectionHandler);
editor.on('remove', function() {
dom.unbind(editor.getDoc(), 'mouseover', cellSelectionHandler);
clear();
});
editor.on('MouseUp', function() {
var rng, sel = editor.selection, selectedCells, walker, node, lastNode;
function setPoint(node, start) {
var walker = new TreeWalker(node, node);
do {
// Text node
if (node.nodeType == 3 && Tools.trim(node.nodeValue).length !== 0) {
if (start) {
rng.setStart(node, 0);
} else {
rng.setEnd(node, node.nodeValue.length);
}
return;
}
// BR element
if (node.nodeName == 'BR') {
if (start) {
rng.setStartBefore(node);
} else {
rng.setEndBefore(node);
}
return;
}
} while ((node = (start ? walker.next() : walker.prev())));
}
// Move selection to startCell
if (startCell) {
if (tableGrid) {
editor.getBody().style.webkitUserSelect = '';
}
// Try to expand text selection as much as we can only Gecko supports cell selection
selectedCells = dom.select('td[data-mce-selected],th[data-mce-selected]');
if (selectedCells.length > 0) {
rng = dom.createRng();
node = selectedCells[0];
rng.setStartBefore(node);
rng.setEndAfter(node);
setPoint(node, 1);
walker = new TreeWalker(node, dom.getParent(selectedCells[0], 'table'));
do {
if (node.nodeName == 'TD' || node.nodeName == 'TH') {
if (!dom.getAttrib(node, 'data-mce-selected')) {
break;
}
lastNode = node;
}
} while ((node = walker.next()));
setPoint(lastNode);
sel.setRng(rng);
}
editor.nodeChanged();
endSelection();
}
});
editor.on('KeyUp Drop SetContent', function(e) {
clear(e.type == 'setcontent');
endSelection();
resizing = false;
});
editor.on('ObjectResizeStart ObjectResized', function(e) {
resizing = e.type != 'objectresized';
});
editor.on('dragstart', function () {
dragging = true;
});
editor.on('drop dragend', function () {
dragging = false;
});
return {
clear: clear
};
};
});