[6779] | 1 | /*
|
---|
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
|
---|
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * @file Paste as plain text plugin
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | (function()
|
---|
| 11 | {
|
---|
| 12 | // The pastetext command definition.
|
---|
| 13 | var pasteTextCmd =
|
---|
| 14 | {
|
---|
| 15 | exec : function( editor )
|
---|
| 16 | {
|
---|
| 17 | // We use getClipboardData just to test if the clipboard access has
|
---|
| 18 | // been granted by the user.
|
---|
| 19 | if ( CKEDITOR.getClipboardData() === false || !window.clipboardData )
|
---|
| 20 | {
|
---|
| 21 | editor.openDialog( 'pastetext' );
|
---|
| 22 | return;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | editor.insertText( window.clipboardData.getData( 'Text' ) );
|
---|
| 26 | }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | // Register the plugin.
|
---|
| 30 | CKEDITOR.plugins.add( 'pastetext',
|
---|
| 31 | {
|
---|
| 32 | init : function( editor )
|
---|
| 33 | {
|
---|
| 34 | var commandName = 'pastetext',
|
---|
| 35 | command = editor.addCommand( commandName, pasteTextCmd );
|
---|
| 36 |
|
---|
| 37 | editor.ui.addButton( 'PasteText',
|
---|
| 38 | {
|
---|
| 39 | label : editor.lang.pasteText.button,
|
---|
| 40 | command : commandName
|
---|
| 41 | });
|
---|
| 42 |
|
---|
| 43 | CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );
|
---|
| 44 |
|
---|
| 45 | if ( editor.config.forcePasteAsPlainText )
|
---|
| 46 | {
|
---|
| 47 | editor.on( 'beforePaste', function( event )
|
---|
| 48 | {
|
---|
| 49 | if ( editor.mode == "wysiwyg" )
|
---|
| 50 | {
|
---|
| 51 | setTimeout( function() { command.exec(); }, 0 );
|
---|
| 52 | event.cancel();
|
---|
| 53 | }
|
---|
| 54 | },
|
---|
| 55 | null, null, 20 );
|
---|
| 56 | }
|
---|
| 57 | },
|
---|
| 58 | requires : [ 'clipboard' ]
|
---|
| 59 | });
|
---|
| 60 |
|
---|
| 61 | var clipboardDiv;
|
---|
| 62 |
|
---|
| 63 | CKEDITOR.getClipboardData = function()
|
---|
| 64 | {
|
---|
| 65 | if ( !CKEDITOR.env.ie )
|
---|
| 66 | return false;
|
---|
| 67 |
|
---|
| 68 | var doc = CKEDITOR.document,
|
---|
| 69 | body = doc.getBody();
|
---|
| 70 |
|
---|
| 71 | if ( !clipboardDiv )
|
---|
| 72 | {
|
---|
| 73 | clipboardDiv = doc.createElement( 'div',
|
---|
| 74 | {
|
---|
| 75 | attributes :
|
---|
| 76 | {
|
---|
| 77 | id: 'cke_hiddenDiv'
|
---|
| 78 | },
|
---|
| 79 | styles :
|
---|
| 80 | {
|
---|
| 81 | position : 'absolute',
|
---|
| 82 | visibility : 'hidden',
|
---|
| 83 | overflow : 'hidden',
|
---|
| 84 | width : '1px',
|
---|
| 85 | height : '1px'
|
---|
| 86 | }
|
---|
| 87 | });
|
---|
| 88 |
|
---|
| 89 | clipboardDiv.setHtml( '' );
|
---|
| 90 |
|
---|
| 91 | clipboardDiv.appendTo( body );
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | // The "enabled" flag is used to check whether the paste operation has
|
---|
| 95 | // been completed (the onpaste event has been fired).
|
---|
| 96 | var enabled = false;
|
---|
| 97 | var setEnabled = function()
|
---|
| 98 | {
|
---|
| 99 | enabled = true;
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | body.on( 'paste', setEnabled );
|
---|
| 103 |
|
---|
| 104 | // Create a text range and move it inside the div.
|
---|
| 105 | var textRange = body.$.createTextRange();
|
---|
| 106 | textRange.moveToElementText( clipboardDiv.$ );
|
---|
| 107 |
|
---|
| 108 | // The execCommand in will fire the "onpaste", only if the
|
---|
| 109 | // security settings are enabled.
|
---|
| 110 | textRange.execCommand( 'Paste' );
|
---|
| 111 |
|
---|
| 112 | // Get the DIV html and reset it.
|
---|
| 113 | var html = clipboardDiv.getHtml();
|
---|
| 114 | clipboardDiv.setHtml( '' );
|
---|
| 115 |
|
---|
| 116 | body.removeListener( 'paste', setEnabled );
|
---|
| 117 |
|
---|
| 118 | // Return the HTML or false if not enabled.
|
---|
| 119 | return enabled && html;
|
---|
| 120 | };
|
---|
| 121 | })();
|
---|
| 122 |
|
---|
| 123 | CKEDITOR.editor.prototype.insertText = function( text )
|
---|
| 124 | {
|
---|
| 125 | text = CKEDITOR.tools.htmlEncode( text );
|
---|
| 126 |
|
---|
| 127 | // TODO: Replace the following with fill line break processing (see V2).
|
---|
| 128 | text = text.replace( /(?:\r\n)|\n|\r/g, '<br>' );
|
---|
| 129 |
|
---|
| 130 | this.insertHtml( text );
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Whether to force all pasting operations to insert on plain text into the
|
---|
| 135 | * editor, loosing any formatting information possibly available in the source
|
---|
| 136 | * text.
|
---|
| 137 | * @type Boolean
|
---|
| 138 | * @default false
|
---|
| 139 | * @example
|
---|
| 140 | * config.forcePasteAsPlainText = true;
|
---|
| 141 | */
|
---|
| 142 | CKEDITOR.config.forcePasteAsPlainText = false;
|
---|