JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.1
[ckeditor.git] / _source / plugins / tabletools / plugin.js
index 4a3a004..7590078 100644 (file)
@@ -86,6 +86,43 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                return retval;\r
        }\r
 \r
+       function getFocusedCell( cellsToDelete ) {\r
+               var i = 0,\r
+                       last = cellsToDelete.length - 1,\r
+                       database = {},\r
+                       cell,focusedCell,\r
+                       tr;\r
+\r
+               while ( ( cell = cellsToDelete[ i++ ] ) )\r
+                       CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true );\r
+\r
+               // 1.first we check left or right side focusable cell row by row;\r
+               i = 0;\r
+               while ( ( cell = cellsToDelete[ i++ ] ) )\r
+               {\r
+                       if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' )\r
+                         || ( focusedCell = cell.getNext()     ) && !focusedCell.getCustomData( 'delete_cell' ) )\r
+                       {\r
+                               CKEDITOR.dom.element.clearAllMarkers( database );\r
+                               return focusedCell;\r
+                       }\r
+               }\r
+\r
+               CKEDITOR.dom.element.clearAllMarkers( database );\r
+\r
+               // 2. then we check the toppest row (outside the selection area square) focusable cell\r
+               tr = cellsToDelete[ 0 ].getParent();\r
+               if ( ( tr = tr.getPrevious() ) )\r
+                       return tr.getLast();\r
+\r
+               // 3. last we check the lowerest  row focusable cell\r
+               tr = cellsToDelete[ last ].getParent();\r
+               if ( ( tr = tr.getNext() ) )\r
+                       return tr.getChild( 0 );\r
+\r
+               return null;\r
+       }\r
+\r
        function clearRow( $tr )\r
        {\r
                // Get the array of row's cells.\r
@@ -277,13 +314,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( selectionOrCell instanceof CKEDITOR.dom.selection )\r
                {\r
                        var cellsToDelete = getSelectedCells( selectionOrCell );\r
+                       var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );\r
+                       var cellToFocus   = getFocusedCell( cellsToDelete );\r
+\r
                        for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )\r
                                deleteCells( cellsToDelete[ i ] );\r
+\r
+                       if ( cellToFocus )\r
+                               placeCursorInCell( cellToFocus, true );\r
+                       else if ( table )\r
+                               table.remove();\r
                }\r
                else if ( selectionOrCell instanceof CKEDITOR.dom.element )\r
                {\r
-                       if ( selectionOrCell.getParent().getChildCount() == 1 )\r
-                               selectionOrCell.getParent().remove();\r
+                       var tr = selectionOrCell.getParent();\r
+                       if ( tr.getChildCount() == 1 )\r
+                               tr.remove();\r
                        else\r
                                selectionOrCell.remove();\r
                }\r