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