| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | <!DOCTYPE html> | 
					
						
							|  |  |  | <html lang="en"> | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  |     <meta charset="utf-8"> | 
					
						
							|  |  |  |     <title>JSDoc: Source: services/frontend_script_api.js</title> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <script src="scripts/prettify/prettify.js"> </script> | 
					
						
							|  |  |  |     <script src="scripts/prettify/lang-css.js"> </script> | 
					
						
							|  |  |  |     <!--[if lt IE 9]>
 | 
					
						
							|  |  |  |       <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> | 
					
						
							|  |  |  |     <![endif]--> | 
					
						
							|  |  |  |     <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> | 
					
						
							|  |  |  |     <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> | 
					
						
							|  |  |  | </head> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <div id="main"> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <h1 class="page-title">Source: services/frontend_script_api.js</h1> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     <section> | 
					
						
							|  |  |  |         <article> | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |             <pre class="prettyprint source linenums"><code>import server from './server.js'; | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | import utils from './utils.js'; | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  | import toastService from './toast.js'; | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | import linkService from './link.js'; | 
					
						
							| 
									
										
										
										
											2021-04-16 23:01:56 +02:00
										 |  |  | import froca from './froca.js'; | 
					
						
							| 
									
										
										
										
											2018-12-22 22:28:49 +01:00
										 |  |  | import noteTooltipService from './note_tooltip.js'; | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  | import protectedSessionService from './protected_session.js'; | 
					
						
							|  |  |  | import dateNotesService from './date_notes.js'; | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  | import searchService from './search.js'; | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  | import CollapsibleWidget from '../widgets/collapsible_widget.js'; | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  | import ws from "./ws.js"; | 
					
						
							| 
									
										
										
										
											2020-02-02 11:44:08 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  | import NoteContextAwareWidget from "../widgets/note_context_aware_widget.js"; | 
					
						
							|  |  |  | import NoteContextCachingWidget from "../widgets/note_context_caching_widget.js"; | 
					
						
							| 
									
										
										
										
											2020-04-07 19:19:20 +02:00
										 |  |  | import BasicWidget from "../widgets/basic_widget.js"; | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This is the main frontend API interface for scripts. It's published in the local "api" object. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @constructor | 
					
						
							|  |  |  |  * @hideconstructor | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  | function FrontendScriptApi(startNote, currentNote, originEntity = null, $container = null) { | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |     const $pluginButtons = $("#plugin-buttons"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 21:35:26 +02:00
										 |  |  |     /** @property {jQuery} container of all the rendered script content */ | 
					
						
							|  |  |  |     this.$container = $container; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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:24:48 +02:00
										 |  |  |     // to keep consistency with backend API | 
					
						
							|  |  |  |     this.dayjs = dayjs; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     /** @property {CollapsibleWidget} */ | 
					
						
							|  |  |  |     this.CollapsibleWidget = CollapsibleWidget; | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @property {NoteContextAwareWidget} | 
					
						
							|  |  |  |      * @deprecated use NoteContextAwareWidget instead | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.TabAwareWidget = NoteContextAwareWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @property {NoteContextAwareWidget} */ | 
					
						
							|  |  |  |     this.NoteContextAwareWidget = NoteContextAwareWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @property {NoteContextCachingWidget} | 
					
						
							|  |  |  |      * @deprecated use NoteContextCachingWidget instead | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.TabCachingWidget = NoteContextCachingWidget; | 
					
						
							| 
									
										
										
										
											2020-04-07 19:19:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |     /** @property {NoteContextAwareWidget} */ | 
					
						
							|  |  |  |     this.NoteContextCachingWidget = NoteContextCachingWidget; | 
					
						
							| 
									
										
										
										
											2020-04-07 19:19:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** @property {BasicWidget} */ | 
					
						
							|  |  |  |     this.BasicWidget = BasicWidget; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Activates note in the tree and in the note detail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     this.activateNote = async notePath => { | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         await appContext.tabManager.getActiveContext().setNote(notePath); | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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-29 20:44:35 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @return {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.activateNewNote = async notePath => { | 
					
						
							| 
									
										
										
										
											2020-09-30 22:48:30 +02:00
										 |  |  |         await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         await appContext.tabManager.getActiveContext().setNote(notePath); | 
					
						
							| 
									
										
										
										
											2020-03-19 09:18:36 +01:00
										 |  |  |         appContext.triggerEvent('focusAndSelectTitle'); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Open a note in a new tab. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @param {boolean} activate - set to true to activate the new tab, false to stay on the current tab | 
					
						
							|  |  |  |      * @return {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.openTabWithNote = async (notePath, activate) => { | 
					
						
							|  |  |  |         await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         await appContext.tabManager.openContextWithNote(notePath, activate); | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (activate) { | 
					
						
							|  |  |  |             appContext.triggerEvent('focusAndSelectTitle'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @typedef {Object} ToolbarButtonOptions | 
					
						
							|  |  |  |      * @property {string} title | 
					
						
							| 
									
										
										
										
											2019-11-08 22:34:30 +01:00
										 |  |  |      * @property {string} [icon] - name of the boxicon to be used (e.g. "time" for "bx-time" icon) | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |      * @property {function} action - callback handling the click on the button | 
					
						
							|  |  |  |      * @property {string} [shortcut] - keyboard shortcut for the button, e.g. "alt+t" | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Adds new button the the plugin area. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {ToolbarButtonOptions} opts | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.addButtonToToolbar = opts => { | 
					
						
							| 
									
										
										
										
											2020-09-30 22:48:30 +02:00
										 |  |  |         const buttonId = "toolbar-button-" + opts.title.replace(/\s/g, "-"); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  |         let button; | 
					
						
							|  |  |  |         if (utils.isMobile()) { | 
					
						
							|  |  |  |             $('#plugin-buttons-placeholder').remove(); | 
					
						
							|  |  |  |             button = $('<a class="dropdown-item" href="#">') | 
					
						
							|  |  |  |                 .on('click', () => { | 
					
						
							|  |  |  |                     setTimeout(() => $pluginButtons.dropdown('hide'), 0); | 
					
						
							|  |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (opts.icon) { | 
					
						
							|  |  |  |                 button.append($("<span>").addClass("bx bx-" + opts.icon)) | 
					
						
							|  |  |  |                     .append("&nbsp;"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             button.append($("<span>").text(opts.title)); | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |             button = $('<span class="button-widget icon-action bx" data-toggle="tooltip" title="" data-placement="right"></span>') | 
					
						
							|  |  |  |                 .addClass("bx bx-" + (opts.icon || "question-mark")); | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |             button.attr("title", opts.title); | 
					
						
							|  |  |  |             button.tooltip({html: true}); | 
					
						
							| 
									
										
										
										
											2018-11-13 12:50:08 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         button = button.on('click', opts.action); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         button.attr('id', buttonId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($("#" + buttonId).replaceWith(button).length === 0) { | 
					
						
							|  |  |  |             $pluginButtons.append(button); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (opts.shortcut) { | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  |             utils.bindGlobalShortcut(opts.shortcut, opts.action); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-01-01 22:03:27 +01:00
										 |  |  |      * Executes given anonymous function on the backend. | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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 | 
					
						
							|  |  |  |      * @param {Array.<?>} params - list of parameters to the anonymous function to be send to backend | 
					
						
							|  |  |  |      * @return {Promise<*>} return value of the executed function on the backend | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-01-01 22:03:27 +01:00
										 |  |  |     this.runOnBackend = async (script, params = []) => { | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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-02-02 11:44:08 +01:00
										 |  |  |         }, "script"); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (ret.success) { | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  |             await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |             return ret.executionResult; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             throw new Error("server error: " + ret.error); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-01 22:03:27 +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.: | 
					
						
							| 
									
										
										
										
											2020-09-30 22:48:30 +02:00
										 |  |  |      * "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search | 
					
						
							| 
									
										
										
										
											2019-04-22 22:52:09 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} searchString | 
					
						
							|  |  |  |      * @returns {Promise<NoteShort[]>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.searchForNotes = async searchString => { | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  |         return await searchService.searchForNotes(searchString); | 
					
						
							| 
									
										
										
										
											2019-04-22 22:52:09 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This is a powerful search method - you can search by attributes and their values, e.g.: | 
					
						
							| 
									
										
										
										
											2020-09-30 22:48:30 +02:00
										 |  |  |      * "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search | 
					
						
							| 
									
										
										
										
											2019-04-22 22:52:09 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @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-14 12:24:48 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns note by given noteId. If note is missing from cache, it's loaded. | 
					
						
							|  |  |  |      ** | 
					
						
							|  |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |     this.getNote = async noteId => await froca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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()) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string[]} noteIds | 
					
						
							|  |  |  |      * @param {boolean} [silentNotFoundError] - don't report error if the note is not found | 
					
						
							|  |  |  |      * @return {Promise<NoteShort[]>} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |     this.getNotes = async (noteIds, silentNotFoundError = false) => await froca.getNotes(noteIds, silentNotFoundError); | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-11-05 21:26:54 +01:00
										 |  |  |      * Update frontend tree (note) cache from the backend. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-10-26 10:00:26 +02:00
										 |  |  |      * @param {string[]} noteIds | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |     this.reloadNotes = async noteIds => await froca.reloadNotes(noteIds); | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +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-29 20:44:35 +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-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-04-07 19:19:20 +02:00
										 |  |  |      * @deprecated - this is now no-op since all the changes should be gracefully handled per widget | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-07 19:19:20 +02:00
										 |  |  |     this.refreshTree = () => {}; | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create note link (jQuery object) for given note. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |      * @param {object} [params] | 
					
						
							|  |  |  |      * @param {boolean} [params.showTooltip=true] - enable/disable tooltip on the link | 
					
						
							|  |  |  |      * @param {boolean} [params.showNotePath=false] - show also whole note's path as part of the link | 
					
						
							| 
									
										
										
										
											2022-01-05 20:31:21 +01:00
										 |  |  |      * @param {boolean} [params.showNoteIcon=false] - show also note icon before the title | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |      * @param {string} [title=] - custom link tile with note's title as default | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-02-02 11:44:08 +01:00
										 |  |  |      * @param {string} text - this must be clear text, HTML is not supported. | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     this.addTextToActiveTabEditor = text => appContext.triggerCommand('addTextToActiveEditor', {text}); | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-12 21:42:27 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-02-02 11:44:08 +01:00
										 |  |  |      * @returns {NoteShort} active note (loaded into right pane) | 
					
						
							| 
									
										
										
										
											2019-03-12 21:42:27 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |     this.getActiveTabNote = () => appContext.tabManager.getActiveContextNote(); | 
					
						
							| 
									
										
										
										
											2019-03-12 21:42:27 +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 11:44:08 +01:00
										 |  |  |      * @param callback - method receiving "textEditor" instance | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     this.getActiveTabTextEditor = callback => appContext.triggerCommand('executeInActiveEditor', {callback}); | 
					
						
							| 
									
										
										
										
											2020-01-10 20:10:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2019-05-21 21:47:28 +02:00
										 |  |  |      * @returns {Promise<string|null>} returns note path of active note or null if there isn't active note | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |     this.getActiveTabNotePath = () => appContext.tabManager.getActiveContextNotePath(); | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 22:24:51 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |      * @param {object} $el - jquery object on which to setup the tooltip | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.setupElementTooltip = noteTooltipService.setupElementTooltip; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @deprecated use protectNote and protectSubtree instead | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2019-02-20 22:24:51 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     this.protectActiveNote = async () => { | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         const activeNote = appContext.tabManager.getActiveContextNote(); | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await protectedSessionService.protectNote(activeNote.noteId, true, false); | 
					
						
							| 
									
										
										
										
											2019-02-20 22:24:51 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-22 22:28:49 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {boolean} protect - true to protect note, false to unprotect | 
					
						
							| 
									
										
										
										
											2018-12-22 22:28:49 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     this.protectNote = async (noteId, protect) => { | 
					
						
							|  |  |  |         await protectedSessionService.protectNote(noteId, protect, false); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-02-20 22:24:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |      * @param {string} noteId | 
					
						
							|  |  |  |      * @param {boolean} protect - true to protect subtree, false to unprotect | 
					
						
							| 
									
										
										
										
											2019-02-20 22:24:51 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-03-08 11:41:42 +01:00
										 |  |  |     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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-10 19:54:38 +01:00
										 |  |  |      * Returns day note for a given date. If it doesn't exist, it is automatically created. | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} date - e.g. "2019-04-29" | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							| 
									
										
										
										
											2022-01-10 19:54:38 +01:00
										 |  |  |      * @deprecated use getDayNote instead | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-01-10 19:54:38 +01:00
										 |  |  |     this.getDateNote = dateNotesService.getDayNote; | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 21:30:54 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-01-10 19:54:38 +01:00
										 |  |  |      * Returns day note for a given date. If it doesn't exist, it is automatically created. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} date - e.g. "2019-04-29" | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getDayNote = dateNotesService.getDayNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns day note for the first date of the week of the given date. If it doesn't exist, it is automatically created. | 
					
						
							| 
									
										
										
										
											2021-11-10 21:30:54 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} date - e.g. "2019-04-29" | 
					
						
							|  |  |  |      * @return {Promise<NoteShort>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |      this.getWeekNote = dateNotesService.getWeekNote; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 12:24:48 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 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
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |      * Hoist note in the current tab. See https://github.com/zadam/trilium/wiki/Note-hoisting | 
					
						
							| 
									
										
										
										
											2019-10-21 21:22:53 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} noteId - set hoisted note. 'root' will effectively unhoist | 
					
						
							|  |  |  |      * @return {Promise} | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |     this.setHoistedNoteId = (noteId) => { | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         const activeNoteContext = appContext.tabManager.getActiveContext(); | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 12:26:13 +02:00
										 |  |  |         if (activeNoteContext) { | 
					
						
							|  |  |  |             activeNoteContext.setHoistedNoteId(noteId); | 
					
						
							| 
									
										
										
										
											2021-02-17 23:55:51 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											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-10 23:04:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-01-01 22:03:27 +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-10 23:04:18 +01:00
										 |  |  |      * @method | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-09-30 22:48:30 +02:00
										 |  |  |     this.waitUntilSynced = ws.waitForMaxKnownEntityChangeId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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}); | 
					
						
							| 
									
										
										
										
											2021-04-07 22:01:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Return randomly generated string of given length. This random string generation is NOT cryptographically secure. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {number} length of the string | 
					
						
							|  |  |  |      * @returns {string} random string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.randomString = utils.randomString; | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-30 22:48:30 +02:00
										 |  |  | export default FrontendScriptApi; | 
					
						
							|  |  |  | </code></pre> | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  |         </article> | 
					
						
							|  |  |  |     </section> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <nav> | 
					
						
							| 
									
										
										
										
											2021-11-10 21:30:54 +01:00
										 |  |  |     <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Attribute.html">Attribute</a></li><li><a href="Branch.html">Branch</a></li><li><a href="FrontendScriptApi.html">FrontendScriptApi</a></li><li><a href="NoteComplement.html">NoteComplement</a></li><li><a href="NoteShort.html">NoteShort</a></li></ul><h3>Global</h3><ul><li><a href="global.html#doRenderBody">doRenderBody</a></li></ul> | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | </nav> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <br class="clear"> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <footer> | 
					
						
							| 
									
										
										
										
											2022-04-16 00:17:32 +02:00
										 |  |  |     Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.10</a> | 
					
						
							| 
									
										
										
										
											2018-08-29 20:44:35 +02:00
										 |  |  | </footer> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script> prettyPrint(); </script> | 
					
						
							|  |  |  | <script src="scripts/linenumber.js"> </script> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | </html> |