convertcase.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * 大小写转换
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. /**
  7. * 把选区内文本变大写,与“tolowercase”命令互斥
  8. * @command touppercase
  9. * @method execCommand
  10. * @param { String } cmd 命令字符串
  11. * @example
  12. * ```javascript
  13. * editor.execCommand( 'touppercase' );
  14. * ```
  15. */
  16. /**
  17. * 把选区内文本变小写,与“touppercase”命令互斥
  18. * @command tolowercase
  19. * @method execCommand
  20. * @param { String } cmd 命令字符串
  21. * @example
  22. * ```javascript
  23. * editor.execCommand( 'tolowercase' );
  24. * ```
  25. */
  26. UE.commands['touppercase'] =
  27. UE.commands['tolowercase'] = {
  28. execCommand:function (cmd) {
  29. var me = this;
  30. var rng = me.selection.getRange();
  31. if(rng.collapsed){
  32. return rng;
  33. }
  34. var bk = rng.createBookmark(),
  35. bkEnd = bk.end,
  36. filterFn = function( node ) {
  37. return !domUtils.isBr(node) && !domUtils.isWhitespace( node );
  38. },
  39. curNode = domUtils.getNextDomNode( bk.start, false, filterFn );
  40. while ( curNode && (domUtils.getPosition( curNode, bkEnd ) & domUtils.POSITION_PRECEDING) ) {
  41. if ( curNode.nodeType == 3 ) {
  42. curNode.nodeValue = curNode.nodeValue[cmd == 'touppercase' ? 'toUpperCase' : 'toLowerCase']();
  43. }
  44. curNode = domUtils.getNextDomNode( curNode, true, filterFn );
  45. if(curNode === bkEnd){
  46. break;
  47. }
  48. }
  49. rng.moveToBookmark(bk).select();
  50. }
  51. };