plugin.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. ( function() {
  6. var meta = {
  7. editorFocus: false,
  8. modes: { wysiwyg: 1, source: 1 }
  9. };
  10. var blurCommand = {
  11. exec: function( editor ) {
  12. editor.container.focusNext( true, editor.tabIndex );
  13. }
  14. };
  15. var blurBackCommand = {
  16. exec: function( editor ) {
  17. editor.container.focusPrevious( true, editor.tabIndex );
  18. }
  19. };
  20. function selectNextCellCommand( backward ) {
  21. return {
  22. editorFocus: false,
  23. canUndo: false,
  24. modes: { wysiwyg: 1 },
  25. exec: function( editor ) {
  26. if ( editor.editable().hasFocus ) {
  27. var sel = editor.getSelection(),
  28. path = new CKEDITOR.dom.elementPath( sel.getCommonAncestor(), sel.root ),
  29. cell;
  30. if ( ( cell = path.contains( { td: 1, th: 1 }, 1 ) ) ) {
  31. var resultRange = editor.createRange(),
  32. next = CKEDITOR.tools.tryThese( function() {
  33. var row = cell.getParent(),
  34. next = row.$.cells[ cell.$.cellIndex + ( backward ? -1 : 1 ) ];
  35. // Invalid any empty value.
  36. next.parentNode.parentNode;
  37. return next;
  38. }, function() {
  39. var row = cell.getParent(),
  40. table = row.getAscendant( 'table' ),
  41. nextRow = table.$.rows[ row.$.rowIndex + ( backward ? -1 : 1 ) ];
  42. return nextRow.cells[ backward ? nextRow.cells.length - 1 : 0 ];
  43. } );
  44. // Clone one more row at the end of table and select the first newly established cell.
  45. if ( !( next || backward ) ) {
  46. var table = cell.getAscendant( 'table' ).$,
  47. cells = cell.getParent().$.cells;
  48. var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document );
  49. for ( var i = 0, count = cells.length; i < count; i++ ) {
  50. var newCell = newRow.append( new CKEDITOR.dom.element( cells[ i ], editor.document ).clone( false, false ) );
  51. newCell.appendBogus();
  52. }
  53. resultRange.moveToElementEditStart( newRow );
  54. } else if ( next ) {
  55. next = new CKEDITOR.dom.element( next );
  56. resultRange.moveToElementEditStart( next );
  57. // Avoid selecting empty block makes the cursor blind.
  58. if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) )
  59. resultRange.selectNodeContents( next );
  60. } else {
  61. return true;
  62. }
  63. resultRange.select( true );
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. };
  70. }
  71. CKEDITOR.plugins.add( 'tab', {
  72. init: function( editor ) {
  73. var tabTools = editor.config.enableTabKeyTools !== false,
  74. tabSpaces = editor.config.tabSpaces || 0,
  75. tabText = '';
  76. while ( tabSpaces-- )
  77. tabText += '\xa0';
  78. if ( tabText ) {
  79. editor.on( 'key', function( ev ) {
  80. // TAB.
  81. if ( ev.data.keyCode == 9 ) {
  82. editor.insertText( tabText );
  83. ev.cancel();
  84. }
  85. } );
  86. }
  87. if ( tabTools ) {
  88. editor.on( 'key', function( ev ) {
  89. if ( ev.data.keyCode == 9 && editor.execCommand( 'selectNextCell' ) || // TAB
  90. ev.data.keyCode == ( CKEDITOR.SHIFT + 9 ) && editor.execCommand( 'selectPreviousCell' ) ) // SHIFT+TAB
  91. ev.cancel();
  92. } );
  93. }
  94. editor.addCommand( 'blur', CKEDITOR.tools.extend( blurCommand, meta ) );
  95. editor.addCommand( 'blurBack', CKEDITOR.tools.extend( blurBackCommand, meta ) );
  96. editor.addCommand( 'selectNextCell', selectNextCellCommand() );
  97. editor.addCommand( 'selectPreviousCell', selectNextCellCommand( true ) );
  98. }
  99. } );
  100. } )();
  101. /**
  102. * Moves the UI focus to the element following this element in the tabindex order.
  103. *
  104. * var element = CKEDITOR.document.getById( 'example' );
  105. * element.focusNext();
  106. *
  107. * @param {Boolean} [ignoreChildren=false]
  108. * @param {Number} [indexToUse]
  109. * @member CKEDITOR.dom.element
  110. */
  111. CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren, indexToUse ) {
  112. var curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ),
  113. passedCurrent, enteredCurrent, elected, electedTabIndex, element, elementTabIndex;
  114. if ( curTabIndex <= 0 ) {
  115. // If this element has tabindex <= 0 then we must simply look for any
  116. // element following it containing tabindex=0.
  117. element = this.getNextSourceNode( ignoreChildren, CKEDITOR.NODE_ELEMENT );
  118. while ( element ) {
  119. if ( element.isVisible() && element.getTabIndex() === 0 ) {
  120. elected = element;
  121. break;
  122. }
  123. element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
  124. }
  125. } else {
  126. // If this element has tabindex > 0 then we must look for:
  127. // 1. An element following this element with the same tabindex.
  128. // 2. The first element in source other with the lowest tabindex
  129. // that is higher than this element tabindex.
  130. // 3. The first element with tabindex=0.
  131. element = this.getDocument().getBody().getFirst();
  132. while ( ( element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) ) {
  133. if ( !passedCurrent ) {
  134. if ( !enteredCurrent && element.equals( this ) ) {
  135. enteredCurrent = true;
  136. // Ignore this element, if required.
  137. if ( ignoreChildren ) {
  138. if ( !( element = element.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT ) ) )
  139. break;
  140. passedCurrent = 1;
  141. }
  142. } else if ( enteredCurrent && !this.contains( element ) ) {
  143. passedCurrent = 1;
  144. }
  145. }
  146. if ( !element.isVisible() || ( elementTabIndex = element.getTabIndex() ) < 0 )
  147. continue;
  148. if ( passedCurrent && elementTabIndex == curTabIndex ) {
  149. elected = element;
  150. break;
  151. }
  152. if ( elementTabIndex > curTabIndex && ( !elected || !electedTabIndex || elementTabIndex < electedTabIndex ) ) {
  153. elected = element;
  154. electedTabIndex = elementTabIndex;
  155. } else if ( !elected && elementTabIndex === 0 ) {
  156. elected = element;
  157. electedTabIndex = elementTabIndex;
  158. }
  159. }
  160. }
  161. if ( elected )
  162. elected.focus();
  163. };
  164. /**
  165. * Moves the UI focus to the element before this element in the tabindex order.
  166. *
  167. * var element = CKEDITOR.document.getById( 'example' );
  168. * element.focusPrevious();
  169. *
  170. * @param {Boolean} [ignoreChildren=false]
  171. * @param {Number} [indexToUse]
  172. * @member CKEDITOR.dom.element
  173. */
  174. CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren, indexToUse ) {
  175. var curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ),
  176. passedCurrent, enteredCurrent, elected,
  177. electedTabIndex = 0,
  178. elementTabIndex;
  179. var element = this.getDocument().getBody().getLast();
  180. while ( ( element = element.getPreviousSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) ) {
  181. if ( !passedCurrent ) {
  182. if ( !enteredCurrent && element.equals( this ) ) {
  183. enteredCurrent = true;
  184. // Ignore this element, if required.
  185. if ( ignoreChildren ) {
  186. if ( !( element = element.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) ) )
  187. break;
  188. passedCurrent = 1;
  189. }
  190. } else if ( enteredCurrent && !this.contains( element ) ) {
  191. passedCurrent = 1;
  192. }
  193. }
  194. if ( !element.isVisible() || ( elementTabIndex = element.getTabIndex() ) < 0 )
  195. continue;
  196. if ( curTabIndex <= 0 ) {
  197. // If this element has tabindex <= 0 then we must look for:
  198. // 1. An element before this one containing tabindex=0.
  199. // 2. The last element with the highest tabindex.
  200. if ( passedCurrent && elementTabIndex === 0 ) {
  201. elected = element;
  202. break;
  203. }
  204. if ( elementTabIndex > electedTabIndex ) {
  205. elected = element;
  206. electedTabIndex = elementTabIndex;
  207. }
  208. } else {
  209. // If this element has tabindex > 0 we must look for:
  210. // 1. An element preceeding this one, with the same tabindex.
  211. // 2. The last element in source other with the highest tabindex
  212. // that is lower than this element tabindex.
  213. if ( passedCurrent && elementTabIndex == curTabIndex ) {
  214. elected = element;
  215. break;
  216. }
  217. if ( elementTabIndex < curTabIndex && ( !elected || elementTabIndex > electedTabIndex ) ) {
  218. elected = element;
  219. electedTabIndex = elementTabIndex;
  220. }
  221. }
  222. }
  223. if ( elected )
  224. elected.focus();
  225. };
  226. /**
  227. * Intructs the editor to add a number of spaces (`&nbsp;`) to the text when
  228. * hitting the *TAB* key. If set to zero, the *TAB* key will be used to move the
  229. * cursor focus to the next element in the page, out of the editor focus.
  230. *
  231. * config.tabSpaces = 4;
  232. *
  233. * @cfg {Number} [tabSpaces=0]
  234. * @member CKEDITOR.config
  235. */
  236. /**
  237. * Allow context-sensitive tab key behaviors, including the following scenarios:
  238. *
  239. * When selection is anchored inside **table cells**:
  240. *
  241. * * If *TAB* is pressed, select the contents of the "next" cell. If in the last
  242. * cell in the table, add a new row to it and focus its first cell.
  243. * * If *SHIFT+TAB* is pressed, select the contents of the "previous" cell.
  244. * Do nothing when it's in the first cell.
  245. *
  246. * Example:
  247. *
  248. * config.enableTabKeyTools = false;
  249. *
  250. * @cfg {Boolean} [enableTabKeyTools=true]
  251. * @member CKEDITOR.config
  252. */
  253. // If the TAB key is not supposed to be enabled for navigation, the following
  254. // settings could be used alternatively:
  255. // config.keystrokes.push(
  256. // [ CKEDITOR.ALT + 38 /*Arrow Up*/, 'selectPreviousCell' ],
  257. // [ CKEDITOR.ALT + 40 /*Arrow Down*/, 'selectNextCell' ]
  258. // );