mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 02:59:03 +01:00 
			
		
		
		
	refactor(react): allow binding multiple events at once
This commit is contained in:
		
							parent
							
								
									51e8a80ca3
								
							
						
					
					
						commit
						bea352855a
					
				| @ -52,16 +52,10 @@ export default function NoteTitleWidget() { | |||||||
|     // Manage focus.
 |     // Manage focus.
 | ||||||
|     const textBoxRef = useRef<HTMLInputElement>(null); |     const textBoxRef = useRef<HTMLInputElement>(null); | ||||||
|     const isNewNote = useRef<boolean>(); |     const isNewNote = useRef<boolean>(); | ||||||
|     useTriliumEventBeta("focusOnTitle", () => { |     useTriliumEventBeta([ "focusOnTitle", "focusAndSelectTitle" ], (e) => { | ||||||
|         if (noteContext?.isActive() && textBoxRef.current) { |  | ||||||
|             console.log(textBoxRef.current); |  | ||||||
|             textBoxRef.current.focus(); |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
|     useTriliumEventBeta("focusAndSelectTitle", ({ isNewNote: _isNewNote } ) => { |  | ||||||
|         if (noteContext?.isActive() && textBoxRef.current) { |         if (noteContext?.isActive() && textBoxRef.current) { | ||||||
|             textBoxRef.current.focus(); |             textBoxRef.current.focus(); | ||||||
|             isNewNote.current = _isNewNote; |             isNewNote.current = ("isNewNote" in e ? e.isNewNote : false); | ||||||
|         } |         } | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -87,11 +87,22 @@ export default function useTriliumEvent<T extends EventNames>(eventName: T, hand | |||||||
|     }, [ eventName, parentWidget, handler ]); |     }, [ eventName, parentWidget, handler ]); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function useTriliumEventBeta<T extends EventNames>(eventName: T, handler: TriliumEventHandler<T>) { | export function useTriliumEventBeta<T extends EventNames>(eventName: T | T[], handler: TriliumEventHandler<T>) { | ||||||
|     const parentComponent = useContext(ParentComponent) as ReactWrappedWidget; |     const parentComponent = useContext(ParentComponent) as ReactWrappedWidget; | ||||||
|     parentComponent.registerHandler(eventName, handler); |  | ||||||
| 
 | 
 | ||||||
|     return (() => parentComponent.removeHandler(eventName, handler)); |     if (Array.isArray(eventName)) { | ||||||
|  |         for (const eventSingleName of eventName) { | ||||||
|  |             parentComponent.registerHandler(eventSingleName, handler); | ||||||
|  |         } | ||||||
|  |         return (() => { | ||||||
|  |             for (const eventSingleName of eventName) { | ||||||
|  |                 parentComponent.removeHandler(eventSingleName, handler) | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |     } else { | ||||||
|  |         parentComponent.registerHandler(eventName, handler); | ||||||
|  |         return (() => parentComponent.removeHandler(eventName, handler)); | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000) { | export function useSpacedUpdate(callback: () => Promise<void>, interval = 1000) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran