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