| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							|  |  |  | import shortcutService from "../services/shortcuts.js"; | 
					
						
							|  |  |  | import server from "../services/server.js"; | 
					
						
							|  |  |  | import Component from "./component.js"; | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  | import froca from "../services/froca.js"; | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class ShortcutComponent extends Component { | 
					
						
							|  |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  |         super(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  |         server.get('keyboard-shortcuts-for-notes').then(shortcutAttributes => { | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  |             for (const attr of shortcutAttributes) { | 
					
						
							|  |  |  |                 this.bindNoteShortcutHandler(attr); | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 16:22:04 +01:00
										 |  |  |     bindNoteShortcutHandler(label) { | 
					
						
							|  |  |  |         const handler = () => appContext.tabManager.getActiveContext().setNote(label.noteId); | 
					
						
							|  |  |  |         const namespace = label.attributeId; | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 16:22:04 +01:00
										 |  |  |         if (label.isDeleted) { | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  |             shortcutService.removeGlobalShortcut(namespace); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-12-01 16:22:04 +01:00
										 |  |  |             shortcutService.bindGlobalShortcut(label.value, handler, namespace); | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-01 13:24:34 +01:00
										 |  |  |     async entitiesReloadedEvent({loadResults}) { | 
					
						
							|  |  |  |         for (const attr of loadResults.getAttributes()) { | 
					
						
							|  |  |  |             if (attr.type === 'label' && attr.name === 'keyboardShortcut') { | 
					
						
							|  |  |  |                 const note = await froca.getNote(attr.noteId); | 
					
						
							|  |  |  |                 // launcher shortcuts are handled specifically
 | 
					
						
							|  |  |  |                 if (note && note.type !== 'launcher') { | 
					
						
							|  |  |  |                     this.bindNoteShortcutHandler(attr); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |