Changeset 695

Show
Ignore:
Timestamp:
2007-08-21 12:49:36 (3 years ago)
Author:
martinkou
Message:

Fix for #1077 : Fixed the issue where "Split Cells Horizontally" creates inefficient tables.
Fix for #1077 : "Merge Cells" in Firefox now works correctly as long as more than one cell is selected and the cell selection is rectangular.
Removed debug messages in table commands.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • FCKeditor/trunk/editor/_source/internals/fcktablehandler.js

    r694 r695  
    315315                        if ( ! tableMap[rowIdx] || ! tableMap[rowIdx][colIdx] ) 
    316316                        { 
    317                                 FCKDebug.Output( rowIdx + "," + colIdx + " not found" ) ; 
    318317                                this._UnmarkCells( cells, '_CellSelected' ) ; 
    319318                                return false ; 
     
    322321                        if ( g.width != geometry.width || g.height != geometry.height ) 
    323322                        { 
    324                                 FCKDebug.Output( "size " + g.width + "x" + g.height + " not match" ) ; 
    325323                                this._UnmarkCells( cells, '_CellSelected' ) ; 
    326324                                return false ; 
     
    335333                        if ( ! tableMap[rowIdx] || ! tableMap[rowIdx][colIdx] ) 
    336334                        { 
    337                                 FCKDebug.Output( rowIdx + "," + colIdx + " not found" ) ; 
    338335                                this._UnmarkCells( cells, '_CellSelected' ) ; 
    339336                                return false ; 
     
    342339                        if ( g.width != geometry.width || g.height != geometry.height ) 
    343340                        { 
    344                                 FCKDebug.Output( "size " + g.width + "x" + g.height + " not match" ) ; 
    345341                                this._UnmarkCells( cells, '_CellSelected' ) ; 
    346342                                return false ; 
     
    356352{ 
    357353        // Get all selected cells. 
    358         var aCells = FCKTableHandler.GetSelectedCells() ; 
    359  
    360         // TODO: Reimplement MergeCells() with this._InstallTableMap(). 
    361  
     354        var cells = this.GetSelectedCells() ; 
     355        if ( cells.length < 2 ) 
     356                return ; 
     357 
     358        // Assume the selected cells are already in a rectangular geometry. 
     359        // Because the checking is already done by FCKTableCommand. 
     360        var refCell = cells[0] ; 
     361        var tableMap = this._CreateTableMap( refCell.parentNode.parentNode ) ; 
     362        var rowIdx = refCell.parentNode.rowIndex ; 
     363        var colIdx = this._GetCellIndexSpan( tableMap, rowIdx, refCell ) ; 
     364 
     365        this._MarkCells( cells, '_SelectedCells' ) ; 
     366        var selectionGeometry = this._GetMarkerGeometry( tableMap, rowIdx, colIdx, '_SelectedCells' ) ; 
     367 
     368        var baseColIdx = colIdx - selectionGeometry.x ; 
     369        var baseRowIdx = rowIdx - selectionGeometry.y ; 
     370        var cellContents = refCell.ownerDocument.createDocumentFragment() ; 
     371        for ( var i = 0 ; i < selectionGeometry.height ; i++ )  
     372        { 
     373                var rowChildNodesCount = 0 ; 
     374                for ( var j = 0 ; j < selectionGeometry.width ; j++ ) 
     375                { 
     376                        var currentCell = tableMap[baseRowIdx + i][baseColIdx + j] ; 
     377                        while ( currentCell.childNodes.length > 0 ) 
     378                        { 
     379                                var node = currentCell.removeChild( currentCell.firstChild ) ; 
     380                                if ( node.nodeType != 1  
     381                                        || ( node.getAttribute( 'type', 2 ) != '_moz' && node.getAttribute( '_moz_dirty' ) != null ) ) 
     382                                { 
     383                                        cellContents.appendChild( node ) ; 
     384                                        rowChildNodesCount++ ; 
     385                                } 
     386                        } 
     387                } 
     388                if ( rowChildNodesCount > 0 ) 
     389                        cellContents.appendChild( refCell.ownerDocument.createElement( 'br' ) ) ; 
     390        } 
     391 
     392        this._ReplaceCellsByMarker( tableMap, '_SelectedCells', refCell ) ; 
     393        this._UnmarkCells( cells, '_SelectedCells' ) ; 
     394        this._InstallTableMap( tableMap, refCell.parentNode.parentNode ) ; 
     395        refCell.appendChild( cellContents ) ; 
     396         
     397        if ( FCKBrowserInfo.IsGecko && ( ! refCell.firstChild ) ) 
     398                FCKTools.AppendBogusBr( refCell ) ; 
     399 
     400        this._MoveCaretToCell( refCell, false ) ; 
    362401} 
    363402 
     
    381420        this._InstallTableMap( tableMap, refCell.parentNode.parentNode ) ; 
    382421 
    383         FCKSelection.SelectNode( refCell ) ; 
    384         FCKSelection.Collapse( false ) ; 
     422        this._MoveCaretToCell( refCell, false ) ; 
    385423} 
    386424 
     
    404442        this._InstallTableMap( tableMap, refCell.parentNode.parentNode ) ; 
    405443 
    406         FCKSelection.SelectNode( refCell ) ; 
    407         FCKSelection.Collapse( false ) ; 
     444        this._MoveCaretToCell( refCell, false ) ; 
    408445} 
    409446 
    410447FCKTableHandler.HorizontalSplitCell = function() 
    411448{ 
    412         // Check that just one cell is selected, otherwise return. 
    413         var aCells = FCKTableHandler.GetSelectedCells() ; 
    414         if ( aCells.length != 1 ) 
     449        var cells = FCKTableHandler.GetSelectedCells() ; 
     450        if ( cells.length != 1 ) 
    415451                return ; 
    416452 
    417         var aMap = this._CreateTableMap( aCells[0].parentNode.parentNode ) ; 
    418         var iCellIndex = FCKTableHandler._GetCellIndexSpan( aMap, aCells[0].parentNode.rowIndex , aCells[0] ) ; 
    419  
    420         var aCollCells = this._GetColumnCells( aMap, iCellIndex ) ; 
    421  
    422         for ( var i = 0 ; i < aCollCells.length ; i++ ) 
    423         { 
    424                 if ( aCollCells[i] == aCells[0] ) 
    425                 { 
    426                         var oNewCell = this.InsertCell( aCells[0], false ) ; 
    427                         if ( !isNaN( aCells[0].rowSpan ) && aCells[0].rowSpan > 1 ) 
    428                                 oNewCell.rowSpan = aCells[0].rowSpan ; 
    429                 } 
    430                 else 
    431                 { 
    432                         if ( isNaN( aCollCells[i].colSpan ) ) 
    433                                 aCollCells[i].colSpan = 2 ; 
     453        var refCell = cells[0] ; 
     454        var tableMap = this._CreateTableMap( refCell.parentNode.parentNode ) ; 
     455        var rowIdx = refCell.parentNode.rowIndex ; 
     456        var colIdx = FCKTableHandler._GetCellIndexSpan( tableMap, rowIdx, refCell ) ; 
     457        var cellSpan = isNaN( refCell.colSpan ) ? 1 : refCell.colSpan ; 
     458 
     459        if ( cellSpan > 1 ) 
     460        { 
     461                // Splittng a multi-column cell - original cell gets ceil(colSpan/2) columns, 
     462                // new cell gets floor(colSpan/2). 
     463                var newCellSpan = Math.ceil( cellSpan / 2 ) ; 
     464                var newCell = refCell.ownerDocument.createElement( 'td' ) ; 
     465                if ( FCKBrowserInfo.IsGecko ) 
     466                        FCKTools.AppendBogusBr( newCell ) ; 
     467                var startIdx = colIdx + newCellSpan ; 
     468                var endIdx = colIdx + cellSpan ; 
     469                var rowSpan = isNaN( refCell.rowSpan ) ? 1 : refCell.rowSpan ; 
     470                for ( var r = rowIdx ; r < rowIdx + rowSpan ; r++ ) 
     471                        for ( var i = startIdx ; i < endIdx ; i++ ) 
     472                                tableMap[r][i] = newCell ; 
     473        } 
     474        else 
     475        { 
     476                // Splitting a single-column cell - add a new cell, and expand  
     477                // cells crossing the same column. 
     478                var newTableMap = [] ; 
     479                for ( var i = 0 ; i < tableMap.length ; i++ )  
     480                { 
     481                        var newRow = tableMap[i].slice( 0, colIdx ) ; 
     482                        if ( tableMap[i].length <= colIdx ) 
     483                        { 
     484                                newTableMap.push( newRow ) ; 
     485                                continue ; 
     486                        } 
     487                        if ( tableMap[i][colIdx] == refCell ) 
     488                        { 
     489                                newRow.push( refCell ) ; 
     490                                newRow.push( refCell.ownerDocument.createElement( 'td' ) ) ; 
     491                                if ( FCKBrowserInfo.IsGecko ) 
     492                                        FCKTools.AppendBogusBr( newRow[newRow.length - 1] ) ; 
     493                        } 
    434494                        else 
    435                                 aCollCells[i].colSpan += 1 ; 
    436                 } 
    437         } 
     495                        { 
     496                                newRow.push( tableMap[i][colIdx] ) ; 
     497                                newRow.push( tableMap[i][colIdx] ) ; 
     498                        } 
     499                        for ( var j = colIdx + 1 ; j < tableMap[i].length ; j++ ) 
     500                                newRow.push( tableMap[i][j] ) ; 
     501                        newTableMap.push( newRow ) ; 
     502                } 
     503                tableMap = newTableMap ; 
     504        } 
     505 
     506        this._InstallTableMap( tableMap, refCell.parentNode.parentNode ) ; 
    438507} 
    439508 
     
    706775} 
    707776 
     777FCKTableHandler._MoveCaretToCell = function ( refCell, toStart ) 
     778{ 
     779        var range = new FCKDomRange( FCK.EditorWindow ) ; 
     780        range.MoveToNodeContents( refCell ) ; 
     781        range.Collapse( toStart ) ; 
     782        range.Select() ; 
     783} 
     784 
    708785FCKTableHandler.ClearRow = function( tr ) 
    709786{