JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / pastetext / plugin.js
diff --git a/_source/plugins/pastetext/plugin.js b/_source/plugins/pastetext/plugin.js
new file mode 100644 (file)
index 0000000..3a1678d
--- /dev/null
@@ -0,0 +1,142 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @file Paste as plain text plugin\r
+ */\r
+\r
+(function()\r
+{\r
+       // The pastetext command definition.\r
+       var pasteTextCmd =\r
+       {\r
+               exec : function( editor )\r
+               {\r
+                       // We use getClipboardData just to test if the clipboard access has\r
+                       // been granted by the user.\r
+                       if ( CKEDITOR.getClipboardData() === false || !window.clipboardData )\r
+                       {\r
+                               editor.openDialog( 'pastetext' );\r
+                               return;\r
+                       }\r
+\r
+                       editor.insertText( window.clipboardData.getData( 'Text' ) );\r
+               }\r
+       };\r
+\r
+       // Register the plugin.\r
+       CKEDITOR.plugins.add( 'pastetext',\r
+       {\r
+               init : function( editor )\r
+               {\r
+                       var commandName = 'pastetext',\r
+                               command = editor.addCommand( commandName, pasteTextCmd );\r
+\r
+                       editor.ui.addButton( 'PasteText',\r
+                               {\r
+                                       label : editor.lang.pasteText.button,\r
+                                       command : commandName\r
+                               });\r
+\r
+                       CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );\r
+\r
+                       if ( editor.config.forcePasteAsPlainText )\r
+                       {\r
+                               editor.on( 'beforePaste', function( event )\r
+                                       {\r
+                                               if ( editor.mode == "wysiwyg" )\r
+                                               {\r
+                                                       setTimeout( function() { command.exec(); }, 0 );\r
+                                                       event.cancel();\r
+                                               }\r
+                                       },\r
+                                       null, null, 20 );\r
+                       }\r
+               },\r
+               requires : [ 'clipboard' ]\r
+       });\r
+\r
+       var clipboardDiv;\r
+\r
+       CKEDITOR.getClipboardData = function()\r
+       {\r
+               if ( !CKEDITOR.env.ie )\r
+                       return false;\r
+\r
+               var doc = CKEDITOR.document,\r
+                       body = doc.getBody();\r
+\r
+               if ( !clipboardDiv )\r
+               {\r
+                       clipboardDiv = doc.createElement( 'div',\r
+                               {\r
+                                       attributes :\r
+                                               {\r
+                                                       id: 'cke_hiddenDiv'\r
+                                               },\r
+                                       styles :\r
+                                               {\r
+                                                       position : 'absolute',\r
+                                                       visibility : 'hidden',\r
+                                                       overflow : 'hidden',\r
+                                                       width : '1px',\r
+                                                       height : '1px'\r
+                                               }\r
+                               });\r
+\r
+                       clipboardDiv.setHtml( '' );\r
+\r
+                       clipboardDiv.appendTo( body );\r
+               }\r
+\r
+               // The "enabled" flag is used to check whether the paste operation has\r
+               // been completed (the onpaste event has been fired).\r
+               var     enabled = false;\r
+               var setEnabled = function()\r
+               {\r
+                       enabled = true;\r
+               };\r
+\r
+               body.on( 'paste', setEnabled );\r
+\r
+               // Create a text range and move it inside the div.\r
+               var textRange = body.$.createTextRange();\r
+               textRange.moveToElementText( clipboardDiv.$ );\r
+\r
+               // The execCommand in will fire the "onpaste", only if the\r
+               // security settings are enabled.\r
+               textRange.execCommand( 'Paste' );\r
+\r
+               // Get the DIV html and reset it.\r
+               var html = clipboardDiv.getHtml();\r
+               clipboardDiv.setHtml( '' );\r
+\r
+               body.removeListener( 'paste', setEnabled );\r
+\r
+               // Return the HTML or false if not enabled.\r
+               return enabled && html;\r
+       };\r
+})();\r
+\r
+CKEDITOR.editor.prototype.insertText = function( text )\r
+{\r
+       text = CKEDITOR.tools.htmlEncode( text );\r
+\r
+       // TODO: Replace the following with fill line break processing (see V2).\r
+       text = text.replace( /(?:\r\n)|\n|\r/g, '<br>' );\r
+\r
+       this.insertHtml( text );\r
+};\r
+\r
+/**\r
+ * Whether to force all pasting operations to insert on plain text into the\r
+ * editor, loosing any formatting information possibly available in the source\r
+ * text.\r
+ * @type Boolean\r
+ * @default false\r
+ * @example\r
+ * config.forcePasteAsPlainText = true;\r
+ */\r
+CKEDITOR.config.forcePasteAsPlainText = false;\r