| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | import protectedSessionHolder from "../services/protected_session_holder.js"; | 
					
						
							|  |  |  | import server from "../services/server.js"; | 
					
						
							|  |  |  | import utils from "../services/utils.js"; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:36:24 +02:00
										 |  |  | import appContext, { type EventData, type EventListener } from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | import treeService from "../services/tree.js"; | 
					
						
							|  |  |  | import Component from "./component.js"; | 
					
						
							|  |  |  | import froca from "../services/froca.js"; | 
					
						
							|  |  |  | import hoistedNoteService from "../services/hoisted_note.js"; | 
					
						
							|  |  |  | import options from "../services/options.js"; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:36:24 +02:00
										 |  |  | import type { ViewScope } from "../services/link.js"; | 
					
						
							| 
									
										
										
										
											2025-01-13 23:18:10 +02:00
										 |  |  | import type FNote from "../entities/fnote.js"; | 
					
						
							| 
									
										
										
										
											2025-02-07 20:27:22 +02:00
										 |  |  | import type TypeWidget from "../widgets/type_widgets/type_widget.js"; | 
					
						
							| 
									
										
										
										
											2025-05-05 21:44:27 +03:00
										 |  |  | import type { CKTextEditor } from "@triliumnext/ckeditor5"; | 
					
						
							| 
									
										
										
										
											2025-05-12 18:24:36 +03:00
										 |  |  | import type CodeMirror from "@triliumnext/codemirror"; | 
					
						
							| 
									
										
										
										
											2025-06-19 22:44:02 +03:00
										 |  |  | import { closeActiveDialog } from "../services/dialog.js"; | 
					
						
							| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-24 10:10:34 +02:00
										 |  |  | export interface SetNoteOpts { | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |     triggerSwitchEvent?: unknown; | 
					
						
							|  |  |  |     viewScope?: ViewScope; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-05 21:44:27 +03:00
										 |  |  | export type GetTextEditorCallback = (editor: CKTextEditor) => void; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | class NoteContext extends Component implements EventListener<"entitiesReloaded"> { | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |     ntxId: string | null; | 
					
						
							|  |  |  |     hoistedNoteId: string; | 
					
						
							| 
									
										
										
										
											2025-01-09 20:20:06 +02:00
										 |  |  |     mainNtxId: string | null; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 21:47:36 +02:00
										 |  |  |     notePath?: string | null; | 
					
						
							| 
									
										
										
										
											2025-01-09 20:20:06 +02:00
										 |  |  |     noteId?: string | null; | 
					
						
							| 
									
										
										
										
											2025-01-17 21:25:36 +02:00
										 |  |  |     parentNoteId?: string | null; | 
					
						
							| 
									
										
										
										
											2025-01-04 11:51:16 +02:00
										 |  |  |     viewScope?: ViewScope; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     constructor(ntxId: string | null = null, hoistedNoteId: string = "root", mainNtxId: string | null = null) { | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |         this.ntxId = ntxId || NoteContext.generateNtxId(); | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |         this.hoistedNoteId = hoistedNoteId; | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         this.mainNtxId = mainNtxId; | 
					
						
							| 
									
										
										
										
											2023-01-24 16:24:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.resetViewScope(); | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 21:41:55 +02:00
										 |  |  |     static generateNtxId() { | 
					
						
							|  |  |  |         return utils.randomString(6); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 11:46:35 +01:00
										 |  |  |     setEmpty() { | 
					
						
							| 
									
										
										
										
											2021-11-01 08:37:59 +01:00
										 |  |  |         this.notePath = null; | 
					
						
							|  |  |  |         this.noteId = null; | 
					
						
							|  |  |  |         this.parentNoteId = null; | 
					
						
							| 
									
										
										
										
											2022-12-13 21:45:57 +01:00
										 |  |  |         // hoisted note is kept intentionally
 | 
					
						
							| 
									
										
										
										
											2021-11-01 08:37:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         this.triggerEvent("noteSwitched", { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |             noteContext: this, | 
					
						
							| 
									
										
										
										
											2020-02-28 11:46:35 +01:00
										 |  |  |             notePath: this.notePath | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2023-01-24 16:24:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.resetViewScope(); | 
					
						
							| 
									
										
										
										
											2020-02-28 11:46:35 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-10 14:35:58 +01:00
										 |  |  |     isEmpty() { | 
					
						
							|  |  |  |         return !this.noteId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 21:47:36 +02:00
										 |  |  |     async setNote(inputNotePath: string | undefined, opts: SetNoteOpts = {}) { | 
					
						
							| 
									
										
										
										
											2023-02-14 16:06:49 +01:00
										 |  |  |         opts.triggerSwitchEvent = opts.triggerSwitchEvent !== undefined ? opts.triggerSwitchEvent : true; | 
					
						
							| 
									
										
										
										
											2023-04-11 17:45:51 +02:00
										 |  |  |         opts.viewScope = opts.viewScope || {}; | 
					
						
							|  |  |  |         opts.viewScope.viewMode = opts.viewScope.viewMode || "default"; | 
					
						
							| 
									
										
										
										
											2023-02-14 16:06:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 21:47:36 +02:00
										 |  |  |         if (!inputNotePath) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |         const resolvedNotePath = await this.getResolvedNotePath(inputNotePath); | 
					
						
							| 
									
										
										
										
											2020-03-23 16:39:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |         if (!resolvedNotePath) { | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 17:45:51 +02:00
										 |  |  |         if (this.notePath === resolvedNotePath && utils.areObjectsEqual(this.viewScope, opts.viewScope)) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         await this.triggerEvent("beforeNoteSwitch", { noteContext: this }); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-19 22:44:02 +03:00
										 |  |  |         closeActiveDialog(); | 
					
						
							| 
									
										
										
										
											2020-02-03 21:56:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 23:33:27 +02:00
										 |  |  |         this.notePath = resolvedNotePath; | 
					
						
							| 
									
										
										
										
											2023-04-11 17:45:51 +02:00
										 |  |  |         this.viewScope = opts.viewScope; | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         ({ noteId: this.noteId, parentNoteId: this.parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(resolvedNotePath)); | 
					
						
							| 
									
										
										
										
											2020-02-02 11:44:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |         this.saveToRecentNotes(resolvedNotePath); | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 11:09:07 +02:00
										 |  |  |         protectedSessionHolder.touchProtectedSessionIfNecessary(this.note); | 
					
						
							| 
									
										
										
										
											2020-01-19 11:03:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 16:06:49 +01:00
										 |  |  |         if (opts.triggerSwitchEvent) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             await this.triggerEvent("noteSwitched", { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |                 noteContext: this, | 
					
						
							| 
									
										
										
										
											2020-02-27 12:26:42 +01:00
										 |  |  |                 notePath: this.notePath | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-19 22:10:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-24 16:24:51 +01:00
										 |  |  |         await this.setHoistedNoteIfNeeded(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (utils.isMobile()) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             this.triggerCommand("setActiveScreen", { screen: "detail" }); | 
					
						
							| 
									
										
										
										
											2023-01-24 16:24:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async setHoistedNoteIfNeeded() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (this.hoistedNoteId === "root" && this.notePath?.startsWith("root/_hidden") && !this.note?.isLabelTruthy("keepCurrentHoisting")) { | 
					
						
							| 
									
										
										
										
											2022-12-23 15:46:33 +01:00
										 |  |  |             // hidden subtree displays only when hoisted, so it doesn't make sense to keep root as hoisted note
 | 
					
						
							| 
									
										
										
										
											2022-12-08 23:36:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             let hoistedNoteId = "_hidden"; | 
					
						
							| 
									
										
										
										
											2022-12-08 23:36:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |             if (this.note?.isLaunchBarConfig()) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |                 hoistedNoteId = "_lbRoot"; | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |             } else if (this.note?.isOptions()) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |                 hoistedNoteId = "_options"; | 
					
						
							| 
									
										
										
										
											2022-12-08 23:36:49 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await this.setHoistedNoteId(hoistedNoteId); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 13:04:08 +02:00
										 |  |  |     getSubContexts() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return appContext.tabManager.noteContexts.filter((nc) => nc.ntxId === this.ntxId || nc.mainNtxId === this.ntxId); | 
					
						
							| 
									
										
										
										
											2021-05-20 23:13:34 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 09:01:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * A main context represents a tab and also the first split. Further splits are the children contexts of the main context. | 
					
						
							|  |  |  |      * Imagine you have a tab with 3 splits, each showing notes A, B, C (in this order). | 
					
						
							|  |  |  |      * In such a scenario, A context is the main context (also representing the tab as a whole), and B, C are the children | 
					
						
							|  |  |  |      * of context A. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @returns {boolean} true if the context is main (= tab) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-24 22:29:49 +02:00
										 |  |  |     isMainContext() { | 
					
						
							| 
									
										
										
										
											2023-05-05 23:41:11 +02:00
										 |  |  |         // if null, then this is a main context
 | 
					
						
							| 
									
										
										
										
											2021-05-24 22:29:49 +02:00
										 |  |  |         return !this.mainNtxId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 09:01:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * See docs for isMainContext() for better explanation. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @returns {NoteContext} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-22 13:04:08 +02:00
										 |  |  |     getMainContext() { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (this.mainNtxId) { | 
					
						
							| 
									
										
										
										
											2021-10-27 21:32:03 +02:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 return appContext.tabManager.getNoteContextById(this.mainNtxId); | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             } catch (e) { | 
					
						
							| 
									
										
										
										
											2021-10-27 21:32:03 +02:00
										 |  |  |                 this.mainNtxId = null; | 
					
						
							|  |  |  |                 return this; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-05-20 23:13:34 +02:00
										 |  |  |             return this; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |     saveToRecentNotes(resolvedNotePath: string) { | 
					
						
							| 
									
										
										
										
											2025-05-21 18:36:58 +02:00
										 |  |  |         if (options.is("databaseReadonly")) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |         setTimeout(async () => { | 
					
						
							| 
									
										
										
										
											2023-05-05 23:41:11 +02:00
										 |  |  |             // we include the note in the recent list only if the user stayed on the note at least 5 seconds
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |             if (resolvedNotePath && resolvedNotePath === this.notePath) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |                 await server.post("recent-notes", { | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |                     noteId: this.note?.noteId, | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |                     notePath: this.notePath | 
					
						
							|  |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2025-02-01 11:07:47 +02:00
										 |  |  |                 utils.reloadTray(); | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, 5000); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |     async getResolvedNotePath(inputNotePath: string) { | 
					
						
							| 
									
										
										
										
											2021-03-03 21:49:57 +01:00
										 |  |  |         const resolvedNotePath = await treeService.resolveNotePath(inputNotePath, this.hoistedNoteId); | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!resolvedNotePath) { | 
					
						
							|  |  |  |             logError(`Cannot resolve note path ${inputNotePath}`); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if ((await hoistedNoteService.checkNoteAccess(resolvedNotePath, this)) === false) { | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |             return; // note is outside of hoisted subtree and user chose not to unhoist
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return resolvedNotePath; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |     get note(): FNote | null { | 
					
						
							| 
									
										
										
										
											2021-05-11 22:00:16 +02:00
										 |  |  |         if (!this.noteId || !(this.noteId in froca.notes)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2020-10-19 22:10:25 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |         return froca.notes[this.noteId]; | 
					
						
							| 
									
										
										
										
											2020-02-01 11:33:31 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 23:17:23 +02:00
										 |  |  |     /** @returns {string[]} */ | 
					
						
							| 
									
										
										
										
											2020-08-17 20:58:34 +02:00
										 |  |  |     get notePathArray() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return this.notePath ? this.notePath.split("/") : []; | 
					
						
							| 
									
										
										
										
											2020-08-17 20:58:34 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 21:43:23 +01:00
										 |  |  |     isActive() { | 
					
						
							| 
									
										
										
										
											2021-05-22 13:04:08 +02:00
										 |  |  |         return appContext.tabManager.activeNtxId === this.ntxId; | 
					
						
							| 
									
										
										
										
											2020-01-21 21:43:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:00:04 +02:00
										 |  |  |     getPojoState() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (this.hoistedNoteId !== "root") { | 
					
						
							| 
									
										
										
										
											2022-12-13 16:57:46 +01:00
										 |  |  |             // keeping empty hoisted tab is esp. important for mobile (e.g. opened launcher config)
 | 
					
						
							| 
									
										
										
										
											2023-05-31 00:03:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!this.notePath && this.getSubContexts().length === 0) { | 
					
						
							|  |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |             ntxId: this.ntxId, | 
					
						
							|  |  |  |             mainNtxId: this.mainNtxId, | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |             notePath: this.notePath, | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |             hoistedNoteId: this.hoistedNoteId, | 
					
						
							| 
									
										
										
										
											2023-02-14 16:06:49 +01:00
										 |  |  |             active: this.isActive(), | 
					
						
							| 
									
										
										
										
											2023-04-03 23:47:24 +02:00
										 |  |  |             viewScope: this.viewScope | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |     async unhoist() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         await this.setHoistedNoteId("root"); | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |     async setHoistedNoteId(noteIdToHoist: string) { | 
					
						
							| 
									
										
										
										
											2022-12-13 21:45:57 +01:00
										 |  |  |         if (this.hoistedNoteId === noteIdToHoist) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 08:50:01 +01:00
										 |  |  |         this.hoistedNoteId = noteIdToHoist; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-04 21:03:03 +02:00
										 |  |  |         if (!this.notePathArray?.includes(noteIdToHoist)) { | 
					
						
							| 
									
										
										
										
											2021-02-07 21:27:09 +01:00
										 |  |  |             await this.setNote(noteIdToHoist); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         await this.triggerEvent("hoistedNoteChanged", { | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |             noteId: noteIdToHoist, | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |             ntxId: this.ntxId | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 22:21:51 +02:00
										 |  |  |     /** @returns {Promise<boolean>} */ | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |     async isReadOnly() { | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |         if (this?.viewScope?.readOnlyTemporarilyDisabled) { | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 23:50:56 +02:00
										 |  |  |         // "readOnly" is a state valid only for text/code notes
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (!this.note || (this.note.type !== "text" && this.note.type !== "code")) { | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-21 17:24:00 +02:00
										 |  |  |         if (options.is("databaseReadonly")) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (this.note.isLabelTruthy("readOnly")) { | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         if (this.viewScope?.viewMode === "source") { | 
					
						
							| 
									
										
										
										
											2023-05-13 15:58:32 +02:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-13 21:59:43 +00:00
										 |  |  |         // Store the initial decision about read-only status in the viewScope
 | 
					
						
							|  |  |  |         // This will be "remembered" until the viewScope is refreshed
 | 
					
						
							|  |  |  |         if (!this.viewScope) { | 
					
						
							|  |  |  |             this.resetViewScope(); | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-06 19:29:06 +03:00
										 |  |  |         const viewScope = this.viewScope!; | 
					
						
							| 
									
										
										
										
											2025-05-13 21:59:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-06 19:29:06 +03:00
										 |  |  |         if (viewScope.isReadOnly === undefined) { | 
					
						
							| 
									
										
										
										
											2025-05-13 21:59:43 +00:00
										 |  |  |             const blob = await this.note.getBlob(); | 
					
						
							|  |  |  |             if (!blob) { | 
					
						
							| 
									
										
										
										
											2025-06-06 19:29:06 +03:00
										 |  |  |                 viewScope.isReadOnly = false; | 
					
						
							| 
									
										
										
										
											2025-05-13 21:59:43 +00:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const sizeLimit = this.note.type === "text" | 
					
						
							|  |  |  |                 ? options.getInt("autoReadonlySizeText") | 
					
						
							|  |  |  |                 : options.getInt("autoReadonlySizeCode"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-06 19:29:06 +03:00
										 |  |  |             viewScope.isReadOnly = Boolean(sizeLimit && | 
					
						
							| 
									
										
										
										
											2025-05-13 21:59:43 +00:00
										 |  |  |                 blob.contentLength > sizeLimit && | 
					
						
							|  |  |  |                 !this.note.isLabelTruthy("autoReadOnlyDisabled")); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-13 21:59:43 +00:00
										 |  |  |         // Return the cached decision, which won't change until viewScope is reset
 | 
					
						
							| 
									
										
										
										
											2025-06-06 19:29:06 +03:00
										 |  |  |         return viewScope.isReadOnly || false; | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |         if (this.noteId && loadResults.isNoteReloaded(this.noteId)) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             const noteRow = loadResults.getEntityRow("notes", this.noteId); | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-05 16:26:05 +02:00
										 |  |  |             if (noteRow.isDeleted) { | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  |                 this.noteId = null; | 
					
						
							|  |  |  |                 this.notePath = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |                 this.triggerEvent("noteSwitched", { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |                     noteContext: this, | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  |                     notePath: this.notePath | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-12 21:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |     hasNoteList() { | 
					
						
							| 
									
										
										
										
											2025-06-25 18:31:45 +03:00
										 |  |  |         const note = this.note; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!note) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!["default", "contextual-help"].includes(this.viewScope?.viewMode ?? "")) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-21 15:01:55 +03:00
										 |  |  |         // Collections must always display a note list, even if no children.
 | 
					
						
							|  |  |  |         const viewType = note.getLabelValue("viewType") ?? "grid"; | 
					
						
							|  |  |  |         if (!["list", "grid"].includes(viewType)) { | 
					
						
							| 
									
										
										
										
											2025-06-25 18:31:45 +03:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!note.hasChildren()) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!["book", "text", "code"].includes(note.type)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (note.mime === "text/x-sqlite;schema=trilium") { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (note.isLabelTruthy("hideChildrenOverview")) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-17 23:53:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 21:47:36 +02:00
										 |  |  |     async getTextEditor(callback?: GetTextEditorCallback) { | 
					
						
							| 
									
										
										
										
											2025-05-05 21:44:27 +03:00
										 |  |  |         return this.timeout<CKTextEditor>( | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |             new Promise((resolve) => | 
					
						
							|  |  |  |                 appContext.triggerCommand("executeWithTextEditor", { | 
					
						
							|  |  |  |                     callback, | 
					
						
							|  |  |  |                     resolve, | 
					
						
							|  |  |  |                     ntxId: this.ntxId | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-05-17 23:53:35 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async getCodeEditor() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return this.timeout( | 
					
						
							| 
									
										
										
										
											2025-05-12 18:24:36 +03:00
										 |  |  |             new Promise<CodeMirror>((resolve) => | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |                 appContext.triggerCommand("executeWithCodeEditor", { | 
					
						
							|  |  |  |                     resolve, | 
					
						
							|  |  |  |                     ntxId: this.ntxId | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-05-25 23:38:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-02 19:08:44 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns a promise which will retrieve the JQuery element of the content of this note context. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Do note that retrieving the content element needs to be handled by the type widget, which is the one which | 
					
						
							|  |  |  |      * provides the content element by listening to the `executeWithContentElement` event. Not all note types support | 
					
						
							|  |  |  |      * this. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * If no content could be determined `null` is returned instead. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-05-25 23:38:06 +02:00
										 |  |  |     async getContentElement() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return this.timeout<JQuery<HTMLElement>>( | 
					
						
							|  |  |  |             new Promise((resolve) => | 
					
						
							|  |  |  |                 appContext.triggerCommand("executeWithContentElement", { | 
					
						
							|  |  |  |                     resolve, | 
					
						
							|  |  |  |                     ntxId: this.ntxId | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2022-05-17 23:53:35 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-06-03 22:05:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async getTypeWidget() { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return this.timeout( | 
					
						
							| 
									
										
										
										
											2025-02-07 20:27:22 +02:00
										 |  |  |             new Promise<TypeWidget | null>((resolve) => | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |                 appContext.triggerCommand("executeWithTypeWidget", { | 
					
						
							|  |  |  |                     resolve, | 
					
						
							|  |  |  |                     ntxId: this.ntxId | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:31:04 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-07 12:34:10 +02:00
										 |  |  |     timeout<T>(promise: Promise<T | null>) { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         return Promise.race([promise, new Promise((res) => setTimeout(() => res(null), 200))]) as Promise<T>; | 
					
						
							| 
									
										
										
										
											2022-06-03 22:05:18 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-24 16:24:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resetViewScope() { | 
					
						
							|  |  |  |         // view scope contains data specific to one note context and one "view".
 | 
					
						
							| 
									
										
										
										
											2023-05-05 23:41:11 +02:00
										 |  |  |         // it is used to e.g., make read-only note temporarily editable or to hide TOC
 | 
					
						
							| 
									
										
										
										
											2023-01-24 16:24:51 +01:00
										 |  |  |         // this is reset after navigating to a different note
 | 
					
						
							|  |  |  |         this.viewScope = {}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-11 21:41:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async getNavigationTitle() { | 
					
						
							|  |  |  |         if (!this.note) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const { note, viewScope } = this; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-05 21:42:34 +02:00
										 |  |  |         const isNormalView = (viewScope?.viewMode === "default" || viewScope?.viewMode === "contextual-help"); | 
					
						
							|  |  |  |         let title = (isNormalView ? note.title : `${note.title}: ${viewScope?.viewMode}`); | 
					
						
							| 
									
										
										
										
											2023-04-11 21:41:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 15:16:41 +02:00
										 |  |  |         if (viewScope?.attachmentId) { | 
					
						
							| 
									
										
										
										
											2023-04-11 21:41:55 +02:00
										 |  |  |             // assuming the attachment has been already loaded
 | 
					
						
							|  |  |  |             const attachment = await note.getAttachmentById(viewScope.attachmentId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (attachment) { | 
					
						
							|  |  |  |                 title += `: ${attachment.title}`; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return title; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  | export default NoteContext; |