1 | /*
|
---|
2 | Copyright (c) 2003-2010, 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 | var clipboardText = CKEDITOR.tools.tryThese(
|
---|
18 | function()
|
---|
19 | {
|
---|
20 | var clipboardText = window.clipboardData.getData( 'Text' );
|
---|
21 | if ( !clipboardText )
|
---|
22 | throw 0;
|
---|
23 | return clipboardText;
|
---|
24 | },
|
---|
25 | function()
|
---|
26 | {
|
---|
27 | window.netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );
|
---|
28 |
|
---|
29 | var clip = window.Components.classes[ "@mozilla.org/widget/clipboard;1" ]
|
---|
30 | .getService( window.Components.interfaces.nsIClipboard );
|
---|
31 | var trans = window.Components.classes[ "@mozilla.org/widget/transferable;1" ]
|
---|
32 | .createInstance( window.Components.interfaces.nsITransferable );
|
---|
33 | trans.addDataFlavor( "text/unicode" );
|
---|
34 | clip.getData( trans, clip.kGlobalClipboard );
|
---|
35 |
|
---|
36 | var str = {}, strLength = {}, clipboardText;
|
---|
37 | trans.getTransferData( "text/unicode", str, strLength );
|
---|
38 | str = str.value.QueryInterface( window.Components.interfaces.nsISupportsString );
|
---|
39 | clipboardText = str.data.substring( 0, strLength.value / 2 );
|
---|
40 | return clipboardText;
|
---|
41 | }
|
---|
42 | // Any other approach that's working...
|
---|
43 | );
|
---|
44 |
|
---|
45 | if ( !clipboardText ) // Clipboard access privilege is not granted.
|
---|
46 | {
|
---|
47 | editor.openDialog( 'pastetext' );
|
---|
48 | return false;
|
---|
49 | }
|
---|
50 | else
|
---|
51 | editor.fire( 'paste', { 'text' : clipboardText } );
|
---|
52 |
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | };
|
---|
56 |
|
---|
57 | function doInsertText( doc, text )
|
---|
58 | {
|
---|
59 | // Native text insertion.
|
---|
60 | if ( CKEDITOR.env.ie )
|
---|
61 | {
|
---|
62 | var selection = doc.selection;
|
---|
63 | if ( selection.type == 'Control' )
|
---|
64 | selection.clear();
|
---|
65 | selection.createRange().pasteHTML( text );
|
---|
66 | }
|
---|
67 | else
|
---|
68 | doc.execCommand( 'inserthtml', false, text );
|
---|
69 | }
|
---|
70 |
|
---|
71 | // Register the plugin.
|
---|
72 | CKEDITOR.plugins.add( 'pastetext',
|
---|
73 | {
|
---|
74 | init : function( editor )
|
---|
75 | {
|
---|
76 | var commandName = 'pastetext',
|
---|
77 | command = editor.addCommand( commandName, pasteTextCmd );
|
---|
78 |
|
---|
79 | editor.ui.addButton( 'PasteText',
|
---|
80 | {
|
---|
81 | label : editor.lang.pasteText.button,
|
---|
82 | command : commandName
|
---|
83 | });
|
---|
84 |
|
---|
85 | CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );
|
---|
86 |
|
---|
87 | if ( editor.config.forcePasteAsPlainText )
|
---|
88 | {
|
---|
89 | // Intercept the default pasting process.
|
---|
90 | editor.on( 'beforeCommandExec', function ( evt )
|
---|
91 | {
|
---|
92 | if ( evt.data.name == 'paste' )
|
---|
93 | {
|
---|
94 | editor.execCommand( 'pastetext' );
|
---|
95 | evt.cancel();
|
---|
96 | }
|
---|
97 | }, null, null, 0 );
|
---|
98 | }
|
---|
99 | },
|
---|
100 |
|
---|
101 | requires : [ 'clipboard' ]
|
---|
102 | });
|
---|
103 |
|
---|
104 | function doEnter( editor, mode, times, forceMode )
|
---|
105 | {
|
---|
106 | while ( times-- )
|
---|
107 | {
|
---|
108 | CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]
|
---|
109 | ( editor, mode, null, forceMode );
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | CKEDITOR.editor.prototype.insertText = function( text )
|
---|
114 | {
|
---|
115 | this.focus();
|
---|
116 | this.fire( 'saveSnapshot' );
|
---|
117 |
|
---|
118 | var mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode,
|
---|
119 | isEnterBrMode = mode == CKEDITOR.ENTER_BR,
|
---|
120 | doc = this.document.$,
|
---|
121 | self = this,
|
---|
122 | line;
|
---|
123 |
|
---|
124 | text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
|
---|
125 |
|
---|
126 | var startIndex = 0;
|
---|
127 | text.replace( /\n+/g, function( match, lastIndex )
|
---|
128 | {
|
---|
129 | line = text.substring( startIndex, lastIndex );
|
---|
130 | startIndex = lastIndex + match.length;
|
---|
131 | line.length && doInsertText( doc, line );
|
---|
132 |
|
---|
133 | var lineBreakNums = match.length,
|
---|
134 | // Duo consequence line-break as a enter block.
|
---|
135 | enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ),
|
---|
136 | // Per link-break as a enter br.
|
---|
137 | enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2;
|
---|
138 |
|
---|
139 | // Line-breaks are converted to editor enter key strokes.
|
---|
140 | doEnter( self, mode, enterBlockTimes );
|
---|
141 | doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true );
|
---|
142 | });
|
---|
143 |
|
---|
144 | // Insert the last text line of text.
|
---|
145 | line = text.substring( startIndex, text.length );
|
---|
146 | line.length && doInsertText( doc, line );
|
---|
147 |
|
---|
148 | this.fire( 'saveSnapshot' );
|
---|
149 | };
|
---|
150 | })();
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Whether to force all pasting operations to insert on plain text into the
|
---|
155 | * editor, loosing any formatting information possibly available in the source
|
---|
156 | * text.
|
---|
157 | * @name CKEDITOR.config.forcePasteAsPlainText
|
---|
158 | * @type Boolean
|
---|
159 | * @default false
|
---|
160 | * @example
|
---|
161 | * config.forcePasteAsPlainText = true;
|
---|
162 | */
|
---|