Files
Trilium/src/public/javascripts/services/script_api.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-03-07 00:17:18 -05:00
function ScriptApi(startNote, currentNote) {
2018-02-14 23:31:20 -05:00
const $pluginButtons = $("#plugin-buttons");
2018-02-03 10:37:57 -05:00
async function activateNote(notePath) {
2018-03-24 21:39:15 -04:00
await treeService.activateNode(notePath);
2018-02-03 10:37:57 -05:00
}
function addButtonToToolbar(buttonId, button) {
$("#" + buttonId).remove();
button.attr('id', buttonId);
2018-02-14 23:31:20 -05:00
$pluginButtons.append(button);
2018-02-03 10:37:57 -05:00
}
2018-03-04 21:43:14 -05:00
function prepareParams(params) {
if (!params) {
return params;
}
return params.map(p => {
if (typeof p === "function") {
return "!@#Function: " + p.toString();
}
else {
return p;
}
});
}
async function runOnServer(script, params = []) {
2018-03-04 21:43:14 -05: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
});
2018-03-04 21:43:14 -05:00
return ret.executionResult;
}
2018-02-03 10:37:57 -05:00
return {
startNote: startNote,
currentNote: currentNote,
2018-02-03 10:37:57 -05:00
addButtonToToolbar,
activateNote,
2018-03-24 21:39:15 -04:00
getInstanceName: treeService.getInstanceName,
2018-03-04 21:43:14 -05:00
runOnServer
2018-02-03 10:37:57 -05:00
}
}
export default ScriptApi;