puretxtpaste.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * 纯文本粘贴插件
  3. * @file
  4. * @since 1.2.6.1
  5. */
  6. UE.plugins['pasteplain'] = function(){
  7. var me = this;
  8. me.setOpt({
  9. 'pasteplain':false,
  10. 'filterTxtRules' : function(){
  11. function transP(node){
  12. node.tagName = 'p';
  13. node.setStyle();
  14. }
  15. function removeNode(node){
  16. node.parentNode.removeChild(node,true)
  17. }
  18. return {
  19. //直接删除及其字节点内容
  20. '-' : 'script style object iframe embed input select',
  21. 'p': {$:{}},
  22. 'br':{$:{}},
  23. div: function (node) {
  24. var tmpNode, p = UE.uNode.createElement('p');
  25. while (tmpNode = node.firstChild()) {
  26. if (tmpNode.type == 'text' || !UE.dom.dtd.$block[tmpNode.tagName]) {
  27. p.appendChild(tmpNode);
  28. } else {
  29. if (p.firstChild()) {
  30. node.parentNode.insertBefore(p, node);
  31. p = UE.uNode.createElement('p');
  32. } else {
  33. node.parentNode.insertBefore(tmpNode, node);
  34. }
  35. }
  36. }
  37. if (p.firstChild()) {
  38. node.parentNode.insertBefore(p, node);
  39. }
  40. node.parentNode.removeChild(node);
  41. },
  42. ol: removeNode,
  43. ul: removeNode,
  44. dl:removeNode,
  45. dt:removeNode,
  46. dd:removeNode,
  47. 'li':removeNode,
  48. 'caption':transP,
  49. 'th':transP,
  50. 'tr':transP,
  51. 'h1':transP,'h2':transP,'h3':transP,'h4':transP,'h5':transP,'h6':transP,
  52. 'td':function(node){
  53. //没有内容的td直接删掉
  54. var txt = !!node.innerText();
  55. if(txt){
  56. node.parentNode.insertAfter(UE.uNode.createText('    '),node);
  57. }
  58. node.parentNode.removeChild(node,node.innerText())
  59. }
  60. }
  61. }()
  62. });
  63. //暂时这里支持一下老版本的属性
  64. var pasteplain = me.options.pasteplain;
  65. /**
  66. * 启用或取消纯文本粘贴模式
  67. * @command pasteplain
  68. * @method execCommand
  69. * @param { String } cmd 命令字符串
  70. * @example
  71. * ```javascript
  72. * editor.queryCommandState( 'pasteplain' );
  73. * ```
  74. */
  75. /**
  76. * 查询当前是否处于纯文本粘贴模式
  77. * @command pasteplain
  78. * @method queryCommandState
  79. * @param { String } cmd 命令字符串
  80. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  81. * @example
  82. * ```javascript
  83. * editor.queryCommandState( 'pasteplain' );
  84. * ```
  85. */
  86. me.commands['pasteplain'] = {
  87. queryCommandState: function (){
  88. return pasteplain ? 1 : 0;
  89. },
  90. execCommand: function (){
  91. pasteplain = !pasteplain|0;
  92. },
  93. notNeedUndo : 1
  94. };
  95. };