JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
342d70888f6b4105bdf6e612d6b82428c6d5801e
[ckeditor.git] / _source / plugins / clipboard / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, 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 = 0;\r
20                 var onExec = function()\r
21                 {\r
22                         enabled = 1;\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, false, null );\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                 this.startDisabled = true;\r
66         };\r
67 \r
68         cutCopyCmd.prototype =\r
69         {\r
70                 exec : function( editor, data )\r
71                 {\r
72                         this.type == 'cut' && fixCut( editor );\r
73 \r
74                         var success = tryToCutCopy( editor, this.type );\r
75 \r
76                         if ( !success )\r
77                                 alert( editor.lang.clipboard[ this.type + 'Error' ] );          // Show cutError or copyError.\r
78 \r
79                         return success;\r
80                 }\r
81         };\r
82 \r
83         // Paste command.\r
84         var pasteCmd =\r
85         {\r
86                 canUndo : false,\r
87 \r
88                 exec :\r
89                         CKEDITOR.env.ie ?\r
90                                 function( editor )\r
91                                 {\r
92                                         // Prevent IE from pasting at the begining of the document.\r
93                                         editor.focus();\r
94 \r
95                                         if ( !editor.document.getBody().fire( 'beforepaste' )\r
96                                                  && !execIECommand( editor, 'paste' ) )\r
97                                         {\r
98                                                 editor.fire( 'pasteDialog' );\r
99                                                 return false;\r
100                                         }\r
101                                 }\r
102                         :\r
103                                 function( editor )\r
104                                 {\r
105                                         try\r
106                                         {\r
107                                                 if ( !editor.document.getBody().fire( 'beforepaste' )\r
108                                                          && !editor.document.$.execCommand( 'Paste', false, null ) )\r
109                                                 {\r
110                                                         throw 0;\r
111                                                 }\r
112                                         }\r
113                                         catch ( e )\r
114                                         {\r
115                                                 setTimeout( function()\r
116                                                         {\r
117                                                                 editor.fire( 'pasteDialog' );\r
118                                                         }, 0 );\r
119                                                 return false;\r
120                                         }\r
121                                 }\r
122         };\r
123 \r
124         // Listens for some clipboard related keystrokes, so they get customized.\r
125         var onKey = function( event )\r
126         {\r
127                 if ( this.mode != 'wysiwyg' )\r
128                         return;\r
129 \r
130                 switch ( event.data.keyCode )\r
131                 {\r
132                         // Paste\r
133                         case CKEDITOR.CTRL + 86 :               // CTRL+V\r
134                         case CKEDITOR.SHIFT + 45 :              // SHIFT+INS\r
135 \r
136                                 var body = this.document.getBody();\r
137 \r
138                                 // Simulate 'beforepaste' event for all none-IEs.\r
139                                 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )\r
140                                         event.cancel();\r
141                                 // Simulate 'paste' event for Opera/Firefox2.\r
142                                 else if ( CKEDITOR.env.opera\r
143                                                  || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
144                                         body.fire( 'paste' );\r
145                                 return;\r
146 \r
147                         // Cut\r
148                         case CKEDITOR.CTRL + 88 :               // CTRL+X\r
149                         case CKEDITOR.SHIFT + 46 :              // SHIFT+DEL\r
150 \r
151                                 // Save Undo snapshot.\r
152                                 var editor = this;\r
153                                 this.fire( 'saveSnapshot' );            // Save before paste\r
154                                 setTimeout( function()\r
155                                         {\r
156                                                 editor.fire( 'saveSnapshot' );          // Save after paste\r
157                                         }, 0 );\r
158                 }\r
159         };\r
160 \r
161         // Allow to peek clipboard content by redirecting the\r
162         // pasting content into a temporary bin and grab the content of it.\r
163         function getClipboardData( evt, mode, callback )\r
164         {\r
165                 var doc = this.document;\r
166 \r
167                 // Avoid recursions on 'paste' event or consequent paste too fast. (#5730)\r
168                 if ( doc.getById( 'cke_pastebin' ) )\r
169                         return;\r
170 \r
171                 // If the browser supports it, get the data directly\r
172                 if ( mode == 'text' && evt.data && evt.data.$.clipboardData )\r
173                 {\r
174                         // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.\r
175                         var plain = evt.data.$.clipboardData.getData( 'text/plain' );\r
176                         if ( plain )\r
177                         {\r
178                                 evt.data.preventDefault();\r
179                                 callback( plain );\r
180                                 return;\r
181                         }\r
182                 }\r
183 \r
184                 var sel = this.getSelection(),\r
185                         range = new CKEDITOR.dom.range( doc );\r
186 \r
187                 // Create container to paste into\r
188                 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc );\r
189                 pastebin.setAttribute( 'id', 'cke_pastebin' );\r
190                 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)\r
191                 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );\r
192                 doc.getBody().append( pastebin );\r
193 \r
194                 pastebin.setStyles(\r
195                         {\r
196                                 position : 'absolute',\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                 // It's definitely a better user experience if we make the paste-bin pretty unnoticed\r
206                 // by pulling it off the screen.\r
207                 pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' );\r
208 \r
209                 var bms = sel.createBookmarks();\r
210 \r
211                 // Turn off design mode temporarily before give focus to the paste bin.\r
212                 if ( mode == 'text' )\r
213                 {\r
214                         if ( CKEDITOR.env.ie )\r
215                         {\r
216                                 var ieRange = doc.getBody().$.createTextRange();\r
217                                 ieRange.moveToElementText( pastebin.$ );\r
218                                 ieRange.execCommand( 'Paste' );\r
219                                 evt.data.preventDefault();\r
220                         }\r
221                         else\r
222                                 pastebin.$.focus();\r
223                 }\r
224                 else\r
225                 {\r
226                         range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );\r
227                         range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );\r
228                         range.select( true );\r
229                 }\r
230 \r
231                 var editor  = this;\r
232                 // Wait a while and grab the pasted contents\r
233                 window.setTimeout( function()\r
234                 {\r
235                         mode == 'text' && CKEDITOR.env.gecko && editor.focusGrabber.focus();\r
236                         pastebin.remove();\r
237 \r
238                         // Grab the HTML contents.\r
239                         // We need to look for a apple style wrapper on webkit it also adds\r
240                         // a div wrapper if you copy/paste the body of the editor.\r
241                         // Remove hidden div and restore selection.\r
242                         var bogusSpan;\r
243                         pastebin = ( CKEDITOR.env.webkit\r
244                                                  && ( bogusSpan = pastebin.getFirst() )\r
245                                                  && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?\r
246                                                         bogusSpan : pastebin );\r
247 \r
248                         sel.selectBookmarks( bms );\r
249                         callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );\r
250                 }, 0 );\r
251         }\r
252 \r
253         // Cutting off control type element in IE standards breaks the selection entirely. (#4881)\r
254         function fixCut( editor )\r
255         {\r
256                 if ( !CKEDITOR.env.ie || CKEDITOR.env.quirks )\r
257                         return;\r
258 \r
259                 var sel = editor.getSelection();\r
260                 var control;\r
261                 if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) )\r
262                 {\r
263                         var range = sel.getRanges()[ 0 ];\r
264                         var dummy = editor.document.createText( '' );\r
265                         dummy.insertBefore( control );\r
266                         range.setStartBefore( dummy );\r
267                         range.setEndAfter( control );\r
268                         sel.selectRanges( [ range ] );\r
269 \r
270                         // Clear up the fix if the paste wasn't succeeded.\r
271                         setTimeout( function()\r
272                         {\r
273                                 // Element still online?\r
274                                 if ( control.getParent() )\r
275                                 {\r
276                                         dummy.remove();\r
277                                         sel.selectElement( control );\r
278                                 }\r
279                         }, 0 );\r
280                 }\r
281         }\r
282 \r
283         var depressBeforeEvent;\r
284         function stateFromNamedCommand( command, editor )\r
285         {\r
286                 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',\r
287                 // guard to distinguish from the ordinary sources( either\r
288                 // keyboard paste or execCommand ) (#4874).\r
289                 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
290 \r
291                 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
292                 depressBeforeEvent = 0;\r
293                 return retval;\r
294         }\r
295 \r
296         var inReadOnly;\r
297         function setToolbarStates()\r
298         {\r
299                 if ( this.mode != 'wysiwyg' )\r
300                         return;\r
301 \r
302                 this.getCommand( 'cut' ).setState( inReadOnly ? CKEDITOR.TRISTATE_DISABLED : stateFromNamedCommand( 'Cut', this ) );\r
303                 this.getCommand( 'copy' ).setState( stateFromNamedCommand( 'Copy', this ) );\r
304                 var pasteState = inReadOnly ? CKEDITOR.TRISTATE_DISABLED :\r
305                                                 CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', this );\r
306                 this.fire( 'pasteState', pasteState );\r
307         }\r
308 \r
309         // Register the plugin.\r
310         CKEDITOR.plugins.add( 'clipboard',\r
311                 {\r
312                         requires : [ 'dialog', 'htmldataprocessor' ],\r
313                         init : function( editor )\r
314                         {\r
315                                 // Inserts processed data into the editor at the end of the\r
316                                 // events chain.\r
317                                 editor.on( 'paste', function( evt )\r
318                                         {\r
319                                                 var data = evt.data;\r
320                                                 if ( data[ 'html' ] )\r
321                                                         editor.insertHtml( data[ 'html' ] );\r
322                                                 else if ( data[ 'text' ] )\r
323                                                         editor.insertText( data[ 'text' ] );\r
324 \r
325                                         }, null, null, 1000 );\r
326 \r
327                                 editor.on( 'pasteDialog', function( evt )\r
328                                         {\r
329                                                 setTimeout( function()\r
330                                                 {\r
331                                                         // Open default paste dialog.\r
332                                                         editor.openDialog( 'paste' );\r
333                                                 }, 0 );\r
334                                         });\r
335 \r
336                                 editor.on( 'pasteState', function( evt )\r
337                                         {\r
338                                                 editor.getCommand( 'paste' ).setState( evt.data );\r
339                                         });\r
340 \r
341                                 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )\r
342                                 {\r
343                                         var lang = editor.lang[ commandName ];\r
344 \r
345                                         editor.addCommand( commandName, command );\r
346                                         editor.ui.addButton( buttonName,\r
347                                                 {\r
348                                                         label : lang,\r
349                                                         command : commandName\r
350                                                 });\r
351 \r
352                                         // If the "menu" plugin is loaded, register the menu item.\r
353                                         if ( editor.addMenuItems )\r
354                                         {\r
355                                                 editor.addMenuItem( commandName,\r
356                                                         {\r
357                                                                 label : lang,\r
358                                                                 command : commandName,\r
359                                                                 group : 'clipboard',\r
360                                                                 order : ctxMenuOrder\r
361                                                         });\r
362                                         }\r
363                                 }\r
364 \r
365                                 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );\r
366                                 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );\r
367                                 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );\r
368 \r
369                                 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );\r
370 \r
371                                 editor.on( 'key', onKey, editor );\r
372 \r
373                                 var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';\r
374 \r
375                                 // We'll be catching all pasted content in one line, regardless of whether the\r
376                                 // it's introduced by a document command execution (e.g. toolbar buttons) or\r
377                                 // user paste behaviors. (e.g. Ctrl-V)\r
378                                 editor.on( 'contentDom', function()\r
379                                 {\r
380                                         var body = editor.document.getBody();\r
381                                         body.on( ( ( mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',\r
382                                                 function( evt )\r
383                                                 {\r
384                                                         if ( depressBeforeEvent )\r
385                                                                 return;\r
386 \r
387                                                         getClipboardData.call( editor, evt, mode, function ( data )\r
388                                                         {\r
389                                                                 // The very last guard to make sure the\r
390                                                                 // paste has successfully happened.\r
391                                                                 if ( !data )\r
392                                                                         return;\r
393 \r
394                                                                 var dataTransfer = {};\r
395                                                                 dataTransfer[ mode ] = data;\r
396                                                                 editor.fire( 'paste', dataTransfer );\r
397                                                         } );\r
398                                                 });\r
399 \r
400                                         body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );\r
401 \r
402                                         body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );\r
403                                         body.on( 'keyup', setToolbarStates, editor );\r
404                                 });\r
405 \r
406                                 // For improved performance, we're checking the readOnly state on selectionChange instead of hooking a key event for that.\r
407                                 editor.on( 'selectionChange', function( evt )\r
408                                 {\r
409                                         inReadOnly = evt.data.selection.getRanges()[ 0 ].checkReadOnly();\r
410                                         setToolbarStates.call( editor );\r
411                                 });\r
412 \r
413                                 // If the "contextmenu" plugin is loaded, register the listeners.\r
414                                 if ( editor.contextMenu )\r
415                                 {\r
416                                         editor.contextMenu.addListener( function( element, selection )\r
417                                                 {\r
418                                                         var readOnly = selection.getRanges()[ 0 ].checkReadOnly();\r
419                                                         return {\r
420                                                                 cut : !readOnly && stateFromNamedCommand( 'Cut', editor ),\r
421                                                                 copy : stateFromNamedCommand( 'Copy', editor ),\r
422                                                                 paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', editor ) )\r
423                                                         };\r
424                                                 });\r
425                                 }\r
426                         }\r
427                 });\r
428 })();\r
429 \r
430 /**\r
431  * Fired when a clipboard operation is about to be taken into the editor.\r
432  * Listeners can manipulate the data to be pasted before having it effectively\r
433  * inserted into the document.\r
434  * @name CKEDITOR.editor#paste\r
435  * @since 3.1\r
436  * @event\r
437  * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.\r
438  * @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
439  */\r
440 \r
441 /**\r
442  * Internal event to open the Paste dialog\r
443  * @name CKEDITOR.editor#pasteDialog\r
444  * @event\r
445  */\r