| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							|  |  |  | import server from './server.js'; | 
					
						
							|  |  |  | import utils from './utils.js'; | 
					
						
							|  |  |  | import infoService from './info.js'; | 
					
						
							|  |  |  | import linkService from './link.js'; | 
					
						
							|  |  |  | import treeCache from './tree_cache.js'; | 
					
						
							| 
									
										
										
										
											2018-09-03 16:05:28 +02:00
										 |  |  | import noteDetailService from './note_detail.js'; | 
					
						
							| 
									
										
										
										
											2018-10-07 14:25:17 +02:00
										 |  |  | import noteTypeService from './note_type.js'; | 
					
						
							| 
									
										
										
										
											2018-12-22 20:57:09 +01:00
										 |  |  | import noteTooltipService from './note_tooltip.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 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function FrontendScriptApi(startNote, currentNote, originEntity = null) { | 
					
						
							|  |  |  |     const $pluginButtons = $("#plugin-buttons"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Activates note in the tree and in the note detail. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.activateNote = treeService.activateNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Activates newly created note. Compared to this.activateNote() also refreshes tree. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {string} notePath (or noteId) | 
					
						
							|  |  |  |      * @return {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.activateNewNote = async notePath => { | 
					
						
							|  |  |  |         await treeService.reload(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await treeService.activateNote(notePath, true); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 15:33:19 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @typedef {Object} ToolbarButtonOptions | 
					
						
							|  |  |  |      * @property {string} title | 
					
						
							| 
									
										
										
										
											2018-11-09 14:49:08 +01:00
										 |  |  |      * @property {string} [icon] - name of the JAM icon to be used (e.g. "clock" for "jam-clock" 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") | 
					
						
							| 
									
										
										
										
											2018-11-09 14:49:08 +01:00
										 |  |  |             .click(opts.action); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (opts.icon) { | 
					
						
							|  |  |  |             button.append($("<span>").addClass("jam jam-" + opts.icon)) | 
					
						
							|  |  |  |                   .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) { | 
					
						
							|  |  |  |             $(document).bind('keydown', opts.shortcut, opts.action); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Executes given anonymous function on the server. | 
					
						
							|  |  |  |      * 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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.runOnServer = async (script, params = []) => { | 
					
						
							|  |  |  |         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 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ret.success) { | 
					
						
							|  |  |  |             return ret.executionResult; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             throw new Error("server error: " + ret.error); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.showMessage = infoService.showMessage; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show error message to the user. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {string} message | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.showError = infoService.showError; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Refresh tree | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @returns {Promise<void>} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.refreshTree = treeService.reload; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 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
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @returns {string} content of currently loaded note in the editor (HTML, code etc.) | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getCurrentNoteContent = noteDetailService.getCurrentNoteContent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-29 08:40:35 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * This method checks whether user navigated away from the note from which the scripts has been started. | 
					
						
							|  |  |  |      * This is necessary because script execution is async and by the time it is finished, the user might have | 
					
						
							|  |  |  |      * already navigated away from this page - the end result would be that script might return data for the wrong | 
					
						
							|  |  |  |      * note. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @return {boolean} returns true if the original note is still loaded, false if user switched to another | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.isNoteStillLoaded = () => { | 
					
						
							|  |  |  |         return this.originEntity.noteId === noteDetailService.getCurrentNoteId(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-03 16:05:28 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							| 
									
										
										
										
											2018-09-03 20:14:45 +02:00
										 |  |  |      * @param {function} func - callback called on note change as user is typing (not necessarily tied to save event) | 
					
						
							| 
									
										
										
										
											2018-09-03 16:05:28 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     this.onNoteChange = noteDetailService.onNoteChange; | 
					
						
							| 
									
										
										
										
											2018-10-07 14:25:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @returns {array} list of default code mime types | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getDefaultCodeMimeTypes = noteTypeService.getDefaultCodeMimeTypes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @returns {array} list of currently used code mime types | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.getCodeMimeTypes = noteTypeService.getCodeMimeTypes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {array} types - list of mime types to be used | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.setCodeMimeTypes = noteTypeService.setCodeMimeTypes; | 
					
						
							| 
									
										
										
										
											2018-12-22 20:57:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @method | 
					
						
							|  |  |  |      * @param {object} $el - jquery object on which to setup the tooltip | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     this.setupElementTooltip = noteTooltipService.setupElementTooltip | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default FrontendScriptApi; |