| 
									
										
										
										
											2024-09-24 09:57:16 +08:00
										 |  |  | import { t } from "../services/i18n.js"; | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +02:00
										 |  |  | import NoteContextAwareWidget from "./note_context_aware_widget.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | import protectedSessionHolder from "../services/protected_session_holder.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | import server from "../services/server.js"; | 
					
						
							| 
									
										
										
										
											2020-01-19 21:40:23 +01:00
										 |  |  | import SpacedUpdate from "../services/spaced_update.js"; | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  | import appContext, { type EventData } from "../components/app_context.js"; | 
					
						
							| 
									
										
										
										
											2021-09-23 23:34:51 +02:00
										 |  |  | import branchService from "../services/branches.js"; | 
					
						
							| 
									
										
										
										
											2022-12-01 00:17:15 +01:00
										 |  |  | import shortcutService from "../services/shortcuts.js"; | 
					
						
							| 
									
										
										
										
											2025-01-04 21:52:41 +02:00
										 |  |  | import utils from "../services/utils.js"; | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  | import type FNote from "../entities/fnote.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-01 23:24:21 +03:00
										 |  |  | const TPL = /*html*/`
 | 
					
						
							| 
									
										
										
										
											2021-06-13 22:55:31 +02:00
										 |  |  | <div class="note-title-widget"> | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     <style> | 
					
						
							| 
									
										
										
										
											2021-06-13 22:55:31 +02:00
										 |  |  |     .note-title-widget { | 
					
						
							| 
									
										
										
										
											2021-05-24 22:29:49 +02:00
										 |  |  |         flex-grow: 1000; | 
					
						
							| 
									
										
										
										
											2021-06-13 22:55:31 +02:00
										 |  |  |         height: 100%; | 
					
						
							| 
									
										
										
										
											2020-01-19 15:44:18 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 21:46:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 22:55:31 +02:00
										 |  |  |     .note-title-widget input.note-title { | 
					
						
							| 
									
										
										
										
											2025-02-08 10:27:22 +02:00
										 |  |  |         font-size: 110%; | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         border: 0; | 
					
						
							| 
									
										
										
										
											2024-08-17 10:59:12 +03:00
										 |  |  |         margin: 2px 0px; | 
					
						
							| 
									
										
										
										
											2020-01-19 18:05:06 +01:00
										 |  |  |         min-width: 5em; | 
					
						
							|  |  |  |         width: 100%; | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-04 21:46:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-08 10:27:22 +02:00
										 |  |  |     body.mobile .note-title-widget input.note-title { | 
					
						
							|  |  |  |         padding: 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     body.desktop .note-title-widget input.note-title { | 
					
						
							|  |  |  |         font-size: 180%; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 22:55:31 +02:00
										 |  |  |     .note-title-widget input.note-title.protected { | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |         text-shadow: 4px 4px 4px var(--muted-text-color); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |     </style> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     <input autocomplete="off" value="" placeholder="${t("note_title.placeholder")}" class="note-title" tabindex="100"> | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | </div>`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +02:00
										 |  |  | export default class NoteTitleWidget extends NoteContextAwareWidget { | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private $noteTitle!: JQuery<HTMLElement>; | 
					
						
							|  |  |  |     private deleteNoteOnEscape: boolean; | 
					
						
							|  |  |  |     private spacedUpdate: SpacedUpdate; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |     constructor() { | 
					
						
							|  |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.spacedUpdate = new SpacedUpdate(async () => { | 
					
						
							|  |  |  |             const title = this.$noteTitle.val(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |             if (this.note) { | 
					
						
							|  |  |  |                 protectedSessionHolder.touchProtectedSessionIfNecessary(this.note); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-04-25 11:09:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             await server.put(`notes/${this.noteId}/title`, { title }, this.componentId); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-02-27 23:39:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 23:34:51 +02:00
										 |  |  |         this.deleteNoteOnEscape = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 23:39:02 +01:00
										 |  |  |         appContext.addBeforeUnloadListener(this); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 20:15:05 +01:00
										 |  |  |     doRender() { | 
					
						
							| 
									
										
										
										
											2020-01-14 20:27:40 +01:00
										 |  |  |         this.$widget = $(TPL); | 
					
						
							|  |  |  |         this.$noteTitle = this.$widget.find(".note-title"); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         this.$noteTitle.on("input", () => this.spacedUpdate.scheduleUpdate()); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         this.$noteTitle.on("blur", () => { | 
					
						
							| 
									
										
										
										
											2022-01-17 20:54:57 +01:00
										 |  |  |             this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.deleteNoteOnEscape = false; | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-09-23 23:34:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         shortcutService.bindElShortcut(this.$noteTitle, "esc", () => { | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |             if (this.deleteNoteOnEscape && this.noteContext?.isActive() && this.noteContext?.note) { | 
					
						
							| 
									
										
										
										
											2021-09-23 23:34:51 +02:00
										 |  |  |                 branchService.deleteNotes(Object.values(this.noteContext.note.parentToBranch)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         shortcutService.bindElShortcut(this.$noteTitle, "return", () => { | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |             this.triggerCommand("focusOnDetail", { ntxId: this.noteContext?.ntxId }); | 
					
						
							| 
									
										
										
										
											2020-01-19 19:33:35 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |     async refreshWithNote(note: FNote) { | 
					
						
							| 
									
										
										
										
											2025-03-05 21:42:34 +02:00
										 |  |  |         const isReadOnly = | 
					
						
							|  |  |  |             (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) | 
					
						
							|  |  |  |             || utils.isLaunchBarConfig(note.noteId) | 
					
						
							|  |  |  |             || this.noteContext?.viewScope?.viewMode !== "default"; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-02 20:47:57 +01:00
										 |  |  |         this.$noteTitle.val(isReadOnly ? (await this.noteContext?.getNavigationTitle()) || "" : note.title); | 
					
						
							| 
									
										
										
										
											2023-04-11 21:41:55 +02:00
										 |  |  |         this.$noteTitle.prop("readonly", isReadOnly); | 
					
						
							| 
									
										
										
										
											2020-04-29 17:29:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:49:20 +02:00
										 |  |  |         this.setProtectedStatus(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |     setProtectedStatus(note: FNote) { | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |         this.$noteTitle.toggleClass("protected", !!note.isProtected); | 
					
						
							| 
									
										
										
										
											2020-01-12 19:05:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |     async beforeNoteSwitchEvent({ noteContext }: EventData<"beforeNoteSwitch">) { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:42:34 +02:00
										 |  |  |         if (this.isNoteContext(noteContext.ntxId)) { | 
					
						
							| 
									
										
										
										
											2020-01-19 21:12:53 +01:00
										 |  |  |             await this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |     async beforeNoteContextRemoveEvent({ ntxIds }: EventData<"beforeNoteContextRemove">) { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:42:34 +02:00
										 |  |  |         if (this.isNoteContext(ntxIds)) { | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  |             await this.spacedUpdate.updateNowIfNecessary(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:23:49 +01:00
										 |  |  |     focusOnTitleEvent() { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (this.noteContext && this.noteContext.isActive()) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             this.$noteTitle.trigger("focus"); | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     focusAndSelectTitleEvent({ isNewNote } = { isNewNote: false }) { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (this.noteContext && this.noteContext.isActive()) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             this.$noteTitle.trigger("focus").trigger("select"); | 
					
						
							| 
									
										
										
										
											2021-09-23 23:34:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             this.deleteNoteOnEscape = isNewNote; | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:07:56 +02:00
										 |  |  |     entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { | 
					
						
							|  |  |  |         if (loadResults.isNoteReloaded(this.noteId) && this.note) { | 
					
						
							| 
									
										
										
										
											2020-05-03 22:49:20 +02:00
										 |  |  |             // not updating the title specifically since the synced title might be older than what the user is currently typing
 | 
					
						
							|  |  |  |             this.setProtectedStatus(this.note); | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-04 22:57:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (loadResults.isNoteReloaded(this.noteId, this.componentId)) { | 
					
						
							|  |  |  |             this.refresh(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-29 00:01:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:23:49 +01:00
										 |  |  |     beforeUnloadEvent() { | 
					
						
							| 
									
										
										
										
											2021-02-27 23:39:02 +01:00
										 |  |  |         return this.spacedUpdate.isAllSavedAndTriggerUpdate(); | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-14 14:30:57 +02:00
										 |  |  | } |