Ticket #979: 979_2.patch

File 979_2.patch, 4.4 KB (added by Garry Yao, 14 years ago)
  • _source/plugins/tab/plugin.js

     
    2727                        }
    2828                };
    2929
     30        function selectNextCellCommand( backward )
     31        {
     32                return {
     33                        editorFocus : false,
     34                        canUndo : false,
     35                        modes : { wysiwyg : 1 },
     36                        exec : function( editor )
     37                        {
     38                                if ( editor.focusManager.hasFocus )
     39                                {
     40                                        var sel = editor.getSelection(),
     41                                                        ancestor = sel.getCommonAncestor(),
     42                                                        cell;
     43
     44                                        if ( cell = ( ancestor.getAscendant( 'td', true ) || ancestor.getAscendant( 'th', true ) ) )
     45                                        {
     46                                                var resultRange = new CKEDITOR.dom.range( editor.document ),
     47                                                                next = CKEDITOR.tools.tryThese( function()
     48                                                                {
     49                                                                        var row = cell.getParent(),
     50                                                                                        next = row.$.cells[ cell.$.cellIndex + ( backward ? - 1 : 1 ) ];
     51
     52                                                                        // Invalid any empty value.
     53                                                                        next.parentNode.parentNode;
     54                                                                        return next;
     55                                                                },
     56                                                                function()
     57                                                                {
     58                                                                        var row = cell.getParent(),
     59                                                                                        table = row.getAscendant( 'table' ),
     60                                                                                        nextRow = table.$.rows[ row.$.rowIndex + ( backward ? - 1 : 1 ) ];
     61
     62                                                                        return nextRow.cells[ backward? nextRow.cells.length -1 : 0 ];
     63                                                                });
     64
     65                                                // Clone one more row at the end of table and select the first newly established cell.
     66                                                if ( ! ( next || backward ) )
     67                                                {
     68                                                        var table = cell.getAscendant( 'table' ).$,
     69                                                                        cells = cell.getParent().$.cells;
     70
     71                                                        var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document );
     72
     73                                                        for ( var i = 0, count = cells.length ; i < count; i++ )
     74                                                        {
     75                                                                var newCell = newRow.append( new CKEDITOR.dom.element(
     76                                                                                cells[ i ], editor.document ).clone( false, false ) );
     77                                                                !CKEDITOR.env.ie && newCell.appendBogus();
     78                                                        }
     79
     80                                                        resultRange.moveToElementEditStart( newRow );
     81                                                }
     82                                                else if ( next )
     83                                                {
     84                                                        next = new CKEDITOR.dom.element( next );
     85                                                        resultRange.moveToElementEditStart( next );
     86                                                        // Avoid selecting empty block makes the cursor blind.
     87                                                        if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) )
     88                                                                resultRange.selectNodeContents( next );
     89                                                }
     90                                                else
     91                                                        return true;
     92
     93                                                resultRange.select( true );
     94                                                return true;
     95                                        }
     96                                }
     97                                return false;
     98                        }
     99                };
     100        }
     101
    30102        CKEDITOR.plugins.add( 'tab',
    31103        {
    32104                requires : [ 'keystrokes' ],
    33105
    34106                init : function( editor )
    35107                {
    36                         var tabSpaces = editor.config.tabSpaces || 0,
     108                        var tabTools = editor.config.enableTabKeyTools !== false,
     109                                tabSpaces = editor.config.tabSpaces || 0,
    37110                                tabText = '';
    38111
    39112                        while ( tabSpaces-- )
     
    51124                                        });
    52125                        }
    53126
     127                        if ( tabTools )
     128                        {
     129                                editor.on( 'key', function( ev )
     130                                {
     131                                        if ( ev.data.keyCode == 9 && editor.execCommand( 'selectNextCell' ) ||  // TAB
     132                                                        ev.data.keyCode == ( CKEDITOR.SHIFT + 9 ) && editor.execCommand( 'selectPreviousCell' ) )       // SHIFT+TAB
     133                                                ev.cancel();
     134                                });
     135                        }
     136
    54137                        if ( CKEDITOR.env.webkit || CKEDITOR.env.gecko )
    55138                        {
    56139                                editor.on( 'key', function( ev )
     
    73156
    74157                        editor.addCommand( 'blur', CKEDITOR.tools.extend( blurCommand, meta ) );
    75158                        editor.addCommand( 'blurBack', CKEDITOR.tools.extend( blurBackCommand, meta ) );
     159                        editor.addCommand( 'selectNextCell', selectNextCellCommand() );
     160                        editor.addCommand( 'selectPreviousCell', selectNextCellCommand( true ) );
    76161                }
    77162        });
    78163})();
     
    259344 * @example
    260345 * config.tabSpaces = 4;
    261346 */
     347
     348/**
     349 * Allow context-sensitive tab key behaviors, including the following scenarios:
     350 * <h5>When selection is anchored inside <b>table cells</b>:</h5>
     351 * <ul>
     352 *              <li>If TAB is pressed, select the contents of the "next" cell. If in the last cell in the table, add a new row to it and focus its first cell.</li>
     353 *              <li>If SHIFT+TAB is pressed, select the contents of the "previous" cell. Do nothing when it's in the first cell.</li>
     354 * </ul>
     355 * @name CKEDITOR.config.enableTabKeyTools
     356 * @type Boolean
     357 * @default true
     358 * @example
     359 * config.enableTabKeyTools = false;
     360 */
     361
     362CKEDITOR.config.keystrokes.push(
     363        [ CKEDITOR.ALT + 38 /*Arrow Up*/, 'selectPreviousCell' ],
     364        [ CKEDITOR.ALT + 40 /*Arrow Down*/, 'selectNextCell' ]
     365)
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy