| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  | import utils from "./utils.js"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 10:03:04 +01:00
										 |  |  | function bindGlobalShortcut(keyboardShortcut, handler, namespace = null) { | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  |     bindElShortcut($(document), keyboardShortcut, handler, namespace); | 
					
						
							| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 10:03:04 +01:00
										 |  |  | function bindElShortcut($el, keyboardShortcut, handler, namespace = null) { | 
					
						
							| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  |     if (utils.isDesktop()) { | 
					
						
							|  |  |  |         keyboardShortcut = normalizeShortcut(keyboardShortcut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 10:03:04 +01:00
										 |  |  |         let eventName = 'keydown'; | 
					
						
							| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 10:03:04 +01:00
										 |  |  |         if (namespace) { | 
					
						
							|  |  |  |             eventName += "." + namespace; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // if there's a namespace then we replace the existing event handler with the new one
 | 
					
						
							|  |  |  |             $el.off(eventName); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // method can be called to remove the shortcut (e.g. when keyboardShortcut label is deleted)
 | 
					
						
							|  |  |  |         if (keyboardShortcut) { | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  |             $el.bind(eventName, keyboardShortcut, e => { | 
					
						
							| 
									
										
										
										
											2022-12-01 10:03:04 +01:00
										 |  |  |                 handler(e); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 e.preventDefault(); | 
					
						
							|  |  |  |                 e.stopPropagation(); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Normalize to the form expected by the jquery.hotkeys.js | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function normalizeShortcut(shortcut) { | 
					
						
							| 
									
										
										
										
											2022-12-01 10:03:04 +01:00
										 |  |  |     if (!shortcut) { | 
					
						
							|  |  |  |         return shortcut; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  |     return shortcut | 
					
						
							|  |  |  |         .toLowerCase() | 
					
						
							|  |  |  |         .replace("enter", "return") | 
					
						
							|  |  |  |         .replace("delete", "del") | 
					
						
							|  |  |  |         .replace("ctrl+alt", "alt+ctrl") | 
					
						
							|  |  |  |         .replace("meta+alt", "alt+meta"); // alt needs to be first;
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |     bindGlobalShortcut, | 
					
						
							|  |  |  |     bindElShortcut, | 
					
						
							|  |  |  |     normalizeShortcut | 
					
						
							|  |  |  | } |