trilium/src/routes/api/cloning.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-01-14 07:02:41 +08:00
"use strict";
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const tree = require('../../services/tree');
const Branch = require('../../entities/branch');
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
async function cloneNoteToParent(req) {
2018-01-14 07:02:41 +08:00
const parentNoteId = req.params.parentNoteId;
const childNoteId = req.params.childNoteId;
const prefix = req.body.prefix;
2018-03-31 01:20:36 +08:00
const validationResult = await tree.validateParentChild(parentNoteId, childNoteId);
if (!validationResult.success) {
return validationResult;
2018-01-14 07:02:41 +08:00
}
2018-03-25 09:39:15 +08:00
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
2018-01-14 07:02:41 +08:00
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const branch = new Branch({
2018-03-31 01:20:36 +08:00
noteId: childNoteId,
parentNoteId: parentNoteId,
prefix: prefix,
notePosition: newNotePos,
2018-04-02 05:38:24 +08:00
isExpanded: 0
});
2018-01-14 07:02:41 +08:00
await branch.save();
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
return { success: true };
}
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
async function cloneNoteAfter(req) {
2018-01-14 07:02:41 +08:00
const noteId = req.params.noteId;
2018-03-25 09:39:15 +08:00
const afterBranchId = req.params.afterBranchId;
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
const afterNote = await tree.getBranch(afterBranchId);
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
if (!validationResult.result) {
return validationResult;
2018-01-14 07:02:41 +08:00
}
2018-03-31 01:20:36 +08:00
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
2018-01-14 07:02:41 +08:00
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
2018-01-14 07:02:41 +08:00
const branch = new Branch({
2018-03-31 01:20:36 +08:00
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
notePosition: afterNote.notePosition + 1,
2018-04-02 05:38:24 +08:00
isExpanded: 0
});
2018-01-14 07:02:41 +08:00
await branch.save();
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
return { success: true };
}
2018-01-14 07:02:41 +08:00
2018-03-31 01:20:36 +08:00
module.exports = {
cloneNoteToParent,
cloneNoteAfter
};