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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* @file Clipboard support
*/
(function()
{
// Tries to execute any of the paste, cut or copy commands in IE. Returns a
// boolean indicating that the operation succeeded.
var execIECommand = function( editor, command )
{
var doc = editor.document,
body = doc.getBody();
var enabled = false;
var onExec = function()
{
enabled = true;
};
// The following seems to be the only reliable way to detect that
// clipboard commands are enabled in IE. It will fire the
// onpaste/oncut/oncopy events only if the security settings allowed
// the command to execute.
body.on( command, onExec );
doc.$.execCommand( command );
body.removeListener( command, onExec );
return enabled;
};
// Attempts to execute the Cut and Copy operations.
var tryToCutCopy =
CKEDITOR.env.ie ?
function( editor, type )
{
return execIECommand( editor, type );
}
: // !IE.
function( editor, type )
{
try
{
// Other browsers throw an error if the command is disabled.
return editor.document.$.execCommand( type );
}
catch( e )
{
return false;
}
};
// A class that represents one of the cut or copy commands.
var cutCopyCmd = function( type )
{
this.type = type;
this.canUndo = ( this.type == 'cut' ); // We can't undo copy to clipboard.
};
cutCopyCmd.prototype =
{
exec : function( editor, data )
{
var success = tryToCutCopy( editor, this.type );
if ( !success )
alert( editor.lang.clipboard[ this.type + 'Error' ] ); // Show cutError or copyError.
return success;
}
};
// Paste command.
var pasteCmd =
CKEDITOR.env.ie ?
{
exec : function( editor, data )
{
// Prevent IE from pasting at the begining of the document.
editor.focus();
if ( !editor.fire( 'beforePaste' )
&& !execIECommand( editor, 'paste' ) )
{
editor.openDialog( 'paste' );
}
}
}
:
{
exec : function( editor )
{
try
{
if ( !editor.fire( 'beforePaste' )
&& !editor.document.$.execCommand( 'Paste', false, null ) )
{
throw 0;
}
}
catch ( e )
{
// Open the paste dialog.
editor.openDialog( 'paste' );
}
}
};
// Listens for some clipboard related keystrokes, so they get customized.
var onKey = function( event )
{
switch ( event.data.keyCode )
{
// Paste
case CKEDITOR.CTRL + 86 : // CTRL+V
case CKEDITOR.SHIFT + 45 : // SHIFT+INS
var editor = this;
editor.fire( 'saveSnapshot' ); // Save before paste
if ( editor.fire( 'beforePaste' ) )
event.cancel();
setTimeout( function()
{
editor.fire( 'saveSnapshot' ); // Save after paste
}, 0 );
return;
// Cut
case CKEDITOR.CTRL + 88 : // CTRL+X
case CKEDITOR.SHIFT + 46 : // SHIFT+DEL
// Save Undo snapshot.
editor = this;
editor.fire( 'saveSnapshot' ); // Save before paste
setTimeout( function()
{
editor.fire( 'saveSnapshot' ); // Save after paste
}, 0 );
}
};
// Register the plugin.
CKEDITOR.plugins.add( 'clipboard',
{
init : function( editor )
{
function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
{
var lang = editor.lang[ commandName ];
editor.addCommand( commandName, command );
editor.ui.addButton( buttonName,
{
label : lang,
command : commandName
});
// If the "menu" plugin is loaded, register the menu item.
if ( editor.addMenuItems )
{
editor.addMenuItem( commandName,
{
label : lang,
command : commandName,
group : 'clipboard',
order : ctxMenuOrder
});
}
}
addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
editor.on( 'key', onKey, editor );
// If the "contextmenu" plugin is loaded, register the listeners.
if ( editor.contextMenu )
{
function stateFromNamedCommand( command )
{
return editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
}
editor.contextMenu.addListener( function()
{
return {
cut : stateFromNamedCommand( 'Cut' ),
// Browser bug: 'Cut' has the correct states for both Copy and Cut.
copy : stateFromNamedCommand( 'Cut' ),
paste : CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste' )
};
});
}
}
});
})();
|