JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
23c58bf9437ea256790919b521a4c538fec7f3dd
[ckeditor.git] / _source / plugins / clipboard / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file Clipboard support\r
8  */\r
9 \r
10 (function()\r
11 {\r
12         // Tries to execute any of the paste, cut or copy commands in IE. Returns a\r
13         // boolean indicating that the operation succeeded.\r
14         var execIECommand = function( editor, command )\r
15         {\r
16                 var doc = editor.document,\r
17                         body = doc.getBody();\r
18 \r
19                 var     enabled = false;\r
20                 var onExec = function()\r
21                 {\r
22                         enabled = true;\r
23                 };\r
24 \r
25                 // The following seems to be the only reliable way to detect that\r
26                 // clipboard commands are enabled in IE. It will fire the\r
27                 // onpaste/oncut/oncopy events only if the security settings allowed\r
28                 // the command to execute.\r
29                 body.on( command, onExec );\r
30 \r
31                 // IE6/7: document.execCommand has problem to paste into positioned element.\r
32                 ( CKEDITOR.env.version > 7 ? doc.$ : doc.$.selection.createRange() ) [ 'execCommand' ]( command );\r
33 \r
34                 body.removeListener( command, onExec );\r
35 \r
36                 return enabled;\r
37         };\r
38 \r
39         // Attempts to execute the Cut and Copy operations.\r
40         var tryToCutCopy =\r
41                 CKEDITOR.env.ie ?\r
42                         function( editor, type )\r
43                         {\r
44                                 return execIECommand( editor, type );\r
45                         }\r
46                 :               // !IE.\r
47                         function( editor, type )\r
48                         {\r
49                                 try\r
50                                 {\r
51                                         // Other browsers throw an error if the command is disabled.\r
52                                         return editor.document.$.execCommand( type );\r
53                                 }\r
54                                 catch( e )\r
55                                 {\r
56                                         return false;\r
57                                 }\r
58                         };\r
59 \r
60         // A class that represents one of the cut or copy commands.\r
61         var cutCopyCmd = function( type )\r
62         {\r
63                 this.type = type;\r
64                 this.canUndo = ( this.type == 'cut' );          // We can't undo copy to clipboard.\r
65         };\r
66 \r
67         cutCopyCmd.prototype =\r
68         {\r
69                 exec : function( editor, data )\r
70                 {\r
71                         var success = tryToCutCopy( editor, this.type );\r
72 \r
73                         if ( !success )\r
74                                 alert( editor.lang.clipboard[ this.type + 'Error' ] );          // Show cutError or copyError.\r
75 \r
76                         return success;\r
77                 }\r
78         };\r
79 \r
80         // Paste command.\r
81         var pasteCmd =\r
82         {\r
83                 canUndo : false,\r
84 \r
85                 exec :\r
86                         CKEDITOR.env.ie ?\r
87                                 function( editor )\r
88                                 {\r
89                                         // Prevent IE from pasting at the begining of the document.\r
90                                         editor.focus();\r
91 \r
92                                         if ( !editor.document.getBody().fire( 'beforepaste' )\r
93                                                  && !execIECommand( editor, 'paste' ) )\r
94                                         {\r
95                                                 editor.fire( 'pasteDialog' );\r
96                                                 return false;\r
97                                         }\r
98                                 }\r
99                         :\r
100                                 function( editor )\r
101                                 {\r
102                                         try\r
103                                         {\r
104                                                 if ( !editor.document.getBody().fire( 'beforepaste' )\r
105                                                          && !editor.document.$.execCommand( 'Paste', false, null ) )\r
106                                                 {\r
107                                                         throw 0;\r
108                                                 }\r
109                                         }\r
110                                         catch ( e )\r
111                                         {\r
112                                                 setTimeout( function()\r
113                                                         {\r
114                                                                 editor.fire( 'pasteDialog' );\r
115                                                         }, 0 );\r
116                                                 return false;\r
117                                         }\r
118                                 }\r
119         };\r
120 \r
121         // Listens for some clipboard related keystrokes, so they get customized.\r
122         var onKey = function( event )\r
123         {\r
124                 if ( this.mode != 'wysiwyg' )\r
125                         return;\r
126 \r
127                 switch ( event.data.keyCode )\r
128                 {\r
129                         // Paste\r
130                         case CKEDITOR.CTRL + 86 :               // CTRL+V\r
131                         case CKEDITOR.SHIFT + 45 :              // SHIFT+INS\r
132 \r
133                                 var body = this.document.getBody();\r
134 \r
135                                 // Simulate 'beforepaste' event for all none-IEs.\r
136                                 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )\r
137                                         event.cancel();\r
138                                 // Simulate 'paste' event for Opera/Firefox2.\r
139                                 else if ( CKEDITOR.env.opera\r
140                                                  || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
141                                         body.fire( 'paste' );\r
142                                 return;\r
143 \r
144                         // Cut\r
145                         case CKEDITOR.CTRL + 88 :               // CTRL+X\r
146                         case CKEDITOR.SHIFT + 46 :              // SHIFT+DEL\r
147 \r
148                                 // Save Undo snapshot.\r
149                                 var editor = this;\r
150                                 this.fire( 'saveSnapshot' );            // Save before paste\r
151                                 setTimeout( function()\r
152                                         {\r
153                                                 editor.fire( 'saveSnapshot' );          // Save after paste\r
154                                         }, 0 );\r
155                 }\r
156         };\r
157 \r
158         // Allow to peek clipboard content by redirecting the\r
159         // pasting content into a temporary bin and grab the content of it.\r
160         function getClipboardData( evt, mode, callback )\r
161         {\r
162                 var doc = this.document;\r
163 \r
164                 // Avoid recursions on 'paste' event for IE.\r
165                 if ( CKEDITOR.env.ie && doc.getById( 'cke_pastebin' ) )\r
166                         return;\r
167 \r
168                 // If the browser supports it, get the data directly\r
169                 if (mode == 'text' && evt.data && evt.data.$.clipboardData)\r
170                 {\r
171                         // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.\r
172                         var plain = evt.data.$.clipboardData.getData( 'text/plain' );\r
173                         if (plain)\r
174                         {\r
175                                 evt.data.preventDefault();\r
176                                 callback( plain );\r
177                                 return;\r
178                         }\r
179                 }\r
180 \r
181                 var sel = this.getSelection(),\r
182                         range = new CKEDITOR.dom.range( doc );\r
183 \r
184                 // Create container to paste into\r
185                 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : 'div', doc );\r
186                 pastebin.setAttribute( 'id', 'cke_pastebin' );\r
187                 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)\r
188                 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );\r
189                 doc.getBody().append( pastebin );\r
190 \r
191                 // It's definitely a better user experience if we make the paste-bin pretty unnoticed\r
192                 // by pulling it off the screen.\r
193                 pastebin.setStyles(\r
194                         {\r
195                                 position : 'absolute',\r
196                                 left : '-1000px',\r
197                                 // Position the bin exactly at the position of the selected element\r
198                                 // to avoid any subsequent document scroll.\r
199                                 top : sel.getStartElement().getDocumentPosition().y + 'px',\r
200                                 width : '1px',\r
201                                 height : '1px',\r
202                                 overflow : 'hidden'\r
203                         });\r
204 \r
205                 var bms = sel.createBookmarks();\r
206 \r
207                 // Turn off design mode temporarily before give focus to the paste bin.\r
208                 if ( mode == 'text' )\r
209                 {\r
210                         if ( CKEDITOR.env.ie )\r
211                         {\r
212                                 var ieRange = doc.getBody().$.createTextRange();\r
213                                 ieRange.moveToElementText( pastebin.$ );\r
214                                 ieRange.execCommand( 'Paste' );\r
215                                 evt.data.preventDefault();\r
216                         }\r
217                         else\r
218                         {\r
219                                 doc.$.designMode = 'off';\r
220                                 pastebin.$.focus();\r
221                         }\r
222                 }\r
223                 else\r
224                 {\r
225                         range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );\r
226                         range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );\r
227                         range.select( true );\r
228                 }\r
229 \r
230                 // Wait a while and grab the pasted contents\r
231                 window.setTimeout( function()\r
232                 {\r
233                         mode == 'text' && !CKEDITOR.env.ie && ( doc.$.designMode = 'on' );\r
234                         pastebin.remove();\r
235 \r
236                         // Grab the HTML contents.\r
237                         // We need to look for a apple style wrapper on webkit it also adds\r
238                         // a div wrapper if you copy/paste the body of the editor.\r
239                         // Remove hidden div and restore selection.\r
240                         var bogusSpan;\r
241                         pastebin = ( CKEDITOR.env.webkit\r
242                                                  && ( bogusSpan = pastebin.getFirst() )\r
243                                                  && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?\r
244                                                         bogusSpan : pastebin );\r
245 \r
246                         sel.selectBookmarks( bms );\r
247                         callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );\r
248                 }, 0 );\r
249         }\r
250 \r
251         // Register the plugin.\r
252         CKEDITOR.plugins.add( 'clipboard',\r
253                 {\r
254                         requires : [ 'dialog', 'htmldataprocessor' ],\r
255                         init : function( editor )\r
256                         {\r
257                                 // Inserts processed data into the editor at the end of the\r
258                                 // events chain.\r
259                                 editor.on( 'paste', function( evt )\r
260                                         {\r
261                                                 var data = evt.data;\r
262                                                 if ( data[ 'html' ] )\r
263                                                         editor.insertHtml( data[ 'html' ] );\r
264                                                 else if ( data[ 'text' ] )\r
265                                                         editor.insertText( data[ 'text' ] );\r
266 \r
267                                         }, null, null, 1000 );\r
268 \r
269                                 editor.on( 'pasteDialog', function( evt )\r
270                                         {\r
271                                                 setTimeout( function()\r
272                                                 {\r
273                                                         // Open default paste dialog.\r
274                                                         editor.openDialog( 'paste' );\r
275                                                 }, 0 );\r
276                                         });\r
277 \r
278                                 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )\r
279                                 {\r
280                                         var lang = editor.lang[ commandName ];\r
281 \r
282                                         editor.addCommand( commandName, command );\r
283                                         editor.ui.addButton( buttonName,\r
284                                                 {\r
285                                                         label : lang,\r
286                                                         command : commandName\r
287                                                 });\r
288 \r
289                                         // If the "menu" plugin is loaded, register the menu item.\r
290                                         if ( editor.addMenuItems )\r
291                                         {\r
292                                                 editor.addMenuItem( commandName,\r
293                                                         {\r
294                                                                 label : lang,\r
295                                                                 command : commandName,\r
296                                                                 group : 'clipboard',\r
297                                                                 order : ctxMenuOrder\r
298                                                         });\r
299                                         }\r
300                                 }\r
301 \r
302                                 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );\r
303                                 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );\r
304                                 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );\r
305 \r
306                                 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );\r
307 \r
308                                 editor.on( 'key', onKey, editor );\r
309 \r
310                                 var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';\r
311 \r
312                                 // We'll be catching all pasted content in one line, regardless of whether the\r
313                                 // it's introduced by a document command execution (e.g. toolbar buttons) or\r
314                                 // user paste behaviors. (e.g. Ctrl-V)\r
315                                 editor.on( 'contentDom', function()\r
316                                 {\r
317                                         var body = editor.document.getBody();\r
318                                         body.on( ( (mode == 'text' && CKEDITOR.env.ie) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',\r
319                                                 function( evt )\r
320                                                 {\r
321                                                         if ( depressBeforePasteEvent )\r
322                                                                 return;\r
323 \r
324                                                         getClipboardData.call( editor, evt, mode, function ( data )\r
325                                                         {\r
326                                                                 // The very last guard to make sure the\r
327                                                                 // paste has successfully happened.\r
328                                                                 if ( !data )\r
329                                                                         return;\r
330 \r
331                                                                 var dataTransfer = {};\r
332                                                                 dataTransfer[ mode ] = data;\r
333                                                                 editor.fire( 'paste', dataTransfer );\r
334                                                         } );\r
335                                                 });\r
336 \r
337                                 });\r
338 \r
339                                 // If the "contextmenu" plugin is loaded, register the listeners.\r
340                                 if ( editor.contextMenu )\r
341                                 {\r
342                                         var depressBeforePasteEvent;\r
343                                         function stateFromNamedCommand( command )\r
344                                         {\r
345                                                 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste',\r
346                                                 // guard to distinguish from the ordinary sources( either\r
347                                                 // keyboard paste or execCommand ) (#4874).\r
348                                                 CKEDITOR.env.ie && command == 'Paste'&& ( depressBeforePasteEvent = 1 );\r
349 \r
350                                                 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
351                                                 depressBeforePasteEvent = 0;\r
352                                                 return retval;\r
353                                         }\r
354 \r
355                                         editor.contextMenu.addListener( function()\r
356                                                 {\r
357                                                         return {\r
358                                                                 cut : stateFromNamedCommand( 'Cut' ),\r
359 \r
360                                                                 // Browser bug: 'Cut' has the correct states for both Copy and Cut.\r
361                                                                 copy : stateFromNamedCommand( 'Cut' ),\r
362                                                                 paste : CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' )\r
363                                                         };\r
364                                                 });\r
365                                 }\r
366                         }\r
367                 });\r
368 })();\r
369 \r
370 /**\r
371  * Fired when a clipboard operation is about to be taken into the editor.\r
372  * Listeners can manipulate the data to be pasted before having it effectively\r
373  * inserted into the document.\r
374  * @name CKEDITOR.editor#paste\r
375  * @since 3.1\r
376  * @event\r
377  * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.\r
378  * @param {String} [data.text] The plain text data to be pasted, available when plain text operations are to used. If not available, e.data.html will be defined.\r
379  */\r