JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / keystrokes / plugin.js
diff --git a/_source/plugins/keystrokes/plugin.js b/_source/plugins/keystrokes/plugin.js
new file mode 100644 (file)
index 0000000..b800d2b
--- /dev/null
@@ -0,0 +1,217 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+// Register a plugin named "sample".\r
+CKEDITOR.plugins.add( 'keystrokes',\r
+{\r
+       beforeInit : function( editor )\r
+       {\r
+               /**\r
+                * Controls keystrokes typing in this editor instance.\r
+                * @name CKEDITOR.editor.prototype.keystrokeHandler\r
+                * @type CKEDITOR.keystrokeHandler\r
+                * @example\r
+                */\r
+               editor.keystrokeHandler = new CKEDITOR.keystrokeHandler( editor );\r
+\r
+               editor.specialKeys = {};\r
+       },\r
+\r
+       init : function( editor )\r
+       {\r
+               var keystrokesConfig    = editor.config.keystrokes,\r
+                       blockedConfig           = editor.config.blockedKeystrokes;\r
+\r
+               var keystrokes                  = editor.keystrokeHandler.keystrokes,\r
+                       blockedKeystrokes       = editor.keystrokeHandler.blockedKeystrokes;\r
+\r
+               for ( var i = 0 ; i < keystrokesConfig.length ; i++ )\r
+               {\r
+                       keystrokes[ keystrokesConfig[i][0] ] = keystrokesConfig[i][1];\r
+               }\r
+\r
+               for ( i = 0 ; i < blockedConfig.length ; i++ )\r
+               {\r
+                       blockedKeystrokes[ blockedConfig[i] ] = 1;\r
+               }\r
+       }\r
+});\r
+\r
+/**\r
+ * Controls keystrokes typing in an editor instance.\r
+ * @constructor\r
+ * @param {CKEDITOR.editor} editor The editor instance.\r
+ * @example\r
+ */\r
+CKEDITOR.keystrokeHandler = function( editor )\r
+{\r
+       if ( editor.keystrokeHandler )\r
+               return editor.keystrokeHandler;\r
+\r
+       /**\r
+        * List of keystrokes associated to commands. Each entry points to the\r
+        * command to be executed.\r
+        * @type Object\r
+        * @example\r
+        */\r
+       this.keystrokes = {};\r
+\r
+       /**\r
+        * List of keystrokes that should be blocked if not defined at\r
+        * {@link keystrokes}. In this way it is possible to block the default\r
+        * browser behavior for those keystrokes.\r
+        * @type Object\r
+        * @example\r
+        */\r
+       this.blockedKeystrokes = {};\r
+\r
+       this._ =\r
+       {\r
+               editor : editor\r
+       };\r
+\r
+       return this;\r
+};\r
+\r
+(function()\r
+{\r
+       var cancel;\r
+\r
+       var onKeyDown = function( event )\r
+       {\r
+               // The DOM event object is passed by the "data" property.\r
+               event = event.data;\r
+\r
+               var keyCombination = event.getKeystroke();\r
+               var command = this.keystrokes[ keyCombination ];\r
+               var editor = this._.editor;\r
+\r
+               cancel = ( editor.fire( 'key', { keyCode : keyCombination } ) === true );\r
+\r
+               if ( !cancel )\r
+               {\r
+                       if ( command )\r
+                       {\r
+                               var data = { from : 'keystrokeHandler' };\r
+                               cancel = ( editor.execCommand( command, data ) !== false );\r
+                       }\r
+\r
+                       if  ( !cancel )\r
+                       {\r
+                               var handler = editor.specialKeys[ keyCombination ];\r
+                               cancel = ( handler && handler( editor ) === true );\r
+\r
+                               if ( !cancel )\r
+                                       cancel = !!this.blockedKeystrokes[ keyCombination ];\r
+                       }\r
+               }\r
+\r
+               if ( cancel )\r
+                       event.preventDefault( true );\r
+\r
+               return !cancel;\r
+       };\r
+\r
+       var onKeyPress = function( event )\r
+       {\r
+               if ( cancel )\r
+               {\r
+                       cancel = false;\r
+                       event.data.preventDefault( true );\r
+               }\r
+       };\r
+\r
+       CKEDITOR.keystrokeHandler.prototype =\r
+       {\r
+               /**\r
+                * Attaches this keystroke handle to a DOM object. Keystrokes typed\r
+                ** over this object will get handled by this keystrokeHandler.\r
+                * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach\r
+                *              to.\r
+                * @example\r
+                */\r
+               attach : function( domObject )\r
+               {\r
+                       // For most browsers, it is enough to listen to the keydown event\r
+                       // only.\r
+                       domObject.on( 'keydown', onKeyDown, this );\r
+\r
+                       // Some browsers instead, don't cancel key events in the keydown, but in the\r
+                       // keypress. So we must do a longer trip in those cases.\r
+                       if ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.mac ) )\r
+                               domObject.on( 'keypress', onKeyPress, this );\r
+               }\r
+       };\r
+})();\r
+\r
+/**\r
+ * A list of keystrokes to be blocked if not defined in the {@link CKEDITOR.config.keystrokes}\r
+ * setting. In this way it is possible to block the default browser behavior\r
+ * for those keystrokes.\r
+ * @type Array\r
+ * @default (see example)\r
+ * @example\r
+ * // This is actually the default value.\r
+ * config.blockedKeystrokes =\r
+ * [\r
+ *     CKEDITOR.CTRL + 66 &#47;*B*&#47;,\r
+ *     CKEDITOR.CTRL + 73 &#47;*I*&#47;,\r
+ *     CKEDITOR.CTRL + 85 &#47;*U*&#47;\r
+ * ];\r
+ */\r
+CKEDITOR.config.blockedKeystrokes =\r
+[\r
+       CKEDITOR.CTRL + 66 /*B*/,\r
+       CKEDITOR.CTRL + 73 /*I*/,\r
+       CKEDITOR.CTRL + 85 /*U*/\r
+];\r
+\r
+/**\r
+ * A list associating keystrokes to editor commands. Each element in the list\r
+ * is an array where the first item is the keystroke, and the second is the\r
+ * name of the command to be executed.\r
+ * @type Array\r
+ * @default (see example)\r
+ * @example\r
+ * // This is actually the default value.\r
+ * config.keystrokes =\r
+ * [\r
+ *     [ CKEDITOR.ALT + 121 &#47;*F10*&#47;, 'toolbarFocus' ],\r
+ *     [ CKEDITOR.ALT + 122 &#47;*F11*&#47;, 'elementsPathFocus' ],\r
+ *\r
+ *     [ CKEDITOR.SHIFT + 121 &#47;*F10*&#47;, 'contextMenu' ],\r
+ *\r
+ *     [ CKEDITOR.CTRL + 90 &#47;*Z*&#47;, 'undo' ],\r
+ *     [ CKEDITOR.CTRL + 89 &#47;*Y*&#47;, 'redo' ],\r
+ *     [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 &#47;*Z*&#47;, 'redo' ],\r
+ *\r
+ *     [ CKEDITOR.CTRL + 76 &#47;*L*&#47;, 'link' ],\r
+ *\r
+ *     [ CKEDITOR.CTRL + 66 &#47;*B*&#47;, 'bold' ],\r
+ *     [ CKEDITOR.CTRL + 73 &#47;*I*&#47;, 'italic' ],\r
+ *     [ CKEDITOR.CTRL + 85 &#47;*U*&#47;, 'underline' ],\r
+ *\r
+ *     [ CKEDITOR.ALT + 109 &#47;*-*&#47;, 'toolbarCollapse' ]\r
+ * ];\r
+ */\r
+CKEDITOR.config.keystrokes =\r
+[\r
+       [ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],\r
+       [ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],\r
+\r
+       [ CKEDITOR.SHIFT + 121 /*F10*/, 'contextMenu' ],\r
+\r
+       [ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],\r
+       [ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],\r
+       [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],\r
+\r
+       [ CKEDITOR.CTRL + 76 /*L*/, 'link' ],\r
+\r
+       [ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],\r
+       [ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],\r
+       [ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],\r
+\r
+       [ CKEDITOR.ALT + 109 /*-*/, 'toolbarCollapse' ]\r
+];\r