| 1 |
define([ |
| 2 |
"dojo", |
| 3 |
"dijit", |
| 4 |
"dojox", |
| 5 |
"dijit/_editor/_Plugin", |
| 6 |
"dijit/_base/manager", |
| 7 |
"dijit/_editor/RichText", |
| 8 |
"dijit/form/Button", |
| 9 |
"dijit/Dialog", |
| 10 |
"dojox/html/format", |
| 11 |
"dojo/_base/connect", |
| 12 |
"dojo/_base/declare", |
| 13 |
"dojo/i18n", |
| 14 |
"dojo/string", |
| 15 |
"dojo/i18n!dojox/editor/plugins/nls/PasteFromWord", |
| 16 |
"dojo/i18n!dijit/nls/common", |
| 17 |
"dojo/i18n!dijit/_editor/nls/commands" |
| 18 |
], function(dojo, dijit, dojox, _Plugin) { |
| 19 |
|
| 20 |
var PasteFromWord = dojo.declare("dojox.editor.plugins.PasteFromWord", _Plugin, { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
iconClassPrefix: "dijitAdditionalEditorIcon", |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
width: "400px", |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
height: "300px", |
| 40 |
|
| 41 |
_template: ["<div class='dijitPasteFromWordEmbeddedRTE'>", |
| 42 |
"<div style='width: ${width}; padding-top: 5px; padding-bottom: 5px;'>${instructions}</div>", |
| 43 |
"<div id='${uId}_rte' style='width: ${width}; height: ${height}'></div>", |
| 44 |
"<table style='width: ${width}' tabindex='-1'>", |
| 45 |
"<tbody>", |
| 46 |
"<tr>", |
| 47 |
"<td align='center'>", |
| 48 |
"<button type='button' dojoType='dijit.form.Button' id='${uId}_paste'>${paste}</button>", |
| 49 |
" ", |
| 50 |
"<button type='button' dojoType='dijit.form.Button' id='${uId}_cancel'>${buttonCancel}</button>", |
| 51 |
"</td>", |
| 52 |
"</tr>", |
| 53 |
"</tbody>", |
| 54 |
"</table>", |
| 55 |
"</div>"].join(""), |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
_filters: [ |
| 64 |
|
| 65 |
{regexp: /(<meta\s*[^>]*\s*>)|(<\s*link\s* href="file:[^>]*\s*>)|(<\/?\s*\w+:[^>]*\s*>)/gi, handler: ""}, |
| 66 |
|
| 67 |
{regexp: /(?:<style([^>]*)>([\s\S]*?)<\/style>|<link\s+(?=[^>]*rel=['"]?stylesheet)([^>]*?href=(['"])([^>]*?)\4[^>\/]*)\/?>)/gi, handler: ""}, |
| 68 |
|
| 69 |
{regexp: /(class="Mso[^"]*")|(<!--(.|\s){1,}?-->)/gi, handler: ""}, |
| 70 |
|
| 71 |
{regexp: /(<p[^>]*>\s*(\ |\u00A0)*\s*<\/p[^>]*>)|(<p[^>]*>\s*<font[^>]*>\s*(\ |\u00A0)*\s*<\/\s*font\s*>\s<\/p[^>]*>)/ig, handler: ""}, |
| 72 |
|
| 73 |
{regexp: /(style="[^"]*mso-[^;][^"]*")|(style="margin:\s*[^;"]*;")/gi, handler: ""}, |
| 74 |
|
| 75 |
{regexp: /(<\s*script[^>]*>((.|\s)*?)<\\?\/\s*script\s*>)|(<\s*script\b([^<>]|\s)*>?)|(<[^>]*=(\s|)*[("|')]javascript:[^$1][(\s|.)]*[$1][^>]*>)/ig, handler: ""}, |
| 76 |
|
| 77 |
{regexp: /<(\/?)o\:p[^>]*>/gi, handler: ""} |
| 78 |
], |
| 79 |
|
| 80 |
_initButton: function(){ |
| 81 |
|
| 82 |
|
| 83 |
this._filters = this._filters.slice(0); |
| 84 |
|
| 85 |
var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "PasteFromWord"); |
| 86 |
dojo.mixin(strings, dojo.i18n.getLocalization("dijit", "common")); |
| 87 |
dojo.mixin(strings, dojo.i18n.getLocalization("dijit._editor", "commands")); |
| 88 |
this.button = new dijit.form.Button({ |
| 89 |
label: strings["pasteFromWord"], |
| 90 |
showLabel: false, |
| 91 |
iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "PasteFromWord", |
| 92 |
tabIndex: "-1", |
| 93 |
onClick: dojo.hitch(this, "_openDialog") |
| 94 |
}); |
| 95 |
|
| 96 |
this._uId = dijit.getUniqueId(this.editor.id); |
| 97 |
|
| 98 |
strings.uId = this._uId; |
| 99 |
strings.width = this.width || "400px"; |
| 100 |
strings.height = this.height || "300px"; |
| 101 |
|
| 102 |
this._dialog = new dijit.Dialog({title: strings["pasteFromWord"]}).placeAt(dojo.body()); |
| 103 |
this._dialog.set("content", dojo.string.substitute(this._template, strings)); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
dojo.style(dojo.byId(this._uId + "_rte"), "opacity", 0.001); |
| 109 |
|
| 110 |
|
| 111 |
this.connect(dijit.byId(this._uId + "_paste"), "onClick", "_paste"); |
| 112 |
this.connect(dijit.byId(this._uId + "_cancel"), "onClick", "_cancel"); |
| 113 |
this.connect(this._dialog, "onHide", "_clearDialog"); |
| 114 |
}, |
| 115 |
|
| 116 |
updateState: function(){ |
| 117 |
|
| 118 |
|
| 119 |
this.button.set("disabled", this.get("disabled")); |
| 120 |
}, |
| 121 |
|
| 122 |
setEditor: function(editor){ |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
this.editor = editor; |
| 128 |
this._initButton(); |
| 129 |
}, |
| 130 |
|
| 131 |
_openDialog: function(){ |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
this._dialog.show(); |
| 137 |
if(!this._rte){ |
| 138 |
|
| 139 |
|
| 140 |
setTimeout(dojo.hitch(this, function() { |
| 141 |
this._rte = new dijit._editor.RichText({height: this.height || "300px"}, this._uId + "_rte"); |
| 142 |
this._rte.startup(); |
| 143 |
this._rte.onLoadDeferred.addCallback(dojo.hitch(this, function() { |
| 144 |
dojo.animateProperty({ |
| 145 |
node: this._rte.domNode, properties: { opacity: { start: 0.001, end: 1.0 } } |
| 146 |
}).play(); |
| 147 |
})); |
| 148 |
}), 100); |
| 149 |
} |
| 150 |
}, |
| 151 |
|
| 152 |
_paste: function(){ |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
var content = dojox.html.format.prettyPrint(this._rte.get("value")); |
| 162 |
|
| 163 |
|
| 164 |
this._dialog.hide(); |
| 165 |
|
| 166 |
|
| 167 |
var i; |
| 168 |
for(i = 0; i < this._filters.length; i++){ |
| 169 |
var filter = this._filters[i]; |
| 170 |
content = content.replace(filter.regexp, filter.handler); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
content = dojox.html.format.prettyPrint(content); |
| 176 |
|
| 177 |
|
| 178 |
this.editor.focus(); |
| 179 |
this.editor.execCommand("inserthtml", content); |
| 180 |
}, |
| 181 |
|
| 182 |
_cancel: function(){ |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
this._dialog.hide(); |
| 189 |
}, |
| 190 |
|
| 191 |
_clearDialog: function(){ |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
this._rte.set("value", ""); |
| 198 |
}, |
| 199 |
|
| 200 |
destroy: function(){ |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if(this._rte){ |
| 206 |
this._rte.destroy(); |
| 207 |
} |
| 208 |
if(this._dialog){ |
| 209 |
this._dialog.destroyRecursive(); |
| 210 |
} |
| 211 |
delete this._dialog; |
| 212 |
delete this._rte; |
| 213 |
this.inherited(arguments); |
| 214 |
} |
| 215 |
|
| 216 |
}); |
| 217 |
|
| 218 |
|
| 219 |
dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){ |
| 220 |
if(o.plugin){ return; } |
| 221 |
var name = o.args.name.toLowerCase(); |
| 222 |
if(name === "pastefromword"){ |
| 223 |
o.plugin = new PasteFromWord({ |
| 224 |
width: ("width" in o.args)?o.args.width:"400px", |
| 225 |
height: ("height" in o.args)?o.args.width:"300px" |
| 226 |
}); |
| 227 |
} |
| 228 |
}); |
| 229 |
|
| 230 |
return PasteFromWord; |
| 231 |
}); |