| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							|  |  |  | import server from './server.js'; | 
					
						
							|  |  |  | import utils from './utils.js'; | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  | import toastService from './toast.js'; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | import linkService from './link.js'; | 
					
						
							|  |  |  | import treeCache from './tree_cache.js'; | 
					
						
							| 
									
										
										
										
											2018-12-22 20:57:09 +01:00
										 |  |  | import noteTooltipService from './note_tooltip.js'; | 
					
						
							| 
									
										
										
										
											2019-08-17 11:28:36 +02:00
										 |  |  | import protectedSessionService from './protected_session.js'; | 
					
						
							|  |  |  | import dateNotesService from './date_notes.js'; | 
					
						
							| 
									
										
										
										
											2020-02-02 20:02:08 +01:00
										 |  |  | import CollapsibleWidget from '../widgets/collapsible_widget.js'; | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  | import ws from "./ws.js"; | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  | import hoistedNoteService from "./hoisted_note.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2020-03-19 17:15:14 +01:00
										 |  |  | import TabAwareWidget from "../widgets/tab_aware_widget.js"; | 
					
						
							|  |  |  | import TabCachingWidget from "../widgets/tab_caching_widget.js"; | 
					
						
							|  |  |  | import BasicWidget from "../widgets/basic_widget.js"; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |  * This is the main frontend API interface for scripts. It's published in the local "api" object. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |  * @constructor | 
					
						
							|  |  |  |  * @hideconstructor | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-02-25 19:19:10 +01:00
										 |  |  | function FrontendScriptApi(startNote, currentNote, originEntity = null, $container = null) { | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     const $pluginButtons = $("#plugin-buttons"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:55:29 +02:00
										 |  |  |     /** @property {jQuery} container of all the rendered script content */ | 
					
						
							|  |  |  |     this.$container = $container; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** @property {object} note where script started executing */ | 
					
						
							|  |  |  |     this.startNote = startNote; | 
					
						
							|  |  |  |     /** @property {object} note where script is currently executing */ | 
					
						
							|  |  |  |     this.currentNote = currentNote; | 
					
						
							|  |  |  |     /** @property {object|null} entity whose event triggered this execution */ | 
					
						
							|  |  |  |     this.originEntity = originEntity; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  |     // to keep consistency with backend API
 | 
					
						
							|  |  |  |     this.dayjs = dayjs; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 18:46:50 +01:00
										 |  |  |     /** @property {CollapsibleWidget} */ | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  |     this.CollapsibleWidget = CollapsibleWidget; | 
					
						
							| 
									
										
										
										
											2019-08-17 11:28:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 17:15:14 +01:00
										 |  |  |     /** @property {TabAwareWidget} */ | 
					
						
							|  |  |  |     this.TabAwareWidget = TabAwareWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @property {TabCachingWidget} */ | 
					
						
							|  |  |  |     this.TabCachingWidget = TabCachingWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @property {BasicWidget} */ | 
					
						
							|  |  |  |     this.BasicWidget = BasicWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Activates note in the tree and in the note detail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-02-02 22:04:28 +01:00
										 |  |  |     this.activateNote = async notePath => { | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  |         await appContext.tabManager.getActiveTabContext().setNote(notePath); | 
					
						
							| 
									
										
										
										
											2019-07-28 14:47:35 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-01-04 21:53:49 +01:00
										 |  |  |      * Activates newly created note. Compared to this.activateNote() also makes sure that frontend has been fully synced. | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @return {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.activateNewNote = async notePath => { | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |         await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  |         await appContext.tabManager.getActiveTabContext().setNote(notePath); | 
					
						
							| 
									
										
										
										
											2020-03-17 21:39:26 +01:00
										 |  |  |         appContext.triggerEvent('focusAndSelectTitle'); | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @typedef {Object} ToolbarButtonOptions | 
					
						
							|  |  |  |      * @property {string} title | 
					
						
							| 
									
										
										
										
											2019-11-02 12:17:00 +01:00
										 |  |  |      * @property {string} [icon] - name of the boxicon to be used (e.g. "time" for "bx-time" icon) | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @property {function} action - callback handling the click on the button | 
					
						
							|  |  |  |      * @property {string} [shortcut] - keyboard shortcut for the button, e.g. "alt+t" | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Adds new button the the plugin area. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @param {ToolbarButtonOptions} opts | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     this.addButtonToToolbar = opts => { | 
					
						
							|  |  |  |         const buttonId = "toolbar-button-" + opts.title.replace(/[^a-zA-Z0-9]/g, "-"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const button = $('<button>') | 
					
						
							| 
									
										
										
										
											2018-11-05 21:12:03 +01:00
										 |  |  |             .addClass("btn btn-sm") | 
					
						
							| 
									
										
										
										
											2019-11-09 17:39:48 +01:00
										 |  |  |             .on('click', opts.action); | 
					
						
							| 
									
										
										
										
											2018-11-09 14:49:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (opts.icon) { | 
					
						
							| 
									
										
										
										
											2019-11-02 12:17:00 +01:00
										 |  |  |             button.append($("<span>").addClass("bx bx-" + opts.icon)) | 
					
						
							| 
									
										
										
										
											2018-11-09 14:49:08 +01:00
										 |  |  |                   .append(" "); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         button.append($("<span>").text(opts.title)); | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         button.attr('id', buttonId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 21:08:02 +02:00
										 |  |  |         if ($("#" + buttonId).replaceWith(button).length === 0) { | 
					
						
							|  |  |  |             $pluginButtons.append(button); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (opts.shortcut) { | 
					
						
							| 
									
										
										
										
											2019-08-12 22:41:53 +02:00
										 |  |  |             utils.bindGlobalShortcut(opts.shortcut, opts.action); | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             button.attr("title", "Shortcut " + opts.shortcut); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function prepareParams(params) { | 
					
						
							|  |  |  |         if (!params) { | 
					
						
							|  |  |  |             return params; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return params.map(p => { | 
					
						
							|  |  |  |             if (typeof p === "function") { | 
					
						
							|  |  |  |                 return "!@#Function: " + p.toString(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 return p; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-12-28 12:55:53 +01:00
										 |  |  |      * Executes given anonymous function on the backend. | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      * Internally this serializes the anonymous function into string and sends it to backend via AJAX. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string} script - script to be executed on the backend | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @param {Array.<?>} params - list of parameters to the anonymous function to be send to backend | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      * @return {Promise<*>} return value of the executed function on the backend | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-12-28 12:55:53 +01:00
										 |  |  |     this.runOnBackend = async (script, params = []) => { | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |         if (typeof script === "function") { | 
					
						
							|  |  |  |             script = script.toString(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const ret = await server.post('script/exec', { | 
					
						
							|  |  |  |             script: script, | 
					
						
							|  |  |  |             params: prepareParams(params), | 
					
						
							|  |  |  |             startNoteId: startNote.noteId, | 
					
						
							|  |  |  |             currentNoteId: currentNote.noteId, | 
					
						
							|  |  |  |             originEntityName: "notes", // currently there's no other entity on frontend which can trigger event
 | 
					
						
							|  |  |  |             originEntityId: originEntity ? originEntity.noteId : null | 
					
						
							| 
									
										
										
										
											2020-01-28 21:54:28 +01:00
										 |  |  |         }, "script"); | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (ret.success) { | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  |             // wait until all the changes done in the script has been synced to frontend before continuing
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |             await ws.waitForEntityChangeId(ret.maxEntityChangeId); | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |             return ret.executionResult; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             throw new Error("server error: " + ret.error); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 12:55:53 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @deprecated new name of this API call is runOnBackend so use that | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.runOnServer = this.runOnBackend; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 22:52:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * This is a powerful search method - you can search by attributes and their values, e.g.: | 
					
						
							|  |  |  |      * "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} searchString | 
					
						
							|  |  |  |      * @returns {Promise<NoteShort[]>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.searchForNotes = async searchString => { | 
					
						
							| 
									
										
										
										
											2020-08-18 23:32:50 +02:00
										 |  |  |         const noteIds = await this.runOnBackend( | 
					
						
							|  |  |  |             searchString => api.searchForNotes(searchString).map(note => note.noteId), | 
					
						
							|  |  |  |             [searchString]); | 
					
						
							| 
									
										
										
										
											2019-04-22 22:52:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return await treeCache.getNotes(noteIds); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This is a powerful search method - you can search by attributes and their values, e.g.: | 
					
						
							|  |  |  |      * "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} searchString | 
					
						
							|  |  |  |      * @returns {Promise<NoteShort|null>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.searchForNote = async searchString => { | 
					
						
							|  |  |  |         const notes = await this.searchForNotes(searchString); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return notes.length > 0 ? notes[0] : null; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 21:13:47 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns note by given noteId. If note is missing from cache, it's loaded. | 
					
						
							|  |  |  |      ** | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getNote = async noteId => await treeCache.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns list of notes. If note is missing from cache, it's loaded. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This is often used to bulk-fill the cache with notes which would have to be picked one by one | 
					
						
							|  |  |  |      * otherwise (by e.g. createNoteLink()) | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @param {string[]} noteIds | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      * @param {boolean} [silentNotFoundError] - don't report error if the note is not found | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |      * @return {Promise<NoteShort[]>} | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     this.getNotes = async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 22:56:45 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-10-26 10:00:26 +02:00
										 |  |  |      * Update frontend tree (note) cache from the backend. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string[]} noteIds | 
					
						
							| 
									
										
										
										
											2019-04-13 22:56:45 +02:00
										 |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-26 10:00:26 +02:00
										 |  |  |     this.reloadNotes = async noteIds => await treeCache.reloadNotes(noteIds); | 
					
						
							| 
									
										
										
										
											2019-04-13 22:56:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Instance name identifies particular Trilium instance. It can be useful for scripts | 
					
						
							|  |  |  |      * if some action needs to happen on only one specific instance. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {string} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getInstanceName = () => window.glob.instanceName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {Date} date | 
					
						
							|  |  |  |      * @returns {string} date in YYYY-MM-DD format | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.formatDateISO = utils.formatDateISO; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} str | 
					
						
							|  |  |  |      * @returns {Date} parsed object | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.parseDate = utils.parseDate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show info message to the user. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} message | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  |     this.showMessage = toastService.showMessage; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show error message to the user. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} message | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  |     this.showError = toastService.showError; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-04-03 19:34:22 +02:00
										 |  |  |      * @deprecated - this is now no-op since all the changes should be gracefully handled per widget | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-03 19:34:22 +02:00
										 |  |  |     this.refreshTree = () => {}; | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create note link (jQuery object) for given note. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @param {string} [noteTitle] - if not present we'll use note title | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.createNoteLink = linkService.createNoteLink; | 
					
						
							| 
									
										
										
										
											2018-09-03 16:05:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Adds given text to the editor cursor | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string} text - this must be clear text, HTML is not supported. | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-02-28 00:11:34 +01:00
										 |  |  |     this.addTextToActiveTabEditor = text => appContext.triggerCommand('addTextToActiveEditor', {text}); | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 16:05:28 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-03-12 21:39:35 +01:00
										 |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-02-01 11:15:58 +01:00
										 |  |  |      * @returns {NoteShort} active note (loaded into right pane) | 
					
						
							| 
									
										
										
										
											2019-03-12 21:39:35 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |     this.getActiveTabNote = () => appContext.tabManager.getActiveTabNote(); | 
					
						
							| 
									
										
										
										
											2019-03-12 21:39:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * See https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html for a documentation on the returned instance.
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-02-02 10:41:43 +01:00
										 |  |  |      * @param callback - method receiving "textEditor" instance | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-02-28 00:11:34 +01:00
										 |  |  |     this.getActiveTabTextEditor = callback => appContext.triggerCommand('executeInActiveEditor', {callback}); | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 21:13:47 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |      * @returns {Promise<string|null>} returns note path of active note or null if there isn't active note | 
					
						
							| 
									
										
										
										
											2019-04-09 21:13:47 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-16 21:16:09 +01:00
										 |  |  |     this.getActiveTabNotePath = () => appContext.tabManager.getActiveTabNotePath(); | 
					
						
							| 
									
										
										
										
											2019-04-09 21:13:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-22 20:57:09 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {object} $el - jquery object on which to setup the tooltip | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-01-01 15:39:13 +01:00
										 |  |  |     this.setupElementTooltip = noteTooltipService.setupElementTooltip; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-02-26 16:37:17 +01:00
										 |  |  |      * @deprecated use protectNote and protectSubtree instead | 
					
						
							| 
									
										
										
										
											2019-01-01 15:39:13 +01:00
										 |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-02-26 16:37:17 +01:00
										 |  |  |     this.protectActiveNote = async () => { | 
					
						
							|  |  |  |         const activeNote = appContext.tabManager.getActiveTabNote(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await protectedSessionService.protectNote(activeNote.noteId, true, false); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {boolean} protect - true to protect note, false to unprotect | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.protectNote = async (noteId, protect) => { | 
					
						
							|  |  |  |         await protectedSessionService.protectNote(noteId, protect, false); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {boolean} protect - true to protect subtree, false to unprotect | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.protectSubTree = async (noteId, protect) => { | 
					
						
							|  |  |  |         await protectedSessionService.protectNote(noteId, protect, true); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns date-note for today. If it doesn't exist, it is automatically created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getTodayNote = dateNotesService.getTodayNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns date-note. If it doesn't exist, it is automatically created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} date - e.g. "2019-04-29" | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getDateNote = dateNotesService.getDateNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns month-note. If it doesn't exist, it is automatically created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} month - e.g. "2019-04" | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getMonthNote = dateNotesService.getMonthNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns year-note. If it doesn't exist, it is automatically created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} year - e.g. "2019" | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getYearNote = dateNotesService.getYearNote; | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Hoist note. See https://github.com/zadam/trilium/wiki/Note-hoisting
 | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId - set hoisted note. 'root' will effectively unhoist | 
					
						
							|  |  |  |      * @return {Promise} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.setHoistedNoteId = hoistedNoteService.setHoistedNoteId; | 
					
						
							| 
									
										
										
										
											2019-11-05 21:26:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} keyboardShortcut - e.g. "ctrl+shift+a" | 
					
						
							|  |  |  |      * @param {function} handler | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.bindGlobalShortcut = utils.bindGlobalShortcut; | 
					
						
							| 
									
										
										
										
											2019-12-09 23:07:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-12-10 23:04:18 +01:00
										 |  |  |      * Trilium runs in backend and frontend process, when something is changed on the backend from script, | 
					
						
							|  |  |  |      * frontend will get asynchronously synchronized. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This method returns a promise which resolves once all the backend -> frontend synchronization is finished. | 
					
						
							|  |  |  |      * Typical use case is when new note has been created, we should wait until it is synced into frontend and only then activate it. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-12-09 23:07:45 +01:00
										 |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |     this.waitUntilSynced = ws.waitForMaxKnownEntityChangeId; | 
					
						
							| 
									
										
										
										
											2020-06-14 10:49:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This will refresh all currently opened notes which have included note specified in the parameter | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param includedNoteId - noteId of the included note | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.refreshIncludedNote = includedNoteId => appContext.triggerEvent('refreshIncludedNote', {noteId: includedNoteId}); | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-14 10:49:37 +02:00
										 |  |  | export default FrontendScriptApi; |