X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ftab%2Fplugin.js;h=f0a21b488d366b8aa0297950f06b846a4dae3060;hb=48b1db88210b4160dce439c6e3e32e14af8c106b;hp=6f848b4f42e65bbbdd1ed972d8fee1ce21bebc0a;hpb=941b0a9ba4e673e292510d80a5a86806994b8ea6;p=ckeditor.git diff --git a/_source/plugins/tab/plugin.js b/_source/plugins/tab/plugin.js index 6f848b4..f0a21b4 100644 --- a/_source/plugins/tab/plugin.js +++ b/_source/plugins/tab/plugin.js @@ -1,15 +1,21 @@ /* -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() { + var meta = + { + editorFocus : false, + modes : { wysiwyg:1, source:1 } + }; + var blurCommand = { exec : function( editor ) { - editor.container.focusNext( true ); + editor.container.focusNext( true, editor.tabIndex ); } }; @@ -17,9 +23,81 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { 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', { @@ -27,57 +105,59 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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', - { - 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; - } + 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(); }); + } - editor.addCommand( 'shiftTab', - { - exec : function( editor ) + 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; - } - }); + if ( keyCode == 9 && !tabText ) // TAB + { + ev.cancel(); + editor.execCommand( 'blur' ); + } - editor.addCommand( 'blur', blurCommand ); - editor.addCommand( 'blurBack', blurBackCommand ); + 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 ) ); } }); })(); @@ -89,10 +169,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; @@ -104,7 +184,7 @@ CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren ) element = this.getNextSourceNode( ignoreChildren, CKEDITOR.NODE_ELEMENT ); - while( element ) + while ( element ) { if ( element.isVisible() && element.getTabIndex() === 0 ) { @@ -125,7 +205,7 @@ CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren ) element = this.getDocument().getBody().getFirst(); - while( ( element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) ) + while ( ( element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) ) { if ( !passedCurrent ) { @@ -177,10 +257,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, @@ -188,7 +268,7 @@ CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren ) var element = this.getDocument().getBody().getLast(); - while( ( element = element.getPreviousSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) ) + while ( ( element = element.getPreviousSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) ) { if ( !passedCurrent ) { @@ -258,9 +338,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' ] +// );