| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | import type { EventData } from "../components/app_context.js"; | 
					
						
							|  |  |  | import type FNote from "../entities/fnote.js"; | 
					
						
							| 
									
										
										
										
											2024-08-15 11:33:44 +08:00
										 |  |  | import { t } from "../services/i18n.js"; | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  | import NoteContextAwareWidget from "./note_context_aware_widget.js"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const TPL = `
 | 
					
						
							|  |  |  | <div class="api-log-widget"> | 
					
						
							|  |  |  |     <style> | 
					
						
							|  |  |  |     .api-log-widget { | 
					
						
							|  |  |  |         padding: 15px; | 
					
						
							|  |  |  |         flex-grow: 1; | 
					
						
							|  |  |  |         max-height: 40%; | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |         position: relative; | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     .hidden-api-log { | 
					
						
							|  |  |  |         display: none; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     .api-log-container { | 
					
						
							|  |  |  |         overflow: auto; | 
					
						
							|  |  |  |         height: 100%; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |     .close-api-log-button { | 
					
						
							|  |  |  |         padding: 5px; | 
					
						
							|  |  |  |         border: 1px solid var(--button-border-color); | 
					
						
							|  |  |  |         background-color: var(--button-background-color); | 
					
						
							| 
									
										
										
										
											2022-09-18 13:52:19 +02:00
										 |  |  |         border-radius: var(--button-border-radius); | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |         color: var(--button-text-color); | 
					
						
							|  |  |  |         position: absolute; | 
					
						
							|  |  |  |         top: 10px; | 
					
						
							|  |  |  |         right: 40px; | 
					
						
							|  |  |  |         cursor: pointer; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     </style> | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     <div class="bx bx-x close-api-log-button" title="${t("api_log.close")}"></div> | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     <div class="api-log-container"></div> | 
					
						
							|  |  |  | </div>`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class ApiLogWidget extends NoteContextAwareWidget { | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private $logContainer!: JQuery<HTMLElement>; | 
					
						
							|  |  |  |     private $closeButton!: JQuery<HTMLElement>; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     isEnabled() { | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  |         return !!this.note && this.note.mime.startsWith("application/javascript;env=") && super.isEnabled(); | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     doRender() { | 
					
						
							|  |  |  |         this.$widget = $(TPL); | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |         this.toggle(false); | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         this.$logContainer = this.$widget.find(".api-log-container"); | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |         this.$closeButton = this.$widget.find(".close-api-log-button"); | 
					
						
							|  |  |  |         this.$closeButton.on("click", () => this.toggle(false)); | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  |     async refreshWithNote(note: FNote) { | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |         this.$logContainer.empty(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  |     apiLogMessagesEvent({ messages, noteId }: EventData<"apiLogMessages">) { | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  |         if (!this.isNote(noteId)) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |         this.toggle(true); | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (const message of messages) { | 
					
						
							|  |  |  |             this.$logContainer.append(message).append($("<br>")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:26:15 +02:00
										 |  |  |     toggle(show: boolean) { | 
					
						
							| 
									
										
										
										
											2022-09-18 08:48:01 +02:00
										 |  |  |         this.$widget.toggleClass("hidden-api-log", !show); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-17 23:06:17 +02:00
										 |  |  | } |