X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ftabletools%2Fplugin.js;h=b92fb05baa9fbffe3d899e5382042f98ffb692e6;hb=4e90e78dc97789709ee7404359a5517540c27553;hp=4a3a00450b168cf1e83defc9ee814b733202d57a;hpb=c6e377a02b54abc07129d72b632763c727476a15;p=ckeditor.git diff --git a/_source/plugins/tabletools/plugin.js b/_source/plugins/tabletools/plugin.js index 4a3a004..b92fb05 100644 --- a/_source/plugins/tabletools/plugin.js +++ b/_source/plugins/tabletools/plugin.js @@ -1,18 +1,10 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ (function() { - function removeRawAttribute( $node, attr ) - { - if ( CKEDITOR.env.ie ) - $node.removeAttribute( attr ); - else - delete $node[ attr ]; - } - var cellNodeRegex = /^(?:td|th)$/; function getSelectedCells( selection ) @@ -86,37 +78,87 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return retval; } - function clearRow( $tr ) - { - // Get the array of row's cells. - var $cells = $tr.cells; + function getFocusElementAfterDelCells( cellsToDelete ) { + var i = 0, + last = cellsToDelete.length - 1, + database = {}, + cell,focusedCell, + tr; - // Empty all cells. - for ( var i = 0 ; i < $cells.length ; i++ ) - { - $cells[ i ].innerHTML = ''; + while ( ( cell = cellsToDelete[ i++ ] ) ) + CKEDITOR.dom.element.setMarker( database, cell, 'delete_cell', true ); - if ( !CKEDITOR.env.ie ) - ( new CKEDITOR.dom.element( $cells[ i ] ) ).appendBogus(); + // 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 insertRow( selection, insertBefore ) { - // Get the row where the selection is placed in. - var row = selection.getStartElement().getAscendant( 'tr' ); - if ( !row ) - return; - - // Create a clone of the row. - var newRow = row.clone( true ); + var cells = getSelectedCells( selection ), + firstCell = cells[ 0 ], + table = firstCell.getAscendant( 'table' ), + doc = firstCell.getDocument(), + startRow = cells[ 0 ].getParent(), + startRowIndex = startRow.$.rowIndex, + lastCell = cells[ cells.length - 1 ], + endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1, + endRow = new CKEDITOR.dom.element( table.$.rows[ endRowIndex ] ), + rowIndex = insertBefore ? startRowIndex : endRowIndex, + row = insertBefore ? startRow : endRow; + + var map = CKEDITOR.tools.buildTableMap( table ), + cloneRow = map[ rowIndex ], + nextRow = insertBefore ? map[ rowIndex - 1 ] : map[ rowIndex + 1 ], + width = map[0].length; + + var newRow = doc.createElement( 'tr' ); + for ( var i = 0; i < width; i++ ) + { + var cell; + // Check whether there's a spanning row here, do not break it. + if ( cloneRow[ i ].rowSpan > 1 && nextRow && cloneRow[ i ] == nextRow[ i ] ) + { + cell = cloneRow[ i ]; + cell.rowSpan += 1; + } + else + { + cell = new CKEDITOR.dom.element( cloneRow[ i ] ).clone(); + cell.removeAttribute( 'rowSpan' ); + !CKEDITOR.env.ie && cell.appendBogus(); + newRow.append( cell ); + cell = cell.$; + } - // Insert the new row before of it. - newRow.insertBefore( row ); + i += cell.colSpan - 1; + } - // Clean one of the rows to produce the illusion of inserting an empty row - // before or after. - clearRow( insertBefore ? newRow.$ : row.$ ); + insertBefore ? + newRow.insertBefore( row ) : + newRow.insertAfter( row ); } function deleteRows( selectionOrRow ) @@ -124,41 +166,59 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( selectionOrRow instanceof CKEDITOR.dom.selection ) { var cells = getSelectedCells( selectionOrRow ), - cellsCount = cells.length, - rowsToDelete = [], - cursorPosition, - previousRowIndex, - nextRowIndex; - - // Queue up the rows - it's possible and likely that we have duplicates. - for ( var i = 0 ; i < cellsCount ; i++ ) + firstCell = cells[ 0 ], + table = firstCell.getAscendant( 'table' ), + map = CKEDITOR.tools.buildTableMap( table ), + startRow = cells[ 0 ].getParent(), + startRowIndex = startRow.$.rowIndex, + lastCell = cells[ cells.length - 1 ], + endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1, + rowsToDelete = []; + + // Delete cell or reduce cell spans by checking through the table map. + for ( var i = startRowIndex; i <= endRowIndex; i++ ) { - var row = cells[ i ].getParent(), - rowIndex = row.$.rowIndex; + var mapRow = map[ i ], + row = new CKEDITOR.dom.element( table.$.rows[ i ] ); + + for ( var j = 0; j < mapRow.length; j++ ) + { + var cell = new CKEDITOR.dom.element( mapRow[ j ] ), + cellRowIndex = cell.getParent().$.rowIndex; - !i && ( previousRowIndex = rowIndex - 1 ); - rowsToDelete[ rowIndex ] = row; - i == cellsCount - 1 && ( nextRowIndex = rowIndex + 1 ); + if ( cell.$.rowSpan == 1 ) + cell.remove(); + // Row spanned cell. + else + { + // Span row of the cell, reduce spanning. + cell.$.rowSpan -= 1; + // Root row of the cell, root cell to next row. + if ( cellRowIndex == i ) + { + var nextMapRow = map[ i + 1 ]; + nextMapRow[ j - 1 ] ? + cell.insertAfter( new CKEDITOR.dom.element( nextMapRow[ j - 1 ] ) ) + : new CKEDITOR.dom.element( table.$.rows[ i + 1 ] ).append( cell, 1 ); + } + } + + j += cell.$.colSpan - 1; + } + + rowsToDelete.push( row ); } - var table = row.getAscendant( 'table' ), - rows = table.$.rows, - rowCount = rows.length; + var rows = table.$.rows; // Where to put the cursor after rows been deleted? // 1. Into next sibling row if any; // 2. Into previous sibling row if any; // 3. Into table's parent element if it's the very last row. - cursorPosition = new CKEDITOR.dom.element( - nextRowIndex < rowCount && table.$.rows[ nextRowIndex ] || - previousRowIndex > 0 && table.$.rows[ previousRowIndex ] || - table.$.parentNode ); + var cursorPosition = new CKEDITOR.dom.element( rows[ startRowIndex ] || rows[ startRowIndex - 1 ] || table.$.parentNode ); for ( i = rowsToDelete.length ; i >= 0 ; i-- ) - { - if ( rowsToDelete[ i ] ) - deleteRows( rowsToDelete[ i ] ); - } + deleteRows( rowsToDelete[ i ] ); return cursorPosition; } @@ -172,91 +232,185 @@ For licensing, see LICENSE.html or http://ckeditor.com/license selectionOrRow.remove(); } - return 0; + return null; } - function insertColumn( selection, insertBefore ) + function getCellColIndex( cell, isStart ) { - // Get the cell where the selection is placed in. - var startElement = selection.getStartElement(); - var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true ); + var row = cell.getParent(), + rowCells = row.$.cells; - if ( !cell ) - return; + var colIndex = 0; + for ( var i = 0; i < rowCells.length; i++ ) + { + var mapCell = rowCells[ i ]; + colIndex += isStart ? 1 : mapCell.colSpan; + if ( mapCell == cell.$ ) + break; + } - // Get the cell's table. - var table = cell.getAscendant( 'table' ); - var cellIndex = cell.$.cellIndex; + return colIndex -1; + } - // Loop through all rows available in the table. - for ( var i = 0 ; i < table.$.rows.length ; i++ ) + function getColumnsIndices( cells, isStart ) + { + var retval = isStart ? Infinity : 0; + for ( var i = 0; i < cells.length; i++ ) { - var $row = table.$.rows[ i ]; + var colIndex = getCellColIndex( cells[ i ], isStart ); + if ( isStart ? colIndex < retval : colIndex > retval ) + retval = colIndex; + } + return retval; + } - // If the row doesn't have enough cells, ignore it. - if ( $row.cells.length < ( cellIndex + 1 ) ) - continue; + function insertColumn( selection, insertBefore ) + { + var cells = getSelectedCells( selection ), + firstCell = cells[ 0 ], + table = firstCell.getAscendant( 'table' ), + startCol = getColumnsIndices( cells, 1 ), + lastCol = getColumnsIndices( cells ), + colIndex = insertBefore? startCol : lastCol; - cell = new CKEDITOR.dom.element( $row.cells[ cellIndex ].cloneNode( false ) ); + var map = CKEDITOR.tools.buildTableMap( table ), + cloneCol = [], + nextCol = [], + height = map.length; - if ( !CKEDITOR.env.ie ) - cell.appendBogus(); + for ( var i = 0; i < height; i++ ) + { + cloneCol.push( map[ i ][ colIndex ] ); + var nextCell = insertBefore ? map[ i ][ colIndex - 1 ] : map[ i ][ colIndex + 1 ]; + nextCell && nextCol.push( nextCell ); + } - // Get back the currently selected cell. - var baseCell = new CKEDITOR.dom.element( $row.cells[ cellIndex ] ); - if ( insertBefore ) - cell.insertBefore( baseCell ); + for ( i = 0; i < height; i++ ) + { + var cell; + // Check whether there's a spanning column here, do not break it. + if ( cloneCol[ i ].colSpan > 1 + && nextCol.length + && nextCol[ i ] == cloneCol[ i ] ) + { + cell = cloneCol[ i ]; + cell.colSpan += 1; + } else - cell.insertAfter( baseCell ); + { + cell = new CKEDITOR.dom.element( cloneCol[ i ] ).clone(); + cell.removeAttribute( 'colSpan' ); + !CKEDITOR.env.ie && cell.appendBogus(); + cell[ insertBefore? 'insertBefore' : 'insertAfter' ].call( cell, new CKEDITOR.dom.element ( cloneCol[ i ] ) ); + cell = cell.$; + } + + i += cell.rowSpan - 1; } } function deleteColumns( selectionOrCell ) { - if ( selectionOrCell instanceof CKEDITOR.dom.selection ) + var cells = getSelectedCells( selectionOrCell ), + firstCell = cells[ 0 ], + lastCell = cells[ cells.length - 1 ], + table = firstCell.getAscendant( 'table' ), + map = CKEDITOR.tools.buildTableMap( table ), + startColIndex, + endColIndex, + rowsToDelete = []; + + // Figure out selected cells' column indices. + for ( var i = 0, rows = map.length; i < rows; i++ ) { - var colsToDelete = getSelectedCells( selectionOrCell ); - for ( var i = colsToDelete.length ; i >= 0 ; i-- ) + for ( var j = 0, cols = map[ i ].length; j < cols; j++ ) { - if ( colsToDelete[ i ] ) - deleteColumns( colsToDelete[ i ] ); + if ( map[ i ][ j ] == firstCell.$ ) + startColIndex = j; + if ( map[ i ][ j ] == lastCell.$ ) + endColIndex = j; } } - else if ( selectionOrCell instanceof CKEDITOR.dom.element ) + + // Delete cell or reduce cell spans by checking through the table map. + for ( i = startColIndex; i <= endColIndex; i++ ) { - // Get the cell's table. - var table = selectionOrCell.getAscendant( 'table' ); + for ( j = 0; j < map.length; j++ ) + { + var mapRow = map[ j ], + row = new CKEDITOR.dom.element( table.$.rows[ j ] ), + cell = new CKEDITOR.dom.element( mapRow[ i ] ); - // Get the cell index. - var cellIndex = selectionOrCell.$.cellIndex; + if ( cell.$.colSpan == 1 ) + cell.remove(); + // Reduce the col spans. + else + cell.$.colSpan -= 1; - /* - * Loop through all rows from down to up, coz it's possible that some rows - * will be deleted. - */ - for ( i = table.$.rows.length - 1 ; i >= 0 ; i-- ) - { - // Get the row. - var row = new CKEDITOR.dom.element( table.$.rows[ i ] ); + j += cell.$.rowSpan - 1; - // If the cell to be removed is the first one and the row has just one cell. - if ( !cellIndex && row.$.cells.length == 1 ) - { - deleteRows( row ); - continue; - } + if ( !row.$.cells.length ) + rowsToDelete.push( row ); + } + } + + var firstRowCells = table.$.rows[ 0 ] && table.$.rows[ 0 ].cells; + + // Where to put the cursor after columns been deleted? + // 1. Into next cell of the first row if any; + // 2. Into previous cell of the first row if any; + // 3. Into table's parent element; + var cursorPosition = new CKEDITOR.dom.element( firstRowCells[ startColIndex ] || ( startColIndex ? firstRowCells[ startColIndex - 1 ] : table.$.parentNode ) ); + + // Delete table rows only if all columns are gone (do not remove empty row). + if ( rowsToDelete.length == rows ) + table.remove(); + + return cursorPosition; + } - // Else, just delete the cell. - if ( row.$.cells[ cellIndex ] ) - row.$.removeChild( row.$.cells[ cellIndex ] ); + 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 insertCell( selection, insertBefore ) { var startElement = selection.getStartElement(); - var cell = startElement.getAscendant( 'td', true ) || startElement.getAscendant( 'th', true ); + var cell = startElement.getAscendant( 'td', 1 ) || startElement.getAscendant( 'th', 1 ); if ( !cell ) return; @@ -277,13 +431,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 +471,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 +524,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, @@ -417,13 +535,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var targetCell; try { + var rowspan = parseInt( firstCell.getAttribute( 'rowspan' ), 10 ) || 1; + var colspan = parseInt( firstCell.getAttribute( 'colspan' ), 10 ) || 1; + targetCell = map[ mergeDirection == 'up' ? - ( startRow - 1 ): - mergeDirection == 'down' ? ( startRow + 1 ) : startRow ] [ - mergeDirection == 'left' ? - ( startColumn - 1 ): - mergeDirection == 'right' ? ( startColumn + 1 ) : startColumn ]; + ( startRow - rowspan ): + mergeDirection == 'down' ? ( startRow + rowspan ) : startRow ] [ + mergeDirection == 'left' ? + ( startColumn - colspan ): + mergeDirection == 'right' ? ( startColumn + colspan ) : startColumn ]; } catch( er ) @@ -480,7 +601,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { var last = frag.getLast( CKEDITOR.dom.walker.whitespaces( true ) ); if ( last && !( last.is && last.is( 'br' ) ) ) - frag.append( new CKEDITOR.dom.element( 'br' ) ); + frag.append( 'br' ); } cell.moveChildren( frag ); @@ -541,7 +662,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 +738,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, @@ -667,24 +788,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { exec : function( editor ) { - var selection = editor.getSelection(); - var startElement = selection && selection.getStartElement(); - var table = startElement && startElement.getAscendant( 'table', true ); + var selection = editor.getSelection(), + startElement = selection && selection.getStartElement(), + table = startElement && startElement.getAscendant( 'table', 1 ); if ( !table ) return; - // Maintain the selection point at where the table was deleted. - selection.selectElement( table ); - var range = selection.getRanges()[0]; - 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(); - else - table.remove(); + // If the table's parent has only one child remove it as well (unless it's the body or a table cell) (#5416, #6289) + var parent = table.getParent(); + if ( parent.getChildCount() == 1 && !parent.is( 'body', 'td', 'th' ) ) + table = parent; + + var range = new CKEDITOR.dom.range( editor.document ); + range.moveToPosition( table, CKEDITOR.POSITION_BEFORE_START ); + table.remove(); + range.select(); } } ); @@ -720,7 +839,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 +1114,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 +1140,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 ; +};