puretxtpaste.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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,
  25. p = UE.uNode.createElement("p");
  26. while ((tmpNode = node.firstChild())) {
  27. if (tmpNode.type == "text" || !UE.dom.dtd.$block[tmpNode.tagName]) {
  28. p.appendChild(tmpNode);
  29. } else {
  30. if (p.firstChild()) {
  31. node.parentNode.insertBefore(p, node);
  32. p = UE.uNode.createElement("p");
  33. } else {
  34. node.parentNode.insertBefore(tmpNode, node);
  35. }
  36. }
  37. }
  38. if (p.firstChild()) {
  39. node.parentNode.insertBefore(p, node);
  40. }
  41. node.parentNode.removeChild(node);
  42. },
  43. ol: removeNode,
  44. ul: removeNode,
  45. dl: removeNode,
  46. dt: removeNode,
  47. dd: removeNode,
  48. li: removeNode,
  49. caption: transP,
  50. th: transP,
  51. tr: transP,
  52. h1: transP,
  53. h2: transP,
  54. h3: transP,
  55. h4: transP,
  56. h5: transP,
  57. h6: transP,
  58. td: function(node) {
  59. //没有内容的td直接删掉
  60. var txt = !!node.innerText();
  61. if (txt) {
  62. node.parentNode.insertAfter(
  63. UE.uNode.createText("    "),
  64. node
  65. );
  66. }
  67. node.parentNode.removeChild(node, node.innerText());
  68. }
  69. };
  70. })()
  71. });
  72. //暂时这里支持一下老版本的属性
  73. var pasteplain = me.options.pasteplain;
  74. /**
  75. * 启用或取消纯文本粘贴模式
  76. * @command pasteplain
  77. * @method execCommand
  78. * @param { String } cmd 命令字符串
  79. * @example
  80. * ```javascript
  81. * editor.queryCommandState( 'pasteplain' );
  82. * ```
  83. */
  84. /**
  85. * 查询当前是否处于纯文本粘贴模式
  86. * @command pasteplain
  87. * @method queryCommandState
  88. * @param { String } cmd 命令字符串
  89. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  90. * @example
  91. * ```javascript
  92. * editor.queryCommandState( 'pasteplain' );
  93. * ```
  94. */
  95. me.commands["pasteplain"] = {
  96. queryCommandState: function() {
  97. return pasteplain ? 1 : 0;
  98. },
  99. execCommand: function() {
  100. pasteplain = !pasteplain | 0;
  101. },
  102. notNeedUndo: 1
  103. };
  104. };