mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 02:45:54 +01:00
ScriptApi separated from ScriptContext, preparation for modularisation
This commit is contained in:
54
src/public/javascripts/script_api.js
Normal file
54
src/public/javascripts/script_api.js
Normal file
@@ -0,0 +1,54 @@
|
||||
function ScriptApi(startNote, currentNote) {
|
||||
const $pluginButtons = $("#plugin-buttons");
|
||||
|
||||
async function activateNote(notePath) {
|
||||
await treeService.activateNode(notePath);
|
||||
}
|
||||
|
||||
function addButtonToToolbar(buttonId, button) {
|
||||
$("#" + buttonId).remove();
|
||||
|
||||
button.attr('id', buttonId);
|
||||
|
||||
$pluginButtons.append(button);
|
||||
}
|
||||
|
||||
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 = []) {
|
||||
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
|
||||
});
|
||||
|
||||
return ret.executionResult;
|
||||
}
|
||||
|
||||
return {
|
||||
startNote: startNote,
|
||||
currentNote: currentNote,
|
||||
addButtonToToolbar,
|
||||
activateNote,
|
||||
getInstanceName: treeService.getInstanceName,
|
||||
runOnServer
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user