SafePaste.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. define("dojox/editor/plugins/SafePaste", [
  2. "dojo",
  3. "dijit",
  4. "dojox",
  5. "dijit/Dialog",
  6. "dojo/_base/connect",
  7. "dojo/_base/declare",
  8. "dojo/i18n",
  9. "dojo/string",
  10. "dojox/editor/plugins/PasteFromWord",
  11. "dojo/i18n!dojox/editor/plugins/nls/SafePaste",
  12. "dojo/i18n!dijit/nls/common",
  13. "dojo/i18n!dijit/_editor/nls/commands"
  14. ], function(dojo, dijit, dojox) {
  15. dojo.declare("dojox.editor.plugins.SafePaste", [dojox.editor.plugins.PasteFromWord],{
  16. // summary:
  17. // This plugin extends from the PasteFromWord plugin and provides
  18. // 'safe pasting', meaning that it will not allow keyboard/menu pasting
  19. // into the dijit editor. It still runs all of the word cleanup code,
  20. // including script strippers. If you use this plugin, you don't need to
  21. // use the 'PasteFromWord Plugin'
  22. _initButton: function(){
  23. // summary:
  24. // Over-ride the editor paste controls
  25. // Create instance local copy.
  26. this._filters = this._filters.slice(0);
  27. var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "SafePaste");
  28. dojo.mixin(strings, dojo.i18n.getLocalization("dijit", "common"));
  29. strings.cancel = strings.buttonCancel;
  30. dojo.mixin(strings, dojo.i18n.getLocalization("dijit._editor", "commands"));
  31. this._uId = dijit.getUniqueId(this.editor.id);
  32. strings.uId = this._uId;
  33. strings.width = this.width || "400px";
  34. strings.height = this.height || "300px";
  35. this._dialog = new dijit.Dialog({title: strings["paste"]}).placeAt(dojo.body());
  36. this._dialog.set("content", dojo.string.substitute(this._template, strings));
  37. // Make it translucent so we can fade in the window when the RTE is created.
  38. // the RTE has to be created 'visible, and this is a ncie trick to make the creation
  39. // 'pretty'.
  40. dojo.style(dojo.byId(this._uId + "_rte"), "opacity", 0.001);
  41. // Link up the action buttons to perform the insert or cleanup.
  42. this.connect(dijit.byId(this._uId + "_paste"), "onClick", "_paste");
  43. this.connect(dijit.byId(this._uId + "_cancel"), "onClick", "_cancel");
  44. this.connect(this._dialog, "onHide", "_clearDialog");
  45. // Create regular expressions for sripping out user-specified tags and register
  46. // them with the filters.
  47. dojo.forEach(this.stripTags, function(tag){
  48. var tagName = tag + "";
  49. var rxStartTag = new RegExp("<\\s*" + tagName + "[^>]*>", "igm");
  50. var rxEndTag = new RegExp("<\\\\?\\/\\s*" + tagName + "\\s*>", "igm");
  51. this._filters.push({regexp:
  52. rxStartTag,
  53. handler: ""
  54. });
  55. this._filters.push({regexp:
  56. rxEndTag,
  57. handler: ""
  58. });
  59. }, this);
  60. },
  61. updateState: function(){
  62. // summary:
  63. // Overrides _Plugin.updateState().
  64. // tags:
  65. // protected
  66. // Do nothing.
  67. },
  68. setEditor: function(editor){
  69. // summary:
  70. // Over-ride for the setting of the editor.
  71. // editor: Object
  72. // The editor to configure for this plugin to use.
  73. this.editor = editor;
  74. this._initButton();
  75. this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
  76. var spFunc = dojo.hitch(this, function(e){
  77. if(e){
  78. dojo.stopEvent(e);
  79. }
  80. this._openDialog();
  81. return true;
  82. });
  83. this.connect(this.editor.editNode, "onpaste", spFunc);
  84. this.editor._pasteImpl = spFunc;
  85. }));
  86. }
  87. });
  88. // Register this plugin.
  89. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  90. if(o.plugin){ return; }
  91. var name = o.args.name.toLowerCase();
  92. if(name === "safepaste"){
  93. o.plugin = new dojox.editor.plugins.SafePaste({
  94. width: (o.args.hasOwnProperty("width"))?o.args.width:"400px",
  95. height: (o.args.hasOwnProperty("height"))?o.args.width:"300px",
  96. stripTags: (o.args.hasOwnProperty("stripTags"))?o.args.stripTags:null
  97. });
  98. }
  99. });
  100. return dojox.editor.plugins.SafePaste;
  101. });