2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
11 (function(tinymce) {
\r
12 var each = tinymce.each;
\r
17 function TableGrid(table, dom, selection) {
\r
18 var grid, startPos, endPos, selectedCell;
\r
21 selectedCell = dom.getParent(selection.getStart(), 'th,td');
\r
23 startPos = getPos(selectedCell);
\r
24 endPos = findEndPos();
\r
25 selectedCell = getCell(startPos.x, startPos.y);
\r
28 function cloneNode(node, children) {
\r
29 node = node.cloneNode(children);
\r
30 node.removeAttribute('id');
\r
35 function buildGrid() {
\r
40 each(['thead', 'tbody', 'tfoot'], function(part) {
\r
41 var rows = dom.select(part + ' tr', table);
\r
43 each(rows, function(tr, y) {
\r
46 each(dom.select('td,th', tr), function(td, x) {
\r
47 var x2, y2, rowspan, colspan;
\r
49 // Skip over existing cells produced by rowspan
\r
55 // Get col/rowspan from cell
\r
56 rowspan = getSpanVal(td, 'rowspan');
\r
57 colspan = getSpanVal(td, 'colspan');
\r
59 // Fill out rowspan/colspan right and down
\r
60 for (y2 = y; y2 < y + rowspan; y2++) {
\r
64 for (x2 = x; x2 < x + colspan; x2++) {
\r
67 real : y2 == y && x2 == x,
\r
77 startY += rows.length;
\r
81 function getCell(x, y) {
\r
89 function getSpanVal(td, name) {
\r
90 return parseInt(td.getAttribute(name) || 1);
\r
93 function isCellSelected(cell) {
\r
94 return dom.hasClass(cell.elm, 'mceSelected') || cell == selectedCell;
\r
97 function getSelectedRows() {
\r
100 each(table.rows, function(row) {
\r
101 each(row.cells, function(cell) {
\r
102 if (dom.hasClass(cell, 'mceSelected') || cell == selectedCell.elm) {
\r
112 function deleteTable() {
\r
113 var rng = dom.createRng();
\r
115 rng.setStartAfter(table);
\r
116 rng.setEndAfter(table);
\r
118 selection.setRng(rng);
\r
123 function cloneCell(cell) {
\r
127 tinymce.walk(cell, function(node) {
\r
130 if (node.nodeType == 3) {
\r
131 each(dom.getParents(node.parentNode, null, cell).reverse(), function(node) {
\r
132 node = cloneNode(node, false);
\r
135 formatNode = curNode = node;
\r
137 curNode.appendChild(node);
\r
142 // Add something to the inner node
\r
144 curNode.innerHTML = tinymce.isIE ? ' ' : '<br _mce_bogus="1" />';
\r
150 cell = cloneNode(cell, false);
\r
151 cell.rowSpan = cell.colSpan = 1;
\r
154 cell.appendChild(formatNode);
\r
157 cell.innerHTML = '<br _mce_bogus="1" />';
\r
163 function cleanup() {
\r
164 var rng = dom.createRng();
\r
167 each(dom.select('tr', table), function(tr) {
\r
168 if (tr.cells.length == 0)
\r
173 if (dom.select('tr', table).length == 0) {
\r
174 rng.setStartAfter(table);
\r
175 rng.setEndAfter(table);
\r
176 selection.setRng(rng);
\r
181 // Empty header/body/footer
\r
182 each(dom.select('thead,tbody,tfoot', table), function(part) {
\r
183 if (part.rows.length == 0)
\r
187 // Restore selection to start position if it still exists
\r
190 // Restore the selection to the closest table position
\r
191 row = grid[Math.min(grid.length - 1, startPos.y)];
\r
193 selection.select(row[Math.min(row.length - 1, startPos.x)].elm, true);
\r
194 selection.collapse(true);
\r
198 function fillLeftDown(x, y, rows, cols) {
\r
199 var tr, x2, r, c, cell;
\r
201 tr = grid[y][x].elm.parentNode;
\r
202 for (r = 1; r <= rows; r++) {
\r
203 tr = dom.getNext(tr, 'tr');
\r
206 // Loop left to find real cell
\r
207 for (x2 = x; x2 >= 0; x2--) {
\r
208 cell = grid[y + r][x2].elm;
\r
210 if (cell.parentNode == tr) {
\r
211 // Append clones after
\r
212 for (c = 1; c <= cols; c++)
\r
213 dom.insertAfter(cloneCell(cell), cell);
\r
220 // Insert nodes before first cell
\r
221 for (c = 1; c <= cols; c++)
\r
222 tr.insertBefore(cloneCell(tr.cells[0]), tr.cells[0]);
\r
229 each(grid, function(row, y) {
\r
230 each(row, function(cell, x) {
\r
231 var colSpan, rowSpan, newCell, i;
\r
233 if (isCellSelected(cell)) {
\r
235 colSpan = getSpanVal(cell, 'colspan');
\r
236 rowSpan = getSpanVal(cell, 'rowspan');
\r
238 if (colSpan > 1 || rowSpan > 1) {
\r
239 cell.colSpan = cell.rowSpan = 1;
\r
241 // Insert cells right
\r
242 for (i = 0; i < colSpan - 1; i++)
\r
243 dom.insertAfter(cloneCell(cell), cell);
\r
245 fillLeftDown(x, y, rowSpan - 1, colSpan);
\r
252 function merge(cell, cols, rows) {
\r
253 var startX, startY, endX, endY, x, y, startCell, endCell, cell, children;
\r
255 // Use specified cell and cols/rows
\r
257 pos = getPos(cell);
\r
260 endX = startX + (cols - 1);
\r
261 endY = startY + (rows - 1);
\r
264 startX = startPos.x;
\r
265 startY = startPos.y;
\r
270 // Find start/end cells
\r
271 startCell = getCell(startX, startY);
\r
272 endCell = getCell(endX, endY);
\r
274 // Check if the cells exists and if they are of the same part for example tbody = tbody
\r
275 if (startCell && endCell && startCell.part == endCell.part) {
\r
276 // Split and rebuild grid
\r
280 // Set row/col span to start cell
\r
281 startCell = getCell(startX, startY).elm;
\r
282 startCell.colSpan = (endX - startX) + 1;
\r
283 startCell.rowSpan = (endY - startY) + 1;
\r
285 // Remove other cells and add it's contents to the start cell
\r
286 for (y = startY; y <= endY; y++) {
\r
287 for (x = startX; x <= endX; x++) {
\r
288 cell = grid[y][x].elm;
\r
290 if (cell != startCell) {
\r
291 // Move children to startCell
\r
292 children = tinymce.grep(cell.childNodes);
\r
293 each(children, function(node, i) {
\r
294 // Jump over last BR element
\r
295 if (node.nodeName != 'BR' || i != children.length - 1)
\r
296 startCell.appendChild(node);
\r
305 // Remove empty rows etc and restore caret location
\r
310 function insertRow(before) {
\r
311 var posY, cell, lastCell, x, rowElm, newRow, newCell, otherCell;
\r
313 // Find first/last row
\r
314 each(grid, function(row, y) {
\r
315 each(row, function(cell, x) {
\r
316 if (isCellSelected(cell)) {
\r
318 rowElm = cell.parentNode;
\r
319 newRow = cloneNode(rowElm, false);
\r
331 for (x = 0; x < grid[0].length; x++) {
\r
332 cell = grid[posY][x].elm;
\r
334 if (cell != lastCell) {
\r
336 rowSpan = getSpanVal(cell, 'rowspan');
\r
338 cell.rowSpan = rowSpan + 1;
\r
342 // Check if cell above can be expanded
\r
343 if (posY > 0 && grid[posY - 1][x]) {
\r
344 otherCell = grid[posY - 1][x].elm;
\r
345 rowSpan = getSpanVal(otherCell, 'rowspan');
\r
347 otherCell.rowSpan = rowSpan + 1;
\r
353 // Insert new cell into new row
\r
354 newCell = cloneCell(cell)
\r
355 newCell.colSpan = cell.colSpan;
\r
356 newRow.appendChild(newCell);
\r
362 if (newRow.hasChildNodes()) {
\r
364 dom.insertAfter(newRow, rowElm);
\r
366 rowElm.parentNode.insertBefore(newRow, rowElm);
\r
370 function insertCol(before) {
\r
371 var posX, lastCell;
\r
373 // Find first/last column
\r
374 each(grid, function(row, y) {
\r
375 each(row, function(cell, x) {
\r
376 if (isCellSelected(cell)) {
\r
388 each(grid, function(row, y) {
\r
389 var cell = row[posX].elm, rowSpan, colSpan;
\r
391 if (cell != lastCell) {
\r
392 colSpan = getSpanVal(cell, 'colspan');
\r
393 rowSpan = getSpanVal(cell, 'rowspan');
\r
395 if (colSpan == 1) {
\r
397 dom.insertAfter(cloneCell(cell), cell);
\r
398 fillLeftDown(posX, y, rowSpan - 1, colSpan);
\r
400 cell.parentNode.insertBefore(cloneCell(cell), cell);
\r
401 fillLeftDown(posX, y, rowSpan - 1, colSpan);
\r
411 function deleteCols() {
\r
414 // Get selected column indexes
\r
415 each(grid, function(row, y) {
\r
416 each(row, function(cell, x) {
\r
417 if (isCellSelected(cell) && tinymce.inArray(cols, x) === -1) {
\r
418 each(grid, function(row) {
\r
419 var cell = row[x].elm, colSpan;
\r
421 colSpan = getSpanVal(cell, 'colspan');
\r
424 cell.colSpan = colSpan - 1;
\r
437 function deleteRows() {
\r
440 function deleteRow(tr) {
\r
441 var nextTr, pos, lastCell;
\r
443 nextTr = dom.getNext(tr, 'tr');
\r
445 // Move down row spanned cells
\r
446 each(tr.cells, function(cell) {
\r
447 var rowSpan = getSpanVal(cell, 'rowspan');
\r
450 cell.rowSpan = rowSpan - 1;
\r
451 pos = getPos(cell);
\r
452 fillLeftDown(pos.x, pos.y, 1, 1);
\r
457 pos = getPos(tr.cells[0]);
\r
458 each(grid[pos.y], function(cell) {
\r
463 if (cell != lastCell) {
\r
464 rowSpan = getSpanVal(cell, 'rowspan');
\r
469 cell.rowSpan = rowSpan - 1;
\r
476 // Get selected rows and move selection out of scope
\r
477 rows = getSelectedRows();
\r
479 // Delete all selected rows
\r
480 each(rows.reverse(), function(tr) {
\r
487 function cutRows() {
\r
488 var rows = getSelectedRows();
\r
496 function copyRows() {
\r
497 var rows = getSelectedRows();
\r
499 each(rows, function(row, i) {
\r
500 rows[i] = cloneNode(row, true);
\r
506 function pasteRows(rows, before) {
\r
507 var selectedRows = getSelectedRows(),
\r
508 targetRow = selectedRows[before ? 0 : selectedRows.length - 1],
\r
509 targetCellCount = targetRow.cells.length;
\r
511 // Calc target cell count
\r
512 each(grid, function(row) {
\r
515 targetCellCount = 0;
\r
516 each(row, function(cell, x) {
\r
518 targetCellCount += cell.colspan;
\r
520 if (cell.elm.parentNode == targetRow)
\r
531 each(rows, function(row) {
\r
532 var cellCount = row.cells.length, cell;
\r
534 // Remove col/rowspans
\r
535 for (i = 0; i < cellCount; i++) {
\r
536 cell = row.cells[i];
\r
537 cell.colSpan = cell.rowSpan = 1;
\r
540 // Needs more cells
\r
541 for (i = cellCount; i < targetCellCount; i++)
\r
542 row.appendChild(cloneCell(row.cells[cellCount - 1]));
\r
544 // Needs less cells
\r
545 for (i = targetCellCount; i < cellCount; i++)
\r
546 dom.remove(row.cells[i]);
\r
548 // Add before/after
\r
550 targetRow.parentNode.insertBefore(row, targetRow);
\r
552 dom.insertAfter(row, targetRow);
\r
556 function getPos(target) {
\r
559 each(grid, function(row, y) {
\r
560 each(row, function(cell, x) {
\r
561 if (cell.elm == target) {
\r
562 pos = {x : x, y : y};
\r
573 function setStartCell(cell) {
\r
574 startPos = getPos(cell);
\r
577 function findEndPos() {
\r
578 var pos, maxX, maxY;
\r
582 each(grid, function(row, y) {
\r
583 each(row, function(cell, x) {
\r
584 var colSpan, rowSpan;
\r
586 if (isCellSelected(cell)) {
\r
596 colSpan = cell.colspan - 1;
\r
597 rowSpan = cell.rowspan - 1;
\r
600 if (x + colSpan > maxX)
\r
601 maxX = x + colSpan;
\r
605 if (y + rowSpan > maxY)
\r
606 maxY = y + rowSpan;
\r
613 return {x : maxX, y : maxY};
\r
616 function setEndCell(cell) {
\r
617 var startX, startY, endX, endY, maxX, maxY, colSpan, rowSpan;
\r
619 endPos = getPos(cell);
\r
621 if (startPos && endPos) {
\r
622 // Get start/end positions
\r
623 startX = Math.min(startPos.x, endPos.x);
\r
624 startY = Math.min(startPos.y, endPos.y);
\r
625 endX = Math.max(startPos.x, endPos.x);
\r
626 endY = Math.max(startPos.y, endPos.y);
\r
628 // Expand end positon to include spans
\r
633 for (y = startY; y <= maxY; y++) {
\r
634 cell = grid[y][startX];
\r
637 if (startX - (cell.colspan - 1) < startX)
\r
638 startX -= cell.colspan - 1;
\r
643 for (x = startX; x <= maxX; x++) {
\r
644 cell = grid[startY][x];
\r
647 if (startY - (cell.rowspan - 1) < startY)
\r
648 startY -= cell.rowspan - 1;
\r
653 for (y = startY; y <= endY; y++) {
\r
654 for (x = startX; x <= endX; x++) {
\r
658 colSpan = cell.colspan - 1;
\r
659 rowSpan = cell.rowspan - 1;
\r
662 if (x + colSpan > maxX)
\r
663 maxX = x + colSpan;
\r
667 if (y + rowSpan > maxY)
\r
668 maxY = y + rowSpan;
\r
674 // Remove current selection
\r
675 dom.removeClass(dom.select('td.mceSelected,th.mceSelected'), 'mceSelected');
\r
677 // Add new selection
\r
678 for (y = startY; y <= maxY; y++) {
\r
679 for (x = startX; x <= maxX; x++)
\r
680 dom.addClass(grid[y][x].elm, 'mceSelected');
\r
685 // Expose to public
\r
686 tinymce.extend(this, {
\r
687 deleteTable : deleteTable,
\r
690 insertRow : insertRow,
\r
691 insertCol : insertCol,
\r
692 deleteCols : deleteCols,
\r
693 deleteRows : deleteRows,
\r
695 copyRows : copyRows,
\r
696 pasteRows : pasteRows,
\r
698 setStartCell : setStartCell,
\r
699 setEndCell : setEndCell
\r
703 tinymce.create('tinymce.plugins.TablePlugin', {
\r
704 init : function(ed, url) {
\r
705 var winMan, clipboardRows;
\r
707 function createTableGrid(node) {
\r
708 var selection = ed.selection, tblElm = ed.dom.getParent(node || selection.getNode(), 'table');
\r
711 return new TableGrid(tblElm, ed.dom, selection);
\r
714 function cleanup() {
\r
715 // Restore selection possibilities
\r
716 ed.getBody().style.webkitUserSelect = '';
\r
717 ed.dom.removeClass(ed.dom.select('td.mceSelected,th.mceSelected'), 'mceSelected');
\r
720 // Register buttons
\r
722 ['table', 'table.desc', 'mceInsertTable', true],
\r
723 ['delete_table', 'table.del', 'mceTableDelete'],
\r
724 ['delete_col', 'table.delete_col_desc', 'mceTableDeleteCol'],
\r
725 ['delete_row', 'table.delete_row_desc', 'mceTableDeleteRow'],
\r
726 ['col_after', 'table.col_after_desc', 'mceTableInsertColAfter'],
\r
727 ['col_before', 'table.col_before_desc', 'mceTableInsertColBefore'],
\r
728 ['row_after', 'table.row_after_desc', 'mceTableInsertRowAfter'],
\r
729 ['row_before', 'table.row_before_desc', 'mceTableInsertRowBefore'],
\r
730 ['row_props', 'table.row_desc', 'mceTableRowProps', true],
\r
731 ['cell_props', 'table.cell_desc', 'mceTableCellProps', true],
\r
732 ['split_cells', 'table.split_cells_desc', 'mceTableSplitCells', true],
\r
733 ['merge_cells', 'table.merge_cells_desc', 'mceTableMergeCells', true]
\r
735 ed.addButton(c[0], {title : c[1], cmd : c[2], ui : c[3]});
\r
738 // Select whole table is a table border is clicked
\r
739 if (!tinymce.isIE) {
\r
740 ed.onClick.add(function(ed, e) {
\r
743 if (e.nodeName === 'TABLE')
\r
744 ed.selection.select(e);
\r
748 // Handle node change updates
\r
749 ed.onNodeChange.add(function(ed, cm, n) {
\r
752 n = ed.selection.getStart();
\r
753 p = ed.dom.getParent(n, 'td,th,caption');
\r
754 cm.setActive('table', n.nodeName === 'TABLE' || !!p);
\r
756 // Disable table tools if we are in caption
\r
757 if (p && p.nodeName === 'CAPTION')
\r
760 cm.setDisabled('delete_table', !p);
\r
761 cm.setDisabled('delete_col', !p);
\r
762 cm.setDisabled('delete_table', !p);
\r
763 cm.setDisabled('delete_row', !p);
\r
764 cm.setDisabled('col_after', !p);
\r
765 cm.setDisabled('col_before', !p);
\r
766 cm.setDisabled('row_after', !p);
\r
767 cm.setDisabled('row_before', !p);
\r
768 cm.setDisabled('row_props', !p);
\r
769 cm.setDisabled('cell_props', !p);
\r
770 cm.setDisabled('split_cells', !p);
\r
771 cm.setDisabled('merge_cells', !p);
\r
774 ed.onInit.add(function(ed) {
\r
775 var startTable, startCell, dom = ed.dom, tableGrid;
\r
777 winMan = ed.windowManager;
\r
779 // Add cell selection logic
\r
780 ed.onMouseDown.add(function(ed, e) {
\r
781 if (e.button != 2) {
\r
784 startCell = dom.getParent(e.target, 'td,th');
\r
785 startTable = dom.getParent(startCell, 'table');
\r
789 dom.bind(ed.getDoc(), 'mouseover', function(e) {
\r
790 var sel, table, target = e.target;
\r
792 if (startCell && (tableGrid || target != startCell) && (target.nodeName == 'TD' || target.nodeName == 'TH')) {
\r
793 table = dom.getParent(target, 'table');
\r
794 if (table == startTable) {
\r
796 tableGrid = createTableGrid(table);
\r
797 tableGrid.setStartCell(startCell);
\r
799 ed.getBody().style.webkitUserSelect = 'none';
\r
802 tableGrid.setEndCell(target);
\r
805 // Remove current selection
\r
806 sel = ed.selection.getSel();
\r
808 if (sel.removeAllRanges)
\r
809 sel.removeAllRanges();
\r
813 e.preventDefault();
\r
817 ed.onMouseUp.add(function(ed, e) {
\r
818 var rng, sel = ed.selection, selectedCells, nativeSel = sel.getSel(), walker, node, lastNode, endNode;
\r
820 // Move selection to startCell
\r
823 ed.getBody().style.webkitUserSelect = '';
\r
825 function setPoint(node, start) {
\r
826 var walker = new tinymce.dom.TreeWalker(node, node);
\r
830 if (node.nodeType == 3 && tinymce.trim(node.nodeValue).length != 0) {
\r
832 rng.setStart(node, 0);
\r
834 rng.setEnd(node, node.nodeValue.length);
\r
840 if (node.nodeName == 'BR') {
\r
842 rng.setStartBefore(node);
\r
844 rng.setEndBefore(node);
\r
848 } while (node = (start ? walker.next() : walker.prev()));
\r
851 // Try to expand text selection as much as we can only Gecko supports cell selection
\r
852 selectedCells = dom.select('td.mceSelected,th.mceSelected');
\r
853 if (selectedCells.length > 0) {
\r
854 rng = dom.createRng();
\r
855 node = selectedCells[0];
\r
856 endNode = selectedCells[selectedCells.length - 1];
\r
859 walker = new tinymce.dom.TreeWalker(node, dom.getParent(selectedCells[0], 'table'));
\r
862 if (node.nodeName == 'TD' || node.nodeName == 'TH') {
\r
863 if (!dom.hasClass(node, 'mceSelected'))
\r
868 } while (node = walker.next());
\r
870 setPoint(lastNode);
\r
876 startCell = tableGrid = startTable = null;
\r
880 ed.onKeyUp.add(function(ed, e) {
\r
884 // Add context menu
\r
885 if (ed && ed.plugins.contextmenu) {
\r
886 ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
\r
887 var sm, se = ed.selection, el = se.getNode() || ed.getBody();
\r
889 if (ed.dom.getParent(e, 'td') || ed.dom.getParent(e, 'th') || ed.dom.select('td.mceSelected,th.mceSelected').length) {
\r
892 if (el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) {
\r
893 m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});
\r
894 m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});
\r
898 if (el.nodeName == 'IMG' && el.className.indexOf('mceItem') == -1) {
\r
899 m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});
\r
903 m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', value : {action : 'insert'}});
\r
904 m.add({title : 'table.props_desc', icon : 'table_props', cmd : 'mceInsertTable'});
\r
905 m.add({title : 'table.del', icon : 'delete_table', cmd : 'mceTableDelete'});
\r
909 sm = m.addMenu({title : 'table.cell'});
\r
910 sm.add({title : 'table.cell_desc', icon : 'cell_props', cmd : 'mceTableCellProps'});
\r
911 sm.add({title : 'table.split_cells_desc', icon : 'split_cells', cmd : 'mceTableSplitCells'});
\r
912 sm.add({title : 'table.merge_cells_desc', icon : 'merge_cells', cmd : 'mceTableMergeCells'});
\r
915 sm = m.addMenu({title : 'table.row'});
\r
916 sm.add({title : 'table.row_desc', icon : 'row_props', cmd : 'mceTableRowProps'});
\r
917 sm.add({title : 'table.row_before_desc', icon : 'row_before', cmd : 'mceTableInsertRowBefore'});
\r
918 sm.add({title : 'table.row_after_desc', icon : 'row_after', cmd : 'mceTableInsertRowAfter'});
\r
919 sm.add({title : 'table.delete_row_desc', icon : 'delete_row', cmd : 'mceTableDeleteRow'});
\r
921 sm.add({title : 'table.cut_row_desc', icon : 'cut', cmd : 'mceTableCutRow'});
\r
922 sm.add({title : 'table.copy_row_desc', icon : 'copy', cmd : 'mceTableCopyRow'});
\r
923 sm.add({title : 'table.paste_row_before_desc', icon : 'paste', cmd : 'mceTablePasteRowBefore'}).setDisabled(!clipboardRows);
\r
924 sm.add({title : 'table.paste_row_after_desc', icon : 'paste', cmd : 'mceTablePasteRowAfter'}).setDisabled(!clipboardRows);
\r
927 sm = m.addMenu({title : 'table.col'});
\r
928 sm.add({title : 'table.col_before_desc', icon : 'col_before', cmd : 'mceTableInsertColBefore'});
\r
929 sm.add({title : 'table.col_after_desc', icon : 'col_after', cmd : 'mceTableInsertColAfter'});
\r
930 sm.add({title : 'table.delete_col_desc', icon : 'delete_col', cmd : 'mceTableDeleteCol'});
\r
932 m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable'});
\r
936 // Fixes an issue on Gecko where it's impossible to place the caret behind a table
\r
937 // This fix will force a paragraph element after the table but only when the forced_root_block setting is enabled
\r
938 if (!tinymce.isIE) {
\r
939 function fixTableCaretPos() {
\r
942 // Skip empty text nodes form the end
\r
943 for (last = ed.getBody().lastChild; last && last.nodeType == 3 && !last.nodeValue.length; last = last.previousSibling) ;
\r
945 if (last && last.nodeName == 'TABLE')
\r
946 ed.dom.add(ed.getBody(), 'p', null, '<br mce_bogus="1" />');
\r
949 // Fixes an bug where it's impossible to place the caret before a table in Gecko
\r
950 // this fix solves it by detecting when the caret is at the beginning of such a table
\r
951 // and then manually moves the caret infront of the table
\r
952 if (tinymce.isGecko) {
\r
953 ed.onKeyDown.add(function(ed, e) {
\r
954 var rng, table, dom = ed.dom;
\r
956 // On gecko it's not possible to place the caret before a table
\r
957 if (e.keyCode == 37 || e.keyCode == 38) {
\r
958 rng = ed.selection.getRng();
\r
959 table = dom.getParent(rng.startContainer, 'table');
\r
961 if (table && ed.getBody().firstChild == table) {
\r
962 if (isAtStart(rng, table)) {
\r
963 rng = dom.createRng();
\r
965 rng.setStartBefore(table);
\r
966 rng.setEndBefore(table);
\r
968 ed.selection.setRng(rng);
\r
970 e.preventDefault();
\r
977 ed.onKeyUp.add(fixTableCaretPos);
\r
978 ed.onSetContent.add(fixTableCaretPos);
\r
979 ed.onVisualAid.add(fixTableCaretPos);
\r
981 ed.onPreProcess.add(function(ed, o) {
\r
982 var last = o.node.lastChild;
\r
984 if (last && last.childNodes.length == 1 && last.firstChild.nodeName == 'BR')
\r
985 ed.dom.remove(last);
\r
988 fixTableCaretPos();
\r
992 // Register action commands
\r
994 mceTableSplitCells : function(grid) {
\r
998 mceTableMergeCells : function(grid) {
\r
999 var rowSpan, colSpan, cell;
\r
1001 cell = ed.dom.getParent(ed.selection.getNode(), 'th,td');
\r
1003 rowSpan = cell.rowSpan;
\r
1004 colSpan = cell.colSpan;
\r
1007 if (!ed.dom.select('td.mceSelected,th.mceSelected').length) {
\r
1009 url : url + '/merge_cells.htm',
\r
1010 width : 240 + parseInt(ed.getLang('table.merge_cells_delta_width', 0)),
\r
1011 height : 110 + parseInt(ed.getLang('table.merge_cells_delta_height', 0)),
\r
1016 onaction : function(data) {
\r
1017 grid.merge(cell, data.cols, data.rows);
\r
1025 mceTableInsertRowBefore : function(grid) {
\r
1026 grid.insertRow(true);
\r
1029 mceTableInsertRowAfter : function(grid) {
\r
1033 mceTableInsertColBefore : function(grid) {
\r
1034 grid.insertCol(true);
\r
1037 mceTableInsertColAfter : function(grid) {
\r
1041 mceTableDeleteCol : function(grid) {
\r
1042 grid.deleteCols();
\r
1045 mceTableDeleteRow : function(grid) {
\r
1046 grid.deleteRows();
\r
1049 mceTableCutRow : function(grid) {
\r
1050 clipboardRows = grid.cutRows();
\r
1053 mceTableCopyRow : function(grid) {
\r
1054 clipboardRows = grid.copyRows();
\r
1057 mceTablePasteRowBefore : function(grid) {
\r
1058 grid.pasteRows(clipboardRows, true);
\r
1061 mceTablePasteRowAfter : function(grid) {
\r
1062 grid.pasteRows(clipboardRows);
\r
1065 mceTableDelete : function(grid) {
\r
1066 grid.deleteTable();
\r
1068 }, function(func, name) {
\r
1069 ed.addCommand(name, function() {
\r
1070 var grid = createTableGrid();
\r
1074 ed.execCommand('mceRepaint');
\r
1080 // Register dialog commands
\r
1082 mceInsertTable : function(val) {
\r
1084 url : url + '/table.htm',
\r
1085 width : 400 + parseInt(ed.getLang('table.table_delta_width', 0)),
\r
1086 height : 320 + parseInt(ed.getLang('table.table_delta_height', 0)),
\r
1090 action : val ? val.action : 0
\r
1094 mceTableRowProps : function() {
\r
1096 url : url + '/row.htm',
\r
1097 width : 400 + parseInt(ed.getLang('table.rowprops_delta_width', 0)),
\r
1098 height : 295 + parseInt(ed.getLang('table.rowprops_delta_height', 0)),
\r
1105 mceTableCellProps : function() {
\r
1107 url : url + '/cell.htm',
\r
1108 width : 400 + parseInt(ed.getLang('table.cellprops_delta_width', 0)),
\r
1109 height : 295 + parseInt(ed.getLang('table.cellprops_delta_height', 0)),
\r
1115 }, function(func, name) {
\r
1116 ed.addCommand(name, function(ui, val) {
\r
1123 // Register plugin
\r
1124 tinymce.PluginManager.add('table', tinymce.plugins.TablePlugin);
\r