X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ftab%2Fplugin.js;h=950fe8262e112a4c40c7a187c68af886e264cfda;hb=3fe9cac293e090ea459a3ee10d78cbe9e1dd0e03;hp=f2adcf99499c0eaa22540a48644688b147624904;hpb=c6e377a02b54abc07129d72b632763c727476a15;p=ckeditor.git diff --git a/_source/plugins/tab/plugin.js b/_source/plugins/tab/plugin.js index f2adcf9..950fe82 100644 --- a/_source/plugins/tab/plugin.js +++ b/_source/plugins/tab/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -13,77 +13,153 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var blurCommand = { + readOnly : 1, exec : function( editor ) { - editor.container.focusNext( true ); + editor.container.focusNext( true, editor.tabIndex ); } }; var blurBackCommand = { + readOnly : 1, exec : function( editor ) { - editor.container.focusPrevious( true ); + editor.container.focusPrevious( true, editor.tabIndex ); } }; + 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 ) { - // Register the keystrokes. - var keystrokes = editor.keystrokeHandler.keystrokes; - keystrokes[ 9 /* TAB */ ] = 'tab'; - keystrokes[ CKEDITOR.SHIFT + 9 /* TAB */ ] = 'shiftTab'; - - var tabSpaces = editor.config.tabSpaces, + var tabTools = editor.config.enableTabKeyTools !== false, + tabSpaces = editor.config.tabSpaces || 0, tabText = ''; while ( tabSpaces-- ) tabText += '\xa0'; - // Register the "tab" and "shiftTab" commands. - editor.addCommand( 'tab', CKEDITOR.tools.extend( - { - exec : function( editor ) + if ( tabText ) + { + editor.on( 'key', function( ev ) { - // Fire the "tab" event, making it possible to - // customize the TAB key behavior on specific cases. - if ( !editor.fire( 'tab' ) ) + if ( ev.data.keyCode == 9 ) // TAB { - if ( tabText.length > 0 ) - editor.insertHtml( tabText ); - else - { - // All browsers jump to the next field on TAB, - // except Safari, so we have to do that manually - // here. - /// https://bugs.webkit.org/show_bug.cgi?id=20597 - return editor.execCommand( 'blur' ); - } + editor.insertHtml( tabText ); + ev.cancel(); } + }); + } - return true; - } - }, meta ) ); - - editor.addCommand( 'shiftTab', CKEDITOR.tools.extend( + if ( tabTools ) + { + editor.on( 'key', function( ev ) { - exec : function( editor ) + 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 ) { - // Fire the "tab" event, making it possible to - // customize the TAB key behavior on specific cases. - if ( !editor.fire( 'shiftTab' ) ) - return editor.execCommand( 'blurBack' ); + var keyCode = ev.data.keyCode; - return true; - } - }, meta ) ); + if ( keyCode == 9 && !tabText ) // TAB + { + ev.cancel(); + editor.execCommand( 'blur' ); + } + + if ( keyCode == ( CKEDITOR.SHIFT + 9 ) ) // SHIFT+TAB + { + editor.execCommand( 'blurBack' ); + ev.cancel(); + } + }); + } 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 ) ); } }); })(); @@ -95,10 +171,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license * var element = CKEDITOR.document.getById( 'example' ); * element.focusNext(); */ -CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren ) +CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren, indexToUse ) { var $ = this.$, - curTabIndex = this.getTabIndex(), + curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ), passedCurrent, enteredCurrent, elected, electedTabIndex, element, elementTabIndex; @@ -183,10 +259,10 @@ CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren ) * var element = CKEDITOR.document.getById( 'example' ); * element.focusPrevious(); */ -CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren ) +CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren, indexToUse ) { var $ = this.$, - curTabIndex = this.getTabIndex(), + curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ), passedCurrent, enteredCurrent, elected, electedTabIndex = 0, @@ -264,9 +340,30 @@ CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren ) * Intructs the editor to add a number of spaces (&nbsp;) to the text when * hitting the TAB key. If set to zero, the TAB key will be used to move the * cursor focus to the next element in the page, out of the editor focus. + * @name CKEDITOR.config.tabSpaces * @type Number * @default 0 * @example * config.tabSpaces = 4; */ -CKEDITOR.config.tabSpaces = 0 ; + +/** + * 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' ] +// );