javascript – 使用“pastefromword”过滤CKEditor 3中的所有粘贴内容

前端之家收集整理的这篇文章主要介绍了javascript – 使用“pastefromword”过滤CKEditor 3中的所有粘贴内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CKEditor是一个很棒的编辑器,pastefromword插件非常好.我想将插件提供的过滤应用于所有粘贴的文本.例如,从单词粘贴时,将删除所有字体和大小.从电子邮件粘贴时不会发生这种情况.

也就是说,我提出了以下解决方案并将其发布在此处以获得一些反馈.我想知道我是否过于复杂,或者是否有更简单的方法.我只是从pastefromword / plugin.js中复制了代码.

通过我的自定义config.js

  1. ...
  2. CKEDITOR.config.pasteFromWordCleanupFile = '/pastefromword.js';
  3. ...
  4. CKEDITOR.on( 'instanceReady',function( ev ) {
  5. /**
  6. * Paste event to apply Paste From Word filtering on all text.
  7. *
  8. * The pastefromword plugin will only process text that has tell-tale signs
  9. * it is from Word. Use this hook to treat all pasted text as if
  10. * it is coming from Word.
  11. *
  12. * This method is a slightly modified version of code found in
  13. * plugins/pastefromword/plugin.js
  14. */
  15. ev.editor.on( 'paste',function( evt ) {
  16. var data = evt.data,editor = evt.editor,content;
  17.  
  18. /**
  19. * "pasteFromWordHappened" is a custom property set in custom
  20. * pastefromword.js,so that filtering does not happen twice for content
  21. * actually coming from Word. It's a dirty hack I know.
  22. */
  23. if( editor.pasteFromWordHappened ) {
  24. // Reset property and exit paste event
  25. editor.pasteFromWordHappened = 0;
  26. return;
  27. }
  28.  
  29. var loadRules = function( callback ) {
  30. var isLoaded = CKEDITOR.cleanWord;
  31.  
  32. if( isLoaded ) {
  33. callback();
  34. }
  35. else {
  36. CKEDITOR.scriptLoader.load( CKEDITOR.config.pasteFromWordCleanupFile,callback,null,false,true );
  37. }
  38.  
  39. return !isLoaded;
  40. };
  41.  
  42. content = data['html'];
  43.  
  44. // No need to filter text if html tags are not presence,so perform a regex
  45. // to test for html tags.
  46. if( content && (/<[^<]+?>/).test(content) ) {
  47. var isLazyLoad = loadRules( function(){
  48. if( isLazyLoad ) {
  49. editor.fire('paste',data);
  50. }
  51. else {
  52. data[ 'html' ] = CKEDITOR.cleanWord( content,editor );
  53. // Reset property or if user tries to paste again,it won't work
  54. editor.pasteFromWordHappened = 0;
  55. }
  56. });
  57.  
  58. isLazyLoad && evt.cancel();
  59. }
  60.  
  61. });
  62. });

解决方法

猜你在找的JavaScript相关文章