JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / keystrokes / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 // Register a plugin named "sample".\r
7 CKEDITOR.plugins.add( 'keystrokes',\r
8 {\r
9         beforeInit : function( editor )\r
10         {\r
11                 /**\r
12                  * Controls keystrokes typing in this editor instance.\r
13                  * @name CKEDITOR.editor.prototype.keystrokeHandler\r
14                  * @type CKEDITOR.keystrokeHandler\r
15                  * @example\r
16                  */\r
17                 editor.keystrokeHandler = new CKEDITOR.keystrokeHandler( editor );\r
18 \r
19                 editor.specialKeys = {};\r
20         },\r
21 \r
22         init : function( editor )\r
23         {\r
24                 var keystrokesConfig    = editor.config.keystrokes,\r
25                         blockedConfig           = editor.config.blockedKeystrokes;\r
26 \r
27                 var keystrokes                  = editor.keystrokeHandler.keystrokes,\r
28                         blockedKeystrokes       = editor.keystrokeHandler.blockedKeystrokes;\r
29 \r
30                 for ( var i = 0 ; i < keystrokesConfig.length ; i++ )\r
31                         keystrokes[ keystrokesConfig[i][0] ] = keystrokesConfig[i][1];\r
32 \r
33                 for ( i = 0 ; i < blockedConfig.length ; i++ )\r
34                         blockedKeystrokes[ blockedConfig[i] ] = 1;\r
35         }\r
36 });\r
37 \r
38 /**\r
39  * Controls keystrokes typing in an editor instance.\r
40  * @constructor\r
41  * @param {CKEDITOR.editor} editor The editor instance.\r
42  * @example\r
43  */\r
44 CKEDITOR.keystrokeHandler = function( editor )\r
45 {\r
46         if ( editor.keystrokeHandler )\r
47                 return editor.keystrokeHandler;\r
48 \r
49         /**\r
50          * List of keystrokes associated to commands. Each entry points to the\r
51          * command to be executed.\r
52          * @type Object\r
53          * @example\r
54          */\r
55         this.keystrokes = {};\r
56 \r
57         /**\r
58          * List of keystrokes that should be blocked if not defined at\r
59          * {@link keystrokes}. In this way it is possible to block the default\r
60          * browser behavior for those keystrokes.\r
61          * @type Object\r
62          * @example\r
63          */\r
64         this.blockedKeystrokes = {};\r
65 \r
66         this._ =\r
67         {\r
68                 editor : editor\r
69         };\r
70 \r
71         return this;\r
72 };\r
73 \r
74 (function()\r
75 {\r
76         var cancel;\r
77 \r
78         var onKeyDown = function( event )\r
79         {\r
80                 // The DOM event object is passed by the "data" property.\r
81                 event = event.data;\r
82 \r
83                 var keyCombination = event.getKeystroke();\r
84                 var command = this.keystrokes[ keyCombination ];\r
85                 var editor = this._.editor;\r
86 \r
87                 cancel = ( editor.fire( 'key', { keyCode : keyCombination } ) === true );\r
88 \r
89                 if ( !cancel )\r
90                 {\r
91                         if ( command )\r
92                         {\r
93                                 var data = { from : 'keystrokeHandler' };\r
94                                 cancel = ( editor.execCommand( command, data ) !== false );\r
95                         }\r
96 \r
97                         if  ( !cancel )\r
98                         {\r
99                                 var handler = editor.specialKeys[ keyCombination ];\r
100                                 cancel = ( handler && handler( editor ) === true );\r
101 \r
102                                 if ( !cancel )\r
103                                         cancel = !!this.blockedKeystrokes[ keyCombination ];\r
104                         }\r
105                 }\r
106 \r
107                 if ( cancel )\r
108                         event.preventDefault( true );\r
109 \r
110                 return !cancel;\r
111         };\r
112 \r
113         var onKeyPress = function( event )\r
114         {\r
115                 if ( cancel )\r
116                 {\r
117                         cancel = false;\r
118                         event.data.preventDefault( true );\r
119                 }\r
120         };\r
121 \r
122         CKEDITOR.keystrokeHandler.prototype =\r
123         {\r
124                 /**\r
125                  * Attaches this keystroke handle to a DOM object. Keystrokes typed\r
126                  ** over this object will get handled by this keystrokeHandler.\r
127                  * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach\r
128                  *              to.\r
129                  * @example\r
130                  */\r
131                 attach : function( domObject )\r
132                 {\r
133                         // For most browsers, it is enough to listen to the keydown event\r
134                         // only.\r
135                         domObject.on( 'keydown', onKeyDown, this );\r
136 \r
137                         // Some browsers instead, don't cancel key events in the keydown, but in the\r
138                         // keypress. So we must do a longer trip in those cases.\r
139                         if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )\r
140                                 domObject.on( 'keypress', onKeyPress, this );\r
141                 }\r
142         };\r
143 })();\r
144 \r
145 /**\r
146  * A list of keystrokes to be blocked if not defined in the {@link CKEDITOR.config.keystrokes}\r
147  * setting. In this way it is possible to block the default browser behavior\r
148  * for those keystrokes.\r
149  * @type Array\r
150  * @default (see example)\r
151  * @example\r
152  * // This is actually the default value.\r
153  * config.blockedKeystrokes =\r
154  * [\r
155  *     CKEDITOR.CTRL + 66 &#47;*B*&#47;,\r
156  *     CKEDITOR.CTRL + 73 &#47;*I*&#47;,\r
157  *     CKEDITOR.CTRL + 85 &#47;*U*&#47;\r
158  * ];\r
159  */\r
160 CKEDITOR.config.blockedKeystrokes =\r
161 [\r
162         CKEDITOR.CTRL + 66 /*B*/,\r
163         CKEDITOR.CTRL + 73 /*I*/,\r
164         CKEDITOR.CTRL + 85 /*U*/\r
165 ];\r
166 \r
167 /**\r
168  * A list associating keystrokes to editor commands. Each element in the list\r
169  * is an array where the first item is the keystroke, and the second is the\r
170  * name of the command to be executed.\r
171  * @type Array\r
172  * @default (see example)\r
173  * @example\r
174  * // This is actually the default value.\r
175  * config.keystrokes =\r
176  * [\r
177  *     [ CKEDITOR.ALT + 121 &#47;*F10*&#47;, 'toolbarFocus' ],\r
178  *     [ CKEDITOR.ALT + 122 &#47;*F11*&#47;, 'elementsPathFocus' ],\r
179  *\r
180  *     [ CKEDITOR.SHIFT + 121 &#47;*F10*&#47;, 'contextMenu' ],\r
181  *\r
182  *     [ CKEDITOR.CTRL + 90 &#47;*Z*&#47;, 'undo' ],\r
183  *     [ CKEDITOR.CTRL + 89 &#47;*Y*&#47;, 'redo' ],\r
184  *     [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 &#47;*Z*&#47;, 'redo' ],\r
185  *\r
186  *     [ CKEDITOR.CTRL + 76 &#47;*L*&#47;, 'link' ],\r
187  *\r
188  *     [ CKEDITOR.CTRL + 66 &#47;*B*&#47;, 'bold' ],\r
189  *     [ CKEDITOR.CTRL + 73 &#47;*I*&#47;, 'italic' ],\r
190  *     [ CKEDITOR.CTRL + 85 &#47;*U*&#47;, 'underline' ],\r
191  *\r
192  *     [ CKEDITOR.ALT + 109 &#47;*-*&#47;, 'toolbarCollapse' ]\r
193  * ];\r
194  */\r
195 CKEDITOR.config.keystrokes =\r
196 [\r
197         [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],\r
198         [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],\r
199 \r
200         [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],\r
201         [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],\r
202 \r
203         [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],\r
204         [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],\r
205         [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],\r
206 \r
207         [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],\r
208 \r
209         [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],\r
210         [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],\r
211         [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],\r
212 \r
213         [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ],\r
214         [ CKEDITOR.ALT + 48 /*0*/, 'a11yHelp' ]\r
215 ];\r
216 \r
217 /**\r
218  * Fired when any keyboard key (or combination) is pressed into the editing area.\r
219  * @name CKEDITOR#key\r
220  * @event\r
221  * @param {Number} data.keyCode A number representing the key code (or\r
222  *              combination). It is the sum of the current key code and the\r
223  *              {@link CKEDITOR.CTRL}, {@link CKEDITOR.SHIFT} and {@link CKEDITOR.ALT}\r
224  *              constants, if those are pressed.\r
225  */\r