| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | import utils from "../services/utils.js"; | 
					
						
							|  |  |  | import dateNoteService from "../services/date_notes.js"; | 
					
						
							|  |  |  | import protectedSessionHolder from '../services/protected_session_holder.js'; | 
					
						
							|  |  |  | import server from "../services/server.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | import Component from "./component.js"; | 
					
						
							|  |  |  | import toastService from "../services/toast.js"; | 
					
						
							|  |  |  | import ws from "../services/ws.js"; | 
					
						
							|  |  |  | import bundleService from "../services/bundle.js"; | 
					
						
							|  |  |  | import froca from "../services/froca.js"; | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  | export default class Entrypoints extends Component { | 
					
						
							| 
									
										
										
										
											2020-02-27 10:03:14 +01:00
										 |  |  |     constructor() { | 
					
						
							|  |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2018-03-26 22:29:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-01 11:04:42 +01:00
										 |  |  |         if (jQuery.hotkeys) { | 
					
						
							|  |  |  |             // hot keys are active also inside inputs and content editables
 | 
					
						
							|  |  |  |             jQuery.hotkeys.options.filterInputAcceptingElements = false; | 
					
						
							|  |  |  |             jQuery.hotkeys.options.filterContentEditable = false; | 
					
						
							|  |  |  |             jQuery.hotkeys.options.filterTextInputs = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-10 20:44:50 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-28 21:52:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     openDevToolsCommand() { | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |         if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2021-11-16 22:43:08 +01:00
										 |  |  |             utils.dynamicRequire('@electron/remote').getCurrentWindow().toggleDevTools(); | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-02 13:02:20 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-10 20:00:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 22:21:13 +01:00
										 |  |  |     async createNoteIntoInboxCommand() { | 
					
						
							|  |  |  |         const inboxNote = await dateNoteService.getInboxNote(); | 
					
						
							| 
									
										
										
										
											2019-11-19 20:53:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 22:21:13 +01:00
										 |  |  |         const {note} = await server.post(`notes/${inboxNote.noteId}/children?target=into`, { | 
					
						
							| 
									
										
										
										
											2020-03-18 10:08:16 +01:00
										 |  |  |             content: '', | 
					
						
							|  |  |  |             type: 'text', | 
					
						
							| 
									
										
										
										
											2020-12-21 23:00:39 +01:00
										 |  |  |             isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable() | 
					
						
							| 
									
										
										
										
											2019-11-21 21:12:07 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |         await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							| 
									
										
										
										
											2019-12-20 20:13:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 23:47:24 +02:00
										 |  |  |         await appContext.tabManager.openTabWithNoteWithHoisting(note.noteId, {activate: true}); | 
					
						
							| 
									
										
										
										
											2019-12-28 10:28:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 23:34:51 +02:00
										 |  |  |         appContext.triggerEvent('focusAndSelectTitle', {isNewNote: true}); | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-23 23:06:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-08 22:19:16 +01:00
										 |  |  |     async toggleNoteHoistingCommand({noteId = appContext.tabManager.getActiveContextNoteId()}) { | 
					
						
							|  |  |  |         const noteToHoist = await froca.getNote(noteId); | 
					
						
							|  |  |  |         const activeNoteContext = appContext.tabManager.getActiveContext(); | 
					
						
							| 
									
										
										
										
											2019-11-23 23:06:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-08 22:19:16 +01:00
										 |  |  |         if (noteToHoist.noteId === activeNoteContext.hoistedNoteId) { | 
					
						
							|  |  |  |             await activeNoteContext.unhoist(); | 
					
						
							| 
									
										
										
										
											2020-02-10 20:57:56 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-08 22:19:16 +01:00
										 |  |  |         else if (noteToHoist.type !== 'search') { | 
					
						
							|  |  |  |             await activeNoteContext.setHoistedNoteId(noteId); | 
					
						
							| 
									
										
										
										
											2020-02-10 20:57:56 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-19 23:02:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 22:32:31 +01:00
										 |  |  |     async hoistNoteCommand({noteId}) { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +02:00
										 |  |  |         const noteContext = appContext.tabManager.getActiveContext(); | 
					
						
							| 
									
										
										
										
											2020-11-29 22:32:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (noteContext.hoistedNoteId !== noteId) { | 
					
						
							|  |  |  |             await noteContext.setHoistedNoteId(noteId); | 
					
						
							| 
									
										
										
										
											2020-11-29 22:32:31 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 23:41:55 +02:00
										 |  |  |     async unhoistCommand() { | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +02:00
										 |  |  |         const activeNoteContext = appContext.tabManager.getActiveContext(); | 
					
						
							| 
									
										
										
										
											2020-11-23 22:52:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:26:45 +02:00
										 |  |  |         if (activeNoteContext) { | 
					
						
							|  |  |  |             activeNoteContext.unhoist(); | 
					
						
							| 
									
										
										
										
											2020-11-23 22:52:48 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-13 23:41:55 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     copyWithoutFormattingCommand() { | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |         utils.copySelectionToClipboard(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     toggleFullscreenCommand() { | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  |         if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2021-11-16 22:43:08 +01:00
										 |  |  |             const win = utils.dynamicRequire('@electron/remote').getCurrentWindow(); | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (win.isFullScreenable()) { | 
					
						
							|  |  |  |                 win.setFullScreen(!win.isFullScreen()); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-12-14 10:29:14 +01:00
										 |  |  |         } // outside of electron this is handled by the browser
 | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     reloadFrontendAppCommand() { | 
					
						
							| 
									
										
										
										
											2021-08-24 22:59:51 +02:00
										 |  |  |         utils.reloadFrontendApp(); | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     logoutCommand() { | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  |         const $logoutForm = $('<form action="logout" method="POST">') | 
					
						
							| 
									
										
										
										
											2022-12-21 16:11:00 +01:00
										 |  |  |             .append($(`<input type='_hidden' name="_csrf" value="${glob.csrfToken}"/>`)); | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $("body").append($logoutForm); | 
					
						
							|  |  |  |         $logoutForm.trigger('submit'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |     backInNoteHistoryCommand() { | 
					
						
							| 
									
										
										
										
											2020-03-17 21:15:57 +01:00
										 |  |  |         if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2020-03-17 21:39:26 +01:00
										 |  |  |             // standard JS version does not work completely correctly in electron
 | 
					
						
							| 
									
										
										
										
											2021-11-16 22:43:08 +01:00
										 |  |  |             const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents(); | 
					
						
							| 
									
										
										
										
											2020-03-17 21:15:57 +01:00
										 |  |  |             const activeIndex = parseInt(webContents.getActiveIndex()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             webContents.goToIndex(activeIndex - 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             window.history.back(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 17:17:18 +01:00
										 |  |  |     forwardInNoteHistoryCommand() { | 
					
						
							| 
									
										
										
										
											2020-03-17 21:15:57 +01:00
										 |  |  |         if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2020-03-17 21:39:26 +01:00
										 |  |  |             // standard JS version does not work completely correctly in electron
 | 
					
						
							| 
									
										
										
										
											2021-11-16 22:43:08 +01:00
										 |  |  |             const webContents = utils.dynamicRequire('@electron/remote').getCurrentWebContents(); | 
					
						
							| 
									
										
										
										
											2020-03-17 21:15:57 +01:00
										 |  |  |             const activeIndex = parseInt(webContents.getActiveIndex()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             webContents.goToIndex(activeIndex + 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             window.history.forward(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-28 00:11:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-01 11:53:02 +01:00
										 |  |  |     async switchToDesktopVersionCommand() { | 
					
						
							|  |  |  |         utils.setCookie('trilium-device', 'desktop'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |         utils.reloadFrontendApp("Switching to desktop version"); | 
					
						
							| 
									
										
										
										
											2020-03-01 11:53:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-31 16:56:23 +01:00
										 |  |  |     async switchToMobileVersionCommand() { | 
					
						
							|  |  |  |         utils.setCookie('trilium-device', 'mobile'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         utils.reloadFrontendApp("Switching to mobile version"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 23:47:24 +02:00
										 |  |  |     async openInWindowCommand({notePath, hoistedNoteId, viewScope}) { | 
					
						
							| 
									
										
										
										
											2021-02-07 21:27:09 +01:00
										 |  |  |         if (!hoistedNoteId) { | 
					
						
							|  |  |  |             hoistedNoteId = 'root'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-05 19:30:03 +02:00
										 |  |  |         if (utils.isElectron()) { | 
					
						
							|  |  |  |             const {ipcRenderer} = utils.dynamicRequire('electron'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 23:47:24 +02:00
										 |  |  |             ipcRenderer.send('create-extra-window', {notePath, hoistedNoteId, viewScope}); | 
					
						
							| 
									
										
										
										
											2020-05-05 19:30:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2023-04-03 23:47:24 +02:00
										 |  |  |             const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}?extraWindow=1&extraHoistedNoteId=${hoistedNoteId}&extraViewScope=${JSON.stringify(viewScope)}#${notePath}`; | 
					
						
							| 
									
										
										
										
											2020-05-05 19:30:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             window.open(url, '', 'width=1000,height=800'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async openNewWindowCommand() { | 
					
						
							| 
									
										
										
										
											2021-02-07 21:27:09 +01:00
										 |  |  |         this.openInWindowCommand({notePath: '', hoistedNoteId: 'root'}); | 
					
						
							| 
									
										
										
										
											2020-05-05 19:30:03 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-05 23:58:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async runActiveNoteCommand() { | 
					
						
							| 
									
										
										
										
											2021-07-27 22:08:41 +02:00
										 |  |  |         const {ntxId, note} = appContext.tabManager.getActiveContext(); | 
					
						
							| 
									
										
										
										
											2020-05-05 23:58:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // ctrl+enter is also used elsewhere so make sure we're running only when appropriate
 | 
					
						
							|  |  |  |         if (!note || note.type !== 'code') { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 21:23:10 +02:00
										 |  |  |         // TODO: use note.executeScript()
 | 
					
						
							| 
									
										
										
										
											2020-05-05 23:58:52 +02:00
										 |  |  |         if (note.mime.endsWith("env=frontend")) { | 
					
						
							|  |  |  |             await bundleService.getAndExecuteBundle(note.noteId); | 
					
						
							| 
									
										
										
										
											2020-12-29 22:27:31 +01:00
										 |  |  |         } else if (note.mime.endsWith("env=backend")) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             await server.post(`script/run/${note.noteId}`); | 
					
						
							| 
									
										
										
										
											2020-12-29 22:27:31 +01:00
										 |  |  |         } else if (note.mime === 'text/x-sqlite;schema=trilium') { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             const resp = await server.post(`sql/execute/${note.noteId}`); | 
					
						
							| 
									
										
										
										
											2020-12-29 22:27:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 13:22:13 +01:00
										 |  |  |             if (!resp.success) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |                 toastService.showError(`Error occurred while executing SQL query: ${resp.message}`); | 
					
						
							| 
									
										
										
										
											2021-12-21 13:22:13 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await appContext.triggerEvent('sqlQueryResults', {ntxId: ntxId, results: resp.results}); | 
					
						
							| 
									
										
										
										
											2020-05-05 23:58:52 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         toastService.showMessage("Note executed"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-05 21:51:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 16:57:46 +01:00
										 |  |  |     hideAllPopups() { | 
					
						
							| 
									
										
										
										
											2020-09-05 21:51:00 +02:00
										 |  |  |         $(".tooltip").removeClass("show"); | 
					
						
							| 
									
										
										
										
											2022-12-13 16:57:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (utils.isDesktop()) { | 
					
						
							|  |  |  |             $(".aa-input").autocomplete("close"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-05 21:51:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +02:00
										 |  |  |     noteSwitchedEvent() { | 
					
						
							| 
									
										
										
										
											2022-12-13 16:57:46 +01:00
										 |  |  |         this.hideAllPopups(); | 
					
						
							| 
									
										
										
										
											2020-09-05 21:51:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 17:58:46 +02:00
										 |  |  |     activeContextChangedEvent() { | 
					
						
							| 
									
										
										
										
											2022-12-13 16:57:46 +01:00
										 |  |  |         this.hideAllPopups(); | 
					
						
							| 
									
										
										
										
											2020-09-05 21:51:00 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-08 22:36:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async forceSaveNoteRevisionCommand() { | 
					
						
							|  |  |  |         const noteId = appContext.tabManager.getActiveContextNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await server.post(`notes/${noteId}/revision`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         toastService.showMessage("Note revision has been created."); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-22 19:41:19 +01:00
										 |  |  | } |