-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.js
More file actions
456 lines (367 loc) · 16.4 KB
/
Copy pathplugin.js
File metadata and controls
456 lines (367 loc) · 16.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
CKEDITOR.plugins.add('ckeditortablecellsselection', {
init: function (editor) {
var pluginDirectory = this.path;
//add custom css
editor.addContentsCss(pluginDirectory + 'styles/tablecellsselection.css');
//add custom script
CKEDITOR.scriptLoader.load(pluginDirectory + 'js/tablecellsselection.js');
//editor.ui.addToolbarGroup('tables',0);//todo: what sens if we cand add it dinamic at runtime (used predefined or user config)
//---------- Handlers
//--- LOAD
/*editor.on('loaded',function(event){
console.log('loaded');
});*/
/*editor.on('instanceReady',function(event) {
console.log('instanceReady');
});*/
/* //--- SNAPSHOT
editor.on('getSnapshot',function(event){
console.log('getSnapshot');
});
//todo: set tableCellsSelection here for undo/redo (delete on change)
editor.on('loadSnapshot',function(event){
console.log('loadSnapshot');
});
editor.on('lockSnapshot',function(event){
console.log('lockSnapshot');
});
editor.on('saveSnapshot',function(event){
console.log('saveSnapshot');
});
editor.on('unlockSnapshot',function(event){
console.log('unlockSnapshot');
});
editor.on('updateSnapshot',function(event){
console.log('updateSnapshot');
});
//--- MODE
editor.on('beforeModeUnload',function(event){
console.log('beforeModeUnload');
});
editor.on('beforeSetMode',function(event){
console.log('beforeSetMode');
});
editor.on('mode',function(event){
console.log('mode');
});
*/
//--- CONTENT DOM
editor.on('contentDom', function (event) {
//console.log('contentDom');
initTablesSelection(event.editor);
});
editor.on('contentDomUnload', function (event) {
//console.log('contentDomUnload');
var document = event.editor.document;
if (!document) return;
destroyTablesSelection($(document.$));
});
/*editor.on('contentDomInvalidated',function(event){
console.log('contentDomInvalidated');
});*/
//--- CHANGE
//on change make all tables selectable (tableCellsSelection-jquery-plugin support multiple initialization on same DOM-node)
editor.on('change', function (event) {
//console.log('change');
initTablesSelection(event.editor);
});
//--- INSERT
/*editor.on('insertHtml',function(event){
console.log('insertHtml');
});
editor.on('afterInsertHtml',function(event){
console.log('afterInsertHtml');
});
editor.on('insertElement',function(event){
console.log('insertElement');
});
editor.on('insertText',function(event){
console.log('insertText');
});
//--- COPY/PASTE
editor.on('paste',function(event){
console.log('afterPaste');
});
editor.on('afterPaste',function(event){
console.log('afterPaste');
});
*/
//--- DATA
//From magicline-ckeditor-plugin
// Thanks to that, an editor never yields data polluted by the box.
// Listen with very high priority, so line will be removed before other
// listeners will see it.
//при получении данных - удаляем классы добавленные jquery-плагином для эмуляции выделения ячеек
//todo: сделать тоже самое при getSnapshot, по идее тогда не будут сохраняться выделения для undo
editor.on('beforeGetData', function (event) {
//console.log('beforeGetData');
var document = event.editor.document;
if (!document) return;
var $document = $(document.$);
//чистим DOM от добавленных классов и других служебных изменений
$().tableCellsSelection('removeDocumentHtmlChanges', $document);
editor.once('getData', function () {
$().tableCellsSelection('addDocumentHtmlChanges', $document);
}, null, null, 1000);
}, null, null, 0);
/*editor.on('getData',function(event){
console.log('getData');
});
editor.on('dataFiltered',function(event){
console.log('dataFiltered');
});
editor.on('dataReady',function(event){
console.log('dataReady');
});
editor.on('setData',function(event){
console.log('setData');
});
*/
//--- FORMAT
/*editor.on('toDataFormat',function(event){
console.log('toDataFormat');
});
editor.on('toHtml',function(event){
console.log('toHtml');
});
//--- FOCUS/BLUR
editor.on('blur',function(event){
console.log('blur');
});
editor.on('focus',function(event){
console.log('focus');
});
//--- SELECTION
editor.on('selectionChange',function(event){
console.log('selectionChange');
});
*/
//todo: проверить нет ли повторного навешивания обработчика событий при редактировании и undo/redo
function initTablesSelection(editor) {
$document = $(editor.document.$);
var $tables = $document.find('table');
//инициализируем таблицы
if ($tables.length > 0) $tables.tableCellsSelection();
//и вешаем обработчик
//this.$table.off('copy');
/*var eventName = 'selectionchange.cellsselector';
$tables.on(eventName,onTableCellSelectionChange);
function onTableCellSelectionChange(event){
editor.fire( 'selectionChange', {
selection: editor.getSelection(),
path: editor.elementPath()
});
}*/
}
function destroyTablesSelection($document) {
var $tables = $document.find('table');
if ($tables.length > 0) $tables.tableCellsSelection('destroy');
}
//Overwrite tabletools-plugin commands
addCmd('cellMerge', createDef({
allowedContent: 'td[colspan,rowspan]',
requiredContent: 'td[colspan,rowspan]',
exec: function (editor) {
//replace tabletools>mergeCells() to mergeCellsByPlugin()
placeCursorInCell(mergeCellsByPlugin(editor.getSelection()), true);
}
}));
/* overwrite tabletools context menu items */
// If the "menu" plugin is loaded
if (editor.getMenuItem) {
var tableCellMenuItem = editor.getMenuItem('tablecell');
var prevGetItemsFunction = tableCellMenuItem.getItems;
tableCellMenuItem.getItems = function () {
//получаем предыдущие значения
var ret = prevGetItemsFunction();
var selection = editor.getSelection(),
cells = getSelectedCellsByTableCellsSelectionPlugin(selection);
//заменяем необходимые на свои
ret.tablecell_merge = mergeCellsByPlugin(selection, null, true) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;//объединение ячеек
ret.tablecell_properties = cells.length > 0 ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
return ret;
};
}
//---------- Helpers
/*full copy of tabletools>mergeCells() except:
- replace getSelectedCells() to getSelectedCellsByTableCellsSelectionPlugin();
*/
function mergeCellsByPlugin(selection, mergeDirection, isDetect) {
//var cells = getSelectedCells( selection );
var cells = getSelectedCellsByTableCellsSelectionPlugin(selection);
// Invalid merge request if:
// 1. In batch mode despite that less than two selected.
// 2. In solo mode while not exactly only one selected.
// 3. Cells distributed in different table groups (e.g. from both thead and tbody).
var commonAncestor;
if (( mergeDirection ? cells.length != 1 : cells.length < 2 ) || ( commonAncestor = selection.getCommonAncestor() ) && commonAncestor.type == CKEDITOR.NODE_ELEMENT && commonAncestor.is('table'))
return false;
var cell,
firstCell = cells[0],
table = firstCell.getAscendant('table'),
map = CKEDITOR.tools.buildTableMap(table),
mapHeight = map.length,
mapWidth = map[0].length,
startRow = firstCell.getParent().$.rowIndex,
startColumn = cellInRow(map, startRow, firstCell);
if (mergeDirection) {
var targetCell;
try {
var rowspan = parseInt(firstCell.getAttribute('rowspan'), 10) || 1;
var colspan = parseInt(firstCell.getAttribute('colspan'), 10) || 1;
targetCell = map[mergeDirection == 'up' ? ( startRow - rowspan ) : mergeDirection == 'down' ? ( startRow + rowspan ) : startRow][
mergeDirection == 'left' ?
( startColumn - colspan ) :
mergeDirection == 'right' ? ( startColumn + colspan ) : startColumn];
} catch (er) {
return false;
}
// 1. No cell could be merged.
// 2. Same cell actually.
if (!targetCell || firstCell.$ == targetCell)
return false;
// Sort in map order regardless of the DOM sequence.
cells[( mergeDirection == 'up' || mergeDirection == 'left' ) ? 'unshift' : 'push'](new CKEDITOR.dom.element(targetCell));
}
// Start from here are merging way ignorance (merge up/right, batch merge).
var doc = firstCell.getDocument(),
lastRowIndex = startRow,
totalRowSpan = 0,
totalColSpan = 0,
// Use a documentFragment as buffer when appending cell contents.
frag = !isDetect && new CKEDITOR.dom.documentFragment(doc),
dimension = 0;
for (var i = 0; i < cells.length; i++) {
cell = cells[i];
var tr = cell.getParent(),
cellFirstChild = cell.getFirst(),
colSpan = cell.$.colSpan,
rowSpan = cell.$.rowSpan,
rowIndex = tr.$.rowIndex,
colIndex = cellInRow(map, rowIndex, cell);
// Accumulated the actual places taken by all selected cells.
dimension += colSpan * rowSpan;
// Accumulated the maximum virtual spans from column and row.
totalColSpan = Math.max(totalColSpan, colIndex - startColumn + colSpan);
totalRowSpan = Math.max(totalRowSpan, rowIndex - startRow + rowSpan);
if (!isDetect) {
// Trim all cell fillers and check to remove empty cells.
if (trimCell(cell), cell.getChildren().count()) {
// Merge vertically cells as two separated paragraphs.
if (rowIndex != lastRowIndex && cellFirstChild && !( cellFirstChild.isBlockBoundary && cellFirstChild.isBlockBoundary({br: 1}) )) {
var last = frag.getLast(CKEDITOR.dom.walker.whitespaces(true));
if (last && !( last.is && last.is('br') ))
frag.append('br');
}
cell.moveChildren(frag);
}
i ? cell.remove() : cell.setHtml('');
}
lastRowIndex = rowIndex;
}
if (!isDetect) {
frag.moveChildren(firstCell);
firstCell.appendBogus();
if (totalColSpan >= mapWidth)
firstCell.removeAttribute('rowSpan');
else
firstCell.$.rowSpan = totalRowSpan;
if (totalRowSpan >= mapHeight)
firstCell.removeAttribute('colSpan');
else
firstCell.$.colSpan = totalColSpan;
// Swip empty <tr> left at the end of table due to the merging.
var trs = new CKEDITOR.dom.nodeList(table.$.rows),
count = trs.count();
for (i = count - 1; i >= 0; i--) {
var tailTr = trs.getItem(i);
if (!tailTr.$.cells.length) {
tailTr.remove();
count++;
continue;
}
}
return firstCell;
}
// Be able to merge cells only if actual dimension of selected
// cells equals to the caculated rectangle.
else {
return ( totalRowSpan * totalColSpan ) == dimension;
}
}
//---------- Helpers > plugin
/**
* Return selected cells by tableCellsSelection-jquery-plugin
*
* @param selection
* @returns {Array}
*/
function getSelectedCellsByTableCellsSelectionPlugin(selection) {
var ranges = selection.getRanges();
var range = ranges[0];
var startNode = range.getCommonAncestor();
var table = startNode.getAscendant('table', true);
var $selectedCells = $(table.$).tableCellsSelection('selectedCells');
var ret = [];
//jquery to ckeditor.dom.element[]
for (var i = 0; i < $selectedCells.length; i++) {
ret[i] = new CKEDITOR.dom.element($selectedCells[i]);
}
return ret;
}
//---------- Helpers > Imported
//from other plugins
//---------- Helpers > Imported > tabletools plugin
function createDef(def) {
return CKEDITOR.tools.extend(def || {}, {
contextSensitive: 1,
refresh: function (editor, path) {
this.setState(path.contains({
td: 1,
th: 1
}, 1) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED);
}
});
}
function addCmd(name, def) {
var cmd = editor.addCommand(name, def);
editor.addFeature(cmd);
}
// Remove filler at end and empty spaces around the cell content.
function trimCell(cell) {
var bogus = cell.getBogus();
bogus && bogus.remove();
cell.trim();
}
function placeCursorInCell(cell, placeAtEnd) {
var docInner = cell.getDocument(),
docOuter = CKEDITOR.document;
// Fixing "Unspecified error" thrown in IE10 by resetting
// selection the dirty and shameful way (#10308).
// We can not apply this hack to IE8 because
// it causes error (#11058).
if (CKEDITOR.env.ie && CKEDITOR.env.version == 10) {
docOuter.focus();
docInner.focus();
}
var range = new CKEDITOR.dom.range(docInner);
if (!range['moveToElementEdit' + ( placeAtEnd ? 'End' : 'Start' )](cell)) {
range.selectNodeContents(cell);
range.collapse(placeAtEnd ? false : true);
}
range.select(true);
}
function cellInRow(tableMap, rowIndex, cell) {
var oRow = tableMap[rowIndex];
if (typeof cell == 'undefined')
return oRow;
for (var c = 0; oRow && c < oRow.length; c++) {
if (cell.is && oRow[c] == cell.$)
return c;
else if (c == cell)
return new CKEDITOR.dom.element(oRow[c]);
}
return cell.is ? -1 : null;
}
}
});