trilium/src/public/javascripts/services/script_api.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
import server from './server.js';
2018-04-05 11:51:47 +08:00
import utils from './utils.js';
import infoService from './info.js';
import linkService from './link.js';
2018-08-10 02:08:00 +08:00
function ScriptApi(startNote, currentNote, workEntity = 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
}
function addButtonToToolbar(buttonId, button) {
$("#" + buttonId).remove();
button.attr('id', buttonId);
2018-02-15 12:31:20 +08:00
$pluginButtons.append(button);
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;
}
});
}
async function runOnServer(script, params = []) {
2018-03-05 10:43:14 +08: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-08-10 02:08:00 +08:00
workEntityName: workEntity ? workEntity.constructor.tableName() : null
workEntityId: workEntity ? workEntity.noteId : null
});
2018-03-05 10:43:14 +08:00
return ret.executionResult;
}
2018-02-03 23:37:57 +08:00
return {
startNote: startNote,
currentNote: currentNote,
2018-08-10 02:08:00 +08:00
workEntity: workEntity,
2018-02-03 23:37:57 +08:00
addButtonToToolbar,
activateNote,
getInstanceName: () => window.glob.instanceName,
2018-04-05 11:51:47 +08:00
runOnServer,
formatDateISO: utils.formatDateISO,
2018-04-08 01:14:01 +08:00
parseDate: utils.parseDate,
showMessage: infoService.showMessage,
showError: infoService.showError,
reloadTree: treeService.reload,
createNoteLink: linkService.createNoteLink
2018-02-03 23:37:57 +08:00
}
}
export default ScriptApi;