X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ftabletools%2Fplugin.js;h=b06f59a50bb24696386a277c0e5c69011bad5355;hb=c9fdde67e6384bd5a66adc2b3bba5c4ce9db56c7;hp=4a3a00450b168cf1e83defc9ee814b733202d57a;hpb=c6e377a02b54abc07129d72b632763c727476a15;p=ckeditor.git diff --git a/_source/plugins/tabletools/plugin.js b/_source/plugins/tabletools/plugin.js index 4a3a004..b06f59a 100644 --- a/_source/plugins/tabletools/plugin.js +++ b/_source/plugins/tabletools/plugin.js @@ -86,6 +86,43 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return retval; } + function getFocusElementAfterDelCells( cellsToDelete ) { + var i = 0, + last = cellsToDelete.length - 1, + database = {}, + cell,focusedCell, + tr; + + while ( ( cell = cellsToDelete[ i++ ] ) ) + CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true ); + + // 1.first we check left or right side focusable cell row by row; + i = 0; + while ( ( cell = cellsToDelete[ i++ ] ) ) + { + if ( ( focusedCell = cell.getPrevious() ) && !focusedCell.getCustomData( 'delete_cell' ) + || ( focusedCell = cell.getNext() ) && !focusedCell.getCustomData( 'delete_cell' ) ) + { + CKEDITOR.dom.element.clearAllMarkers( database ); + return focusedCell; + } + } + + CKEDITOR.dom.element.clearAllMarkers( database ); + + // 2. then we check the toppest row (outside the selection area square) focusable cell + tr = cellsToDelete[ 0 ].getParent(); + if ( ( tr = tr.getPrevious() ) ) + return tr.getLast(); + + // 3. last we check the lowerest row focusable cell + tr = cellsToDelete[ last ].getParent(); + if ( ( tr = tr.getNext() ) ) + return tr.getChild( 0 ); + + return null; + } + function clearRow( $tr ) { // Get the array of row's cells. @@ -111,12 +148,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Create a clone of the row. var newRow = row.clone( true ); - // Insert the new row before of it. - newRow.insertBefore( row ); + insertBefore ? + newRow.insertBefore( row ) : + newRow.insertAfter( row ); - // Clean one of the rows to produce the illusion of inserting an empty row - // before or after. - clearRow( insertBefore ? newRow.$ : row.$ ); + // Clean the new row. + clearRow( newRow.$ ); } function deleteRows( selectionOrRow ) @@ -197,7 +234,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( $row.cells.length < ( cellIndex + 1 ) ) continue; - cell = new CKEDITOR.dom.element( $row.cells[ cellIndex ].cloneNode( false ) ); + cell = ( new CKEDITOR.dom.element( $row.cells[ cellIndex ] ) ).clone( false ); if ( !CKEDITOR.env.ie ) cell.appendBogus(); @@ -211,21 +248,65 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } } + function getFocusElementAfterDelCols( cells ) + { + var cellIndexList = [], + table = cells[ 0 ] && cells[ 0 ].getAscendant( 'table' ), + i, length, + targetIndex, targetCell; + + // get the cellIndex list of delete cells + for ( i = 0, length = cells.length; i < length; i++ ) + cellIndexList.push( cells[i].$.cellIndex ); + + // get the focusable column index + cellIndexList.sort(); + for ( i = 1, length = cellIndexList.length; i < length; i++ ) + { + if ( cellIndexList[ i ] - cellIndexList[ i - 1 ] > 1 ) + { + targetIndex = cellIndexList[ i - 1 ] + 1; + break; + } + } + + if ( !targetIndex ) + targetIndex = cellIndexList[ 0 ] > 0 ? ( cellIndexList[ 0 ] - 1 ) + : ( cellIndexList[ cellIndexList.length - 1 ] + 1 ); + + // scan row by row to get the target cell + var rows = table.$.rows; + for ( i = 0, length = rows.length; i < length ; i++ ) + { + targetCell = rows[ i ].cells[ targetIndex ]; + if ( targetCell ) + break; + } + + return targetCell ? new CKEDITOR.dom.element( targetCell ) : table.getPrevious(); + } + function deleteColumns( selectionOrCell ) { if ( selectionOrCell instanceof CKEDITOR.dom.selection ) { - var colsToDelete = getSelectedCells( selectionOrCell ); - for ( var i = colsToDelete.length ; i >= 0 ; i-- ) + var colsToDelete = getSelectedCells( selectionOrCell ), + elementToFocus = getFocusElementAfterDelCols( colsToDelete ); + + for ( var i = colsToDelete.length - 1 ; i >= 0 ; i-- ) { if ( colsToDelete[ i ] ) deleteColumns( colsToDelete[ i ] ); } + + return elementToFocus; } else if ( selectionOrCell instanceof CKEDITOR.dom.element ) { // Get the cell's table. var table = selectionOrCell.getAscendant( 'table' ); + if ( !table ) + return null; // Get the cell index. var cellIndex = selectionOrCell.$.cellIndex; @@ -251,6 +332,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license row.$.removeChild( row.$.cells[ cellIndex ] ); } } + + return null; } function insertCell( selection, insertBefore ) @@ -277,13 +360,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( selectionOrCell instanceof CKEDITOR.dom.selection ) { var cellsToDelete = getSelectedCells( selectionOrCell ); + var table = cellsToDelete[ 0 ] && cellsToDelete[ 0 ].getAscendant( 'table' ); + var cellToFocus = getFocusElementAfterDelCells( cellsToDelete ); + for ( var i = cellsToDelete.length - 1 ; i >= 0 ; i-- ) deleteCells( cellsToDelete[ i ] ); + + if ( cellToFocus ) + placeCursorInCell( cellToFocus, true ); + else if ( table ) + table.remove(); } else if ( selectionOrCell instanceof CKEDITOR.dom.element ) { - if ( selectionOrCell.getParent().getChildCount() == 1 ) - selectionOrCell.getParent().remove(); + var tr = selectionOrCell.getParent(); + if ( tr.getChildCount() == 1 ) + tr.remove(); else selectionOrCell.remove(); } @@ -308,51 +400,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license range.select( true ); } - function buildTableMap( table ) - { - - var aRows = table.$.rows ; - - // Row and Column counters. - var r = -1 ; - - var aMap = []; - - for ( var i = 0 ; i < aRows.length ; i++ ) - { - r++ ; - !aMap[r] && ( aMap[r] = [] ); - - var c = -1 ; - - for ( var j = 0 ; j < aRows[i].cells.length ; j++ ) - { - var oCell = aRows[i].cells[j] ; - - c++ ; - while ( aMap[r][c] ) - c++ ; - - var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ; - var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ; - - for ( var rs = 0 ; rs < iRowSpan ; rs++ ) - { - if ( !aMap[r + rs] ) - aMap[r + rs] = new Array() ; - - for ( var cs = 0 ; cs < iColSpan ; cs++ ) - { - aMap[r + rs][c + cs] = aRows[i].cells[j] ; - } - } - - c += iColSpan - 1 ; - } - } - return aMap ; - } - function cellInRow( tableMap, rowIndex, cell ) { var oRow = tableMap[ rowIndex ]; @@ -406,7 +453,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var cell, firstCell = cells[ 0 ], table = firstCell.getAscendant( 'table' ), - map = buildTableMap( table ), + map = CKEDITOR.tools.buildTableMap( table ), mapHeight = map.length, mapWidth = map[ 0 ].length, startRow = firstCell.getParent().$.rowIndex, @@ -541,7 +588,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var cell = cells[ 0 ], tr = cell.getParent(), table = tr.getAscendant( 'table' ), - map = buildTableMap( table ), + map = CKEDITOR.tools.buildTableMap( table ), rowIndex = tr.$.rowIndex, colIndex = cellInRow( map, rowIndex, cell ), rowSpan = cell.$.rowSpan, @@ -617,7 +664,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var cell = cells[ 0 ], tr = cell.getParent(), table = tr.getAscendant( 'table' ), - map = buildTableMap( table ), + map = CKEDITOR.tools.buildTableMap( table ), rowIndex = tr.$.rowIndex, colIndex = cellInRow( map, rowIndex, cell ), colSpan = cell.$.colSpan, @@ -680,9 +727,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license range.collapse(); selection.selectRanges( [ range ] ); - // If the table's parent has only one child, remove it as well. - if ( table.getParent().getChildCount() == 1 ) - table.getParent().remove(); + // If the table's parent has only one child, remove it,except body,as well.( #5416 ) + var parent = table.getParent(); + if ( parent.getChildCount() == 1 && parent.getName() != 'body' ) + parent.remove(); else table.remove(); } @@ -720,7 +768,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license exec : function( editor ) { var selection = editor.getSelection(); - deleteColumns( selection ); + var element = deleteColumns( selection ); + element && placeCursorInCell( element, true ); } } ); @@ -994,7 +1043,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { editor.contextMenu.addListener( function( element, selection ) { - if ( !element ) + if ( !element || element.isReadOnly() ) return null; while ( element ) @@ -1020,3 +1069,52 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }; CKEDITOR.plugins.add( 'tabletools', CKEDITOR.plugins.tabletools ); })(); + +/** + * Create a two-dimension array that reflects the actual layout of table cells, + * with cell spans, with mappings to the original td elements. + * @param table {CKEDITOR.dom.element} + */ +CKEDITOR.tools.buildTableMap = function ( table ) +{ + var aRows = table.$.rows ; + + // Row and Column counters. + var r = -1 ; + + var aMap = []; + + for ( var i = 0 ; i < aRows.length ; i++ ) + { + r++ ; + !aMap[r] && ( aMap[r] = [] ); + + var c = -1 ; + + for ( var j = 0 ; j < aRows[i].cells.length ; j++ ) + { + var oCell = aRows[i].cells[j] ; + + c++ ; + while ( aMap[r][c] ) + c++ ; + + var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ; + var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ; + + for ( var rs = 0 ; rs < iRowSpan ; rs++ ) + { + if ( !aMap[r + rs] ) + aMap[r + rs] = []; + + for ( var cs = 0 ; cs < iColSpan ; cs++ ) + { + aMap[r + rs][c + cs] = aRows[i].cells[j] ; + } + } + + c += iColSpan - 1 ; + } + } + return aMap ; +};