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