Ticket #5577: 5577_2.patch

File 5577_2.patch, 3.4 KB (added by brooks, 14 years ago)
  • _source/plugins/tabletools/plugin.js

     
    8686                return retval;
    8787        }
    8888
    89         function getFocusedCell( cellsToDelete ) {
     89        function getFocusElementAfterDelCells( cellsToDelete ) {
    9090                var i = 0,
    9191                        last = cellsToDelete.length - 1,
    9292                        database = {},
     
    247247                                cell.insertAfter( baseCell );
    248248                }
    249249        }
     250       
     251        function getFocusElementAfterDelCols( cells )
     252        {
     253                var cellIndex = [],
     254                        cellIndexFilter = {},
     255                        table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ),
     256                        i = 0,
     257                        cell, targetCell, index, targetIndex, preIndex, len;
     258               
     259                // get a unrepeat cellIndex list
     260                while ( cell = cells[ i++ ] )
     261                {
     262                        index = cell.$.cellIndex;
     263                       
     264                        if ( cellIndexFilter[ index ] )
     265                                continue;
     266                       
     267                        cellIndex.push( index );
     268                        cellIndexFilter[ index ] = 1;
     269                }
    250270
     271                cellIndex.sort();
     272
     273                preIndex = cellIndex[ 0 ];
     274                for ( var i = 1, length = cellIndex.length; i < length; i++ )
     275                {
     276                        if ( cellIndex[ i ] - preIndex > 1 )
     277                        {
     278                                targetIndex = preIndex + 1;
     279                                break;
     280                        }
     281                        preIndex = cellIndex[ i ];
     282                }
     283               
     284                if ( !targetIndex )
     285                        targetIndex = cellIndex[ 0 ] > 0 ? ( cellIndex[ 0 ] - 1 ) : ( cellIndex[ len - 1 ] + 1 );
     286
     287                // we scan row by row to get the target cell
     288                var trs = table.$.getElementsByTagName( 'tr' );
     289                for ( var i = 0, length = trs.length; i < length ; i++ )
     290                {
     291                        targetCell = trs[ i ].getElementsByTagName( 'td' )[ targetIndex ];
     292                        if ( targetCell )
     293                                break;
     294                }
     295
     296                return targetCell ?  new CKEDITOR.dom.element( targetCell ) :  table.getPrevious();
     297        }
     298
    251299        function deleteColumns( selectionOrCell )
    252300        {
    253301                if ( selectionOrCell instanceof CKEDITOR.dom.selection )
    254302                {
    255                         var colsToDelete = getSelectedCells( selectionOrCell );
    256                         for ( var i = colsToDelete.length ; i >= 0 ; i-- )
     303                        var colsToDelete = getSelectedCells( selectionOrCell ),
     304                                elementToFocus = getFocusElementAfterDelCols( colsToDelete );
     305
     306                        for ( var i = colsToDelete.length - 1 ; i >= 0 ; i-- )
    257307                        {
    258308                                if ( colsToDelete[ i ] )
    259309                                        deleteColumns( colsToDelete[ i ] );
    260310                        }
     311
     312                        return elementToFocus;
    261313                }
    262314                else if ( selectionOrCell instanceof CKEDITOR.dom.element )
    263315                {
    264316                        // Get the cell's table.
    265317                        var table = selectionOrCell.getAscendant( 'table' );
     318                        if ( !table )
     319                                return;
    266320
    267321                        // Get the cell index.
    268322                        var cellIndex = selectionOrCell.$.cellIndex;
     
    288342                                        row.$.removeChild( row.$.cells[ cellIndex ] );
    289343                        }
    290344                }
     345
     346                return null;
    291347        }
    292348
    293349        function insertCell( selection, insertBefore )
     
    315371                {
    316372                        var cellsToDelete = getSelectedCells( selectionOrCell );
    317373                        var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' );
    318                         var cellToFocus   = getFocusedCell( cellsToDelete );
     374                        var cellToFocus   = getFocusElementAfterDelCells( cellsToDelete );
    319375
    320376                        for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- )
    321377                                deleteCells( cellsToDelete[ i ] );
     
    767823                                        exec : function( editor )
    768824                                        {
    769825                                                var selection = editor.getSelection();
    770                                                 deleteColumns( selection );
     826                                                var element = deleteColumns( selection );
     827                                                element &&  placeCursorInCell( element, true );
    771828                                        }
    772829                                } );
    773830
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy