2018-08-30 02:44:35 +08: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>
|
|
|
|
<pre class="prettyprint source linenums"><code>import treeService from './tree.js';
|
|
|
|
import server from './server.js';
|
|
|
|
import utils from './utils.js';
|
2019-10-20 16:00:18 +08:00
|
|
|
import toastService from './toast.js';
|
2018-08-30 02:44:35 +08:00
|
|
|
import linkService from './link.js';
|
|
|
|
import treeCache from './tree_cache.js';
|
2018-09-03 22:05:28 +08:00
|
|
|
import noteDetailService from './note_detail.js';
|
2018-12-23 05:28:49 +08:00
|
|
|
import noteTooltipService from './note_tooltip.js';
|
2019-08-21 03:40:47 +08:00
|
|
|
import protectedSessionService from './protected_session.js';
|
|
|
|
import dateNotesService from './date_notes.js';
|
|
|
|
import StandardWidget from '../widgets/standard_widget.js';
|
2019-10-22 03:22:53 +08:00
|
|
|
import ws from "./ws.js";
|
|
|
|
import hoistedNoteService from "./hoisted_note.js";
|
2019-11-19 06:01:31 +08:00
|
|
|
import KeyboardAction from "./keyboard_action.js";
|
2018-08-30 02:44:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the main frontend API interface for scripts. It's published in the local "api" object.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @hideconstructor
|
|
|
|
*/
|
2019-10-07 03:35:26 +08:00
|
|
|
function FrontendScriptApi(startNote, currentNote, originEntity = null, tabContext = null, $container = null) {
|
2018-08-30 02:44:35 +08:00
|
|
|
const $pluginButtons = $("#plugin-buttons");
|
|
|
|
|
2019-10-07 03:35:26 +08:00
|
|
|
/** @property {jQuery} container of all the rendered script content */
|
|
|
|
this.$container = $container;
|
|
|
|
|
2018-08-30 02:44:35 +08: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 18:24:48 +08:00
|
|
|
// to keep consistency with backend API
|
|
|
|
this.dayjs = dayjs;
|
|
|
|
|
2019-05-22 03:47:28 +08:00
|
|
|
/** @property {TabContext|null} - experimental! */
|
|
|
|
this.tabContext = tabContext;
|
|
|
|
|
2019-08-21 03:40:47 +08:00
|
|
|
/** @property {StandardWidget} */
|
|
|
|
this.StandardWidget = StandardWidget;
|
|
|
|
|
2019-11-19 06:01:31 +08:00
|
|
|
/** @property {KeyboardAction} */
|
|
|
|
this.keys = new KeyboardAction();
|
|
|
|
|
|
|
|
this.bind = keys.bind;
|
|
|
|
|
2018-08-30 02:44:35 +08:00
|
|
|
/**
|
|
|
|
* Activates note in the tree and in the note detail.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} notePath (or noteId)
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2019-08-21 03:40:47 +08:00
|
|
|
this.activateNote = async (notePath, noteLoadedListener) => {
|
|
|
|
await treeService.activateNote(notePath, async () => {
|
|
|
|
await treeService.scrollToActiveNote();
|
|
|
|
|
|
|
|
if (noteLoadedListener) {
|
|
|
|
noteLoadedListener();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-08-30 02:44:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
2019-02-21 05:24:51 +08:00
|
|
|
await treeService.activateNote(notePath, noteDetailService.focusAndSelectTitle);
|
2018-08-30 02:44:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ToolbarButtonOptions
|
|
|
|
* @property {string} title
|
2019-11-09 05:34:30 +08:00
|
|
|
* @property {string} [icon] - name of the boxicon to be used (e.g. "time" for "bx-time" icon)
|
2018-08-30 02:44:35 +08: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 => {
|
|
|
|
const buttonId = "toolbar-button-" + opts.title.replace(/[^a-zA-Z0-9]/g, "-");
|
|
|
|
|
|
|
|
const button = $('<button>')
|
2018-11-09 06:49:44 +08:00
|
|
|
.addClass("btn btn-sm")
|
2019-11-19 06:01:31 +08:00
|
|
|
.on('click', opts.action);
|
2018-11-13 19:50:08 +08:00
|
|
|
|
|
|
|
if (opts.icon) {
|
2019-11-09 05:34:30 +08:00
|
|
|
button.append($("<span>").addClass("bx bx-" + opts.icon))
|
2018-11-13 19:50:08 +08:00
|
|
|
.append("&nbsp;");
|
|
|
|
}
|
|
|
|
|
|
|
|
button.append($("<span>").text(opts.title));
|
2018-08-30 02:44:35 +08:00
|
|
|
|
|
|
|
button.attr('id', buttonId);
|
|
|
|
|
|
|
|
if ($("#" + buttonId).replaceWith(button).length === 0) {
|
|
|
|
$pluginButtons.append(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.shortcut) {
|
2019-08-21 03:40:47 +08:00
|
|
|
utils.bindGlobalShortcut(opts.shortcut, opts.action);
|
2018-08-30 02:44:35 +08: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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
2019-10-22 03:22:53 +08:00
|
|
|
}, {
|
|
|
|
'trilium-source-id': "script"
|
2018-08-30 02:44:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (ret.success) {
|
2019-10-22 03:22:53 +08:00
|
|
|
// wait until all the changes done in the script has been synced to frontend before continuing
|
|
|
|
await ws.waitForSyncId(ret.maxSyncId);
|
|
|
|
|
2018-08-30 02:44:35 +08:00
|
|
|
return ret.executionResult;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("server error: " + ret.error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-23 04:52:09 +08: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 => {
|
|
|
|
const noteIds = await this.runOnServer(async searchString => {
|
|
|
|
const notes = await api.searchForNotes(searchString);
|
|
|
|
|
|
|
|
return notes.map(note => note.noteId);
|
|
|
|
}, [searchString]);
|
|
|
|
|
|
|
|
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-14 18:24:48 +08: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-30 02:44:35 +08: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[]>}
|
|
|
|
*/
|
|
|
|
this.getNotes = async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError);
|
|
|
|
|
2019-04-14 18:24:48 +08:00
|
|
|
/**
|
2019-11-06 04:26:54 +08:00
|
|
|
* Update frontend tree (note) cache from the backend.
|
|
|
|
*
|
2019-10-26 16:00:26 +08:00
|
|
|
* @param {string[]} noteIds
|
2019-04-14 18:24:48 +08:00
|
|
|
* @method
|
|
|
|
*/
|
2019-10-26 16:00:26 +08:00
|
|
|
this.reloadNotes = async noteIds => await treeCache.reloadNotes(noteIds);
|
2019-04-14 18:24:48 +08:00
|
|
|
|
2018-08-30 02:44:35 +08: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 16:00:18 +08:00
|
|
|
this.showMessage = toastService.showMessage;
|
2018-08-30 02:44:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show error message to the user.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} message
|
|
|
|
*/
|
2019-10-20 16:00:18 +08:00
|
|
|
this.showError = toastService.showError;
|
2018-08-30 02:44:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh tree
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
this.refreshTree = treeService.reload;
|
|
|
|
|
2019-11-11 02:34:15 +08:00
|
|
|
/**
|
|
|
|
* Refresh active tab
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
this.refreshActiveTab = noteDetailService.reload;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh current tab
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
this.refreshAllTabs = noteDetailService.reloadAllTabs;
|
|
|
|
|
2018-08-30 02:44:35 +08: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 22:05:28 +08:00
|
|
|
|
2019-03-13 04:42:27 +08:00
|
|
|
/**
|
|
|
|
* @method
|
2019-03-13 04:52:19 +08:00
|
|
|
* @returns {NoteFull} active note (loaded into right pane)
|
2019-03-13 04:42:27 +08:00
|
|
|
*/
|
2019-10-20 18:29:34 +08:00
|
|
|
this.getActiveTabNote = noteDetailService.getActiveTabNote;
|
2019-03-13 04:42:27 +08:00
|
|
|
|
2019-04-14 18:24:48 +08:00
|
|
|
/**
|
|
|
|
* @method
|
2019-05-22 03:47:28 +08:00
|
|
|
* @returns {Promise<string|null>} returns note path of active note or null if there isn't active note
|
2019-04-14 18:24:48 +08:00
|
|
|
*/
|
2019-10-22 03:22:53 +08:00
|
|
|
this.getActiveTabNotePath = noteDetailService.getActiveTabNotePath;
|
2019-04-14 18:24:48 +08:00
|
|
|
|
2019-02-21 05:24:51 +08: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
|
|
|
|
*/
|
2019-03-13 04:52:19 +08:00
|
|
|
this.isNoteStillActive = () => {
|
2019-10-07 03:35:26 +08:00
|
|
|
return tabContext.note && this.originEntity.noteId === tabContext.note.noteId;
|
2019-02-21 05:24:51 +08:00
|
|
|
};
|
|
|
|
|
2018-09-03 22:05:28 +08:00
|
|
|
/**
|
|
|
|
* @method
|
2018-10-07 22:27:12 +08:00
|
|
|
* @param {function} func - callback called on note change as user is typing (not necessarily tied to save event)
|
2018-09-03 22:05:28 +08:00
|
|
|
*/
|
|
|
|
this.onNoteChange = noteDetailService.onNoteChange;
|
2018-10-07 22:27:12 +08:00
|
|
|
|
2018-12-23 05:28:49 +08:00
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* @param {object} $el - jquery object on which to setup the tooltip
|
|
|
|
*/
|
2019-02-21 05:24:51 +08:00
|
|
|
this.setupElementTooltip = noteTooltipService.setupElementTooltip;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
2019-03-13 04:52:19 +08:00
|
|
|
this.protectActiveNote = protectedSessionService.protectNoteAndSendToServer;
|
2019-04-14 18:24:48 +08: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-22 03:22:53 +08: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-06 04:26:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* @param {string} keyboardShortcut - e.g. "ctrl+shift+a"
|
|
|
|
* @param {function} handler
|
|
|
|
*/
|
|
|
|
this.bindGlobalShortcut = utils.bindGlobalShortcut;
|
2018-08-30 02:44:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default FrontendScriptApi;</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
2019-11-19 06:01:31 +08:00
|
|
|
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Branch.html">Branch</a></li><li><a href="FrontendScriptApi.html">FrontendScriptApi</a></li><li><a href="KeyboardAction.html">KeyboardAction</a></li><li><a href="NoteFull.html">NoteFull</a></li><li><a href="NoteShort.html">NoteShort</a></li></ul><h3><a href="global.html">Global</a></h3>
|
2018-08-30 02:44:35 +08:00
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
2019-08-21 03:40:47 +08:00
|
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a>
|
2018-08-30 02:44:35 +08:00
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|