| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | import protectedSessionHolder from "./protected_session_holder.js"; | 
					
						
							|  |  |  | import server from "./server.js"; | 
					
						
							|  |  |  | import utils from "./utils.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2020-01-25 09:56:08 +01:00
										 |  |  | import treeService from "./tree.js"; | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | import Component from "../widgets/component.js"; | 
					
						
							| 
									
										
										
										
											2021-04-16 23:01:56 +02:00
										 |  |  | import froca from "./froca.js"; | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  | import hoistedNoteService from "./hoisted_note.js"; | 
					
						
							| 
									
										
										
										
											2021-09-20 21:08:41 +02:00
										 |  |  | import options from "./options.js"; | 
					
						
							| 
									
										
										
										
											2019-05-01 23:06:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  | class NoteContext extends Component { | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |      * @param {string|null} ntxId | 
					
						
							| 
									
										
										
										
											2019-05-11 19:44:58 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |     constructor(ntxId = null, hoistedNoteId = 'root', mainNtxId = null) { | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2020-01-15 21:36:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         this.ntxId = ntxId || utils.randomString(4); | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |         this.hoistedNoteId = hoistedNoteId; | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         this.mainNtxId = mainNtxId; | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-11-01 08:50:01 +01:00
										 |  |  |         this.hoistedNoteId = 'root'; | 
					
						
							| 
									
										
										
										
											2021-11-01 08:37:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +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 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 12:26:42 +01:00
										 |  |  |     async setNote(inputNotePath, triggerSwitchEvent = true) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         await this.triggerEvent('beforeNoteSwitch', {noteContext: this}); | 
					
						
							| 
									
										
										
										
											2020-01-19 20:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 21:56:45 +01:00
										 |  |  |         utils.closeActiveDialog(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 23:33:27 +02:00
										 |  |  |         this.notePath = resolvedNotePath; | 
					
						
							| 
									
										
										
										
											2021-09-20 23:04:41 +02:00
										 |  |  |         ({noteId: this.noteId, parentNoteId: this.parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(resolvedNotePath)); | 
					
						
							| 
									
										
										
										
											2020-01-25 14:37:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |         this.readOnlyTemporarilyDisabled = false; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 12:26:42 +01:00
										 |  |  |         if (triggerSwitchEvent) { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-19 23:58:13 +02:00
										 |  |  |         if (utils.isDesktop()) { | 
					
						
							|  |  |  |             // close dangling autocompletes after closing the tab
 | 
					
						
							|  |  |  |             $(".aa-input").autocomplete("close"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-13 22:08:06 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 13:04:08 +02:00
										 |  |  |     getSubContexts() { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         return appContext.tabManager.noteContexts.filter(nc => nc.ntxId === this.ntxId || nc.mainNtxId === this.ntxId); | 
					
						
							| 
									
										
										
										
											2021-05-20 23:13:34 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-24 22:29:49 +02:00
										 |  |  |     isMainContext() { | 
					
						
							|  |  |  |         // if null then this is a main context
 | 
					
						
							|  |  |  |         return !this.mainNtxId; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             catch (e) { | 
					
						
							|  |  |  |                 this.mainNtxId = null; | 
					
						
							|  |  |  |                 return this; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-05-20 23:13:34 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return this; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |     saveToRecentNotes(resolvedNotePath) { | 
					
						
							|  |  |  |         setTimeout(async () => { | 
					
						
							|  |  |  |             // we include the note into recent list only if the user stayed on the note at least 5 seconds
 | 
					
						
							|  |  |  |             if (resolvedNotePath && resolvedNotePath === this.notePath) { | 
					
						
							|  |  |  |                 await server.post('recent-notes', { | 
					
						
							|  |  |  |                     noteId: this.note.noteId, | 
					
						
							|  |  |  |                     notePath: this.notePath | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, 5000); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async getResolvedNotePath(inputNotePath) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (resolvedNotePath === this.notePath) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (await hoistedNoteService.checkNoteAccess(resolvedNotePath, this) === false) { | 
					
						
							|  |  |  |             return; // note is outside of hoisted subtree and user chose not to unhoist
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return resolvedNotePath; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 22:29:32 +01:00
										 |  |  |     /** @property {NoteShort} */ | 
					
						
							|  |  |  |     get note() { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 20:58:34 +02:00
										 |  |  |     /** @property {string[]} */ | 
					
						
							|  |  |  |     get notePathArray() { | 
					
						
							|  |  |  |         return this.notePath ? this.notePath.split('/') : []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 14:53:45 +02:00
										 |  |  |     /** @returns {NoteComplement} */ | 
					
						
							| 
									
										
										
										
											2020-02-01 11:33:31 +01:00
										 |  |  |     async getNoteComplement() { | 
					
						
							|  |  |  |         if (!this.noteId) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |         return await froca.getNoteComplement(this.noteId); | 
					
						
							| 
									
										
										
										
											2020-02-01 11:33:31 +01: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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |     getTabState() { | 
					
						
							|  |  |  |         if (!this.notePath) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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, | 
					
						
							| 
									
										
										
										
											2020-02-08 21:23:42 +01:00
										 |  |  |             active: this.isActive() | 
					
						
							| 
									
										
										
										
											2019-08-15 10:04:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |     async unhoist() { | 
					
						
							|  |  |  |         await this.setHoistedNoteId('root'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async setHoistedNoteId(noteIdToHoist) { | 
					
						
							| 
									
										
										
										
											2021-11-01 08:50:01 +01:00
										 |  |  |         this.hoistedNoteId = noteIdToHoist; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!this.notePathArray?.includes(noteIdToHoist)) { | 
					
						
							| 
									
										
										
										
											2021-02-07 21:27:09 +01:00
										 |  |  |             await this.setNote(noteIdToHoist); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |         await this.triggerEvent('hoistedNoteChanged', { | 
					
						
							|  |  |  |             noteId: noteIdToHoist, | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |             ntxId: this.ntxId | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |     async isReadOnly() { | 
					
						
							|  |  |  |         if (this.readOnlyTemporarilyDisabled) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 23:50:56 +02:00
										 |  |  |         // "readOnly" is a state valid only for text/code notes
 | 
					
						
							|  |  |  |         if (!this.note || this.note.type !== 'text' && this.note.type !== 'code') { | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.note.hasLabel('readOnly')) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const noteComplement = await this.getNoteComplement(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 21:12:35 +02:00
										 |  |  |         const sizeLimit = this.note.type === 'text' ? | 
					
						
							|  |  |  |             options.getInt('autoReadonlySizeText') | 
					
						
							|  |  |  |                 : options.getInt('autoReadonlySizeCode'); | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return noteComplement.content | 
					
						
							| 
									
										
										
										
											2021-09-20 21:12:35 +02:00
										 |  |  |             && noteComplement.content.length > sizeLimit | 
					
						
							| 
									
										
										
										
											2021-06-15 22:59:13 +02:00
										 |  |  |             && !this.note.hasLabel('autoReadOnlyDisabled'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:23:49 +01:00
										 |  |  |     async entitiesReloadedEvent({loadResults}) { | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  |         if (loadResults.isNoteReloaded(this.noteId)) { | 
					
						
							| 
									
										
										
										
											2021-08-24 22:37:00 +02:00
										 |  |  |             const note = loadResults.getEntity('notes', this.noteId); | 
					
						
							| 
									
										
										
										
											2020-02-09 22:31:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (note.isDeleted) { | 
					
						
							|  |  |  |                 this.noteId = null; | 
					
						
							|  |  |  |                 this.notePath = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +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() { | 
					
						
							| 
									
										
										
										
											2022-01-12 21:14:12 +01:00
										 |  |  |         return this.note | 
					
						
							|  |  |  |             && this.note.hasChildren() | 
					
						
							| 
									
										
										
										
											2022-01-07 19:33:59 +01:00
										 |  |  |             && ['book', 'text', 'code'].includes(this.note.type) | 
					
						
							|  |  |  |             && this.note.mime !== 'text/x-sqlite;schema=trilium' | 
					
						
							|  |  |  |             && !this.note.hasLabel('hideChildrenOverview'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-17 23:53:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async getTextEditor(callback) { | 
					
						
							| 
									
										
										
										
											2022-05-25 23:38:06 +02:00
										 |  |  |         return new Promise(resolve => appContext.triggerCommand('executeWithTextEditor', { | 
					
						
							| 
									
										
										
										
											2022-05-17 23:53:35 +02:00
										 |  |  |             callback, | 
					
						
							|  |  |  |             resolve, | 
					
						
							|  |  |  |             ntxId: this.ntxId | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async getCodeEditor() { | 
					
						
							| 
									
										
										
										
											2022-05-25 23:38:06 +02:00
										 |  |  |         return new Promise(resolve => appContext.triggerCommand('executeWithCodeEditor', { | 
					
						
							|  |  |  |             resolve, | 
					
						
							|  |  |  |             ntxId: this.ntxId | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async getContentElement() { | 
					
						
							|  |  |  |         return new Promise(resolve => appContext.triggerCommand('executeWithContentElement', { | 
					
						
							| 
									
										
										
										
											2022-05-17 23:53:35 +02:00
										 |  |  |             resolve, | 
					
						
							|  |  |  |             ntxId: this.ntxId | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  | export default NoteContext; |