1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-29 22:48:10 +02:00

Clipper: Allow selecting a folder and fixed screenshot taking issue

This commit is contained in:
Laurent Cozic
2018-05-26 15:46:57 +01:00
parent d6c6ef20d4
commit 89b486a3ee
10 changed files with 246 additions and 56 deletions

View File

@@ -161,7 +161,10 @@ class BaseModel {
}
static async all(options = null) {
let q = this.applySqlOptions(options, 'SELECT * FROM `' + this.tableName() + '`');
if (!options) options = {};
if (!options.fields) options.fields = '*';
let q = this.applySqlOptions(options, 'SELECT ' + this.db().escapeFields(options.fields) + ' FROM `' + this.tableName() + '`');
return this.modelSelectAll(q.sql);
}

View File

@@ -64,14 +64,6 @@ class ClipperServer {
});
}
// startState() {
// return this.startState_;
// }
// port() {
// return this.port_;
// }
htmlToMdParser() {
if (this.htmlToMdParser_) return this.htmlToMdParser_;
this.htmlToMdParser_ = new HtmlToMd();
@@ -93,8 +85,8 @@ class ClipperServer {
});
}
if (requestNote.parent_id) {
output.parent_id = requestNote.parent_id;
if (requestNote.parentId) {
output.parent_id = requestNote.parentId;
} else {
const folder = await Folder.defaultFolder();
if (!folder) throw new Error('Cannot find folder for note');
@@ -227,11 +219,11 @@ class ClipperServer {
this.server_ = require('http').createServer();
this.server_.on('request', (request, response) => {
this.server_.on('request', async (request, response) => {
const writeCorsHeaders = (code) => {
const writeCorsHeaders = (code, contentType = "application/json") => {
response.writeHead(code, {
"Content-Type": "application/json",
"Content-Type": contentType,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
'Access-Control-Allow-Headers': 'X-Requested-With,content-type',
@@ -245,7 +237,7 @@ class ClipperServer {
}
const writeResponseText = (code, text) => {
writeCorsHeaders(code);
writeCorsHeaders(code, 'text/plain');
response.write(text);
response.end();
}
@@ -259,6 +251,11 @@ class ClipperServer {
if (url.pathname === '/ping') {
return writeResponseText(200, 'JoplinClipperServer');
}
if (url.pathname === '/folders') {
const structure = await Folder.allAsTree({ fields: ['id', 'parent_id', 'title'] });
return writeResponseJson(200, structure);
}
} else if (request.method === 'POST') {
if (url.pathname === '/notes') {
let body = '';

View File

@@ -127,6 +127,34 @@ class Folder extends BaseItem {
return output;
}
static async allAsTree(options = null) {
const all = await this.all(options);
// https://stackoverflow.com/a/49387427/561309
function getNestedChildren(models, parentId) {
const nestedTreeStructure = [];
const length = models.length;
for (let i = 0; i < length; i++) {
const model = models[i];
if (model.parent_id == parentId) {
const children = getNestedChildren(models, model.id);
if (children.length > 0) {
model.children = children;
}
nestedTreeStructure.push(model);
}
}
return nestedTreeStructure;
}
return getNestedChildren(all, '');
}
static load(id) {
if (id == this.conflictFolderId()) return this.conflictFolder();
return super.load(id);