]>
git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blob - sources/core/keystrokehandler.js
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
7 * Controls keystrokes typing in an editor instance.
10 * @constructor Creates a keystrokeHandler class instance.
11 * @param {CKEDITOR.editor} editor The editor instance.
13 CKEDITOR
.keystrokeHandler = function( editor
) {
14 if ( editor
.keystrokeHandler
)
15 return editor
.keystrokeHandler
;
18 * A list of keystrokes associated with commands. Each entry points to the
19 * command to be executed.
21 * Since CKEditor 4 there is no need to modify this property directly during the runtime.
22 * Use {@link CKEDITOR.editor#setKeystroke} instead.
27 * A list of keystrokes that should be blocked if not defined in
28 * {@link #keystrokes}. In this way it is possible to block the default
29 * browser behavior for those keystrokes.
31 this.blockedKeystrokes
= {};
43 var onKeyDown = function( event
) {
44 // The DOM event object is passed by the "data" property.
47 var keyCombination
= event
.getKeystroke();
48 var command
= this.keystrokes
[ keyCombination
];
49 var editor
= this._
.editor
;
51 cancel
= ( editor
.fire( 'key', { keyCode: keyCombination
, domEvent: event
} ) === false );
55 var data
= { from: 'keystrokeHandler' };
56 cancel
= ( editor
.execCommand( command
, data
) !== false );
60 cancel
= !!this.blockedKeystrokes
[ keyCombination
];
64 event
.preventDefault( true );
69 var onKeyPress = function( event
) {
72 event
.data
.preventDefault( true );
76 CKEDITOR
.keystrokeHandler
.prototype = {
78 * Attaches this keystroke handle to a DOM object. Keystrokes typed
79 * over this object will be handled by this keystrokeHandler.
81 * @param {CKEDITOR.dom.domObject} domObject The DOM object to attach to.
83 attach: function( domObject
) {
84 // For most browsers, it is enough to listen to the keydown event
86 domObject
.on( 'keydown', onKeyDown
, this );
88 // Some browsers instead, don't cancel key events in the keydown, but in the
89 // keypress. So we must do a longer trip in those cases.
90 if ( CKEDITOR
.env
.gecko
&& CKEDITOR
.env
.mac
)
91 domObject
.on( 'keypress', onKeyPress
, this );
97 * A list associating keystrokes with editor commands. Each element in the list
98 * is an array where the first item is the keystroke, and the second is the
99 * name of the command to be executed.
101 * This setting should be used to define (as well as to overwrite or remove) keystrokes
102 * set by plugins (like `link` and `basicstyles`). If you want to set a keystroke
103 * for your plugin or during the runtime, use {@link CKEDITOR.editor#setKeystroke} instead.
105 * Since default keystrokes are set by the {@link CKEDITOR.editor#setKeystroke}
106 * method, by default `config.keystrokes` is an empty array.
108 * See {@link CKEDITOR.editor#setKeystroke} documentation for more details
109 * regarding the start up order.
111 * // Change default Ctrl+L keystroke for 'link' command to Ctrl+Shift+L.
112 * config.keystrokes = [
114 * [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 76, 'link' ], // Ctrl+Shift+L
118 * To reset a particular keystroke, the following approach can be used:
120 * // Disable default Ctrl+L keystroke which executes the 'link' command by default.
121 * config.keystrokes = [
123 * [ CKEDITOR.CTRL + 76, null ], // Ctrl+L
127 * In order to reset all default keystrokes, a {@link CKEDITOR#instanceReady} callback should be
128 * used. This is since editor defaults are merged rather than overwritten by
131 * **Note**: This can be potentially harmful for the editor. Avoid this unless you are
132 * aware of the consequences.
134 * // Reset all default keystrokes.
135 * config.on.instanceReady = function() {
136 * this.keystrokeHandler.keystrokes = [];
139 * @cfg {Array} [keystrokes=[]]
140 * @member CKEDITOR.config
144 * Fired when any keyboard key (or a combination thereof) is pressed in the editing area.
146 * editor.on( 'key', function( evt ) {
147 * if ( evt.data.keyCode == CKEDITOR.CTRL + 90 ) {
150 * // Cancel the event, so other listeners will not be executed and
151 * // the keydown's default behavior will be prevented.
156 * Usually you will want to use the {@link CKEDITOR.editor#setKeystroke} method or
157 * the {@link CKEDITOR.config#keystrokes} option to attach a keystroke to some {@link CKEDITOR.command command}.
158 * Key event listeners are usuful when some action should be executed conditionally, based
159 * for example on precise selection location.
162 * @member CKEDITOR.editor
164 * @param {Number} data.keyCode A number representing the key code (or a combination thereof).
165 * It is the sum of the current key code and the {@link CKEDITOR#CTRL}, {@link CKEDITOR#SHIFT}
166 * and {@link CKEDITOR#ALT} constants, if those are pressed.
167 * @param {CKEDITOR.dom.event} data.domEvent A `keydown` DOM event instance. Available since CKEditor 4.4.1.
168 * @param {CKEDITOR.editor} editor This editor instance.