| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | import utils from "../services/utils.js"; | 
					
						
							| 
									
										
										
										
											2025-01-14 19:12:29 +02:00
										 |  |  | import type { CommandMappings, CommandNames, EventData, EventNames } from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2020-02-01 11:15:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Abstract class for all components in the Trilium's frontend. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contains also event implementation with following properties: | 
					
						
							| 
									
										
										
										
											2023-01-15 21:04:17 +01:00
										 |  |  |  * - event / command distribution is synchronous which among others mean that events are well-ordered - event | 
					
						
							| 
									
										
										
										
											2020-06-10 23:43:59 +02:00
										 |  |  |  *   which was sent out first will also be processed first by the component | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |  * - execution of the event / command is asynchronous - each component executes the event on its own without regard for | 
					
						
							|  |  |  |  *   other components. | 
					
						
							| 
									
										
										
										
											2023-01-15 21:04:17 +01:00
										 |  |  |  * - although the execution is async, we are collecting all the promises, and therefore it is possible to wait until the | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |  *   event / command is executed in all components - by simply awaiting the `triggerEvent()`. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-01-05 12:21:01 +02:00
										 |  |  | export class TypedComponent<ChildT extends TypedComponent<ChildT>> { | 
					
						
							| 
									
										
										
										
											2024-12-22 16:22:10 +02:00
										 |  |  |     $widget!: JQuery<HTMLElement>; | 
					
						
							| 
									
										
										
										
											2024-07-25 20:55:04 +03:00
										 |  |  |     componentId: string; | 
					
						
							| 
									
										
										
										
											2025-01-05 12:21:01 +02:00
										 |  |  |     children: ChildT[]; | 
					
						
							| 
									
										
										
										
											2024-07-25 20:55:04 +03:00
										 |  |  |     initialized: Promise<void> | null; | 
					
						
							| 
									
										
										
										
											2025-01-05 12:21:01 +02:00
										 |  |  |     parent?: TypedComponent<any>; | 
					
						
							| 
									
										
										
										
											2025-03-03 21:20:42 +02:00
										 |  |  |     _position!: number; | 
					
						
							| 
									
										
										
										
											2024-07-25 19:21:24 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 00:58:10 +01:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         this.componentId = `${this.sanitizedClassName}-${utils.randomString(8)}`; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |         this.children = []; | 
					
						
							| 
									
										
										
										
											2022-06-23 23:03:35 +02:00
										 |  |  |         this.initialized = null; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-30 22:29:38 +02:00
										 |  |  |     get sanitizedClassName() { | 
					
						
							|  |  |  |         // webpack mangles names and sometimes uses unsafe characters
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return this.constructor.name.replace(/[^A-Z0-9]/gi, "_"); | 
					
						
							| 
									
										
										
										
											2020-08-30 22:29:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 21:20:42 +02:00
										 |  |  |     get position() { | 
					
						
							|  |  |  |         return this._position; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set position(newPosition: number) { | 
					
						
							|  |  |  |         this._position = newPosition; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 12:21:01 +02:00
										 |  |  |     setParent(parent: TypedComponent<any>) { | 
					
						
							| 
									
										
										
										
											2020-02-27 00:58:10 +01:00
										 |  |  |         this.parent = parent; | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 12:21:01 +02:00
										 |  |  |     child(...components: ChildT[]) { | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |         for (const component of components) { | 
					
						
							| 
									
										
										
										
											2020-03-16 22:14:18 +01:00
										 |  |  |             component.setParent(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.children.push(component); | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-19 22:46:37 +02:00
										 |  |  |     handleEvent<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null | undefined { | 
					
						
							| 
									
										
										
										
											2022-11-27 23:43:25 +01:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             const callMethodPromise = this.initialized ? this.initialized.then(() => this.callMethod((this as any)[`${name}Event`], data)) : this.callMethod((this as any)[`${name}Event`], data); | 
					
						
							| 
									
										
										
										
											2022-06-23 23:03:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-27 23:43:25 +01:00
										 |  |  |             const childrenPromise = this.handleEventInChildren(name, data); | 
					
						
							| 
									
										
										
										
											2022-06-23 23:03:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-27 23:43:25 +01:00
										 |  |  |             // don't create promises if not needed (optimization)
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             return callMethodPromise && childrenPromise ? Promise.all([callMethodPromise, childrenPromise]) : callMethodPromise || childrenPromise; | 
					
						
							|  |  |  |         } catch (e: any) { | 
					
						
							| 
									
										
										
										
											2022-11-27 23:43:25 +01:00
										 |  |  |             console.error(`Handling of event '${name}' failed in ${this.constructor.name} with error ${e.message} ${e.stack}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 22:50:36 +02:00
										 |  |  |     triggerEvent<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown> | undefined | null { | 
					
						
							| 
									
										
										
										
											2024-07-25 19:21:24 +03:00
										 |  |  |         return this.parent?.triggerEvent(name, data); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-14 19:12:29 +02:00
										 |  |  |     handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null { | 
					
						
							|  |  |  |         const promises: Promise<unknown>[] = []; | 
					
						
							| 
									
										
										
										
											2020-02-12 20:07:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  |         for (const child of this.children) { | 
					
						
							| 
									
										
										
										
											2025-01-14 19:12:29 +02:00
										 |  |  |             const ret = child.handleEvent(name, data) as Promise<void>; | 
					
						
							| 
									
										
										
										
											2022-07-10 15:01:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (ret) { | 
					
						
							|  |  |  |                 promises.push(ret); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-02-12 20:07:04 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-23 23:03:35 +02:00
										 |  |  |         // don't create promises if not needed (optimization)
 | 
					
						
							| 
									
										
										
										
											2022-07-10 15:01:05 +02:00
										 |  |  |         return promises.length > 0 ? Promise.all(promises) : null; | 
					
						
							| 
									
										
										
										
											2020-01-24 20:15:53 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-15 09:43:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-24 12:39:40 +02:00
										 |  |  |     triggerCommand<K extends CommandNames>(name: K, data?: CommandMappings[K]): Promise<unknown> | undefined | null { | 
					
						
							| 
									
										
										
										
											2024-07-25 19:21:24 +03:00
										 |  |  |         const fun = (this as any)[`${name}Command`]; | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |         if (fun) { | 
					
						
							|  |  |  |             return this.callMethod(fun, data); | 
					
						
							| 
									
										
										
										
											2024-11-22 23:02:43 +02:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             if (!this.parent) { | 
					
						
							|  |  |  |                 throw new Error(`Component "${this.componentId}" does not have a parent attached to propagate a command.`); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 19:43:19 +01:00
										 |  |  |             return this.parent.triggerCommand(name, data); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-25 19:21:24 +03:00
										 |  |  |     callMethod(fun: (arg: unknown) => Promise<unknown>, data: unknown) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (typeof fun !== "function") { | 
					
						
							| 
									
										
										
										
											2022-07-10 15:01:05 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2020-02-15 10:41:21 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-30 22:29:38 +02:00
										 |  |  |         const startTime = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const promise = fun.call(this, data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const took = Date.now() - startTime; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (glob.isDev && took > 20) { | 
					
						
							|  |  |  |             // measuring only sync handlers
 | 
					
						
							| 
									
										
										
										
											2020-08-30 22:29:38 +02:00
										 |  |  |             console.log(`Call to ${fun.name} in ${this.componentId} took ${took}ms`); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 15:01:05 +02:00
										 |  |  |         if (glob.isDev && promise) { | 
					
						
							|  |  |  |             return utils.timeLimit(promise, 20000, `Time limit failed on ${this.constructor.name} with ${fun.name}`); | 
					
						
							| 
									
										
										
										
											2021-02-21 21:28:00 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-15 09:43:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 15:01:05 +02:00
										 |  |  |         return promise; | 
					
						
							| 
									
										
										
										
											2020-02-15 09:43:47 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-19 22:58:08 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-05 12:21:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class Component extends TypedComponent<Component> {} |