src/pyams_skin/resources/js/ext/tinymce/dev/classes/Shortcuts.js
changeset 557 bca7a7e058a3
equal deleted inserted replaced
-1:000000000000 557:bca7a7e058a3
       
     1 /**
       
     2  * Shortcuts.js
       
     3  *
       
     4  * Copyright, Moxiecode Systems AB
       
     5  * Released under LGPL License.
       
     6  *
       
     7  * License: http://www.tinymce.com/license
       
     8  * Contributing: http://www.tinymce.com/contributing
       
     9  */
       
    10 
       
    11 /**
       
    12  * Contains all logic for handling of keyboard shortcuts.
       
    13  *
       
    14  * @example
       
    15  * editor.shortcuts.add('ctrl+a', function() {});
       
    16  * editor.shortcuts.add('meta+a', function() {}); // "meta" maps to Command on Mac and Ctrl on PC
       
    17  * editor.shortcuts.add('ctrl+alt+a', function() {});
       
    18  * editor.shortcuts.add('access+a', function() {}); // "access" maps to ctrl+alt on Mac and shift+alt on PC
       
    19  */
       
    20 define("tinymce/Shortcuts", [
       
    21 	"tinymce/util/Tools",
       
    22 	"tinymce/Env"
       
    23 ], function(Tools, Env) {
       
    24 	var each = Tools.each, explode = Tools.explode;
       
    25 
       
    26 	var keyCodeLookup = {
       
    27 		"f9": 120,
       
    28 		"f10": 121,
       
    29 		"f11": 122
       
    30 	};
       
    31 
       
    32 	var modifierNames = Tools.makeMap('alt,ctrl,shift,meta,access');
       
    33 
       
    34 	return function(editor) {
       
    35 		var self = this, shortcuts = {};
       
    36 
       
    37 		function createShortcut(pattern, desc, cmdFunc, scope) {
       
    38 			var id, key, shortcut;
       
    39 
       
    40 			shortcut = {
       
    41 				func: cmdFunc,
       
    42 				scope: scope || editor,
       
    43 				desc: editor.translate(desc)
       
    44 			};
       
    45 
       
    46 			// Parse modifiers and keys ctrl+alt+b for example
       
    47 			each(explode(pattern, '+'), function(value) {
       
    48 				if (value in modifierNames) {
       
    49 					shortcut[value] = true;
       
    50 				} else {
       
    51 					// Allow numeric keycodes like ctrl+219 for ctrl+[
       
    52 					if (/^[0-9]{2,}$/.test(value)) {
       
    53 						shortcut.keyCode = parseInt(value, 10);
       
    54 					} else {
       
    55 						shortcut.charCode = value.charCodeAt(0);
       
    56 						shortcut.keyCode = keyCodeLookup[value] || value.toUpperCase().charCodeAt(0);
       
    57 					}
       
    58 				}
       
    59 			});
       
    60 
       
    61 			// Generate unique id for modifier combination and set default state for unused modifiers
       
    62 			id = [shortcut.keyCode];
       
    63 			for (key in modifierNames) {
       
    64 				if (shortcut[key]) {
       
    65 					id.push(key);
       
    66 				} else {
       
    67 					shortcut[key] = false;
       
    68 				}
       
    69 			}
       
    70 			shortcut.id = id.join(',');
       
    71 
       
    72 			// Handle special access modifier differently depending on Mac/Win
       
    73 			if (shortcut.access) {
       
    74 				shortcut.alt = true;
       
    75 
       
    76 				if (Env.mac) {
       
    77 					shortcut.ctrl = true;
       
    78 				} else {
       
    79 					shortcut.shift = true;
       
    80 				}
       
    81 			}
       
    82 
       
    83 			// Handle special meta modifier differently depending on Mac/Win
       
    84 			if (shortcut.meta) {
       
    85 				if (Env.mac) {
       
    86 					shortcut.meta = true;
       
    87 				} else {
       
    88 					shortcut.ctrl = true;
       
    89 					shortcut.meta = false;
       
    90 				}
       
    91 			}
       
    92 
       
    93 			return shortcut;
       
    94 		}
       
    95 
       
    96 		editor.on('keyup keypress keydown', function(e) {
       
    97 			if ((e.altKey || e.ctrlKey || e.metaKey) && !e.isDefaultPrevented()) {
       
    98 				each(shortcuts, function(shortcut) {
       
    99 					if (shortcut.ctrl != e.ctrlKey || shortcut.meta != e.metaKey) {
       
   100 						return;
       
   101 					}
       
   102 
       
   103 					if (shortcut.alt != e.altKey || shortcut.shift != e.shiftKey) {
       
   104 						return;
       
   105 					}
       
   106 
       
   107 					if (e.keyCode == shortcut.keyCode || (e.charCode && e.charCode == shortcut.charCode)) {
       
   108 						e.preventDefault();
       
   109 
       
   110 						if (e.type == "keydown") {
       
   111 							shortcut.func.call(shortcut.scope);
       
   112 						}
       
   113 
       
   114 						return true;
       
   115 					}
       
   116 				});
       
   117 			}
       
   118 		});
       
   119 
       
   120 		/**
       
   121 		 * Adds a keyboard shortcut for some command or function.
       
   122 		 *
       
   123 		 * @method addShortcut
       
   124 		 * @param {String} pattern Shortcut pattern. Like for example: ctrl+alt+o.
       
   125 		 * @param {String} desc Text description for the command.
       
   126 		 * @param {String/Function} cmdFunc Command name string or function to execute when the key is pressed.
       
   127 		 * @param {Object} sc Optional scope to execute the function in.
       
   128 		 * @return {Boolean} true/false state if the shortcut was added or not.
       
   129 		 */
       
   130 		self.add = function(pattern, desc, cmdFunc, scope) {
       
   131 			var cmd;
       
   132 
       
   133 			cmd = cmdFunc;
       
   134 
       
   135 			if (typeof cmdFunc === 'string') {
       
   136 				cmdFunc = function() {
       
   137 					editor.execCommand(cmd, false, null);
       
   138 				};
       
   139 			} else if (Tools.isArray(cmd)) {
       
   140 				cmdFunc = function() {
       
   141 					editor.execCommand(cmd[0], cmd[1], cmd[2]);
       
   142 				};
       
   143 			}
       
   144 
       
   145 			each(explode(pattern.toLowerCase()), function(pattern) {
       
   146 				var shortcut = createShortcut(pattern, desc, cmdFunc, scope);
       
   147 				shortcuts[shortcut.id] = shortcut;
       
   148 			});
       
   149 
       
   150 			return true;
       
   151 		};
       
   152 
       
   153 		/**
       
   154 		 * Remove a keyboard shortcut by pattern.
       
   155 		 *
       
   156 		 * @method remove
       
   157 		 * @param {String} pattern Shortcut pattern. Like for example: ctrl+alt+o.
       
   158 		 * @return {Boolean} true/false state if the shortcut was removed or not.
       
   159 		 */
       
   160 		self.remove = function(pattern) {
       
   161 			var shortcut = createShortcut(pattern);
       
   162 
       
   163 			if (shortcuts[shortcut.id]) {
       
   164 				delete shortcuts[shortcut.id];
       
   165 				return true;
       
   166 			}
       
   167 
       
   168 			return false;
       
   169 		};
       
   170 	};
       
   171 });