summaryrefslogtreecommitdiff
path: root/_source/plugins/pastetext/plugin.js
blob: 0d9cd9d92e7f02134851d9f1d7ff8ba570a023a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

/**
 * @file Paste as plain text plugin
 */

(function()
{
	// The pastetext command definition.
	var pasteTextCmd =
	{
		exec : function( editor )
		{
			var clipboardText = CKEDITOR.tools.tryThese(
				function()
				{
					var clipboardText = window.clipboardData.getData( 'Text' );
					if ( !clipboardText )
						throw 0;
					return clipboardText;
				},
				function()
				{
					window.netscape.security.PrivilegeManager.enablePrivilege( "UniversalXPConnect" );

					var clip = window.Components.classes[ "@mozilla.org/widget/clipboard;1" ]
							.getService( window.Components.interfaces.nsIClipboard );
					var trans = window.Components.classes[ "@mozilla.org/widget/transferable;1" ]
							.createInstance( window.Components.interfaces.nsITransferable );
					trans.addDataFlavor( "text/unicode" );
					clip.getData( trans, clip.kGlobalClipboard );

					var str = {}, strLength = {}, clipboardText;
					trans.getTransferData( "text/unicode", str, strLength );
					str = str.value.QueryInterface( window.Components.interfaces.nsISupportsString );
					clipboardText = str.data.substring( 0, strLength.value / 2 );
					return clipboardText;
				}
				// Any other approach that's working...
				);

			if ( !clipboardText )   // Clipboard access privilege is not granted.
			{
				editor.openDialog( 'pastetext' );
				return false;
			}
			else
				editor.fire( 'paste', { 'text' : clipboardText } );

			return true;
		}
	};

	function doInsertText( doc, text )
	{
		// Native text insertion.
		if ( CKEDITOR.env.ie )
		{
			var selection = doc.selection;
			if ( selection.type == 'Control' )
				selection.clear();
			selection.createRange().pasteHTML( text );
		}
		else
			doc.execCommand( 'inserthtml', false, text );
	}

	// Register the plugin.
	CKEDITOR.plugins.add( 'pastetext',
	{
		init : function( editor )
		{
			var commandName = 'pastetext',
				command = editor.addCommand( commandName, pasteTextCmd );

			editor.ui.addButton( 'PasteText',
				{
					label : editor.lang.pasteText.button,
					command : commandName
				});

			CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/pastetext.js' ) );

			if ( editor.config.forcePasteAsPlainText )
			{
				// Intercept the default pasting process.
				editor.on( 'beforeCommandExec', function ( evt )
				{
					if ( evt.data.name == 'paste' )
					{
						editor.execCommand( 'pastetext' );
						evt.cancel();
					}
				}, null, null, 0 );
			}
		},

		requires : [ 'clipboard' ]
	});

	function doEnter( editor, mode, times, forceMode )
	{
		while ( times-- )
		{
			CKEDITOR.plugins.enterkey[ mode == CKEDITOR.ENTER_BR ? 'enterBr' : 'enterBlock' ]
					( editor, mode, null, forceMode );
		}
	}

	CKEDITOR.editor.prototype.insertText = function( text )
	{
		this.focus();
		this.fire( 'saveSnapshot' );

		var mode = this.getSelection().getStartElement().hasAscendant( 'pre', true ) ? CKEDITOR.ENTER_BR : this.config.enterMode,
			isEnterBrMode = mode == CKEDITOR.ENTER_BR,
			doc = this.document.$,
			self = this,
			line;

		text = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );

		var startIndex = 0;
		text.replace( /\n+/g, function( match, lastIndex )
		 {
			line = text.substring( startIndex, lastIndex );
			startIndex = lastIndex + match.length;
			line.length && doInsertText( doc, line );

			var lineBreakNums = match.length,
				// Duo consequence line-break as a enter block.
				enterBlockTimes = isEnterBrMode ? 0 : Math.floor( lineBreakNums / 2 ),
				// Per link-break as a enter br.
				enterBrTimes = isEnterBrMode ? lineBreakNums : lineBreakNums % 2;

			// Line-breaks are converted to editor enter key strokes.
			doEnter( self, mode, enterBlockTimes );
			doEnter( self, CKEDITOR.ENTER_BR, enterBrTimes, isEnterBrMode ? false : true );
		 });

		// Insert the last text line of text.
		line = text.substring( startIndex, text.length );
		line.length && doInsertText( doc, line );

		this.fire( 'saveSnapshot' );
	};
})();


/**
 * Whether to force all pasting operations to insert on plain text into the
 * editor, loosing any formatting information possibly available in the source
 * text.
 * @name CKEDITOR.config.forcePasteAsPlainText
 * @type Boolean
 * @default false
 * @example
 * config.forcePasteAsPlainText = true;
 */