JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4b
[ckeditor.git] / _source / plugins / tab / plugin.js
index c66fb43..f63cd5b 100644 (file)
@@ -27,13 +27,86 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                };\r
 \r
+       function selectNextCellCommand( backward )\r
+       {\r
+               return {\r
+                       editorFocus : false,\r
+                       canUndo : false,\r
+                       modes : { wysiwyg : 1 },\r
+                       exec : function( editor )\r
+                       {\r
+                               if ( editor.focusManager.hasFocus )\r
+                               {\r
+                                       var sel = editor.getSelection(),\r
+                                                       ancestor = sel.getCommonAncestor(),\r
+                                                       cell;\r
+\r
+                                       if ( ( cell = ( ancestor.getAscendant( 'td', true ) || ancestor.getAscendant( 'th', true ) ) ) )\r
+                                       {\r
+                                               var resultRange = new CKEDITOR.dom.range( editor.document ),\r
+                                                               next = CKEDITOR.tools.tryThese( function()\r
+                                                               {\r
+                                                                       var row = cell.getParent(),\r
+                                                                                       next = row.$.cells[ cell.$.cellIndex + ( backward ? - 1 : 1 ) ];\r
+\r
+                                                                       // Invalid any empty value.\r
+                                                                       next.parentNode.parentNode;\r
+                                                                       return next;\r
+                                                               },\r
+                                                               function()\r
+                                                               {\r
+                                                                       var row = cell.getParent(),\r
+                                                                                       table = row.getAscendant( 'table' ),\r
+                                                                                       nextRow = table.$.rows[ row.$.rowIndex + ( backward ? - 1 : 1 ) ];\r
+\r
+                                                                       return nextRow.cells[ backward? nextRow.cells.length -1 : 0 ];\r
+                                                               });\r
+\r
+                                               // Clone one more row at the end of table and select the first newly established cell.\r
+                                               if ( ! ( next || backward ) )\r
+                                               {\r
+                                                       var table = cell.getAscendant( 'table' ).$,\r
+                                                                       cells = cell.getParent().$.cells;\r
+\r
+                                                       var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document );\r
+\r
+                                                       for ( var i = 0, count = cells.length ; i < count; i++ )\r
+                                                       {\r
+                                                               var newCell = newRow.append( new CKEDITOR.dom.element(\r
+                                                                               cells[ i ], editor.document ).clone( false, false ) );\r
+                                                               !CKEDITOR.env.ie && newCell.appendBogus();\r
+                                                       }\r
+\r
+                                                       resultRange.moveToElementEditStart( newRow );\r
+                                               }\r
+                                               else if ( next )\r
+                                               {\r
+                                                       next = new CKEDITOR.dom.element( next );\r
+                                                       resultRange.moveToElementEditStart( next );\r
+                                                       // Avoid selecting empty block makes the cursor blind.\r
+                                                       if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) )\r
+                                                               resultRange.selectNodeContents( next );\r
+                                               }\r
+                                               else\r
+                                                       return true;\r
+\r
+                                               resultRange.select( true );\r
+                                               return true;\r
+                                       }\r
+                               }\r
+                               return false;\r
+                       }\r
+               };\r
+       }\r
+\r
        CKEDITOR.plugins.add( 'tab',\r
        {\r
                requires : [ 'keystrokes' ],\r
 \r
                init : function( editor )\r
                {\r
-                       var tabSpaces = editor.config.tabSpaces || 0,\r
+                       var tabTools = editor.config.enableTabKeyTools !== false,\r
+                               tabSpaces = editor.config.tabSpaces || 0,\r
                                tabText = '';\r
 \r
                        while ( tabSpaces-- )\r
@@ -51,6 +124,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        });\r
                        }\r
 \r
+                       if ( tabTools )\r
+                       {\r
+                               editor.on( 'key', function( ev )\r
+                               {\r
+                                       if ( ev.data.keyCode == 9 && editor.execCommand( 'selectNextCell' ) ||  // TAB\r
+                                                       ev.data.keyCode == ( CKEDITOR.SHIFT + 9 ) && editor.execCommand( 'selectPreviousCell' ) )       // SHIFT+TAB\r
+                                               ev.cancel();\r
+                               });\r
+                       }\r
+\r
                        if ( CKEDITOR.env.webkit || CKEDITOR.env.gecko )\r
                        {\r
                                editor.on( 'key', function( ev )\r
@@ -73,6 +156,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        editor.addCommand( 'blur', CKEDITOR.tools.extend( blurCommand, meta ) );\r
                        editor.addCommand( 'blurBack', CKEDITOR.tools.extend( blurBackCommand, meta ) );\r
+                       editor.addCommand( 'selectNextCell', selectNextCellCommand() );\r
+                       editor.addCommand( 'selectPreviousCell', selectNextCellCommand( true ) );\r
                }\r
        });\r
 })();\r
@@ -259,3 +344,24 @@ CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren, indexTo
  * @example\r
  * config.tabSpaces = 4;\r
  */\r
+\r
+/**\r
+ * Allow context-sensitive tab key behaviors, including the following scenarios:\r
+ * <h5>When selection is anchored inside <b>table cells</b>:</h5>\r
+ * <ul>\r
+ *             <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>\r
+ *             <li>If SHIFT+TAB is pressed, select the contents of the "previous" cell. Do nothing when it's in the first cell.</li>\r
+ * </ul>\r
+ * @name CKEDITOR.config.enableTabKeyTools\r
+ * @type Boolean\r
+ * @default true\r
+ * @example\r
+ * config.enableTabKeyTools = false;\r
+ */\r
+\r
+// If the TAB key is not supposed to be enabled for navigation, the following\r
+// settings could be used alternatively:\r
+// config.keystrokes.push(\r
+//     [ CKEDITOR.ALT + 38 /*Arrow Up*/, 'selectPreviousCell' ],\r
+//     [ CKEDITOR.ALT + 40 /*Arrow Down*/, 'selectNextCell' ]\r
+// );\r