X-Git-Url: https://jasonwoof.com/gitweb/?p=ckeditor.git;a=blobdiff_plain;f=_source%2Fplugins%2Ftab%2Fplugin.js;h=f63cd5b8e07c8fe4ed94dd656197eb06ec52ab88;hp=c66fb43f077dcb72a3f211b98164e7cc9392e818;hb=e371ddf8abcb89013e20e6d0dd746adec344d0e5;hpb=055b6b0792ce7dc53d47af606b367c04b927c2ab diff --git a/_source/plugins/tab/plugin.js b/_source/plugins/tab/plugin.js index c66fb43..f63cd5b 100644 --- a/_source/plugins/tab/plugin.js +++ b/_source/plugins/tab/plugin.js @@ -27,13 +27,86 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }; + function selectNextCellCommand( backward ) + { + return { + editorFocus : false, + canUndo : false, + modes : { wysiwyg : 1 }, + exec : function( editor ) + { + if ( editor.focusManager.hasFocus ) + { + var sel = editor.getSelection(), + ancestor = sel.getCommonAncestor(), + cell; + + if ( ( cell = ( ancestor.getAscendant( 'td', true ) || ancestor.getAscendant( 'th', true ) ) ) ) + { + var resultRange = new CKEDITOR.dom.range( editor.document ), + next = CKEDITOR.tools.tryThese( function() + { + var row = cell.getParent(), + next = row.$.cells[ cell.$.cellIndex + ( backward ? - 1 : 1 ) ]; + + // Invalid any empty value. + next.parentNode.parentNode; + return next; + }, + function() + { + var row = cell.getParent(), + table = row.getAscendant( 'table' ), + nextRow = table.$.rows[ row.$.rowIndex + ( backward ? - 1 : 1 ) ]; + + return nextRow.cells[ backward? nextRow.cells.length -1 : 0 ]; + }); + + // Clone one more row at the end of table and select the first newly established cell. + if ( ! ( next || backward ) ) + { + var table = cell.getAscendant( 'table' ).$, + cells = cell.getParent().$.cells; + + var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document ); + + for ( var i = 0, count = cells.length ; i < count; i++ ) + { + var newCell = newRow.append( new CKEDITOR.dom.element( + cells[ i ], editor.document ).clone( false, false ) ); + !CKEDITOR.env.ie && newCell.appendBogus(); + } + + resultRange.moveToElementEditStart( newRow ); + } + else if ( next ) + { + next = new CKEDITOR.dom.element( next ); + resultRange.moveToElementEditStart( next ); + // Avoid selecting empty block makes the cursor blind. + if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) ) + resultRange.selectNodeContents( next ); + } + else + return true; + + resultRange.select( true ); + return true; + } + } + return false; + } + }; + } + CKEDITOR.plugins.add( 'tab', { requires : [ 'keystrokes' ], init : function( editor ) { - var tabSpaces = editor.config.tabSpaces || 0, + var tabTools = editor.config.enableTabKeyTools !== false, + tabSpaces = editor.config.tabSpaces || 0, tabText = ''; while ( tabSpaces-- ) @@ -51,6 +124,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }); } + if ( tabTools ) + { + editor.on( 'key', function( ev ) + { + if ( ev.data.keyCode == 9 && editor.execCommand( 'selectNextCell' ) || // TAB + ev.data.keyCode == ( CKEDITOR.SHIFT + 9 ) && editor.execCommand( 'selectPreviousCell' ) ) // SHIFT+TAB + ev.cancel(); + }); + } + if ( CKEDITOR.env.webkit || CKEDITOR.env.gecko ) { editor.on( 'key', function( ev ) @@ -73,6 +156,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license editor.addCommand( 'blur', CKEDITOR.tools.extend( blurCommand, meta ) ); editor.addCommand( 'blurBack', CKEDITOR.tools.extend( blurBackCommand, meta ) ); + editor.addCommand( 'selectNextCell', selectNextCellCommand() ); + editor.addCommand( 'selectPreviousCell', selectNextCellCommand( true ) ); } }); })(); @@ -259,3 +344,24 @@ CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren, indexTo * @example * config.tabSpaces = 4; */ + +/** + * Allow context-sensitive tab key behaviors, including the following scenarios: + *
When selection is anchored inside table cells:
+ * + * @name CKEDITOR.config.enableTabKeyTools + * @type Boolean + * @default true + * @example + * config.enableTabKeyTools = false; + */ + +// If the TAB key is not supposed to be enabled for navigation, the following +// settings could be used alternatively: +// config.keystrokes.push( +// [ CKEDITOR.ALT + 38 /*Arrow Up*/, 'selectPreviousCell' ], +// [ CKEDITOR.ALT + 40 /*Arrow Down*/, 'selectNextCell' ] +// );