2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-03-26 07:49:33 +08:00
|
|
|
import server from './server.js';
|
2018-04-05 11:51:47 +08:00
|
|
|
import utils from './utils.js';
|
2018-04-08 19:48:47 +08:00
|
|
|
import infoService from './info.js';
|
2018-07-29 22:06:13 +08:00
|
|
|
import linkService from './link.js';
|
2018-08-17 05:00:04 +08:00
|
|
|
import treeCache from './tree_cache.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-08-10 19:30:20 +08:00
|
|
|
function ScriptApi(startNote, currentNote, originEntity = null) {
|
2018-02-15 12:31:20 +08:00
|
|
|
const $pluginButtons = $("#plugin-buttons");
|
2018-02-03 23:37:57 +08:00
|
|
|
|
|
|
|
async function activateNote(notePath) {
|
2018-03-25 09:39:15 +08:00
|
|
|
await treeService.activateNode(notePath);
|
2018-02-03 23:37:57 +08:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:50:04 +08:00
|
|
|
async function activateNewNote(notePath) {
|
|
|
|
await treeService.reload();
|
|
|
|
|
|
|
|
await treeService.activateNode(notePath, true);
|
|
|
|
}
|
|
|
|
|
2018-08-17 02:26:40 +08:00
|
|
|
function addButtonToToolbar(opts) {
|
|
|
|
const buttonId = "toolbar-button-" + opts.title.replace(/[^a-zA-Z0-9]/g, "-");
|
|
|
|
|
2018-02-03 23:37:57 +08:00
|
|
|
$("#" + buttonId).remove();
|
|
|
|
|
2018-08-17 02:26:40 +08:00
|
|
|
const icon = $("<span>")
|
|
|
|
.addClass("ui-icon ui-icon-" + opts.icon);
|
|
|
|
|
|
|
|
const button = $('<button>')
|
|
|
|
.addClass("btn btn-xs")
|
|
|
|
.click(opts.action)
|
|
|
|
.append(icon)
|
|
|
|
.append($("<span>").text(opts.title));
|
|
|
|
|
2018-02-03 23:37:57 +08:00
|
|
|
button.attr('id', buttonId);
|
|
|
|
|
2018-02-15 12:31:20 +08:00
|
|
|
$pluginButtons.append(button);
|
2018-08-17 02:26:40 +08:00
|
|
|
|
|
|
|
if (opts.shortcut) {
|
|
|
|
$(document).bind('keydown', opts.shortcut, opts.action);
|
|
|
|
|
|
|
|
button.attr("title", "Shortcut " + opts.shortcut);
|
|
|
|
}
|
2018-02-03 23:37:57 +08:00
|
|
|
}
|
|
|
|
|
2018-03-05 10:43:14 +08:00
|
|
|
function prepareParams(params) {
|
|
|
|
if (!params) {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
return params.map(p => {
|
|
|
|
if (typeof p === "function") {
|
|
|
|
return "!@#Function: " + p.toString();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
async function runOnServer(script, params = []) {
|
2018-03-05 10:43:14 +08:00
|
|
|
if (typeof script === "function") {
|
|
|
|
script = script.toString();
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:28:26 +08:00
|
|
|
const ret = await server.post('script/exec', {
|
|
|
|
script: script,
|
|
|
|
params: prepareParams(params),
|
2018-03-07 12:04:35 +08:00
|
|
|
startNoteId: startNote.noteId,
|
2018-07-29 22:06:13 +08:00
|
|
|
currentNoteId: currentNote.noteId,
|
2018-08-17 05:00:04 +08:00
|
|
|
originEntityName: "notes", // currently there's no other entity on frontend which can trigger event
|
2018-08-10 19:30:20 +08:00
|
|
|
originEntityId: originEntity ? originEntity.noteId : null
|
2018-03-05 12:28:26 +08:00
|
|
|
});
|
2018-03-05 10:43:14 +08:00
|
|
|
|
|
|
|
return ret.executionResult;
|
|
|
|
}
|
|
|
|
|
2018-02-03 23:37:57 +08:00
|
|
|
return {
|
2018-03-07 12:04:35 +08:00
|
|
|
startNote: startNote,
|
|
|
|
currentNote: currentNote,
|
2018-08-10 19:30:20 +08:00
|
|
|
originEntity: originEntity,
|
2018-08-17 05:00:04 +08:00
|
|
|
// needs to have the longform, can't be shortened!
|
|
|
|
// used also to load many rows to cache before further code starts using them
|
|
|
|
getNotes: async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError),
|
2018-02-03 23:37:57 +08:00
|
|
|
addButtonToToolbar,
|
2018-02-25 10:23:04 +08:00
|
|
|
activateNote,
|
2018-08-14 19:50:04 +08:00
|
|
|
activateNewNote,
|
2018-03-26 10:56:23 +08:00
|
|
|
getInstanceName: () => window.glob.instanceName,
|
2018-04-05 11:51:47 +08:00
|
|
|
runOnServer,
|
2018-04-07 07:08:42 +08:00
|
|
|
formatDateISO: utils.formatDateISO,
|
2018-04-08 01:14:01 +08:00
|
|
|
parseDate: utils.parseDate,
|
2018-04-08 19:48:47 +08:00
|
|
|
showMessage: infoService.showMessage,
|
|
|
|
showError: infoService.showError,
|
2018-08-14 18:54:58 +08:00
|
|
|
reloadTree: treeService.reload, // deprecated
|
|
|
|
refreshTree: treeService.reload,
|
2018-07-29 22:06:13 +08:00
|
|
|
createNoteLink: linkService.createNoteLink
|
2018-02-03 23:37:57 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ScriptApi;
|