JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / clipboard / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, 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, 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 'paste' event for Opera/Firefox2.\r
139                                 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         function cancel( evt ) { evt.cancel(); }\r
159 \r
160         // Allow to peek clipboard content by redirecting the\r
161         // pasting content into a temporary bin and grab the content of it.\r
162         function getClipboardData( evt, mode, callback )\r
163         {\r
164                 var doc = this.document;\r
165 \r
166                 // Avoid recursions on 'paste' event or consequent paste too fast. (#5730)\r
167                 if ( doc.getById( 'cke_pastebin' ) )\r
168                         return;\r
169 \r
170                 // If the browser supports it, get the data directly\r
171                 if ( mode == 'text' && evt.data && evt.data.$.clipboardData )\r
172                 {\r
173                         // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.\r
174                         var plain = evt.data.$.clipboardData.getData( 'text/plain' );\r
175                         if ( plain )\r
176                         {\r
177                                 evt.data.preventDefault();\r
178                                 callback( plain );\r
179                                 return;\r
180                         }\r
181                 }\r
182 \r
183                 var sel = this.getSelection(),\r
184                         range = new CKEDITOR.dom.range( doc );\r
185 \r
186                 // Create container to paste into\r
187                 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc );\r
188                 pastebin.setAttribute( 'id', 'cke_pastebin' );\r
189                 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)\r
190                 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );\r
191                 doc.getBody().append( pastebin );\r
192 \r
193                 pastebin.setStyles(\r
194                         {\r
195                                 position : 'absolute',\r
196                                 // Position the bin exactly at the position of the selected element\r
197                                 // to avoid any subsequent document scroll.\r
198                                 top : sel.getStartElement().getDocumentPosition().y + 'px',\r
199                                 width : '1px',\r
200                                 height : '1px',\r
201                                 overflow : 'hidden'\r
202                         });\r
203 \r
204                 // It's definitely a better user experience if we make the paste-bin pretty unnoticed\r
205                 // by pulling it off the screen.\r
206                 pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' );\r
207 \r
208                 var bms = sel.createBookmarks();\r
209 \r
210                 this.on( 'selectionChange', cancel, null, null, 0 );\r
211 \r
212                 // Turn off design mode temporarily before give focus to the paste bin.\r
213                 if ( mode == 'text' )\r
214                         pastebin.$.focus();\r
215                 else\r
216                 {\r
217                         range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );\r
218                         range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );\r
219                         range.select( true );\r
220                 }\r
221 \r
222                 var editor  = this;\r
223                 // Wait a while and grab the pasted contents\r
224                 window.setTimeout( function()\r
225                 {\r
226                         // Restore properly the document focus. (#5684, #8849)\r
227                         editor.document.getBody().focus();\r
228 \r
229                         editor.removeListener( 'selectionChange', cancel );\r
230 \r
231                         // Grab the HTML contents.\r
232                         // We need to look for a apple style wrapper on webkit it also adds\r
233                         // a div wrapper if you copy/paste the body of the editor.\r
234                         // Remove hidden div and restore selection.\r
235                         var bogusSpan;\r
236                         pastebin = ( CKEDITOR.env.webkit\r
237                                                  && ( bogusSpan = pastebin.getFirst() )\r
238                                                  && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?\r
239                                                         bogusSpan : pastebin );\r
240 \r
241                         // IE7: selection must go before removing paste. (#8691)\r
242                         sel.selectBookmarks( bms );\r
243                         pastebin.remove();\r
244                         callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );\r
245                 }, 0 );\r
246         }\r
247 \r
248         // Cutting off control type element in IE standards breaks the selection entirely. (#4881)\r
249         function fixCut( editor )\r
250         {\r
251                 if ( !CKEDITOR.env.ie || CKEDITOR.env.quirks )\r
252                         return;\r
253 \r
254                 var sel = editor.getSelection();\r
255                 var control;\r
256                 if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) )\r
257                 {\r
258                         var range = sel.getRanges()[ 0 ];\r
259                         var dummy = editor.document.createText( '' );\r
260                         dummy.insertBefore( control );\r
261                         range.setStartBefore( dummy );\r
262                         range.setEndAfter( control );\r
263                         sel.selectRanges( [ range ] );\r
264 \r
265                         // Clear up the fix if the paste wasn't succeeded.\r
266                         setTimeout( function()\r
267                         {\r
268                                 // Element still online?\r
269                                 if ( control.getParent() )\r
270                                 {\r
271                                         dummy.remove();\r
272                                         sel.selectElement( control );\r
273                                 }\r
274                         }, 0 );\r
275                 }\r
276         }\r
277 \r
278         var depressBeforeEvent;\r
279         function stateFromNamedCommand( command, editor )\r
280         {\r
281                 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',\r
282                 // guard to distinguish from the ordinary sources( either\r
283                 // keyboard paste or execCommand ) (#4874).\r
284                 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
285 \r
286                 var retval = CKEDITOR.TRISTATE_OFF;\r
287                 try { retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; }catch( er ){}\r
288 \r
289                 depressBeforeEvent = 0;\r
290                 return retval;\r
291         }\r
292 \r
293         var inReadOnly;\r
294         function setToolbarStates()\r
295         {\r
296                 if ( this.mode != 'wysiwyg' )\r
297                         return;\r
298 \r
299                 this.getCommand( 'cut' ).setState( inReadOnly ? CKEDITOR.TRISTATE_DISABLED : stateFromNamedCommand( 'Cut', this ) );\r
300                 this.getCommand( 'copy' ).setState( stateFromNamedCommand( 'Copy', this ) );\r
301                 var pasteState = inReadOnly ? CKEDITOR.TRISTATE_DISABLED :\r
302                                                 CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', this );\r
303                 this.fire( 'pasteState', pasteState );\r
304         }\r
305 \r
306         // Register the plugin.\r
307         CKEDITOR.plugins.add( 'clipboard',\r
308                 {\r
309                         requires : [ 'dialog', 'htmldataprocessor' ],\r
310                         init : function( editor )\r
311                         {\r
312                                 // Inserts processed data into the editor at the end of the\r
313                                 // events chain.\r
314                                 editor.on( 'paste', function( evt )\r
315                                         {\r
316                                                 var data = evt.data;\r
317                                                 if ( data[ 'html' ] )\r
318                                                         editor.insertHtml( data[ 'html' ] );\r
319                                                 else if ( data[ 'text' ] )\r
320                                                         editor.insertText( data[ 'text' ] );\r
321 \r
322                                                 setTimeout( function () { editor.fire( 'afterPaste' ); }, 0 );\r
323 \r
324                                         }, null, null, 1000 );\r
325 \r
326                                 editor.on( 'pasteDialog', function( evt )\r
327                                         {\r
328                                                 setTimeout( function()\r
329                                                 {\r
330                                                         // Open default paste dialog.\r
331                                                         editor.openDialog( 'paste' );\r
332                                                 }, 0 );\r
333                                         });\r
334 \r
335                                 editor.on( 'pasteState', function( evt )\r
336                                         {\r
337                                                 editor.getCommand( 'paste' ).setState( evt.data );\r
338                                         });\r
339 \r
340                                 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )\r
341                                 {\r
342                                         var lang = editor.lang[ commandName ];\r
343 \r
344                                         editor.addCommand( commandName, command );\r
345                                         editor.ui.addButton( buttonName,\r
346                                                 {\r
347                                                         label : lang,\r
348                                                         command : commandName\r
349                                                 });\r
350 \r
351                                         // If the "menu" plugin is loaded, register the menu item.\r
352                                         if ( editor.addMenuItems )\r
353                                         {\r
354                                                 editor.addMenuItem( commandName,\r
355                                                         {\r
356                                                                 label : lang,\r
357                                                                 command : commandName,\r
358                                                                 group : 'clipboard',\r
359                                                                 order : ctxMenuOrder\r
360                                                         });\r
361                                         }\r
362                                 }\r
363 \r
364                                 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );\r
365                                 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );\r
366                                 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );\r
367 \r
368                                 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );\r
369 \r
370                                 editor.on( 'key', onKey, editor );\r
371 \r
372                                 // We'll be catching all pasted content in one line, regardless of whether the\r
373                                 // it's introduced by a document command execution (e.g. toolbar buttons) or\r
374                                 // user paste behaviors. (e.g. Ctrl-V)\r
375                                 editor.on( 'contentDom', function()\r
376                                 {\r
377                                         var body = editor.document.getBody();\r
378 \r
379                                         // Intercept the paste before it actually takes place.\r
380                                         body.on( !CKEDITOR.env.ie ? 'paste' : 'beforepaste', function( evt )\r
381                                                 {\r
382                                                         if ( depressBeforeEvent )\r
383                                                                 return;\r
384 \r
385                                                         // Dismiss the (wrong) 'beforepaste' event fired on toolbar menu open.\r
386                                                         var domEvent = evt.data && evt.data.$;\r
387                                                         if ( CKEDITOR.env.ie && domEvent && !domEvent.ctrlKey )\r
388                                                                 return;\r
389 \r
390                                                         // Fire 'beforePaste' event so clipboard flavor get customized\r
391                                                         // by other plugins.\r
392                                                         var eventData =  { mode : 'html' };\r
393                                                         editor.fire( 'beforePaste', eventData );\r
394 \r
395                                                         getClipboardData.call( editor, evt, eventData.mode, function ( data )\r
396                                                         {\r
397                                                                 // The very last guard to make sure the\r
398                                                                 // paste has successfully happened.\r
399                                                                 if ( !( data = CKEDITOR.tools.trim( data.replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/ig,'' ) ) ) )\r
400                                                                         return;\r
401 \r
402                                                                 var dataTransfer = {};\r
403                                                                 dataTransfer[ eventData.mode ] = data;\r
404                                                                 editor.fire( 'paste', dataTransfer );\r
405                                                         } );\r
406                                                 });\r
407 \r
408                                         if ( CKEDITOR.env.ie )\r
409                                         {\r
410                                                 // Dismiss the (wrong) 'beforepaste' event fired on context menu open. (#7953)\r
411                                                 body.on( 'contextmenu', function()\r
412                                                 {\r
413                                                         depressBeforeEvent = 1;\r
414                                                         // Important: The following timeout will be called only after menu closed.\r
415                                                         setTimeout( function() { depressBeforeEvent = 0; }, 0 );\r
416                                                 } );\r
417 \r
418                                                 // Handle IE's late coming "paste" event when pasting from\r
419                                                 // browser toolbar/context menu.\r
420                                                 body.on( 'paste', function( evt )\r
421                                                 {\r
422                                                         if ( !editor.document.getById( 'cke_pastebin' ) )\r
423                                                         {\r
424                                                                 // Prevent native paste.\r
425                                                                 evt.data.preventDefault();\r
426 \r
427                                                                 depressBeforeEvent = 0;\r
428                                                                 // Resort to the paste command.\r
429                                                                 pasteCmd.exec( editor );\r
430                                                         }\r
431                                                 } );\r
432                                         }\r
433 \r
434                                         body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );\r
435 \r
436                                         body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );\r
437                                         body.on( 'keyup', setToolbarStates, editor );\r
438                                 });\r
439 \r
440                                 // For improved performance, we're checking the readOnly state on selectionChange instead of hooking a key event for that.\r
441                                 editor.on( 'selectionChange', function( evt )\r
442                                 {\r
443                                         inReadOnly = evt.data.selection.getRanges()[ 0 ].checkReadOnly();\r
444                                         setToolbarStates.call( editor );\r
445                                 });\r
446 \r
447                                 // If the "contextmenu" plugin is loaded, register the listeners.\r
448                                 if ( editor.contextMenu )\r
449                                 {\r
450                                         editor.contextMenu.addListener( function( element, selection )\r
451                                                 {\r
452                                                         var readOnly = selection.getRanges()[ 0 ].checkReadOnly();\r
453                                                         return {\r
454                                                                 cut : !readOnly && stateFromNamedCommand( 'Cut', editor ),\r
455                                                                 copy : stateFromNamedCommand( 'Copy', editor ),\r
456                                                                 paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', editor ) )\r
457                                                         };\r
458                                                 });\r
459                                 }\r
460                         }\r
461                 });\r
462 })();\r
463 \r
464 /**\r
465  * Fired when a clipboard operation is about to be taken into the editor.\r
466  * Listeners can manipulate the data to be pasted before having it effectively\r
467  * inserted into the document.\r
468  * @name CKEDITOR.editor#paste\r
469  * @since 3.1\r
470  * @event\r
471  * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.\r
472  * @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
473  */\r
474 \r
475 /**\r
476  * Internal event to open the Paste dialog\r
477  * @name CKEDITOR.editor#pasteDialog\r
478  * @event\r
479  */\r