1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 // Register a plugin named "sample".
  7 CKEDITOR.plugins.add( 'keystrokes',
  8 {
  9 	beforeInit : function( editor )
 10 	{
 11 		/**
 12 		 * Controls keystrokes typing in this editor instance.
 13 		 * @name CKEDITOR.editor.prototype.keystrokeHandler
 14 		 * @type CKEDITOR.keystrokeHandler
 15 		 * @example
 16 		 */
 17 		editor.keystrokeHandler = new CKEDITOR.keystrokeHandler( editor );
 18 	},
 19
 20 	init : function( editor )
 21 	{
 22 		var keystrokesConfig	= editor.config.keystrokes,
 23 			blockedConfig		= editor.config.blockedKeystrokes;
 24
 25 		var keystrokes			= editor.keystrokeHandler.keystrokes,
 26 			blockedKeystrokes	= editor.keystrokeHandler.blockedKeystrokes;
 27
 28 		for ( var i = 0 ; i < keystrokesConfig.length ; i++ )
 29 		{
 30 			keystrokes[ keystrokesConfig[i][0] ] = keystrokesConfig[i][1];
 31 		}
 32
 33 		for ( i = 0 ; i < blockedConfig.length ; i++ )
 34 		{
 35 			blockedKeystrokes[ blockedConfig[i] ] = 1;
 36 		}
 37 	}
 38 });
 39
 40 /**
 41  * Controls keystrokes typing in an editor instance.
 42  * @constructor
 43  * @param {CKEDITOR.editor} editor The editor instance.
 44  * @example
 45  */
 46 CKEDITOR.keystrokeHandler = function( editor )
 47 {
 48 	if ( editor.keystrokeHandler )
 49 		return editor.keystrokeHandler;
 50
 51 	/**
 52 	 * List of keystrokes associated to commands. Each entry points to the
 53 	 * command to be executed.
 54 	 * @type Object
 55 	 * @example
 56 	 */
 57 	this.keystrokes = {};
 58
 59 	/**
 60 	 * List of keystrokes that should be blocked if not defined at
 61 	 * {@link keystrokes}. In this way it is possible to block the default
 62 	 * browser behavior for those keystrokes.
 63 	 * @type Object
 64 	 * @example
 65 	 */
 66 	this.blockedKeystrokes = {};
 67
 68 	this._ =
 69 	{
 70 		editor : editor
 71 	};
 72
 73 	return this;
 74 };
 75
 76 (function()
 77 {
 78 	var onKeyDown = function( event )
 79 	{
 80 		// The DOM event object is passed by the "data" property.
 81 		event = event.data;
 82
 83 		var keyCombination = event.getKeystroke();
 84 		var command = this.keystrokes[ keyCombination ];
 85
 86 		var cancel = !this._.editor.fire( 'key', { keyCode : keyCombination } );
 87
 88 		if ( !cancel )
 89 		{
 90 			if ( command )
 91 				cancel = ( this._.editor.execCommand( command ) !== false );
 92
 93 			if ( !cancel )
 94 				cancel = !!this.blockedKeystrokes[ keyCombination ];
 95 		}
 96
 97 		if ( cancel )
 98 			event.preventDefault( true );
 99
100 		return !cancel;
101 	};
102
103 	CKEDITOR.keystrokeHandler.prototype =
104 	{
105 		/**
106 		 * Attaches this keystroke handle to a DOM object. Keystrokes typed
107 		 ** over this object will get handled by this keystrokeHandler.
108 		 * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach
109 		 *		to.
110 		 * @example
111 		 */
112 		attach : function( domObject )
113 		{
114 			domObject.on( 'keydown', onKeyDown, this );
115 		}
116 	};
117 })();
118
119 /**
120  * A list of keystrokes to be blocked if not defined in the {@link #keystrokes}
121  * setting. In this way it is possible to block the default browser behavior
122  * for those keystrokes.
123  * @type Array
124  * @example
125  */
126 CKEDITOR.config.blockedKeystrokes =
127 [
128 	CKEDITOR.CTRL + 66 /*B*/,
129 	CKEDITOR.CTRL + 73 /*I*/,
130 	CKEDITOR.CTRL + 85 /*U*/
131 ];
132
133 /**
134  * A list associating keystrokes to editor commands. Each element in the list
135  * is an array where the first item is the keystroke, and the second is the
136  * command to be executed.
137  * @type Array
138  * @example
139  */
140 CKEDITOR.config.keystrokes =
141 [
142 	[ CKEDITOR.ALT + 121 /*F10*/, 'toolbarFocus' ],
143 	[ CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' ],
144
145 	[ CKEDITOR.CTRL + 86 /*V*/, 'paste' ],
146 	[ CKEDITOR.SHIFT + 45 /*INS*/, 'paste' ],
147 	[ CKEDITOR.CTRL + 88 /*X*/, 'cut' ],
148 	[ CKEDITOR.SHIFT + 46 /*DEL*/, 'cut' ],
149 	[ CKEDITOR.CTRL + 90 /*Z*/, 'undo' ],
150 	[ CKEDITOR.CTRL + 89 /*Y*/, 'redo' ],
151 	[ CKEDITOR.CTRL + CKEDITOR.SHIFT + 90 /*Z*/, 'redo' ],
152 	[ CKEDITOR.CTRL + 76 /*L*/, 'link' ],
153 	[ CKEDITOR.CTRL + 66 /*B*/, 'bold' ],
154 	[ CKEDITOR.CTRL + 73 /*I*/, 'italic' ],
155 	[ CKEDITOR.CTRL + 85 /*U*/, 'underline' ],
156 	[ CKEDITOR.CTRL + CKEDITOR.ALT + 13 /*ENTER*/, 'fitWindow' ],
157 	[ CKEDITOR.SHIFT + 32 /*SPACE*/, 'nbsp' ]
158 ];
159