Changeset 1380 for trunk


Ignore:
Timestamp:
02/07/18 04:54:13 (10 months ago)
Author:
gogo
Message:

#1617 - Implement tab/shift-tab for TableOperatios?

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/XinhaCore.js

    r1378 r1380  
    51615161  } 
    51625162}; 
     5163 
     5164/** If the given or current selection comprises or is enclosed by  
     5165 *   one of the given tag names, return the deepest encloser. 
     5166 *  
     5167 *  Example 
     5168 *  
     5169 *    <ul> <li> [Caret Here] </li> </li> 
     5170 *  
     5171 * editor.getElementIsOrEnclosingSelection(['ul','li']) 
     5172 *  
     5173 * will return the list element 
     5174 *  
     5175 */ 
     5176 
     5177Xinha.prototype.getElementIsOrEnclosingSelection = function(types, sel) 
     5178{ 
     5179  if(sel == null)  
     5180  { 
     5181    sel = this.getSelection(); 
     5182  } 
     5183   
     5184  var currentElement = this.activeElement(sel) ? this.activeElement(sel) : this.getParentElement(sel); 
     5185   
     5186   
     5187  var typeMatched = false; 
     5188  if(!(typeof currentElement.tagName == 'undefined')) 
     5189  { 
     5190    for(var i = 0; i < types.length; i++) 
     5191    { 
     5192      if(currentElement.tagName.toLowerCase().match(types[i].toLowerCase())) 
     5193      { 
     5194        typeMatched = true;  
     5195        break; 
     5196      } 
     5197    } 
     5198  } 
     5199   
     5200  // It wasn't the selected element, see if there is an ancestor 
     5201  if(!typeMatched) 
     5202  { 
     5203    currentElement = null; 
     5204    currentElement = this._getFirstAncestor(sel, types); 
     5205  } 
     5206   
     5207  return currentElement; 
     5208}; 
     5209 
    51635210/** Returns an array with all the ancestor nodes of the selection or current cursor position. 
    51645211* @returns {Array} 
  • trunk/plugins/TableOperations/TableOperations.js

    r1379 r1380  
    1919  // this is useful if you have the ContextMenu plugin and want to save toolbar space 
    2020  // (the context menu can perform all the button operations) 
     21  'tabToNext':   true, // Hit tab in a table cell goes to next (shift for prev) cell 
    2122  'addToolbarLineBreak': true // By default TableOperations adds a 'linebreak' in the toolbar. 
    2223  // Set to false to prevent this and instead just append the buttons without a 'linebreak'. 
     
    722723  this.dialogRowCell.show(); 
    723724}; 
     725 
     726TableOperations.prototype.onKeyPress = function(ev) 
     727{ 
     728  var editor = this.editor; 
     729  
     730  // Not enabled, drop out 
     731  if(!editor.config.TableOperations.tabToNext) return false; 
     732   
     733  if( ev.keyCode !== 9 ) { return false; } 
     734 
     735  var currentcell = editor.getElementIsOrEnclosingSelection(['td','th']); 
     736   
     737  if( currentcell === null )  
     738  { 
     739    // Not in a table cell, drop through for others 
     740    return false; 
     741  } 
     742 
     743  ev.preventDefault(); 
     744     
     745  // find the next cell, get all the cells (td/th) which are in this table 
     746  // find ourself in that list 
     747  // set the new cell to pick to be the current index +/- 1 
     748  // select that new cell 
     749  var row = currentcell.parentNode; 
     750  var candidates = [ ]; 
     751  var all = row.parentNode.getElementsByTagName("*") 
     752  var ourindex = null; 
     753   
     754  for(var i = 0; i < all.length; i++) 
     755  { 
     756    // Same table (or tbody/thead) 
     757    if(all[i].parentNode.parentNode != currentcell.parentNode.parentNode) continue; 
     758     
     759    if(all[i].tagName.toLowerCase() == 'td' || all[i].tagName.toLowerCase() == 'th') 
     760    { 
     761      candidates[candidates.length] = all[i]; 
     762      if(all[i] == currentcell) ourindex=candidates.length-1; 
     763    } 
     764  } 
     765   
     766  var nextIndex = null; 
     767  if(ev.shiftKey) 
     768  { 
     769     nextIndex = Math.max(0,ourindex-1); 
     770  } 
     771  else 
     772  { 
     773    nextIndex = Math.min(ourindex+1, candidates.length-1); 
     774  } 
     775   
     776  if(ourindex == nextIndex) 
     777  { 
     778    // No other cell to go to, stop now 
     779    // maybe @TODO add a new row? 
     780    return true; 
     781  } 
     782   
     783  editor.selectNodeContents(candidates[nextIndex]); 
     784   
     785  /* If you wanted to collapse the selection to put the caret before/after it, you coudl do this. 
     786   *  but I think having it selected 
     787   * is more natural, that's how spreadsheets work (you tab into a field and start typing it will 
     788   * replace the field contents with the new contents) 
     789    
     790      if(ourindex < nextIndex) 
     791      { 
     792        sel.collapseToEnd(); 
     793      } 
     794      else 
     795      { 
     796        sel.collapseToEnd(); 
     797      } 
     798  */ 
     799 
     800  return true; 
     801} 
Note: See TracChangeset for help on using the changeset viewer.