1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Desktop: API: Add support for external editing from API

This commit is contained in:
Laurent Cozic 2020-06-20 02:30:09 +01:00
parent e38794171a
commit 45160a2e73
2 changed files with 38 additions and 0 deletions

View File

@ -26,6 +26,21 @@ class ExternalEditWatcher {
return this.instance_;
}
externalApi() {
return {
openAndWatch: async ({ noteId }) => {
const note = await Note.load(noteId);
return this.openAndWatch(note);
},
stopWatching: async ({ noteId }) => {
return this.stopWatching(noteId);
},
noteIsWatched: async ({ noteId }) => {
return this.noteIsWatched(noteId);
},
};
}
tempDir() {
return Setting.value('profileDir');
}

View File

@ -25,6 +25,8 @@ const uri2path = require('file-uri-to-path');
const { MarkupToHtml } = require('lib/joplin-renderer');
const { uuid } = require('lib/uuid');
const ExternalEditWatcher = require('lib/services/ExternalEditWatcher');
class ApiError extends Error {
constructor(message, httpCode = 400) {
super(message);
@ -377,6 +379,27 @@ class Api {
return options;
}
async execServiceActionFromRequest_(externalApi, request) {
const action = externalApi[request.action];
if (!action) throw new ErrorNotFound(`Invalid action: ${request.action}`);
const args = Object.assign({}, request);
delete args.action;
return action(args);
}
async action_services(request, serviceName) {
this.checkToken_(request);
if (request.method !== 'POST') throw new ErrorMethodNotAllowed();
if (serviceName === 'externalEditWatcher') {
const externalApi = ExternalEditWatcher.instance().externalApi();
return this.execServiceActionFromRequest_(externalApi, JSON.parse(request.body));
} else {
throw new ErrorNotFound(`No such service: ${serviceName}`);
}
}
async action_notes(request, id = null, link = null) {
this.checkToken_(request);